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