5cb06845bdf4a570398048f4f2bf9ddec49f6ac3
[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=(2*PNL_ICON+5)/block->aspect + 0.001f;
455         else pnl_icons= (PNL_ICON+5)/block->aspect + 0.001f;
456
457         pnl_icons += panel->labelofs;
458         
459         if(nr==1) {
460                 
461                 /* active tab */
462                 /* draw text label */
463                 UI_ThemeColor(TH_TITLE);
464                 
465                 hrect= *rect;
466                 hrect.xmin= rect->xmin+pnl_icons;
467                 uiStyleFontDraw(&style->paneltitle, &hrect, activename);
468                 
469                 return;
470         }
471         
472         a= 0;
473         width= (rect->xmax-rect->xmin - 3 - pnl_icons - PNL_ICON)/nr;
474         for(pa= ar->panels.first; pa; pa=pa->next) {
475                 panelname= pa->drawname[0]?pa->drawname:pa->panelname;
476                 
477                 if((pa->runtime_flag & PNL_ACTIVE) && (pa==panel || pa->paneltab==panel)) {
478                         float col[3];
479                         
480                         UI_GetThemeColor3fv(TH_TITLE, col);
481
482                         /* active tab */
483                         if(pa==panel)
484                                 glColor4f(col[0], col[1], col[2], 1.0f);
485                         else
486                                 glColor4f(col[0], col[1], col[2], 0.5f);
487                         
488                         hrect= *rect;
489                         hrect.xmin= rect->xmin+pnl_icons + a*width;
490                         hrect.xmax= hrect.xmin + width;
491                         uiStyleFontDraw(&style->paneltitle, &hrect, panelname);
492                         
493                         a++;
494                 }
495         }
496 }
497
498 static void rectf_scale(rctf *rect, float scale)
499 {
500         float centx= 0.5f*(rect->xmin+rect->xmax);
501         float centy= 0.5f*(rect->ymin+rect->ymax);
502         float sizex= 0.5f*scale*(rect->xmax - rect->xmin);
503         float sizey= 0.5f*scale*(rect->ymax - rect->ymin);
504         
505         rect->xmin= centx - sizex;
506         rect->xmax= centx + sizex;
507         rect->ymin= centy - sizey;
508         rect->ymax= centy + sizey;
509 }
510
511 /* panel integrated in buttonswindow, tool/property lists etc */
512 void ui_draw_aligned_panel(ARegion *ar, uiStyle *style, uiBlock *block, rcti *rect)
513 {
514         Panel *panel= block->panel, *prev;
515         rcti headrect;
516         rctf itemrect;
517         int ofsx;
518         
519         if(panel->paneltab) return;
520
521         /* calculate header rect */
522         /* + 0.001f to prevent flicker due to float inaccuracy */
523         headrect= *rect;
524         headrect.ymin= headrect.ymax;
525         headrect.ymax= headrect.ymin + floor(PNL_HEADER/block->aspect + 0.001f);
526         
527         /* divider only when there's a previous panel */
528         prev= panel->prev;
529         while(prev) {
530                 if(prev->runtime_flag & PNL_ACTIVE) break;
531                 prev= prev->prev;
532         }
533         
534         if(panel->sortorder != 0) {
535                 float minx= rect->xmin+5.0f/block->aspect;
536                 float maxx= rect->xmax-5.0f/block->aspect;
537                 float y= headrect.ymax;
538                 
539                 glEnable(GL_BLEND);
540                 glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
541                 fdrawline(minx, y+1, maxx, y+1);
542                 glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
543                 fdrawline(minx, y, maxx, y);
544                 glDisable(GL_BLEND);
545         }
546         
547         /* title */
548         if(!(panel->flag & PNL_CLOSEDX)) {
549                 ui_draw_aligned_panel_header(ar, style, block, &headrect);
550                 
551                 /* itemrect smaller */  
552                 itemrect.xmax= headrect.xmax - 5.0f/block->aspect;
553                 itemrect.xmin= itemrect.xmax - (headrect.ymax-headrect.ymin);
554                 itemrect.ymin= headrect.ymin;
555                 itemrect.ymax= headrect.ymax;
556                 rectf_scale(&itemrect, 0.8f);
557                 ui_draw_panel_dragwidget(&itemrect);
558         }
559         
560         /* if the panel is minimized vertically:
561                 * (------)
562                 */
563         if(panel->flag & PNL_CLOSEDY) {
564                 
565                 
566                 /* if it's being overlapped by a panel being dragged */
567                 if(panel->flag & PNL_OVERLAP) {
568                         UI_ThemeColor(TH_TEXT_HI);
569                         uiRoundRect(rect->xmin, rect->ymax, rect->xmax, rect->ymax+PNL_HEADER, 8);
570                 }
571                 
572         }
573         else if(panel->flag & PNL_CLOSEDX) {
574                 
575         }
576         /* an open panel */
577         else {
578                 
579                 /* in some occasions, draw a border */
580                 if(panel->flag & PNL_SELECT) {
581                         if(panel->control & UI_PNL_SOLID) uiSetRoundBox(15);
582                         else uiSetRoundBox(3);
583                         
584                         UI_ThemeColorShade(TH_BACK, -120);
585                         uiRoundRect(rect->xmin, rect->ymin, rect->xmax, headrect.ymax+1, 8);
586                 }
587                 if(panel->flag & PNL_OVERLAP) {
588                         if(panel->control & UI_PNL_SOLID) uiSetRoundBox(15);
589                         else uiSetRoundBox(3);
590                         
591                         UI_ThemeColor(TH_TEXT_HI);
592                         uiRoundRect(rect->xmin, rect->ymin, rect->xmax, headrect.ymax+1, 8);
593                 }
594                 
595                 if(panel->control & UI_PNL_SCALE)
596                         ui_draw_panel_scalewidget(rect);
597         }
598         
599         /* draw optional close icon */
600         
601         ofsx= 6;
602         if(panel->control & UI_PNL_CLOSE) {
603                 
604                 UI_ThemeColor(TH_TEXT);
605                 ui_draw_x_icon(rect->xmin+2+ofsx, rect->ymax+2);
606                 ofsx= 22;
607         }
608         
609         /* draw collapse icon */
610         UI_ThemeColor(TH_TEXT);
611         
612         /* itemrect smaller */  
613         itemrect.xmin= headrect.xmin + 5.0f/block->aspect;
614         itemrect.xmax= itemrect.xmin + (headrect.ymax-headrect.ymin);
615         itemrect.ymin= headrect.ymin;
616         itemrect.ymax= headrect.ymax;
617         
618         rectf_scale(&itemrect, 0.5f);
619         
620         if(panel->flag & PNL_CLOSEDY)
621                 ui_draw_tria_rect(&itemrect, 'h');
622         else if(panel->flag & PNL_CLOSEDX)
623                 ui_draw_tria_rect(&itemrect, 'h');
624         else
625                 ui_draw_tria_rect(&itemrect, 'v');
626         
627         
628 }
629
630 /************************** panel alignment *************************/
631
632 /* this function is needed because uiBlock and Panel itself dont
633 change sizey or location when closed */
634 static int get_panel_real_ofsy(Panel *pa)
635 {
636         if(pa->flag & PNL_CLOSEDY) return pa->ofsy+pa->sizey;
637         else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDY)) return pa->ofsy+pa->sizey;
638         else if(pa->paneltab) return pa->paneltab->ofsy;
639         else return pa->ofsy;
640 }
641
642 static int get_panel_real_ofsx(Panel *pa)
643 {
644         if(pa->flag & PNL_CLOSEDX) return pa->ofsx+PNL_HEADER;
645         else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDX)) return pa->ofsx+PNL_HEADER;
646         else return pa->ofsx+pa->sizex;
647 }
648
649 typedef struct PanelSort {
650         Panel *pa, *orig;
651 } PanelSort;
652
653 /* note about sorting;
654    the sortorder has a lower value for new panels being added.
655    however, that only works to insert a single panel, when more new panels get
656    added the coordinates of existing panels and the previously stored to-be-insterted
657    panels do not match for sorting */
658
659 static int find_leftmost_panel(const void *a1, const void *a2)
660 {
661         const PanelSort *ps1=a1, *ps2=a2;
662         
663         if(ps1->pa->ofsx > ps2->pa->ofsx) return 1;
664         else if(ps1->pa->ofsx < ps2->pa->ofsx) return -1;
665         else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
666         else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
667
668         return 0;
669 }
670
671
672 static int find_highest_panel(const void *a1, const void *a2)
673 {
674         const PanelSort *ps1=a1, *ps2=a2;
675         
676         if(ps1->pa->ofsy+ps1->pa->sizey < ps2->pa->ofsy+ps2->pa->sizey) return 1;
677         else if(ps1->pa->ofsy+ps1->pa->sizey > ps2->pa->ofsy+ps2->pa->sizey) return -1;
678         else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
679         else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
680         
681         return 0;
682 }
683
684 static int compare_panel(const void *a1, const void *a2)
685 {
686         const PanelSort *ps1=a1, *ps2=a2;
687         
688         if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
689         else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
690         
691         return 0;
692 }
693
694 /* this doesnt draw */
695 /* returns 1 when it did something */
696 int uiAlignPanelStep(ScrArea *sa, ARegion *ar, float fac, int drag)
697 {
698         uiStyle *style= U.uistyles.first;
699         Panel *pa;
700         PanelSort *ps, *panelsort, *psnext;
701         int a, tot=0, done;
702         int align= panel_aligned(sa, ar);
703         
704         /* count active, not tabbed panels */
705         for(pa= ar->panels.first; pa; pa= pa->next)
706                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL)
707                         tot++;
708
709         if(tot==0) return 0;
710
711         /* extra; change close direction? */
712         for(pa= ar->panels.first; pa; pa= pa->next) {
713                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
714                         if((pa->flag & PNL_CLOSEDX) && (align==BUT_VERTICAL))
715                                 pa->flag ^= PNL_CLOSED;
716                         else if((pa->flag & PNL_CLOSEDY) && (align==BUT_HORIZONTAL))
717                                 pa->flag ^= PNL_CLOSED;
718                 }
719         }
720
721         /* sort panels */
722         panelsort= MEM_callocN(tot*sizeof(PanelSort), "panelsort");
723         
724         ps= panelsort;
725         for(pa= ar->panels.first; pa; pa= pa->next) {
726                 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
727                         ps->pa= MEM_dupallocN(pa);
728                         ps->orig= pa;
729                         ps++;
730                 }
731         }
732         
733         if(drag) {
734                 /* while we are dragging, we sort on location and update sortorder */
735                 if(align==BUT_VERTICAL) 
736                         qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
737                 else
738                         qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
739
740                 for(ps=panelsort, a=0; a<tot; a++, ps++)
741                         ps->orig->sortorder= a;
742         }
743         else
744                 /* otherwise use sortorder */
745                 qsort(panelsort, tot, sizeof(PanelSort), compare_panel);
746         
747         /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
748         ps= panelsort;
749         ps->pa->ofsx= 0;
750         ps->pa->ofsy= -ps->pa->sizey-PNL_HEADER-style->panelouter;
751
752         for(a=0; a<tot-1; a++, ps++) {
753                 psnext= ps+1;
754         
755                 if(align==BUT_VERTICAL) {
756                         psnext->pa->ofsx= ps->pa->ofsx;
757                         psnext->pa->ofsy= get_panel_real_ofsy(ps->pa) - psnext->pa->sizey-PNL_HEADER-style->panelouter;
758                 }
759                 else {
760                         psnext->pa->ofsx= get_panel_real_ofsx(ps->pa);
761                         psnext->pa->ofsy= ps->pa->ofsy + ps->pa->sizey - psnext->pa->sizey;
762                 }
763         }
764         
765         /* we interpolate */
766         done= 0;
767         ps= panelsort;
768         for(a=0; a<tot; a++, ps++) {
769                 if((ps->pa->flag & PNL_SELECT)==0) {
770                         if((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) {
771                                 ps->orig->ofsx= floor(0.5 + fac*ps->pa->ofsx + (1.0-fac)*ps->orig->ofsx);
772                                 ps->orig->ofsy= floor(0.5 + fac*ps->pa->ofsy + (1.0-fac)*ps->orig->ofsy);
773                                 done= 1;
774                         }
775                 }
776         }
777
778         /* copy locations to tabs */
779         for(pa= ar->panels.first; pa; pa= pa->next)
780                 if(pa->paneltab && (pa->runtime_flag & PNL_ACTIVE))
781                         ui_panel_copy_offset(pa, pa->paneltab);
782
783         /* free panelsort array */
784         for(ps= panelsort, a=0; a<tot; a++, ps++)
785                 MEM_freeN(ps->pa);
786         MEM_freeN(panelsort);
787         
788         return done;
789 }
790
791
792 static void ui_do_animate(const bContext *C, Panel *panel)
793 {
794         uiHandlePanelData *data= panel->activedata;
795         ScrArea *sa= CTX_wm_area(C);
796         ARegion *ar= CTX_wm_region(C);
797         float fac;
798
799         fac= (PIL_check_seconds_timer()-data->starttime)/ANIMATION_TIME;
800         fac= sqrt(fac);
801         fac= MIN2(fac, 1.0f);
802
803         /* for max 1 second, interpolate positions */
804         if(uiAlignPanelStep(sa, ar, fac, 0))
805                 ED_region_tag_redraw(ar);
806         else
807                 fac= 1.0f;
808
809         if(fac >= 1.0f) {
810                 panel_activate_state(C, panel, PANEL_STATE_EXIT);
811                 return;
812         }
813 }
814
815 void uiBeginPanels(const bContext *C, ARegion *ar)
816 {
817         Panel *pa;
818   
819         /* set all panels as inactive, so that at the end we know
820          * which ones were used */
821         for(pa=ar->panels.first; pa; pa=pa->next) {
822                 if(pa->runtime_flag & PNL_ACTIVE)
823                         pa->runtime_flag= PNL_WAS_ACTIVE;
824                 else
825                         pa->runtime_flag= 0;
826         }
827 }
828
829 /* only draws blocks with panels */
830 void uiEndPanels(const bContext *C, ARegion *ar)
831 {
832         ScrArea *sa= CTX_wm_area(C);
833         uiBlock *block;
834         Panel *panot, *panew, *patest, *pa;
835         
836         /* offset contents */
837         for(block= ar->uiblocks.first; block; block= block->next)
838                 if(block->active && block->panel)
839                         ui_offset_panel_block(block);
840
841         /* consistancy; are panels not made, whilst they have tabs */
842         for(panot= ar->panels.first; panot; panot= panot->next) {
843                 if((panot->runtime_flag & PNL_ACTIVE)==0) { // not made
844
845                         for(panew= ar->panels.first; panew; panew= panew->next) {
846                                 if((panew->runtime_flag & PNL_ACTIVE)) {
847                                         if(panew->paneltab==panot) { // panew is tab in notmade pa
848                                                 break;
849                                         }
850                                 }
851                         }
852                         /* now panew can become the new parent, check all other tabs */
853                         if(panew) {
854                                 for(patest= ar->panels.first; patest; patest= patest->next) {
855                                         if(patest->paneltab == panot) {
856                                                 patest->paneltab= panew;
857                                         }
858                                 }
859                                 panot->paneltab= panew;
860                                 panew->paneltab= NULL;
861                                 ED_region_tag_redraw(ar); // the buttons panew were not made
862                         }
863                 }       
864         }
865
866         /* re-align, possibly with animation */
867         if(panels_re_align(sa, ar, &pa)) {
868                 if(pa)
869                         panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
870                 else
871                         uiAlignPanelStep(sa, ar, 1.0, 0);
872         }
873
874         /* draw panels, selected on top */
875         for(block= ar->uiblocks.first; block; block=block->next) {
876                 if(block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
877                         uiDrawBlock(C, block);
878                 }
879         }
880
881         for(block= ar->uiblocks.first; block; block=block->next) {
882                 if(block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
883                         uiDrawBlock(C, block);
884                 }
885         }
886 }
887
888 /* ------------ panel merging ---------------- */
889
890 static void check_panel_overlap(ARegion *ar, Panel *panel)
891 {
892         Panel *pa;
893
894         /* also called with panel==NULL for clear */
895         
896         for(pa=ar->panels.first; pa; pa=pa->next) {
897                 pa->flag &= ~PNL_OVERLAP;
898                 if(panel && (pa != panel)) {
899                         if(pa->paneltab==NULL && (pa->runtime_flag & PNL_ACTIVE)) {
900                                 float safex= 0.2, safey= 0.2;
901                                 
902                                 if(pa->flag & PNL_CLOSEDX) safex= 0.05;
903                                 else if(pa->flag & PNL_CLOSEDY) safey= 0.05;
904                                 else if(panel->flag & PNL_CLOSEDX) safex= 0.05;
905                                 else if(panel->flag & PNL_CLOSEDY) safey= 0.05;
906                                 
907                                 if(pa->ofsx > panel->ofsx- safex*panel->sizex)
908                                 if(pa->ofsx+pa->sizex < panel->ofsx+ (1.0+safex)*panel->sizex)
909                                 if(pa->ofsy > panel->ofsy- safey*panel->sizey)
910                                 if(pa->ofsy+pa->sizey < panel->ofsy+ (1.0+safey)*panel->sizey)
911                                         pa->flag |= PNL_OVERLAP;
912                         }
913                 }
914         }
915 }
916
917 static void test_add_new_tabs(ARegion *ar)
918 {
919         Panel *pa, *pasel=NULL, *palap=NULL;
920         /* search selected and overlapped panel */
921         
922         pa= ar->panels.first;
923         while(pa) {
924                 if(pa->runtime_flag & PNL_ACTIVE) {
925                         if(pa->flag & PNL_SELECT) pasel= pa;
926                         if(pa->flag & PNL_OVERLAP) palap= pa;
927                 }
928                 pa= pa->next;
929         }
930         
931         if(pasel && palap==NULL) {
932
933                 /* copy locations */
934                 pa= ar->panels.first;
935                 while(pa) {
936                         if(pa->paneltab==pasel) {
937                                 ui_panel_copy_offset(pa, pasel);
938                         }
939                         pa= pa->next;
940                 }
941         }
942         
943         if(pasel==NULL || palap==NULL) return;
944         
945         /* the overlapped panel becomes a tab */
946         palap->paneltab= pasel;
947         
948         /* the selected panel gets coords of overlapped one */
949         ui_panel_copy_offset(pasel, palap);
950
951         /* and its tabs */
952         pa= ar->panels.first;
953         while(pa) {
954                 if(pa->paneltab == pasel) {
955                         ui_panel_copy_offset(pa, palap);
956                 }
957                 pa= pa->next;
958         }
959         
960         /* but, the overlapped panel already can have tabs too! */
961         pa= ar->panels.first;
962         while(pa) {
963                 if(pa->paneltab == palap) {
964                         pa->paneltab = pasel;
965                 }
966                 pa= pa->next;
967         }
968 }
969
970 /************************ panel dragging ****************************/
971
972 static void ui_do_drag(const bContext *C, wmEvent *event, Panel *panel)
973 {
974         uiHandlePanelData *data= panel->activedata;
975         ScrArea *sa= CTX_wm_area(C);
976         ARegion *ar= CTX_wm_region(C);
977         short align= panel_aligned(sa, ar), dx=0, dy=0;
978         
979         /* first clip for window, no dragging outside */
980         if(!BLI_in_rcti(&ar->winrct, event->x, event->y))
981                 return;
982
983         dx= (event->x-data->startx) & ~(PNL_GRID-1);
984         dy= (event->y-data->starty) & ~(PNL_GRID-1);
985         
986         if(data->state == PANEL_STATE_DRAG_SCALE) {
987                 panel->sizex = MAX2(data->startsizex+dx, UI_PANEL_MINX);
988                 
989                 if(data->startsizey-dy < UI_PANEL_MINY)
990                         dy= -UI_PANEL_MINY+data->startsizey;
991
992                 panel->sizey= data->startsizey-dy;
993                 panel->ofsy= data->startofsy+dy;
994         }
995         else {
996                 /* reset the panel snapping, to allow dragging away from snapped edges */
997                 panel->snap = PNL_SNAP_NONE;
998                 
999                 panel->ofsx = data->startofsx+dx;
1000                 panel->ofsy = data->startofsy+dy;
1001                 check_panel_overlap(ar, panel);
1002                 
1003                 if(align) uiAlignPanelStep(sa, ar, 0.2, 1);
1004         }
1005
1006         ED_region_tag_redraw(ar);
1007 }
1008
1009 static void ui_do_untab(const bContext *C, wmEvent *event, Panel *panel)
1010 {
1011         uiHandlePanelData *data= panel->activedata;
1012         ARegion *ar= CTX_wm_region(C);
1013         Panel *pa, *panew= NULL;
1014         int nr;
1015
1016         /* wait until a threshold is passed to untab */
1017         if(abs(event->x-data->startx) + abs(event->y-data->starty) > 6) {
1018                 /* find new parent panel */
1019                 nr= 0;
1020                 for(pa= ar->panels.first; pa; pa=pa->next) {
1021                         if(pa->paneltab==panel) {
1022                                 panew= pa;
1023                                 nr++;
1024                         }
1025                 }
1026                 
1027                 /* make old tabs point to panew */
1028                 panew->paneltab= NULL;
1029                 
1030                 for(pa= ar->panels.first; pa; pa=pa->next)
1031                         if(pa->paneltab==panel)
1032                                 pa->paneltab= panew;
1033                 
1034                 panel_activate_state(C, panel, PANEL_STATE_DRAG);
1035         }
1036 }
1037
1038 /******************* region level panel interaction *****************/
1039
1040 static void panel_clicked_tabs(const bContext *C, ScrArea *sa, ARegion *ar, uiBlock *block,  int mousex)
1041 {
1042         Panel *pa, *tabsel=NULL, *panel= block->panel;
1043         int nr= 1, a, width, ofsx;
1044         
1045         ofsx= PNL_ICON;
1046         if(block->panel->type && (block->panel->control & UI_PNL_CLOSE)) ofsx+= PNL_ICON;
1047         
1048         /* count */
1049         for(pa= ar->panels.first; pa; pa=pa->next)
1050                 if(pa!=panel)
1051                         if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==panel)
1052                                 nr++;
1053
1054         if(nr==1) return;
1055         
1056         /* find clicked tab, mouse in panel coords */
1057         a= 0;
1058         width= (int)((float)(panel->sizex - ofsx-10)/nr);
1059         pa= ar->panels.first;
1060         while(pa) {
1061                 if(pa==panel || ((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==panel)) {
1062                         if((mousex > ofsx+a*width) && (mousex < ofsx+(a+1)*width)) {
1063                                 tabsel= pa;
1064                                 break;
1065                         }
1066                         a++;
1067                 }
1068                 pa= pa->next;
1069         }
1070
1071         if(tabsel) {
1072                 if(tabsel == panel) {
1073                         panel_activate_state(C, panel, PANEL_STATE_WAIT_UNTAB);
1074                 }
1075                 else {
1076                         /* tabsel now becomes parent for all others */
1077                         panel->paneltab= tabsel;
1078                         tabsel->paneltab= NULL;
1079                         
1080                         pa= ar->panels.first;
1081                         while(pa) {
1082                                 if(pa->paneltab == panel) pa->paneltab = tabsel;
1083                                 pa= pa->next;
1084                         }
1085                         
1086                         /* copy locations to tabs */
1087                         for(pa= ar->panels.first; pa; pa= pa->next) {
1088                                 if(pa->paneltab && pa->runtime_flag & PNL_ACTIVE) {
1089                                         ui_panel_copy_offset(pa, pa->paneltab);
1090                                 }
1091                         }
1092                         
1093                         /* panels now differ size.. */
1094                         if(panel_aligned(sa, ar))
1095                                 panel_activate_state(C, tabsel, PANEL_STATE_ANIMATION);
1096
1097                         ED_region_tag_redraw(ar);
1098                 }
1099         }
1100         
1101 }
1102
1103 /* this function is supposed to call general window drawing too */
1104 /* also it supposes a block has panel, and isnt a menu */
1105 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my)
1106 {
1107         ScrArea *sa= CTX_wm_area(C);
1108         ARegion *ar= CTX_wm_region(C);
1109         Panel *pa;
1110         int align= panel_aligned(sa, ar), button= 0;
1111
1112         /* mouse coordinates in panel space! */
1113         
1114         /* XXX weak code, currently it assumes layout style for location of widgets */
1115         
1116         /* check open/collapsed button */
1117         if(block->panel->flag & PNL_CLOSEDX) {
1118                 if(my >= block->maxy) button= 1;
1119         }
1120         else if(block->panel->control & UI_PNL_CLOSE) {
1121                 if(mx <= block->minx+10+PNL_ICON-2) button= 2;
1122                 else if(mx <= block->minx+10+2*PNL_ICON+2) button= 1;
1123         }
1124         else if(mx <= block->minx+10+PNL_ICON+2) {
1125                 button= 1;
1126         }
1127         
1128         if(button) {
1129                 if(button==2) { // close
1130                         ED_region_tag_redraw(ar);
1131                 }
1132                 else {  // collapse
1133                         if(block->panel->flag & PNL_CLOSED) {
1134                                 block->panel->flag &= ~PNL_CLOSED;
1135                                 /* snap back up so full panel aligns with screen edge */
1136                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1137                                         block->panel->ofsy= 0;
1138                         }
1139                         else if(align==BUT_HORIZONTAL) {
1140                                 block->panel->flag |= PNL_CLOSEDX;
1141                         }
1142                         else {
1143                                 /* snap down to bottom screen edge*/
1144                                 block->panel->flag |= PNL_CLOSEDY;
1145                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1146                                         block->panel->ofsy= -block->panel->sizey;
1147                         }
1148                         
1149                         for(pa= ar->panels.first; pa; pa= pa->next) {
1150                                 if(pa->paneltab==block->panel) {
1151                                         if(block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1152                                         else pa->flag &= ~PNL_CLOSED;
1153                                 }
1154                         }
1155                 }
1156
1157                 if(align)
1158                         panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1159                 else
1160                         ED_region_tag_redraw(ar);
1161         }
1162         else if(block->panel->flag & PNL_CLOSED) {
1163                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1164         }
1165         /* check if clicked in tabbed area */
1166         else if(mx < block->maxx-PNL_ICON-3 && panel_has_tabs(ar, block->panel)) {
1167                 panel_clicked_tabs(C, sa, ar, block, mx);
1168         }
1169         else {
1170                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1171         }
1172 }
1173
1174 int ui_handler_panel_region(bContext *C, wmEvent *event)
1175 {
1176         ARegion *ar= CTX_wm_region(C);
1177         uiBlock *block;
1178         int retval, mx, my, inside_header= 0, inside_scale= 0, inside;
1179
1180         retval= WM_UI_HANDLER_CONTINUE;
1181
1182         /* buttons get priority */
1183         if(ui_button_is_active(ar))
1184                 return retval;
1185
1186         for(block=ar->uiblocks.last; block; block=block->prev) {
1187                 mx= event->x;
1188                 my= event->y;
1189                 ui_window_to_block(ar, block, &mx, &my);
1190
1191                 /* check if inside boundbox */
1192                 inside= 0;
1193
1194                 if(block->panel && block->panel->paneltab==NULL)
1195                         if(block->minx <= mx && block->maxx >= mx)
1196                                 if(block->miny <= my && block->maxy+PNL_HEADER >= my)
1197                                         inside= 1;
1198
1199                 if(inside) {
1200                         /* clicked at panel header? */
1201                         if(block->panel->flag & PNL_CLOSEDX) {
1202                                 if(block->minx <= mx && block->minx+PNL_HEADER >= mx) 
1203                                         inside_header= 1;
1204                         }
1205                         else if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my)) {
1206                                 inside_header= 1;
1207                         }
1208                         else if(block->panel->control & UI_PNL_SCALE) {
1209                                 if(block->maxx-PNL_HEADER <= mx)
1210                                         if(block->miny+PNL_HEADER >= my)
1211                                                 inside_scale= 1;
1212                         }
1213
1214                         if(event->val==KM_PRESS) {
1215                                 if(event->type == LEFTMOUSE) {
1216                                         if(inside_header) {
1217                                                 ui_handle_panel_header(C, block, mx, my);
1218                                                 break;
1219                                         }
1220                                         else if(inside_scale && !(block->panel->flag & PNL_CLOSED)) {
1221                                                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG_SCALE);
1222                                                 break;
1223                                         }
1224                                 }
1225                                 else if(event->type == ESCKEY) {
1226                                         /*XXX 2.50 if(block->handler) {
1227                                                 rem_blockhandler(sa, block->handler);
1228                                                 ED_region_tag_redraw(ar);
1229                                                 retval= WM_UI_HANDLER_BREAK;
1230                                         }*/
1231                                 }
1232                                 else if(event->type==PADPLUSKEY || event->type==PADMINUS) {
1233                                         int zoom=0;
1234                                 
1235                                         /* if panel is closed, only zoom if mouse is over the header */
1236                                         if (block->panel->flag & (PNL_CLOSEDX|PNL_CLOSEDY)) {
1237                                                 if (inside_header)
1238                                                         zoom=1;
1239                                         }
1240                                         else
1241                                                 zoom=1;
1242
1243 #if 0 // XXX make float panel exception?
1244                                         if(zoom) {
1245                                                 ScrArea *sa= CTX_wm_area(C);
1246                                                 SpaceLink *sl= sa->spacedata.first;
1247
1248                                                 if(sa->spacetype!=SPACE_BUTS) {
1249                                                         if(!(block->panel->control & UI_PNL_SCALE)) {
1250                                                                 if(event->type==PADPLUSKEY) sl->blockscale+= 0.1;
1251                                                                 else sl->blockscale-= 0.1;
1252                                                                 CLAMP(sl->blockscale, 0.6, 1.0);
1253
1254                                                                 ED_region_tag_redraw(ar);
1255                                                                 retval= WM_UI_HANDLER_BREAK;
1256                                                         }                                               
1257                                                 }
1258                                         }
1259 #endif
1260                                 }
1261                         }
1262                 }
1263         }
1264
1265         return retval;
1266 }
1267
1268 /**************** window level modal panel interaction **************/
1269
1270 static int ui_handler_panel(bContext *C, wmEvent *event, void *userdata)
1271 {
1272         Panel *panel= userdata;
1273         uiHandlePanelData *data= panel->activedata;
1274
1275         /* verify if we can stop */
1276         if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1277                 ScrArea *sa= CTX_wm_area(C);
1278                 ARegion *ar= CTX_wm_region(C);
1279                 int align= panel_aligned(sa, ar);
1280
1281                 if(align)
1282                         panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1283                 else
1284                         panel_activate_state(C, panel, PANEL_STATE_EXIT);
1285
1286                 return WM_UI_HANDLER_BREAK;
1287         }
1288         else if(event->type == MOUSEMOVE) {
1289                 if(data->state == PANEL_STATE_WAIT_UNTAB)
1290                         ui_do_untab(C, event, panel);
1291                 else if(data->state == PANEL_STATE_DRAG)
1292                         ui_do_drag(C, event, panel);
1293         }
1294         else if(event->type == TIMER && event->customdata == data->animtimer) {
1295                 if(data->state == PANEL_STATE_ANIMATION)
1296                         ui_do_animate(C, panel);
1297                 else if(data->state == PANEL_STATE_DRAG)
1298                         ui_do_drag(C, event, panel);
1299         }
1300
1301         data= panel->activedata;
1302
1303         if(data && data->state == PANEL_STATE_ANIMATION)
1304                 return WM_UI_HANDLER_CONTINUE;
1305         else
1306                 return WM_UI_HANDLER_BREAK;
1307 }
1308
1309 static void ui_handler_remove_panel(bContext *C, void *userdata)
1310 {
1311         Panel *pa= userdata;
1312
1313         panel_activate_state(C, pa, PANEL_STATE_EXIT);
1314 }
1315
1316 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
1317 {
1318         uiHandlePanelData *data= pa->activedata;
1319         wmWindow *win= CTX_wm_window(C);
1320         ARegion *ar= CTX_wm_region(C);
1321         
1322         if(data && data->state == state)
1323                 return;
1324
1325         if(state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1326                 if(data && data->state != PANEL_STATE_ANIMATION) {
1327                         test_add_new_tabs(ar);   // also copies locations of tabs in dragged panel
1328                         check_panel_overlap(ar, NULL);  // clears
1329                 }
1330
1331                 pa->flag &= ~PNL_SELECT;
1332         }
1333         else
1334                 pa->flag |= PNL_SELECT;
1335
1336         if(data && data->animtimer) {
1337                 WM_event_remove_window_timer(win, data->animtimer);
1338                 data->animtimer= NULL;
1339         }
1340
1341         if(state == PANEL_STATE_EXIT) {
1342                 MEM_freeN(data);
1343                 pa->activedata= NULL;
1344
1345                 WM_event_remove_ui_handler(&win->handlers, ui_handler_panel, ui_handler_remove_panel, pa);
1346         }
1347         else {
1348                 if(!data) {
1349                         data= MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1350                         pa->activedata= data;
1351
1352                         WM_event_add_ui_handler(C, &win->handlers, ui_handler_panel, ui_handler_remove_panel, pa);
1353                 }
1354
1355                 if(ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1356                         data->animtimer= WM_event_add_window_timer(win, TIMER, ANIMATION_INTERVAL);
1357
1358                 data->state= state;
1359                 data->startx= win->eventstate->x;
1360                 data->starty= win->eventstate->y;
1361                 data->startofsx= pa->ofsx;
1362                 data->startofsy= pa->ofsy;
1363                 data->startsizex= pa->sizex;
1364                 data->startsizey= pa->sizey;
1365                 data->starttime= PIL_check_seconds_timer();
1366         }
1367
1368         ED_region_tag_redraw(ar);
1369
1370         /* XXX exception handling, 3d window preview panel */
1371         /* if(block->drawextra==BIF_view3d_previewdraw)
1372                 BIF_view3d_previewrender_clear(curarea);*/
1373         
1374         /* XXX exception handling, 3d window preview panel */
1375         /* if(block->drawextra==BIF_view3d_previewdraw)
1376                 BIF_view3d_previewrender_signal(curarea, PR_DISPRECT);
1377         else if(strcmp(block->name, "image_panel_preview")==0)
1378                 image_preview_event(2); */
1379 }
1380