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