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