2.5
[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                         float col[3];
588                         
589                         UI_GetThemeColor3fv(TH_TEXT, col);
590
591                         /* active tab */
592                         if(pa==panel)
593                                 glColor4f(col[0], col[1], col[2], 1.0f);
594                         else
595                                 glColor4f(col[0], col[1], col[2], 0.5f);
596                         
597                         hrect= *rect;
598                         hrect.xmin= rect->xmin+pnl_icons + a*width;
599                         hrect.xmax= hrect.xmin + width;
600                         uiStyleFontDraw(&style->paneltitle, &hrect, panelname);
601                         
602                         a++;
603                 }
604         }
605 }
606
607 static void rectf_scale(rctf *rect, float scale)
608 {
609         float centx= 0.5f*(rect->xmin+rect->xmax);
610         float centy= 0.5f*(rect->ymin+rect->ymax);
611         float sizex= 0.5f*scale*(rect->xmax - rect->xmin);
612         float sizey= 0.5f*scale*(rect->ymax - rect->ymin);
613         
614         rect->xmin= centx - sizex;
615         rect->xmax= centx + sizex;
616         rect->ymin= centy - sizey;
617         rect->ymax= centy + sizey;
618 }
619
620 void ui_draw_panel(ARegion *ar, uiStyle *style, uiBlock *block, rcti *rect)
621 {
622         Panel *panel= block->panel, *prev;
623         rcti headrect;
624         rctf itemrect;
625         int ofsx;
626         
627         if(panel->paneltab) return;
628         
629         /* calculate header rect */
630         headrect= *rect;
631         headrect.ymin= headrect.ymax;
632         headrect.ymax= headrect.ymin + floor(PNL_HEADER/block->aspect);
633         
634         /* divider only when there's a previous panel */
635         prev= panel->prev;
636         while(prev) {
637                 if(prev->active) break;
638                 prev= prev->prev;
639         }
640         
641         if(prev) {
642                 float minx= rect->xmin+10.0f/block->aspect;
643                 float maxx= rect->xmax-10.0f/block->aspect;
644                 float y= headrect.ymax;
645                 
646                 glEnable(GL_BLEND);
647                 glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
648                 fdrawline(minx, y+1, maxx, y+1);
649                 glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
650                 fdrawline(minx, y, maxx, y);
651                 glDisable(GL_BLEND);
652         }
653         
654         /* title */
655         if(!(panel->flag & PNL_CLOSEDX)) {
656                 ui_draw_panel_header_style(ar, style, block, &headrect);
657                 
658                 /* itemrect smaller */  
659                 itemrect.xmax= headrect.xmax - 10.0f/block->aspect;
660                 itemrect.xmin= itemrect.xmax - (headrect.ymax-headrect.ymin);
661                 itemrect.ymin= headrect.ymin;
662                 itemrect.ymax= headrect.ymax;
663                 rectf_scale(&itemrect, 0.8f);
664                 ui_draw_panel_dragwidget(&itemrect);
665         }
666         
667         /* if the panel is minimized vertically:
668                 * (------)
669                 */
670         if(panel->flag & PNL_CLOSEDY) {
671                 
672                 
673                 /* if it's being overlapped by a panel being dragged */
674                 if(panel->flag & PNL_OVERLAP) {
675                         UI_ThemeColor(TH_TEXT_HI);
676                         uiRoundRect(rect->xmin, rect->ymax, rect->xmax, rect->ymax+PNL_HEADER, 8);
677                 }
678                 
679         }
680         else if(panel->flag & PNL_CLOSEDX) {
681                 
682         }
683         /* an open panel */
684         else {
685                 
686                 /* in some occasions, draw a border */
687                 if(panel->flag & PNL_SELECT) {
688                         if(panel->control & UI_PNL_SOLID) uiSetRoundBox(15);
689                         else uiSetRoundBox(3);
690                         
691                         UI_ThemeColorShade(TH_HEADER, -120);
692                         uiRoundRect(rect->xmin, rect->ymin, rect->xmax, headrect.ymax+1, 8);
693                 }
694                 if(panel->flag & PNL_OVERLAP) {
695                         if(panel->control & UI_PNL_SOLID) uiSetRoundBox(15);
696                         else uiSetRoundBox(3);
697                         
698                         UI_ThemeColor(TH_TEXT_HI);
699                         uiRoundRect(rect->xmin, rect->ymin, rect->xmax, headrect.ymax+1, 8);
700                 }
701                 
702                 if(panel->control & UI_PNL_SCALE)
703                         ui_draw_panel_scalewidget(rect);
704         }
705         
706         /* draw optional close icon */
707         
708         ofsx= 6;
709         if(panel->control & UI_PNL_CLOSE) {
710                 
711                 ui_draw_x_icon(rect->xmin+2+ofsx, rect->ymax+2);
712                 ofsx= 22;
713         }
714         
715         /* draw collapse icon */
716         UI_ThemeColor(TH_TEXT);
717         
718         /* itemrect smaller */  
719         itemrect.xmin= headrect.xmin + 10.0f/block->aspect;
720         itemrect.xmax= itemrect.xmin + (headrect.ymax-headrect.ymin);
721         itemrect.ymin= headrect.ymin;
722         itemrect.ymax= headrect.ymax;
723         
724         rectf_scale(&itemrect, 0.7f);
725         
726         if(panel->flag & PNL_CLOSEDY)
727                 ui_draw_tria_rect(&itemrect, 'h');
728         else if(panel->flag & PNL_CLOSEDX)
729                 ui_draw_tria_rect(&itemrect, 'h');
730         else
731                 ui_draw_tria_rect(&itemrect, 'v');
732         
733         
734 }
735
736
737 /* ------------ panel alignment ---------------- */
738
739
740 /* this function is needed because uiBlock and Panel itself dont
741 change sizey or location when closed */
742 static int get_panel_real_ofsy(Panel *pa)
743 {
744         if(pa->flag & PNL_CLOSEDY) return pa->ofsy+pa->sizey;
745         else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDY)) return pa->ofsy+pa->sizey;
746         else if(pa->paneltab) return pa->paneltab->ofsy;
747         else return pa->ofsy;
748 }
749
750 static int get_panel_real_ofsx(Panel *pa)
751 {
752         if(pa->flag & PNL_CLOSEDX) return pa->ofsx+PNL_HEADER;
753         else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDX)) return pa->ofsx+PNL_HEADER;
754         else return pa->ofsx+pa->sizex;
755 }
756
757
758 typedef struct PanelSort {
759         Panel *pa, *orig;
760 } PanelSort;
761
762 /* note about sorting;
763    the sortcounter has a lower value for new panels being added.
764    however, that only works to insert a single panel, when more new panels get
765    added the coordinates of existing panels and the previously stored to-be-insterted
766    panels do not match for sorting */
767
768 static int find_leftmost_panel(const void *a1, const void *a2)
769 {
770         const PanelSort *ps1=a1, *ps2=a2;
771         
772         if( ps1->pa->ofsx > ps2->pa->ofsx) return 1;
773         else if( ps1->pa->ofsx < ps2->pa->ofsx) return -1;
774         else if( ps1->pa->sortcounter > ps2->pa->sortcounter) return 1;
775         else if( ps1->pa->sortcounter < ps2->pa->sortcounter) return -1;
776
777         return 0;
778 }
779
780
781 static int find_highest_panel(const void *a1, const void *a2)
782 {
783         const PanelSort *ps1=a1, *ps2=a2;
784         
785         if( ps1->pa->ofsy+ps1->pa->sizey < ps2->pa->ofsy+ps2->pa->sizey) return 1;
786         else if( ps1->pa->ofsy+ps1->pa->sizey > ps2->pa->ofsy+ps2->pa->sizey) return -1;
787         else if( ps1->pa->sortcounter > ps2->pa->sortcounter) return 1;
788         else if( ps1->pa->sortcounter < ps2->pa->sortcounter) return -1;
789         
790         return 0;
791 }
792
793 /* this doesnt draw */
794 /* returns 1 when it did something */
795 int uiAlignPanelStep(ScrArea *sa, ARegion *ar, float fac)
796 {
797         Panel *pa;
798         PanelSort *ps, *panelsort, *psnext;
799         static int sortcounter= 0;
800         int a, tot=0, done;
801         int align= panel_aligned(sa, ar);
802         
803         /* count active, not tabbed Panels */
804         for(pa= ar->panels.first; pa; pa= pa->next) {
805                 if(pa->active && pa->paneltab==NULL) tot++;
806         }
807
808         if(tot==0) return 0;
809
810         /* extra; change close direction? */
811         for(pa= ar->panels.first; pa; pa= pa->next) {
812                 if(pa->active && pa->paneltab==NULL) {
813                         if( (pa->flag & PNL_CLOSEDX) && (align==BUT_VERTICAL) )
814                                 pa->flag ^= PNL_CLOSED;
815                         
816                         else if( (pa->flag & PNL_CLOSEDY) && (align==BUT_HORIZONTAL) )
817                                 pa->flag ^= PNL_CLOSED;
818                         
819                 }
820         }
821
822         panelsort= MEM_callocN( tot*sizeof(PanelSort), "panelsort");
823         
824         /* fill panelsort array */
825         ps= panelsort;
826         for(pa= ar->panels.first; pa; pa= pa->next) {
827                 if(pa->active && pa->paneltab==NULL) {
828                         ps->pa= MEM_dupallocN(pa);
829                         ps->orig= pa;
830                         ps++;
831                 }
832         }
833         
834         if(align==BUT_VERTICAL) 
835                 qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
836         else
837                 qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
838         
839         /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
840         ps= panelsort;
841         ps->pa->ofsx= PNL_DIST;
842         if(align==BUT_VERTICAL)
843                 ps->pa->ofsy= -ps->pa->sizey-PNL_HEADER-PNL_DIST;
844         else
845                 ps->pa->ofsy= -ps->pa->sizey-PNL_HEADER-PNL_DIST; // XXX was 0;
846
847         for(a=0 ; a<tot-1; a++, ps++) {
848                 psnext= ps+1;
849         
850                 if(align==BUT_VERTICAL) {
851                         psnext->pa->ofsx = ps->pa->ofsx;
852                         psnext->pa->ofsy = get_panel_real_ofsy(ps->pa) - psnext->pa->sizey-PNL_HEADER-PNL_DIST;
853                 }
854                 else {
855                         psnext->pa->ofsx = get_panel_real_ofsx(ps->pa)+PNL_DIST;
856                         psnext->pa->ofsy = ps->pa->ofsy + ps->pa->sizey - psnext->pa->sizey;
857                 }
858         }
859         
860         /* we interpolate */
861         done= 0;
862         ps= panelsort;
863         for(a=0; a<tot; a++, ps++) {
864                 if((ps->pa->flag & PNL_SELECT)==0) {
865                         if((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) {
866                                 ps->orig->ofsx= floor(0.5 + fac*ps->pa->ofsx + (1.0-fac)*ps->orig->ofsx);
867                                 ps->orig->ofsy= floor(0.5 + fac*ps->pa->ofsy + (1.0-fac)*ps->orig->ofsy);
868                                 done= 1;
869                         }
870                 }
871         }
872
873         /* copy locations to tabs */
874         for(pa= ar->panels.first; pa; pa= pa->next) {
875                 if(pa->paneltab && pa->active) {
876                         copy_panel_offset(pa, pa->paneltab);
877                 }
878         }
879
880         /* set counter, used for sorting with newly added panels */
881         sortcounter++;
882         for(pa= ar->panels.first; pa; pa= pa->next)
883                 if(pa->active)
884                         pa->sortcounter= sortcounter;
885
886         /* free panelsort array */
887         for(ps= panelsort, a=0; a<tot; a++, ps++)
888                 MEM_freeN(ps->pa);
889         MEM_freeN(panelsort);
890         
891         return done;
892 }
893
894
895 static void ui_do_animate(bContext *C, Panel *panel)
896 {
897         uiHandlePanelData *data= panel->activedata;
898         ScrArea *sa= CTX_wm_area(C);
899         ARegion *ar= CTX_wm_region(C);
900         float fac;
901
902         fac= (PIL_check_seconds_timer()-data->starttime)/ANIMATION_TIME;
903         fac= sqrt(fac);
904         fac= MIN2(fac, 1.0f);
905
906         /* for max 1 second, interpolate positions */
907         if(uiAlignPanelStep(sa, ar, fac))
908                 ED_region_tag_redraw(ar);
909         else
910                 fac= 1.0f;
911
912         if(fac == 1.0f) {
913                 panel_activate_state(C, panel, PANEL_STATE_EXIT);
914                 return;
915         }
916 }
917
918 void uiBeginPanels(const bContext *C, ARegion *ar)
919 {
920         Panel *pa;
921   
922         /* set all panels as inactive, so that at the end we know
923          * which ones were used */
924         for(pa=ar->panels.first; pa; pa=pa->next)
925                 pa->active= 0;
926 }
927
928 /* only draws blocks with panels */
929 void uiEndPanels(const bContext *C, ARegion *ar)
930 {
931         ScrArea *sa= CTX_wm_area(C);
932         uiBlock *block;
933         Panel *panot, *panew, *patest;
934         
935         /* scaling contents */
936         for(block= ar->uiblocks.first; block; block= block->next)
937                 if(block->active && block->panel)
938                         ui_scale_panel_block(block);
939
940         /* consistancy; are panels not made, whilst they have tabs */
941         for(panot= ar->panels.first; panot; panot= panot->next) {
942                 if(panot->active==0) { // not made
943
944                         for(panew= ar->panels.first; panew; panew= panew->next) {
945                                 if(panew->active) {
946                                         if(panew->paneltab==panot) { // panew is tab in notmade pa
947                                                 break;
948                                         }
949                                 }
950                         }
951                         /* now panew can become the new parent, check all other tabs */
952                         if(panew) {
953                                 for(patest= ar->panels.first; patest; patest= patest->next) {
954                                         if(patest->paneltab == panot) {
955                                                 patest->paneltab= panew;
956                                         }
957                                 }
958                                 panot->paneltab= panew;
959                                 panew->paneltab= NULL;
960                                 ED_region_tag_redraw(ar); // the buttons panew were not made
961                         }
962                 }       
963         }
964
965         /* re-align */
966         if(panels_re_align(sa, ar))
967                 uiAlignPanelStep(sa, ar, 1.0);
968         
969         if(sa->spacetype!=SPACE_BUTS) {
970 #if 0 // XXX make float panel exception
971                 SpaceLink *sl= sa->spacedata.first;
972                 for(block= ar->uiblocks.first; block; block= block->next) {
973                         if(block->active && block->panel && block->panel->active && block->panel->paneltab == NULL) {
974                                 float dx=0.0, dy=0.0, minx, miny, maxx, maxy, miny_panel;
975                                 
976                                 minx= sl->blockscale*block->panel->ofsx;
977                                 maxx= sl->blockscale*(block->panel->ofsx+block->panel->sizex);
978                                 miny= sl->blockscale*(block->panel->ofsy+block->panel->sizey);
979                                 maxy= sl->blockscale*(block->panel->ofsy+block->panel->sizey+PNL_HEADER);
980                                 miny_panel= sl->blockscale*(block->panel->ofsy);
981                                 
982                                 /* check to see if snapped panels have been left out in the open by resizing a window
983                                  * and if so, offset them back to where they belong */
984                                 if (block->panel->snap) {
985                                         if (((block->panel->snap) & PNL_SNAP_RIGHT) &&
986                                                 (maxx < (float)sa->winx)) {
987                                                 
988                                                 dx = sa->winx-maxx;
989                                                 block->panel->ofsx+= dx/sl->blockscale;
990                                         }
991                                         if (((block->panel->snap) & PNL_SNAP_TOP) &&
992                                                 (maxy < (float)sa->winy)) {
993                                                 
994                                                 dy = sa->winy-maxy;
995                                                 block->panel->ofsy+= dy/sl->blockscale;
996                                         }
997                                         
998                                         /* reset these vars with updated panel offset distances */
999                                         minx= sl->blockscale*block->panel->ofsx;
1000                                         maxx= sl->blockscale*(block->panel->ofsx+block->panel->sizex);
1001                                         miny= sl->blockscale*(block->panel->ofsy+block->panel->sizey);
1002                                         maxy= sl->blockscale*(block->panel->ofsy+block->panel->sizey+PNL_HEADER);
1003                                         miny_panel= sl->blockscale*(block->panel->ofsy);
1004                                 } else
1005                                         /* reset to no snapping */
1006                                         block->panel->snap = PNL_SNAP_NONE;
1007         
1008
1009                                 /* clip panels (headers) for non-butspace situations (maybe make optimized event later) */
1010                                 
1011                                 /* check left and right edges */
1012                                 if (minx < PNL_SNAP_DIST) {
1013                                         dx = -minx;
1014                                         block->panel->snap |= PNL_SNAP_LEFT;
1015                                 }
1016                                 else if (maxx > ((float)sa->winx - PNL_SNAP_DIST)) {
1017                                         dx= sa->winx-maxx;
1018                                         block->panel->snap |= PNL_SNAP_RIGHT;
1019                                 }                               
1020                                 if( minx + dx < 0.0) dx= -minx; // when panel cant fit, put it fixed here
1021                                 
1022                                 /* check top and bottom edges */
1023                                 if ((miny_panel < PNL_SNAP_DIST) && (miny_panel > -PNL_SNAP_DIST)) {
1024                                         dy= -miny_panel;
1025                                         block->panel->snap |= PNL_SNAP_BOTTOM;
1026                                 }
1027                                 if(miny < PNL_SNAP_DIST)  {
1028                                         dy= -miny;
1029                                         block->panel->snap |= PNL_SNAP_BOTTOM;
1030                                 }
1031                                 else if(maxy > ((float)sa->winy - PNL_SNAP_DIST)) {
1032                                         dy= sa->winy-maxy;
1033                                         block->panel->snap |= PNL_SNAP_TOP;
1034                                 }
1035                                 if( miny + dy < 0.0) dy= -miny; // when panel cant fit, put it fixed here
1036
1037                                 
1038                                 block->panel->ofsx+= dx/sl->blockscale;
1039                                 block->panel->ofsy+= dy/sl->blockscale;
1040
1041                                 /* copy locations */
1042                                 for(patest= ar->panels.first; patest; patest= patest->next) {
1043                                         if(patest->paneltab==block->panel) copy_panel_offset(patest, block->panel);
1044                                 }
1045                                 
1046                         }
1047                 }
1048 #endif
1049         }
1050
1051         /* update v2d->totrct and update view */
1052         ui_panels_update_totrct(ar);
1053         UI_view2d_view_restore(C);
1054         UI_view2d_view_ortho(C, &ar->v2d);
1055
1056         /* draw panels, selected on top */
1057         for(block= ar->uiblocks.first; block; block=block->next) {
1058                 if(block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
1059                         uiDrawBlock(C, block);
1060                 }
1061         }
1062
1063         for(block= ar->uiblocks.first; block; block=block->next) {
1064                 if(block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
1065                         uiDrawBlock(C, block);
1066                 }
1067         }
1068 }
1069
1070 /* ------------ panel merging ---------------- */
1071
1072 static void check_panel_overlap(ARegion *ar, Panel *panel)
1073 {
1074         Panel *pa;
1075
1076         /* also called with panel==NULL for clear */
1077         
1078         for(pa=ar->panels.first; pa; pa=pa->next) {
1079                 pa->flag &= ~PNL_OVERLAP;
1080                 if(panel && (pa != panel)) {
1081                         if(pa->paneltab==NULL && pa->active) {
1082                                 float safex= 0.2, safey= 0.2;
1083                                 
1084                                 if( pa->flag & PNL_CLOSEDX) safex= 0.05;
1085                                 else if(pa->flag & PNL_CLOSEDY) safey= 0.05;
1086                                 else if( panel->flag & PNL_CLOSEDX) safex= 0.05;
1087                                 else if(panel->flag & PNL_CLOSEDY) safey= 0.05;
1088                                 
1089                                 if( pa->ofsx > panel->ofsx- safex*panel->sizex)
1090                                 if( pa->ofsx+pa->sizex < panel->ofsx+ (1.0+safex)*panel->sizex)
1091                                 if( pa->ofsy > panel->ofsy- safey*panel->sizey)
1092                                 if( pa->ofsy+pa->sizey < panel->ofsy+ (1.0+safey)*panel->sizey)
1093                                         pa->flag |= PNL_OVERLAP;
1094                         }
1095                 }
1096         }
1097 }
1098
1099 static void test_add_new_tabs(ARegion *ar)
1100 {
1101         Panel *pa, *pasel=NULL, *palap=NULL;
1102         /* search selected and overlapped panel */
1103         
1104         pa= ar->panels.first;
1105         while(pa) {
1106                 if(pa->active) {
1107                         if(pa->flag & PNL_SELECT) pasel= pa;
1108                         if(pa->flag & PNL_OVERLAP) palap= pa;
1109                 }
1110                 pa= pa->next;
1111         }
1112         
1113         if(pasel && palap==NULL) {
1114
1115                 /* copy locations */
1116                 pa= ar->panels.first;
1117                 while(pa) {
1118                         if(pa->paneltab==pasel) {
1119                                 copy_panel_offset(pa, pasel);
1120                         }
1121                         pa= pa->next;
1122                 }
1123         }
1124         
1125         if(pasel==NULL || palap==NULL) return;
1126         
1127         /* the overlapped panel becomes a tab */
1128         palap->paneltab= pasel;
1129         
1130         /* the selected panel gets coords of overlapped one */
1131         copy_panel_offset(pasel, palap);
1132
1133         /* and its tabs */
1134         pa= ar->panels.first;
1135         while(pa) {
1136                 if(pa->paneltab == pasel) {
1137                         copy_panel_offset(pa, palap);
1138                 }
1139                 pa= pa->next;
1140         }
1141         
1142         /* but, the overlapped panel already can have tabs too! */
1143         pa= ar->panels.first;
1144         while(pa) {
1145                 if(pa->paneltab == palap) {
1146                         pa->paneltab = pasel;
1147                 }
1148                 pa= pa->next;
1149         }
1150 }
1151
1152 /* ------------ panel drag ---------------- */
1153
1154 static void ui_do_drag(bContext *C, wmEvent *event, Panel *panel)
1155 {
1156         uiHandlePanelData *data= panel->activedata;
1157         ScrArea *sa= CTX_wm_area(C);
1158         ARegion *ar= CTX_wm_region(C);
1159         short align= panel_aligned(sa, ar), dx=0, dy=0;
1160         
1161         /* first clip for window, no dragging outside */
1162         if(!BLI_in_rcti(&ar->winrct, event->x, event->y))
1163                 return;
1164
1165         dx= (event->x-data->startx) & ~(PNL_GRID-1);
1166         dy= (event->y-data->starty) & ~(PNL_GRID-1);
1167         
1168         if(data->state == PANEL_STATE_DRAG_SCALE) {
1169                 panel->sizex = MAX2(data->startsizex+dx, UI_PANEL_MINX);
1170                 
1171                 if(data->startsizey-dy < UI_PANEL_MINY) {
1172                         dy= -UI_PANEL_MINY+data->startsizey;
1173                 }
1174                 panel->sizey = data->startsizey-dy;
1175                 
1176                 panel->ofsy= data->startofsy+dy;
1177
1178         }
1179         else {
1180                 /* reset the panel snapping, to allow dragging away from snapped edges */
1181                 panel->snap = PNL_SNAP_NONE;
1182                 
1183                 panel->ofsx = data->startofsx+dx;
1184                 panel->ofsy = data->startofsy+dy;
1185                 check_panel_overlap(ar, panel);
1186                 
1187                 if(align) uiAlignPanelStep(sa, ar, 0.2);
1188         }
1189
1190         ED_region_tag_redraw(ar);
1191 }
1192
1193 static void ui_do_untab(bContext *C, wmEvent *event, Panel *panel)
1194 {
1195         uiHandlePanelData *data= panel->activedata;
1196         ARegion *ar= CTX_wm_region(C);
1197         Panel *pa, *panew= NULL;
1198         int nr;
1199
1200         /* wait until a threshold is passed to untab */
1201         if(abs(event->x-data->startx) + abs(event->y-data->starty) > 6) {
1202                 /* find new parent panel */
1203                 nr= 0;
1204                 for(pa= ar->panels.first; pa; pa=pa->next) {
1205                         if(pa->paneltab==panel) {
1206                                 panew= pa;
1207                                 nr++;
1208                         }
1209                 }
1210                 
1211                 /* make old tabs point to panew */
1212                 panew->paneltab= NULL;
1213                 
1214                 for(pa= ar->panels.first; pa; pa=pa->next)
1215                         if(pa->paneltab==panel)
1216                                 pa->paneltab= panew;
1217                 
1218                 panel_activate_state(C, panel, PANEL_STATE_DRAG);
1219         }
1220 }
1221
1222 /* region level panel interaction */
1223
1224 static void panel_clicked_tabs(bContext *C, ScrArea *sa, ARegion *ar, uiBlock *block,  int mousex)
1225 {
1226         Panel *pa, *tabsel=NULL, *panel= block->panel;
1227         int nr= 1, a, width, ofsx;
1228         
1229         ofsx= PNL_ICON;
1230         if(block->panel->control & UI_PNL_CLOSE) ofsx+= PNL_ICON;
1231         
1232         /* count */
1233         for(pa= ar->panels.first; pa; pa=pa->next)
1234                 if(pa!=panel)
1235                         if(pa->active && pa->paneltab==panel)
1236                                 nr++;
1237
1238         if(nr==1) return;
1239         
1240         /* find clicked tab, mouse in panel coords */
1241         a= 0;
1242         width= (int)((float)(panel->sizex - ofsx-10)/nr);
1243         pa= ar->panels.first;
1244         while(pa) {
1245                 if(pa==panel || (pa->active && pa->paneltab==panel)) {
1246                         if( (mousex > ofsx+a*width) && (mousex < ofsx+(a+1)*width) ) {
1247                                 tabsel= pa;
1248                                 break;
1249                         }
1250                         a++;
1251                 }
1252                 pa= pa->next;
1253         }
1254
1255         if(tabsel) {
1256                 if(tabsel == panel) {
1257                         panel_activate_state(C, panel, PANEL_STATE_WAIT_UNTAB);
1258                 }
1259                 else {
1260                         /* tabsel now becomes parent for all others */
1261                         panel->paneltab= tabsel;
1262                         tabsel->paneltab= NULL;
1263                         
1264                         pa= ar->panels.first;
1265                         while(pa) {
1266                                 if(pa->paneltab == panel) pa->paneltab = tabsel;
1267                                 pa= pa->next;
1268                         }
1269                         
1270                         /* copy locations to tabs */
1271                         for(pa= ar->panels.first; pa; pa= pa->next) {
1272                                 if(pa->paneltab && pa->active) {
1273                                         copy_panel_offset(pa, pa->paneltab);
1274                                 }
1275                         }
1276                         
1277                         /* panels now differ size.. */
1278                         if(panel_aligned(sa, ar))
1279                                 panel_activate_state(C, tabsel, PANEL_STATE_ANIMATION);
1280
1281                         ED_region_tag_redraw(ar);
1282                 }
1283         }
1284         
1285 }
1286
1287 /* this function is supposed to call general window drawing too */
1288 /* also it supposes a block has panel, and isnt a menu */
1289 static void ui_handle_panel_header(bContext *C, uiBlock *block, int mx, int my)
1290 {
1291         ScrArea *sa= CTX_wm_area(C);
1292         ARegion *ar= CTX_wm_region(C);
1293         Panel *pa;
1294         int align= panel_aligned(sa, ar), button= 0;
1295
1296         /* mouse coordinates in panel space! */
1297         
1298         /* XXX weak code, currently it assumes layout style for location of widgets */
1299         
1300         /* check open/collapsed button */
1301         if(block->panel->flag & PNL_CLOSEDX) {
1302                 if(my >= block->maxy) button= 1;
1303         }
1304         else if(block->panel->control & UI_PNL_CLOSE) {
1305                 if(mx <= block->minx+10+PNL_ICON-2) button= 2;
1306                 else if(mx <= block->minx+10+2*PNL_ICON+2) button= 1;
1307         }
1308         else if(mx <= block->minx+10+PNL_ICON+2) {
1309                 button= 1;
1310         }
1311         
1312         if(button) {
1313                 if(button==2) { // close
1314                         ED_region_tag_redraw(ar);
1315                 }
1316                 else {  // collapse
1317                         if(block->panel->flag & PNL_CLOSED) {
1318                                 block->panel->flag &= ~PNL_CLOSED;
1319                                 /* snap back up so full panel aligns with screen edge */
1320                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1321                                         block->panel->ofsy= 0;
1322                         }
1323                         else if(align==BUT_HORIZONTAL) {
1324                                 block->panel->flag |= PNL_CLOSEDX;
1325                         }
1326                         else {
1327                                 /* snap down to bottom screen edge*/
1328                                 block->panel->flag |= PNL_CLOSEDY;
1329                                 if (block->panel->snap & PNL_SNAP_BOTTOM) 
1330                                         block->panel->ofsy= -block->panel->sizey;
1331                         }
1332                         
1333                         for(pa= ar->panels.first; pa; pa= pa->next) {
1334                                 if(pa->paneltab==block->panel) {
1335                                         if(block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1336                                         else pa->flag &= ~PNL_CLOSED;
1337                                 }
1338                         }
1339                 }
1340
1341                 if(align)
1342                         panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1343                 else
1344                         ED_region_tag_redraw(ar);
1345         }
1346         else if(block->panel->flag & PNL_CLOSED) {
1347                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1348         }
1349         /* check if clicked in tabbed area */
1350         else if(mx < block->maxx-PNL_ICON-3 && panel_has_tabs(ar, block->panel)) {
1351                 panel_clicked_tabs(C, sa, ar, block, mx);
1352         }
1353         else {
1354                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1355         }
1356 }
1357
1358 int ui_handler_panel_region(bContext *C, wmEvent *event)
1359 {
1360         ARegion *ar= CTX_wm_region(C);
1361         uiBlock *block;
1362         int retval, mx, my, inside_header= 0, inside_scale= 0, inside;
1363
1364         retval= WM_UI_HANDLER_CONTINUE;
1365
1366         for(block=ar->uiblocks.last; block; block=block->prev) {
1367                 mx= event->x;
1368                 my= event->y;
1369                 ui_window_to_block(ar, block, &mx, &my);
1370
1371                 /* check if inside boundbox */
1372                 inside= 0;
1373
1374                 if(block->panel && block->panel->paneltab==NULL)
1375                         if(block->minx <= mx && block->maxx >= mx)
1376                                 if(block->miny <= my && block->maxy+PNL_HEADER >= my)
1377                                         inside= 1;
1378
1379                 if(inside) {
1380                         /* clicked at panel header? */
1381                         if(block->panel->flag & PNL_CLOSEDX) {
1382                                 if(block->minx <= mx && block->minx+PNL_HEADER >= mx) 
1383                                         inside_header= 1;
1384                         }
1385                         else if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my)) {
1386                                 inside_header= 1;
1387                         }
1388                         else if(block->panel->control & UI_PNL_SCALE) {
1389                                 if(block->maxx-PNL_HEADER <= mx)
1390                                         if(block->miny+PNL_HEADER >= my)
1391                                                 inside_scale= 1;
1392                         }
1393
1394                         if(event->val==KM_PRESS) {
1395                                 if(event->type == LEFTMOUSE) {
1396                                         if(inside_header)
1397                                                 ui_handle_panel_header(C, block, mx, my);
1398                                         else if(inside_scale && !(block->panel->flag & PNL_CLOSED))
1399                                                 panel_activate_state(C, block->panel, PANEL_STATE_DRAG_SCALE);
1400                                 }
1401                                 else if(event->type == ESCKEY) {
1402                                         /*XXX 2.50 if(block->handler) {
1403                                                 rem_blockhandler(sa, block->handler);
1404                                                 ED_region_tag_redraw(ar);
1405                                                 retval= WM_UI_HANDLER_BREAK;
1406                                         }*/
1407                                 }
1408                                 else if(event->type==PADPLUSKEY || event->type==PADMINUS) {
1409                                         int zoom=0;
1410                                 
1411                                         /* if panel is closed, only zoom if mouse is over the header */
1412                                         if (block->panel->flag & (PNL_CLOSEDX|PNL_CLOSEDY)) {
1413                                                 if (inside_header)
1414                                                         zoom=1;
1415                                         }
1416                                         else
1417                                                 zoom=1;
1418
1419 #if 0 // XXX make float panel exception?
1420                                         if(zoom) {
1421                                                 ScrArea *sa= CTX_wm_area(C);
1422                                                 SpaceLink *sl= sa->spacedata.first;
1423
1424                                                 if(sa->spacetype!=SPACE_BUTS) {
1425                                                         if(!(block->panel->control & UI_PNL_SCALE)) {
1426                                                                 if(event->type==PADPLUSKEY) sl->blockscale+= 0.1;
1427                                                                 else sl->blockscale-= 0.1;
1428                                                                 CLAMP(sl->blockscale, 0.6, 1.0);
1429
1430                                                                 ED_region_tag_redraw(ar);
1431                                                                 retval= WM_UI_HANDLER_BREAK;
1432                                                         }                                               
1433                                                 }
1434                                         }
1435 #endif
1436                                 }
1437                         }
1438                 }
1439         }
1440
1441         return retval;
1442 }
1443
1444 /* window level modal panel interaction */
1445
1446 static int ui_handler_panel(bContext *C, wmEvent *event, void *userdata)
1447 {
1448         Panel *panel= userdata;
1449         uiHandlePanelData *data= panel->activedata;
1450
1451         /* verify if we can stop */
1452         if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1453                 ScrArea *sa= CTX_wm_area(C);
1454                 ARegion *ar= CTX_wm_region(C);
1455                 int align= panel_aligned(sa, ar);
1456
1457                 if(align)
1458                         panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1459                 else
1460                         panel_activate_state(C, panel, PANEL_STATE_EXIT);
1461
1462                 return WM_UI_HANDLER_BREAK;
1463         }
1464         else if(event->type == MOUSEMOVE) {
1465                 if(data->state == PANEL_STATE_WAIT_UNTAB)
1466                         ui_do_untab(C, event, panel);
1467                 else if(data->state == PANEL_STATE_DRAG)
1468                         ui_do_drag(C, event, panel);
1469         }
1470         else if(event->type == TIMER && event->customdata == data->animtimer) {
1471                 if(data->state == PANEL_STATE_ANIMATION)
1472                         ui_do_animate(C, panel);
1473                 else if(data->state == PANEL_STATE_DRAG)
1474                         ui_do_drag(C, event, panel);
1475         }
1476
1477         data= panel->activedata;
1478
1479         if(data && data->state == PANEL_STATE_ANIMATION)
1480                 return WM_UI_HANDLER_CONTINUE;
1481         else
1482                 return WM_UI_HANDLER_BREAK;
1483 }
1484
1485 static void ui_handler_remove_panel(bContext *C, void *userdata)
1486 {
1487         Panel *pa= userdata;
1488
1489         panel_activate_state(C, pa, PANEL_STATE_EXIT);
1490 }
1491
1492 static void panel_activate_state(bContext *C, Panel *pa, uiHandlePanelState state)
1493 {
1494         uiHandlePanelData *data= pa->activedata;
1495         wmWindow *win= CTX_wm_window(C);
1496         ARegion *ar= CTX_wm_region(C);
1497         
1498         if(data && data->state == state)
1499                 return;
1500
1501         if(state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1502                 if(data && data->state != PANEL_STATE_ANIMATION) {
1503                         test_add_new_tabs(ar);   // also copies locations of tabs in dragged panel
1504                         check_panel_overlap(ar, NULL);  // clears
1505                 }
1506
1507                 pa->flag &= ~PNL_SELECT;
1508         }
1509         else
1510                 pa->flag |= PNL_SELECT;
1511
1512         if(data && data->animtimer) {
1513                 WM_event_remove_window_timer(win, data->animtimer);
1514                 data->animtimer= NULL;
1515         }
1516
1517         if(state == PANEL_STATE_EXIT) {
1518                 MEM_freeN(data);
1519                 pa->activedata= NULL;
1520
1521                 WM_event_remove_ui_handler(&win->handlers, ui_handler_panel, ui_handler_remove_panel, pa);
1522         }
1523         else {
1524                 if(!data) {
1525                         data= MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1526                         pa->activedata= data;
1527
1528                         WM_event_add_ui_handler(C, &win->handlers, ui_handler_panel, ui_handler_remove_panel, pa);
1529                 }
1530
1531                 if(ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1532                         data->animtimer= WM_event_add_window_timer(win, TIMER, ANIMATION_INTERVAL);
1533
1534                 data->state= state;
1535                 data->startx= win->eventstate->x;
1536                 data->starty= win->eventstate->y;
1537                 data->startofsx= pa->ofsx;
1538                 data->startofsy= pa->ofsy;
1539                 data->startsizex= pa->sizex;
1540                 data->startsizey= pa->sizey;
1541                 data->starttime= PIL_check_seconds_timer();
1542         }
1543
1544         ED_region_tag_redraw(ar);
1545
1546         /* XXX exception handling, 3d window preview panel */
1547         /* if(block->drawextra==BIF_view3d_previewdraw)
1548                 BIF_view3d_previewrender_clear(curarea);*/
1549         
1550         /* XXX exception handling, 3d window preview panel */
1551         /* if(block->drawextra==BIF_view3d_previewdraw)
1552                 BIF_view3d_previewrender_signal(curarea, PR_DISPRECT);
1553         else if(strcmp(block->name, "image_panel_preview")==0)
1554                 image_preview_event(2); */
1555 }
1556