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