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