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