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