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