Cleanup: use typed unsigned ints
[blender.git] / source / blender / editors / interface / interface_panel.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2003-2009 full recode.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_panel.c
27  *  \ingroup edinterface
28  */
29
30
31 /* a full doc with API notes can be found in bf-blender/trunk/blender/doc/guides/interface_API.txt */
32
33 #include <math.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "PIL_time.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45
46 #include "BLT_translation.h"
47
48 #include "DNA_userdef_types.h"
49
50 #include "BKE_context.h"
51 #include "BKE_screen.h"
52
53 #include "BLF_api.h"
54
55 #include "WM_api.h"
56 #include "WM_types.h"
57
58 #include "ED_screen.h"
59
60 #include "UI_view2d.h"
61 #include "UI_interface.h"
62 #include "UI_interface_icons.h"
63 #include "UI_resources.h"
64
65 #include "GPU_immediate.h"
66 #include "GPU_state.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 /* the state of the mouse position relative to the panel */
86 typedef enum uiPanelMouseState {
87         PANEL_MOUSE_OUTSIDE,        /* mouse is not in the panel */
88         PANEL_MOUSE_INSIDE_CONTENT, /* mouse is in the actual panel content */
89         PANEL_MOUSE_INSIDE_HEADER,  /* mouse is in the panel header */
90         PANEL_MOUSE_INSIDE_SCALE,   /* mouse is inside panel scale widget */
91 } uiPanelMouseState;
92
93 typedef enum uiHandlePanelState {
94         PANEL_STATE_DRAG,
95         PANEL_STATE_DRAG_SCALE,
96         PANEL_STATE_WAIT_UNTAB,
97         PANEL_STATE_ANIMATION,
98         PANEL_STATE_EXIT
99 } uiHandlePanelState;
100
101 typedef struct uiHandlePanelData {
102         uiHandlePanelState state;
103
104         /* animation */
105         wmTimer *animtimer;
106         double starttime;
107
108         /* dragging */
109         int startx, starty;
110         int startofsx, startofsy;
111         int startsizex, startsizey;
112 } uiHandlePanelData;
113
114 static int get_panel_real_size_y(const Panel *pa);
115 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state);
116
117 static void panel_title_color_get(bool show_background, uchar color[4])
118 {
119         if (show_background) {
120                 UI_GetThemeColor4ubv(TH_TITLE, color);
121         }
122         else {
123                 /* Use menu colors for floating panels. */
124                 bTheme *btheme = UI_GetTheme();
125                 const uiWidgetColors *wcol = &btheme->tui.wcol_menu_back;
126                 copy_v4_v4_uchar(color, (const uchar *)wcol->text);
127         }
128 }
129
130 /*********************** space specific code ************************/
131 /* temporary code to remove all sbuts stuff from panel code         */
132
133 /* SpaceButs.align */
134 typedef enum eSpaceButtons_Align {
135         BUT_HORIZONTAL = 0,
136         BUT_VERTICAL = 1,
137         BUT_AUTO = 2,
138 } eSpaceButtons_Align;
139
140 static int panel_aligned(ScrArea *sa, ARegion *ar)
141 {
142         if (sa->spacetype == SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW)
143                 return BUT_VERTICAL;
144         else if (sa->spacetype == SPACE_USERPREF && ar->regiontype == RGN_TYPE_WINDOW)
145                 return BUT_VERTICAL;
146         else if (sa->spacetype == SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS)
147                 return BUT_VERTICAL;
148         else if (sa->spacetype == SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW)
149                 return BUT_VERTICAL;
150         else if (ELEM(ar->regiontype, RGN_TYPE_UI, RGN_TYPE_TOOLS, RGN_TYPE_TOOL_PROPS, RGN_TYPE_HUD, RGN_TYPE_NAV_BAR))
151                 return BUT_VERTICAL;
152
153         return 0;
154 }
155
156 static bool panel_active_animation_changed(ListBase *lb, Panel **pa_animation, bool *no_animation)
157 {
158         for (Panel *pa = lb->first; pa; pa = pa->next) {
159                 /* Detect panel active flag changes. */
160                 if (!(pa->type && pa->type->parent)) {
161                         if ((pa->runtime_flag & PNL_WAS_ACTIVE) && !(pa->runtime_flag & PNL_ACTIVE)) {
162                                 return true;
163                         }
164                         if (!(pa->runtime_flag & PNL_WAS_ACTIVE) && (pa->runtime_flag & PNL_ACTIVE)) {
165                                 return true;
166                         }
167                 }
168
169                 if ((pa->runtime_flag & PNL_ACTIVE) && !(pa->flag & PNL_CLOSED)) {
170                         if (panel_active_animation_changed(&pa->children, pa_animation, no_animation)) {
171                                 return true;
172                         }
173                 }
174
175                 /* Detect animation. */
176                 if (pa->activedata) {
177                         uiHandlePanelData *data = pa->activedata;
178                         if (data->state == PANEL_STATE_ANIMATION) {
179                                 *pa_animation = pa;
180                         }
181                         else {
182                                 /* Don't animate while handling other interaction. */
183                                 *no_animation = true;
184                         }
185                 }
186                 if ((pa->runtime_flag & PNL_ANIM_ALIGN) && !(*pa_animation)) {
187                         *pa_animation = pa;
188                 }
189         }
190
191         return false;
192 }
193
194 static bool panels_need_realign(ScrArea *sa, ARegion *ar, Panel **pa_animate)
195 {
196         *pa_animate = NULL;
197
198         if (sa->spacetype == SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
199                 SpaceButs *sbuts = sa->spacedata.first;
200
201                 if (sbuts->mainbo != sbuts->mainb) {
202                         return true;
203                 }
204         }
205         else if (sa->spacetype == SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW) {
206                 return true;
207         }
208         else if (sa->spacetype == SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS) {
209                 return true;
210         }
211
212         /* Detect if a panel was added or removed. */
213         Panel *pa_animation = NULL;
214         bool no_animation = false;
215         if (panel_active_animation_changed(&ar->panels, &pa_animation, &no_animation)) {
216                 return true;
217         }
218
219         /* Detect panel marked for animation, if we're not already animating. */
220         if (pa_animation) {
221                 if (!no_animation) {
222                         *pa_animate = pa_animation;
223                 }
224                 return true;
225         }
226
227         return false;
228 }
229
230 /****************************** panels ******************************/
231
232 static void panels_collapse_all(ScrArea *sa, ARegion *ar, const Panel *from_pa)
233 {
234         const bool has_category_tabs = UI_panel_category_is_visible(ar);
235         const char *category = has_category_tabs ? UI_panel_category_active_get(ar, false) : NULL;
236         const int flag = ((panel_aligned(sa, ar) == BUT_HORIZONTAL) ? PNL_CLOSEDX : PNL_CLOSEDY);
237         const PanelType *from_pt = from_pa->type;
238         Panel *pa;
239
240         for (pa = ar->panels.first; pa; pa = pa->next) {
241                 PanelType *pt = pa->type;
242
243                 /* close panels with headers in the same context */
244                 if (pt && from_pt && !(pt->flag & PNL_NO_HEADER)) {
245                         if (!pt->context[0] || !from_pt->context[0] || STREQ(pt->context, from_pt->context)) {
246                                 if ((pa->flag & PNL_PIN) || !category || !pt->category[0] || STREQ(pt->category, category)) {
247                                         pa->flag &= ~PNL_CLOSED;
248                                         pa->flag |= flag;
249                                 }
250                         }
251                 }
252         }
253 }
254
255
256 static void ui_panel_copy_offset(Panel *pa, Panel *papar)
257 {
258         /* with respect to sizes... papar is parent */
259
260         pa->ofsx = papar->ofsx;
261         pa->ofsy = papar->ofsy + papar->sizey - pa->sizey;
262 }
263
264
265 /**
266  * XXX Disabled paneltab handling for now. Old 2.4x feature, *DO NOT* confuse it with new tool tabs in 2.70. ;)
267  * See also T41704.
268  */
269 /* #define UI_USE_PANELTAB */
270
271 Panel *UI_panel_find_by_type(ListBase *lb, PanelType *pt)
272 {
273         Panel *pa;
274         const char *idname = pt->idname;
275
276 #ifdef UI_USE_PANELTAB
277         const char *tabname = pt->idname;
278         for (pa = lb->first; pa; pa = pa->next) {
279                 if (STREQLEN(pa->panelname, idname, sizeof(pa->panelname))) {
280                         if (STREQLEN(pa->tabname, tabname, sizeof(pa->tabname))) {
281                                 return pa;
282                         }
283                 }
284         }
285 #else
286         for (pa = lb->first; pa; pa = pa->next) {
287                 if (STREQLEN(pa->panelname, idname, sizeof(pa->panelname))) {
288                         return pa;
289                 }
290         }
291 #endif
292
293         return NULL;
294 }
295
296 /**
297  * \note \a pa should be return value from #UI_panel_find_by_type and can be NULL.
298  */
299 Panel *UI_panel_begin(ScrArea *sa, ARegion *ar, ListBase *lb, uiBlock *block, PanelType *pt, Panel *pa, bool *r_open)
300 {
301         Panel *palast, *panext;
302         const char *drawname = CTX_IFACE_(pt->translation_context, pt->label);
303         const char *idname = pt->idname;
304 #ifdef UI_USE_PANELTAB
305         const char *tabname = pt->idname;
306         const char *hookname = NULL;
307 #endif
308         const bool newpanel = (pa == NULL);
309         int align = panel_aligned(sa, ar);
310
311         if (!newpanel) {
312                 pa->type = pt;
313         }
314         else {
315                 /* new panel */
316                 pa = MEM_callocN(sizeof(Panel), "new panel");
317                 pa->type = pt;
318                 BLI_strncpy(pa->panelname, idname, sizeof(pa->panelname));
319
320                 if (pt->flag & PNL_DEFAULT_CLOSED) {
321                         if (align == BUT_VERTICAL)
322                                 pa->flag |= PNL_CLOSEDY;
323                         else
324                                 pa->flag |= PNL_CLOSEDX;
325                 }
326
327                 pa->ofsx = 0;
328                 pa->ofsy = 0;
329                 pa->sizex = 0;
330                 pa->sizey = 0;
331                 pa->blocksizex = 0;
332                 pa->blocksizey = 0;
333                 pa->runtime_flag |= PNL_NEW_ADDED;
334
335                 BLI_addtail(lb, pa);
336
337 #ifdef UI_USE_PANELTAB
338                 BLI_strncpy(pa->tabname, tabname, sizeof(pa->tabname));
339
340                 /* make new Panel tabbed? */
341                 if (hookname) {
342                         Panel *patab;
343                         for (patab = lb->first; patab; patab = patab->next) {
344                                 if ((patab->runtime_flag & PNL_ACTIVE) && patab->paneltab == NULL) {
345                                         if (STREQLEN(hookname, patab->panelname, sizeof(patab->panelname))) {
346                                                 if (STREQLEN(tabname, patab->tabname, sizeof(patab->tabname))) {
347                                                         pa->paneltab = patab;
348                                                         ui_panel_copy_offset(pa, patab);
349                                                         break;
350                                                 }
351                                         }
352                                 }
353                         }
354                 }
355 #else
356                 BLI_strncpy(pa->tabname, idname, sizeof(pa->tabname));
357 #endif
358         }
359
360         /* Do not allow closed panels without headers! Else user could get "disappeared" UI! */
361         if ((pt->flag & PNL_NO_HEADER) && (pa->flag & PNL_CLOSED)) {
362                 pa->flag &= ~PNL_CLOSED;
363                 /* Force update of panels' positions! */
364                 pa->sizex = 0;
365                 pa->sizey = 0;
366                 pa->blocksizex = 0;
367                 pa->blocksizey = 0;
368         }
369
370         BLI_strncpy(pa->drawname, drawname, sizeof(pa->drawname));
371
372         /* if a new panel is added, we insert it right after the panel
373          * that was last added. this way new panels are inserted in the
374          * right place between versions */
375         for (palast = lb->first; palast; palast = palast->next) {
376                 if (palast->runtime_flag & PNL_LAST_ADDED) {
377                         BLI_remlink(lb, pa);
378                         BLI_insertlinkafter(lb, palast, pa);
379                         break;
380                 }
381         }
382
383         if (newpanel) {
384                 pa->sortorder = (palast) ? palast->sortorder + 1 : 0;
385
386                 for (panext = lb->first; panext; panext = panext->next)
387                         if (panext != pa && panext->sortorder >= pa->sortorder)
388                                 panext->sortorder++;
389         }
390
391         if (palast)
392                 palast->runtime_flag &= ~PNL_LAST_ADDED;
393
394         /* assign to block */
395         block->panel = pa;
396         pa->runtime_flag |= PNL_ACTIVE | PNL_LAST_ADDED;
397         if (ar->alignment == RGN_ALIGN_FLOAT) {
398                 UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_POPUP);
399         }
400
401         *r_open = false;
402
403         if (pa->paneltab) return pa;
404         if (pa->flag & PNL_CLOSED) return pa;
405
406         *r_open = true;
407
408         return pa;
409 }
410
411 void UI_panel_end(uiBlock *block, int width, int height)
412 {
413         Panel *pa = block->panel;
414
415         /* Set panel size excluding children. */
416         pa->blocksizex = width;
417         pa->blocksizey = height;
418
419         /* Compute total panel size including children. */
420         for (Panel *pachild = pa->children.first; pachild; pachild = pachild->next) {
421                 if (pachild->runtime_flag & PNL_ACTIVE) {
422                         width = max_ii(width, pachild->sizex);
423                         height += get_panel_real_size_y(pachild);
424                 }
425         }
426
427         /* Update total panel size. */
428         if (pa->runtime_flag & PNL_NEW_ADDED) {
429                 pa->runtime_flag &= ~PNL_NEW_ADDED;
430                 pa->sizex = width;
431                 pa->sizey = height;
432         }
433         else {
434                 /* check if we need to do an animation */
435                 if (!ELEM(width, 0, pa->sizex) || !ELEM(height, 0, pa->sizey)) {
436                         pa->runtime_flag |= PNL_ANIM_ALIGN;
437                         if (height != 0)
438                                 pa->ofsy += pa->sizey - height;
439                 }
440
441                 /* update width/height if non-zero */
442                 if (width != 0)
443                         pa->sizex = width;
444                 if (height != 0)
445                         pa->sizey = height;
446         }
447 }
448
449 static void ui_offset_panel_block(uiBlock *block)
450 {
451         uiStyle *style = UI_style_get_dpi();
452
453         /* compute bounds and offset */
454         ui_block_bounds_calc(block);
455
456         int ofsy = block->panel->sizey - style->panelspace;
457
458         for (uiBut *but = block->buttons.first; but; but = but->next) {
459                 but->rect.ymin += ofsy;
460                 but->rect.ymax += ofsy;
461         }
462
463         block->rect.xmax = block->panel->sizex;
464         block->rect.ymax = block->panel->sizey;
465         block->rect.xmin = block->rect.ymin = 0.0;
466 }
467
468 /**************************** drawing *******************************/
469
470 /* triangle 'icon' for panel header */
471 void UI_draw_icon_tri(float x, float y, char dir, const float color[4])
472 {
473         float f3 = 0.05 * U.widget_unit;
474         float f5 = 0.15 * U.widget_unit;
475         float f7 = 0.25 * U.widget_unit;
476
477         if (dir == 'h') {
478                 UI_draw_anti_tria(x - f3, y - f5, x - f3, y + f5, x + f7, y, color);
479         }
480         else if (dir == 't') {
481                 UI_draw_anti_tria(x - f5, y - f7, x + f5, y - f7, x, y + f3, color);
482         }
483         else { /* 'v' = vertical, down */
484                 UI_draw_anti_tria(x - f5, y + f3, x + f5, y + f3, x, y - f7, color);
485         }
486 }
487
488 static void ui_draw_anti_x(uint pos, float x1, float y1, float x2, float y2)
489 {
490
491         /* set antialias line */
492         GPU_line_smooth(true);
493         GPU_blend(true);
494
495         GPU_line_width(2.0);
496
497         immBegin(GPU_PRIM_LINES, 4);
498
499         immVertex2f(pos, x1, y1);
500         immVertex2f(pos, x2, y2);
501
502         immVertex2f(pos, x1, y2);
503         immVertex2f(pos, x2, y1);
504
505         immEnd();
506
507         GPU_line_smooth(false);
508         GPU_blend(false);
509
510 }
511
512 /* x 'icon' for panel header */
513 static void ui_draw_x_icon(uint pos, float x, float y)
514 {
515
516         ui_draw_anti_x(pos, x, y, x + 9.375f, y + 9.375f);
517
518 }
519
520 #define PNL_ICON    UI_UNIT_X  /* could be UI_UNIT_Y too */
521
522 static void ui_draw_panel_scalewidget(uint pos, const rcti *rect)
523 {
524         float xmin, xmax, dx;
525         float ymin, ymax, dy;
526
527         xmin = rect->xmax - PNL_HEADER + 2;
528         xmax = rect->xmax - 3;
529         ymin = rect->ymin + 3;
530         ymax = rect->ymin + PNL_HEADER - 2;
531
532         dx = 0.5f * (xmax - xmin);
533         dy = 0.5f * (ymax - ymin);
534
535         GPU_blend(true);
536         immUniformColor4ub(255, 255, 255, 50);
537
538         immBegin(GPU_PRIM_LINES, 4);
539
540         immVertex2f(pos, xmin, ymin);
541         immVertex2f(pos, xmax, ymax);
542
543         immVertex2f(pos, xmin + dx, ymin);
544         immVertex2f(pos, xmax, ymax - dy);
545
546         immEnd();
547
548         immUniformColor4ub(0, 0, 0, 50);
549
550         immBegin(GPU_PRIM_LINES, 4);
551
552         immVertex2f(pos, xmin, ymin + 1);
553         immVertex2f(pos, xmax, ymax + 1);
554
555         immVertex2f(pos, xmin + dx, ymin + 1);
556         immVertex2f(pos, xmax, ymax - dy + 1);
557
558         immEnd();
559
560         GPU_blend(false);
561 }
562
563 static void immRectf_tris_color_ex(
564         uint pos, float x1, float y1, float x2, float y2,
565         uint col, const float color[3])
566 {
567         immAttr4fv(col, color);
568         immVertex2f(pos, x1, y1);
569         immAttr4fv(col, color);
570         immVertex2f(pos, x2, y1);
571         immAttr4fv(col, color);
572         immVertex2f(pos, x2, y2);
573
574         immAttr4fv(col, color);
575         immVertex2f(pos, x1, y1);
576         immAttr4fv(col, color);
577         immVertex2f(pos, x2, y2);
578         immAttr4fv(col, color);
579         immVertex2f(pos, x1, y2);
580 }
581
582 static void ui_draw_panel_dragwidget(uint pos, uint col, const rctf *rect)
583 {
584         float col_high[4], col_dark[4];
585         const int col_tint = 84;
586
587         const int px = (int)U.pixelsize;
588         const int px_zoom = max_ii(round_fl_to_int(BLI_rctf_size_y(rect) / 22.0f), 1);
589
590         const int box_margin = max_ii(round_fl_to_int((float)(px_zoom * 2.0f)), px);
591         const int box_size = max_ii(round_fl_to_int((BLI_rctf_size_y(rect) / 8.0f) - px), px);
592
593         const int x_min = rect->xmin;
594         const int y_min = rect->ymin;
595         const int y_ofs = max_ii(round_fl_to_int(BLI_rctf_size_y(rect) / 2.5f), px);
596         const int x_ofs = y_ofs;
597         int i_x, i_y;
598
599         UI_GetThemeColorShade4fv(TH_PANEL_HEADER,  col_tint, col_high);
600         UI_GetThemeColorShade4fv(TH_PANEL_BACK, -col_tint, col_dark);
601
602         /* draw multiple boxes */
603         immBegin(GPU_PRIM_TRIS, 4 * 2 * (6 * 2));
604         for (i_x = 0; i_x < 4; i_x++) {
605                 for (i_y = 0; i_y < 2; i_y++) {
606                         const int x_co = (x_min + x_ofs) + (i_x * (box_size + box_margin));
607                         const int y_co = (y_min + y_ofs) + (i_y * (box_size + box_margin));
608
609                         immRectf_tris_color_ex(
610                                 pos, x_co - box_size, y_co - px_zoom, x_co, (y_co + box_size) - px_zoom,
611                                 col, col_dark);
612                         immRectf_tris_color_ex(
613                                 pos, x_co - box_size, y_co, x_co, y_co + box_size,
614                                 col, col_high);
615                 }
616         }
617         immEnd();
618 }
619
620 /* For button layout next to label. */
621 void UI_panel_label_offset(uiBlock *block, int *x, int *y)
622 {
623         Panel *panel = block->panel;
624         uiStyle *style = UI_style_get_dpi();
625         const bool is_subpanel = (panel->type && panel->type->parent);
626
627         *x = UI_UNIT_X * 1.1f;
628         *y = (UI_UNIT_Y * 1.1f) + style->panelspace;
629
630         if (is_subpanel) {
631                 *x += 5.0f / block->aspect;
632         }
633 }
634
635 static void ui_draw_aligned_panel_header(
636         uiStyle *style, uiBlock *block, const rcti *rect, char dir,
637         const bool show_background)
638 {
639         Panel *panel = block->panel;
640         rcti hrect;
641         int pnl_icons;
642         const char *activename = panel->drawname[0] ? panel->drawname : panel->panelname;
643         const bool is_subpanel = (panel->type && panel->type->parent);
644         uiFontStyle *fontstyle = (is_subpanel) ? &style->widgetlabel : &style->paneltitle;
645         uchar col_title[4];
646
647         /* + 0.001f to avoid flirting with float inaccuracy */
648         if (panel->control & UI_PNL_CLOSE)
649                 pnl_icons = (panel->labelofs + 2 * PNL_ICON + 5) / block->aspect + 0.001f;
650         else
651                 pnl_icons = (panel->labelofs + PNL_ICON + 5) / block->aspect + 0.001f;
652
653         /* draw text label */
654         panel_title_color_get(show_background, col_title);
655         col_title[3] = 255;
656
657         hrect = *rect;
658         if (dir == 'h') {
659                 hrect.xmin = rect->xmin + pnl_icons;
660                 hrect.ymin += 2.0f / block->aspect;
661                 UI_fontstyle_draw(
662                         fontstyle, &hrect, activename, col_title,
663                         &(struct uiFontStyleDraw_Params) { .align = UI_STYLE_TEXT_LEFT, });
664         }
665         else {
666                 /* ignore 'pnl_icons', otherwise the text gets offset horizontally
667                  * + 0.001f to avoid flirting with float inaccuracy
668                  */
669                 hrect.xmin = rect->xmin + (PNL_ICON + 5) / block->aspect + 0.001f;
670                 UI_fontstyle_draw_rotated(fontstyle, &hrect, activename, col_title);
671         }
672 }
673
674 /* panel integrated in buttonswindow, tool/property lists etc */
675 void ui_draw_aligned_panel(
676         uiStyle *style, uiBlock *block, const rcti *rect,
677         const bool show_pin, const bool show_background)
678 {
679         Panel *panel = block->panel;
680         rcti headrect;
681         rctf itemrect;
682         float color[4];
683         const bool is_closed_x = (panel->flag & PNL_CLOSEDX) ? true : false;
684         const bool is_closed_y = (panel->flag & PNL_CLOSEDY) ? true : false;
685         const bool is_subpanel = (panel->type && panel->type->parent);
686         const bool show_drag = (
687                 !is_subpanel &&
688                 /* FIXME(campbell): currently no background means floating panel which can't be dragged.
689                  * This may be changed in future. */
690                 show_background);
691
692         if (panel->paneltab) return;
693         if (panel->type && (panel->type->flag & PNL_NO_HEADER)) return;
694
695         /* calculate header rect */
696         /* + 0.001f to prevent flicker due to float inaccuracy */
697         headrect = *rect;
698         headrect.ymin = headrect.ymax;
699         headrect.ymax = headrect.ymin + floor(PNL_HEADER / block->aspect + 0.001f);
700
701         rcti titlerect = headrect;
702         if (is_subpanel) {
703                 titlerect.xmin += 5.0f / block->aspect;
704         }
705
706         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
707         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
708
709         if (show_background && !is_subpanel) {
710                 float minx = rect->xmin;
711                 float maxx = is_closed_x ? (minx + PNL_HEADER / block->aspect) : rect->xmax;
712                 float y = headrect.ymax;
713
714                 GPU_blend(true);
715
716                 /* draw with background color */
717                 immUniformThemeColor(TH_PANEL_HEADER);
718                 immRectf(pos, minx, headrect.ymin, maxx, y);
719
720                 immBegin(GPU_PRIM_LINES, 4);
721
722                 immVertex2f(pos, minx, y);
723                 immVertex2f(pos, maxx, y);
724
725                 immVertex2f(pos, minx, y);
726                 immVertex2f(pos, maxx, y);
727
728                 immEnd();
729
730                 GPU_blend(false);
731         }
732
733         immUnbindProgram();
734
735         /* draw optional pin icon */
736
737 #ifdef USE_PIN_HIDDEN
738         if (show_pin && (block->panel->flag & PNL_PIN))
739 #else
740         if (show_pin)
741 #endif
742         {
743                 uchar col_title[4];
744                 panel_title_color_get(show_background, col_title);
745
746                 GPU_blend(true);
747                 UI_icon_draw_aspect(
748                         headrect.xmax - ((PNL_ICON * 2.2f) / block->aspect), headrect.ymin + (5.0f / block->aspect),
749                         (panel->flag & PNL_PIN) ? ICON_PINNED : ICON_UNPINNED,
750                         (block->aspect / UI_DPI_FAC), 1.0f, (const char *)col_title);
751                 GPU_blend(false);
752         }
753
754
755         /* horizontal title */
756         if (is_closed_x == false) {
757                 ui_draw_aligned_panel_header(style, block, &titlerect, 'h', show_background);
758
759                 if (show_drag) {
760                         uint col;
761                         GPUVertFormat *format = immVertexFormat();
762                         pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
763                         col = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
764
765                         /* itemrect smaller */
766                         itemrect.xmax = headrect.xmax - 5.0f / block->aspect;
767                         itemrect.xmin = itemrect.xmax - BLI_rcti_size_y(&headrect);
768                         itemrect.ymin = headrect.ymin;
769                         itemrect.ymax = headrect.ymax;
770
771                         BLI_rctf_scale(&itemrect, 0.7f);
772                         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
773                         ui_draw_panel_dragwidget(pos, col, &itemrect);
774                         immUnbindProgram();
775
776                         /* Restore format for the following draws. */
777                         pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
778                 }
779         }
780
781         /* if the panel is minimized vertically:
782          * (------)
783          */
784         if (is_closed_y) {
785                 /* skip */
786         }
787         else if (is_closed_x) {
788                 /* draw vertical title */
789                 ui_draw_aligned_panel_header(style, block, &headrect, 'v', show_background);
790                 pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
791         }
792         /* an open panel */
793         else {
794                 /* in some occasions, draw a border */
795                 if (panel->flag & PNL_SELECT) {
796                         if (panel->control & UI_PNL_SOLID) UI_draw_roundbox_corner_set(UI_CNR_ALL);
797                         else UI_draw_roundbox_corner_set(UI_CNR_NONE);
798
799                         UI_GetThemeColorShade4fv(TH_BACK, -120, color);
800                         UI_draw_roundbox_aa(false, 0.5f + rect->xmin, 0.5f + rect->ymin, 0.5f + rect->xmax, 0.5f + headrect.ymax + 1, 8, color);
801                 }
802
803                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
804
805                 GPU_blend(true);
806
807                 if (show_background) {
808                         /* panel backdrop */
809                         int panel_col = is_subpanel ? TH_PANEL_SUB_BACK : TH_PANEL_BACK;
810
811                         immUniformThemeColor(panel_col);
812                         immRectf(pos, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
813                 }
814
815                 if (panel->control & UI_PNL_SCALE)
816                         ui_draw_panel_scalewidget(pos, rect);
817
818                 immUnbindProgram();
819         }
820
821
822         uchar col_title[4];
823         panel_title_color_get(show_background, col_title);
824
825         /* draw optional close icon */
826
827         if (panel->control & UI_PNL_CLOSE) {
828                 const int ofsx = 6;
829                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
830                 immUniformColor3ubv(col_title);
831                 ui_draw_x_icon(pos, rect->xmin + 2 + ofsx, rect->ymax + 2);
832                 immUnbindProgram();
833         }
834
835         /* draw collapse icon */
836
837         /* itemrect smaller */
838         itemrect.xmin = titlerect.xmin + 3.0f / block->aspect;
839         itemrect.xmax = itemrect.xmin + BLI_rcti_size_y(&titlerect);
840         itemrect.ymin = titlerect.ymin;
841         itemrect.ymax = titlerect.ymax;
842
843         BLI_rctf_scale(&itemrect, 0.25f);
844
845         {
846                 float tria_color[4];
847                 rgb_uchar_to_float(tria_color, col_title);
848                 tria_color[3] = 1.0f;
849
850                 if (is_closed_y)
851                         ui_draw_anti_tria_rect(&itemrect, 'h', tria_color);
852                 else if (is_closed_x)
853                         ui_draw_anti_tria_rect(&itemrect, 'h', tria_color);
854                 else
855                         ui_draw_anti_tria_rect(&itemrect, 'v', tria_color);
856         }
857 }
858
859 /************************** panel alignment *************************/
860
861 static int get_panel_header(const Panel *pa)
862 {
863         if (pa->type && (pa->type->flag & PNL_NO_HEADER))
864                 return 0;
865
866         return PNL_HEADER;
867 }
868
869 static int get_panel_size_y(const Panel *pa)
870 {
871         if (pa->type && (pa->type->flag & PNL_NO_HEADER))
872                 return pa->sizey;
873
874         return PNL_HEADER + pa->sizey;
875 }
876
877 static int get_panel_real_size_y(const Panel *pa)
878 {
879         int sizey = (pa->flag & PNL_CLOSED) ? 0 : pa->sizey;
880
881         if (pa->type && (pa->type->flag & PNL_NO_HEADER))
882                 return sizey;
883
884         return PNL_HEADER + sizey;
885 }
886
887 int UI_panel_size_y(const Panel *pa)
888 {
889         return get_panel_real_size_y(pa);
890 }
891
892 /* this function is needed because uiBlock and Panel itself don't
893  * change sizey or location when closed */
894 static int get_panel_real_ofsy(Panel *pa)
895 {
896         if (pa->flag & PNL_CLOSEDY) return pa->ofsy + pa->sizey;
897         else if (pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDY)) return pa->ofsy + pa->sizey;
898         else if (pa->paneltab) return pa->paneltab->ofsy;
899         else return pa->ofsy;
900 }
901
902 static int get_panel_real_ofsx(Panel *pa)
903 {
904         if (pa->flag & PNL_CLOSEDX) return pa->ofsx + get_panel_header(pa);
905         else if (pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDX)) return pa->ofsx + get_panel_header(pa);
906         else return pa->ofsx + pa->sizex;
907 }
908
909 typedef struct PanelSort {
910         Panel *pa, *orig;
911 } PanelSort;
912
913 /**
914  * \note about sorting;
915  * the sortorder has a lower value for new panels being added.
916  * however, that only works to insert a single panel, when more new panels get
917  * added the coordinates of existing panels and the previously stored to-be-inserted
918  * panels do not match for sorting
919  */
920
921 static int find_leftmost_panel(const void *a1, const void *a2)
922 {
923         const PanelSort *ps1 = a1, *ps2 = a2;
924
925         if (ps1->pa->ofsx > ps2->pa->ofsx) return 1;
926         else if (ps1->pa->ofsx < ps2->pa->ofsx) return -1;
927         else if (ps1->pa->sortorder > ps2->pa->sortorder) return 1;
928         else if (ps1->pa->sortorder < ps2->pa->sortorder) return -1;
929
930         return 0;
931 }
932
933
934 static int find_highest_panel(const void *a1, const void *a2)
935 {
936         const PanelSort *ps1 = a1, *ps2 = a2;
937
938         /* stick uppermost header-less panels to the top of the region -
939          * prevent them from being sorted (multiple header-less panels have to be sorted though) */
940         if (ps1->pa->type->flag & PNL_NO_HEADER && ps2->pa->type->flag & PNL_NO_HEADER) {
941                 /* skip and check for ofs and sortorder below */
942         }
943         else if (ps1->pa->type->flag & PNL_NO_HEADER) return -1;
944         else if (ps2->pa->type->flag & PNL_NO_HEADER) return 1;
945
946         if (ps1->pa->ofsy + ps1->pa->sizey < ps2->pa->ofsy + ps2->pa->sizey) return 1;
947         else if (ps1->pa->ofsy + ps1->pa->sizey > ps2->pa->ofsy + ps2->pa->sizey) return -1;
948         else if (ps1->pa->sortorder > ps2->pa->sortorder) return 1;
949         else if (ps1->pa->sortorder < ps2->pa->sortorder) return -1;
950
951         return 0;
952 }
953
954 static int compare_panel(const void *a1, const void *a2)
955 {
956         const PanelSort *ps1 = a1, *ps2 = a2;
957
958         if (ps1->pa->sortorder > ps2->pa->sortorder) return 1;
959         else if (ps1->pa->sortorder < ps2->pa->sortorder) return -1;
960
961         return 0;
962 }
963
964 static void align_sub_panels(Panel *pa)
965 {
966         /* Position sub panels. */
967         int ofsy = get_panel_real_ofsy(pa) + pa->sizey - pa->blocksizey;
968
969         for (Panel *pachild = pa->children.first; pachild; pachild = pachild->next) {
970                 if (pachild->runtime_flag & PNL_ACTIVE) {
971                         pachild->ofsx = pa->ofsx;
972                         pachild->ofsy = ofsy - get_panel_size_y(pachild);
973                         ofsy -= get_panel_real_size_y(pachild);
974
975                         if (pachild->children.first) {
976                                 align_sub_panels(pachild);
977                         }
978                 }
979         }
980 }
981
982 /* this doesn't draw */
983 /* returns 1 when it did something */
984 static bool uiAlignPanelStep(ScrArea *sa, ARegion *ar, const float fac, const bool drag)
985 {
986         Panel *pa;
987         PanelSort *ps, *panelsort, *psnext;
988         int a, tot = 0;
989         bool done;
990         int align = panel_aligned(sa, ar);
991         bool has_category_tabs = UI_panel_category_is_visible(ar);
992
993         /* count active, not tabbed panels */
994         for (pa = ar->panels.first; pa; pa = pa->next)
995                 if ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab == NULL)
996                         tot++;
997
998         if (tot == 0) return 0;
999
1000         /* extra; change close direction? */
1001         for (pa = ar->panels.first; pa; pa = pa->next) {
1002                 if ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab == NULL) {
1003                         if ((pa->flag & PNL_CLOSEDX) && (align == BUT_VERTICAL))
1004                                 pa->flag ^= PNL_CLOSED;
1005                         else if ((pa->flag & PNL_CLOSEDY) && (align == BUT_HORIZONTAL))
1006                                 pa->flag ^= PNL_CLOSED;
1007                 }
1008         }
1009
1010         /* sort panels */
1011         panelsort = MEM_callocN(tot * sizeof(PanelSort), "panelsort");
1012
1013         ps = panelsort;
1014         for (pa = ar->panels.first; pa; pa = pa->next) {
1015                 if ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab == NULL) {
1016                         ps->pa = MEM_dupallocN(pa);
1017                         ps->orig = pa;
1018                         ps++;
1019                 }
1020         }
1021
1022         if (drag) {
1023                 /* while we are dragging, we sort on location and update sortorder */
1024                 if (align == BUT_VERTICAL)
1025                         qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
1026                 else
1027                         qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
1028
1029                 for (ps = panelsort, a = 0; a < tot; a++, ps++)
1030                         ps->orig->sortorder = a;
1031         }
1032         else
1033                 /* otherwise use sortorder */
1034                 qsort(panelsort, tot, sizeof(PanelSort), compare_panel);
1035
1036         /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
1037         ps = panelsort;
1038         ps->pa->ofsx = 0;
1039         ps->pa->ofsy = -get_panel_size_y(ps->pa);
1040
1041         if (has_category_tabs) {
1042                 if (align == BUT_VERTICAL && (ar->alignment != RGN_ALIGN_RIGHT)) {
1043                         ps->pa->ofsx += UI_PANEL_CATEGORY_MARGIN_WIDTH;
1044                 }
1045         }
1046
1047         for (a = 0; a < tot - 1; a++, ps++) {
1048                 psnext = ps + 1;
1049
1050                 if (align == BUT_VERTICAL) {
1051                         psnext->pa->ofsx = ps->pa->ofsx;
1052                         psnext->pa->ofsy = get_panel_real_ofsy(ps->pa) - get_panel_size_y(psnext->pa);
1053                 }
1054                 else {
1055                         psnext->pa->ofsx = get_panel_real_ofsx(ps->pa);
1056                         psnext->pa->ofsy = ps->pa->ofsy + get_panel_size_y(ps->pa) - get_panel_size_y(psnext->pa);
1057                 }
1058         }
1059
1060         /* we interpolate */
1061         done = false;
1062         ps = panelsort;
1063         for (a = 0; a < tot; a++, ps++) {
1064                 if ((ps->pa->flag & PNL_SELECT) == 0) {
1065                         if ((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) {
1066                                 ps->orig->ofsx = round_fl_to_int(fac * (float)ps->pa->ofsx + (1.0f - fac) * (float)ps->orig->ofsx);
1067                                 ps->orig->ofsy = round_fl_to_int(fac * (float)ps->pa->ofsy + (1.0f - fac) * (float)ps->orig->ofsy);
1068                                 done = true;
1069                         }
1070                 }
1071         }
1072
1073         /* set locations for tabbed and sub panels */
1074         for (pa = ar->panels.first; pa; pa = pa->next) {
1075                 if (pa->runtime_flag & PNL_ACTIVE) {
1076                         if (pa->paneltab) {
1077                                 ui_panel_copy_offset(pa, pa->paneltab);
1078                         }
1079                         if (pa->children.first) {
1080                                 align_sub_panels(pa);
1081                         }
1082                 }
1083         }
1084
1085         /* free panelsort array */
1086         for (ps = panelsort, a = 0; a < tot; a++, ps++) {
1087                 MEM_freeN(ps->pa);
1088         }
1089         MEM_freeN(panelsort);
1090
1091         return done;
1092 }
1093
1094 static void ui_panels_size(ScrArea *sa, ARegion *ar, int *x, int *y)
1095 {
1096         Panel *pa;
1097         int align = panel_aligned(sa, ar);
1098         int sizex = 0;
1099         int sizey = 0;
1100
1101         /* compute size taken up by panels, for setting in view2d */
1102         for (pa = ar->panels.first; pa; pa = pa->next) {
1103                 if (pa->runtime_flag & PNL_ACTIVE) {
1104                         int pa_sizex, pa_sizey;
1105
1106                         if (align == BUT_VERTICAL) {
1107                                 pa_sizex = pa->ofsx + pa->sizex;
1108                                 pa_sizey = get_panel_real_ofsy(pa);
1109                         }
1110                         else {
1111                                 pa_sizex = get_panel_real_ofsx(pa) + pa->sizex;
1112                                 pa_sizey = pa->ofsy + get_panel_size_y(pa);
1113                         }
1114
1115                         sizex = max_ii(sizex, pa_sizex);
1116                         sizey = min_ii(sizey, pa_sizey);
1117                 }
1118         }
1119
1120         if (sizex == 0)
1121                 sizex = UI_PANEL_WIDTH;
1122         if (sizey == 0)
1123                 sizey = -UI_PANEL_WIDTH;
1124
1125         *x = sizex;
1126         *y = sizey;
1127 }
1128
1129 static void ui_do_animate(const bContext *C, Panel *panel)
1130 {
1131         uiHandlePanelData *data = panel->activedata;
1132         ScrArea *sa = CTX_wm_area(C);
1133         ARegion *ar = CTX_wm_region(C);
1134         float fac;
1135
1136         fac = (PIL_check_seconds_timer() - data->starttime) / ANIMATION_TIME;
1137         fac = min_ff(sqrtf(fac), 1.0f);
1138
1139         /* for max 1 second, interpolate positions */
1140         if (uiAlignPanelStep(sa, ar, fac, false)) {
1141                 ED_region_tag_redraw(ar);
1142         }
1143         else {
1144                 fac = 1.0f;
1145         }
1146
1147         if (fac >= 1.0f) {
1148                 panel_activate_state(C, panel, PANEL_STATE_EXIT);
1149                 return;
1150         }
1151 }
1152
1153 static void panel_list_clear_active(ListBase *lb)
1154 {
1155         /* set all panels as inactive, so that at the end we know
1156          * which ones were used */
1157         for (Panel *pa = lb->first; pa; pa = pa->next) {
1158                 if (pa->runtime_flag & PNL_ACTIVE) {
1159                         pa->runtime_flag = PNL_WAS_ACTIVE;
1160                 }
1161                 else {
1162                         pa->runtime_flag = 0;
1163                 }
1164
1165                 panel_list_clear_active(&pa->children);
1166         }
1167 }
1168
1169 void UI_panels_begin(const bContext *UNUSED(C), ARegion *ar)
1170 {
1171         panel_list_clear_active(&ar->panels);
1172 }
1173
1174 /* only draws blocks with panels */
1175 void UI_panels_end(const bContext *C, ARegion *ar, int *x, int *y)
1176 {
1177         ScrArea *sa = CTX_wm_area(C);
1178         uiBlock *block;
1179         Panel *panot, *panew, *patest, *pa, *firstpa;
1180
1181         /* offset contents */
1182         for (block = ar->uiblocks.first; block; block = block->next)
1183                 if (block->active && block->panel)
1184                         ui_offset_panel_block(block);
1185
1186         /* consistency; are panels not made, whilst they have tabs */
1187         for (panot = ar->panels.first; panot; panot = panot->next) {
1188                 if ((panot->runtime_flag & PNL_ACTIVE) == 0) {  /* not made */
1189
1190                         for (panew = ar->panels.first; panew; panew = panew->next) {
1191                                 if ((panew->runtime_flag & PNL_ACTIVE)) {
1192                                         if (panew->paneltab == panot) {  /* panew is tab in notmade pa */
1193                                                 break;
1194                                         }
1195                                 }
1196                         }
1197                         /* now panew can become the new parent, check all other tabs */
1198                         if (panew) {
1199                                 for (patest = ar->panels.first; patest; patest = patest->next) {
1200                                         if (patest->paneltab == panot) {
1201                                                 patest->paneltab = panew;
1202                                         }
1203                                 }
1204                                 panot->paneltab = panew;
1205                                 panew->paneltab = NULL;
1206                                 ED_region_tag_redraw(ar); /* the buttons panew were not made */
1207                         }
1208                 }
1209         }
1210
1211         /* re-align, possibly with animation */
1212         if (panels_need_realign(sa, ar, &pa)) {
1213                 if (pa)
1214                         panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
1215                 else
1216                         uiAlignPanelStep(sa, ar, 1.0, false);
1217         }
1218
1219         /* tag first panel */
1220         firstpa = NULL;
1221         for (block = ar->uiblocks.first; block; block = block->next)
1222                 if (block->active && block->panel)
1223                         if (!firstpa || block->panel->sortorder < firstpa->sortorder)
1224                                 firstpa = block->panel;
1225
1226         if (firstpa)
1227                 firstpa->runtime_flag |= PNL_FIRST;
1228
1229         /* compute size taken up by panel */
1230         ui_panels_size(sa, ar, x, y);
1231 }
1232
1233 void UI_panels_draw(const bContext *C, ARegion *ar)
1234 {
1235         uiBlock *block;
1236
1237         if (ar->alignment != RGN_ALIGN_FLOAT) {
1238                 UI_ThemeClearColor(TH_BACK);
1239         }
1240
1241         /* Draw panels, selected on top. Also in reverse order, because
1242          * UI blocks are added in reverse order and we need child panels
1243          * to draw on top. */
1244         for (block = ar->uiblocks.last; block; block = block->prev) {
1245                 if (block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
1246                         UI_block_draw(C, block);
1247                 }
1248         }
1249
1250         for (block = ar->uiblocks.last; block; block = block->prev) {
1251                 if (block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
1252                         UI_block_draw(C, block);
1253                 }
1254         }
1255 }
1256
1257 void UI_panels_scale(ARegion *ar, float new_width)
1258 {
1259         uiBlock *block;
1260         uiBut *but;
1261
1262         for (block = ar->uiblocks.first; block; block = block->next) {
1263                 if (block->panel) {
1264                         float fac = new_width / (float)block->panel->sizex;
1265                         block->panel->sizex = new_width;
1266
1267                         for (but = block->buttons.first; but; but = but->next) {
1268                                 but->rect.xmin *= fac;
1269                                 but->rect.xmax *= fac;
1270                         }
1271                 }
1272         }
1273 }
1274
1275 /* ------------ panel merging ---------------- */
1276
1277 static void check_panel_overlap(ARegion *ar, Panel *panel)
1278 {
1279         Panel *pa;
1280
1281         /* also called with (panel == NULL) for clear */
1282
1283         for (pa = ar->panels.first; pa; pa = pa->next) {
1284                 pa->flag &= ~PNL_OVERLAP;
1285                 if (panel && (pa != panel)) {
1286                         if (pa->paneltab == NULL && (pa->runtime_flag & PNL_ACTIVE)) {
1287                                 float safex = 0.2, safey = 0.2;
1288
1289                                 if (pa->flag & PNL_CLOSEDX) safex = 0.05;
1290                                 else if (pa->flag & PNL_CLOSEDY) safey = 0.05;
1291                                 else if (panel->flag & PNL_CLOSEDX) safex = 0.05;
1292                                 else if (panel->flag & PNL_CLOSEDY) safey = 0.05;
1293
1294                                 if (pa->ofsx > panel->ofsx - safex * panel->sizex)
1295                                         if (pa->ofsx + pa->sizex < panel->ofsx + (1.0f + safex) * panel->sizex)
1296                                                 if (pa->ofsy > panel->ofsy - safey * panel->sizey)
1297                                                         if (pa->ofsy + pa->sizey < panel->ofsy + (1.0f + safey) * panel->sizey)
1298                                                                 pa->flag |= PNL_OVERLAP;
1299                         }
1300                 }
1301         }
1302 }
1303
1304 /************************ panel dragging ****************************/
1305
1306 static void ui_do_drag(const bContext *C, const wmEvent *event, Panel *panel)
1307 {
1308         uiHandlePanelData *data = panel->activedata;
1309         ScrArea *sa = CTX_wm_area(C);
1310         ARegion *ar = CTX_wm_region(C);
1311         short align = panel_aligned(sa, ar), dx = 0, dy = 0;
1312
1313         /* first clip for window, no dragging outside */
1314         if (!BLI_rcti_isect_pt_v(&ar->winrct, &event->x))
1315                 return;
1316
1317         dx = (event->x - data->startx) & ~(PNL_GRID - 1);
1318         dy = (event->y - data->starty) & ~(PNL_GRID - 1);
1319
1320         dx *= (float)BLI_rctf_size_x(&ar->v2d.cur) / (float)BLI_rcti_size_x(&ar->winrct);
1321         dy *= (float)BLI_rctf_size_y(&ar->v2d.cur) / (float)BLI_rcti_size_y(&ar->winrct);
1322
1323         if (data->state == PANEL_STATE_DRAG_SCALE) {
1324                 panel->sizex = MAX2(data->startsizex + dx, UI_PANEL_MINX);
1325
1326                 if (data->startsizey - dy < UI_PANEL_MINY)
1327                         dy = -UI_PANEL_MINY + data->startsizey;
1328
1329                 panel->sizey = data->startsizey - dy;
1330                 panel->ofsy = data->startofsy + dy;
1331         }
1332         else {
1333                 /* reset the panel snapping, to allow dragging away from snapped edges */
1334                 panel->snap = PNL_SNAP_NONE;
1335
1336                 panel->ofsx = data->startofsx + dx;
1337                 panel->ofsy = data->startofsy + dy;
1338                 check_panel_overlap(ar, panel);
1339
1340                 if (align) uiAlignPanelStep(sa, ar, 0.2, true);
1341         }
1342
1343         ED_region_tag_redraw(ar);
1344 }
1345
1346 /******************* region level panel interaction *****************/
1347
1348 static uiPanelMouseState ui_panel_mouse_state_get(const uiBlock *block, const Panel *pa, const int mx, const int my)
1349 {
1350         /* open panel */
1351         if (pa->flag & PNL_CLOSEDX) {
1352                 if ((block->rect.xmin <= mx) && (block->rect.xmin + PNL_HEADER >= mx)) {
1353                         return PANEL_MOUSE_INSIDE_HEADER;
1354                 }
1355         }
1356         /* outside left/right side */
1357         else if ((block->rect.xmin > mx) || (block->rect.xmax < mx)) {
1358                 /* pass */
1359         }
1360         else if ((block->rect.ymax <= my) && (block->rect.ymax + PNL_HEADER >= my)) {
1361                 return PANEL_MOUSE_INSIDE_HEADER;
1362         }
1363         /* open panel */
1364         else if (!(pa->flag & PNL_CLOSEDY)) {
1365                 if (pa->control & UI_PNL_SCALE) {
1366                         if (block->rect.xmax - PNL_HEADER <= mx) {
1367                                 if (block->rect.ymin + PNL_HEADER >= my) {
1368                                         return PANEL_MOUSE_INSIDE_SCALE;
1369                                 }
1370                         }
1371                 }
1372                 if ((block->rect.xmin <= mx) && (block->rect.xmax >= mx)) {
1373                         if ((block->rect.ymin <= my) && (block->rect.ymax + PNL_HEADER >= my)) {
1374                                 return PANEL_MOUSE_INSIDE_CONTENT;
1375                         }
1376                 }
1377         }
1378         return PANEL_MOUSE_OUTSIDE;
1379 }
1380
1381 typedef struct uiPanelDragCollapseHandle {
1382         bool was_first_open;
1383         int xy_init[2];
1384 } uiPanelDragCollapseHandle;
1385
1386 static void ui_panel_drag_collapse_handler_remove(bContext *UNUSED(C), void *userdata)
1387 {
1388         uiPanelDragCollapseHandle *dragcol_data = userdata;
1389         MEM_freeN(dragcol_data);
1390 }
1391
1392 static void ui_panel_drag_collapse(bContext *C, uiPanelDragCollapseHandle *dragcol_data, const int xy_dst[2])
1393 {
1394         ScrArea *sa = CTX_wm_area(C);
1395         ARegion *ar = CTX_wm_region(C);
1396         uiBlock *block;
1397         Panel *pa;
1398
1399         for (block = ar->uiblocks.first; block; block = block->next) {
1400                 float xy_a_block[2] = {UNPACK2(dragcol_data->xy_init)};
1401                 float xy_b_block[2] = {UNPACK2(xy_dst)};
1402                 rctf rect = block->rect;
1403                 int oldflag;
1404                 const bool is_horizontal = (panel_aligned(sa, ar) == BUT_HORIZONTAL);
1405
1406                 if ((pa = block->panel) == 0 || (pa->type && (pa->type->flag & PNL_NO_HEADER))) {
1407                         continue;
1408                 }
1409                 oldflag = pa->flag;
1410
1411                 /* lock one axis */
1412                 if (is_horizontal) {
1413                         xy_b_block[1] = dragcol_data->xy_init[1];
1414                 }
1415                 else {
1416                         xy_b_block[0] = dragcol_data->xy_init[0];
1417                 }
1418
1419                 /* use cursor coords in block space */
1420                 ui_window_to_block_fl(ar, block, &xy_a_block[0], &xy_a_block[1]);
1421                 ui_window_to_block_fl(ar, block, &xy_b_block[0], &xy_b_block[1]);
1422
1423                 /* set up rect to match header size */
1424                 rect.ymin = rect.ymax;
1425                 rect.ymax = rect.ymin + PNL_HEADER;
1426                 if (pa->flag & PNL_CLOSEDX) {
1427                         rect.xmax = rect.xmin + PNL_HEADER;
1428                 }
1429
1430                 /* touch all panels between last mouse coord and the current one */
1431                 if (BLI_rctf_isect_segment(&rect, xy_a_block, xy_b_block)) {
1432                         /* force panel to close */
1433                         if (dragcol_data->was_first_open == true) {
1434                                 pa->flag |= (is_horizontal ? PNL_CLOSEDX : PNL_CLOSEDY);
1435                         }
1436                         /* force panel to open */
1437                         else {
1438                                 pa->flag &= ~PNL_CLOSED;
1439                         }
1440
1441                         /* if pa->flag has changed this means a panel was opened/closed here */
1442                         if (pa->flag != oldflag) {
1443                                 panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
1444                         }
1445                 }
1446         }
1447 }
1448
1449 /**
1450  * Panel drag-collapse (modal handler)
1451  * Clicking and dragging over panels toggles their collapse state based on the panel that was first
1452  * dragged over. If it was open all affected panels incl the initial one are closed and vice versa.
1453  */
1454 static int ui_panel_drag_collapse_handler(bContext *C, const wmEvent *event, void *userdata)
1455 {
1456         wmWindow *win = CTX_wm_window(C);
1457         uiPanelDragCollapseHandle *dragcol_data = userdata;
1458         short retval = WM_UI_HANDLER_CONTINUE;
1459
1460         switch (event->type) {
1461                 case MOUSEMOVE:
1462                         ui_panel_drag_collapse(C, dragcol_data, &event->x);
1463
1464                         retval = WM_UI_HANDLER_BREAK;
1465                         break;
1466                 case LEFTMOUSE:
1467                         if (event->val == KM_RELEASE) {
1468                                 /* done! */
1469                                 WM_event_remove_ui_handler(
1470                                         &win->modalhandlers,
1471                                         ui_panel_drag_collapse_handler,
1472                                         ui_panel_drag_collapse_handler_remove,
1473                                         dragcol_data, true);
1474                                 ui_panel_drag_collapse_handler_remove(C, dragcol_data);
1475                         }
1476                         /* don't let any left-mouse event fall through! */
1477                         retval = WM_UI_HANDLER_BREAK;
1478                         break;
1479         }
1480
1481         return retval;
1482 }
1483
1484 static void ui_panel_drag_collapse_handler_add(const bContext *C, const bool was_open)
1485 {
1486         wmWindow *win = CTX_wm_window(C);
1487         const wmEvent *event = win->eventstate;
1488         uiPanelDragCollapseHandle *dragcol_data = MEM_mallocN(sizeof(*dragcol_data), __func__);
1489
1490         dragcol_data->was_first_open = was_open;
1491         copy_v2_v2_int(dragcol_data->xy_init, &event->x);
1492
1493         WM_event_add_ui_handler(
1494                 C, &win->modalhandlers,
1495                 ui_panel_drag_collapse_handler,
1496                 ui_panel_drag_collapse_handler_remove,
1497                 dragcol_data, 0);
1498 }
1499
1500 /* this function is supposed to call general window drawing too */
1501 /* also it supposes a block has panel, and isn't a menu */
1502 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my, int event, short ctrl, short shift)
1503 {
1504         ScrArea *sa = CTX_wm_area(C);
1505         ARegion *ar = CTX_wm_region(C);
1506         Panel *pa;
1507 #ifdef USE_PIN_HIDDEN
1508         const bool show_pin = UI_panel_category_is_visible(ar) && (block->panel->flag & PNL_PIN);
1509 #else
1510         const bool show_pin = UI_panel_category_is_visible(ar);
1511 #endif
1512         const bool is_subpanel = (block->panel->type && block->panel->type->parent);
1513         const bool show_drag = !is_subpanel;
1514
1515         int align = panel_aligned(sa, ar), button = 0;
1516
1517         rctf rect_drag, rect_pin;
1518         float rect_leftmost;
1519
1520
1521         /* drag and pin rect's */
1522         rect_drag = block->rect;
1523         rect_drag.xmin = block->rect.xmax - (PNL_ICON * 1.5f);
1524         rect_pin = rect_drag;
1525         if (show_pin) {
1526                 BLI_rctf_translate(&rect_pin, -PNL_ICON, 0.0f);
1527         }
1528         rect_leftmost = rect_pin.xmin;
1529
1530         /* mouse coordinates in panel space! */
1531
1532         /* XXX weak code, currently it assumes layout style for location of widgets */
1533
1534         /* check open/collapsed button */
1535         if (event == RETKEY)
1536                 button = 1;
1537         else if (event == AKEY)
1538                 button = 1;
1539         else if (ELEM(event, 0, RETKEY, LEFTMOUSE) && shift) {
1540                 block->panel->flag ^= PNL_PIN;
1541                 button = 2;
1542         }
1543         else if (block->panel->flag & PNL_CLOSEDX) {
1544                 if (my >= block->rect.ymax) button = 1;
1545         }
1546         else if (block->panel->control & UI_PNL_CLOSE) {
1547                 /* whole of header can be used to collapse panel (except top-right corner) */
1548                 if (mx <= block->rect.xmax - 8 - PNL_ICON) button = 2;
1549                 //else if (mx <= block->rect.xmin + 10 + 2 * PNL_ICON + 2) button = 1;
1550         }
1551         else if (mx < rect_leftmost) {
1552                 button = 1;
1553         }
1554
1555         if (button) {
1556                 if (button == 2) {  /* close */
1557                         ED_region_tag_redraw(ar);
1558                 }
1559                 else {  /* collapse */
1560                         if (ctrl) {
1561                                 panels_collapse_all(sa, ar, block->panel);
1562
1563                                 /* reset the view - we don't want to display a view without content */
1564                                 UI_view2d_offset(&ar->v2d, 0.0f, 1.0f);
1565                         }
1566
1567                         if (block->panel->flag & PNL_CLOSED) {
1568                                 block->panel->flag &= ~PNL_CLOSED;
1569                                 /* snap back up so full panel aligns with screen edge */
1570                                 if (block->panel->snap & PNL_SNAP_BOTTOM)
1571                                         block->panel->ofsy = 0;
1572
1573                                 if (event == LEFTMOUSE) {
1574                                         ui_panel_drag_collapse_handler_add(C, false);
1575                                 }
1576                         }
1577                         else if (align == BUT_HORIZONTAL) {
1578                                 block->panel->flag |= PNL_CLOSEDX;
1579
1580                                 if (event == LEFTMOUSE) {
1581                                         ui_panel_drag_collapse_handler_add(C, true);
1582                                 }
1583                         }
1584                         else {
1585                                 /* snap down to bottom screen edge */
1586                                 block->panel->flag |= PNL_CLOSEDY;
1587                                 if (block->panel->snap & PNL_SNAP_BOTTOM)
1588                                         block->panel->ofsy = -block->panel->sizey;
1589
1590                                 if (event == LEFTMOUSE) {
1591                                         ui_panel_drag_collapse_handler_add(C, true);
1592                                 }
1593                         }
1594
1595                         for (pa = ar->panels.first; pa; pa = pa->next) {
1596                                 if (pa->paneltab == block->panel) {
1597                                         if (block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1598                                         else pa->flag &= ~PNL_CLOSED;
1599                                 }
1600                         }
1601                 }
1602
1603                 if (align) {
1604                         panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1605                 }
1606                 else {
1607                         /* FIXME: this doesn't update the panel drawing, assert to avoid debugging why this is.
1608                          * We could fix this in the future if it's ever needed. */
1609                         BLI_assert(0);
1610                         ED_region_tag_redraw(ar);
1611                 }
1612         }
1613         else if (show_drag && BLI_rctf_isect_x(&rect_drag, mx)) {
1614                 /* XXX, for now don't allow dragging in floating windows yet. */
1615                 if (ar->alignment == RGN_ALIGN_FLOAT) {
1616                         return;
1617                 }
1618                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1619         }
1620         else if (show_pin && BLI_rctf_isect_x(&rect_pin, mx)) {
1621                 block->panel->flag ^= PNL_PIN;
1622                 ED_region_tag_redraw(ar);
1623         }
1624 }
1625
1626 bool UI_panel_category_is_visible(ARegion *ar)
1627 {
1628         /* more than one */
1629         return ar->panels_category.first && ar->panels_category.first != ar->panels_category.last;
1630 }
1631
1632 PanelCategoryDyn *UI_panel_category_find(ARegion *ar, const char *idname)
1633 {
1634         return BLI_findstring(&ar->panels_category, idname, offsetof(PanelCategoryDyn, idname));
1635 }
1636
1637 PanelCategoryStack *UI_panel_category_active_find(ARegion *ar, const char *idname)
1638 {
1639         return BLI_findstring(&ar->panels_category_active, idname, offsetof(PanelCategoryStack, idname));
1640 }
1641
1642 const char *UI_panel_category_active_get(ARegion *ar, bool set_fallback)
1643 {
1644         PanelCategoryStack *pc_act;
1645
1646         for (pc_act = ar->panels_category_active.first; pc_act; pc_act = pc_act->next) {
1647                 if (UI_panel_category_find(ar, pc_act->idname)) {
1648                         return pc_act->idname;
1649                 }
1650         }
1651
1652         if (set_fallback) {
1653                 PanelCategoryDyn *pc_dyn = ar->panels_category.first;
1654                 if (pc_dyn) {
1655                         UI_panel_category_active_set(ar, pc_dyn->idname);
1656                         return pc_dyn->idname;
1657                 }
1658         }
1659
1660         return NULL;
1661 }
1662
1663 void UI_panel_category_active_set(ARegion *ar, const char *idname)
1664 {
1665         ListBase *lb = &ar->panels_category_active;
1666         PanelCategoryStack *pc_act = UI_panel_category_active_find(ar, idname);
1667
1668         if (pc_act) {
1669                 BLI_remlink(lb, pc_act);
1670         }
1671         else {
1672                 pc_act = MEM_callocN(sizeof(PanelCategoryStack), __func__);
1673                 BLI_strncpy(pc_act->idname, idname, sizeof(pc_act->idname));
1674         }
1675
1676         BLI_addhead(lb, pc_act);
1677
1678
1679         /* validate all active panels, we could do this on load,
1680          * they are harmless - but we should remove somewhere.
1681          * (addons could define own and gather cruft over time) */
1682         {
1683                 PanelCategoryStack *pc_act_next;
1684                 /* intentionally skip first */
1685                 pc_act_next = pc_act->next;
1686                 while ((pc_act = pc_act_next)) {
1687                         pc_act_next = pc_act->next;
1688                         if (!BLI_findstring(&ar->type->paneltypes, pc_act->idname, offsetof(PanelType, category))) {
1689                                 BLI_remlink(lb, pc_act);
1690                                 MEM_freeN(pc_act);
1691                         }
1692                 }
1693         }
1694 }
1695
1696 PanelCategoryDyn *UI_panel_category_find_mouse_over_ex(ARegion *ar, const int x, const int y)
1697 {
1698         PanelCategoryDyn *ptd;
1699
1700         for (ptd = ar->panels_category.first; ptd; ptd = ptd->next) {
1701                 if (BLI_rcti_isect_pt(&ptd->rect, x, y)) {
1702                         return ptd;
1703                 }
1704         }
1705
1706         return NULL;
1707 }
1708
1709 PanelCategoryDyn *UI_panel_category_find_mouse_over(ARegion *ar, const wmEvent *event)
1710 {
1711         return UI_panel_category_find_mouse_over_ex(ar, event->mval[0], event->mval[1]);
1712 }
1713
1714
1715 void UI_panel_category_add(ARegion *ar, const char *name)
1716 {
1717         PanelCategoryDyn *pc_dyn = MEM_callocN(sizeof(*pc_dyn), __func__);
1718         BLI_addtail(&ar->panels_category, pc_dyn);
1719
1720         BLI_strncpy(pc_dyn->idname, name, sizeof(pc_dyn->idname));
1721
1722         /* 'pc_dyn->rect' must be set on draw */
1723 }
1724
1725 void UI_panel_category_clear_all(ARegion *ar)
1726 {
1727         BLI_freelistN(&ar->panels_category);
1728 }
1729
1730 static void imm_buf_append(
1731         float vbuf[][2],
1732         uchar cbuf[][3],
1733         float x, float y, const uchar col[3], int *index)
1734 {
1735         ARRAY_SET_ITEMS(vbuf[*index], x, y);
1736         ARRAY_SET_ITEMS(cbuf[*index], UNPACK3(col));
1737         (*index)++;
1738 }
1739
1740 /* based on UI_draw_roundbox, check on making a version which allows us to skip some sides */
1741 static void ui_panel_category_draw_tab(
1742         bool filled, float minx, float miny, float maxx, float maxy, float rad,
1743         const int roundboxtype,
1744         const bool use_highlight, const bool use_shadow, const bool use_flip_x,
1745         const uchar highlight_fade[3],
1746         const uchar col[3])
1747 {
1748         float vec[4][2] = {
1749             {0.195, 0.02},
1750             {0.55, 0.169},
1751             {0.831, 0.45},
1752             {0.98, 0.805}};
1753         int a;
1754
1755         /* mult */
1756         for (a = 0; a < 4; a++) {
1757                 mul_v2_fl(vec[a], rad);
1758         }
1759
1760         uint vert_len = 0;
1761         if (use_highlight) {
1762                 vert_len += (roundboxtype & UI_CNR_TOP_RIGHT) ? 6 : 1;
1763                 vert_len += (roundboxtype & UI_CNR_TOP_LEFT) ? 6 : 1;
1764         }
1765         if (use_highlight && !use_shadow) {
1766                 vert_len++;
1767         }
1768         else {
1769                 vert_len += (roundboxtype & UI_CNR_BOTTOM_RIGHT) ? 6 : 1;
1770                 vert_len += (roundboxtype & UI_CNR_BOTTOM_LEFT) ? 6 : 1;
1771         }
1772         /* Maximum size. */
1773         float vbuf[24][2];
1774         uchar cbuf[24][3];
1775         int buf_index = 0;
1776
1777         /* start with corner right-top */
1778         if (use_highlight) {
1779                 if (roundboxtype & UI_CNR_TOP_RIGHT) {
1780                         imm_buf_append(vbuf, cbuf, maxx, maxy - rad, col, &buf_index);
1781                         for (a = 0; a < 4; a++) {
1782                                 imm_buf_append(vbuf, cbuf, maxx - vec[a][1], maxy - rad + vec[a][0], col, &buf_index);
1783                         }
1784                         imm_buf_append(vbuf, cbuf, maxx - rad, maxy, col, &buf_index);
1785                 }
1786                 else {
1787                         imm_buf_append(vbuf, cbuf, maxx, maxy, col, &buf_index);
1788                 }
1789
1790                 /* corner left-top */
1791                 if (roundboxtype & UI_CNR_TOP_LEFT) {
1792                         imm_buf_append(vbuf, cbuf, minx + rad, maxy, col, &buf_index);
1793                         for (a = 0; a < 4; a++) {
1794                                 imm_buf_append(vbuf, cbuf, minx + rad - vec[a][0], maxy - vec[a][1], col, &buf_index);
1795                         }
1796                         imm_buf_append(vbuf, cbuf, minx, maxy - rad, col, &buf_index);
1797                 }
1798                 else {
1799                         imm_buf_append(vbuf, cbuf, minx, maxy, col, &buf_index);
1800                 }
1801         }
1802
1803         if (use_highlight && !use_shadow) {
1804                 imm_buf_append(vbuf, cbuf, minx, miny + rad, highlight_fade ? col : highlight_fade, &buf_index);
1805         }
1806         else {
1807                 /* corner left-bottom */
1808                 if (roundboxtype & UI_CNR_BOTTOM_LEFT) {
1809                         imm_buf_append(vbuf, cbuf,  minx, miny + rad, col, &buf_index);
1810                         for (a = 0; a < 4; a++) {
1811                                 imm_buf_append(vbuf, cbuf,  minx + vec[a][1], miny + rad - vec[a][0], col, &buf_index);
1812                         }
1813                         imm_buf_append(vbuf, cbuf, minx + rad, miny, col, &buf_index);
1814                 }
1815                 else {
1816                         imm_buf_append(vbuf, cbuf, minx, miny, col, &buf_index);
1817                 }
1818
1819                 /* corner right-bottom */
1820                 if (roundboxtype & UI_CNR_BOTTOM_RIGHT) {
1821                         imm_buf_append(vbuf, cbuf, maxx - rad, miny, col, &buf_index);
1822                         for (a = 0; a < 4; a++) {
1823                                 imm_buf_append(vbuf, cbuf, maxx - rad + vec[a][0], miny + vec[a][1], col, &buf_index);
1824                         }
1825                         imm_buf_append(vbuf, cbuf, maxx, miny + rad, col, &buf_index);
1826                 }
1827                 else {
1828                         imm_buf_append(vbuf, cbuf, maxx, miny, col, &buf_index);
1829                 }
1830         }
1831
1832         if (use_flip_x) {
1833                 float midx = (minx + maxx) / 2.0f;
1834                 for (int i = 0; i < buf_index; i++) {
1835                         vbuf[i][0] = midx - (vbuf[i][0] - midx);
1836                 }
1837
1838         }
1839
1840         GPUVertFormat *format = immVertexFormat();
1841         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
1842         uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
1843
1844         immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR);
1845         immBegin(filled ? GPU_PRIM_TRI_FAN : GPU_PRIM_LINE_STRIP, vert_len);
1846         for (int i = 0; i < buf_index; i++) {
1847                 immAttr3ubv(color, cbuf[i]);
1848                 immVertex2fv(pos, vbuf[i]);
1849         }
1850         immEnd();
1851         immUnbindProgram();
1852 }
1853
1854
1855 /**
1856  * Draw vertical tabs on the left side of the region,
1857  * one tab per category.
1858  */
1859 void UI_panel_category_draw_all(ARegion *ar, const char *category_id_active)
1860 {
1861         /* no tab outlines for */
1862 // #define USE_FLAT_INACTIVE
1863         const bool is_left = (ar->alignment != RGN_ALIGN_RIGHT);
1864         View2D *v2d = &ar->v2d;
1865         uiStyle *style = UI_style_get();
1866         const uiFontStyle *fstyle = &style->widget;
1867         const int fontid = fstyle->uifont_id;
1868         short fstyle_points = fstyle->points;
1869         PanelCategoryDyn *pc_dyn;
1870         const float aspect = ((uiBlock *)ar->uiblocks.first)->aspect;
1871         const float zoom = 1.0f / aspect;
1872         const int px = max_ii(1, round_fl_to_int(U.pixelsize));
1873         const int px_x_sign = is_left ? px : -px;
1874         const int category_tabs_width = round_fl_to_int(UI_PANEL_CATEGORY_MARGIN_WIDTH * zoom);
1875         const float dpi_fac = UI_DPI_FAC;
1876         const int tab_v_pad_text = round_fl_to_int((2 + ((px * 3) * dpi_fac)) * zoom);  /* padding of tabs around text */
1877         const int tab_v_pad = round_fl_to_int((4 + (2 * px * dpi_fac)) * zoom);  /* padding between tabs */
1878         const float tab_curve_radius = ((px * 3) * dpi_fac) * zoom;
1879         /* We flip the tab drawing, so always use these flags. */
1880         const int roundboxtype = UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT;
1881         bool is_alpha;
1882         bool do_scaletabs = false;
1883 #ifdef USE_FLAT_INACTIVE
1884         bool is_active_prev = false;
1885 #endif
1886         float scaletabs = 1.0f;
1887         /* same for all tabs */
1888         /* intentionally dont scale by 'px' */
1889         const int rct_xmin = is_left ? v2d->mask.xmin + 3 : (v2d->mask.xmax - category_tabs_width);
1890         const int rct_xmax = is_left ? v2d->mask.xmin + category_tabs_width : (v2d->mask.xmax - 3);
1891         const int text_v_ofs = (rct_xmax - rct_xmin) * 0.3f;
1892
1893         int y_ofs = tab_v_pad;
1894
1895         /* Primary theme colors */
1896         uchar theme_col_back[4];
1897         uchar theme_col_text[3];
1898         uchar theme_col_text_hi[3];
1899
1900         /* Tab colors */
1901         uchar theme_col_tab_bg[4];
1902         uchar theme_col_tab_active[3];
1903         uchar theme_col_tab_inactive[3];
1904
1905         /* Secondary theme colors */
1906         uchar theme_col_tab_outline[3];
1907         uchar theme_col_tab_divider[3];  /* line that divides tabs from the main region */
1908         uchar theme_col_tab_highlight[3];
1909         uchar theme_col_tab_highlight_inactive[3];
1910
1911
1912
1913         UI_GetThemeColor4ubv(TH_BACK, theme_col_back);
1914         UI_GetThemeColor3ubv(TH_TEXT, theme_col_text);
1915         UI_GetThemeColor3ubv(TH_TEXT_HI, theme_col_text_hi);
1916
1917         UI_GetThemeColor4ubv(TH_TAB_BACK, theme_col_tab_bg);
1918         UI_GetThemeColor3ubv(TH_TAB_ACTIVE, theme_col_tab_active);
1919         UI_GetThemeColor3ubv(TH_TAB_INACTIVE, theme_col_tab_inactive);
1920         UI_GetThemeColor3ubv(TH_TAB_OUTLINE, theme_col_tab_outline);
1921
1922         interp_v3_v3v3_uchar(theme_col_tab_divider, theme_col_back, theme_col_tab_outline, 0.3f);
1923         interp_v3_v3v3_uchar(theme_col_tab_highlight, theme_col_back, theme_col_text_hi, 0.2f);
1924         interp_v3_v3v3_uchar(theme_col_tab_highlight_inactive, theme_col_tab_inactive, theme_col_text_hi, 0.12f);
1925
1926         is_alpha = (ar->overlap && (theme_col_back[3] != 255));
1927
1928         if (fstyle->kerning == 1) {
1929                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1930         }
1931
1932         BLF_enable(fontid, BLF_ROTATION);
1933         BLF_rotation(fontid, M_PI_2);
1934         //UI_fontstyle_set(&style->widget);
1935         ui_fontscale(&fstyle_points, aspect / (U.pixelsize * 1.1f));
1936         BLF_size(fontid, fstyle_points, U.dpi);
1937
1938         BLI_assert(UI_panel_category_is_visible(ar));
1939
1940
1941         /* calculate tab rect's and check if we need to scale down */
1942         for (pc_dyn = ar->panels_category.first; pc_dyn; pc_dyn = pc_dyn->next) {
1943                 rcti *rct = &pc_dyn->rect;
1944                 const char *category_id = pc_dyn->idname;
1945                 const char *category_id_draw = IFACE_(category_id);
1946                 const int category_width = BLF_width(fontid, category_id_draw, BLF_DRAW_STR_DUMMY_MAX);
1947
1948                 rct->xmin = rct_xmin;
1949                 rct->xmax = rct_xmax;
1950
1951                 rct->ymin = v2d->mask.ymax - (y_ofs + category_width + (tab_v_pad_text * 2));
1952                 rct->ymax = v2d->mask.ymax - (y_ofs);
1953
1954                 y_ofs += category_width + tab_v_pad + (tab_v_pad_text * 2);
1955         }
1956
1957         if (y_ofs > BLI_rcti_size_y(&v2d->mask)) {
1958                 scaletabs = (float)BLI_rcti_size_y(&v2d->mask) / (float)y_ofs;
1959
1960                 for (pc_dyn = ar->panels_category.first; pc_dyn; pc_dyn = pc_dyn->next) {
1961                         rcti *rct = &pc_dyn->rect;
1962                         rct->ymin = ((rct->ymin - v2d->mask.ymax) * scaletabs) + v2d->mask.ymax;
1963                         rct->ymax = ((rct->ymax - v2d->mask.ymax) * scaletabs) + v2d->mask.ymax;
1964                 }
1965
1966                 do_scaletabs = true;
1967         }
1968
1969
1970         /* begin drawing */
1971         GPU_line_smooth(true);
1972
1973         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
1974         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
1975
1976         /* draw the background */
1977         if (is_alpha) {
1978                 GPU_blend(true);
1979                 immUniformColor4ubv(theme_col_tab_bg);
1980         }
1981         else {
1982                 immUniformColor3ubv(theme_col_tab_bg);
1983         }
1984
1985         if (is_left) {
1986                 immRecti(pos, v2d->mask.xmin, v2d->mask.ymin, v2d->mask.xmin + category_tabs_width, v2d->mask.ymax);
1987         }
1988         else {
1989                 immRecti(pos, v2d->mask.xmax - category_tabs_width, v2d->mask.ymin, v2d->mask.xmax, v2d->mask.ymax);
1990         }
1991
1992         if (is_alpha) {
1993                 GPU_blend(false);
1994         }
1995
1996         immUnbindProgram();
1997
1998         const int divider_xmin =
1999                 is_left ? (v2d->mask.xmin + (category_tabs_width - px)) : (v2d->mask.xmax - category_tabs_width) + px;
2000         const int divider_xmax =
2001                 is_left ? (v2d->mask.xmin + category_tabs_width) : (v2d->mask.xmax - (category_tabs_width + px)) + px;
2002
2003         for (pc_dyn = ar->panels_category.first; pc_dyn; pc_dyn = pc_dyn->next) {
2004                 const rcti *rct = &pc_dyn->rect;
2005                 const char *category_id = pc_dyn->idname;
2006                 const char *category_id_draw = IFACE_(category_id);
2007                 int category_width = BLI_rcti_size_y(rct) - (tab_v_pad_text * 2);
2008                 size_t category_draw_len = BLF_DRAW_STR_DUMMY_MAX;
2009                 // int category_width = BLF_width(fontid, category_id_draw, BLF_DRAW_STR_DUMMY_MAX);
2010
2011                 const bool is_active = STREQ(category_id, category_id_active);
2012
2013                 GPU_blend(true);
2014
2015 #ifdef USE_FLAT_INACTIVE
2016                 if (is_active)
2017 #endif
2018                 {
2019                         const bool use_flip_x = !is_left;
2020                         ui_panel_category_draw_tab(
2021                                 true, rct->xmin, rct->ymin, rct->xmax, rct->ymax,
2022                                 tab_curve_radius - px, roundboxtype, true, true, use_flip_x,
2023                                 NULL,
2024                                 is_active ? theme_col_tab_active : theme_col_tab_inactive);
2025
2026                         /* tab outline */
2027                         ui_panel_category_draw_tab(
2028                                 false, rct->xmin - px_x_sign, rct->ymin - px, rct->xmax - px_x_sign, rct->ymax + px,
2029                                 tab_curve_radius, roundboxtype, true, true, use_flip_x,
2030                                 NULL,
2031                                 theme_col_tab_outline);
2032
2033                         /* tab highlight (3d look) */
2034                         ui_panel_category_draw_tab(
2035                                 false, rct->xmin, rct->ymin, rct->xmax, rct->ymax,
2036                                 tab_curve_radius, roundboxtype, true, false, use_flip_x,
2037                                 is_active ? theme_col_back : theme_col_tab_inactive,
2038                                 is_active ? theme_col_tab_highlight : theme_col_tab_highlight_inactive);
2039                 }
2040
2041                 /* tab blackline */
2042                 if (!is_active) {
2043                         pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
2044                         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2045
2046                         immUniformColor3ubv(theme_col_tab_divider);
2047                         immRecti(pos,
2048                                  divider_xmin,
2049                                  rct->ymin - tab_v_pad,
2050                                  divider_xmax,
2051                                  rct->ymax + tab_v_pad);
2052                         immUnbindProgram();
2053                 }
2054
2055                 if (do_scaletabs) {
2056                         category_draw_len = BLF_width_to_strlen(
2057                                 fontid, category_id_draw, category_draw_len,
2058                                 category_width, NULL);
2059                 }
2060
2061                 BLF_position(fontid, rct->xmax - text_v_ofs, rct->ymin + tab_v_pad_text, 0.0f);
2062
2063                 /* tab titles */
2064
2065                 /* draw white shadow to give text more depth */
2066                 BLF_color3ubv(fontid, theme_col_text);
2067
2068                 /* main tab title */
2069                 BLF_draw(fontid, category_id_draw, category_draw_len);
2070
2071                 GPU_blend(false);
2072
2073                 /* tab blackline remaining (last tab) */
2074                 pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
2075                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2076                 if (pc_dyn->prev == NULL) {
2077                         immUniformColor3ubv(theme_col_tab_divider);
2078                         immRecti(pos,
2079                                  divider_xmin,
2080                                  rct->ymax + px,
2081                                  divider_xmax,
2082                                  v2d->mask.ymax);
2083                 }
2084                 if (pc_dyn->next == NULL) {
2085                         immUniformColor3ubv(theme_col_tab_divider);
2086                         immRecti(pos,
2087                                  divider_xmin,
2088                                  0,
2089                                  divider_xmax,
2090                                  rct->ymin);
2091                 }
2092
2093 #ifdef USE_FLAT_INACTIVE
2094                 /* draw line between inactive tabs */
2095                 if (is_active == false && is_active_prev == false && pc_dyn->prev) {
2096                         immUniformColor3ubv(theme_col_tab_divider);
2097                         immRecti(pos, v2d->mask.xmin + (category_tabs_width / 5),
2098                                  rct->ymax + px,
2099                                  (v2d->mask.xmin + category_tabs_width) - (category_tabs_width / 5),
2100                                  rct->ymax + (px * 3));
2101                 }
2102
2103                 is_active_prev = is_active;
2104 #endif
2105                 immUnbindProgram();
2106
2107                 /* not essential, but allows events to be handled right up until the region edge [#38171] */
2108                 if (is_left) {
2109                         pc_dyn->rect.xmin = v2d->mask.xmin;
2110                 }
2111                 else {
2112                         pc_dyn->rect.xmax = v2d->mask.xmax;
2113                 }
2114         }
2115
2116         GPU_line_smooth(false);
2117
2118         BLF_disable(fontid, BLF_ROTATION);
2119
2120         if (fstyle->kerning == 1) {
2121                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
2122         }
2123
2124 #undef USE_FLAT_INACTIVE
2125 }
2126
2127 static int ui_handle_panel_category_cycling(const wmEvent *event, ARegion *ar, const uiBut *active_but)
2128 {
2129         const bool is_mousewheel = ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE);
2130         const bool inside_tabregion = (
2131                 (ar->alignment != RGN_ALIGN_RIGHT) ?
2132                 (event->mval[0] < ((PanelCategoryDyn *)ar->panels_category.first)->rect.xmax) :
2133                 (event->mval[0] > ((PanelCategoryDyn *)ar->panels_category.first)->rect.xmin));
2134
2135         /* if mouse is inside non-tab region, ctrl key is required */
2136         if (is_mousewheel && !event->ctrl && !inside_tabregion)
2137                 return WM_UI_HANDLER_CONTINUE;
2138
2139
2140         if (active_but && ui_but_supports_cycling(active_but)) {
2141                 /* skip - exception to make cycling buttons
2142                  * using ctrl+mousewheel work in tabbed regions */
2143         }
2144         else {
2145                 const char *category = UI_panel_category_active_get(ar, false);
2146                 if (LIKELY(category)) {
2147                         PanelCategoryDyn *pc_dyn = UI_panel_category_find(ar, category);
2148                         if (LIKELY(pc_dyn)) {
2149                                 if (is_mousewheel) {
2150                                         /* we can probably get rid of this and only allow ctrl+tabbing */
2151                                         pc_dyn = (event->type == WHEELDOWNMOUSE) ? pc_dyn->next : pc_dyn->prev;
2152                                 }
2153                                 else {
2154                                         const bool backwards = event->shift;
2155                                         pc_dyn = backwards ? pc_dyn->prev : pc_dyn->next;
2156                                         if (!pc_dyn) {
2157                                                 /* proper cyclic behavior, back to first/last category (only used for ctrl+tab) */
2158                                                 pc_dyn = backwards ? ar->panels_category.last : ar->panels_category.first;
2159                                         }
2160                                 }
2161
2162                                 if (pc_dyn) {
2163                                         /* intentionally don't reset scroll in this case,
2164                                          * this allows for quick browsing between tabs */
2165                                         UI_panel_category_active_set(ar, pc_dyn->idname);
2166                                         ED_region_tag_redraw(ar);
2167                                 }
2168                         }
2169                 }
2170                 return WM_UI_HANDLER_BREAK;
2171         }
2172
2173         return WM_UI_HANDLER_CONTINUE;
2174 }
2175
2176 /* XXX should become modal keymap */
2177 /* AKey is opening/closing panels, independent of button state now */
2178
2179 int ui_handler_panel_region(bContext *C, const wmEvent *event, ARegion *ar, const uiBut *active_but)
2180 {
2181         uiBlock *block;
2182         Panel *pa;
2183         int retval, mx, my;
2184         bool has_category_tabs = UI_panel_category_is_visible(ar);
2185
2186         retval = WM_UI_HANDLER_CONTINUE;
2187
2188         /* Scrollbars can overlap panels now, they have handling priority. */
2189         if (UI_view2d_mouse_in_scrollers(ar, &ar->v2d, event->x, event->y)) {
2190                 return retval;
2191         }
2192
2193         /* handle category tabs */
2194         if (has_category_tabs) {
2195                 if (event->val == KM_PRESS) {
2196                         if (event->type == LEFTMOUSE) {
2197                                 PanelCategoryDyn *pc_dyn = UI_panel_category_find_mouse_over(ar, event);
2198                                 if (pc_dyn) {
2199                                         UI_panel_category_active_set(ar, pc_dyn->idname);
2200                                         ED_region_tag_redraw(ar);
2201
2202                                         /* reset scroll to the top [#38348] */
2203                                         UI_view2d_offset(&ar->v2d, -1.0f, 1.0f);
2204
2205                                         retval = WM_UI_HANDLER_BREAK;
2206                                 }
2207                         }
2208                         else if ((event->type == TABKEY && event->ctrl) || ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE)) {
2209                                 /* cycle tabs */
2210                                 retval = ui_handle_panel_category_cycling(event, ar, active_but);
2211                         }
2212                 }
2213         }
2214
2215         if (retval == WM_UI_HANDLER_BREAK) {
2216                 return retval;
2217         }
2218
2219         for (block = ar->uiblocks.last; block; block = block->prev) {
2220                 uiPanelMouseState mouse_state;
2221
2222                 mx = event->x;
2223                 my = event->y;
2224                 ui_window_to_block(ar, block, &mx, &my);
2225
2226                 /* checks for mouse position inside */
2227                 pa = block->panel;
2228
2229                 if (!pa || pa->paneltab != NULL)
2230                         continue;
2231                 if (pa->type && pa->type->flag & PNL_NO_HEADER)  /* XXX - accessed freed panels when scripts reload, need to fix. */
2232                         continue;
2233
2234                 mouse_state = ui_panel_mouse_state_get(block, pa, mx, my);
2235
2236                 /* XXX hardcoded key warning */
2237                 if (ELEM(mouse_state, PANEL_MOUSE_INSIDE_CONTENT, PANEL_MOUSE_INSIDE_HEADER) && event->val == KM_PRESS) {
2238                         if (event->type == AKEY && ((event->ctrl + event->oskey + event->shift + event->alt) == 0)) {
2239
2240                                 if (pa->flag & PNL_CLOSEDY) {
2241                                         if ((block->rect.ymax <= my) && (block->rect.ymax + PNL_HEADER >= my))
2242                                                 ui_handle_panel_header(C, block, mx, my, event->type, event->ctrl, event->shift);
2243                                 }
2244                                 else
2245                                         ui_handle_panel_header(C, block, mx, my, event->type, event->ctrl, event->shift);
2246
2247                                 retval = WM_UI_HANDLER_BREAK;
2248                                 continue;
2249                         }
2250                 }
2251
2252                 /* on active button, do not handle panels */
2253                 if (ui_but_is_active(ar))
2254                         continue;
2255
2256                 if (ELEM(mouse_state, PANEL_MOUSE_INSIDE_CONTENT, PANEL_MOUSE_INSIDE_HEADER)) {
2257
2258                         if (event->val == KM_PRESS) {
2259
2260                                 /* open close on header */
2261                                 if (ELEM(event->type, RETKEY, PADENTER)) {
2262                                         if (mouse_state == PANEL_MOUSE_INSIDE_HEADER) {
2263                                                 ui_handle_panel_header(C, block, mx, my, RETKEY, event->ctrl, event->shift);
2264                                                 retval = WM_UI_HANDLER_BREAK;
2265                                                 break;
2266                                         }
2267                                 }
2268                                 else if (event->type == LEFTMOUSE) {
2269                                         /* all inside clicks should return in break - overlapping/float panels */
2270                                         retval = WM_UI_HANDLER_BREAK;
2271
2272                                         if (mouse_state == PANEL_MOUSE_INSIDE_HEADER) {
2273                                                 ui_handle_panel_header(C, block, mx, my, event->type, event->ctrl, event->shift);
2274                                                 retval = WM_UI_HANDLER_BREAK;
2275                                                 break;
2276                                         }
2277                                         else if ((mouse_state == PANEL_MOUSE_INSIDE_SCALE) && !(pa->flag & PNL_CLOSED)) {
2278                                                 panel_activate_state(C, pa, PANEL_STATE_DRAG_SCALE);
2279                                                 retval = WM_UI_HANDLER_BREAK;
2280                                                 break;
2281                                         }
2282
2283                                 }
2284                                 else if (event->type == RIGHTMOUSE) {
2285                                         if (mouse_state == PANEL_MOUSE_INSIDE_HEADER) {
2286                                                 ui_popup_context_menu_for_panel(C, ar, block->panel);
2287                                                 retval = WM_UI_HANDLER_BREAK;
2288                                                 break;
2289                                         }
2290                                 }
2291                                 else if (event->type == ESCKEY) {
2292                                         /*XXX 2.50*/
2293 #if 0
2294                                         if (block->handler) {
2295                                                 rem_blockhandler(sa, block->handler);
2296                                                 ED_region_tag_redraw(ar);
2297                                                 retval = WM_UI_HANDLER_BREAK;
2298                                         }
2299 #endif
2300                                 }
2301                                 else if (event->type == PADPLUSKEY || event->type == PADMINUS) {
2302 #if 0 /* XXX make float panel exception? */
2303                                         int zoom = 0;
2304
2305                                         /* if panel is closed, only zoom if mouse is over the header */
2306                                         if (pa->flag & (PNL_CLOSEDX | PNL_CLOSEDY)) {
2307                                                 if (inside_header)
2308                                                         zoom = 1;
2309                                         }
2310                                         else
2311                                                 zoom = 1;
2312
2313                                         if (zoom) {
2314                                                 ScrArea *sa = CTX_wm_area(C);
2315                                                 SpaceLink *sl = sa->spacedata.first;
2316
2317                                                 if (sa->spacetype != SPACE_BUTS) {
2318                                                         if (!(pa->control & UI_PNL_SCALE)) {
2319                                                                 if (event->type == PADPLUSKEY) sl->blockscale += 0.1;
2320                                                                 else sl->blockscale -= 0.1;
2321                                                                 CLAMP(sl->blockscale, 0.6, 1.0);
2322
2323                                                                 ED_region_tag_redraw(ar);
2324                                                                 retval = WM_UI_HANDLER_BREAK;
2325                                                         }
2326                                                 }
2327                                         }
2328 #endif
2329                                 }
2330                         }
2331                 }
2332         }
2333
2334         return retval;
2335 }
2336
2337 /**************** window level modal panel interaction **************/
2338
2339 /* note, this is modal handler and should not swallow events for animation */
2340 static int ui_handler_panel(bContext *C, const wmEvent *event, void *userdata)
2341 {
2342         Panel *panel = userdata;
2343         uiHandlePanelData *data = panel->activedata;
2344
2345         /* verify if we can stop */
2346         if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
2347                 ScrArea *sa = CTX_wm_area(C);
2348                 ARegion *ar = CTX_wm_region(C);
2349                 int align = panel_aligned(sa, ar);
2350
2351                 if (align)
2352                         panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
2353                 else
2354                         panel_activate_state(C, panel, PANEL_STATE_EXIT);
2355         }
2356         else if (event->type == MOUSEMOVE) {
2357                 if (data->state == PANEL_STATE_DRAG)
2358                         ui_do_drag(C, event, panel);
2359         }
2360         else if (event->type == TIMER && event->customdata == data->animtimer) {
2361                 if (data->state == PANEL_STATE_ANIMATION)
2362                         ui_do_animate(C, panel);
2363                 else if (data->state == PANEL_STATE_DRAG)
2364                         ui_do_drag(C, event, panel);
2365         }
2366
2367         data = panel->activedata;
2368
2369         if (data && data->state == PANEL_STATE_ANIMATION)
2370                 return WM_UI_HANDLER_CONTINUE;
2371         else
2372                 return WM_UI_HANDLER_BREAK;
2373 }
2374
2375 static void ui_handler_remove_panel(bContext *C, void *userdata)
2376 {
2377         Panel *pa = userdata;
2378
2379         panel_activate_state(C, pa, PANEL_STATE_EXIT);
2380 }
2381
2382 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
2383 {
2384         uiHandlePanelData *data = pa->activedata;
2385         wmWindow *win = CTX_wm_window(C);
2386         ARegion *ar = CTX_wm_region(C);
2387
2388         if (data && data->state == state)
2389                 return;
2390
2391         if (state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
2392                 if (data && data->state != PANEL_STATE_ANIMATION) {
2393                         /* XXX:
2394                          * - the panel tabbing function call below (test_add_new_tabs()) has been commented out
2395                          *   "It is too easy to do by accident when reordering panels,
2396                          *   is very hard to control and use, and has no real benefit." - BillRey
2397                          * Aligorith, 2009Sep
2398                          */
2399                         //test_add_new_tabs(ar);   // also copies locations of tabs in dragged panel
2400                         check_panel_overlap(ar, NULL);  /* clears */
2401                 }
2402
2403                 pa->flag &= ~PNL_SELECT;
2404         }
2405         else
2406                 pa->flag |= PNL_SELECT;
2407
2408         if (data && data->animtimer) {
2409                 WM_event_remove_timer(CTX_wm_manager(C), win, data->animtimer);
2410                 data->animtimer = NULL;
2411         }
2412
2413         if (state == PANEL_STATE_EXIT) {
2414                 MEM_freeN(data);
2415                 pa->activedata = NULL;
2416
2417                 WM_event_remove_ui_handler(&win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa, false);
2418         }
2419         else {
2420                 if (!data) {
2421                         data = MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
2422                         pa->activedata = data;
2423
2424                         WM_event_add_ui_handler(C, &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa, 0);
2425                 }
2426
2427                 if (ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
2428                         data->animtimer = WM_event_add_timer(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
2429
2430                 data->state = state;
2431                 data->startx = win->eventstate->x;
2432                 data->starty = win->eventstate->y;
2433                 data->startofsx = pa->ofsx;
2434                 data->startofsy = pa->ofsy;
2435                 data->startsizex = pa->sizex;
2436                 data->startsizey = pa->sizey;
2437                 data->starttime = PIL_check_seconds_timer();
2438         }
2439
2440         ED_region_tag_redraw(ar);
2441 }
2442
2443 PanelType *UI_paneltype_find(int space_id, int region_id, const char *idname)
2444 {
2445         SpaceType *st = BKE_spacetype_from_id(space_id);
2446         if (st) {
2447                 ARegionType *art = BKE_regiontype_from_id(st, region_id);
2448                 if (art) {
2449                         return BLI_findstring(&art->paneltypes, idname, offsetof(PanelType, idname));
2450                 }
2451         }
2452         return NULL;
2453 }