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