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