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