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