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