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