Cycles: svn merge -r41225:41232 ^/trunk/blender
[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, 20);
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, 20);
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         {
498                 float minx= rect->xmin + 0.0f/block->aspect;
499                 float maxx= rect->xmax - 0.0f/block->aspect;
500                 float y= headrect.ymax;
501                 
502                 glEnable(GL_BLEND);
503                 glColor4f(0.0f, 0.0f, 0.0f, 0.1f);
504                 glRectf(minx, y-1, maxx, y);
505                 glColor4f(0.0f, 0.0f, 0.0f, 0.1f);
506                 glRectf(minx, headrect.ymin, 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_NONE);
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.35f);
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 *UNUSED(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;
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
855 void uiDrawPanels(const bContext *C, ARegion *ar)
856 {
857         uiBlock *block;
858
859         UI_ThemeClearColor(TH_BACK);
860         
861         /* draw panels, selected on top */
862         for(block= ar->uiblocks.first; block; block=block->next) {
863                 if(block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
864                         uiDrawBlock(C, block);
865                 }
866         }
867
868         for(block= ar->uiblocks.first; block; block=block->next) {
869                 if(block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
870                         uiDrawBlock(C, block);
871                 }
872         }
873 }
874
875 /* ------------ panel merging ---------------- */
876
877 static void check_panel_overlap(ARegion *ar, Panel *panel)
878 {
879         Panel *pa;
880
881         /* also called with panel==NULL for clear */
882         
883         for(pa=ar->panels.first; pa; pa=pa->next) {
884                 pa->flag &= ~PNL_OVERLAP;
885                 if(panel && (pa != panel)) {
886                         if(pa->paneltab==NULL && (pa->runtime_flag & PNL_ACTIVE)) {
887                                 float safex= 0.2, safey= 0.2;
888                                 
889                                 if(pa->flag & PNL_CLOSEDX) safex= 0.05;
890                                 else if(pa->flag & PNL_CLOSEDY) safey= 0.05;
891                                 else if(panel->flag & PNL_CLOSEDX) safex= 0.05;
892                                 else if(panel->flag & PNL_CLOSEDY) safey= 0.05;
893                                 
894                                 if(pa->ofsx > panel->ofsx- safex*panel->sizex)
895                                 if(pa->ofsx+pa->sizex < panel->ofsx+ (1.0f+safex)*panel->sizex)
896                                 if(pa->ofsy > panel->ofsy- safey*panel->sizey)
897                                 if(pa->ofsy+pa->sizey < panel->ofsy+ (1.0f+safey)*panel->sizey)
898                                         pa->flag |= PNL_OVERLAP;
899                         }
900                 }
901         }
902 }
903
904 /************************ panel dragging ****************************/
905
906 static void ui_do_drag(const bContext *C, wmEvent *event, Panel *panel)
907 {
908         uiHandlePanelData *data= panel->activedata;
909         ScrArea *sa= CTX_wm_area(C);
910         ARegion *ar= CTX_wm_region(C);
911         short align= panel_aligned(sa, ar), dx=0, dy=0;
912         
913         /* first clip for window, no dragging outside */
914         if(!BLI_in_rcti(&ar->winrct, event->x, event->y))
915                 return;
916
917         dx= (event->x-data->startx) & ~(PNL_GRID-1);
918         dy= (event->y-data->starty) & ~(PNL_GRID-1);
919
920         dx *= (float)(ar->v2d.cur.xmax - ar->v2d.cur.xmin)/(float)(ar->winrct.xmax - ar->winrct.xmin);
921         dy *= (float)(ar->v2d.cur.ymax - ar->v2d.cur.ymin)/(float)(ar->winrct.ymax - ar->winrct.ymin);
922         
923         if(data->state == PANEL_STATE_DRAG_SCALE) {
924                 panel->sizex = MAX2(data->startsizex+dx, UI_PANEL_MINX);
925                 
926                 if(data->startsizey-dy < UI_PANEL_MINY)
927                         dy= -UI_PANEL_MINY+data->startsizey;
928
929                 panel->sizey= data->startsizey-dy;
930                 panel->ofsy= data->startofsy+dy;
931         }
932         else {
933                 /* reset the panel snapping, to allow dragging away from snapped edges */
934                 panel->snap = PNL_SNAP_NONE;
935                 
936                 panel->ofsx = data->startofsx+dx;
937                 panel->ofsy = data->startofsy+dy;
938                 check_panel_overlap(ar, panel);
939                 
940                 if(align) uiAlignPanelStep(sa, ar, 0.2, 1);
941         }
942
943         ED_region_tag_redraw(ar);
944 }
945
946 /******************* region level panel interaction *****************/
947
948
949 /* this function is supposed to call general window drawing too */
950 /* also it supposes a block has panel, and isnt a menu */
951 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my, int event)
952 {
953         ScrArea *sa= CTX_wm_area(C);
954         ARegion *ar= CTX_wm_region(C);
955         Panel *pa;
956         int align= panel_aligned(sa, ar), button= 0;
957
958         /* mouse coordinates in panel space! */
959         
960         /* XXX weak code, currently it assumes layout style for location of widgets */
961         
962         /* check open/collapsed button */
963         if(event==RETKEY)
964                 button= 1;
965         else if(event==AKEY)
966                 button= 1;
967         else if(block->panel->flag & PNL_CLOSEDX) {
968                 if(my >= block->maxy) button= 1;
969         }
970         else if(block->panel->control & UI_PNL_CLOSE) {
971                 /* whole of header can be used to collapse panel (except top-right corner) */
972                 if(mx <= block->maxx-8-PNL_ICON) button= 2;
973                 //else if(mx <= block->minx+10+2*PNL_ICON+2) button= 1;
974         }
975         else if(mx <= block->maxx-PNL_ICON-12) {
976                 button= 1;
977         }
978         
979         if(button) {
980                 if(button==2) { // close
981                         ED_region_tag_redraw(ar);
982                 }
983                 else {  // collapse
984                         if(block->panel->flag & PNL_CLOSED) {
985                                 block->panel->flag &= ~PNL_CLOSED;
986                                 /* snap back up so full panel aligns with screen edge */
987                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
988                                         block->panel->ofsy= 0;
989                         }
990                         else if(align==BUT_HORIZONTAL) {
991                                 block->panel->flag |= PNL_CLOSEDX;
992                         }
993                         else {
994                                 /* snap down to bottom screen edge*/
995                                 block->panel->flag |= PNL_CLOSEDY;
996                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
997                                         block->panel->ofsy= -block->panel->sizey;
998                         }
999                         
1000                         for(pa= ar->panels.first; pa; pa= pa->next) {
1001                                 if(pa->paneltab==block->panel) {
1002                                         if(block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1003                                         else pa->flag &= ~PNL_CLOSED;
1004                                 }
1005                         }
1006                 }
1007
1008                 if(align)
1009                         panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1010                 else
1011                         ED_region_tag_redraw(ar);
1012         }
1013         else if(mx <= (block->maxx-PNL_ICON-12)+PNL_ICON+2) {
1014                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1015         }
1016 }
1017
1018 /* XXX should become modal keymap */
1019 /* AKey is opening/closing panels, independent of button state now */
1020
1021 int ui_handler_panel_region(bContext *C, wmEvent *event)
1022 {
1023         ARegion *ar= CTX_wm_region(C);
1024         uiBlock *block;
1025         Panel *pa;
1026         int retval, mx, my, inside_header= 0, inside_scale= 0, inside;
1027
1028         retval= WM_UI_HANDLER_CONTINUE;
1029         for(block=ar->uiblocks.last; block; block=block->prev) {
1030                 mx= event->x;
1031                 my= event->y;
1032                 ui_window_to_block(ar, block, &mx, &my);
1033
1034                 /* check if inside boundbox */
1035                 inside= 0;
1036                 pa= block->panel;
1037
1038                 if(!pa || pa->paneltab!=NULL)
1039                         continue;
1040                 if(pa->type && pa->type->flag & PNL_NO_HEADER) // XXX - accessed freed panels when scripts reload, need to fix.
1041                         continue;
1042
1043                 if(block->minx <= mx && block->maxx >= mx)
1044                         if(block->miny <= my && block->maxy+PNL_HEADER >= my)
1045                                 inside= 1;
1046                 
1047                 if(inside && event->val==KM_PRESS) {
1048                         if(event->type == AKEY && !ELEM4(KM_MOD_FIRST, event->ctrl, event->oskey, event->shift, event->alt)) {
1049                                 
1050                                 if(pa->flag & PNL_CLOSEDY) {
1051                                         if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my))
1052                                                 ui_handle_panel_header(C, block, mx, my, event->type);
1053                                 }
1054                                 else
1055                                         ui_handle_panel_header(C, block, mx, my, event->type);
1056                                 
1057                                 continue;
1058                         }
1059                 }
1060                 
1061                 /* on active button, do not handle panels */
1062                 if(ui_button_is_active(ar))
1063                         continue;
1064                 
1065                 if(inside) {
1066                         /* clicked at panel header? */
1067                         if(pa->flag & PNL_CLOSEDX) {
1068                                 if(block->minx <= mx && block->minx+PNL_HEADER >= mx) 
1069                                         inside_header= 1;
1070                         }
1071                         else if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my)) {
1072                                 inside_header= 1;
1073                         }
1074                         else if(pa->control & UI_PNL_SCALE) {
1075                                 if(block->maxx-PNL_HEADER <= mx)
1076                                         if(block->miny+PNL_HEADER >= my)
1077                                                 inside_scale= 1;
1078                         }
1079
1080                         if(event->val==KM_PRESS) {
1081                                 /* open close on header */
1082                                 if(ELEM(event->type, RETKEY, PADENTER)) {
1083                                         if(inside_header) {
1084                                                 ui_handle_panel_header(C, block, mx, my, RETKEY);
1085                                                 break;
1086                                         }
1087                                 }
1088                                 else if(event->type == LEFTMOUSE) {
1089                                         if(inside_header) {
1090                                                 ui_handle_panel_header(C, block, mx, my, 0);
1091                                                 break;
1092                                         }
1093                                         else if(inside_scale && !(pa->flag & PNL_CLOSED)) {
1094                                                 panel_activate_state(C, pa, PANEL_STATE_DRAG_SCALE);
1095                                                 break;
1096                                         }
1097                                 }
1098                                 else if(event->type == ESCKEY) {
1099                                         /*XXX 2.50 if(block->handler) {
1100                                                 rem_blockhandler(sa, block->handler);
1101                                                 ED_region_tag_redraw(ar);
1102                                                 retval= WM_UI_HANDLER_BREAK;
1103                                         }*/
1104                                 }
1105                                 else if(event->type==PADPLUSKEY || event->type==PADMINUS) {
1106 #if 0 // XXX make float panel exception?
1107                                         int zoom=0;
1108                                 
1109                                         /* if panel is closed, only zoom if mouse is over the header */
1110                                         if (pa->flag & (PNL_CLOSEDX|PNL_CLOSEDY)) {
1111                                                 if (inside_header)
1112                                                         zoom=1;
1113                                         }
1114                                         else
1115                                                 zoom=1;
1116
1117                                         if(zoom) {
1118                                                 ScrArea *sa= CTX_wm_area(C);
1119                                                 SpaceLink *sl= sa->spacedata.first;
1120
1121                                                 if(sa->spacetype!=SPACE_BUTS) {
1122                                                         if(!(pa->control & UI_PNL_SCALE)) {
1123                                                                 if(event->type==PADPLUSKEY) sl->blockscale+= 0.1;
1124                                                                 else sl->blockscale-= 0.1;
1125                                                                 CLAMP(sl->blockscale, 0.6, 1.0);
1126
1127                                                                 ED_region_tag_redraw(ar);
1128                                                                 retval= WM_UI_HANDLER_BREAK;
1129                                                         }                                               
1130                                                 }
1131                                         }
1132 #endif
1133                                 }
1134                         }
1135                 }
1136         }
1137
1138         return retval;
1139 }
1140
1141 /**************** window level modal panel interaction **************/
1142
1143 /* note, this is modal handler and should not swallow events for animation */
1144 static int ui_handler_panel(bContext *C, wmEvent *event, void *userdata)
1145 {
1146         Panel *panel= userdata;
1147         uiHandlePanelData *data= panel->activedata;
1148
1149         /* verify if we can stop */
1150         if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1151                 ScrArea *sa= CTX_wm_area(C);
1152                 ARegion *ar= CTX_wm_region(C);
1153                 int align= panel_aligned(sa, ar);
1154
1155                 if(align)
1156                         panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1157                 else
1158                         panel_activate_state(C, panel, PANEL_STATE_EXIT);
1159         }
1160         else if(event->type == MOUSEMOVE) {
1161                 if(data->state == PANEL_STATE_DRAG)
1162                         ui_do_drag(C, event, panel);
1163         }
1164         else if(event->type == TIMER && event->customdata == data->animtimer) {
1165                 if(data->state == PANEL_STATE_ANIMATION)
1166                         ui_do_animate(C, panel);
1167                 else if(data->state == PANEL_STATE_DRAG)
1168                         ui_do_drag(C, event, panel);
1169         }
1170
1171         data= panel->activedata;
1172
1173         if(data && data->state == PANEL_STATE_ANIMATION)
1174                 return WM_UI_HANDLER_CONTINUE;
1175         else
1176                 return WM_UI_HANDLER_BREAK;
1177 }
1178
1179 static void ui_handler_remove_panel(bContext *C, void *userdata)
1180 {
1181         Panel *pa= userdata;
1182
1183         panel_activate_state(C, pa, PANEL_STATE_EXIT);
1184 }
1185
1186 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
1187 {
1188         uiHandlePanelData *data= pa->activedata;
1189         wmWindow *win= CTX_wm_window(C);
1190         ARegion *ar= CTX_wm_region(C);
1191         
1192         if(data && data->state == state)
1193                 return;
1194
1195         if(state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1196                 if(data && data->state != PANEL_STATE_ANIMATION) {
1197                         /* XXX:
1198                          *      - the panel tabbing function call below (test_add_new_tabs()) has been commented out
1199                          *        "It is too easy to do by accident when reordering panels, is very hard to control and use, and has no real benefit." - BillRey
1200                          * Aligorith, 2009Sep
1201                          */
1202                         //test_add_new_tabs(ar);   // also copies locations of tabs in dragged panel
1203                         check_panel_overlap(ar, NULL);  // clears
1204                 }
1205
1206                 pa->flag &= ~PNL_SELECT;
1207         }
1208         else
1209                 pa->flag |= PNL_SELECT;
1210
1211         if(data && data->animtimer) {
1212                 WM_event_remove_timer(CTX_wm_manager(C), win, data->animtimer);
1213                 data->animtimer= NULL;
1214         }
1215
1216         if(state == PANEL_STATE_EXIT) {
1217                 MEM_freeN(data);
1218                 pa->activedata= NULL;
1219
1220                 WM_event_remove_ui_handler(&win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa, 0);
1221         }
1222         else {
1223                 if(!data) {
1224                         data= MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1225                         pa->activedata= data;
1226
1227                         WM_event_add_ui_handler(C, &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa);
1228                 }
1229
1230                 if(ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1231                         data->animtimer= WM_event_add_timer(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
1232
1233                 data->state= state;
1234                 data->startx= win->eventstate->x;
1235                 data->starty= win->eventstate->y;
1236                 data->startofsx= pa->ofsx;
1237                 data->startofsy= pa->ofsy;
1238                 data->startsizex= pa->sizex;
1239                 data->startsizey= pa->sizey;
1240                 data->starttime= PIL_check_seconds_timer();
1241         }
1242
1243         ED_region_tag_redraw(ar);
1244
1245         /* XXX exception handling, 3d window preview panel */
1246         /* if(block->drawextra==BIF_view3d_previewdraw)
1247                 BIF_view3d_previewrender_clear(curarea);*/
1248         
1249         /* XXX exception handling, 3d window preview panel */
1250         /* if(block->drawextra==BIF_view3d_previewdraw)
1251                 BIF_view3d_previewrender_signal(curarea, PR_DISPRECT);
1252         else if(strcmp(block->name, "image_panel_preview")==0)
1253                 image_preview_event(2); */
1254 }
1255