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