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