Merging r42648 through r42722 from trunk into soc-2011-tomato
[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         MEM_freeN(panelsort);
778         
779         return done;
780 }
781
782 static void ui_panels_size(ScrArea *sa, ARegion *ar, int *x, int *y)
783 {
784         Panel *pa;
785         int align= panel_aligned(sa, ar);
786         int sizex = UI_PANEL_WIDTH;
787         int sizey = UI_PANEL_WIDTH;
788
789         /* compute size taken up by panels, for setting in view2d */
790         for(pa= ar->panels.first; pa; pa= pa->next) {
791                 if(pa->runtime_flag & PNL_ACTIVE) {
792                         int pa_sizex, pa_sizey;
793
794                         if(align==BUT_VERTICAL) {
795                                 pa_sizex= pa->ofsx + pa->sizex;
796                                 pa_sizey= get_panel_real_ofsy(pa);
797                         }
798                         else {
799                                 pa_sizex= get_panel_real_ofsx(pa) + pa->sizex;
800                                 pa_sizey= pa->ofsy + get_panel_size_y(pa);
801                         }
802
803                         sizex= MAX2(sizex, pa_sizex);
804                         sizey= MIN2(sizey, pa_sizey);
805                 }
806         }
807
808         *x= sizex;
809         *y= sizey;
810 }
811
812 static void ui_do_animate(const bContext *C, Panel *panel)
813 {
814         uiHandlePanelData *data= panel->activedata;
815         ScrArea *sa= CTX_wm_area(C);
816         ARegion *ar= CTX_wm_region(C);
817         float fac;
818
819         fac= (PIL_check_seconds_timer()-data->starttime)/ANIMATION_TIME;
820         fac= sqrt(fac);
821         fac= MIN2(fac, 1.0f);
822
823         /* for max 1 second, interpolate positions */
824         if(uiAlignPanelStep(sa, ar, fac, 0))
825                 ED_region_tag_redraw(ar);
826         else
827                 fac= 1.0f;
828
829         if(fac >= 1.0f) {
830                 panel_activate_state(C, panel, PANEL_STATE_EXIT);
831                 return;
832         }
833 }
834
835 void uiBeginPanels(const bContext *UNUSED(C), ARegion *ar)
836 {
837         Panel *pa;
838   
839           /* set all panels as inactive, so that at the end we know
840          * which ones were used */
841         for(pa=ar->panels.first; pa; pa=pa->next) {
842                 if(pa->runtime_flag & PNL_ACTIVE)
843                         pa->runtime_flag= PNL_WAS_ACTIVE;
844                 else
845                         pa->runtime_flag= 0;
846         }
847 }
848
849 /* only draws blocks with panels */
850 void uiEndPanels(const bContext *C, ARegion *ar, int *x, int *y)
851 {
852         ScrArea *sa= CTX_wm_area(C);
853         uiBlock *block;
854         Panel *panot, *panew, *patest, *pa, *firstpa;
855         
856         /* offset contents */
857         for(block= ar->uiblocks.first; block; block= block->next)
858                 if(block->active && block->panel)
859                         ui_offset_panel_block(block);
860
861         /* consistency; are panels not made, whilst they have tabs */
862         for(panot= ar->panels.first; panot; panot= panot->next) {
863                 if((panot->runtime_flag & PNL_ACTIVE)==0) { // not made
864
865                         for(panew= ar->panels.first; panew; panew= panew->next) {
866                                 if((panew->runtime_flag & PNL_ACTIVE)) {
867                                         if(panew->paneltab==panot) { // panew is tab in notmade pa
868                                                 break;
869                                         }
870                                 }
871                         }
872                         /* now panew can become the new parent, check all other tabs */
873                         if(panew) {
874                                 for(patest= ar->panels.first; patest; patest= patest->next) {
875                                         if(patest->paneltab == panot) {
876                                                 patest->paneltab= panew;
877                                         }
878                                 }
879                                 panot->paneltab= panew;
880                                 panew->paneltab= NULL;
881                                 ED_region_tag_redraw(ar); // the buttons panew were not made
882                         }
883                 }       
884         }
885
886         /* re-align, possibly with animation */
887         if(panels_re_align(sa, ar, &pa)) {
888                 if(pa)
889                         panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
890                 else
891                         uiAlignPanelStep(sa, ar, 1.0, 0);
892         }
893
894         /* tag first panel */
895         firstpa= NULL;
896         for(block= ar->uiblocks.first; block; block=block->next)
897                 if(block->active && block->panel)
898                         if(!firstpa || block->panel->sortorder < firstpa->sortorder)
899                                 firstpa= block->panel;
900         
901         if(firstpa)
902                 firstpa->runtime_flag |= PNL_FIRST;
903         
904         /* compute size taken up by panel */
905         ui_panels_size(sa, ar, x, y);
906 }
907
908 void uiDrawPanels(const bContext *C, ARegion *ar)
909 {
910         uiBlock *block;
911
912         UI_ThemeClearColor(TH_BACK);
913         
914         /* draw panels, selected on top */
915         for(block= ar->uiblocks.first; block; block=block->next) {
916                 if(block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
917                         uiDrawBlock(C, block);
918                 }
919         }
920
921         for(block= ar->uiblocks.first; block; block=block->next) {
922                 if(block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
923                         uiDrawBlock(C, block);
924                 }
925         }
926 }
927
928 /* ------------ panel merging ---------------- */
929
930 static void check_panel_overlap(ARegion *ar, Panel *panel)
931 {
932         Panel *pa;
933
934         /* also called with panel==NULL for clear */
935         
936         for(pa=ar->panels.first; pa; pa=pa->next) {
937                 pa->flag &= ~PNL_OVERLAP;
938                 if(panel && (pa != panel)) {
939                         if(pa->paneltab==NULL && (pa->runtime_flag & PNL_ACTIVE)) {
940                                 float safex= 0.2, safey= 0.2;
941                                 
942                                 if(pa->flag & PNL_CLOSEDX) safex= 0.05;
943                                 else if(pa->flag & PNL_CLOSEDY) safey= 0.05;
944                                 else if(panel->flag & PNL_CLOSEDX) safex= 0.05;
945                                 else if(panel->flag & PNL_CLOSEDY) safey= 0.05;
946                                 
947                                 if(pa->ofsx > panel->ofsx- safex*panel->sizex)
948                                 if(pa->ofsx+pa->sizex < panel->ofsx+ (1.0f+safex)*panel->sizex)
949                                 if(pa->ofsy > panel->ofsy- safey*panel->sizey)
950                                 if(pa->ofsy+pa->sizey < panel->ofsy+ (1.0f+safey)*panel->sizey)
951                                         pa->flag |= PNL_OVERLAP;
952                         }
953                 }
954         }
955 }
956
957 /************************ panel dragging ****************************/
958
959 static void ui_do_drag(const bContext *C, wmEvent *event, Panel *panel)
960 {
961         uiHandlePanelData *data= panel->activedata;
962         ScrArea *sa= CTX_wm_area(C);
963         ARegion *ar= CTX_wm_region(C);
964         short align= panel_aligned(sa, ar), dx=0, dy=0;
965         
966         /* first clip for window, no dragging outside */
967         if(!BLI_in_rcti(&ar->winrct, event->x, event->y))
968                 return;
969
970         dx= (event->x-data->startx) & ~(PNL_GRID-1);
971         dy= (event->y-data->starty) & ~(PNL_GRID-1);
972
973         dx *= (float)(ar->v2d.cur.xmax - ar->v2d.cur.xmin)/(float)(ar->winrct.xmax - ar->winrct.xmin);
974         dy *= (float)(ar->v2d.cur.ymax - ar->v2d.cur.ymin)/(float)(ar->winrct.ymax - ar->winrct.ymin);
975         
976         if(data->state == PANEL_STATE_DRAG_SCALE) {
977                 panel->sizex = MAX2(data->startsizex+dx, UI_PANEL_MINX);
978                 
979                 if(data->startsizey-dy < UI_PANEL_MINY)
980                         dy= -UI_PANEL_MINY+data->startsizey;
981
982                 panel->sizey= data->startsizey-dy;
983                 panel->ofsy= data->startofsy+dy;
984         }
985         else {
986                 /* reset the panel snapping, to allow dragging away from snapped edges */
987                 panel->snap = PNL_SNAP_NONE;
988                 
989                 panel->ofsx = data->startofsx+dx;
990                 panel->ofsy = data->startofsy+dy;
991                 check_panel_overlap(ar, panel);
992                 
993                 if(align) uiAlignPanelStep(sa, ar, 0.2, 1);
994         }
995
996         ED_region_tag_redraw(ar);
997 }
998
999 /******************* region level panel interaction *****************/
1000
1001
1002 /* this function is supposed to call general window drawing too */
1003 /* also it supposes a block has panel, and isnt a menu */
1004 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my, int event)
1005 {
1006         ScrArea *sa= CTX_wm_area(C);
1007         ARegion *ar= CTX_wm_region(C);
1008         Panel *pa;
1009         int align= panel_aligned(sa, ar), button= 0;
1010
1011         /* mouse coordinates in panel space! */
1012         
1013         /* XXX weak code, currently it assumes layout style for location of widgets */
1014         
1015         /* check open/collapsed button */
1016         if(event==RETKEY)
1017                 button= 1;
1018         else if(event==AKEY)
1019                 button= 1;
1020         else if(block->panel->flag & PNL_CLOSEDX) {
1021                 if(my >= block->maxy) button= 1;
1022         }
1023         else if(block->panel->control & UI_PNL_CLOSE) {
1024                 /* whole of header can be used to collapse panel (except top-right corner) */
1025                 if(mx <= block->maxx-8-PNL_ICON) button= 2;
1026                 //else if(mx <= block->minx+10+2*PNL_ICON+2) button= 1;
1027         }
1028         else if(mx <= block->maxx-PNL_ICON-12) {
1029                 button= 1;
1030         }
1031         
1032         if(button) {
1033                 if(button==2) { // close
1034                         ED_region_tag_redraw(ar);
1035                 }
1036                 else {  // collapse
1037                         if(block->panel->flag & PNL_CLOSED) {
1038                                 block->panel->flag &= ~PNL_CLOSED;
1039                                 /* snap back up so full panel aligns with screen edge */
1040                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1041                                         block->panel->ofsy= 0;
1042                         }
1043                         else if(align==BUT_HORIZONTAL) {
1044                                 block->panel->flag |= PNL_CLOSEDX;
1045                         }
1046                         else {
1047                                 /* snap down to bottom screen edge*/
1048                                 block->panel->flag |= PNL_CLOSEDY;
1049                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1050                                         block->panel->ofsy= -block->panel->sizey;
1051                         }
1052                         
1053                         for(pa= ar->panels.first; pa; pa= pa->next) {
1054                                 if(pa->paneltab==block->panel) {
1055                                         if(block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1056                                         else pa->flag &= ~PNL_CLOSED;
1057                                 }
1058                         }
1059                 }
1060
1061                 if(align)
1062                         panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1063                 else
1064                         ED_region_tag_redraw(ar);
1065         }
1066         else if(mx <= (block->maxx-PNL_ICON-12)+PNL_ICON+2) {
1067                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1068         }
1069 }
1070
1071 /* XXX should become modal keymap */
1072 /* AKey is opening/closing panels, independent of button state now */
1073
1074 int ui_handler_panel_region(bContext *C, wmEvent *event)
1075 {
1076         ARegion *ar= CTX_wm_region(C);
1077         uiBlock *block;
1078         Panel *pa;
1079         int retval, mx, my, inside_header= 0, inside_scale= 0, inside;
1080
1081         retval= WM_UI_HANDLER_CONTINUE;
1082         for(block=ar->uiblocks.last; block; block=block->prev) {
1083                 mx= event->x;
1084                 my= event->y;
1085                 ui_window_to_block(ar, block, &mx, &my);
1086
1087                 /* check if inside boundbox */
1088                 inside= 0;
1089                 pa= block->panel;
1090
1091                 if(!pa || pa->paneltab!=NULL)
1092                         continue;
1093                 if(pa->type && pa->type->flag & PNL_NO_HEADER) // XXX - accessed freed panels when scripts reload, need to fix.
1094                         continue;
1095
1096                 if(block->minx <= mx && block->maxx >= mx)
1097                         if(block->miny <= my && block->maxy+PNL_HEADER >= my)
1098                                 inside= 1;
1099                 
1100                 if(inside && event->val==KM_PRESS) {
1101                         if(event->type == AKEY && !ELEM4(KM_MOD_FIRST, event->ctrl, event->oskey, event->shift, event->alt)) {
1102                                 
1103                                 if(pa->flag & PNL_CLOSEDY) {
1104                                         if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my))
1105                                                 ui_handle_panel_header(C, block, mx, my, event->type);
1106                                 }
1107                                 else
1108                                         ui_handle_panel_header(C, block, mx, my, event->type);
1109                                 
1110                                 continue;
1111                         }
1112                 }
1113                 
1114                 /* on active button, do not handle panels */
1115                 if(ui_button_is_active(ar))
1116                         continue;
1117                 
1118                 if(inside) {
1119                         /* clicked at panel header? */
1120                         if(pa->flag & PNL_CLOSEDX) {
1121                                 if(block->minx <= mx && block->minx+PNL_HEADER >= mx) 
1122                                         inside_header= 1;
1123                         }
1124                         else if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my)) {
1125                                 inside_header= 1;
1126                         }
1127                         else if(pa->control & UI_PNL_SCALE) {
1128                                 if(block->maxx-PNL_HEADER <= mx)
1129                                         if(block->miny+PNL_HEADER >= my)
1130                                                 inside_scale= 1;
1131                         }
1132
1133                         if(event->val==KM_PRESS) {
1134                                 /* open close on header */
1135                                 if(ELEM(event->type, RETKEY, PADENTER)) {
1136                                         if(inside_header) {
1137                                                 ui_handle_panel_header(C, block, mx, my, RETKEY);
1138                                                 break;
1139                                         }
1140                                 }
1141                                 else if(event->type == LEFTMOUSE) {
1142                                         if(inside_header) {
1143                                                 ui_handle_panel_header(C, block, mx, my, 0);
1144                                                 break;
1145                                         }
1146                                         else if(inside_scale && !(pa->flag & PNL_CLOSED)) {
1147                                                 panel_activate_state(C, pa, PANEL_STATE_DRAG_SCALE);
1148                                                 break;
1149                                         }
1150                                 }
1151                                 else if(event->type == ESCKEY) {
1152                                         /*XXX 2.50 if(block->handler) {
1153                                                 rem_blockhandler(sa, block->handler);
1154                                                 ED_region_tag_redraw(ar);
1155                                                 retval= WM_UI_HANDLER_BREAK;
1156                                         }*/
1157                                 }
1158                                 else if(event->type==PADPLUSKEY || event->type==PADMINUS) {
1159 #if 0 // XXX make float panel exception?
1160                                         int zoom=0;
1161                                 
1162                                         /* if panel is closed, only zoom if mouse is over the header */
1163                                         if (pa->flag & (PNL_CLOSEDX|PNL_CLOSEDY)) {
1164                                                 if (inside_header)
1165                                                         zoom=1;
1166                                         }
1167                                         else
1168                                                 zoom=1;
1169
1170                                         if(zoom) {
1171                                                 ScrArea *sa= CTX_wm_area(C);
1172                                                 SpaceLink *sl= sa->spacedata.first;
1173
1174                                                 if(sa->spacetype!=SPACE_BUTS) {
1175                                                         if(!(pa->control & UI_PNL_SCALE)) {
1176                                                                 if(event->type==PADPLUSKEY) sl->blockscale+= 0.1;
1177                                                                 else sl->blockscale-= 0.1;
1178                                                                 CLAMP(sl->blockscale, 0.6, 1.0);
1179
1180                                                                 ED_region_tag_redraw(ar);
1181                                                                 retval= WM_UI_HANDLER_BREAK;
1182                                                         }                                               
1183                                                 }
1184                                         }
1185 #endif
1186                                 }
1187                         }
1188                 }
1189         }
1190
1191         return retval;
1192 }
1193
1194 /**************** window level modal panel interaction **************/
1195
1196 /* note, this is modal handler and should not swallow events for animation */
1197 static int ui_handler_panel(bContext *C, wmEvent *event, void *userdata)
1198 {
1199         Panel *panel= userdata;
1200         uiHandlePanelData *data= panel->activedata;
1201
1202         /* verify if we can stop */
1203         if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1204                 ScrArea *sa= CTX_wm_area(C);
1205                 ARegion *ar= CTX_wm_region(C);
1206                 int align= panel_aligned(sa, ar);
1207
1208                 if(align)
1209                         panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1210                 else
1211                         panel_activate_state(C, panel, PANEL_STATE_EXIT);
1212         }
1213         else if(event->type == MOUSEMOVE) {
1214                 if(data->state == PANEL_STATE_DRAG)
1215                         ui_do_drag(C, event, panel);
1216         }
1217         else if(event->type == TIMER && event->customdata == data->animtimer) {
1218                 if(data->state == PANEL_STATE_ANIMATION)
1219                         ui_do_animate(C, panel);
1220                 else if(data->state == PANEL_STATE_DRAG)
1221                         ui_do_drag(C, event, panel);
1222         }
1223
1224         data= panel->activedata;
1225
1226         if(data && data->state == PANEL_STATE_ANIMATION)
1227                 return WM_UI_HANDLER_CONTINUE;
1228         else
1229                 return WM_UI_HANDLER_BREAK;
1230 }
1231
1232 static void ui_handler_remove_panel(bContext *C, void *userdata)
1233 {
1234         Panel *pa= userdata;
1235
1236         panel_activate_state(C, pa, PANEL_STATE_EXIT);
1237 }
1238
1239 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
1240 {
1241         uiHandlePanelData *data= pa->activedata;
1242         wmWindow *win= CTX_wm_window(C);
1243         ARegion *ar= CTX_wm_region(C);
1244         
1245         if(data && data->state == state)
1246                 return;
1247
1248         if(state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1249                 if(data && data->state != PANEL_STATE_ANIMATION) {
1250                         /* XXX:
1251                          *      - the panel tabbing function call below (test_add_new_tabs()) has been commented out
1252                          *        "It is too easy to do by accident when reordering panels, is very hard to control and use, and has no real benefit." - BillRey
1253                          * Aligorith, 2009Sep
1254                          */
1255                         //test_add_new_tabs(ar);   // also copies locations of tabs in dragged panel
1256                         check_panel_overlap(ar, NULL);  // clears
1257                 }
1258
1259                 pa->flag &= ~PNL_SELECT;
1260         }
1261         else
1262                 pa->flag |= PNL_SELECT;
1263
1264         if(data && data->animtimer) {
1265                 WM_event_remove_timer(CTX_wm_manager(C), win, data->animtimer);
1266                 data->animtimer= NULL;
1267         }
1268
1269         if(state == PANEL_STATE_EXIT) {
1270                 MEM_freeN(data);
1271                 pa->activedata= NULL;
1272
1273                 WM_event_remove_ui_handler(&win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa, 0);
1274         }
1275         else {
1276                 if(!data) {
1277                         data= MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1278                         pa->activedata= data;
1279
1280                         WM_event_add_ui_handler(C, &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa);
1281                 }
1282
1283                 if(ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1284                         data->animtimer= WM_event_add_timer(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
1285
1286                 data->state= state;
1287                 data->startx= win->eventstate->x;
1288                 data->starty= win->eventstate->y;
1289                 data->startofsx= pa->ofsx;
1290                 data->startofsy= pa->ofsy;
1291                 data->startsizex= pa->sizex;
1292                 data->startsizey= pa->sizey;
1293                 data->starttime= PIL_check_seconds_timer();
1294         }
1295
1296         ED_region_tag_redraw(ar);
1297
1298         /* XXX exception handling, 3d window preview panel */
1299         /* if(block->drawextra==BIF_view3d_previewdraw)
1300                 BIF_view3d_previewrender_clear(curarea);*/
1301         
1302         /* XXX exception handling, 3d window preview panel */
1303         /* if(block->drawextra==BIF_view3d_previewdraw)
1304                 BIF_view3d_previewrender_signal(curarea, PR_DISPRECT);
1305         else if(strcmp(block->name, "image_panel_preview")==0)
1306                 image_preview_event(2); */
1307 }
1308