UI:
[blender.git] / source / blender / editors / interface / interface_panel.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2003-2009 full recode.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /* a full doc with API notes can be found in bf-blender/blender/doc/interface_API.txt */
29  
30 #include <math.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "PIL_time.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_arithb.h"
41
42 #include "DNA_screen_types.h"
43 #include "DNA_space_types.h"
44 #include "DNA_userdef_types.h"
45
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48 #include "BKE_utildefines.h"
49
50 #include "BIF_gl.h"
51 #include "BIF_glutil.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "ED_screen.h"
57
58 #include "UI_interface.h"
59 #include "UI_view2d.h"
60
61 #include "interface_intern.h"
62
63 /*********************** defines and structs ************************/
64
65 #define ANIMATION_TIME          0.30
66 #define ANIMATION_INTERVAL      0.02
67
68 #define PNL_LAST_ADDED          1
69 #define PNL_ACTIVE                      2
70 #define PNL_WAS_ACTIVE          4
71 #define PNL_ANIM_ALIGN          8
72
73 typedef enum uiHandlePanelState {
74         PANEL_STATE_DRAG,
75         PANEL_STATE_DRAG_SCALE,
76         PANEL_STATE_WAIT_UNTAB,
77         PANEL_STATE_ANIMATION,
78         PANEL_STATE_EXIT
79 } uiHandlePanelState;
80
81 typedef struct uiHandlePanelData {
82         uiHandlePanelState state;
83
84         /* animation */
85         wmTimer *animtimer;
86         double starttime;
87
88         /* dragging */
89         int startx, starty;
90         int startofsx, startofsy;
91         int startsizex, startsizey;
92 } uiHandlePanelData;
93
94 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state);
95
96 /*********************** space specific code ************************/
97 /* temporary code to remove all sbuts stuff from panel code         */
98
99 static int panel_aligned(ScrArea *sa, ARegion *ar)
100 {
101         if(sa->spacetype==SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
102                 SpaceButs *sbuts= sa->spacedata.first;
103                 return sbuts->align;
104         }
105         else if(ar->regiontype==RGN_TYPE_UI)
106                 return BUT_VERTICAL;
107         
108         return 0;
109 }
110
111 static int panels_re_align(ScrArea *sa, ARegion *ar, Panel **r_pa)
112 {
113         Panel *pa;
114         int active= 0;
115
116         *r_pa= NULL;
117
118         if(sa->spacetype==SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
119                 SpaceButs *sbuts= sa->spacedata.first;
120
121                 if(sbuts->align)
122                         if(sbuts->re_align || sbuts->mainbo!=sbuts->mainb || sbuts->tabo!=sbuts->tab[sbuts->mainb])
123                                 return 1;
124         }
125         else if(ar->regiontype==RGN_TYPE_UI)
126                 return 1;
127
128         /* in case panel is added or disappears */
129         for(pa=ar->panels.first; pa; pa=pa->next) {
130                 if((pa->runtime_flag & PNL_WAS_ACTIVE) && !(pa->runtime_flag & PNL_ACTIVE))
131                         return 1;
132                 if(!(pa->runtime_flag & PNL_WAS_ACTIVE) && (pa->runtime_flag & PNL_ACTIVE))
133                         return 1;
134                 if(pa->activedata)
135                         active= 1;
136         }
137
138         /* in case we need to do an animation (size changes) */
139         for(pa=ar->panels.first; pa; pa=pa->next) {
140                 if(pa->runtime_flag & PNL_ANIM_ALIGN) {
141                         if(!active)
142                                 *r_pa= pa;
143                         return 1;
144                 }
145         }
146         
147         return 0;
148 }
149
150 /****************************** panels ******************************/
151
152 static void ui_panel_copy_offset(Panel *pa, Panel *papar)
153 {
154         /* with respect to sizes... papar is parent */
155
156         pa->ofsx= papar->ofsx;
157         pa->ofsy= papar->ofsy + papar->sizey-pa->sizey;
158 }
159
160 Panel *uiBeginPanel(ARegion *ar, uiBlock *block, PanelType *pt, int *open)
161 {
162         uiStyle *style= U.uistyles.first;
163         Panel *pa, *patab, *palast, *panext;
164         char *panelname= pt->label;
165         char *tabname= pt->label;
166         char *hookname= NULL;
167         int newpanel;
168         
169         /* check if Panel exists, then use that one */
170         for(pa=ar->panels.first; pa; pa=pa->next)
171                 if(strncmp(pa->panelname, panelname, UI_MAX_NAME_STR)==0)
172                         if(strncmp(pa->tabname, tabname, UI_MAX_NAME_STR)==0)
173                                 break;
174         
175         newpanel= (pa == NULL);
176
177         if(!newpanel) {
178                 pa->type= pt;
179         }
180         else {
181                 /* new panel */
182                 pa= MEM_callocN(sizeof(Panel), "new panel");
183                 pa->type= pt;
184                 BLI_strncpy(pa->panelname, panelname, UI_MAX_NAME_STR);
185                 BLI_strncpy(pa->tabname, tabname, UI_MAX_NAME_STR);
186         
187                 pa->ofsx= 0;
188                 pa->ofsy= style->panelouter;
189                 pa->sizex= 0;
190                 pa->sizey= 0;
191
192                 BLI_addtail(&ar->panels, pa);
193                 
194                 /* make new Panel tabbed? */
195                 if(hookname) {
196                         for(patab= ar->panels.first; patab; patab= patab->next) {
197                                 if((patab->runtime_flag & PNL_ACTIVE) && patab->paneltab==NULL) {
198                                         if(strncmp(hookname, patab->panelname, UI_MAX_NAME_STR)==0) {
199                                                 if(strncmp(tabname, patab->tabname, UI_MAX_NAME_STR)==0) {
200                                                         pa->paneltab= patab;
201                                                         ui_panel_copy_offset(pa, patab);
202                                                         break;
203                                                 }
204                                         }
205                                 }
206                         } 
207                 }
208         }
209
210         /* if a new panel is added, we insert it right after the panel
211          * that was last added. this way new panels are inserted in the
212          * right place between versions */
213         for(palast=ar->panels.first; palast; palast=palast->next)
214                 if(palast->runtime_flag & PNL_LAST_ADDED)
215                         break;
216         
217         if(newpanel) {
218                 pa->sortorder= (palast)? palast->sortorder+1: 0;
219
220                 for(panext=ar->panels.first; panext; panext=panext->next)
221                         if(panext != pa && panext->sortorder >= pa->sortorder)
222                                 panext->sortorder++;
223         }
224
225         if(palast)
226                 palast->runtime_flag &= ~PNL_LAST_ADDED;
227
228         /* assign to block */
229         block->panel= pa;
230         pa->runtime_flag |= PNL_ACTIVE|PNL_LAST_ADDED;
231
232         *open= 0;
233
234         if(pa->paneltab) return pa;
235         if(pa->flag & PNL_CLOSED) return pa;
236
237         *open= 1;
238         pa->drawname[0]= 0; /* otherwise closes panels show wrong title */
239         
240         return pa;
241 }
242
243 void uiEndPanel(uiBlock *block, int width, int height)
244 {
245         Panel *pa= block->panel;
246
247         if(pa->sizex != width || pa->sizey != height) {
248                 pa->runtime_flag |= PNL_ANIM_ALIGN;
249                 pa->ofsy += pa->sizey-height;
250         }
251
252         pa->sizex= width;
253         pa->sizey= height;
254 }
255
256 #if 0
257 void uiPanelToMouse(const bContext *C, Panel *pa)
258 {
259         /* global control over this feature; UI_PNL_TO_MOUSE only called for hotkey panels */
260         if(U.uiflag & USER_PANELPINNED);
261         else if(pa->control & UI_PNL_TO_MOUSE) {
262                 int mx, my;
263
264                 mx= CTX_wm_window(C)->eventstate->x;
265                 my= CTX_wm_window(C)->eventstate->y;
266                 
267                 pa->ofsx= mx-pa->sizex/2;
268                 pa->ofsy= my-pa->sizey/2;
269                 
270                 if(pa->flag & PNL_CLOSED) pa->flag &= ~PNL_CLOSED;
271         }
272         
273         if(pa->control & UI_PNL_UNSTOW) {
274                 if(pa->flag & PNL_CLOSEDY) {
275                         pa->flag &= ~PNL_CLOSED;
276                 }
277         }
278 }
279 #endif
280
281 static int panel_has_tabs(ARegion *ar, Panel *panel)
282 {
283         Panel *pa= ar->panels.first;
284         
285         if(panel==NULL) return 0;
286         
287         while(pa) {
288                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==panel) {
289                         return 1;
290                 }
291                 pa= pa->next;
292         }
293         return 0;
294 }
295
296 static void ui_offset_panel_block(uiBlock *block)
297 {
298         uiStyle *style= U.uistyles.first;
299         uiBut *but;
300         int ofsy;
301
302         /* compute bounds and offset */
303         ui_bounds_block(block);
304
305         ofsy= block->panel->sizey - style->panelspace;
306
307         for(but= block->buttons.first; but; but=but->next) {
308                 but->y1 += ofsy;
309                 but->y2 += ofsy;
310         }
311
312         block->maxx= block->panel->sizex;
313         block->maxy= block->panel->sizey;
314         block->minx= block->miny= 0.0;
315 }
316
317 /**************************** drawing *******************************/
318
319 /* extern used by previewrender */
320 void uiPanelPush(uiBlock *block)
321 {
322         glPushMatrix(); 
323
324         if(block->panel)
325                 glTranslatef((float)block->panel->ofsx, (float)block->panel->ofsy, 0.0);
326 }
327
328 void uiPanelPop(uiBlock *block)
329 {
330         glPopMatrix();
331 }
332
333 /* triangle 'icon' for panel header */
334 void ui_draw_tria_icon(float x, float y, char dir)
335 {
336         if(dir=='h') {
337                 ui_draw_anti_tria(x-1, y, x-1, y+11.0, x+9, y+6.25);
338         }
339         else {
340                 ui_draw_anti_tria(x-3, y+10,  x+8-1, y+10, x+4.25-2, y);        
341         }
342 }
343
344 /* triangle 'icon' inside rect */
345 void ui_draw_tria_rect(rctf *rect, char dir)
346 {
347         if(dir=='h') {
348                 float half= 0.5f*(rect->ymax - rect->ymin);
349                 ui_draw_anti_tria(rect->xmin, rect->ymin, rect->xmin, rect->ymax, rect->xmax, rect->ymin+half);
350         }
351         else {
352                 float half= 0.5f*(rect->xmax - rect->xmin);
353                 ui_draw_anti_tria(rect->xmin, rect->ymax, rect->xmax, rect->ymax, rect->xmin+half, rect->ymin);
354         }
355 }
356
357 void ui_draw_anti_x(float x1, float y1, float x2, float y2)
358 {
359
360         /* set antialias line */
361         glEnable(GL_LINE_SMOOTH);
362         glEnable(GL_BLEND);
363
364         glLineWidth(2.0);
365         
366         fdrawline(x1, y1, x2, y2);
367         fdrawline(x1, y2, x2, y1);
368         
369         glLineWidth(1.0);
370         
371         glDisable(GL_LINE_SMOOTH);
372         glDisable(GL_BLEND);
373         
374 }
375
376 /* x 'icon' for panel header */
377 static void ui_draw_x_icon(float x, float y)
378 {
379
380         ui_draw_anti_x(x, y, x+9.375, y+9.375);
381
382 }
383
384 #define PNL_ICON        20
385
386 static void ui_draw_panel_scalewidget(rcti *rect)
387 {
388         float xmin, xmax, dx;
389         float ymin, ymax, dy;
390         
391         xmin= rect->xmax-PNL_HEADER+2;
392         xmax= rect->xmax-3;
393         ymin= rect->ymin+3;
394         ymax= rect->ymin+PNL_HEADER-2;
395                 
396         dx= 0.5f*(xmax-xmin);
397         dy= 0.5f*(ymax-ymin);
398         
399         glEnable(GL_BLEND);
400         glColor4ub(255, 255, 255, 50);
401         fdrawline(xmin, ymin, xmax, ymax);
402         fdrawline(xmin+dx, ymin, xmax, ymax-dy);
403         
404         glColor4ub(0, 0, 0, 50);
405         fdrawline(xmin, ymin+1, xmax, ymax+1);
406         fdrawline(xmin+dx, ymin+1, xmax, ymax-dy+1);
407         glDisable(GL_BLEND);
408 }
409
410 static void ui_draw_panel_dragwidget(rctf *rect)
411 {
412         float xmin, xmax, dx;
413         float ymin, ymax, dy;
414         
415         xmin= rect->xmin;
416         xmax= rect->xmax;
417         ymin= rect->ymin;
418         ymax= rect->ymax;
419         
420         dx= 0.333f*(xmax-xmin);
421         dy= 0.333f*(ymax-ymin);
422         
423         glEnable(GL_BLEND);
424         glColor4ub(255, 255, 255, 50);
425         fdrawline(xmin, ymax, xmax, ymin);
426         fdrawline(xmin+dx, ymax, xmax, ymin+dy);
427         fdrawline(xmin+2*dx, ymax, xmax, ymin+2*dy);
428         
429         glColor4ub(0, 0, 0, 50);
430         fdrawline(xmin, ymax+1, xmax, ymin+1);
431         fdrawline(xmin+dx, ymax+1, xmax, ymin+dy+1);
432         fdrawline(xmin+2*dx, ymax+1, xmax, ymin+2*dy+1);
433         glDisable(GL_BLEND);
434 }
435
436
437 static void ui_draw_aligned_panel_header(ARegion *ar, uiStyle *style, uiBlock *block, rcti *rect)
438 {
439         Panel *panel= block->panel;
440         Panel *pa;
441         rcti hrect;
442         float width;
443         int a, nr= 1, pnl_icons;
444         char *activename= panel->drawname[0]?panel->drawname:panel->panelname;
445         char *panelname;
446         
447         /* count */
448         for(pa= ar->panels.first; pa; pa=pa->next)
449                 if(pa->runtime_flag & PNL_ACTIVE)
450                         if(pa->paneltab==panel)
451                                 nr++;
452         
453         /* + 0.001f to avoid flirting with float inaccuracy */
454         if(panel->control & UI_PNL_CLOSE) pnl_icons=(panel->labelofs+2*PNL_ICON+5)/block->aspect + 0.001f;
455         else pnl_icons= (panel->labelofs+PNL_ICON+5)/block->aspect + 0.001f;
456         
457         if(nr==1) {
458                 
459                 /* active tab */
460                 /* draw text label */
461                 UI_ThemeColor(TH_TITLE);
462                 
463                 hrect= *rect;
464                 hrect.xmin= rect->xmin+pnl_icons;
465                 uiStyleFontDraw(&style->paneltitle, &hrect, activename);
466                 
467                 return;
468         }
469         
470         a= 0;
471         width= (rect->xmax-rect->xmin - 3 - pnl_icons - PNL_ICON)/nr;
472         for(pa= ar->panels.first; pa; pa=pa->next) {
473                 panelname= pa->drawname[0]?pa->drawname:pa->panelname;
474                 
475                 if((pa->runtime_flag & PNL_ACTIVE) && (pa==panel || pa->paneltab==panel)) {
476                         float col[3];
477                         
478                         UI_GetThemeColor3fv(TH_TITLE, col);
479
480                         /* active tab */
481                         if(pa==panel)
482                                 glColor4f(col[0], col[1], col[2], 1.0f);
483                         else
484                                 glColor4f(col[0], col[1], col[2], 0.5f);
485                         
486                         hrect= *rect;
487                         hrect.xmin= rect->xmin+pnl_icons + a*width;
488                         hrect.xmax= hrect.xmin + width;
489                         uiStyleFontDraw(&style->paneltitle, &hrect, panelname);
490                         
491                         a++;
492                 }
493         }
494 }
495
496 static void rectf_scale(rctf *rect, float scale)
497 {
498         float centx= 0.5f*(rect->xmin+rect->xmax);
499         float centy= 0.5f*(rect->ymin+rect->ymax);
500         float sizex= 0.5f*scale*(rect->xmax - rect->xmin);
501         float sizey= 0.5f*scale*(rect->ymax - rect->ymin);
502         
503         rect->xmin= centx - sizex;
504         rect->xmax= centx + sizex;
505         rect->ymin= centy - sizey;
506         rect->ymax= centy + sizey;
507 }
508
509 /* panel integrated in buttonswindow, tool/property lists etc */
510 void ui_draw_aligned_panel(ARegion *ar, uiStyle *style, uiBlock *block, rcti *rect)
511 {
512         Panel *panel= block->panel, *prev;
513         rcti headrect;
514         rctf itemrect;
515         int ofsx;
516         
517         if(panel->paneltab) return;
518
519         /* calculate header rect */
520         /* + 0.001f to prevent flicker due to float inaccuracy */
521         headrect= *rect;
522         headrect.ymin= headrect.ymax;
523         headrect.ymax= headrect.ymin + floor(PNL_HEADER/block->aspect + 0.001f);
524         
525         /* divider only when there's a previous panel */
526         prev= panel->prev;
527         while(prev) {
528                 if(prev->runtime_flag & PNL_ACTIVE) break;
529                 prev= prev->prev;
530         }
531         
532         if(panel->sortorder != 0) {
533                 float minx= rect->xmin+5.0f/block->aspect;
534                 float maxx= rect->xmax-5.0f/block->aspect;
535                 float y= headrect.ymax;
536                 
537                 glEnable(GL_BLEND);
538                 glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
539                 fdrawline(minx, y+1, maxx, y+1);
540                 glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
541                 fdrawline(minx, y, maxx, y);
542                 glDisable(GL_BLEND);
543         }
544         
545         /* title */
546         if(!(panel->flag & PNL_CLOSEDX)) {
547                 ui_draw_aligned_panel_header(ar, style, block, &headrect);
548                 
549                 /* itemrect smaller */  
550                 itemrect.xmax= headrect.xmax - 5.0f/block->aspect;
551                 itemrect.xmin= itemrect.xmax - (headrect.ymax-headrect.ymin);
552                 itemrect.ymin= headrect.ymin;
553                 itemrect.ymax= headrect.ymax;
554                 rectf_scale(&itemrect, 0.8f);
555                 ui_draw_panel_dragwidget(&itemrect);
556         }
557         
558         /* if the panel is minimized vertically:
559                 * (------)
560                 */
561         if(panel->flag & PNL_CLOSEDY) {
562                 
563                 
564                 /* if it's being overlapped by a panel being dragged */
565                 if(panel->flag & PNL_OVERLAP) {
566                         UI_ThemeColor(TH_TEXT_HI);
567                         uiRoundRect(rect->xmin, rect->ymax, rect->xmax, rect->ymax+PNL_HEADER, 8);
568                 }
569                 
570         }
571         else if(panel->flag & PNL_CLOSEDX) {
572                 
573         }
574         /* an open panel */
575         else {
576                 
577                 /* in some occasions, draw a border */
578                 if(panel->flag & PNL_SELECT) {
579                         if(panel->control & UI_PNL_SOLID) uiSetRoundBox(15);
580                         else uiSetRoundBox(3);
581                         
582                         UI_ThemeColorShade(TH_BACK, -120);
583                         uiRoundRect(rect->xmin, rect->ymin, rect->xmax, headrect.ymax+1, 8);
584                 }
585                 if(panel->flag & PNL_OVERLAP) {
586                         if(panel->control & UI_PNL_SOLID) uiSetRoundBox(15);
587                         else uiSetRoundBox(3);
588                         
589                         UI_ThemeColor(TH_TEXT_HI);
590                         uiRoundRect(rect->xmin, rect->ymin, rect->xmax, headrect.ymax+1, 8);
591                 }
592                 
593                 if(panel->control & UI_PNL_SCALE)
594                         ui_draw_panel_scalewidget(rect);
595         }
596         
597         /* draw optional close icon */
598         
599         ofsx= 6;
600         if(panel->control & UI_PNL_CLOSE) {
601                 
602                 UI_ThemeColor(TH_TEXT);
603                 ui_draw_x_icon(rect->xmin+2+ofsx, rect->ymax+2);
604                 ofsx= 22;
605         }
606         
607         /* draw collapse icon */
608         UI_ThemeColor(TH_TEXT);
609         
610         /* itemrect smaller */  
611         itemrect.xmin= headrect.xmin + 5.0f/block->aspect;
612         itemrect.xmax= itemrect.xmin + (headrect.ymax-headrect.ymin);
613         itemrect.ymin= headrect.ymin;
614         itemrect.ymax= headrect.ymax;
615         
616         rectf_scale(&itemrect, 0.5f);
617         
618         if(panel->flag & PNL_CLOSEDY)
619                 ui_draw_tria_rect(&itemrect, 'h');
620         else if(panel->flag & PNL_CLOSEDX)
621                 ui_draw_tria_rect(&itemrect, 'h');
622         else
623                 ui_draw_tria_rect(&itemrect, 'v');
624         
625         
626 }
627
628 /************************** panel alignment *************************/
629
630 /* this function is needed because uiBlock and Panel itself dont
631 change sizey or location when closed */
632 static int get_panel_real_ofsy(Panel *pa)
633 {
634         if(pa->flag & PNL_CLOSEDY) return pa->ofsy+pa->sizey;
635         else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDY)) return pa->ofsy+pa->sizey;
636         else if(pa->paneltab) return pa->paneltab->ofsy;
637         else return pa->ofsy;
638 }
639
640 static int get_panel_real_ofsx(Panel *pa)
641 {
642         if(pa->flag & PNL_CLOSEDX) return pa->ofsx+PNL_HEADER;
643         else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDX)) return pa->ofsx+PNL_HEADER;
644         else return pa->ofsx+pa->sizex;
645 }
646
647 typedef struct PanelSort {
648         Panel *pa, *orig;
649 } PanelSort;
650
651 /* note about sorting;
652    the sortorder has a lower value for new panels being added.
653    however, that only works to insert a single panel, when more new panels get
654    added the coordinates of existing panels and the previously stored to-be-insterted
655    panels do not match for sorting */
656
657 static int find_leftmost_panel(const void *a1, const void *a2)
658 {
659         const PanelSort *ps1=a1, *ps2=a2;
660         
661         if(ps1->pa->ofsx > ps2->pa->ofsx) return 1;
662         else if(ps1->pa->ofsx < ps2->pa->ofsx) return -1;
663         else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
664         else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
665
666         return 0;
667 }
668
669
670 static int find_highest_panel(const void *a1, const void *a2)
671 {
672         const PanelSort *ps1=a1, *ps2=a2;
673         
674         if(ps1->pa->ofsy+ps1->pa->sizey < ps2->pa->ofsy+ps2->pa->sizey) return 1;
675         else if(ps1->pa->ofsy+ps1->pa->sizey > ps2->pa->ofsy+ps2->pa->sizey) return -1;
676         else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
677         else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
678         
679         return 0;
680 }
681
682 static int compare_panel(const void *a1, const void *a2)
683 {
684         const PanelSort *ps1=a1, *ps2=a2;
685         
686         if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
687         else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
688         
689         return 0;
690 }
691
692 /* this doesnt draw */
693 /* returns 1 when it did something */
694 int uiAlignPanelStep(ScrArea *sa, ARegion *ar, float fac, int drag)
695 {
696         uiStyle *style= U.uistyles.first;
697         Panel *pa;
698         PanelSort *ps, *panelsort, *psnext;
699         int a, tot=0, done;
700         int align= panel_aligned(sa, ar);
701         
702         /* count active, not tabbed panels */
703         for(pa= ar->panels.first; pa; pa= pa->next)
704                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL)
705                         tot++;
706
707         if(tot==0) return 0;
708
709         /* extra; change close direction? */
710         for(pa= ar->panels.first; pa; pa= pa->next) {
711                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
712                         if((pa->flag & PNL_CLOSEDX) && (align==BUT_VERTICAL))
713                                 pa->flag ^= PNL_CLOSED;
714                         else if((pa->flag & PNL_CLOSEDY) && (align==BUT_HORIZONTAL))
715                                 pa->flag ^= PNL_CLOSED;
716                 }
717         }
718
719         /* sort panels */
720         panelsort= MEM_callocN(tot*sizeof(PanelSort), "panelsort");
721         
722         ps= panelsort;
723         for(pa= ar->panels.first; pa; pa= pa->next) {
724                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
725                         ps->pa= MEM_dupallocN(pa);
726                         ps->orig= pa;
727                         ps++;
728                 }
729         }
730         
731         if(drag) {
732                 /* while we are dragging, we sort on location and update sortorder */
733                 if(align==BUT_VERTICAL) 
734                         qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
735                 else
736                         qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
737
738                 for(ps=panelsort, a=0; a<tot; a++, ps++)
739                         ps->orig->sortorder= a;
740         }
741         else
742                 /* otherwise use sortorder */
743                 qsort(panelsort, tot, sizeof(PanelSort), compare_panel);
744         
745         /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
746         ps= panelsort;
747         ps->pa->ofsx= 0;
748         ps->pa->ofsy= -ps->pa->sizey-PNL_HEADER-style->panelouter;
749
750         for(a=0; a<tot-1; a++, ps++) {
751                 psnext= ps+1;
752         
753                 if(align==BUT_VERTICAL) {
754                         psnext->pa->ofsx= ps->pa->ofsx;
755                         psnext->pa->ofsy= get_panel_real_ofsy(ps->pa) - psnext->pa->sizey-PNL_HEADER-style->panelouter;
756                 }
757                 else {
758                         psnext->pa->ofsx= get_panel_real_ofsx(ps->pa);
759                         psnext->pa->ofsy= ps->pa->ofsy + ps->pa->sizey - psnext->pa->sizey;
760                 }
761         }
762         
763         /* we interpolate */
764         done= 0;
765         ps= panelsort;
766         for(a=0; a<tot; a++, ps++) {
767                 if((ps->pa->flag & PNL_SELECT)==0) {
768                         if((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) {
769                                 ps->orig->ofsx= floor(0.5 + fac*ps->pa->ofsx + (1.0-fac)*ps->orig->ofsx);
770                                 ps->orig->ofsy= floor(0.5 + fac*ps->pa->ofsy + (1.0-fac)*ps->orig->ofsy);
771                                 done= 1;
772                         }
773                 }
774         }
775
776         /* copy locations to tabs */
777         for(pa= ar->panels.first; pa; pa= pa->next)
778                 if(pa->paneltab && (pa->runtime_flag & PNL_ACTIVE))
779                         ui_panel_copy_offset(pa, pa->paneltab);
780
781         /* free panelsort array */
782         for(ps= panelsort, a=0; a<tot; a++, ps++)
783                 MEM_freeN(ps->pa);
784         MEM_freeN(panelsort);
785         
786         return done;
787 }
788
789
790 static void ui_do_animate(const bContext *C, Panel *panel)
791 {
792         uiHandlePanelData *data= panel->activedata;
793         ScrArea *sa= CTX_wm_area(C);
794         ARegion *ar= CTX_wm_region(C);
795         float fac;
796
797         fac= (PIL_check_seconds_timer()-data->starttime)/ANIMATION_TIME;
798         fac= sqrt(fac);
799         fac= MIN2(fac, 1.0f);
800
801         /* for max 1 second, interpolate positions */
802         if(uiAlignPanelStep(sa, ar, fac, 0))
803                 ED_region_tag_redraw(ar);
804         else
805                 fac= 1.0f;
806
807         if(fac >= 1.0f) {
808                 panel_activate_state(C, panel, PANEL_STATE_EXIT);
809                 return;
810         }
811 }
812
813 void uiBeginPanels(const bContext *C, ARegion *ar)
814 {
815         Panel *pa;
816   
817         /* set all panels as inactive, so that at the end we know
818          * which ones were used */
819         for(pa=ar->panels.first; pa; pa=pa->next) {
820                 if(pa->runtime_flag & PNL_ACTIVE)
821                         pa->runtime_flag= PNL_WAS_ACTIVE;
822                 else
823                         pa->runtime_flag= 0;
824         }
825 }
826
827 /* only draws blocks with panels */
828 void uiEndPanels(const bContext *C, ARegion *ar)
829 {
830         ScrArea *sa= CTX_wm_area(C);
831         uiBlock *block;
832         Panel *panot, *panew, *patest, *pa;
833         
834         /* offset contents */
835         for(block= ar->uiblocks.first; block; block= block->next)
836                 if(block->active && block->panel)
837                         ui_offset_panel_block(block);
838
839         /* consistancy; are panels not made, whilst they have tabs */
840         for(panot= ar->panels.first; panot; panot= panot->next) {
841                 if((panot->runtime_flag & PNL_ACTIVE)==0) { // not made
842
843                         for(panew= ar->panels.first; panew; panew= panew->next) {
844                                 if((panew->runtime_flag & PNL_ACTIVE)) {
845                                         if(panew->paneltab==panot) { // panew is tab in notmade pa
846                                                 break;
847                                         }
848                                 }
849                         }
850                         /* now panew can become the new parent, check all other tabs */
851                         if(panew) {
852                                 for(patest= ar->panels.first; patest; patest= patest->next) {
853                                         if(patest->paneltab == panot) {
854                                                 patest->paneltab= panew;
855                                         }
856                                 }
857                                 panot->paneltab= panew;
858                                 panew->paneltab= NULL;
859                                 ED_region_tag_redraw(ar); // the buttons panew were not made
860                         }
861                 }       
862         }
863
864         /* re-align, possibly with animation */
865         if(panels_re_align(sa, ar, &pa)) {
866                 if(pa)
867                         panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
868                 else
869                         uiAlignPanelStep(sa, ar, 1.0, 0);
870         }
871
872         /* draw panels, selected on top */
873         for(block= ar->uiblocks.first; block; block=block->next) {
874                 if(block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
875                         uiDrawBlock(C, block);
876                 }
877         }
878
879         for(block= ar->uiblocks.first; block; block=block->next) {
880                 if(block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
881                         uiDrawBlock(C, block);
882                 }
883         }
884 }
885
886 /* ------------ panel merging ---------------- */
887
888 static void check_panel_overlap(ARegion *ar, Panel *panel)
889 {
890         Panel *pa;
891
892         /* also called with panel==NULL for clear */
893         
894         for(pa=ar->panels.first; pa; pa=pa->next) {
895                 pa->flag &= ~PNL_OVERLAP;
896                 if(panel && (pa != panel)) {
897                         if(pa->paneltab==NULL && (pa->runtime_flag & PNL_ACTIVE)) {
898                                 float safex= 0.2, safey= 0.2;
899                                 
900                                 if(pa->flag & PNL_CLOSEDX) safex= 0.05;
901                                 else if(pa->flag & PNL_CLOSEDY) safey= 0.05;
902                                 else if(panel->flag & PNL_CLOSEDX) safex= 0.05;
903                                 else if(panel->flag & PNL_CLOSEDY) safey= 0.05;
904                                 
905                                 if(pa->ofsx > panel->ofsx- safex*panel->sizex)
906                                 if(pa->ofsx+pa->sizex < panel->ofsx+ (1.0+safex)*panel->sizex)
907                                 if(pa->ofsy > panel->ofsy- safey*panel->sizey)
908                                 if(pa->ofsy+pa->sizey < panel->ofsy+ (1.0+safey)*panel->sizey)
909                                         pa->flag |= PNL_OVERLAP;
910                         }
911                 }
912         }
913 }
914
915 static void test_add_new_tabs(ARegion *ar)
916 {
917         Panel *pa, *pasel=NULL, *palap=NULL;
918         /* search selected and overlapped panel */
919         
920         pa= ar->panels.first;
921         while(pa) {
922                 if(pa->runtime_flag & PNL_ACTIVE) {
923                         if(pa->flag & PNL_SELECT) pasel= pa;
924                         if(pa->flag & PNL_OVERLAP) palap= pa;
925                 }
926                 pa= pa->next;
927         }
928         
929         if(pasel && palap==NULL) {
930
931                 /* copy locations */
932                 pa= ar->panels.first;
933                 while(pa) {
934                         if(pa->paneltab==pasel) {
935                                 ui_panel_copy_offset(pa, pasel);
936                         }
937                         pa= pa->next;
938                 }
939         }
940         
941         if(pasel==NULL || palap==NULL) return;
942         
943         /* the overlapped panel becomes a tab */
944         palap->paneltab= pasel;
945         
946         /* the selected panel gets coords of overlapped one */
947         ui_panel_copy_offset(pasel, palap);
948
949         /* and its tabs */
950         pa= ar->panels.first;
951         while(pa) {
952                 if(pa->paneltab == pasel) {
953                         ui_panel_copy_offset(pa, palap);
954                 }
955                 pa= pa->next;
956         }
957         
958         /* but, the overlapped panel already can have tabs too! */
959         pa= ar->panels.first;
960         while(pa) {
961                 if(pa->paneltab == palap) {
962                         pa->paneltab = pasel;
963                 }
964                 pa= pa->next;
965         }
966 }
967
968 /************************ panel dragging ****************************/
969
970 static void ui_do_drag(const bContext *C, wmEvent *event, Panel *panel)
971 {
972         uiHandlePanelData *data= panel->activedata;
973         ScrArea *sa= CTX_wm_area(C);
974         ARegion *ar= CTX_wm_region(C);
975         short align= panel_aligned(sa, ar), dx=0, dy=0;
976         
977         /* first clip for window, no dragging outside */
978         if(!BLI_in_rcti(&ar->winrct, event->x, event->y))
979                 return;
980
981         dx= (event->x-data->startx) & ~(PNL_GRID-1);
982         dy= (event->y-data->starty) & ~(PNL_GRID-1);
983         
984         if(data->state == PANEL_STATE_DRAG_SCALE) {
985                 panel->sizex = MAX2(data->startsizex+dx, UI_PANEL_MINX);
986                 
987                 if(data->startsizey-dy < UI_PANEL_MINY)
988                         dy= -UI_PANEL_MINY+data->startsizey;
989
990                 panel->sizey= data->startsizey-dy;
991                 panel->ofsy= data->startofsy+dy;
992         }
993         else {
994                 /* reset the panel snapping, to allow dragging away from snapped edges */
995                 panel->snap = PNL_SNAP_NONE;
996                 
997                 panel->ofsx = data->startofsx+dx;
998                 panel->ofsy = data->startofsy+dy;
999                 check_panel_overlap(ar, panel);
1000                 
1001                 if(align) uiAlignPanelStep(sa, ar, 0.2, 1);
1002         }
1003
1004         ED_region_tag_redraw(ar);
1005 }
1006
1007 static void ui_do_untab(const bContext *C, wmEvent *event, Panel *panel)
1008 {
1009         uiHandlePanelData *data= panel->activedata;
1010         ARegion *ar= CTX_wm_region(C);
1011         Panel *pa, *panew= NULL;
1012         int nr;
1013
1014         /* wait until a threshold is passed to untab */
1015         if(abs(event->x-data->startx) + abs(event->y-data->starty) > 6) {
1016                 /* find new parent panel */
1017                 nr= 0;
1018                 for(pa= ar->panels.first; pa; pa=pa->next) {
1019                         if(pa->paneltab==panel) {
1020                                 panew= pa;
1021                                 nr++;
1022                         }
1023                 }
1024                 
1025                 /* make old tabs point to panew */
1026                 panew->paneltab= NULL;
1027                 
1028                 for(pa= ar->panels.first; pa; pa=pa->next)
1029                         if(pa->paneltab==panel)
1030                                 pa->paneltab= panew;
1031                 
1032                 panel_activate_state(C, panel, PANEL_STATE_DRAG);
1033         }
1034 }
1035
1036 /******************* region level panel interaction *****************/
1037
1038 static void panel_clicked_tabs(const bContext *C, ScrArea *sa, ARegion *ar, uiBlock *block,  int mousex)
1039 {
1040         Panel *pa, *tabsel=NULL, *panel= block->panel;
1041         int nr= 1, a, width, ofsx;
1042         
1043         ofsx= PNL_ICON;
1044         if(block->panel->type && (block->panel->control & UI_PNL_CLOSE)) ofsx+= PNL_ICON;
1045         
1046         /* count */
1047         for(pa= ar->panels.first; pa; pa=pa->next)
1048                 if(pa!=panel)
1049                         if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==panel)
1050                                 nr++;
1051
1052         if(nr==1) return;
1053         
1054         /* find clicked tab, mouse in panel coords */
1055         a= 0;
1056         width= (int)((float)(panel->sizex - ofsx-10)/nr);
1057         pa= ar->panels.first;
1058         while(pa) {
1059                 if(pa==panel || ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==panel)) {
1060                         if((mousex > ofsx+a*width) && (mousex < ofsx+(a+1)*width)) {
1061                                 tabsel= pa;
1062                                 break;
1063                         }
1064                         a++;
1065                 }
1066                 pa= pa->next;
1067         }
1068
1069         if(tabsel) {
1070                 if(tabsel == panel) {
1071                         panel_activate_state(C, panel, PANEL_STATE_WAIT_UNTAB);
1072                 }
1073                 else {
1074                         /* tabsel now becomes parent for all others */
1075                         panel->paneltab= tabsel;
1076                         tabsel->paneltab= NULL;
1077                         
1078                         pa= ar->panels.first;
1079                         while(pa) {
1080                                 if(pa->paneltab == panel) pa->paneltab = tabsel;
1081                                 pa= pa->next;
1082                         }
1083                         
1084                         /* copy locations to tabs */
1085                         for(pa= ar->panels.first; pa; pa= pa->next) {
1086                                 if(pa->paneltab && pa->runtime_flag & PNL_ACTIVE) {
1087                                         ui_panel_copy_offset(pa, pa->paneltab);
1088                                 }
1089                         }
1090                         
1091                         /* panels now differ size.. */
1092                         if(panel_aligned(sa, ar))
1093                                 panel_activate_state(C, tabsel, PANEL_STATE_ANIMATION);
1094
1095                         ED_region_tag_redraw(ar);
1096                 }
1097         }
1098         
1099 }
1100
1101 /* this function is supposed to call general window drawing too */
1102 /* also it supposes a block has panel, and isnt a menu */
1103 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my)
1104 {
1105         ScrArea *sa= CTX_wm_area(C);
1106         ARegion *ar= CTX_wm_region(C);
1107         Panel *pa;
1108         int align= panel_aligned(sa, ar), button= 0;
1109
1110         /* mouse coordinates in panel space! */
1111         
1112         /* XXX weak code, currently it assumes layout style for location of widgets */
1113         
1114         /* check open/collapsed button */
1115         if(block->panel->flag & PNL_CLOSEDX) {
1116                 if(my >= block->maxy) button= 1;
1117         }
1118         else if(block->panel->control & UI_PNL_CLOSE) {
1119                 if(mx <= block->minx+10+PNL_ICON-2) button= 2;
1120                 else if(mx <= block->minx+10+2*PNL_ICON+2) button= 1;
1121         }
1122         else if(mx <= block->minx+10+PNL_ICON+2) {
1123                 button= 1;
1124         }
1125         
1126         if(button) {
1127                 if(button==2) { // close
1128                         ED_region_tag_redraw(ar);
1129                 }
1130                 else {  // collapse
1131                         if(block->panel->flag & PNL_CLOSED) {
1132                                 block->panel->flag &= ~PNL_CLOSED;
1133                                 /* snap back up so full panel aligns with screen edge */
1134                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1135                                         block->panel->ofsy= 0;
1136                         }
1137                         else if(align==BUT_HORIZONTAL) {
1138                                 block->panel->flag |= PNL_CLOSEDX;
1139                         }
1140                         else {
1141                                 /* snap down to bottom screen edge*/
1142                                 block->panel->flag |= PNL_CLOSEDY;
1143                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1144                                         block->panel->ofsy= -block->panel->sizey;
1145                         }
1146                         
1147                         for(pa= ar->panels.first; pa; pa= pa->next) {
1148                                 if(pa->paneltab==block->panel) {
1149                                         if(block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1150                                         else pa->flag &= ~PNL_CLOSED;
1151                                 }
1152                         }
1153                 }
1154
1155                 if(align)
1156                         panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1157                 else
1158                         ED_region_tag_redraw(ar);
1159         }
1160         else if(block->panel->flag & PNL_CLOSED) {
1161                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1162         }
1163         /* check if clicked in tabbed area */
1164         else if(mx < block->maxx-PNL_ICON-3 && panel_has_tabs(ar, block->panel)) {
1165                 panel_clicked_tabs(C, sa, ar, block, mx);
1166         }
1167         else {
1168                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1169         }
1170 }
1171
1172 int ui_handler_panel_region(bContext *C, wmEvent *event)
1173 {
1174         ARegion *ar= CTX_wm_region(C);
1175         uiBlock *block;
1176         int retval, mx, my, inside_header= 0, inside_scale= 0, inside;
1177
1178         retval= WM_UI_HANDLER_CONTINUE;
1179
1180         /* buttons get priority */
1181         if(ui_button_is_active(ar))
1182                 return retval;
1183
1184         for(block=ar->uiblocks.last; block; block=block->prev) {
1185                 mx= event->x;
1186                 my= event->y;
1187                 ui_window_to_block(ar, block, &mx, &my);
1188
1189                 /* check if inside boundbox */
1190                 inside= 0;
1191
1192                 if(block->panel && block->panel->paneltab==NULL)
1193                         if(block->minx <= mx && block->maxx >= mx)
1194                                 if(block->miny <= my && block->maxy+PNL_HEADER >= my)
1195                                         inside= 1;
1196
1197                 if(inside) {
1198                         /* clicked at panel header? */
1199                         if(block->panel->flag & PNL_CLOSEDX) {
1200                                 if(block->minx <= mx && block->minx+PNL_HEADER >= mx) 
1201                                         inside_header= 1;
1202                         }
1203                         else if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my)) {
1204                                 inside_header= 1;
1205                         }
1206                         else if(block->panel->control & UI_PNL_SCALE) {
1207                                 if(block->maxx-PNL_HEADER <= mx)
1208                                         if(block->miny+PNL_HEADER >= my)
1209                                                 inside_scale= 1;
1210                         }
1211
1212                         if(event->val==KM_PRESS) {
1213                                 if(event->type == LEFTMOUSE) {
1214                                         if(inside_header) {
1215                                                 ui_handle_panel_header(C, block, mx, my);
1216                                                 break;
1217                                         }
1218                                         else if(inside_scale && !(block->panel->flag & PNL_CLOSED)) {
1219                                                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG_SCALE);
1220                                                 break;
1221                                         }
1222                                 }
1223                                 else if(event->type == ESCKEY) {
1224                                         /*XXX 2.50 if(block->handler) {
1225                                                 rem_blockhandler(sa, block->handler);
1226                                                 ED_region_tag_redraw(ar);
1227                                                 retval= WM_UI_HANDLER_BREAK;
1228                                         }*/
1229                                 }
1230                                 else if(event->type==PADPLUSKEY || event->type==PADMINUS) {
1231                                         int zoom=0;
1232                                 
1233                                         /* if panel is closed, only zoom if mouse is over the header */
1234                                         if (block->panel->flag & (PNL_CLOSEDX|PNL_CLOSEDY)) {
1235                                                 if (inside_header)
1236                                                         zoom=1;
1237                                         }
1238                                         else
1239                                                 zoom=1;
1240
1241 #if 0 // XXX make float panel exception?
1242                                         if(zoom) {
1243                                                 ScrArea *sa= CTX_wm_area(C);
1244                                                 SpaceLink *sl= sa->spacedata.first;
1245
1246                                                 if(sa->spacetype!=SPACE_BUTS) {
1247                                                         if(!(block->panel->control & UI_PNL_SCALE)) {
1248                                                                 if(event->type==PADPLUSKEY) sl->blockscale+= 0.1;
1249                                                                 else sl->blockscale-= 0.1;
1250                                                                 CLAMP(sl->blockscale, 0.6, 1.0);
1251
1252                                                                 ED_region_tag_redraw(ar);
1253                                                                 retval= WM_UI_HANDLER_BREAK;
1254                                                         }                                               
1255                                                 }
1256                                         }
1257 #endif
1258                                 }
1259                         }
1260                 }
1261         }
1262
1263         return retval;
1264 }
1265
1266 /**************** window level modal panel interaction **************/
1267
1268 static int ui_handler_panel(bContext *C, wmEvent *event, void *userdata)
1269 {
1270         Panel *panel= userdata;
1271         uiHandlePanelData *data= panel->activedata;
1272
1273         /* verify if we can stop */
1274         if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1275                 ScrArea *sa= CTX_wm_area(C);
1276                 ARegion *ar= CTX_wm_region(C);
1277                 int align= panel_aligned(sa, ar);
1278
1279                 if(align)
1280                         panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1281                 else
1282                         panel_activate_state(C, panel, PANEL_STATE_EXIT);
1283
1284                 return WM_UI_HANDLER_BREAK;
1285         }
1286         else if(event->type == MOUSEMOVE) {
1287                 if(data->state == PANEL_STATE_WAIT_UNTAB)
1288                         ui_do_untab(C, event, panel);
1289                 else if(data->state == PANEL_STATE_DRAG)
1290                         ui_do_drag(C, event, panel);
1291         }
1292         else if(event->type == TIMER && event->customdata == data->animtimer) {
1293                 if(data->state == PANEL_STATE_ANIMATION)
1294                         ui_do_animate(C, panel);
1295                 else if(data->state == PANEL_STATE_DRAG)
1296                         ui_do_drag(C, event, panel);
1297         }
1298
1299         data= panel->activedata;
1300
1301         if(data && data->state == PANEL_STATE_ANIMATION)
1302                 return WM_UI_HANDLER_CONTINUE;
1303         else
1304                 return WM_UI_HANDLER_BREAK;
1305 }
1306
1307 static void ui_handler_remove_panel(bContext *C, void *userdata)
1308 {
1309         Panel *pa= userdata;
1310
1311         panel_activate_state(C, pa, PANEL_STATE_EXIT);
1312 }
1313
1314 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
1315 {
1316         uiHandlePanelData *data= pa->activedata;
1317         wmWindow *win= CTX_wm_window(C);
1318         ARegion *ar= CTX_wm_region(C);
1319         
1320         if(data && data->state == state)
1321                 return;
1322
1323         if(state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1324                 if(data && data->state != PANEL_STATE_ANIMATION) {
1325                         test_add_new_tabs(ar);   // also copies locations of tabs in dragged panel
1326                         check_panel_overlap(ar, NULL);  // clears
1327                 }
1328
1329                 pa->flag &= ~PNL_SELECT;
1330         }
1331         else
1332                 pa->flag |= PNL_SELECT;
1333
1334         if(data && data->animtimer) {
1335                 WM_event_remove_window_timer(win, data->animtimer);
1336                 data->animtimer= NULL;
1337         }
1338
1339         if(state == PANEL_STATE_EXIT) {
1340                 MEM_freeN(data);
1341                 pa->activedata= NULL;
1342
1343                 WM_event_remove_ui_handler(&win->handlers, ui_handler_panel, ui_handler_remove_panel, pa);
1344         }
1345         else {
1346                 if(!data) {
1347                         data= MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1348                         pa->activedata= data;
1349
1350                         WM_event_add_ui_handler(C, &win->handlers, ui_handler_panel, ui_handler_remove_panel, pa);
1351                 }
1352
1353                 if(ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1354                         data->animtimer= WM_event_add_window_timer(win, TIMER, ANIMATION_INTERVAL);
1355
1356                 data->state= state;
1357                 data->startx= win->eventstate->x;
1358                 data->starty= win->eventstate->y;
1359                 data->startofsx= pa->ofsx;
1360                 data->startofsy= pa->ofsy;
1361                 data->startsizex= pa->sizex;
1362                 data->startsizey= pa->sizey;
1363                 data->starttime= PIL_check_seconds_timer();
1364         }
1365
1366         ED_region_tag_redraw(ar);
1367
1368         /* XXX exception handling, 3d window preview panel */
1369         /* if(block->drawextra==BIF_view3d_previewdraw)
1370                 BIF_view3d_previewrender_clear(curarea);*/
1371         
1372         /* XXX exception handling, 3d window preview panel */
1373         /* if(block->drawextra==BIF_view3d_previewdraw)
1374                 BIF_view3d_previewrender_signal(curarea, PR_DISPRECT);
1375         else if(strcmp(block->name, "image_panel_preview")==0)
1376                 image_preview_event(2); */
1377 }
1378