93b5305fd120d9c50dc0c0846cc8aa909b368b68
[blender.git] / source / blender / editors / interface / interface_panel.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2003-2009 full recode.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /* a full doc with API notes can be found in bf-blender/trunk/blender/doc/guides/interface_API.txt */
29  
30 #include <math.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "PIL_time.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41 #include "BLI_utildefines.h"
42
43 #include "DNA_userdef_types.h"
44
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
47
48 #include "BIF_gl.h"
49 #include "BIF_glutil.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "ED_screen.h"
55
56 #include "UI_interface.h"
57
58 #include "interface_intern.h"
59
60 /*********************** defines and structs ************************/
61
62 #define ANIMATION_TIME          0.30
63 #define ANIMATION_INTERVAL      0.02
64
65 #define PNL_LAST_ADDED          1
66 #define PNL_ACTIVE                      2
67 #define PNL_WAS_ACTIVE          4
68 #define PNL_ANIM_ALIGN          8
69 #define PNL_NEW_ADDED           16
70 #define PNL_FIRST                       32
71
72 typedef enum uiHandlePanelState {
73         PANEL_STATE_DRAG,
74         PANEL_STATE_DRAG_SCALE,
75         PANEL_STATE_WAIT_UNTAB,
76         PANEL_STATE_ANIMATION,
77         PANEL_STATE_EXIT
78 } uiHandlePanelState;
79
80 typedef struct uiHandlePanelData {
81         uiHandlePanelState state;
82
83         /* animation */
84         wmTimer *animtimer;
85         double starttime;
86
87         /* dragging */
88         int startx, starty;
89         int startofsx, startofsy;
90         int startsizex, startsizey;
91 } uiHandlePanelData;
92
93 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state);
94
95 /*********************** space specific code ************************/
96 /* temporary code to remove all sbuts stuff from panel code         */
97
98 static int panel_aligned(ScrArea *sa, ARegion *ar)
99 {
100         if(sa->spacetype==SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
101                 SpaceButs *sbuts= sa->spacedata.first;
102                 return sbuts->align;
103         }
104         else if(sa->spacetype==SPACE_USERPREF && ar->regiontype == RGN_TYPE_WINDOW)
105                 return BUT_VERTICAL;
106         else if(sa->spacetype==SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS)
107                 return BUT_VERTICAL;
108         else if(sa->spacetype==SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW)
109                 return BUT_VERTICAL; 
110         else if(ELEM3(ar->regiontype, RGN_TYPE_UI, RGN_TYPE_TOOLS, RGN_TYPE_TOOL_PROPS))
111                 return BUT_VERTICAL;
112         
113         return 0;
114 }
115
116 static int panels_re_align(ScrArea *sa, ARegion *ar, Panel **r_pa)
117 {
118         Panel *pa;
119         int active= 0;
120
121         *r_pa= NULL;
122
123         if(sa->spacetype==SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
124                 SpaceButs *sbuts= sa->spacedata.first;
125
126                 if(sbuts->align)
127                         if(sbuts->re_align || sbuts->mainbo!=sbuts->mainb)
128                                 return 1;
129         }
130         else if(ar->regiontype==RGN_TYPE_UI)
131                 return 1;
132         else if(sa->spacetype==SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW)
133                 return 1;
134         else if(sa->spacetype==SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS)
135                 return 1;
136
137         /* in case panel is added or disappears */
138         for(pa=ar->panels.first; pa; pa=pa->next) {
139                 if((pa->runtime_flag & PNL_WAS_ACTIVE) && !(pa->runtime_flag & PNL_ACTIVE))
140                         return 1;
141                 if(!(pa->runtime_flag & PNL_WAS_ACTIVE) && (pa->runtime_flag & PNL_ACTIVE))
142                         return 1;
143                 if(pa->activedata)
144                         active= 1;
145         }
146
147         /* in case we need to do an animation (size changes) */
148         for(pa=ar->panels.first; pa; pa=pa->next) {
149                 if(pa->runtime_flag & PNL_ANIM_ALIGN) {
150                         if(!active)
151                                 *r_pa= pa;
152                         return 1;
153                 }
154         }
155         
156         return 0;
157 }
158
159 /****************************** panels ******************************/
160
161 static void ui_panel_copy_offset(Panel *pa, Panel *papar)
162 {
163         /* with respect to sizes... papar is parent */
164
165         pa->ofsx= papar->ofsx;
166         pa->ofsy= papar->ofsy + papar->sizey-pa->sizey;
167 }
168
169 Panel *uiBeginPanel(ScrArea *sa, ARegion *ar, uiBlock *block, PanelType *pt, int *open)
170 {
171         uiStyle *style= U.uistyles.first;
172         Panel *pa, *patab, *palast, *panext;
173         char *drawname= pt->label;
174         char *idname= pt->idname;
175         char *tabname= pt->idname;
176         char *hookname= NULL;
177         int newpanel;
178         int align= panel_aligned(sa, ar);
179         
180         /* check if Panel exists, then use that one */
181         for(pa=ar->panels.first; pa; pa=pa->next)
182                 if(strncmp(pa->panelname, idname, UI_MAX_NAME_STR)==0)
183                         if(strncmp(pa->tabname, tabname, UI_MAX_NAME_STR)==0)
184                                 break;
185         
186         newpanel= (pa == NULL);
187
188         if(!newpanel) {
189                 pa->type= pt;
190         }
191         else {
192                 /* new panel */
193                 pa= MEM_callocN(sizeof(Panel), "new panel");
194                 pa->type= pt;
195                 BLI_strncpy(pa->panelname, idname, UI_MAX_NAME_STR);
196                 BLI_strncpy(pa->tabname, tabname, UI_MAX_NAME_STR);
197
198                 if(pt->flag & PNL_DEFAULT_CLOSED) {
199                         if(align == BUT_VERTICAL)
200                                 pa->flag |= PNL_CLOSEDY;
201                         else
202                                 pa->flag |= PNL_CLOSEDX;
203                 }
204         
205                 pa->ofsx= 0;
206                 pa->ofsy= style->panelouter;
207                 pa->sizex= 0;
208                 pa->sizey= 0;
209                 pa->runtime_flag |= PNL_NEW_ADDED;
210
211                 BLI_addtail(&ar->panels, pa);
212                 
213                 /* make new Panel tabbed? */
214                 if(hookname) {
215                         for(patab= ar->panels.first; patab; patab= patab->next) {
216                                 if((patab->runtime_flag & PNL_ACTIVE) && patab->paneltab==NULL) {
217                                         if(strncmp(hookname, patab->panelname, UI_MAX_NAME_STR)==0) {
218                                                 if(strncmp(tabname, patab->tabname, UI_MAX_NAME_STR)==0) {
219                                                         pa->paneltab= patab;
220                                                         ui_panel_copy_offset(pa, patab);
221                                                         break;
222                                                 }
223                                         }
224                                 }
225                         } 
226                 }
227         }
228
229         BLI_strncpy(pa->drawname, drawname, UI_MAX_NAME_STR);
230
231         /* if a new panel is added, we insert it right after the panel
232          * that was last added. this way new panels are inserted in the
233          * right place between versions */
234         for(palast=ar->panels.first; palast; palast=palast->next)
235                 if(palast->runtime_flag & PNL_LAST_ADDED)
236                         break;
237         
238         if(newpanel) {
239                 pa->sortorder= (palast)? palast->sortorder+1: 0;
240
241                 for(panext=ar->panels.first; panext; panext=panext->next)
242                         if(panext != pa && panext->sortorder >= pa->sortorder)
243                                 panext->sortorder++;
244         }
245
246         if(palast)
247                 palast->runtime_flag &= ~PNL_LAST_ADDED;
248
249         /* assign to block */
250         block->panel= pa;
251         pa->runtime_flag |= PNL_ACTIVE|PNL_LAST_ADDED;
252
253         *open= 0;
254
255         if(pa->paneltab) return pa;
256         if(pa->flag & PNL_CLOSED) return pa;
257
258         *open= 1;
259         
260         return pa;
261 }
262
263 void uiEndPanel(uiBlock *block, int width, int height)
264 {
265         Panel *pa= block->panel;
266
267         if(pa->runtime_flag & PNL_NEW_ADDED) {
268                 pa->runtime_flag &= ~PNL_NEW_ADDED;
269                 pa->sizex= width;
270                 pa->sizey= height;
271         }
272         else {
273                 /* check if we need to do an animation */
274                 if(!ELEM(width, 0, pa->sizex) || !ELEM(height, 0, pa->sizey)) {
275                         pa->runtime_flag |= PNL_ANIM_ALIGN;
276                         if(height != 0)
277                                 pa->ofsy += pa->sizey-height;
278                 }
279
280                 /* update width/height if non-zero */
281                 if(width != 0)
282                         pa->sizex= width;
283                 if(height != 0)
284                         pa->sizey= height;
285         }
286 }
287
288 static void ui_offset_panel_block(uiBlock *block)
289 {
290         uiStyle *style= U.uistyles.first;
291         uiBut *but;
292         int ofsy;
293
294         /* compute bounds and offset */
295         ui_bounds_block(block);
296
297         ofsy= block->panel->sizey - style->panelspace;
298
299         for(but= block->buttons.first; but; but=but->next) {
300                 but->y1 += ofsy;
301                 but->y2 += ofsy;
302         }
303
304         block->maxx= block->panel->sizex;
305         block->maxy= block->panel->sizey;
306         block->minx= block->miny= 0.0;
307 }
308
309 /**************************** drawing *******************************/
310
311 /* extern used by previewrender */
312 static void uiPanelPush(uiBlock *block)
313 {
314         glPushMatrix(); 
315
316         if(block->panel)
317                 glTranslatef((float)block->panel->ofsx, (float)block->panel->ofsy, 0.0);
318 }
319
320 static void uiPanelPop(uiBlock *UNUSED(block))
321 {
322         glPopMatrix();
323 }
324
325 /* triangle 'icon' for panel header */
326 /* NOTE - this seems to be only used for hiding nodes now */
327 void UI_DrawTriIcon(float x, float y, char dir)
328 {
329         if(dir=='h') {
330                 ui_draw_anti_tria( x-3,y-5, x-3,y+5, x+7,y );
331         }
332         else {
333                 ui_draw_anti_tria( x-5,y+3,  x+5,y+3, x,y-7);   
334         }
335 }
336
337 /* triangle 'icon' inside rect */
338 static void ui_draw_tria_rect(rctf *rect, char dir)
339 {
340         if(dir=='h') {
341                 float half= 0.5f*(rect->ymax - rect->ymin);
342                 ui_draw_anti_tria(rect->xmin, rect->ymin, rect->xmin, rect->ymax, rect->xmax, rect->ymin+half);
343         }
344         else {
345                 float half= 0.5f*(rect->xmax - rect->xmin);
346                 ui_draw_anti_tria(rect->xmin, rect->ymax, rect->xmax, rect->ymax, rect->xmin+half, rect->ymin);
347         }
348 }
349
350 static void ui_draw_anti_x(float x1, float y1, float x2, float y2)
351 {
352
353         /* set antialias line */
354         glEnable(GL_LINE_SMOOTH);
355         glEnable(GL_BLEND);
356
357         glLineWidth(2.0);
358         
359         fdrawline(x1, y1, x2, y2);
360         fdrawline(x1, y2, x2, y1);
361         
362         glLineWidth(1.0);
363         
364         glDisable(GL_LINE_SMOOTH);
365         glDisable(GL_BLEND);
366         
367 }
368
369 /* x 'icon' for panel header */
370 static void ui_draw_x_icon(float x, float y)
371 {
372
373         ui_draw_anti_x(x, y, x+9.375, y+9.375);
374
375 }
376
377 #define PNL_ICON        20
378
379 static void ui_draw_panel_scalewidget(rcti *rect)
380 {
381         float xmin, xmax, dx;
382         float ymin, ymax, dy;
383         
384         xmin= rect->xmax-PNL_HEADER+2;
385         xmax= rect->xmax-3;
386         ymin= rect->ymin+3;
387         ymax= rect->ymin+PNL_HEADER-2;
388                 
389         dx= 0.5f*(xmax-xmin);
390         dy= 0.5f*(ymax-ymin);
391         
392         glEnable(GL_BLEND);
393         glColor4ub(255, 255, 255, 50);
394         fdrawline(xmin, ymin, xmax, ymax);
395         fdrawline(xmin+dx, ymin, xmax, ymax-dy);
396         
397         glColor4ub(0, 0, 0, 50);
398         fdrawline(xmin, ymin+1, xmax, ymax+1);
399         fdrawline(xmin+dx, ymin+1, xmax, ymax-dy+1);
400         glDisable(GL_BLEND);
401 }
402
403 static void ui_draw_panel_dragwidget(rctf *rect)
404 {
405         float xmin, xmax, dx;
406         float ymin, ymax, dy;
407         
408         xmin= rect->xmin;
409         xmax= rect->xmax;
410         ymin= rect->ymin;
411         ymax= rect->ymax;
412         
413         dx= 0.333f*(xmax-xmin);
414         dy= 0.333f*(ymax-ymin);
415         
416         glEnable(GL_BLEND);
417         glColor4ub(255, 255, 255, 50);
418         fdrawline(xmin, ymax, xmax, ymin);
419         fdrawline(xmin+dx, ymax, xmax, ymin+dy);
420         fdrawline(xmin+2*dx, ymax, xmax, ymin+2*dy);
421         
422         glColor4ub(0, 0, 0, 50);
423         fdrawline(xmin, ymax+1, xmax, ymin+1);
424         fdrawline(xmin+dx, ymax+1, xmax, ymin+dy+1);
425         fdrawline(xmin+2*dx, ymax+1, xmax, ymin+2*dy+1);
426         glDisable(GL_BLEND);
427 }
428
429
430 static void ui_draw_aligned_panel_header(uiStyle *style, uiBlock *block, rcti *rect, char dir)
431 {
432         Panel *panel= block->panel;
433         rcti hrect;
434         int  pnl_icons;
435         char *activename= panel->drawname[0]?panel->drawname:panel->panelname;
436         
437         /* + 0.001f to avoid flirting with float inaccuracy */
438         if(panel->control & UI_PNL_CLOSE) pnl_icons=(panel->labelofs+2*PNL_ICON+5)/block->aspect + 0.001f;
439         else pnl_icons= (panel->labelofs+PNL_ICON+5)/block->aspect + 0.001f;
440         
441         /* active tab */
442         /* draw text label */
443         UI_ThemeColor(TH_TITLE);
444         
445         hrect= *rect;
446         if(dir == 'h') {
447                 hrect.xmin= rect->xmin+pnl_icons;
448                 uiStyleFontDraw(&style->paneltitle, &hrect, activename);
449         }
450         else {
451                 /* ignore 'pnl_icons', otherwise the text gets offset horizontally 
452                  * + 0.001f to avoid flirting with float inaccuracy
453                  */
454                 hrect.xmin= rect->xmin + (PNL_ICON+5)/block->aspect + 0.001f;
455                 uiStyleFontDrawRotated(&style->paneltitle, &hrect, activename);
456         }
457 }
458
459 static void rectf_scale(rctf *rect, float scale)
460 {
461         float centx= 0.5f*(rect->xmin+rect->xmax);
462         float centy= 0.5f*(rect->ymin+rect->ymax);
463         float sizex= 0.5f*scale*(rect->xmax - rect->xmin);
464         float sizey= 0.5f*scale*(rect->ymax - rect->ymin);
465         
466         rect->xmin= centx - sizex;
467         rect->xmax= centx + sizex;
468         rect->ymin= centy - sizey;
469         rect->ymax= centy + sizey;
470 }
471
472 /* panel integrated in buttonswindow, tool/property lists etc */
473 void ui_draw_aligned_panel(uiStyle *style, uiBlock *block, rcti *rect)
474 {
475         Panel *panel= block->panel;
476         rcti headrect;
477         rctf itemrect;
478         int ofsx;
479         
480         if(panel->paneltab) return;
481         if(panel->type && (panel->type->flag & PNL_NO_HEADER)) return;
482
483         /* calculate header rect */
484         /* + 0.001f to prevent flicker due to float inaccuracy */
485         headrect= *rect;
486         headrect.ymin= headrect.ymax;
487         headrect.ymax= headrect.ymin + floor(PNL_HEADER/block->aspect + 0.001f);
488         
489         if(!(panel->runtime_flag & PNL_FIRST)) {
490                 float minx= rect->xmin+5.0f/block->aspect;
491                 float maxx= rect->xmax-5.0f/block->aspect;
492                 float y= headrect.ymax;
493                 
494                 glEnable(GL_BLEND);
495                 glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
496                 fdrawline(minx, y+1, maxx, y+1);
497                 glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
498                 fdrawline(minx, y, maxx, y);
499                 glDisable(GL_BLEND);
500         }
501         
502         /* horizontal title */
503         if(!(panel->flag & PNL_CLOSEDX)) {
504                 ui_draw_aligned_panel_header(style, block, &headrect, 'h');
505                 
506                 /* itemrect smaller */  
507                 itemrect.xmax= headrect.xmax - 5.0f/block->aspect;
508                 itemrect.xmin= itemrect.xmax - (headrect.ymax-headrect.ymin);
509                 itemrect.ymin= headrect.ymin;
510                 itemrect.ymax= headrect.ymax;
511                 rectf_scale(&itemrect, 0.8f);
512                 ui_draw_panel_dragwidget(&itemrect);
513         }
514         
515         /* if the panel is minimized vertically:
516                 * (------)
517                 */
518         if(panel->flag & PNL_CLOSEDY) {
519                 
520         }
521         else if(panel->flag & PNL_CLOSEDX) {
522                 /* draw vertical title */
523                 ui_draw_aligned_panel_header(style, block, &headrect, 'v');
524         }
525         /* an open panel */
526         else {
527                 
528                 /* in some occasions, draw a border */
529                 if(panel->flag & PNL_SELECT) {
530                         if(panel->control & UI_PNL_SOLID) uiSetRoundBox(15);
531                         else uiSetRoundBox(3);
532                         
533                         UI_ThemeColorShade(TH_BACK, -120);
534                         uiRoundRect(0.5f + rect->xmin, 0.5f + rect->ymin, 0.5f + rect->xmax, 0.5f + headrect.ymax+1, 8);
535                 }
536                 
537                 if(panel->control & UI_PNL_SCALE)
538                         ui_draw_panel_scalewidget(rect);
539         }
540         
541         /* draw optional close icon */
542         
543         ofsx= 6;
544         if(panel->control & UI_PNL_CLOSE) {
545                 
546                 UI_ThemeColor(TH_TEXT);
547                 ui_draw_x_icon(rect->xmin+2+ofsx, rect->ymax+2);
548                 ofsx= 22;
549         }
550         
551         /* draw collapse icon */
552         UI_ThemeColor(TH_TEXT);
553         
554         /* itemrect smaller */  
555         itemrect.xmin= headrect.xmin + 5.0f/block->aspect;
556         itemrect.xmax= itemrect.xmin + (headrect.ymax-headrect.ymin);
557         itemrect.ymin= headrect.ymin;
558         itemrect.ymax= headrect.ymax;
559         
560         rectf_scale(&itemrect, 0.5f);
561         
562         if(panel->flag & PNL_CLOSEDY)
563                 ui_draw_tria_rect(&itemrect, 'h');
564         else if(panel->flag & PNL_CLOSEDX)
565                 ui_draw_tria_rect(&itemrect, 'h');
566         else
567                 ui_draw_tria_rect(&itemrect, 'v');
568         
569         
570 }
571
572 /************************** panel alignment *************************/
573
574 static int get_panel_header(Panel *pa)
575 {
576         if(pa->type && (pa->type->flag & PNL_NO_HEADER))
577                 return 0;
578
579         return PNL_HEADER;
580 }
581
582 static int get_panel_size_y(uiStyle *style, Panel *pa)
583 {
584         if(pa->type && (pa->type->flag & PNL_NO_HEADER))
585                 return pa->sizey;
586
587         return PNL_HEADER + pa->sizey + style->panelouter;
588 }
589
590 /* this function is needed because uiBlock and Panel itself dont
591 change sizey or location when closed */
592 static int get_panel_real_ofsy(Panel *pa)
593 {
594         if(pa->flag & PNL_CLOSEDY) return pa->ofsy+pa->sizey;
595         else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDY)) return pa->ofsy+pa->sizey;
596         else if(pa->paneltab) return pa->paneltab->ofsy;
597         else return pa->ofsy;
598 }
599
600 static int get_panel_real_ofsx(Panel *pa)
601 {
602         if(pa->flag & PNL_CLOSEDX) return pa->ofsx+get_panel_header(pa);
603         else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDX)) return pa->ofsx+get_panel_header(pa);
604         else return pa->ofsx+pa->sizex;
605 }
606
607 typedef struct PanelSort {
608         Panel *pa, *orig;
609 } PanelSort;
610
611 /* note about sorting;
612    the sortorder has a lower value for new panels being added.
613    however, that only works to insert a single panel, when more new panels get
614    added the coordinates of existing panels and the previously stored to-be-insterted
615    panels do not match for sorting */
616
617 static int find_leftmost_panel(const void *a1, const void *a2)
618 {
619         const PanelSort *ps1=a1, *ps2=a2;
620         
621         if(ps1->pa->ofsx > ps2->pa->ofsx) return 1;
622         else if(ps1->pa->ofsx < ps2->pa->ofsx) return -1;
623         else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
624         else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
625
626         return 0;
627 }
628
629
630 static int find_highest_panel(const void *a1, const void *a2)
631 {
632         const PanelSort *ps1=a1, *ps2=a2;
633         
634         /* stick uppermost header-less panels to the top of the region -
635          * prevent them from being sorted */
636         if (ps1->pa->sortorder < ps2->pa->sortorder && ps1->pa->type->flag & PNL_NO_HEADER) return -1;
637         
638         if(ps1->pa->ofsy+ps1->pa->sizey < ps2->pa->ofsy+ps2->pa->sizey) return 1;
639         else if(ps1->pa->ofsy+ps1->pa->sizey > ps2->pa->ofsy+ps2->pa->sizey) return -1;
640         else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
641         else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
642         
643         return 0;
644 }
645
646 static int compare_panel(const void *a1, const void *a2)
647 {
648         const PanelSort *ps1=a1, *ps2=a2;
649         
650         if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
651         else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
652         
653         return 0;
654 }
655
656 /* this doesnt draw */
657 /* returns 1 when it did something */
658 static int uiAlignPanelStep(ScrArea *sa, ARegion *ar, float fac, int drag)
659 {
660         uiStyle *style= U.uistyles.first;
661         Panel *pa;
662         PanelSort *ps, *panelsort, *psnext;
663         int a, tot=0, done;
664         int align= panel_aligned(sa, ar);
665         
666         /* count active, not tabbed panels */
667         for(pa= ar->panels.first; pa; pa= pa->next)
668                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL)
669                         tot++;
670
671         if(tot==0) return 0;
672
673         /* extra; change close direction? */
674         for(pa= ar->panels.first; pa; pa= pa->next) {
675                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
676                         if((pa->flag & PNL_CLOSEDX) && (align==BUT_VERTICAL))
677                                 pa->flag ^= PNL_CLOSED;
678                         else if((pa->flag & PNL_CLOSEDY) && (align==BUT_HORIZONTAL))
679                                 pa->flag ^= PNL_CLOSED;
680                 }
681         }
682
683         /* sort panels */
684         panelsort= MEM_callocN(tot*sizeof(PanelSort), "panelsort");
685         
686         ps= panelsort;
687         for(pa= ar->panels.first; pa; pa= pa->next) {
688                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
689                         ps->pa= MEM_dupallocN(pa);
690                         ps->orig= pa;
691                         ps++;
692                 }
693         }
694         
695         if(drag) {
696                 /* while we are dragging, we sort on location and update sortorder */
697                 if(align==BUT_VERTICAL) 
698                         qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
699                 else
700                         qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
701
702                 for(ps=panelsort, a=0; a<tot; a++, ps++)
703                         ps->orig->sortorder= a;
704         }
705         else
706                 /* otherwise use sortorder */
707                 qsort(panelsort, tot, sizeof(PanelSort), compare_panel);
708         
709         /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
710         ps= panelsort;
711         ps->pa->ofsx= 0;
712         ps->pa->ofsy= -get_panel_size_y(style, ps->pa);
713
714         for(a=0; a<tot-1; a++, ps++) {
715                 psnext= ps+1;
716         
717                 if(align==BUT_VERTICAL) {
718                         psnext->pa->ofsx= ps->pa->ofsx;
719                         psnext->pa->ofsy= get_panel_real_ofsy(ps->pa) - get_panel_size_y(style, psnext->pa);
720                 }
721                 else {
722                         psnext->pa->ofsx= get_panel_real_ofsx(ps->pa);
723                         psnext->pa->ofsy= ps->pa->ofsy + get_panel_size_y(style, ps->pa) - get_panel_size_y(style, psnext->pa);
724                 }
725         }
726         
727         /* we interpolate */
728         done= 0;
729         ps= panelsort;
730         for(a=0; a<tot; a++, ps++) {
731                 if((ps->pa->flag & PNL_SELECT)==0) {
732                         if((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) {
733                                 ps->orig->ofsx= floor(0.5 + fac*ps->pa->ofsx + (1.0-fac)*ps->orig->ofsx);
734                                 ps->orig->ofsy= floor(0.5 + fac*ps->pa->ofsy + (1.0-fac)*ps->orig->ofsy);
735                                 done= 1;
736                         }
737                 }
738         }
739
740         /* copy locations to tabs */
741         for(pa= ar->panels.first; pa; pa= pa->next)
742                 if(pa->paneltab && (pa->runtime_flag & PNL_ACTIVE))
743                         ui_panel_copy_offset(pa, pa->paneltab);
744
745         /* free panelsort array */
746         for(ps= panelsort, a=0; a<tot; a++, ps++)
747                 MEM_freeN(ps->pa);
748         MEM_freeN(panelsort);
749         
750         return done;
751 }
752
753
754 static void ui_do_animate(const bContext *C, Panel *panel)
755 {
756         uiHandlePanelData *data= panel->activedata;
757         ScrArea *sa= CTX_wm_area(C);
758         ARegion *ar= CTX_wm_region(C);
759         float fac;
760
761         fac= (PIL_check_seconds_timer()-data->starttime)/ANIMATION_TIME;
762         fac= sqrt(fac);
763         fac= MIN2(fac, 1.0f);
764
765         /* for max 1 second, interpolate positions */
766         if(uiAlignPanelStep(sa, ar, fac, 0))
767                 ED_region_tag_redraw(ar);
768         else
769                 fac= 1.0f;
770
771         if(fac >= 1.0f) {
772                 panel_activate_state(C, panel, PANEL_STATE_EXIT);
773                 return;
774         }
775 }
776
777 void uiBeginPanels(const bContext *UNUSED(C), ARegion *ar)
778 {
779         Panel *pa;
780   
781           /* set all panels as inactive, so that at the end we know
782          * which ones were used */
783         for(pa=ar->panels.first; pa; pa=pa->next) {
784                 if(pa->runtime_flag & PNL_ACTIVE)
785                         pa->runtime_flag= PNL_WAS_ACTIVE;
786                 else
787                         pa->runtime_flag= 0;
788         }
789 }
790
791 /* only draws blocks with panels */
792 void uiEndPanels(const bContext *C, ARegion *ar)
793 {
794         ScrArea *sa= CTX_wm_area(C);
795         uiBlock *block;
796         Panel *panot, *panew, *patest, *pa, *firstpa;
797         
798         /* offset contents */
799         for(block= ar->uiblocks.first; block; block= block->next)
800                 if(block->active && block->panel)
801                         ui_offset_panel_block(block);
802
803         /* consistency; are panels not made, whilst they have tabs */
804         for(panot= ar->panels.first; panot; panot= panot->next) {
805                 if((panot->runtime_flag & PNL_ACTIVE)==0) { // not made
806
807                         for(panew= ar->panels.first; panew; panew= panew->next) {
808                                 if((panew->runtime_flag & PNL_ACTIVE)) {
809                                         if(panew->paneltab==panot) { // panew is tab in notmade pa
810                                                 break;
811                                         }
812                                 }
813                         }
814                         /* now panew can become the new parent, check all other tabs */
815                         if(panew) {
816                                 for(patest= ar->panels.first; patest; patest= patest->next) {
817                                         if(patest->paneltab == panot) {
818                                                 patest->paneltab= panew;
819                                         }
820                                 }
821                                 panot->paneltab= panew;
822                                 panew->paneltab= NULL;
823                                 ED_region_tag_redraw(ar); // the buttons panew were not made
824                         }
825                 }       
826         }
827
828         /* re-align, possibly with animation */
829         if(panels_re_align(sa, ar, &pa)) {
830                 if(pa)
831                         panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
832                 else
833                         uiAlignPanelStep(sa, ar, 1.0, 0);
834         }
835
836         /* tag first panel */
837         firstpa= NULL;
838         for(block= ar->uiblocks.first; block; block=block->next)
839                 if(block->active && block->panel)
840                         if(!firstpa || block->panel->sortorder < firstpa->sortorder)
841                                 firstpa= block->panel;
842         
843         if(firstpa)
844                 firstpa->runtime_flag |= PNL_FIRST;
845
846         UI_ThemeClearColor(TH_BACK);
847         
848         /* draw panels, selected on top */
849         for(block= ar->uiblocks.first; block; block=block->next) {
850                 if(block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
851                         uiDrawBlock(C, block);
852                 }
853         }
854
855         for(block= ar->uiblocks.first; block; block=block->next) {
856                 if(block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
857                         uiDrawBlock(C, block);
858                 }
859         }
860 }
861
862 /* ------------ panel merging ---------------- */
863
864 static void check_panel_overlap(ARegion *ar, Panel *panel)
865 {
866         Panel *pa;
867
868         /* also called with panel==NULL for clear */
869         
870         for(pa=ar->panels.first; pa; pa=pa->next) {
871                 pa->flag &= ~PNL_OVERLAP;
872                 if(panel && (pa != panel)) {
873                         if(pa->paneltab==NULL && (pa->runtime_flag & PNL_ACTIVE)) {
874                                 float safex= 0.2, safey= 0.2;
875                                 
876                                 if(pa->flag & PNL_CLOSEDX) safex= 0.05;
877                                 else if(pa->flag & PNL_CLOSEDY) safey= 0.05;
878                                 else if(panel->flag & PNL_CLOSEDX) safex= 0.05;
879                                 else if(panel->flag & PNL_CLOSEDY) safey= 0.05;
880                                 
881                                 if(pa->ofsx > panel->ofsx- safex*panel->sizex)
882                                 if(pa->ofsx+pa->sizex < panel->ofsx+ (1.0+safex)*panel->sizex)
883                                 if(pa->ofsy > panel->ofsy- safey*panel->sizey)
884                                 if(pa->ofsy+pa->sizey < panel->ofsy+ (1.0+safey)*panel->sizey)
885                                         pa->flag |= PNL_OVERLAP;
886                         }
887                 }
888         }
889 }
890
891 /************************ panel dragging ****************************/
892
893 static void ui_do_drag(const bContext *C, wmEvent *event, Panel *panel)
894 {
895         uiHandlePanelData *data= panel->activedata;
896         ScrArea *sa= CTX_wm_area(C);
897         ARegion *ar= CTX_wm_region(C);
898         short align= panel_aligned(sa, ar), dx=0, dy=0;
899         
900         /* first clip for window, no dragging outside */
901         if(!BLI_in_rcti(&ar->winrct, event->x, event->y))
902                 return;
903
904         dx= (event->x-data->startx) & ~(PNL_GRID-1);
905         dy= (event->y-data->starty) & ~(PNL_GRID-1);
906
907         dx *= (float)(ar->v2d.cur.xmax - ar->v2d.cur.xmin)/(float)(ar->winrct.xmax - ar->winrct.xmin);
908         dy *= (float)(ar->v2d.cur.ymax - ar->v2d.cur.ymin)/(float)(ar->winrct.ymax - ar->winrct.ymin);
909         
910         if(data->state == PANEL_STATE_DRAG_SCALE) {
911                 panel->sizex = MAX2(data->startsizex+dx, UI_PANEL_MINX);
912                 
913                 if(data->startsizey-dy < UI_PANEL_MINY)
914                         dy= -UI_PANEL_MINY+data->startsizey;
915
916                 panel->sizey= data->startsizey-dy;
917                 panel->ofsy= data->startofsy+dy;
918         }
919         else {
920                 /* reset the panel snapping, to allow dragging away from snapped edges */
921                 panel->snap = PNL_SNAP_NONE;
922                 
923                 panel->ofsx = data->startofsx+dx;
924                 panel->ofsy = data->startofsy+dy;
925                 check_panel_overlap(ar, panel);
926                 
927                 if(align) uiAlignPanelStep(sa, ar, 0.2, 1);
928         }
929
930         ED_region_tag_redraw(ar);
931 }
932
933 /******************* region level panel interaction *****************/
934
935
936 /* this function is supposed to call general window drawing too */
937 /* also it supposes a block has panel, and isnt a menu */
938 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my, int event)
939 {
940         ScrArea *sa= CTX_wm_area(C);
941         ARegion *ar= CTX_wm_region(C);
942         Panel *pa;
943         int align= panel_aligned(sa, ar), button= 0;
944
945         /* mouse coordinates in panel space! */
946         
947         /* XXX weak code, currently it assumes layout style for location of widgets */
948         
949         /* check open/collapsed button */
950         if(event==RETKEY)
951                 button= 1;
952         else if(event==AKEY)
953                 button= 1;
954         else if(block->panel->flag & PNL_CLOSEDX) {
955                 if(my >= block->maxy) button= 1;
956         }
957         else if(block->panel->control & UI_PNL_CLOSE) {
958                 /* whole of header can be used to collapse panel (except top-right corner) */
959                 if(mx <= block->maxx-8-PNL_ICON) button= 2;
960                 //else if(mx <= block->minx+10+2*PNL_ICON+2) button= 1;
961         }
962         else if(mx <= block->maxx-PNL_ICON-12) {
963                 button= 1;
964         }
965         
966         if(button) {
967                 if(button==2) { // close
968                         ED_region_tag_redraw(ar);
969                 }
970                 else {  // collapse
971                         if(block->panel->flag & PNL_CLOSED) {
972                                 block->panel->flag &= ~PNL_CLOSED;
973                                 /* snap back up so full panel aligns with screen edge */
974                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
975                                         block->panel->ofsy= 0;
976                         }
977                         else if(align==BUT_HORIZONTAL) {
978                                 block->panel->flag |= PNL_CLOSEDX;
979                         }
980                         else {
981                                 /* snap down to bottom screen edge*/
982                                 block->panel->flag |= PNL_CLOSEDY;
983                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
984                                         block->panel->ofsy= -block->panel->sizey;
985                         }
986                         
987                         for(pa= ar->panels.first; pa; pa= pa->next) {
988                                 if(pa->paneltab==block->panel) {
989                                         if(block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
990                                         else pa->flag &= ~PNL_CLOSED;
991                                 }
992                         }
993                 }
994
995                 if(align)
996                         panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
997                 else
998                         ED_region_tag_redraw(ar);
999         }
1000         else if(mx <= (block->maxx-PNL_ICON-12)+PNL_ICON+2) {
1001                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1002         }
1003 }
1004
1005 /* XXX should become modal keymap */
1006 /* AKey is opening/closing panels, independent of button state now */
1007
1008 int ui_handler_panel_region(bContext *C, wmEvent *event)
1009 {
1010         ARegion *ar= CTX_wm_region(C);
1011         uiBlock *block;
1012         Panel *pa;
1013         int retval, mx, my, inside_header= 0, inside_scale= 0, inside;
1014
1015         retval= WM_UI_HANDLER_CONTINUE;
1016         for(block=ar->uiblocks.last; block; block=block->prev) {
1017                 mx= event->x;
1018                 my= event->y;
1019                 ui_window_to_block(ar, block, &mx, &my);
1020
1021                 /* check if inside boundbox */
1022                 inside= 0;
1023                 pa= block->panel;
1024
1025                 if(!pa || pa->paneltab!=NULL)
1026                         continue;
1027                 if(pa->type && pa->type->flag & PNL_NO_HEADER) // XXX - accessed freed panels when scripts reload, need to fix.
1028                         continue;
1029
1030                 if(block->minx <= mx && block->maxx >= mx)
1031                         if(block->miny <= my && block->maxy+PNL_HEADER >= my)
1032                                 inside= 1;
1033                 
1034                 if(inside && event->val==KM_PRESS) {
1035                         if(event->type == AKEY && !ELEM4(1, event->ctrl, event->oskey, event->shift, event->alt)) {
1036                                 
1037                                 if(pa->flag & PNL_CLOSEDY) {
1038                                         if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my))
1039                                                 ui_handle_panel_header(C, block, mx, my, event->type);
1040                                 }
1041                                 else
1042                                         ui_handle_panel_header(C, block, mx, my, event->type);
1043                                 
1044                                 continue;
1045                         }
1046                 }
1047                 
1048                 /* on active button, do not handle panels */
1049                 if(ui_button_is_active(ar))
1050                         continue;
1051                 
1052                 if(inside) {
1053                         /* clicked at panel header? */
1054                         if(pa->flag & PNL_CLOSEDX) {
1055                                 if(block->minx <= mx && block->minx+PNL_HEADER >= mx) 
1056                                         inside_header= 1;
1057                         }
1058                         else if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my)) {
1059                                 inside_header= 1;
1060                         }
1061                         else if(pa->control & UI_PNL_SCALE) {
1062                                 if(block->maxx-PNL_HEADER <= mx)
1063                                         if(block->miny+PNL_HEADER >= my)
1064                                                 inside_scale= 1;
1065                         }
1066
1067                         if(event->val==KM_PRESS) {
1068                                 /* open close on header */
1069                                 if(ELEM(event->type, RETKEY, PADENTER)) {
1070                                         if(inside_header) {
1071                                                 ui_handle_panel_header(C, block, mx, my, RETKEY);
1072                                                 break;
1073                                         }
1074                                 }
1075                                 else if(event->type == LEFTMOUSE) {
1076                                         if(inside_header) {
1077                                                 ui_handle_panel_header(C, block, mx, my, 0);
1078                                                 break;
1079                                         }
1080                                         else if(inside_scale && !(pa->flag & PNL_CLOSED)) {
1081                                                 panel_activate_state(C, pa, PANEL_STATE_DRAG_SCALE);
1082                                                 break;
1083                                         }
1084                                 }
1085                                 else if(event->type == ESCKEY) {
1086                                         /*XXX 2.50 if(block->handler) {
1087                                                 rem_blockhandler(sa, block->handler);
1088                                                 ED_region_tag_redraw(ar);
1089                                                 retval= WM_UI_HANDLER_BREAK;
1090                                         }*/
1091                                 }
1092                                 else if(event->type==PADPLUSKEY || event->type==PADMINUS) {
1093                                         int zoom=0;
1094                                 
1095                                         /* if panel is closed, only zoom if mouse is over the header */
1096                                         if (pa->flag & (PNL_CLOSEDX|PNL_CLOSEDY)) {
1097                                                 if (inside_header)
1098                                                         zoom=1;
1099                                         }
1100                                         else
1101                                                 zoom=1;
1102
1103 #if 0 // XXX make float panel exception?
1104                                         if(zoom) {
1105                                                 ScrArea *sa= CTX_wm_area(C);
1106                                                 SpaceLink *sl= sa->spacedata.first;
1107
1108                                                 if(sa->spacetype!=SPACE_BUTS) {
1109                                                         if(!(pa->control & UI_PNL_SCALE)) {
1110                                                                 if(event->type==PADPLUSKEY) sl->blockscale+= 0.1;
1111                                                                 else sl->blockscale-= 0.1;
1112                                                                 CLAMP(sl->blockscale, 0.6, 1.0);
1113
1114                                                                 ED_region_tag_redraw(ar);
1115                                                                 retval= WM_UI_HANDLER_BREAK;
1116                                                         }                                               
1117                                                 }
1118                                         }
1119 #endif
1120                                 }
1121                         }
1122                 }
1123         }
1124
1125         return retval;
1126 }
1127
1128 /**************** window level modal panel interaction **************/
1129
1130 /* note, this is modal handler and should not swallow events for animation */
1131 static int ui_handler_panel(bContext *C, wmEvent *event, void *userdata)
1132 {
1133         Panel *panel= userdata;
1134         uiHandlePanelData *data= panel->activedata;
1135
1136         /* verify if we can stop */
1137         if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1138                 ScrArea *sa= CTX_wm_area(C);
1139                 ARegion *ar= CTX_wm_region(C);
1140                 int align= panel_aligned(sa, ar);
1141
1142                 if(align)
1143                         panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1144                 else
1145                         panel_activate_state(C, panel, PANEL_STATE_EXIT);
1146         }
1147         else if(event->type == MOUSEMOVE) {
1148                 if(data->state == PANEL_STATE_DRAG)
1149                         ui_do_drag(C, event, panel);
1150         }
1151         else if(event->type == TIMER && event->customdata == data->animtimer) {
1152                 if(data->state == PANEL_STATE_ANIMATION)
1153                         ui_do_animate(C, panel);
1154                 else if(data->state == PANEL_STATE_DRAG)
1155                         ui_do_drag(C, event, panel);
1156         }
1157
1158         data= panel->activedata;
1159
1160         if(data && data->state == PANEL_STATE_ANIMATION)
1161                 return WM_UI_HANDLER_CONTINUE;
1162         else
1163                 return WM_UI_HANDLER_BREAK;
1164 }
1165
1166 static void ui_handler_remove_panel(bContext *C, void *userdata)
1167 {
1168         Panel *pa= userdata;
1169
1170         panel_activate_state(C, pa, PANEL_STATE_EXIT);
1171 }
1172
1173 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
1174 {
1175         uiHandlePanelData *data= pa->activedata;
1176         wmWindow *win= CTX_wm_window(C);
1177         ARegion *ar= CTX_wm_region(C);
1178         
1179         if(data && data->state == state)
1180                 return;
1181
1182         if(state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1183                 if(data && data->state != PANEL_STATE_ANIMATION) {
1184                         /* XXX:
1185                          *      - the panel tabbing function call below (test_add_new_tabs()) has been commented out
1186                          *        "It is too easy to do by accident when reordering panels, is very hard to control and use, and has no real benefit." - BillRey
1187                          * Aligorith, 2009Sep
1188                          */
1189                         //test_add_new_tabs(ar);   // also copies locations of tabs in dragged panel
1190                         check_panel_overlap(ar, NULL);  // clears
1191                 }
1192
1193                 pa->flag &= ~PNL_SELECT;
1194         }
1195         else
1196                 pa->flag |= PNL_SELECT;
1197
1198         if(data && data->animtimer) {
1199                 WM_event_remove_timer(CTX_wm_manager(C), win, data->animtimer);
1200                 data->animtimer= NULL;
1201         }
1202
1203         if(state == PANEL_STATE_EXIT) {
1204                 MEM_freeN(data);
1205                 pa->activedata= NULL;
1206
1207                 WM_event_remove_ui_handler(&win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa);
1208         }
1209         else {
1210                 if(!data) {
1211                         data= MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1212                         pa->activedata= data;
1213
1214                         WM_event_add_ui_handler(C, &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa);
1215                 }
1216
1217                 if(ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1218                         data->animtimer= WM_event_add_timer(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
1219
1220                 data->state= state;
1221                 data->startx= win->eventstate->x;
1222                 data->starty= win->eventstate->y;
1223                 data->startofsx= pa->ofsx;
1224                 data->startofsy= pa->ofsy;
1225                 data->startsizex= pa->sizex;
1226                 data->startsizey= pa->sizey;
1227                 data->starttime= PIL_check_seconds_timer();
1228         }
1229
1230         ED_region_tag_redraw(ar);
1231
1232         /* XXX exception handling, 3d window preview panel */
1233         /* if(block->drawextra==BIF_view3d_previewdraw)
1234                 BIF_view3d_previewrender_clear(curarea);*/
1235         
1236         /* XXX exception handling, 3d window preview panel */
1237         /* if(block->drawextra==BIF_view3d_previewdraw)
1238                 BIF_view3d_previewrender_signal(curarea, PR_DISPRECT);
1239         else if(strcmp(block->name, "image_panel_preview")==0)
1240                 image_preview_event(2); */
1241 }
1242