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