cb1f59cfd68a78ff45bd507a41201db5c416abb4
[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_math_color_blend.h"
45 #include "BLI_utildefines.h"
46
47 #include "BLF_translation.h"
48
49 #include "DNA_userdef_types.h"
50
51 #include "BKE_context.h"
52 #include "BKE_screen.h"
53
54 #include "BIF_gl.h"
55 #include "BIF_glutil.h"
56
57 #include "BLF_api.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "ED_screen.h"
63
64 #include "UI_view2d.h"
65 #include "UI_interface.h"
66 #include "UI_interface_icons.h"
67 #include "UI_resources.h"
68
69 #include "interface_intern.h"
70
71 /*********************** defines and structs ************************/
72
73 #define ANIMATION_TIME      0.30
74 #define ANIMATION_INTERVAL  0.02
75
76 #define PNL_LAST_ADDED      1
77 #define PNL_ACTIVE          2
78 #define PNL_WAS_ACTIVE      4
79 #define PNL_ANIM_ALIGN      8
80 #define PNL_NEW_ADDED       16
81 #define PNL_FIRST           32
82
83 /* only show pin header button for pinned panels */
84 #define USE_PIN_HIDDEN
85
86 typedef enum uiHandlePanelState {
87         PANEL_STATE_DRAG,
88         PANEL_STATE_DRAG_SCALE,
89         PANEL_STATE_WAIT_UNTAB,
90         PANEL_STATE_ANIMATION,
91         PANEL_STATE_EXIT
92 } uiHandlePanelState;
93
94 typedef struct uiHandlePanelData {
95         uiHandlePanelState state;
96
97         /* animation */
98         wmTimer *animtimer;
99         double starttime;
100
101         /* dragging */
102         int startx, starty;
103         int startofsx, startofsy;
104         int startsizex, startsizey;
105 } uiHandlePanelData;
106
107 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state);
108
109 /*********************** space specific code ************************/
110 /* temporary code to remove all sbuts stuff from panel code         */
111
112 static int panel_aligned(ScrArea *sa, ARegion *ar)
113 {
114         if (sa->spacetype == SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
115                 SpaceButs *sbuts = sa->spacedata.first;
116                 return sbuts->align;
117         }
118         else if (sa->spacetype == SPACE_USERPREF && ar->regiontype == RGN_TYPE_WINDOW)
119                 return BUT_VERTICAL;
120         else if (sa->spacetype == SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS)
121                 return BUT_VERTICAL;
122         else if (sa->spacetype == SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW)
123                 return BUT_VERTICAL;
124         else if (ELEM3(ar->regiontype, RGN_TYPE_UI, RGN_TYPE_TOOLS, RGN_TYPE_TOOL_PROPS))
125                 return BUT_VERTICAL;
126         
127         return 0;
128 }
129
130 static int panels_re_align(ScrArea *sa, ARegion *ar, Panel **r_pa)
131 {
132         Panel *pa;
133         int active = 0;
134
135         *r_pa = NULL;
136
137         if (sa->spacetype == SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
138                 SpaceButs *sbuts = sa->spacedata.first;
139
140                 if (sbuts->align)
141                         if (sbuts->re_align || sbuts->mainbo != sbuts->mainb)
142                                 return 1;
143         }
144         else if (sa->spacetype == SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW)
145                 return 1;
146         else if (sa->spacetype == SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS)
147                 return 1;
148
149         /* in case panel is added or disappears */
150         for (pa = ar->panels.first; pa; pa = pa->next) {
151                 if ((pa->runtime_flag & PNL_WAS_ACTIVE) && !(pa->runtime_flag & PNL_ACTIVE))
152                         return 1;
153                 if (!(pa->runtime_flag & PNL_WAS_ACTIVE) && (pa->runtime_flag & PNL_ACTIVE))
154                         return 1;
155                 if (pa->activedata)
156                         active = 1;
157         }
158
159         /* in case we need to do an animation (size changes) */
160         for (pa = ar->panels.first; pa; pa = pa->next) {
161                 if (pa->runtime_flag & PNL_ANIM_ALIGN) {
162                         if (!active)
163                                 *r_pa = pa;
164                         return 1;
165                 }
166         }
167         
168         return 0;
169 }
170
171 /****************************** panels ******************************/
172
173 static void panels_collapse_all(ScrArea *sa, ARegion *ar, Panel *from_pa)
174 {
175         Panel *pa;
176         PanelType *pt, *from_pt;
177         int flag = ((panel_aligned(sa, ar) == BUT_HORIZONTAL) ? PNL_CLOSEDX : PNL_CLOSEDY);
178
179         for (pa = ar->panels.first; pa; pa = pa->next) {
180                 pt = pa->type;
181                 from_pt = from_pa->type;
182
183                 /* close panels with headers in the same context */
184                 if (pt && from_pt && !(pt->flag & PNL_NO_HEADER)) {
185                         if (!pt->context[0] || strcmp(pt->context, from_pt->context) == 0) {
186                                 if (!pt->category[0] || strcmp(pt->category, from_pt->category) == 0) {
187                                         pa->flag &= ~PNL_CLOSED;
188                                         pa->flag |= flag;
189                                 }
190                         }
191                 }
192         }
193 }
194
195
196 static void ui_panel_copy_offset(Panel *pa, Panel *papar)
197 {
198         /* with respect to sizes... papar is parent */
199
200         pa->ofsx = papar->ofsx;
201         pa->ofsy = papar->ofsy + papar->sizey - pa->sizey;
202 }
203
204 Panel *uiPanelFindByType(ARegion *ar, PanelType *pt)
205 {
206         Panel *pa;
207
208         const char *idname = pt->idname;
209         const char *tabname = pt->idname;
210
211         for (pa = ar->panels.first; pa; pa = pa->next) {
212                 if (STREQLEN(pa->panelname, idname, UI_MAX_NAME_STR)) {
213                         if (STREQLEN(pa->tabname, tabname, UI_MAX_NAME_STR)) {
214                                 return pa;
215                         }
216                 }
217         }
218
219         return NULL;
220 }
221
222 /**
223  * \note \a pa should be return value from #uiPanelFindByType and can be NULL.
224  */
225 Panel *uiBeginPanel(ScrArea *sa, ARegion *ar, uiBlock *block, PanelType *pt, Panel *pa, bool *r_open)
226 {
227         Panel *patab, *palast, *panext;
228         const char *drawname = CTX_IFACE_(pt->translation_context, pt->label);
229         char *idname = pt->idname;
230         char *tabname = pt->idname;
231         char *hookname = NULL;
232         const bool newpanel = (pa == NULL);
233         int align = panel_aligned(sa, ar);
234
235         if (!newpanel) {
236                 pa->type = pt;
237         }
238         else {
239                 /* new panel */
240                 pa = MEM_callocN(sizeof(Panel), "new panel");
241                 pa->type = pt;
242                 BLI_strncpy(pa->panelname, idname, UI_MAX_NAME_STR);
243                 BLI_strncpy(pa->tabname, tabname, UI_MAX_NAME_STR);
244
245                 if (pt->flag & PNL_DEFAULT_CLOSED) {
246                         if (align == BUT_VERTICAL)
247                                 pa->flag |= PNL_CLOSEDY;
248                         else
249                                 pa->flag |= PNL_CLOSEDX;
250                 }
251         
252                 pa->ofsx = 0;
253                 pa->ofsy = 0;
254                 pa->sizex = 0;
255                 pa->sizey = 0;
256                 pa->runtime_flag |= PNL_NEW_ADDED;
257
258                 BLI_addtail(&ar->panels, pa);
259                 
260                 /* make new Panel tabbed? */
261                 if (hookname) {
262                         for (patab = ar->panels.first; patab; patab = patab->next) {
263                                 if ((patab->runtime_flag & PNL_ACTIVE) && patab->paneltab == NULL) {
264                                         if (strncmp(hookname, patab->panelname, UI_MAX_NAME_STR) == 0) {
265                                                 if (strncmp(tabname, patab->tabname, UI_MAX_NAME_STR) == 0) {
266                                                         pa->paneltab = patab;
267                                                         ui_panel_copy_offset(pa, patab);
268                                                         break;
269                                                 }
270                                         }
271                                 }
272                         }
273                 }
274         }
275
276         /* Do not allow closed panels without headers! Else user could get "disappeared" UI! */
277         if ((pt->flag & PNL_NO_HEADER) && (pa->flag & PNL_CLOSED)) {
278                 pa->flag &= ~PNL_CLOSED;
279                 /* Force update of panels' positions! */
280                 pa->sizex = 0;
281                 pa->sizey = 0;
282         }
283
284         BLI_strncpy(pa->drawname, drawname, sizeof(pa->drawname));
285
286         /* if a new panel is added, we insert it right after the panel
287          * that was last added. this way new panels are inserted in the
288          * right place between versions */
289         for (palast = ar->panels.first; palast; palast = palast->next)
290                 if (palast->runtime_flag & PNL_LAST_ADDED)
291                         break;
292         
293         if (newpanel) {
294                 pa->sortorder = (palast) ? palast->sortorder + 1 : 0;
295
296                 for (panext = ar->panels.first; panext; panext = panext->next)
297                         if (panext != pa && panext->sortorder >= pa->sortorder)
298                                 panext->sortorder++;
299         }
300
301         if (palast)
302                 palast->runtime_flag &= ~PNL_LAST_ADDED;
303
304         /* assign to block */
305         block->panel = pa;
306         pa->runtime_flag |= PNL_ACTIVE | PNL_LAST_ADDED;
307
308         *r_open = false;
309
310         if (pa->paneltab) return pa;
311         if (pa->flag & PNL_CLOSED) return pa;
312
313         *r_open = true;
314         
315         return pa;
316 }
317
318 void uiEndPanel(uiBlock *block, int width, int height)
319 {
320         Panel *pa = block->panel;
321
322         if (pa->runtime_flag & PNL_NEW_ADDED) {
323                 pa->runtime_flag &= ~PNL_NEW_ADDED;
324                 pa->sizex = width;
325                 pa->sizey = height;
326         }
327         else {
328                 /* check if we need to do an animation */
329                 if (!ELEM(width, 0, pa->sizex) || !ELEM(height, 0, pa->sizey)) {
330                         pa->runtime_flag |= PNL_ANIM_ALIGN;
331                         if (height != 0)
332                                 pa->ofsy += pa->sizey - height;
333                 }
334
335                 /* update width/height if non-zero */
336                 if (width != 0)
337                         pa->sizex = width;
338                 if (height != 0)
339                         pa->sizey = height;
340         }
341 }
342
343 static void ui_offset_panel_block(uiBlock *block)
344 {
345         uiStyle *style = UI_GetStyleDraw();
346         uiBut *but;
347         int ofsy;
348
349         /* compute bounds and offset */
350         ui_bounds_block(block);
351
352         ofsy = block->panel->sizey - style->panelspace;
353
354         for (but = block->buttons.first; but; but = but->next) {
355                 but->rect.ymin += ofsy;
356                 but->rect.ymax += ofsy;
357         }
358
359         block->rect.xmax = block->panel->sizex;
360         block->rect.ymax = block->panel->sizey;
361         block->rect.xmin = block->rect.ymin = 0.0;
362 }
363
364 /**************************** drawing *******************************/
365
366 /* extern used by previewrender */
367 #if 0 /*UNUSED 2.5*/
368 static void uiPanelPush(uiBlock *block)
369 {
370         glPushMatrix(); 
371
372         if (block->panel)
373                 glTranslatef((float)block->panel->ofsx, (float)block->panel->ofsy, 0.0);
374 }
375
376 static void uiPanelPop(uiBlock *UNUSED(block))
377 {
378         glPopMatrix();
379 }
380 #endif
381
382 /* triangle 'icon' for panel header */
383 void UI_DrawTriIcon(float x, float y, char dir)
384 {
385         float f3 = 0.15 * U.widget_unit;
386         float f5 = 0.25 * U.widget_unit;
387         float f7 = 0.35 * U.widget_unit;
388         
389         if (dir == 'h') {
390                 ui_draw_anti_tria(x - f3, y - f5, x - f3, y + f5, x + f7, y);
391         }
392         else if (dir == 't') {
393                 ui_draw_anti_tria(x - f5, y - f7, x + f5, y - f7, x, y + f3);
394         }
395         else { /* 'v' = vertical, down */
396                 ui_draw_anti_tria(x - f5, y + f3, x + f5, y + f3, x, y - f7);
397         }
398 }
399
400 /* triangle 'icon' inside rect */
401 static void ui_draw_tria_rect(const rctf *rect, char dir)
402 {
403         if (dir == 'h') {
404                 float half = 0.5f * BLI_rctf_size_y(rect);
405                 ui_draw_anti_tria(rect->xmin, rect->ymin, rect->xmin, rect->ymax, rect->xmax, rect->ymin + half);
406         }
407         else {
408                 float half = 0.5f * BLI_rctf_size_x(rect);
409                 ui_draw_anti_tria(rect->xmin, rect->ymax, rect->xmax, rect->ymax, rect->xmin + half, rect->ymin);
410         }
411 }
412
413 static void ui_draw_anti_x(float x1, float y1, float x2, float y2)
414 {
415
416         /* set antialias line */
417         glEnable(GL_LINE_SMOOTH);
418         glEnable(GL_BLEND);
419
420         glLineWidth(2.0);
421         
422         fdrawline(x1, y1, x2, y2);
423         fdrawline(x1, y2, x2, y1);
424         
425         glLineWidth(1.0);
426         
427         glDisable(GL_LINE_SMOOTH);
428         glDisable(GL_BLEND);
429         
430 }
431
432 /* x 'icon' for panel header */
433 static void ui_draw_x_icon(float x, float y)
434 {
435
436         ui_draw_anti_x(x, y, x + 9.375f, y + 9.375f);
437
438 }
439
440 #define PNL_ICON    UI_UNIT_X  /* could be UI_UNIT_Y too */
441
442 static void ui_draw_panel_scalewidget(const rcti *rect)
443 {
444         float xmin, xmax, dx;
445         float ymin, ymax, dy;
446         
447         xmin = rect->xmax - PNL_HEADER + 2;
448         xmax = rect->xmax - 3;
449         ymin = rect->ymin + 3;
450         ymax = rect->ymin + PNL_HEADER - 2;
451                 
452         dx = 0.5f * (xmax - xmin);
453         dy = 0.5f * (ymax - ymin);
454         
455         glEnable(GL_BLEND);
456         glColor4ub(255, 255, 255, 50);
457         fdrawline(xmin, ymin, xmax, ymax);
458         fdrawline(xmin + dx, ymin, xmax, ymax - dy);
459         
460         glColor4ub(0, 0, 0, 50);
461         fdrawline(xmin, ymin + 1, xmax, ymax + 1);
462         fdrawline(xmin + dx, ymin + 1, xmax, ymax - dy + 1);
463         glDisable(GL_BLEND);
464 }
465
466 static void ui_draw_panel_dragwidget(const rctf *rect)
467 {
468         float xmin, xmax, dx;
469         float ymin, ymax, dy;
470         
471         xmin = rect->xmin;
472         xmax = rect->xmax;
473         ymin = rect->ymin;
474         ymax = rect->ymax;
475         
476         dx = (xmax - xmin) / 3.0f;
477         dy = (ymax - ymin) / 3.0f;
478         
479         glEnable(GL_BLEND);
480         glColor4ub(255, 255, 255, 50);
481         fdrawline(xmin, ymax, xmax, ymin);
482         fdrawline(xmin + dx, ymax, xmax, ymin + dy);
483         fdrawline(xmin + 2 * dx, ymax, xmax, ymin + 2 * dy);
484         
485         glColor4ub(0, 0, 0, 50);
486         fdrawline(xmin, ymax + 1, xmax, ymin + 1);
487         fdrawline(xmin + dx, ymax + 1, xmax, ymin + dy + 1);
488         fdrawline(xmin + 2 * dx, ymax + 1, xmax, ymin + 2 * dy + 1);
489         glDisable(GL_BLEND);
490 }
491
492
493 static void ui_draw_aligned_panel_header(uiStyle *style, uiBlock *block, const rcti *rect, char dir)
494 {
495         Panel *panel = block->panel;
496         rcti hrect;
497         int pnl_icons;
498         const char *activename = panel->drawname[0] ? panel->drawname : panel->panelname;
499
500         /* + 0.001f to avoid flirting with float inaccuracy */
501         if (panel->control & UI_PNL_CLOSE)
502                 pnl_icons = (panel->labelofs + 2 * PNL_ICON + 5) / block->aspect + 0.001f;
503         else
504                 pnl_icons = (panel->labelofs + PNL_ICON + 5) / block->aspect + 0.001f;
505         
506         /* active tab */
507         /* draw text label */
508         UI_ThemeColor(TH_TITLE);
509         
510         hrect = *rect;
511         if (dir == 'h') {
512                 hrect.xmin = rect->xmin + pnl_icons;
513                 hrect.ymin += 2.0f / block->aspect;
514                 uiStyleFontDraw(&style->paneltitle, &hrect, activename);
515         }
516         else {
517                 /* ignore 'pnl_icons', otherwise the text gets offset horizontally 
518                  * + 0.001f to avoid flirting with float inaccuracy
519                  */
520                 hrect.xmin = rect->xmin + (PNL_ICON + 5) / block->aspect + 0.001f;
521                 uiStyleFontDrawRotated(&style->paneltitle, &hrect, activename);
522         }
523 }
524
525 /* panel integrated in buttonswindow, tool/property lists etc */
526 void ui_draw_aligned_panel(uiStyle *style, uiBlock *block, const rcti *rect, const bool show_pin)
527 {
528         Panel *panel = block->panel;
529         rcti headrect;
530         rctf itemrect;
531         int ofsx;
532
533         if (panel->paneltab) return;
534         if (panel->type && (panel->type->flag & PNL_NO_HEADER)) return;
535
536         /* calculate header rect */
537         /* + 0.001f to prevent flicker due to float inaccuracy */
538         headrect = *rect;
539         headrect.ymin = headrect.ymax;
540         headrect.ymax = headrect.ymin + floor(PNL_HEADER / block->aspect + 0.001f);
541
542         {
543                 float minx = rect->xmin;
544                 float maxx = rect->xmax;
545                 float y = headrect.ymax;
546
547                 glEnable(GL_BLEND);
548
549                 if (UI_GetThemeValue(TH_PANEL_SHOW_HEADER)) {
550                         /* draw with background color */
551                         UI_ThemeColor4(TH_PANEL_HEADER);
552                         glRectf(minx, headrect.ymin + 1, maxx, y);
553
554                         fdrawline(minx, y, maxx, y);
555                         fdrawline(minx, y, maxx, y);
556                 }
557                 else if (!(panel->runtime_flag & PNL_FIRST)) {
558                         /* draw embossed separator */
559                         minx += 5.0f / block->aspect;
560                         maxx -= 5.0f / block->aspect;
561
562                         glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
563                         fdrawline(minx, y, maxx, y);
564                         glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
565                         fdrawline(minx, y - 1, maxx, y - 1);
566                 }
567
568                 glDisable(GL_BLEND);
569         }
570
571         /* draw optional pin icon */
572
573 #ifdef USE_PIN_HIDDEN
574         if (show_pin && (block->panel->flag & PNL_PIN))
575 #else
576         if (show_pin)
577 #endif
578         {
579                 glEnable(GL_BLEND);
580                 UI_icon_draw_aspect(headrect.xmax - ((PNL_ICON * 2.2f) / block->aspect), headrect.ymin + (5.0f / block->aspect),
581                                     (panel->flag & PNL_PIN) ? ICON_PINNED : ICON_UNPINNED,
582                                     (block->aspect / UI_DPI_FAC), 1.0f);
583                 glDisable(GL_BLEND);
584         }
585
586         /* horizontal title */
587         if (!(panel->flag & PNL_CLOSEDX)) {
588                 ui_draw_aligned_panel_header(style, block, &headrect, 'h');
589
590                 /* itemrect smaller */
591                 itemrect.xmax = headrect.xmax - 5.0f / block->aspect;
592                 itemrect.xmin = itemrect.xmax - BLI_rcti_size_y(&headrect);
593                 itemrect.ymin = headrect.ymin;
594                 itemrect.ymax = headrect.ymax;
595
596                 BLI_rctf_scale(&itemrect, 0.7f);
597                 ui_draw_panel_dragwidget(&itemrect);
598         }
599
600         /* if the panel is minimized vertically:
601          * (------)
602          */
603         if (panel->flag & PNL_CLOSEDY) {
604         }
605         else if (panel->flag & PNL_CLOSEDX) {
606                 /* draw vertical title */
607                 ui_draw_aligned_panel_header(style, block, &headrect, 'v');
608         }
609         /* an open panel */
610         else {
611                 /* in some occasions, draw a border */
612                 if (panel->flag & PNL_SELECT) {
613                         if (panel->control & UI_PNL_SOLID) uiSetRoundBox(UI_CNR_ALL);
614                         else uiSetRoundBox(UI_CNR_NONE);
615
616                         UI_ThemeColorShade(TH_BACK, -120);
617                         uiRoundRect(0.5f + rect->xmin, 0.5f + rect->ymin, 0.5f + rect->xmax, 0.5f + headrect.ymax + 1, 8);
618                 }
619
620                 /* panel backdrop */
621                 if (UI_GetThemeValue(TH_PANEL_SHOW_BACK)) {
622                         /* draw with background color */
623                         glEnable(GL_BLEND);
624                         UI_ThemeColor4(TH_PANEL_BACK);
625                         glRecti(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
626                 }
627
628                 if (panel->control & UI_PNL_SCALE)
629                         ui_draw_panel_scalewidget(rect);
630         }
631
632         /* draw optional close icon */
633
634         ofsx = 6;
635         if (panel->control & UI_PNL_CLOSE) {
636                 UI_ThemeColor(TH_TITLE);
637                 ui_draw_x_icon(rect->xmin + 2 + ofsx, rect->ymax + 2);
638                 ofsx = 22;
639         }
640
641         /* draw collapse icon */
642         UI_ThemeColor(TH_TITLE);
643
644         /* itemrect smaller */
645         itemrect.xmin = headrect.xmin + 5.0f / block->aspect;
646         itemrect.xmax = itemrect.xmin + BLI_rcti_size_y(&headrect);
647         itemrect.ymin = headrect.ymin;
648         itemrect.ymax = headrect.ymax;
649
650         BLI_rctf_scale(&itemrect, 0.35f);
651
652         if (panel->flag & PNL_CLOSEDY)
653                 ui_draw_tria_rect(&itemrect, 'h');
654         else if (panel->flag & PNL_CLOSEDX)
655                 ui_draw_tria_rect(&itemrect, 'h');
656         else
657                 ui_draw_tria_rect(&itemrect, 'v');
658
659         (void)ofsx;
660 }
661
662 /************************** panel alignment *************************/
663
664 static int get_panel_header(Panel *pa)
665 {
666         if (pa->type && (pa->type->flag & PNL_NO_HEADER))
667                 return 0;
668
669         return PNL_HEADER;
670 }
671
672 static int get_panel_size_y(Panel *pa)
673 {
674         if (pa->type && (pa->type->flag & PNL_NO_HEADER))
675                 return pa->sizey;
676
677         return PNL_HEADER + pa->sizey;
678 }
679
680 /* this function is needed because uiBlock and Panel itself don't
681  * change sizey or location when closed */
682 static int get_panel_real_ofsy(Panel *pa)
683 {
684         if (pa->flag & PNL_CLOSEDY) return pa->ofsy + pa->sizey;
685         else if (pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDY)) return pa->ofsy + pa->sizey;
686         else if (pa->paneltab) return pa->paneltab->ofsy;
687         else return pa->ofsy;
688 }
689
690 static int get_panel_real_ofsx(Panel *pa)
691 {
692         if (pa->flag & PNL_CLOSEDX) return pa->ofsx + get_panel_header(pa);
693         else if (pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDX)) return pa->ofsx + get_panel_header(pa);
694         else return pa->ofsx + pa->sizex;
695 }
696
697 typedef struct PanelSort {
698         Panel *pa, *orig;
699 } PanelSort;
700
701 /* note about sorting;
702  * the sortorder has a lower value for new panels being added.
703  * however, that only works to insert a single panel, when more new panels get
704  * added the coordinates of existing panels and the previously stored to-be-inserted
705  * panels do not match for sorting */
706
707 static int find_leftmost_panel(const void *a1, const void *a2)
708 {
709         const PanelSort *ps1 = a1, *ps2 = a2;
710         
711         if (ps1->pa->ofsx > ps2->pa->ofsx) return 1;
712         else if (ps1->pa->ofsx < ps2->pa->ofsx) return -1;
713         else if (ps1->pa->sortorder > ps2->pa->sortorder) return 1;
714         else if (ps1->pa->sortorder < ps2->pa->sortorder) return -1;
715
716         return 0;
717 }
718
719
720 static int find_highest_panel(const void *a1, const void *a2)
721 {
722         const PanelSort *ps1 = a1, *ps2 = a2;
723         
724         /* stick uppermost header-less panels to the top of the region -
725          * prevent them from being sorted */
726         if (ps1->pa->sortorder < ps2->pa->sortorder && ps1->pa->type->flag & PNL_NO_HEADER) return -1;
727         
728         if (ps1->pa->ofsy + ps1->pa->sizey < ps2->pa->ofsy + ps2->pa->sizey) return 1;
729         else if (ps1->pa->ofsy + ps1->pa->sizey > ps2->pa->ofsy + ps2->pa->sizey) return -1;
730         else if (ps1->pa->sortorder > ps2->pa->sortorder) return 1;
731         else if (ps1->pa->sortorder < ps2->pa->sortorder) return -1;
732         
733         return 0;
734 }
735
736 static int compare_panel(const void *a1, const void *a2)
737 {
738         const PanelSort *ps1 = a1, *ps2 = a2;
739         
740         if (ps1->pa->sortorder > ps2->pa->sortorder) return 1;
741         else if (ps1->pa->sortorder < ps2->pa->sortorder) return -1;
742         
743         return 0;
744 }
745
746 /* this doesnt draw */
747 /* returns 1 when it did something */
748 static bool uiAlignPanelStep(ScrArea *sa, ARegion *ar, const float fac, const bool drag)
749 {
750         Panel *pa;
751         PanelSort *ps, *panelsort, *psnext;
752         int a, tot = 0;
753         bool done;
754         int align = panel_aligned(sa, ar);
755         bool has_category_tabs = UI_panel_category_is_visible(ar);
756         
757         /* count active, not tabbed panels */
758         for (pa = ar->panels.first; pa; pa = pa->next)
759                 if ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab == NULL)
760                         tot++;
761
762         if (tot == 0) return 0;
763
764         /* extra; change close direction? */
765         for (pa = ar->panels.first; pa; pa = pa->next) {
766                 if ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab == NULL) {
767                         if ((pa->flag & PNL_CLOSEDX) && (align == BUT_VERTICAL))
768                                 pa->flag ^= PNL_CLOSED;
769                         else if ((pa->flag & PNL_CLOSEDY) && (align == BUT_HORIZONTAL))
770                                 pa->flag ^= PNL_CLOSED;
771                 }
772         }
773
774         /* sort panels */
775         panelsort = MEM_callocN(tot * sizeof(PanelSort), "panelsort");
776         
777         ps = panelsort;
778         for (pa = ar->panels.first; pa; pa = pa->next) {
779                 if ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab == NULL) {
780                         ps->pa = MEM_dupallocN(pa);
781                         ps->orig = pa;
782                         ps++;
783                 }
784         }
785         
786         if (drag) {
787                 /* while we are dragging, we sort on location and update sortorder */
788                 if (align == BUT_VERTICAL)
789                         qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
790                 else
791                         qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
792
793                 for (ps = panelsort, a = 0; a < tot; a++, ps++)
794                         ps->orig->sortorder = a;
795         }
796         else
797                 /* otherwise use sortorder */
798                 qsort(panelsort, tot, sizeof(PanelSort), compare_panel);
799         
800         /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
801         ps = panelsort;
802         ps->pa->ofsx = 0;
803         ps->pa->ofsy = -get_panel_size_y(ps->pa);
804
805         if (has_category_tabs) {
806                 if (align == BUT_VERTICAL) {
807                         ps->pa->ofsx += UI_PANEL_CATEGORY_MARGIN_WIDTH;
808                 }
809         }
810
811         for (a = 0; a < tot - 1; a++, ps++) {
812                 psnext = ps + 1;
813
814                 if (align == BUT_VERTICAL) {
815                         psnext->pa->ofsx = ps->pa->ofsx;
816                         psnext->pa->ofsy = get_panel_real_ofsy(ps->pa) - get_panel_size_y(psnext->pa);
817                 }
818                 else {
819                         psnext->pa->ofsx = get_panel_real_ofsx(ps->pa);
820                         psnext->pa->ofsy = ps->pa->ofsy + get_panel_size_y(ps->pa) - get_panel_size_y(psnext->pa);
821                 }
822         }
823         
824         /* we interpolate */
825         done = false;
826         ps = panelsort;
827         for (a = 0; a < tot; a++, ps++) {
828                 if ((ps->pa->flag & PNL_SELECT) == 0) {
829                         if ((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) {
830                                 ps->orig->ofsx = iroundf(fac * (float)ps->pa->ofsx + (1.0f - fac) * (float)ps->orig->ofsx);
831                                 ps->orig->ofsy = iroundf(fac * (float)ps->pa->ofsy + (1.0f - fac) * (float)ps->orig->ofsy);
832                                 done = true;
833                         }
834                 }
835         }
836
837         /* copy locations to tabs */
838         for (pa = ar->panels.first; pa; pa = pa->next)
839                 if (pa->paneltab && (pa->runtime_flag & PNL_ACTIVE))
840                         ui_panel_copy_offset(pa, pa->paneltab);
841
842         /* free panelsort array */
843         for (ps = panelsort, a = 0; a < tot; a++, ps++) {
844                 MEM_freeN(ps->pa);
845         }
846         MEM_freeN(panelsort);
847         
848         return done;
849 }
850
851 static void ui_panels_size(ScrArea *sa, ARegion *ar, int *x, int *y)
852 {
853         Panel *pa;
854         int align = panel_aligned(sa, ar);
855         int sizex = 0;
856         int sizey = 0;
857
858         /* compute size taken up by panels, for setting in view2d */
859         for (pa = ar->panels.first; pa; pa = pa->next) {
860                 if (pa->runtime_flag & PNL_ACTIVE) {
861                         int pa_sizex, pa_sizey;
862
863                         if (align == BUT_VERTICAL) {
864                                 pa_sizex = pa->ofsx + pa->sizex;
865                                 pa_sizey = get_panel_real_ofsy(pa);
866                         }
867                         else {
868                                 pa_sizex = get_panel_real_ofsx(pa) + pa->sizex;
869                                 pa_sizey = pa->ofsy + get_panel_size_y(pa);
870                         }
871
872                         sizex = max_ii(sizex, pa_sizex);
873                         sizey = min_ii(sizey, pa_sizey);
874                 }
875         }
876
877         if (sizex == 0)
878                 sizex = UI_PANEL_WIDTH;
879         if (sizey == 0)
880                 sizey = -UI_PANEL_WIDTH;
881         
882         *x = sizex;
883         *y = sizey;
884 }
885
886 static void ui_do_animate(const bContext *C, Panel *panel)
887 {
888         uiHandlePanelData *data = panel->activedata;
889         ScrArea *sa = CTX_wm_area(C);
890         ARegion *ar = CTX_wm_region(C);
891         float fac;
892
893         fac = (PIL_check_seconds_timer() - data->starttime) / ANIMATION_TIME;
894         fac = min_ff(sqrt(fac), 1.0f);
895
896         /* for max 1 second, interpolate positions */
897         if (uiAlignPanelStep(sa, ar, fac, false)) {
898                 ED_region_tag_redraw(ar);
899         }
900         else {
901                 fac = 1.0f;
902         }
903
904         if (fac >= 1.0f) {
905                 panel_activate_state(C, panel, PANEL_STATE_EXIT);
906                 return;
907         }
908 }
909
910 void uiBeginPanels(const bContext *UNUSED(C), ARegion *ar)
911 {
912         Panel *pa;
913
914         /* set all panels as inactive, so that at the end we know
915          * which ones were used */
916         for (pa = ar->panels.first; pa; pa = pa->next) {
917                 if (pa->runtime_flag & PNL_ACTIVE)
918                         pa->runtime_flag = PNL_WAS_ACTIVE;
919                 else
920                         pa->runtime_flag = 0;
921         }
922 }
923
924 /* only draws blocks with panels */
925 void uiEndPanels(const bContext *C, ARegion *ar, int *x, int *y)
926 {
927         ScrArea *sa = CTX_wm_area(C);
928         uiBlock *block;
929         Panel *panot, *panew, *patest, *pa, *firstpa;
930         
931         /* offset contents */
932         for (block = ar->uiblocks.first; block; block = block->next)
933                 if (block->active && block->panel)
934                         ui_offset_panel_block(block);
935
936         /* consistency; are panels not made, whilst they have tabs */
937         for (panot = ar->panels.first; panot; panot = panot->next) {
938                 if ((panot->runtime_flag & PNL_ACTIVE) == 0) {  /* not made */
939
940                         for (panew = ar->panels.first; panew; panew = panew->next) {
941                                 if ((panew->runtime_flag & PNL_ACTIVE)) {
942                                         if (panew->paneltab == panot) {  /* panew is tab in notmade pa */
943                                                 break;
944                                         }
945                                 }
946                         }
947                         /* now panew can become the new parent, check all other tabs */
948                         if (panew) {
949                                 for (patest = ar->panels.first; patest; patest = patest->next) {
950                                         if (patest->paneltab == panot) {
951                                                 patest->paneltab = panew;
952                                         }
953                                 }
954                                 panot->paneltab = panew;
955                                 panew->paneltab = NULL;
956                                 ED_region_tag_redraw(ar); /* the buttons panew were not made */
957                         }
958                 }
959         }
960
961         /* re-align, possibly with animation */
962         if (panels_re_align(sa, ar, &pa)) {
963                 /* XXX code never gets here... PNL_ANIM_ALIGN flag is never set */
964                 if (pa)
965                         panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
966                 else
967                         uiAlignPanelStep(sa, ar, 1.0, false);
968         }
969
970         /* tag first panel */
971         firstpa = NULL;
972         for (block = ar->uiblocks.first; block; block = block->next)
973                 if (block->active && block->panel)
974                         if (!firstpa || block->panel->sortorder < firstpa->sortorder)
975                                 firstpa = block->panel;
976         
977         if (firstpa)
978                 firstpa->runtime_flag |= PNL_FIRST;
979         
980         /* compute size taken up by panel */
981         ui_panels_size(sa, ar, x, y);
982 }
983
984 void uiDrawPanels(const bContext *C, ARegion *ar)
985 {
986         uiBlock *block;
987
988         UI_ThemeClearColor(TH_BACK);
989         
990         /* draw panels, selected on top */
991         for (block = ar->uiblocks.first; block; block = block->next) {
992                 if (block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
993                         uiDrawBlock(C, block);
994                 }
995         }
996
997         for (block = ar->uiblocks.first; block; block = block->next) {
998                 if (block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
999                         uiDrawBlock(C, block);
1000                 }
1001         }
1002 }
1003
1004 void uiScalePanels(ARegion *ar, float new_width)
1005 {
1006         uiBlock *block;
1007         uiBut *but;
1008         
1009         for (block = ar->uiblocks.first; block; block = block->next) {
1010                 if (block->panel) {
1011                         float fac = new_width / (float)block->panel->sizex;
1012                         printf("scaled %f\n", fac);
1013                         block->panel->sizex = new_width;
1014                         
1015                         for (but = block->buttons.first; but; but = but->next) {
1016                                 but->rect.xmin *= fac;
1017                                 but->rect.xmax *= fac;
1018                         }
1019                 }
1020         }
1021 }
1022
1023 /* ------------ panel merging ---------------- */
1024
1025 static void check_panel_overlap(ARegion *ar, Panel *panel)
1026 {
1027         Panel *pa;
1028
1029         /* also called with (panel == NULL) for clear */
1030         
1031         for (pa = ar->panels.first; pa; pa = pa->next) {
1032                 pa->flag &= ~PNL_OVERLAP;
1033                 if (panel && (pa != panel)) {
1034                         if (pa->paneltab == NULL && (pa->runtime_flag & PNL_ACTIVE)) {
1035                                 float safex = 0.2, safey = 0.2;
1036                                 
1037                                 if (pa->flag & PNL_CLOSEDX) safex = 0.05;
1038                                 else if (pa->flag & PNL_CLOSEDY) safey = 0.05;
1039                                 else if (panel->flag & PNL_CLOSEDX) safex = 0.05;
1040                                 else if (panel->flag & PNL_CLOSEDY) safey = 0.05;
1041
1042                                 if (pa->ofsx > panel->ofsx - safex * panel->sizex)
1043                                         if (pa->ofsx + pa->sizex < panel->ofsx + (1.0f + safex) * panel->sizex)
1044                                                 if (pa->ofsy > panel->ofsy - safey * panel->sizey)
1045                                                         if (pa->ofsy + pa->sizey < panel->ofsy + (1.0f + safey) * panel->sizey)
1046                                                                 pa->flag |= PNL_OVERLAP;
1047                         }
1048                 }
1049         }
1050 }
1051
1052 /************************ panel dragging ****************************/
1053
1054 static void ui_do_drag(const bContext *C, const wmEvent *event, Panel *panel)
1055 {
1056         uiHandlePanelData *data = panel->activedata;
1057         ScrArea *sa = CTX_wm_area(C);
1058         ARegion *ar = CTX_wm_region(C);
1059         short align = panel_aligned(sa, ar), dx = 0, dy = 0;
1060         
1061         /* first clip for window, no dragging outside */
1062         if (!BLI_rcti_isect_pt_v(&ar->winrct, &event->x))
1063                 return;
1064
1065         dx = (event->x - data->startx) & ~(PNL_GRID - 1);
1066         dy = (event->y - data->starty) & ~(PNL_GRID - 1);
1067
1068         dx *= (float)BLI_rctf_size_x(&ar->v2d.cur) / (float)BLI_rcti_size_x(&ar->winrct);
1069         dy *= (float)BLI_rctf_size_y(&ar->v2d.cur) / (float)BLI_rcti_size_y(&ar->winrct);
1070         
1071         if (data->state == PANEL_STATE_DRAG_SCALE) {
1072                 panel->sizex = MAX2(data->startsizex + dx, UI_PANEL_MINX);
1073                 
1074                 if (data->startsizey - dy < UI_PANEL_MINY)
1075                         dy = -UI_PANEL_MINY + data->startsizey;
1076
1077                 panel->sizey = data->startsizey - dy;
1078                 panel->ofsy = data->startofsy + dy;
1079         }
1080         else {
1081                 /* reset the panel snapping, to allow dragging away from snapped edges */
1082                 panel->snap = PNL_SNAP_NONE;
1083                 
1084                 panel->ofsx = data->startofsx + dx;
1085                 panel->ofsy = data->startofsy + dy;
1086                 check_panel_overlap(ar, panel);
1087                 
1088                 if (align) uiAlignPanelStep(sa, ar, 0.2, true);
1089         }
1090
1091         ED_region_tag_redraw(ar);
1092 }
1093
1094 /******************* region level panel interaction *****************/
1095
1096
1097 /* this function is supposed to call general window drawing too */
1098 /* also it supposes a block has panel, and isn't a menu */
1099 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my, int event, short ctrl, short shift)
1100 {
1101         ScrArea *sa = CTX_wm_area(C);
1102         ARegion *ar = CTX_wm_region(C);
1103         Panel *pa;
1104 #ifdef USE_PIN_HIDDEN
1105         const bool show_pin = UI_panel_category_is_visible(ar) && (block->panel->flag & PNL_PIN);
1106 #else
1107         const bool show_pin = UI_panel_category_is_visible(ar);
1108 #endif
1109
1110         int align = panel_aligned(sa, ar), button = 0;
1111
1112         rctf rect_drag, rect_pin;
1113         float rect_leftmost;
1114
1115
1116         /* drag and pin rect's */
1117         rect_drag = block->rect;
1118         rect_drag.xmin = block->rect.xmax - (PNL_ICON * 1.5f);
1119         rect_pin = rect_drag;
1120         if (show_pin) {
1121                 BLI_rctf_translate(&rect_pin, -PNL_ICON, 0.0f);
1122         }
1123         rect_leftmost = rect_pin.xmin;
1124
1125         /* mouse coordinates in panel space! */
1126         
1127         /* XXX weak code, currently it assumes layout style for location of widgets */
1128         
1129         /* check open/collapsed button */
1130         if (event == RETKEY)
1131                 button = 1;
1132         else if (event == AKEY)
1133                 button = 1;
1134         else if (ELEM3(event, 0, RETKEY, LEFTMOUSE) && shift) {
1135                 block->panel->flag ^= PNL_PIN;
1136                 button = 2;
1137         }
1138         else if (block->panel->flag & PNL_CLOSEDX) {
1139                 if (my >= block->rect.ymax) button = 1;
1140         }
1141         else if (block->panel->control & UI_PNL_CLOSE) {
1142                 /* whole of header can be used to collapse panel (except top-right corner) */
1143                 if (mx <= block->rect.xmax - 8 - PNL_ICON) button = 2;
1144                 //else if (mx <= block->rect.xmin + 10 + 2 * PNL_ICON + 2) button = 1;
1145         }
1146         else if (mx < rect_leftmost) {
1147                 button = 1;
1148         }
1149         
1150         if (button) {
1151                 if (button == 2) {  /* close */
1152                         ED_region_tag_redraw(ar);
1153                 }
1154                 else {  /* collapse */
1155                         if (ctrl)
1156                                 panels_collapse_all(sa, ar, block->panel);
1157
1158                         if (block->panel->flag & PNL_CLOSED) {
1159                                 block->panel->flag &= ~PNL_CLOSED;
1160                                 /* snap back up so full panel aligns with screen edge */
1161                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1162                                         block->panel->ofsy = 0;
1163                         }
1164                         else if (align == BUT_HORIZONTAL) {
1165                                 block->panel->flag |= PNL_CLOSEDX;
1166                         }
1167                         else {
1168                                 /* snap down to bottom screen edge*/
1169                                 block->panel->flag |= PNL_CLOSEDY;
1170                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1171                                         block->panel->ofsy = -block->panel->sizey;
1172                         }
1173                         
1174                         for (pa = ar->panels.first; pa; pa = pa->next) {
1175                                 if (pa->paneltab == block->panel) {
1176                                         if (block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1177                                         else pa->flag &= ~PNL_CLOSED;
1178                                 }
1179                         }
1180                 }
1181
1182                 if (align)
1183                         panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1184                 else
1185                         ED_region_tag_redraw(ar);
1186         }
1187         else if (BLI_rctf_isect_x(&rect_drag, mx)) {
1188                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1189         }
1190         else if (show_pin && BLI_rctf_isect_x(&rect_pin, mx)) {
1191                 block->panel->flag ^= PNL_PIN;
1192                 ED_region_tag_redraw(ar);
1193         }
1194 }
1195
1196 bool UI_panel_category_is_visible(ARegion *ar)
1197 {
1198         /* more then one */
1199         return ar->panels_category.first && ar->panels_category.first != ar->panels_category.last;
1200 }
1201
1202 PanelCategoryDyn *UI_panel_category_find(ARegion *ar, const char *idname)
1203 {
1204         return BLI_findstring(&ar->panels_category, idname, offsetof(PanelCategoryDyn, idname));
1205 }
1206
1207 PanelCategoryStack *UI_panel_category_active_find(ARegion *ar, const char *idname)
1208 {
1209         return BLI_findstring(&ar->panels_category_active, idname, offsetof(PanelCategoryStack, idname));
1210 }
1211
1212 const char *UI_panel_category_active_get(ARegion *ar, bool set_fallback)
1213 {
1214         PanelCategoryStack *pc_act;
1215
1216         for (pc_act = ar->panels_category_active.first; pc_act; pc_act = pc_act->next) {
1217                 if (UI_panel_category_find(ar, pc_act->idname)) {
1218                         return pc_act->idname;
1219                 }
1220         }
1221
1222         if (set_fallback) {
1223                 PanelCategoryDyn *pc_dyn = ar->panels_category.first;
1224                 if (pc_dyn) {
1225                         UI_panel_category_active_set(ar, pc_dyn->idname);
1226                         return pc_dyn->idname;
1227                 }
1228         }
1229
1230         return NULL;
1231 }
1232
1233 void UI_panel_category_active_set(ARegion *ar, const char *idname)
1234 {
1235         ListBase *lb = &ar->panels_category_active;
1236         PanelCategoryStack *pc_act = UI_panel_category_active_find(ar, idname);
1237
1238         if (pc_act) {
1239                 BLI_remlink(lb, pc_act);
1240         }
1241         else {
1242                 pc_act = MEM_callocN(sizeof(PanelCategoryStack), __func__);
1243                 BLI_strncpy(pc_act->idname, idname, sizeof(pc_act->idname));
1244         }
1245
1246         BLI_addhead(lb, pc_act);
1247
1248
1249         /* validate all active panels, we could do this on load,
1250          * they are harmless - but we should remove somewhere.
1251          * (addons could define own and gather cruft over time) */
1252         {
1253                 PanelCategoryStack *pc_act_next;
1254                 /* intentionally skip first */
1255                 pc_act_next = pc_act->next;
1256                 while ((pc_act = pc_act_next)) {
1257                         pc_act_next = pc_act->next;
1258                         if (!BLI_findstring(&ar->type->paneltypes, pc_act->idname, offsetof(PanelType, category))) {
1259                                 BLI_remlink(lb, pc_act);
1260                         }
1261                 }
1262         }
1263 }
1264
1265 PanelCategoryDyn *UI_panel_category_find_mouse_over_ex(ARegion *ar, const int x, const int y)
1266 {
1267         PanelCategoryDyn *ptd;
1268
1269         for (ptd = ar->panels_category.first; ptd; ptd = ptd->next) {
1270                 if (BLI_rcti_isect_pt(&ptd->rect, x, y)) {
1271                         return ptd;
1272                 }
1273         }
1274
1275         return NULL;
1276 }
1277
1278 PanelCategoryDyn *UI_panel_category_find_mouse_over(ARegion *ar, const wmEvent *event)
1279 {
1280         return UI_panel_category_find_mouse_over_ex(ar, event->mval[0], event->mval[1]);
1281 }
1282
1283
1284 void UI_panel_category_add(ARegion *ar, const char *name)
1285 {
1286         PanelCategoryDyn *pc_dyn = MEM_callocN(sizeof(*pc_dyn), __func__);
1287         BLI_addtail(&ar->panels_category, pc_dyn);
1288
1289         BLI_strncpy(pc_dyn->idname, name, sizeof(pc_dyn->idname));
1290
1291         /* 'pc_dyn->rect' must be set on draw */
1292 }
1293
1294 void UI_panel_category_clear_all(ARegion *ar)
1295 {
1296         BLI_freelistN(&ar->panels_category);
1297 }
1298
1299 /* based on uiDrawBox, check on making a version which allows us to skip some sides */
1300 static void ui_panel_category_draw_tab(int mode, float minx, float miny, float maxx, float maxy, float rad,
1301                                        int roundboxtype,
1302                                        const bool use_highlight, const bool use_shadow,
1303                                        const unsigned char highlight_fade[3])
1304 {
1305         float vec[4][2] = {
1306             {0.195, 0.02},
1307             {0.55, 0.169},
1308             {0.831, 0.45},
1309             {0.98, 0.805}};
1310         int a;
1311
1312         /* mult */
1313         for (a = 0; a < 4; a++) {
1314                 mul_v2_fl(vec[a], rad);
1315         }
1316
1317         glBegin(mode);
1318
1319         /* start with corner right-top */
1320         if (use_highlight) {
1321                 if (roundboxtype & UI_CNR_TOP_RIGHT) {
1322                         glVertex2f(maxx, maxy - rad);
1323                         for (a = 0; a < 4; a++) {
1324                                 glVertex2f(maxx - vec[a][1], maxy - rad + vec[a][0]);
1325                         }
1326                         glVertex2f(maxx - rad, maxy);
1327                 }
1328                 else {
1329                         glVertex2f(maxx, maxy);
1330                 }
1331
1332                 /* corner left-top */
1333                 if (roundboxtype & UI_CNR_TOP_LEFT) {
1334                         glVertex2f(minx + rad, maxy);
1335                         for (a = 0; a < 4; a++) {
1336                                 glVertex2f(minx + rad - vec[a][0], maxy - vec[a][1]);
1337                         }
1338                         glVertex2f(minx, maxy - rad);
1339                 }
1340                 else {
1341                         glVertex2f(minx, maxy);
1342                 }
1343         }
1344
1345         if (use_highlight && !use_shadow) {
1346                 if (highlight_fade) {
1347                         glColor3ubv(highlight_fade);
1348                 }
1349                 glVertex2f(minx, miny + rad);
1350                 glEnd();
1351                 return;
1352         }
1353
1354         /* corner left-bottom */
1355         if (roundboxtype & UI_CNR_BOTTOM_LEFT) {
1356                 glVertex2f(minx, miny + rad);
1357                 for (a = 0; a < 4; a++) {
1358                         glVertex2f(minx + vec[a][1], miny + rad - vec[a][0]);
1359                 }
1360                 glVertex2f(minx + rad, miny);
1361         }
1362         else {
1363                 glVertex2f(minx, miny);
1364         }
1365
1366         /* corner right-bottom */
1367
1368         if (roundboxtype & UI_CNR_BOTTOM_RIGHT) {
1369                 glVertex2f(maxx - rad, miny);
1370                 for (a = 0; a < 4; a++) {
1371                         glVertex2f(maxx - rad + vec[a][0], miny + vec[a][1]);
1372                 }
1373                 glVertex2f(maxx, miny + rad);
1374         }
1375         else {
1376                 glVertex2f(maxx, miny);
1377         }
1378
1379         glEnd();
1380 }
1381
1382
1383 /**
1384  * Draw vertical tabs on the left side of the region,
1385  * one tab per category.
1386  */
1387 void UI_panel_category_draw_all(ARegion *ar, const char *category_id_active)
1388 {
1389         /* no tab outlines for */
1390 // #define USE_FLAT_INACTIVE
1391         View2D *v2d = &ar->v2d;
1392         uiStyle *style = UI_GetStyle();
1393         const uiFontStyle *fstyle = &style->widget;
1394         const int fontid = fstyle->uifont_id;
1395         short fstyle_points = fstyle->points;
1396
1397         PanelCategoryDyn *pc_dyn;
1398         const float aspect = ((uiBlock *)ar->uiblocks.first)->aspect;
1399         const float zoom = 1.0f / aspect;
1400         const int px = max_ii(1, iroundf(U.pixelsize));
1401         const int category_tabs_width = iroundf(UI_PANEL_CATEGORY_MARGIN_WIDTH * zoom);
1402         const float dpi_fac = UI_DPI_FAC;
1403         const int tab_v_pad_text = iroundf((2 + ((px * 3) * dpi_fac)) * zoom);  /* pading of tabs around text */
1404         const int tab_v_pad = iroundf((4 + (2 * px * dpi_fac)) * zoom);  /* padding between tabs */
1405         const float tab_curve_radius = ((px * 3) * dpi_fac) * zoom;
1406         const int roundboxtype = UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT;
1407         bool is_alpha;
1408         bool do_scaletabs = false;
1409 #ifdef USE_FLAT_INACTIVE
1410         bool is_active_prev = false;
1411 #endif
1412         float scaletabs = 1.0f;
1413         /* same for all tabs */
1414         const int rct_xmin = v2d->mask.xmin + 3;  /* intentionally dont scale by 'px' */
1415         const int rct_xmax = v2d->mask.xmin + category_tabs_width;
1416         const int text_v_ofs = (rct_xmax - rct_xmin) * 0.3f;
1417
1418         int y_ofs = tab_v_pad;
1419
1420         /* Primary theme colors */
1421         unsigned char theme_col_back[4];
1422         unsigned char theme_col_text[3];
1423         unsigned char theme_col_text_hi[3];
1424
1425         /* Tab colors */
1426         unsigned char theme_col_tab_bg[4];
1427         unsigned char theme_col_tab_active[3];
1428         unsigned char theme_col_tab_inactive[3];
1429
1430         /* Secondary theme colors */
1431         unsigned char theme_col_tab_outline[3];
1432         unsigned char theme_col_tab_divider[3];  /* line that divides tabs from the main area */
1433         unsigned char theme_col_tab_highlight[3];
1434         unsigned char theme_col_tab_highlight_inactive[3];
1435
1436
1437
1438         UI_GetThemeColor4ubv(TH_BACK, theme_col_back);
1439         UI_GetThemeColor3ubv(TH_TEXT, theme_col_text);
1440         UI_GetThemeColor3ubv(TH_TEXT_HI, theme_col_text_hi);
1441
1442         UI_GetThemeColor4ubv(TH_TAB_BACK, theme_col_tab_bg);
1443         UI_GetThemeColor3ubv(TH_TAB_ACTIVE, theme_col_tab_active);
1444         UI_GetThemeColor3ubv(TH_TAB_INACTIVE, theme_col_tab_inactive);
1445         UI_GetThemeColor3ubv(TH_TAB_OUTLINE, theme_col_tab_outline);
1446
1447         interp_v3_v3v3_uchar(theme_col_tab_divider, theme_col_back, theme_col_tab_outline, 0.3f);
1448         interp_v3_v3v3_uchar(theme_col_tab_highlight, theme_col_back, theme_col_text_hi, 0.2f);
1449         interp_v3_v3v3_uchar(theme_col_tab_highlight_inactive, theme_col_tab_inactive, theme_col_text_hi, 0.12f);
1450
1451         is_alpha = (ar->overlap && (theme_col_back[3] != 255));
1452
1453         if (fstyle->kerning == 1) {
1454                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1455         }
1456
1457         BLF_enable(fontid, BLF_ROTATION);
1458         BLF_rotation(fontid, M_PI / 2);
1459         //uiStyleFontSet(&style->widget);
1460         ui_fontscale(&fstyle_points, aspect / (U.pixelsize * 1.1f));
1461         BLF_size(fontid, fstyle_points, U.dpi);
1462
1463         BLF_enable(fontid, BLF_SHADOW);
1464         BLF_shadow(fontid, 3, 1.0f, 1.0f, 1.0f, 0.25f);
1465         BLF_shadow_offset(fontid, -1, -1);
1466
1467         BLI_assert(UI_panel_category_is_visible(ar));
1468
1469
1470         /* calculate tab rect's and check if we need to scale down */
1471         for (pc_dyn = ar->panels_category.first; pc_dyn; pc_dyn = pc_dyn->next) {
1472                 rcti *rct = &pc_dyn->rect;
1473                 const char *category_id = pc_dyn->idname;
1474                 const char *category_id_draw = IFACE_(category_id);
1475                 const int category_width = BLF_width(fontid, category_id_draw, BLF_DRAW_STR_DUMMY_MAX);
1476
1477                 rct->xmin = rct_xmin;
1478                 rct->xmax = rct_xmax;
1479
1480                 rct->ymin = v2d->mask.ymax - (y_ofs + category_width + (tab_v_pad_text * 2));
1481                 rct->ymax = v2d->mask.ymax - (y_ofs);
1482
1483                 y_ofs += category_width + tab_v_pad + (tab_v_pad_text * 2);
1484         }
1485
1486         if (y_ofs > BLI_rcti_size_y(&v2d->mask)) {
1487                 scaletabs = (float)BLI_rcti_size_y(&v2d->mask) / (float)y_ofs;
1488
1489                 for (pc_dyn = ar->panels_category.first; pc_dyn; pc_dyn = pc_dyn->next) {
1490                         rcti *rct = &pc_dyn->rect;
1491                         rct->ymin = ((rct->ymin - v2d->mask.ymax) * scaletabs) + v2d->mask.ymax;
1492                         rct->ymax = ((rct->ymax - v2d->mask.ymax) * scaletabs) + v2d->mask.ymax;
1493                 }
1494
1495                 do_scaletabs = true;
1496         }
1497
1498
1499         /* begin drawing */
1500         glEnable(GL_LINE_SMOOTH);
1501
1502         /* draw the background */
1503         if (is_alpha) {
1504                 glEnable(GL_BLEND);
1505                 glColor4ubv(theme_col_tab_bg);
1506         }
1507         else {
1508                 glColor3ubv(theme_col_tab_bg);
1509         }
1510
1511         glRecti(v2d->mask.xmin, v2d->mask.ymin, v2d->mask.xmin + category_tabs_width, v2d->mask.ymax);
1512
1513         if (is_alpha) {
1514                 glDisable(GL_BLEND);
1515         }
1516
1517         for (pc_dyn = ar->panels_category.first; pc_dyn; pc_dyn = pc_dyn->next) {
1518                 const rcti *rct = &pc_dyn->rect;
1519                 const char *category_id = pc_dyn->idname;
1520                 const char *category_id_draw = IFACE_(category_id);
1521                 int category_width = BLI_rcti_size_y(rct) - (tab_v_pad_text * 2);
1522                 size_t category_draw_len = BLF_DRAW_STR_DUMMY_MAX;
1523                 // int category_width = BLF_width(fontid, category_id_draw, BLF_DRAW_STR_DUMMY_MAX);
1524
1525                 const bool is_active = STREQ(category_id, category_id_active);
1526
1527                 glEnable(GL_BLEND);
1528
1529 #ifdef USE_FLAT_INACTIVE
1530                 if (is_active)
1531 #endif
1532                 {
1533                         glColor3ubv(is_active ? theme_col_tab_active : theme_col_tab_inactive);
1534                         ui_panel_category_draw_tab(GL_POLYGON, rct->xmin, rct->ymin, rct->xmax, rct->ymax,
1535                                                    tab_curve_radius - px, roundboxtype, true, true, NULL);
1536
1537                         /* tab outline */
1538                         glColor3ubv(theme_col_tab_outline);
1539                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1540                         ui_panel_category_draw_tab(GL_LINE_STRIP, rct->xmin - px, rct->ymin - px, rct->xmax - px, rct->ymax + px,
1541                                                    tab_curve_radius, roundboxtype, true, true, NULL);
1542                         /* tab highlight (3d look) */
1543                         glShadeModel(GL_SMOOTH);
1544                         glColor3ubv(is_active ? theme_col_tab_highlight : theme_col_tab_highlight_inactive);
1545                         ui_panel_category_draw_tab(GL_LINE_STRIP, rct->xmin, rct->ymin, rct->xmax, rct->ymax,
1546                                                    tab_curve_radius, roundboxtype, true, false,
1547                                                    is_active ? theme_col_back : theme_col_tab_inactive);
1548                         glShadeModel(GL_FLAT);
1549
1550                         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1551                 }
1552
1553                 /* tab blackline */
1554                 if (!is_active) {
1555                         glColor3ubv(theme_col_tab_divider);
1556                         glRecti(v2d->mask.xmin + category_tabs_width - px,
1557                                 rct->ymin - tab_v_pad,
1558                                 v2d->mask.xmin + category_tabs_width,
1559                                 rct->ymax + tab_v_pad);
1560                 }
1561
1562                 if (do_scaletabs) {
1563                         category_draw_len = BLF_width_to_strlen(fontid, category_id_draw, category_draw_len,
1564                                                                 category_width, NULL);
1565                 }
1566
1567                 BLF_position(fontid, rct->xmax - text_v_ofs, rct->ymin + tab_v_pad_text, 0.0f);
1568
1569                 /* tab titles */
1570
1571                 /* draw white shadow to give text more depth */
1572                 glColor3ubv(theme_col_text);
1573
1574                 /* main tab title */
1575                 BLF_draw(fontid, category_id_draw, category_draw_len);
1576
1577                 glDisable(GL_BLEND);
1578
1579                 /* tab blackline remaining (last tab) */
1580                 if (pc_dyn->prev == NULL) {
1581                         glColor3ubv(theme_col_tab_divider);
1582                         glRecti(v2d->mask.xmin + category_tabs_width - px,
1583                                 rct->ymax + px,
1584                                 v2d->mask.xmin + category_tabs_width,
1585                                 v2d->mask.ymax);
1586                 }
1587                 if (pc_dyn->next == NULL) {
1588                         glColor3ubv(theme_col_tab_divider);
1589                         glRecti(v2d->mask.xmin + category_tabs_width - px,
1590                                 0,
1591                                 v2d->mask.xmin + category_tabs_width,
1592                                 rct->ymin);
1593                 }
1594
1595 #ifdef USE_FLAT_INACTIVE
1596                 /* draw line between inactive tabs */
1597                 if (is_active == false && is_active_prev == false && pc_dyn->prev) {
1598                         glColor3ubv(theme_col_tab_divider);
1599                         glRecti(v2d->mask.xmin + (category_tabs_width / 5),
1600                                 rct->ymax + px,
1601                                 (v2d->mask.xmin + category_tabs_width) - (category_tabs_width / 5),
1602                                 rct->ymax + (px * 3));
1603                 }
1604
1605                 is_active_prev = is_active;
1606 #endif
1607
1608                 /* not essential, but allows events to be handled right up until the region edge [#38171] */
1609                 pc_dyn->rect.xmin = v2d->mask.xmin;
1610         }
1611
1612         glDisable(GL_LINE_SMOOTH);
1613
1614         BLF_disable(fontid, BLF_ROTATION);
1615
1616         BLF_disable(fontid, BLF_SHADOW);
1617
1618         if (fstyle->kerning == 1) {
1619                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1620         }
1621
1622 #undef USE_FLAT_INACTIVE
1623 }
1624
1625 /* XXX should become modal keymap */
1626 /* AKey is opening/closing panels, independent of button state now */
1627
1628 int ui_handler_panel_region(bContext *C, const wmEvent *event, ARegion *ar)
1629 {
1630         uiBlock *block;
1631         Panel *pa;
1632         int retval, mx, my;
1633         bool has_category_tabs = UI_panel_category_is_visible(ar);
1634
1635         retval = WM_UI_HANDLER_CONTINUE;
1636
1637         if (has_category_tabs) {
1638                 if (event->val == KM_PRESS) {
1639                         if (event->type == LEFTMOUSE) {
1640                                 PanelCategoryDyn *pc_dyn = UI_panel_category_find_mouse_over(ar, event);
1641                                 if (pc_dyn) {
1642                                         UI_panel_category_active_set(ar, pc_dyn->idname);
1643                                         ED_region_tag_redraw(ar);
1644
1645                                         /* reset scroll to the top [#38348] */
1646                                         UI_view2d_offset(&ar->v2d, -1.0f, 1.0f);
1647
1648                                         retval = WM_UI_HANDLER_BREAK;
1649                                 }
1650                         }
1651                         else if (ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE)) {
1652                                 /* mouse wheel cycle tabs */
1653
1654                                 /* first check if the mouse is in the tab region */
1655                                 if (event->ctrl || (event->mval[0] < ((PanelCategoryDyn *)ar->panels_category.first)->rect.xmax)) {
1656                                         const char *category = UI_panel_category_active_get(ar, false);
1657                                         if (LIKELY(category)) {
1658                                                 PanelCategoryDyn *pc_dyn = UI_panel_category_find(ar, category);
1659                                                 if (LIKELY(pc_dyn)) {
1660                                                         pc_dyn = (event->type == WHEELDOWNMOUSE) ? pc_dyn->next : pc_dyn->prev;
1661                                                         if (pc_dyn) {
1662                                                                 /* intentionally don't reset scroll in this case,
1663                                                                  * this allows for quick browsing between tabs */
1664                                                                 UI_panel_category_active_set(ar, pc_dyn->idname);
1665                                                                 ED_region_tag_redraw(ar);
1666                                                         }
1667                                                 }
1668                                         }
1669                                         retval = WM_UI_HANDLER_BREAK;
1670                                 }
1671                         }
1672                 }
1673         }
1674
1675         if (retval == WM_UI_HANDLER_BREAK) {
1676                 return retval;
1677         }
1678
1679         for (block = ar->uiblocks.last; block; block = block->prev) {
1680                 bool inside = false, inside_header = false, inside_scale = false;
1681                 
1682                 mx = event->x;
1683                 my = event->y;
1684                 ui_window_to_block(ar, block, &mx, &my);
1685
1686                 /* checks for mouse position inside */
1687                 pa = block->panel;
1688
1689                 if (!pa || pa->paneltab != NULL)
1690                         continue;
1691                 if (pa->type && pa->type->flag & PNL_NO_HEADER)  /* XXX - accessed freed panels when scripts reload, need to fix. */
1692                         continue;
1693                 
1694                 /* clicked at panel header? */
1695                 if (pa->flag & PNL_CLOSEDX) {
1696                         if (block->rect.xmin <= mx && block->rect.xmin + PNL_HEADER >= mx)
1697                                 inside_header = true;
1698                 }
1699                 else if (block->rect.xmin > mx || block->rect.xmax < mx) {
1700                         /* outside left/right side */
1701                 }
1702                 else if ((block->rect.ymax <= my) && (block->rect.ymax + PNL_HEADER >= my)) {
1703                         inside_header = true;
1704                 }
1705                 else if (!(pa->flag & PNL_CLOSEDY)) {
1706                         /* open panel */
1707                         if (pa->control & UI_PNL_SCALE) {
1708                                 if (block->rect.xmax - PNL_HEADER <= mx)
1709                                         if (block->rect.ymin + PNL_HEADER >= my)
1710                                                 inside_scale = true;
1711                         }
1712                         if (block->rect.xmin <= mx && block->rect.xmax >= mx)
1713                                 if (block->rect.ymin <= my && block->rect.ymax + PNL_HEADER >= my)
1714                                         inside = true;
1715                 }
1716                 
1717                 /* XXX hardcoded key warning */
1718                 if ((inside || inside_header) && event->val == KM_PRESS) {
1719                         if (event->type == AKEY && !ELEM4(KM_MOD_FIRST, event->ctrl, event->oskey, event->shift, event->alt)) {
1720                                 
1721                                 if (pa->flag & PNL_CLOSEDY) {
1722                                         if ((block->rect.ymax <= my) && (block->rect.ymax + PNL_HEADER >= my))
1723                                                 ui_handle_panel_header(C, block, mx, my, event->type, event->ctrl, event->shift);
1724                                 }
1725                                 else
1726                                         ui_handle_panel_header(C, block, mx, my, event->type, event->ctrl, event->shift);
1727                                 
1728                                 retval = WM_UI_HANDLER_BREAK;
1729                                 continue;
1730                         }
1731                 }
1732                 
1733                 /* on active button, do not handle panels */
1734                 if (ui_button_is_active(ar))
1735                         continue;
1736                 
1737                 if (inside || inside_header) {
1738
1739                         if (event->val == KM_PRESS) {
1740                                 
1741                                 /* open close on header */
1742                                 if (ELEM(event->type, RETKEY, PADENTER)) {
1743                                         if (inside_header) {
1744                                                 ui_handle_panel_header(C, block, mx, my, RETKEY, event->ctrl, event->shift);
1745                                                 retval = WM_UI_HANDLER_BREAK;
1746                                                 break;
1747                                         }
1748                                 }
1749                                 else if (event->type == LEFTMOUSE) {
1750                                         /* all inside clicks should return in break - overlapping/float panels */
1751                                         retval = WM_UI_HANDLER_BREAK;
1752                                         
1753                                         if (inside_header) {
1754                                                 ui_handle_panel_header(C, block, mx, my, 0, event->ctrl, event->shift);
1755                                                 retval = WM_UI_HANDLER_BREAK;
1756                                                 break;
1757                                         }
1758                                         else if (inside_scale && !(pa->flag & PNL_CLOSED)) {
1759                                                 panel_activate_state(C, pa, PANEL_STATE_DRAG_SCALE);
1760                                                 retval = WM_UI_HANDLER_BREAK;
1761                                                 break;
1762                                         }
1763
1764                                 }
1765                                 else if (event->type == RIGHTMOUSE) {
1766                                         if (inside_header) {
1767                                                 ui_panel_menu(C, ar, block->panel);
1768                                                 retval = WM_UI_HANDLER_BREAK;
1769                                                 break;
1770                                         }
1771                                 }
1772                                 else if (event->type == ESCKEY) {
1773                                         /*XXX 2.50*/
1774 #if 0
1775                                         if (block->handler) {
1776                                                 rem_blockhandler(sa, block->handler);
1777                                                 ED_region_tag_redraw(ar);
1778                                                 retval = WM_UI_HANDLER_BREAK;
1779                                         }
1780 #endif
1781                                 }
1782                                 else if (event->type == PADPLUSKEY || event->type == PADMINUS) {
1783 #if 0 /* XXX make float panel exception? */
1784                                         int zoom = 0;
1785                                 
1786                                         /* if panel is closed, only zoom if mouse is over the header */
1787                                         if (pa->flag & (PNL_CLOSEDX | PNL_CLOSEDY)) {
1788                                                 if (inside_header)
1789                                                         zoom = 1;
1790                                         }
1791                                         else
1792                                                 zoom = 1;
1793
1794                                         if (zoom) {
1795                                                 ScrArea *sa = CTX_wm_area(C);
1796                                                 SpaceLink *sl = sa->spacedata.first;
1797
1798                                                 if (sa->spacetype != SPACE_BUTS) {
1799                                                         if (!(pa->control & UI_PNL_SCALE)) {
1800                                                                 if (event->type == PADPLUSKEY) sl->blockscale += 0.1;
1801                                                                 else sl->blockscale -= 0.1;
1802                                                                 CLAMP(sl->blockscale, 0.6, 1.0);
1803
1804                                                                 ED_region_tag_redraw(ar);
1805                                                                 retval = WM_UI_HANDLER_BREAK;
1806                                                         }
1807                                                 }
1808                                         }
1809 #endif
1810                                 }
1811                         }
1812                 }
1813         }
1814
1815         return retval;
1816 }
1817
1818 /**************** window level modal panel interaction **************/
1819
1820 /* note, this is modal handler and should not swallow events for animation */
1821 static int ui_handler_panel(bContext *C, const wmEvent *event, void *userdata)
1822 {
1823         Panel *panel = userdata;
1824         uiHandlePanelData *data = panel->activedata;
1825
1826         /* verify if we can stop */
1827         if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
1828                 ScrArea *sa = CTX_wm_area(C);
1829                 ARegion *ar = CTX_wm_region(C);
1830                 int align = panel_aligned(sa, ar);
1831
1832                 if (align)
1833                         panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1834                 else
1835                         panel_activate_state(C, panel, PANEL_STATE_EXIT);
1836         }
1837         else if (event->type == MOUSEMOVE) {
1838                 if (data->state == PANEL_STATE_DRAG)
1839                         ui_do_drag(C, event, panel);
1840         }
1841         else if (event->type == TIMER && event->customdata == data->animtimer) {
1842                 if (data->state == PANEL_STATE_ANIMATION)
1843                         ui_do_animate(C, panel);
1844                 else if (data->state == PANEL_STATE_DRAG)
1845                         ui_do_drag(C, event, panel);
1846         }
1847
1848         data = panel->activedata;
1849
1850         if (data && data->state == PANEL_STATE_ANIMATION)
1851                 return WM_UI_HANDLER_CONTINUE;
1852         else
1853                 return WM_UI_HANDLER_BREAK;
1854 }
1855
1856 static void ui_handler_remove_panel(bContext *C, void *userdata)
1857 {
1858         Panel *pa = userdata;
1859
1860         panel_activate_state(C, pa, PANEL_STATE_EXIT);
1861 }
1862
1863 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
1864 {
1865         uiHandlePanelData *data = pa->activedata;
1866         wmWindow *win = CTX_wm_window(C);
1867         ARegion *ar = CTX_wm_region(C);
1868         
1869         if (data && data->state == state)
1870                 return;
1871
1872         if (state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1873                 if (data && data->state != PANEL_STATE_ANIMATION) {
1874                         /* XXX:
1875                          *      - the panel tabbing function call below (test_add_new_tabs()) has been commented out
1876                          *        "It is too easy to do by accident when reordering panels,
1877                          *     is very hard to control and use, and has no real benefit." - BillRey
1878                          * Aligorith, 2009Sep
1879                          */
1880                         //test_add_new_tabs(ar);   // also copies locations of tabs in dragged panel
1881                         check_panel_overlap(ar, NULL);  /* clears */
1882                 }
1883
1884                 pa->flag &= ~PNL_SELECT;
1885         }
1886         else
1887                 pa->flag |= PNL_SELECT;
1888
1889         if (data && data->animtimer) {
1890                 WM_event_remove_timer(CTX_wm_manager(C), win, data->animtimer);
1891                 data->animtimer = NULL;
1892         }
1893
1894         if (state == PANEL_STATE_EXIT) {
1895                 MEM_freeN(data);
1896                 pa->activedata = NULL;
1897
1898                 WM_event_remove_ui_handler(&win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa, false);
1899         }
1900         else {
1901                 if (!data) {
1902                         data = MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1903                         pa->activedata = data;
1904
1905                         WM_event_add_ui_handler(C, &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa);
1906                 }
1907
1908                 if (ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1909                         data->animtimer = WM_event_add_timer(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
1910
1911                 data->state = state;
1912                 data->startx = win->eventstate->x;
1913                 data->starty = win->eventstate->y;
1914                 data->startofsx = pa->ofsx;
1915                 data->startofsy = pa->ofsy;
1916                 data->startsizex = pa->sizex;
1917                 data->startsizey = pa->sizey;
1918                 data->starttime = PIL_check_seconds_timer();
1919         }
1920
1921         ED_region_tag_redraw(ar);
1922 }
1923