Finished themes for transparent Button regions in Blender.
[blender.git] / source / blender / editors / interface / interface_panel.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2003-2009 full recode.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_panel.c
27  *  \ingroup edinterface
28  */
29
30
31 /* a full doc with API notes can be found in bf-blender/trunk/blender/doc/guides/interface_API.txt */
32  
33 #include <math.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "PIL_time.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45
46 #include "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_GetStyleDraw();
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->rect.ymin += ofsy;
319                 but->rect.ymax += ofsy;
320         }
321
322         block->rect.xmax = block->panel->sizex;
323         block->rect.ymax = block->panel->sizey;
324         block->rect.xmin = block->rect.ymin = 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         float f3 = 0.15 * U.widget_unit;
349         float f5 = 0.25 * U.widget_unit;
350         float f7 = 0.35 * U.widget_unit;
351         
352         if (dir == 'h') {
353                 ui_draw_anti_tria(x - f3, y - f5, x - f3, y + f5, x + f7, y);
354         }
355         else if (dir == 't') {
356                 ui_draw_anti_tria(x - f5, y - f7, x + f5, y - f7, x, y + f3);
357         }
358         else { /* 'v' = vertical, down */
359                 ui_draw_anti_tria(x - f5, y + f3, x + f5, y + f3, x, y - f7);
360         }
361 }
362
363 /* triangle 'icon' inside rect */
364 static void ui_draw_tria_rect(const rctf *rect, char dir)
365 {
366         if (dir == 'h') {
367                 float half = 0.5f * BLI_rctf_size_y(rect);
368                 ui_draw_anti_tria(rect->xmin, rect->ymin, rect->xmin, rect->ymax, rect->xmax, rect->ymin + half);
369         }
370         else {
371                 float half = 0.5f * BLI_rctf_size_x(rect);
372                 ui_draw_anti_tria(rect->xmin, rect->ymax, rect->xmax, rect->ymax, rect->xmin + half, rect->ymin);
373         }
374 }
375
376 static void ui_draw_anti_x(float x1, float y1, float x2, float y2)
377 {
378
379         /* set antialias line */
380         glEnable(GL_LINE_SMOOTH);
381         glEnable(GL_BLEND);
382
383         glLineWidth(2.0);
384         
385         fdrawline(x1, y1, x2, y2);
386         fdrawline(x1, y2, x2, y1);
387         
388         glLineWidth(1.0);
389         
390         glDisable(GL_LINE_SMOOTH);
391         glDisable(GL_BLEND);
392         
393 }
394
395 /* x 'icon' for panel header */
396 static void ui_draw_x_icon(float x, float y)
397 {
398
399         ui_draw_anti_x(x, y, x + 9.375f, y + 9.375f);
400
401 }
402
403 #define PNL_ICON    UI_UNIT_X  /* could be UI_UNIT_Y too */
404
405 static void ui_draw_panel_scalewidget(rcti *rect)
406 {
407         float xmin, xmax, dx;
408         float ymin, ymax, dy;
409         
410         xmin = rect->xmax - PNL_HEADER + 2;
411         xmax = rect->xmax - 3;
412         ymin = rect->ymin + 3;
413         ymax = rect->ymin + PNL_HEADER - 2;
414                 
415         dx = 0.5f * (xmax - xmin);
416         dy = 0.5f * (ymax - ymin);
417         
418         glEnable(GL_BLEND);
419         glColor4ub(255, 255, 255, 50);
420         fdrawline(xmin, ymin, xmax, ymax);
421         fdrawline(xmin + dx, ymin, xmax, ymax - dy);
422         
423         glColor4ub(0, 0, 0, 50);
424         fdrawline(xmin, ymin + 1, xmax, ymax + 1);
425         fdrawline(xmin + dx, ymin + 1, xmax, ymax - dy + 1);
426         glDisable(GL_BLEND);
427 }
428
429 static void ui_draw_panel_dragwidget(const rctf *rect)
430 {
431         float xmin, xmax, dx;
432         float ymin, ymax, dy;
433         
434         xmin = rect->xmin;
435         xmax = rect->xmax;
436         ymin = rect->ymin;
437         ymax = rect->ymax;
438         
439         dx = 0.333f * (xmax - xmin);
440         dy = 0.333f * (ymax - ymin);
441         
442         glEnable(GL_BLEND);
443         glColor4ub(255, 255, 255, 50);
444         fdrawline(xmin, ymax, xmax, ymin);
445         fdrawline(xmin + dx, ymax, xmax, ymin + dy);
446         fdrawline(xmin + 2 * dx, ymax, xmax, ymin + 2 * dy);
447         
448         glColor4ub(0, 0, 0, 50);
449         fdrawline(xmin, ymax + 1, xmax, ymin + 1);
450         fdrawline(xmin + dx, ymax + 1, xmax, ymin + dy + 1);
451         fdrawline(xmin + 2 * dx, ymax + 1, xmax, ymin + 2 * dy + 1);
452         glDisable(GL_BLEND);
453 }
454
455
456 static void ui_draw_aligned_panel_header(uiStyle *style, uiBlock *block, rcti *rect, char dir)
457 {
458         Panel *panel = block->panel;
459         rcti hrect;
460         int pnl_icons;
461         const char *activename = IFACE_(panel->drawname[0] ? panel->drawname : panel->panelname);
462
463         /* + 0.001f to avoid flirting with float inaccuracy */
464         if (panel->control & UI_PNL_CLOSE) pnl_icons = (panel->labelofs + 2 * PNL_ICON + 5) / block->aspect + 0.001f;
465         else pnl_icons = (panel->labelofs + PNL_ICON + 5) / block->aspect + 0.001f;
466         
467         /* active tab */
468         /* draw text label */
469         UI_ThemeColor(TH_TITLE);
470         
471         hrect = *rect;
472         if (dir == 'h') {
473                 hrect.xmin = rect->xmin + pnl_icons;
474                 hrect.ymin += 2.0f / block->aspect;
475                 uiStyleFontDraw(&style->paneltitle, &hrect, activename);
476         }
477         else {
478                 /* ignore 'pnl_icons', otherwise the text gets offset horizontally 
479                  * + 0.001f to avoid flirting with float inaccuracy
480                  */
481                 hrect.xmin = rect->xmin + (PNL_ICON + 5) / block->aspect + 0.001f;
482                 uiStyleFontDrawRotated(&style->paneltitle, &hrect, activename);
483         }
484 }
485
486 static void rectf_scale(rctf *rect, const float scale)
487 {
488         float centx = BLI_rctf_cent_x(rect);
489         float centy = BLI_rctf_cent_y(rect);
490         float sizex = BLI_rctf_size_x(rect) * 0.5f * scale;
491         float sizey = BLI_rctf_size_y(rect) * 0.5f * scale;
492         
493         rect->xmin = centx - sizex;
494         rect->xmax = centx + sizex;
495         rect->ymin = centy - sizey;
496         rect->ymax = centy + sizey;
497 }
498
499 /* panel integrated in buttonswindow, tool/property lists etc */
500 void ui_draw_aligned_panel(uiStyle *style, uiBlock *block, rcti *rect)
501 {
502         Panel *panel = block->panel;
503         rcti headrect;
504         rctf itemrect;
505         int ofsx;
506         
507         if (panel->paneltab) return;
508         if (panel->type && (panel->type->flag & PNL_NO_HEADER)) return;
509
510         /* calculate header rect */
511         /* + 0.001f to prevent flicker due to float inaccuracy */
512         headrect = *rect;
513         headrect.ymin = headrect.ymax;
514         headrect.ymax = headrect.ymin + floor(PNL_HEADER / block->aspect + 0.001f);
515         
516         {
517                 float minx = rect->xmin;
518                 float maxx = rect->xmax;
519                 float y = headrect.ymax;
520
521                 glEnable(GL_BLEND);
522
523                 
524                 if (UI_GetThemeValue(TH_PANEL_SHOW_HEADER)) {
525                         /* draw with background color */
526                         glEnable(GL_BLEND);
527                         UI_ThemeColor4(TH_PANEL_HEADER);
528                         glRectf(minx, headrect.ymin + 1, maxx, y);
529
530                         fdrawline(minx, y, maxx, y);
531                         fdrawline(minx, y, maxx, y);
532                 }
533                 else if (!(panel->runtime_flag & PNL_FIRST)) {
534                         /* draw embossed separator */
535                         minx += 5.0f / block->aspect;
536                         maxx -= 5.0f / block->aspect;
537                         
538                         glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
539                         fdrawline(minx, y, maxx, y);
540                         glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
541                         fdrawline(minx, y - 1, maxx, y - 1);
542                         glDisable(GL_BLEND);
543                 }
544
545                 glDisable(GL_BLEND);
546         }
547         
548         /* horizontal title */
549         if (!(panel->flag & PNL_CLOSEDX)) {
550                 ui_draw_aligned_panel_header(style, block, &headrect, 'h');
551                 
552                 /* itemrect smaller */
553                 itemrect.xmax = headrect.xmax - 5.0f / block->aspect;
554                 itemrect.xmin = itemrect.xmax - BLI_rcti_size_y(&headrect);
555                 itemrect.ymin = headrect.ymin;
556                 itemrect.ymax = headrect.ymax;
557
558                 rectf_scale(&itemrect, 0.7f);
559                 ui_draw_panel_dragwidget(&itemrect);
560         }
561         
562         /* if the panel is minimized vertically:
563          * (------)
564          */
565         if (panel->flag & PNL_CLOSEDY) {
566                 
567         }
568         else if (panel->flag & PNL_CLOSEDX) {
569                 /* draw vertical title */
570                 ui_draw_aligned_panel_header(style, block, &headrect, 'v');
571         }
572         /* an open panel */
573         else {
574                 
575                 /* in some occasions, draw a border */
576                 if (panel->flag & PNL_SELECT) {
577                         if (panel->control & UI_PNL_SOLID) uiSetRoundBox(UI_CNR_ALL);
578                         else uiSetRoundBox(UI_CNR_NONE);
579                         
580                         UI_ThemeColorShade(TH_BACK, -120);
581                         uiRoundRect(0.5f + rect->xmin, 0.5f + rect->ymin, 0.5f + rect->xmax, 0.5f + headrect.ymax + 1, 8);
582                 }
583                 
584                 /* panel backdrop */
585                 if (UI_GetThemeValue(TH_PANEL_SHOW_BACK)) {
586                         /* draw with background color */
587                         glEnable(GL_BLEND);
588                         UI_ThemeColor4(TH_PANEL_BACK);
589                         glRecti(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
590                 }
591                 
592                 if (panel->control & UI_PNL_SCALE)
593                         ui_draw_panel_scalewidget(rect);
594         }
595         
596         /* draw optional close icon */
597         
598         ofsx = 6;
599         if (panel->control & UI_PNL_CLOSE) {
600                 
601                 UI_ThemeColor(TH_TEXT);
602                 ui_draw_x_icon(rect->xmin + 2 + ofsx, rect->ymax + 2);
603                 ofsx = 22;
604         }
605         
606         /* draw collapse icon */
607         UI_ThemeColor(TH_TEXT);
608         
609         /* itemrect smaller */
610         itemrect.xmin = headrect.xmin + 5.0f / block->aspect;
611         itemrect.xmax = itemrect.xmin + BLI_rcti_size_y(&headrect);
612         itemrect.ymin = headrect.ymin;
613         itemrect.ymax = headrect.ymax;
614         
615         rectf_scale(&itemrect, 0.35f);
616         
617         if (panel->flag & PNL_CLOSEDY)
618                 ui_draw_tria_rect(&itemrect, 'h');
619         else if (panel->flag & PNL_CLOSEDX)
620                 ui_draw_tria_rect(&itemrect, 'h');
621         else
622                 ui_draw_tria_rect(&itemrect, 'v');
623
624         (void)ofsx;
625 }
626
627 /************************** panel alignment *************************/
628
629 static int get_panel_header(Panel *pa)
630 {
631         if (pa->type && (pa->type->flag & PNL_NO_HEADER))
632                 return 0;
633
634         return PNL_HEADER;
635 }
636
637 static int get_panel_size_y(Panel *pa)
638 {
639         if (pa->type && (pa->type->flag & PNL_NO_HEADER))
640                 return pa->sizey;
641
642         return PNL_HEADER + pa->sizey;
643 }
644
645 /* this function is needed because uiBlock and Panel itself don't
646  * change sizey or location when closed */
647 static int get_panel_real_ofsy(Panel *pa)
648 {
649         if (pa->flag & PNL_CLOSEDY) return pa->ofsy + pa->sizey;
650         else if (pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDY)) return pa->ofsy + pa->sizey;
651         else if (pa->paneltab) return pa->paneltab->ofsy;
652         else return pa->ofsy;
653 }
654
655 static int get_panel_real_ofsx(Panel *pa)
656 {
657         if (pa->flag & PNL_CLOSEDX) return pa->ofsx + get_panel_header(pa);
658         else if (pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDX)) return pa->ofsx + get_panel_header(pa);
659         else return pa->ofsx + pa->sizex;
660 }
661
662 typedef struct PanelSort {
663         Panel *pa, *orig;
664 } PanelSort;
665
666 /* note about sorting;
667  * the sortorder has a lower value for new panels being added.
668  * however, that only works to insert a single panel, when more new panels get
669  * added the coordinates of existing panels and the previously stored to-be-inserted
670  * panels do not match for sorting */
671
672 static int find_leftmost_panel(const void *a1, const void *a2)
673 {
674         const PanelSort *ps1 = a1, *ps2 = a2;
675         
676         if (ps1->pa->ofsx > ps2->pa->ofsx) return 1;
677         else if (ps1->pa->ofsx < ps2->pa->ofsx) return -1;
678         else if (ps1->pa->sortorder > ps2->pa->sortorder) return 1;
679         else if (ps1->pa->sortorder < ps2->pa->sortorder) return -1;
680
681         return 0;
682 }
683
684
685 static int find_highest_panel(const void *a1, const void *a2)
686 {
687         const PanelSort *ps1 = a1, *ps2 = a2;
688         
689         /* stick uppermost header-less panels to the top of the region -
690          * prevent them from being sorted */
691         if (ps1->pa->sortorder < ps2->pa->sortorder && ps1->pa->type->flag & PNL_NO_HEADER) return -1;
692         
693         if (ps1->pa->ofsy + ps1->pa->sizey < ps2->pa->ofsy + ps2->pa->sizey) return 1;
694         else if (ps1->pa->ofsy + ps1->pa->sizey > ps2->pa->ofsy + ps2->pa->sizey) return -1;
695         else if (ps1->pa->sortorder > ps2->pa->sortorder) return 1;
696         else if (ps1->pa->sortorder < ps2->pa->sortorder) return -1;
697         
698         return 0;
699 }
700
701 static int compare_panel(const void *a1, const void *a2)
702 {
703         const PanelSort *ps1 = a1, *ps2 = a2;
704         
705         if (ps1->pa->sortorder > ps2->pa->sortorder) return 1;
706         else if (ps1->pa->sortorder < ps2->pa->sortorder) return -1;
707         
708         return 0;
709 }
710
711 /* this doesnt draw */
712 /* returns 1 when it did something */
713 static int uiAlignPanelStep(ScrArea *sa, ARegion *ar, float fac, int drag)
714 {
715         Panel *pa;
716         PanelSort *ps, *panelsort, *psnext;
717         int a, tot = 0, done;
718         int align = panel_aligned(sa, ar);
719         
720         /* count active, not tabbed panels */
721         for (pa = ar->panels.first; pa; pa = pa->next)
722                 if ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab == NULL)
723                         tot++;
724
725         if (tot == 0) return 0;
726
727         /* extra; change close direction? */
728         for (pa = ar->panels.first; pa; pa = pa->next) {
729                 if ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab == NULL) {
730                         if ((pa->flag & PNL_CLOSEDX) && (align == BUT_VERTICAL))
731                                 pa->flag ^= PNL_CLOSED;
732                         else if ((pa->flag & PNL_CLOSEDY) && (align == BUT_HORIZONTAL))
733                                 pa->flag ^= PNL_CLOSED;
734                 }
735         }
736
737         /* sort panels */
738         panelsort = MEM_callocN(tot * sizeof(PanelSort), "panelsort");
739         
740         ps = panelsort;
741         for (pa = ar->panels.first; pa; pa = pa->next) {
742                 if ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab == NULL) {
743                         ps->pa = MEM_dupallocN(pa);
744                         ps->orig = pa;
745                         ps++;
746                 }
747         }
748         
749         if (drag) {
750                 /* while we are dragging, we sort on location and update sortorder */
751                 if (align == BUT_VERTICAL)
752                         qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
753                 else
754                         qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
755
756                 for (ps = panelsort, a = 0; a < tot; a++, ps++)
757                         ps->orig->sortorder = a;
758         }
759         else
760                 /* otherwise use sortorder */
761                 qsort(panelsort, tot, sizeof(PanelSort), compare_panel);
762         
763         /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
764         ps = panelsort;
765         ps->pa->ofsx = 0;
766         ps->pa->ofsy = -get_panel_size_y(ps->pa);
767
768         for (a = 0; a < tot - 1; a++, ps++) {
769                 psnext = ps + 1;
770
771                 if (align == BUT_VERTICAL) {
772                         psnext->pa->ofsx = ps->pa->ofsx;
773                         psnext->pa->ofsy = get_panel_real_ofsy(ps->pa) - get_panel_size_y(psnext->pa);
774                 }
775                 else {
776                         psnext->pa->ofsx = get_panel_real_ofsx(ps->pa);
777                         psnext->pa->ofsy = ps->pa->ofsy + get_panel_size_y(ps->pa) - get_panel_size_y(psnext->pa);
778                 }
779         }
780         
781         /* we interpolate */
782         done = FALSE;
783         ps = panelsort;
784         for (a = 0; a < tot; a++, ps++) {
785                 if ((ps->pa->flag & PNL_SELECT) == 0) {
786                         if ((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) {
787                                 ps->orig->ofsx = floorf(0.5f + fac * (float)ps->pa->ofsx + (1.0f - fac) * (float)ps->orig->ofsx);
788                                 ps->orig->ofsy = floorf(0.5f + fac * (float)ps->pa->ofsy + (1.0f - fac) * (float)ps->orig->ofsy);
789                                 done = TRUE;
790                         }
791                 }
792         }
793
794         /* copy locations to tabs */
795         for (pa = ar->panels.first; pa; pa = pa->next)
796                 if (pa->paneltab && (pa->runtime_flag & PNL_ACTIVE))
797                         ui_panel_copy_offset(pa, pa->paneltab);
798
799         /* free panelsort array */
800         for (ps = panelsort, a = 0; a < tot; a++, ps++) {
801                 MEM_freeN(ps->pa);
802         }
803         MEM_freeN(panelsort);
804         
805         return done;
806 }
807
808 static void ui_panels_size(ScrArea *sa, ARegion *ar, int *x, int *y)
809 {
810         Panel *pa;
811         int align = panel_aligned(sa, ar);
812         int sizex = UI_PANEL_WIDTH;
813         int sizey = UI_PANEL_WIDTH;
814
815         /* compute size taken up by panels, for setting in view2d */
816         for (pa = ar->panels.first; pa; pa = pa->next) {
817                 if (pa->runtime_flag & PNL_ACTIVE) {
818                         int pa_sizex, pa_sizey;
819
820                         if (align == BUT_VERTICAL) {
821                                 pa_sizex = pa->ofsx + pa->sizex;
822                                 pa_sizey = get_panel_real_ofsy(pa);
823                         }
824                         else {
825                                 pa_sizex = get_panel_real_ofsx(pa) + pa->sizex;
826                                 pa_sizey = pa->ofsy + get_panel_size_y(pa);
827                         }
828
829                         sizex = max_ii(sizex, pa_sizex);
830                         sizey = min_ii(sizey, pa_sizey);
831                 }
832         }
833
834         *x = sizex;
835         *y = sizey;
836 }
837
838 static void ui_do_animate(const bContext *C, Panel *panel)
839 {
840         uiHandlePanelData *data = panel->activedata;
841         ScrArea *sa = CTX_wm_area(C);
842         ARegion *ar = CTX_wm_region(C);
843         float fac;
844
845         fac = (PIL_check_seconds_timer() - data->starttime) / ANIMATION_TIME;
846         fac = min_ff(sqrt(fac), 1.0f);
847
848         /* for max 1 second, interpolate positions */
849         if (uiAlignPanelStep(sa, ar, fac, 0)) {
850                 ED_region_tag_redraw(ar);
851         }
852         else {
853                 fac = 1.0f;
854         }
855
856         if (fac >= 1.0f) {
857                 panel_activate_state(C, panel, PANEL_STATE_EXIT);
858                 return;
859         }
860 }
861
862 void uiBeginPanels(const bContext *UNUSED(C), ARegion *ar)
863 {
864         Panel *pa;
865   
866         /* set all panels as inactive, so that at the end we know
867          * which ones were used */
868         for (pa = ar->panels.first; pa; pa = pa->next) {
869                 if (pa->runtime_flag & PNL_ACTIVE)
870                         pa->runtime_flag = PNL_WAS_ACTIVE;
871                 else
872                         pa->runtime_flag = 0;
873         }
874 }
875
876 /* only draws blocks with panels */
877 void uiEndPanels(const bContext *C, ARegion *ar, int *x, int *y)
878 {
879         ScrArea *sa = CTX_wm_area(C);
880         uiBlock *block;
881         Panel *panot, *panew, *patest, *pa, *firstpa;
882         
883         /* offset contents */
884         for (block = ar->uiblocks.first; block; block = block->next)
885                 if (block->active && block->panel)
886                         ui_offset_panel_block(block);
887
888         /* consistency; are panels not made, whilst they have tabs */
889         for (panot = ar->panels.first; panot; panot = panot->next) {
890                 if ((panot->runtime_flag & PNL_ACTIVE) == 0) {  /* not made */
891
892                         for (panew = ar->panels.first; panew; panew = panew->next) {
893                                 if ((panew->runtime_flag & PNL_ACTIVE)) {
894                                         if (panew->paneltab == panot) {  /* panew is tab in notmade pa */
895                                                 break;
896                                         }
897                                 }
898                         }
899                         /* now panew can become the new parent, check all other tabs */
900                         if (panew) {
901                                 for (patest = ar->panels.first; patest; patest = patest->next) {
902                                         if (patest->paneltab == panot) {
903                                                 patest->paneltab = panew;
904                                         }
905                                 }
906                                 panot->paneltab = panew;
907                                 panew->paneltab = NULL;
908                                 ED_region_tag_redraw(ar); /* the buttons panew were not made */
909                         }
910                 }
911         }
912
913         /* re-align, possibly with animation */
914         if (panels_re_align(sa, ar, &pa)) {
915                 if (pa)
916                         panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
917                 else
918                         uiAlignPanelStep(sa, ar, 1.0, 0);
919         }
920
921         /* tag first panel */
922         firstpa = NULL;
923         for (block = ar->uiblocks.first; block; block = block->next)
924                 if (block->active && block->panel)
925                         if (!firstpa || block->panel->sortorder < firstpa->sortorder)
926                                 firstpa = block->panel;
927         
928         if (firstpa)
929                 firstpa->runtime_flag |= PNL_FIRST;
930         
931         /* compute size taken up by panel */
932         ui_panels_size(sa, ar, x, y);
933 }
934
935 void uiDrawPanels(const bContext *C, ARegion *ar)
936 {
937         uiBlock *block;
938
939         UI_ThemeClearColor(TH_BACK);
940         
941         /* draw panels, selected on top */
942         for (block = ar->uiblocks.first; block; block = block->next) {
943                 if (block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
944                         uiDrawBlock(C, block);
945                 }
946         }
947
948         for (block = ar->uiblocks.first; block; block = block->next) {
949                 if (block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
950                         uiDrawBlock(C, block);
951                 }
952         }
953 }
954
955 /* ------------ panel merging ---------------- */
956
957 static void check_panel_overlap(ARegion *ar, Panel *panel)
958 {
959         Panel *pa;
960
961         /* also called with (panel == NULL) for clear */
962         
963         for (pa = ar->panels.first; pa; pa = pa->next) {
964                 pa->flag &= ~PNL_OVERLAP;
965                 if (panel && (pa != panel)) {
966                         if (pa->paneltab == NULL && (pa->runtime_flag & PNL_ACTIVE)) {
967                                 float safex = 0.2, safey = 0.2;
968                                 
969                                 if (pa->flag & PNL_CLOSEDX) safex = 0.05;
970                                 else if (pa->flag & PNL_CLOSEDY) safey = 0.05;
971                                 else if (panel->flag & PNL_CLOSEDX) safex = 0.05;
972                                 else if (panel->flag & PNL_CLOSEDY) safey = 0.05;
973
974                                 if (pa->ofsx > panel->ofsx - safex * panel->sizex)
975                                         if (pa->ofsx + pa->sizex < panel->ofsx + (1.0f + safex) * panel->sizex)
976                                                 if (pa->ofsy > panel->ofsy - safey * panel->sizey)
977                                                         if (pa->ofsy + pa->sizey < panel->ofsy + (1.0f + safey) * panel->sizey)
978                                                                 pa->flag |= PNL_OVERLAP;
979                         }
980                 }
981         }
982 }
983
984 /************************ panel dragging ****************************/
985
986 static void ui_do_drag(const bContext *C, wmEvent *event, Panel *panel)
987 {
988         uiHandlePanelData *data = panel->activedata;
989         ScrArea *sa = CTX_wm_area(C);
990         ARegion *ar = CTX_wm_region(C);
991         short align = panel_aligned(sa, ar), dx = 0, dy = 0;
992         
993         /* first clip for window, no dragging outside */
994         if (!BLI_rcti_isect_pt_v(&ar->winrct, &event->x))
995                 return;
996
997         dx = (event->x - data->startx) & ~(PNL_GRID - 1);
998         dy = (event->y - data->starty) & ~(PNL_GRID - 1);
999
1000         dx *= (float)BLI_rctf_size_x(&ar->v2d.cur) / (float)BLI_rcti_size_x(&ar->winrct);
1001         dy *= (float)BLI_rctf_size_y(&ar->v2d.cur) / (float)BLI_rcti_size_y(&ar->winrct);
1002         
1003         if (data->state == PANEL_STATE_DRAG_SCALE) {
1004                 panel->sizex = MAX2(data->startsizex + dx, UI_PANEL_MINX);
1005                 
1006                 if (data->startsizey - dy < UI_PANEL_MINY)
1007                         dy = -UI_PANEL_MINY + data->startsizey;
1008
1009                 panel->sizey = data->startsizey - dy;
1010                 panel->ofsy = data->startofsy + dy;
1011         }
1012         else {
1013                 /* reset the panel snapping, to allow dragging away from snapped edges */
1014                 panel->snap = PNL_SNAP_NONE;
1015                 
1016                 panel->ofsx = data->startofsx + dx;
1017                 panel->ofsy = data->startofsy + dy;
1018                 check_panel_overlap(ar, panel);
1019                 
1020                 if (align) uiAlignPanelStep(sa, ar, 0.2, 1);
1021         }
1022
1023         ED_region_tag_redraw(ar);
1024 }
1025
1026 /******************* region level panel interaction *****************/
1027
1028
1029 /* this function is supposed to call general window drawing too */
1030 /* also it supposes a block has panel, and isn't a menu */
1031 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my, int event)
1032 {
1033         ScrArea *sa = CTX_wm_area(C);
1034         ARegion *ar = CTX_wm_region(C);
1035         Panel *pa;
1036         int align = panel_aligned(sa, ar), button = 0;
1037
1038         /* mouse coordinates in panel space! */
1039         
1040         /* XXX weak code, currently it assumes layout style for location of widgets */
1041         
1042         /* check open/collapsed button */
1043         if (event == RETKEY)
1044                 button = 1;
1045         else if (event == AKEY)
1046                 button = 1;
1047         else if (block->panel->flag & PNL_CLOSEDX) {
1048                 if (my >= block->rect.ymax) button = 1;
1049         }
1050         else if (block->panel->control & UI_PNL_CLOSE) {
1051                 /* whole of header can be used to collapse panel (except top-right corner) */
1052                 if (mx <= block->rect.xmax - 8 - PNL_ICON) button = 2;
1053                 //else if (mx <= block->rect.xmin + 10 + 2 * PNL_ICON + 2) button = 1;
1054         }
1055         else if (mx <= block->rect.xmax - PNL_ICON - 12) {
1056                 button = 1;
1057         }
1058         
1059         if (button) {
1060                 if (button == 2) {  /* close */
1061                         ED_region_tag_redraw(ar);
1062                 }
1063                 else {  /* collapse */
1064                         if (block->panel->flag & PNL_CLOSED) {
1065                                 block->panel->flag &= ~PNL_CLOSED;
1066                                 /* snap back up so full panel aligns with screen edge */
1067                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1068                                         block->panel->ofsy = 0;
1069                         }
1070                         else if (align == BUT_HORIZONTAL) {
1071                                 block->panel->flag |= PNL_CLOSEDX;
1072                         }
1073                         else {
1074                                 /* snap down to bottom screen edge*/
1075                                 block->panel->flag |= PNL_CLOSEDY;
1076                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1077                                         block->panel->ofsy = -block->panel->sizey;
1078                         }
1079                         
1080                         for (pa = ar->panels.first; pa; pa = pa->next) {
1081                                 if (pa->paneltab == block->panel) {
1082                                         if (block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1083                                         else pa->flag &= ~PNL_CLOSED;
1084                                 }
1085                         }
1086                 }
1087
1088                 if (align)
1089                         panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1090                 else
1091                         ED_region_tag_redraw(ar);
1092         }
1093         else if (mx <= (block->rect.xmax - PNL_ICON - 12) + PNL_ICON + 2) {
1094                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1095         }
1096 }
1097
1098 /* XXX should become modal keymap */
1099 /* AKey is opening/closing panels, independent of button state now */
1100
1101 int ui_handler_panel_region(bContext *C, wmEvent *event)
1102 {
1103         ScrArea *sa = CTX_wm_area(C);
1104         ARegion *ar = CTX_wm_region(C);
1105         uiBlock *block;
1106         Panel *pa;
1107         int retval, mx, my;
1108
1109         retval = WM_UI_HANDLER_CONTINUE;
1110         for (block = ar->uiblocks.last; block; block = block->prev) {
1111                 int inside = 0, inside_header = 0, inside_scale = 0;
1112                 
1113                 mx = event->x;
1114                 my = event->y;
1115                 ui_window_to_block(ar, block, &mx, &my);
1116
1117                 /* checks for mouse position inside */
1118                 pa = block->panel;
1119
1120                 if (!pa || pa->paneltab != NULL)
1121                         continue;
1122                 if (pa->type && pa->type->flag & PNL_NO_HEADER)  /* XXX - accessed freed panels when scripts reload, need to fix. */
1123                         continue;
1124                 
1125                 /* clicked at panel header? */
1126                 if (pa->flag & PNL_CLOSEDX) {
1127                         if (block->rect.xmin <= mx && block->rect.xmin + PNL_HEADER >= mx)
1128                                 inside_header = 1;
1129                 }
1130                 else if ((block->rect.ymax <= my) && (block->rect.ymax + PNL_HEADER >= my)) {
1131                         inside_header = 1;
1132                 }
1133                 else if (!(pa->flag & PNL_CLOSEDY)) {
1134                         /* open panel */
1135                         if (pa->control & UI_PNL_SCALE) {
1136                                 if (block->rect.xmax - PNL_HEADER <= mx)
1137                                         if (block->rect.ymin + PNL_HEADER >= my)
1138                                                 inside_scale = 1;
1139                         }
1140                         if (block->rect.xmin <= mx && block->rect.xmax >= mx)
1141                                 if (block->rect.ymin <= my && block->rect.ymax + PNL_HEADER >= my)
1142                                         inside = 1;
1143                 }
1144                 
1145                 /* XXX hardcoded key warning */
1146                 if (inside && event->val == KM_PRESS) {
1147                         if (event->type == AKEY && !ELEM4(KM_MOD_FIRST, event->ctrl, event->oskey, event->shift, event->alt)) {
1148                                 
1149                                 if (pa->flag & PNL_CLOSEDY) {
1150                                         if ((block->rect.ymax <= my) && (block->rect.ymax + PNL_HEADER >= my))
1151                                                 ui_handle_panel_header(C, block, mx, my, event->type);
1152                                 }
1153                                 else
1154                                         ui_handle_panel_header(C, block, mx, my, event->type);
1155                                 
1156                                 continue;
1157                         }
1158                 }
1159                 
1160                 /* on active button, do not handle panels */
1161                 if (ui_button_is_active(ar))
1162                         continue;
1163                 
1164                 if (inside || inside_header) {
1165
1166                         if (event->val == KM_PRESS) {
1167                                 
1168                                 /* open close on header */
1169                                 if (ELEM(event->type, RETKEY, PADENTER)) {
1170                                         if (inside_header) {
1171                                                 ui_handle_panel_header(C, block, mx, my, RETKEY);
1172                                                 retval = WM_UI_HANDLER_BREAK;
1173                                                 break;
1174                                         }
1175                                 }
1176                                 else if (event->type == LEFTMOUSE) {
1177                                         /* all inside clicks should return in break - overlapping/float panels */
1178                                         retval = WM_UI_HANDLER_BREAK;
1179                                         
1180                                         if (inside_header) {
1181                                                 if (event->ctrl)
1182                                                         panels_collapse_all(sa, ar);
1183                                                 ui_handle_panel_header(C, block, mx, my, 0);
1184                                                 retval = WM_UI_HANDLER_BREAK;
1185                                                 break;
1186                                         }
1187                                         else if (inside_scale && !(pa->flag & PNL_CLOSED)) {
1188                                                 panel_activate_state(C, pa, PANEL_STATE_DRAG_SCALE);
1189                                                 retval = WM_UI_HANDLER_BREAK;
1190                                                 break;
1191                                         }
1192
1193                                 }
1194                                 else if (event->type == ESCKEY) {
1195                                         /*XXX 2.50*/
1196 #if 0
1197                                         if (block->handler) {
1198                                                 rem_blockhandler(sa, block->handler);
1199                                                 ED_region_tag_redraw(ar);
1200                                                 retval = WM_UI_HANDLER_BREAK;
1201                                         }
1202 #endif
1203                                 }
1204                                 else if (event->type == PADPLUSKEY || event->type == PADMINUS) {
1205 #if 0 /* XXX make float panel exception? */
1206                                         int zoom = 0;
1207                                 
1208                                         /* if panel is closed, only zoom if mouse is over the header */
1209                                         if (pa->flag & (PNL_CLOSEDX | PNL_CLOSEDY)) {
1210                                                 if (inside_header)
1211                                                         zoom = 1;
1212                                         }
1213                                         else
1214                                                 zoom = 1;
1215
1216                                         if (zoom) {
1217                                                 ScrArea *sa = CTX_wm_area(C);
1218                                                 SpaceLink *sl = sa->spacedata.first;
1219
1220                                                 if (sa->spacetype != SPACE_BUTS) {
1221                                                         if (!(pa->control & UI_PNL_SCALE)) {
1222                                                                 if (event->type == PADPLUSKEY) sl->blockscale += 0.1;
1223                                                                 else sl->blockscale -= 0.1;
1224                                                                 CLAMP(sl->blockscale, 0.6, 1.0);
1225
1226                                                                 ED_region_tag_redraw(ar);
1227                                                                 retval = WM_UI_HANDLER_BREAK;
1228                                                         }
1229                                                 }
1230                                         }
1231 #endif
1232                                 }
1233                         }
1234                 }
1235         }
1236
1237         return retval;
1238 }
1239
1240 /**************** window level modal panel interaction **************/
1241
1242 /* note, this is modal handler and should not swallow events for animation */
1243 static int ui_handler_panel(bContext *C, wmEvent *event, void *userdata)
1244 {
1245         Panel *panel = userdata;
1246         uiHandlePanelData *data = panel->activedata;
1247
1248         /* verify if we can stop */
1249         if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
1250                 ScrArea *sa = CTX_wm_area(C);
1251                 ARegion *ar = CTX_wm_region(C);
1252                 int align = panel_aligned(sa, ar);
1253
1254                 if (align)
1255                         panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1256                 else
1257                         panel_activate_state(C, panel, PANEL_STATE_EXIT);
1258         }
1259         else if (event->type == MOUSEMOVE) {
1260                 if (data->state == PANEL_STATE_DRAG)
1261                         ui_do_drag(C, event, panel);
1262         }
1263         else if (event->type == TIMER && event->customdata == data->animtimer) {
1264                 if (data->state == PANEL_STATE_ANIMATION)
1265                         ui_do_animate(C, panel);
1266                 else if (data->state == PANEL_STATE_DRAG)
1267                         ui_do_drag(C, event, panel);
1268         }
1269
1270         data = panel->activedata;
1271
1272         if (data && data->state == PANEL_STATE_ANIMATION)
1273                 return WM_UI_HANDLER_CONTINUE;
1274         else
1275                 return WM_UI_HANDLER_BREAK;
1276 }
1277
1278 static void ui_handler_remove_panel(bContext *C, void *userdata)
1279 {
1280         Panel *pa = userdata;
1281
1282         panel_activate_state(C, pa, PANEL_STATE_EXIT);
1283 }
1284
1285 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
1286 {
1287         uiHandlePanelData *data = pa->activedata;
1288         wmWindow *win = CTX_wm_window(C);
1289         ARegion *ar = CTX_wm_region(C);
1290         
1291         if (data && data->state == state)
1292                 return;
1293
1294         if (state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1295                 if (data && data->state != PANEL_STATE_ANIMATION) {
1296                         /* XXX:
1297                          *      - the panel tabbing function call below (test_add_new_tabs()) has been commented out
1298                          *        "It is too easy to do by accident when reordering panels,
1299                          *     is very hard to control and use, and has no real benefit." - BillRey
1300                          * Aligorith, 2009Sep
1301                          */
1302                         //test_add_new_tabs(ar);   // also copies locations of tabs in dragged panel
1303                         check_panel_overlap(ar, NULL);  /* clears */
1304                 }
1305
1306                 pa->flag &= ~PNL_SELECT;
1307         }
1308         else
1309                 pa->flag |= PNL_SELECT;
1310
1311         if (data && data->animtimer) {
1312                 WM_event_remove_timer(CTX_wm_manager(C), win, data->animtimer);
1313                 data->animtimer = NULL;
1314         }
1315
1316         if (state == PANEL_STATE_EXIT) {
1317                 MEM_freeN(data);
1318                 pa->activedata = NULL;
1319
1320                 WM_event_remove_ui_handler(&win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa, FALSE);
1321         }
1322         else {
1323                 if (!data) {
1324                         data = MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1325                         pa->activedata = data;
1326
1327                         WM_event_add_ui_handler(C, &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa);
1328                 }
1329
1330                 if (ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1331                         data->animtimer = WM_event_add_timer(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
1332
1333                 data->state = state;
1334                 data->startx = win->eventstate->x;
1335                 data->starty = win->eventstate->y;
1336                 data->startofsx = pa->ofsx;
1337                 data->startofsy = pa->ofsy;
1338                 data->startsizex = pa->sizex;
1339                 data->startsizey = pa->sizey;
1340                 data->starttime = PIL_check_seconds_timer();
1341         }
1342
1343         ED_region_tag_redraw(ar);
1344
1345         /* XXX exception handling, 3d window preview panel */
1346 #if 0
1347         if (block->drawextra == BIF_view3d_previewdraw)
1348                 BIF_view3d_previewrender_clear(curarea);
1349 #endif
1350
1351         /* XXX exception handling, 3d window preview panel */
1352 #if 0
1353         if (block->drawextra == BIF_view3d_previewdraw)
1354                 BIF_view3d_previewrender_signal(curarea, PR_DISPRECT);
1355         else if (strcmp(block->name, "image_panel_preview") == 0)
1356                 image_preview_event(2);
1357 #endif
1358 }
1359