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