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