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