2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2003-2009 full recode.
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/interface/interface_panel.c
27 * \ingroup edinterface
31 /* a full doc with API notes can be found in bf-blender/trunk/blender/doc/guides/interface_API.txt */
38 #include "MEM_guardedalloc.h"
42 #include "BLI_blenlib.h"
44 #include "BLI_utildefines.h"
46 #include "BLF_translation.h"
48 #include "DNA_userdef_types.h"
50 #include "BKE_context.h"
51 #include "BKE_screen.h"
54 #include "BIF_glutil.h"
59 #include "ED_screen.h"
61 #include "UI_interface.h"
62 #include "UI_resources.h"
64 #include "interface_intern.h"
66 /*********************** defines and structs ************************/
68 #define ANIMATION_TIME 0.30
69 #define ANIMATION_INTERVAL 0.02
71 #define PNL_LAST_ADDED 1
73 #define PNL_WAS_ACTIVE 4
74 #define PNL_ANIM_ALIGN 8
75 #define PNL_NEW_ADDED 16
78 typedef enum uiHandlePanelState {
80 PANEL_STATE_DRAG_SCALE,
81 PANEL_STATE_WAIT_UNTAB,
82 PANEL_STATE_ANIMATION,
86 typedef struct uiHandlePanelData {
87 uiHandlePanelState state;
95 int startofsx, startofsy;
96 int startsizex, startsizey;
99 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state);
101 /*********************** space specific code ************************/
102 /* temporary code to remove all sbuts stuff from panel code */
104 static int panel_aligned(ScrArea *sa, ARegion *ar)
106 if(sa->spacetype==SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
107 SpaceButs *sbuts= sa->spacedata.first;
110 else if(sa->spacetype==SPACE_USERPREF && ar->regiontype == RGN_TYPE_WINDOW)
112 else if(sa->spacetype==SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS)
114 else if(sa->spacetype==SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW)
116 else if(ELEM3(ar->regiontype, RGN_TYPE_UI, RGN_TYPE_TOOLS, RGN_TYPE_TOOL_PROPS))
122 static int panels_re_align(ScrArea *sa, ARegion *ar, Panel **r_pa)
129 if(sa->spacetype==SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
130 SpaceButs *sbuts= sa->spacedata.first;
133 if(sbuts->re_align || sbuts->mainbo!=sbuts->mainb)
136 else if(ar->regiontype==RGN_TYPE_UI)
138 else if(sa->spacetype==SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW)
140 else if(sa->spacetype==SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS)
143 /* in case panel is added or disappears */
144 for(pa=ar->panels.first; pa; pa=pa->next) {
145 if((pa->runtime_flag & PNL_WAS_ACTIVE) && !(pa->runtime_flag & PNL_ACTIVE))
147 if(!(pa->runtime_flag & PNL_WAS_ACTIVE) && (pa->runtime_flag & PNL_ACTIVE))
153 /* in case we need to do an animation (size changes) */
154 for(pa=ar->panels.first; pa; pa=pa->next) {
155 if(pa->runtime_flag & PNL_ANIM_ALIGN) {
165 /****************************** panels ******************************/
167 static void ui_panel_copy_offset(Panel *pa, Panel *papar)
169 /* with respect to sizes... papar is parent */
171 pa->ofsx= papar->ofsx;
172 pa->ofsy= papar->ofsy + papar->sizey-pa->sizey;
175 Panel *uiBeginPanel(ScrArea *sa, ARegion *ar, uiBlock *block, PanelType *pt, int *open)
177 Panel *pa, *patab, *palast, *panext;
178 char *drawname= pt->label;
179 char *idname= pt->idname;
180 char *tabname= pt->idname;
181 char *hookname= NULL;
183 int align= panel_aligned(sa, ar);
185 /* check if Panel exists, then use that one */
186 for(pa=ar->panels.first; pa; pa=pa->next)
187 if(strncmp(pa->panelname, idname, UI_MAX_NAME_STR)==0)
188 if(strncmp(pa->tabname, tabname, UI_MAX_NAME_STR)==0)
191 newpanel= (pa == NULL);
198 pa= MEM_callocN(sizeof(Panel), "new panel");
200 BLI_strncpy(pa->panelname, idname, UI_MAX_NAME_STR);
201 BLI_strncpy(pa->tabname, tabname, UI_MAX_NAME_STR);
203 if(pt->flag & PNL_DEFAULT_CLOSED) {
204 if(align == BUT_VERTICAL)
205 pa->flag |= PNL_CLOSEDY;
207 pa->flag |= PNL_CLOSEDX;
214 pa->runtime_flag |= PNL_NEW_ADDED;
216 BLI_addtail(&ar->panels, pa);
218 /* make new Panel tabbed? */
220 for(patab= ar->panels.first; patab; patab= patab->next) {
221 if((patab->runtime_flag & PNL_ACTIVE) && patab->paneltab==NULL) {
222 if(strncmp(hookname, patab->panelname, UI_MAX_NAME_STR)==0) {
223 if(strncmp(tabname, patab->tabname, UI_MAX_NAME_STR)==0) {
225 ui_panel_copy_offset(pa, patab);
234 BLI_strncpy(pa->drawname, drawname, UI_MAX_NAME_STR);
236 /* if a new panel is added, we insert it right after the panel
237 * that was last added. this way new panels are inserted in the
238 * right place between versions */
239 for(palast=ar->panels.first; palast; palast=palast->next)
240 if(palast->runtime_flag & PNL_LAST_ADDED)
244 pa->sortorder= (palast)? palast->sortorder+1: 0;
246 for(panext=ar->panels.first; panext; panext=panext->next)
247 if(panext != pa && panext->sortorder >= pa->sortorder)
252 palast->runtime_flag &= ~PNL_LAST_ADDED;
254 /* assign to block */
256 pa->runtime_flag |= PNL_ACTIVE|PNL_LAST_ADDED;
260 if(pa->paneltab) return pa;
261 if(pa->flag & PNL_CLOSED) return pa;
268 void uiEndPanel(uiBlock *block, int width, int height)
270 Panel *pa= block->panel;
272 if(pa->runtime_flag & PNL_NEW_ADDED) {
273 pa->runtime_flag &= ~PNL_NEW_ADDED;
278 /* check if we need to do an animation */
279 if(!ELEM(width, 0, pa->sizex) || !ELEM(height, 0, pa->sizey)) {
280 pa->runtime_flag |= PNL_ANIM_ALIGN;
282 pa->ofsy += pa->sizey-height;
285 /* update width/height if non-zero */
293 static void ui_offset_panel_block(uiBlock *block)
295 uiStyle *style= UI_GetStyle();
299 /* compute bounds and offset */
300 ui_bounds_block(block);
302 ofsy= block->panel->sizey - style->panelspace;
304 for(but= block->buttons.first; but; but=but->next) {
309 block->maxx= block->panel->sizex;
310 block->maxy= block->panel->sizey;
311 block->minx= block->miny= 0.0;
314 /**************************** drawing *******************************/
316 /* extern used by previewrender */
318 static void uiPanelPush(uiBlock *block)
323 glTranslatef((float)block->panel->ofsx, (float)block->panel->ofsy, 0.0);
326 static void uiPanelPop(uiBlock *UNUSED(block))
332 /* triangle 'icon' for panel header */
333 void UI_DrawTriIcon(float x, float y, char dir)
336 ui_draw_anti_tria( x-3, y-5, x-3, y+5, x+7,y );
339 ui_draw_anti_tria( x-5, y-7, x+5, y-7, x, y+3);
341 else { /* 'v' = vertical, down */
342 ui_draw_anti_tria( x-5, y+3, x+5, y+3, x, y-7);
346 /* triangle 'icon' inside rect */
347 static void ui_draw_tria_rect(rctf *rect, char dir)
350 float half= 0.5f*(rect->ymax - rect->ymin);
351 ui_draw_anti_tria(rect->xmin, rect->ymin, rect->xmin, rect->ymax, rect->xmax, rect->ymin+half);
354 float half= 0.5f*(rect->xmax - rect->xmin);
355 ui_draw_anti_tria(rect->xmin, rect->ymax, rect->xmax, rect->ymax, rect->xmin+half, rect->ymin);
359 static void ui_draw_anti_x(float x1, float y1, float x2, float y2)
362 /* set antialias line */
363 glEnable(GL_LINE_SMOOTH);
368 fdrawline(x1, y1, x2, y2);
369 fdrawline(x1, y2, x2, y1);
373 glDisable(GL_LINE_SMOOTH);
378 /* x 'icon' for panel header */
379 static void ui_draw_x_icon(float x, float y)
382 ui_draw_anti_x(x, y, x+9.375f, y+9.375f);
386 #define PNL_ICON UI_UNIT_X /* could be UI_UNIT_Y too */
388 static void ui_draw_panel_scalewidget(rcti *rect)
390 float xmin, xmax, dx;
391 float ymin, ymax, dy;
393 xmin= rect->xmax-PNL_HEADER+2;
396 ymax= rect->ymin+PNL_HEADER-2;
398 dx= 0.5f*(xmax-xmin);
399 dy= 0.5f*(ymax-ymin);
402 glColor4ub(255, 255, 255, 50);
403 fdrawline(xmin, ymin, xmax, ymax);
404 fdrawline(xmin+dx, ymin, xmax, ymax-dy);
406 glColor4ub(0, 0, 0, 50);
407 fdrawline(xmin, ymin+1, xmax, ymax+1);
408 fdrawline(xmin+dx, ymin+1, xmax, ymax-dy+1);
412 static void ui_draw_panel_dragwidget(rctf *rect)
414 float xmin, xmax, dx;
415 float ymin, ymax, dy;
422 dx= 0.333f*(xmax-xmin);
423 dy= 0.333f*(ymax-ymin);
426 glColor4ub(255, 255, 255, 50);
427 fdrawline(xmin, ymax, xmax, ymin);
428 fdrawline(xmin+dx, ymax, xmax, ymin+dy);
429 fdrawline(xmin+2*dx, ymax, xmax, ymin+2*dy);
431 glColor4ub(0, 0, 0, 50);
432 fdrawline(xmin, ymax+1, xmax, ymin+1);
433 fdrawline(xmin+dx, ymax+1, xmax, ymin+dy+1);
434 fdrawline(xmin+2*dx, ymax+1, xmax, ymin+2*dy+1);
439 static void ui_draw_aligned_panel_header(uiStyle *style, uiBlock *block, rcti *rect, char dir)
441 Panel *panel= block->panel;
444 const char *activename = IFACE_(panel->drawname[0] ? panel->drawname : panel->panelname);
446 /* + 0.001f to avoid flirting with float inaccuracy */
447 if(panel->control & UI_PNL_CLOSE) pnl_icons=(panel->labelofs+2*PNL_ICON+5)/block->aspect + 0.001f;
448 else pnl_icons= (panel->labelofs+PNL_ICON+5)/block->aspect + 0.001f;
451 /* draw text label */
452 UI_ThemeColor(TH_TITLE);
456 hrect.xmin = rect->xmin+pnl_icons;
457 hrect.ymin += 2.0f/block->aspect;
458 uiStyleFontDraw(&style->paneltitle, &hrect, activename);
461 /* ignore 'pnl_icons', otherwise the text gets offset horizontally
462 * + 0.001f to avoid flirting with float inaccuracy
464 hrect.xmin = rect->xmin + (PNL_ICON+5)/block->aspect + 0.001f;
465 uiStyleFontDrawRotated(&style->paneltitle, &hrect, activename);
469 static void rectf_scale(rctf *rect, float scale)
471 float centx= 0.5f*(rect->xmin+rect->xmax);
472 float centy= 0.5f*(rect->ymin+rect->ymax);
473 float sizex= 0.5f*scale*(rect->xmax - rect->xmin);
474 float sizey= 0.5f*scale*(rect->ymax - rect->ymin);
476 rect->xmin = centx - sizex;
477 rect->xmax = centx + sizex;
478 rect->ymin = centy - sizey;
479 rect->ymax = centy + sizey;
482 /* panel integrated in buttonswindow, tool/property lists etc */
483 void ui_draw_aligned_panel(uiStyle *style, uiBlock *block, rcti *rect)
485 bTheme *btheme= UI_GetTheme();
486 Panel *panel= block->panel;
491 if(panel->paneltab) return;
492 if(panel->type && (panel->type->flag & PNL_NO_HEADER)) return;
494 /* calculate header rect */
495 /* + 0.001f to prevent flicker due to float inaccuracy */
497 headrect.ymin = headrect.ymax;
498 headrect.ymax = headrect.ymin + floor(PNL_HEADER/block->aspect + 0.001f);
501 float minx= rect->xmin;
502 float maxx= rect->xmax;
503 float y= headrect.ymax;
507 if(btheme->tui.panel.show_header) {
508 /* draw with background color */
510 glColor4ubv((unsigned char*)btheme->tui.panel.header);
511 glRectf(minx, headrect.ymin+1, maxx, y);
513 fdrawline(minx, y, maxx, y);
514 fdrawline(minx, y, maxx, y);
516 else if(!(panel->runtime_flag & PNL_FIRST)) {
517 /* draw embossed separator */
518 minx += 5.0f/block->aspect;
519 maxx -= 5.0f/block->aspect;
521 glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
522 fdrawline(minx, y, maxx, y);
523 glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
524 fdrawline(minx, y-1, maxx, y-1);
531 /* horizontal title */
532 if(!(panel->flag & PNL_CLOSEDX)) {
533 ui_draw_aligned_panel_header(style, block, &headrect, 'h');
535 /* itemrect smaller */
536 itemrect.xmax = headrect.xmax - 5.0f/block->aspect;
537 itemrect.xmin = itemrect.xmax - (headrect.ymax-headrect.ymin);
538 itemrect.ymin = headrect.ymin;
539 itemrect.ymax = headrect.ymax;
541 rectf_scale(&itemrect, 0.7f);
542 ui_draw_panel_dragwidget(&itemrect);
545 /* if the panel is minimized vertically:
548 if(panel->flag & PNL_CLOSEDY) {
551 else if(panel->flag & PNL_CLOSEDX) {
552 /* draw vertical title */
553 ui_draw_aligned_panel_header(style, block, &headrect, 'v');
558 /* in some occasions, draw a border */
559 if(panel->flag & PNL_SELECT) {
560 if(panel->control & UI_PNL_SOLID) uiSetRoundBox(UI_CNR_ALL);
561 else uiSetRoundBox(UI_CNR_NONE);
563 UI_ThemeColorShade(TH_BACK, -120);
564 uiRoundRect(0.5f + rect->xmin, 0.5f + rect->ymin, 0.5f + rect->xmax, 0.5f + headrect.ymax+1, 8);
567 if(panel->control & UI_PNL_SCALE)
568 ui_draw_panel_scalewidget(rect);
571 /* draw optional close icon */
574 if(panel->control & UI_PNL_CLOSE) {
576 UI_ThemeColor(TH_TEXT);
577 ui_draw_x_icon(rect->xmin+2+ofsx, rect->ymax+2);
581 /* draw collapse icon */
582 UI_ThemeColor(TH_TEXT);
584 /* itemrect smaller */
585 itemrect.xmin = headrect.xmin + 5.0f/block->aspect;
586 itemrect.xmax = itemrect.xmin + (headrect.ymax-headrect.ymin);
587 itemrect.ymin = headrect.ymin;
588 itemrect.ymax = headrect.ymax;
590 rectf_scale(&itemrect, 0.35f);
592 if(panel->flag & PNL_CLOSEDY)
593 ui_draw_tria_rect(&itemrect, 'h');
594 else if(panel->flag & PNL_CLOSEDX)
595 ui_draw_tria_rect(&itemrect, 'h');
597 ui_draw_tria_rect(&itemrect, 'v');
602 /************************** panel alignment *************************/
604 static int get_panel_header(Panel *pa)
606 if(pa->type && (pa->type->flag & PNL_NO_HEADER))
612 static int get_panel_size_y(Panel *pa)
614 if(pa->type && (pa->type->flag & PNL_NO_HEADER))
617 return PNL_HEADER + pa->sizey;
620 /* this function is needed because uiBlock and Panel itself don't
621 * change sizey or location when closed */
622 static int get_panel_real_ofsy(Panel *pa)
624 if(pa->flag & PNL_CLOSEDY) return pa->ofsy+pa->sizey;
625 else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDY)) return pa->ofsy+pa->sizey;
626 else if(pa->paneltab) return pa->paneltab->ofsy;
627 else return pa->ofsy;
630 static int get_panel_real_ofsx(Panel *pa)
632 if(pa->flag & PNL_CLOSEDX) return pa->ofsx+get_panel_header(pa);
633 else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDX)) return pa->ofsx+get_panel_header(pa);
634 else return pa->ofsx+pa->sizex;
637 typedef struct PanelSort {
641 /* note about sorting;
642 * the sortorder has a lower value for new panels being added.
643 * however, that only works to insert a single panel, when more new panels get
644 * added the coordinates of existing panels and the previously stored to-be-inserted
645 * panels do not match for sorting */
647 static int find_leftmost_panel(const void *a1, const void *a2)
649 const PanelSort *ps1=a1, *ps2=a2;
651 if(ps1->pa->ofsx > ps2->pa->ofsx) return 1;
652 else if(ps1->pa->ofsx < ps2->pa->ofsx) return -1;
653 else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
654 else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
660 static int find_highest_panel(const void *a1, const void *a2)
662 const PanelSort *ps1=a1, *ps2=a2;
664 /* stick uppermost header-less panels to the top of the region -
665 * prevent them from being sorted */
666 if (ps1->pa->sortorder < ps2->pa->sortorder && ps1->pa->type->flag & PNL_NO_HEADER) return -1;
668 if(ps1->pa->ofsy+ps1->pa->sizey < ps2->pa->ofsy+ps2->pa->sizey) return 1;
669 else if(ps1->pa->ofsy+ps1->pa->sizey > ps2->pa->ofsy+ps2->pa->sizey) return -1;
670 else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
671 else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
676 static int compare_panel(const void *a1, const void *a2)
678 const PanelSort *ps1=a1, *ps2=a2;
680 if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
681 else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
686 /* this doesnt draw */
687 /* returns 1 when it did something */
688 static int uiAlignPanelStep(ScrArea *sa, ARegion *ar, float fac, int drag)
691 PanelSort *ps, *panelsort, *psnext;
693 int align= panel_aligned(sa, ar);
695 /* count active, not tabbed panels */
696 for(pa= ar->panels.first; pa; pa= pa->next)
697 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL)
702 /* extra; change close direction? */
703 for(pa= ar->panels.first; pa; pa= pa->next) {
704 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
705 if((pa->flag & PNL_CLOSEDX) && (align==BUT_VERTICAL))
706 pa->flag ^= PNL_CLOSED;
707 else if((pa->flag & PNL_CLOSEDY) && (align==BUT_HORIZONTAL))
708 pa->flag ^= PNL_CLOSED;
713 panelsort= MEM_callocN(tot*sizeof(PanelSort), "panelsort");
716 for(pa= ar->panels.first; pa; pa= pa->next) {
717 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
718 ps->pa= MEM_dupallocN(pa);
725 /* while we are dragging, we sort on location and update sortorder */
726 if(align==BUT_VERTICAL)
727 qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
729 qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
731 for(ps=panelsort, a=0; a<tot; a++, ps++)
732 ps->orig->sortorder= a;
735 /* otherwise use sortorder */
736 qsort(panelsort, tot, sizeof(PanelSort), compare_panel);
738 /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
741 ps->pa->ofsy= -get_panel_size_y(ps->pa);
743 for(a=0; a<tot-1; a++, ps++) {
746 if(align==BUT_VERTICAL) {
747 psnext->pa->ofsx= ps->pa->ofsx;
748 psnext->pa->ofsy= get_panel_real_ofsy(ps->pa) - get_panel_size_y(psnext->pa);
751 psnext->pa->ofsx= get_panel_real_ofsx(ps->pa);
752 psnext->pa->ofsy= ps->pa->ofsy + get_panel_size_y(ps->pa) - get_panel_size_y(psnext->pa);
759 for(a=0; a<tot; a++, ps++) {
760 if((ps->pa->flag & PNL_SELECT)==0) {
761 if((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) {
762 ps->orig->ofsx= floorf(0.5f + fac*(float)ps->pa->ofsx + (1.0f-fac)*(float)ps->orig->ofsx);
763 ps->orig->ofsy= floorf(0.5f + fac*(float)ps->pa->ofsy + (1.0f-fac)*(float)ps->orig->ofsy);
769 /* copy locations to tabs */
770 for(pa= ar->panels.first; pa; pa= pa->next)
771 if(pa->paneltab && (pa->runtime_flag & PNL_ACTIVE))
772 ui_panel_copy_offset(pa, pa->paneltab);
774 /* free panelsort array */
775 for (ps = panelsort, a = 0; a < tot; a++, ps++) {
778 MEM_freeN(panelsort);
783 static void ui_panels_size(ScrArea *sa, ARegion *ar, int *x, int *y)
786 int align= panel_aligned(sa, ar);
787 int sizex = UI_PANEL_WIDTH;
788 int sizey = UI_PANEL_WIDTH;
790 /* compute size taken up by panels, for setting in view2d */
791 for(pa= ar->panels.first; pa; pa= pa->next) {
792 if(pa->runtime_flag & PNL_ACTIVE) {
793 int pa_sizex, pa_sizey;
795 if(align==BUT_VERTICAL) {
796 pa_sizex= pa->ofsx + pa->sizex;
797 pa_sizey= get_panel_real_ofsy(pa);
800 pa_sizex= get_panel_real_ofsx(pa) + pa->sizex;
801 pa_sizey= pa->ofsy + get_panel_size_y(pa);
804 sizex= MAX2(sizex, pa_sizex);
805 sizey= MIN2(sizey, pa_sizey);
813 static void ui_do_animate(const bContext *C, Panel *panel)
815 uiHandlePanelData *data= panel->activedata;
816 ScrArea *sa= CTX_wm_area(C);
817 ARegion *ar= CTX_wm_region(C);
820 fac= (PIL_check_seconds_timer()-data->starttime)/ANIMATION_TIME;
822 fac= MIN2(fac, 1.0f);
824 /* for max 1 second, interpolate positions */
825 if(uiAlignPanelStep(sa, ar, fac, 0))
826 ED_region_tag_redraw(ar);
831 panel_activate_state(C, panel, PANEL_STATE_EXIT);
836 void uiBeginPanels(const bContext *UNUSED(C), ARegion *ar)
840 /* set all panels as inactive, so that at the end we know
841 * which ones were used */
842 for(pa=ar->panels.first; pa; pa=pa->next) {
843 if(pa->runtime_flag & PNL_ACTIVE)
844 pa->runtime_flag= PNL_WAS_ACTIVE;
850 /* only draws blocks with panels */
851 void uiEndPanels(const bContext *C, ARegion *ar, int *x, int *y)
853 ScrArea *sa= CTX_wm_area(C);
855 Panel *panot, *panew, *patest, *pa, *firstpa;
857 /* offset contents */
858 for(block= ar->uiblocks.first; block; block= block->next)
859 if(block->active && block->panel)
860 ui_offset_panel_block(block);
862 /* consistency; are panels not made, whilst they have tabs */
863 for(panot= ar->panels.first; panot; panot= panot->next) {
864 if((panot->runtime_flag & PNL_ACTIVE)==0) { // not made
866 for(panew= ar->panels.first; panew; panew= panew->next) {
867 if((panew->runtime_flag & PNL_ACTIVE)) {
868 if(panew->paneltab==panot) { // panew is tab in notmade pa
873 /* now panew can become the new parent, check all other tabs */
875 for(patest= ar->panels.first; patest; patest= patest->next) {
876 if(patest->paneltab == panot) {
877 patest->paneltab= panew;
880 panot->paneltab= panew;
881 panew->paneltab= NULL;
882 ED_region_tag_redraw(ar); // the buttons panew were not made
887 /* re-align, possibly with animation */
888 if(panels_re_align(sa, ar, &pa)) {
890 panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
892 uiAlignPanelStep(sa, ar, 1.0, 0);
895 /* tag first panel */
897 for(block= ar->uiblocks.first; block; block=block->next)
898 if(block->active && block->panel)
899 if(!firstpa || block->panel->sortorder < firstpa->sortorder)
900 firstpa= block->panel;
903 firstpa->runtime_flag |= PNL_FIRST;
905 /* compute size taken up by panel */
906 ui_panels_size(sa, ar, x, y);
909 void uiDrawPanels(const bContext *C, ARegion *ar)
913 UI_ThemeClearColor(TH_BACK);
915 /* draw panels, selected on top */
916 for(block= ar->uiblocks.first; block; block=block->next) {
917 if(block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
918 uiDrawBlock(C, block);
922 for(block= ar->uiblocks.first; block; block=block->next) {
923 if(block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
924 uiDrawBlock(C, block);
929 /* ------------ panel merging ---------------- */
931 static void check_panel_overlap(ARegion *ar, Panel *panel)
935 /* also called with panel==NULL for clear */
937 for(pa=ar->panels.first; pa; pa=pa->next) {
938 pa->flag &= ~PNL_OVERLAP;
939 if(panel && (pa != panel)) {
940 if(pa->paneltab==NULL && (pa->runtime_flag & PNL_ACTIVE)) {
941 float safex= 0.2, safey= 0.2;
943 if(pa->flag & PNL_CLOSEDX) safex= 0.05;
944 else if(pa->flag & PNL_CLOSEDY) safey= 0.05;
945 else if(panel->flag & PNL_CLOSEDX) safex= 0.05;
946 else if(panel->flag & PNL_CLOSEDY) safey= 0.05;
948 if(pa->ofsx > panel->ofsx- safex*panel->sizex)
949 if(pa->ofsx+pa->sizex < panel->ofsx+ (1.0f+safex)*panel->sizex)
950 if(pa->ofsy > panel->ofsy- safey*panel->sizey)
951 if(pa->ofsy+pa->sizey < panel->ofsy+ (1.0f+safey)*panel->sizey)
952 pa->flag |= PNL_OVERLAP;
958 /************************ panel dragging ****************************/
960 static void ui_do_drag(const bContext *C, wmEvent *event, Panel *panel)
962 uiHandlePanelData *data= panel->activedata;
963 ScrArea *sa= CTX_wm_area(C);
964 ARegion *ar= CTX_wm_region(C);
965 short align= panel_aligned(sa, ar), dx=0, dy=0;
967 /* first clip for window, no dragging outside */
968 if(!BLI_in_rcti(&ar->winrct, event->x, event->y))
971 dx= (event->x-data->startx) & ~(PNL_GRID-1);
972 dy= (event->y-data->starty) & ~(PNL_GRID-1);
974 dx *= (float)(ar->v2d.cur.xmax - ar->v2d.cur.xmin)/(float)(ar->winrct.xmax - ar->winrct.xmin);
975 dy *= (float)(ar->v2d.cur.ymax - ar->v2d.cur.ymin)/(float)(ar->winrct.ymax - ar->winrct.ymin);
977 if(data->state == PANEL_STATE_DRAG_SCALE) {
978 panel->sizex = MAX2(data->startsizex+dx, UI_PANEL_MINX);
980 if(data->startsizey-dy < UI_PANEL_MINY)
981 dy= -UI_PANEL_MINY+data->startsizey;
983 panel->sizey= data->startsizey-dy;
984 panel->ofsy= data->startofsy+dy;
987 /* reset the panel snapping, to allow dragging away from snapped edges */
988 panel->snap = PNL_SNAP_NONE;
990 panel->ofsx = data->startofsx+dx;
991 panel->ofsy = data->startofsy+dy;
992 check_panel_overlap(ar, panel);
994 if(align) uiAlignPanelStep(sa, ar, 0.2, 1);
997 ED_region_tag_redraw(ar);
1000 /******************* region level panel interaction *****************/
1003 /* this function is supposed to call general window drawing too */
1004 /* also it supposes a block has panel, and isn't a menu */
1005 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my, int event)
1007 ScrArea *sa= CTX_wm_area(C);
1008 ARegion *ar= CTX_wm_region(C);
1010 int align= panel_aligned(sa, ar), button= 0;
1012 /* mouse coordinates in panel space! */
1014 /* XXX weak code, currently it assumes layout style for location of widgets */
1016 /* check open/collapsed button */
1019 else if(event==AKEY)
1021 else if(block->panel->flag & PNL_CLOSEDX) {
1022 if(my >= block->maxy) button= 1;
1024 else if(block->panel->control & UI_PNL_CLOSE) {
1025 /* whole of header can be used to collapse panel (except top-right corner) */
1026 if(mx <= block->maxx-8-PNL_ICON) button= 2;
1027 //else if(mx <= block->minx+10+2*PNL_ICON+2) button= 1;
1029 else if(mx <= block->maxx-PNL_ICON-12) {
1034 if(button==2) { // close
1035 ED_region_tag_redraw(ar);
1038 if(block->panel->flag & PNL_CLOSED) {
1039 block->panel->flag &= ~PNL_CLOSED;
1040 /* snap back up so full panel aligns with screen edge */
1041 if (block->panel->snap & PNL_SNAP_BOTTOM)
1042 block->panel->ofsy= 0;
1044 else if(align==BUT_HORIZONTAL) {
1045 block->panel->flag |= PNL_CLOSEDX;
1048 /* snap down to bottom screen edge*/
1049 block->panel->flag |= PNL_CLOSEDY;
1050 if (block->panel->snap & PNL_SNAP_BOTTOM)
1051 block->panel->ofsy= -block->panel->sizey;
1054 for(pa= ar->panels.first; pa; pa= pa->next) {
1055 if(pa->paneltab==block->panel) {
1056 if(block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1057 else pa->flag &= ~PNL_CLOSED;
1063 panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1065 ED_region_tag_redraw(ar);
1067 else if(mx <= (block->maxx-PNL_ICON-12)+PNL_ICON+2) {
1068 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1072 /* XXX should become modal keymap */
1073 /* AKey is opening/closing panels, independent of button state now */
1075 int ui_handler_panel_region(bContext *C, wmEvent *event)
1077 ARegion *ar= CTX_wm_region(C);
1080 int retval, mx, my, inside_header= 0, inside_scale= 0, inside;
1082 retval= WM_UI_HANDLER_CONTINUE;
1083 for(block=ar->uiblocks.last; block; block=block->prev) {
1086 ui_window_to_block(ar, block, &mx, &my);
1088 /* check if inside boundbox */
1092 if(!pa || pa->paneltab!=NULL)
1094 if(pa->type && pa->type->flag & PNL_NO_HEADER) // XXX - accessed freed panels when scripts reload, need to fix.
1097 if(block->minx <= mx && block->maxx >= mx)
1098 if(block->miny <= my && block->maxy+PNL_HEADER >= my)
1101 if(inside && event->val==KM_PRESS) {
1102 if(event->type == AKEY && !ELEM4(KM_MOD_FIRST, event->ctrl, event->oskey, event->shift, event->alt)) {
1104 if(pa->flag & PNL_CLOSEDY) {
1105 if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my))
1106 ui_handle_panel_header(C, block, mx, my, event->type);
1109 ui_handle_panel_header(C, block, mx, my, event->type);
1115 /* on active button, do not handle panels */
1116 if(ui_button_is_active(ar))
1120 /* clicked at panel header? */
1121 if(pa->flag & PNL_CLOSEDX) {
1122 if(block->minx <= mx && block->minx+PNL_HEADER >= mx)
1125 else if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my)) {
1128 else if(pa->control & UI_PNL_SCALE) {
1129 if(block->maxx-PNL_HEADER <= mx)
1130 if(block->miny+PNL_HEADER >= my)
1134 if(event->val==KM_PRESS) {
1135 /* open close on header */
1136 if(ELEM(event->type, RETKEY, PADENTER)) {
1138 ui_handle_panel_header(C, block, mx, my, RETKEY);
1142 else if(event->type == LEFTMOUSE) {
1144 ui_handle_panel_header(C, block, mx, my, 0);
1147 else if(inside_scale && !(pa->flag & PNL_CLOSED)) {
1148 panel_activate_state(C, pa, PANEL_STATE_DRAG_SCALE);
1152 else if(event->type == ESCKEY) {
1155 if(block->handler) {
1156 rem_blockhandler(sa, block->handler);
1157 ED_region_tag_redraw(ar);
1158 retval= WM_UI_HANDLER_BREAK;
1162 else if(event->type==PADPLUSKEY || event->type==PADMINUS) {
1163 #if 0 // XXX make float panel exception?
1166 /* if panel is closed, only zoom if mouse is over the header */
1167 if (pa->flag & (PNL_CLOSEDX|PNL_CLOSEDY)) {
1175 ScrArea *sa= CTX_wm_area(C);
1176 SpaceLink *sl= sa->spacedata.first;
1178 if(sa->spacetype!=SPACE_BUTS) {
1179 if(!(pa->control & UI_PNL_SCALE)) {
1180 if(event->type==PADPLUSKEY) sl->blockscale+= 0.1;
1181 else sl->blockscale-= 0.1;
1182 CLAMP(sl->blockscale, 0.6, 1.0);
1184 ED_region_tag_redraw(ar);
1185 retval= WM_UI_HANDLER_BREAK;
1198 /**************** window level modal panel interaction **************/
1200 /* note, this is modal handler and should not swallow events for animation */
1201 static int ui_handler_panel(bContext *C, wmEvent *event, void *userdata)
1203 Panel *panel= userdata;
1204 uiHandlePanelData *data= panel->activedata;
1206 /* verify if we can stop */
1207 if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1208 ScrArea *sa= CTX_wm_area(C);
1209 ARegion *ar= CTX_wm_region(C);
1210 int align= panel_aligned(sa, ar);
1213 panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1215 panel_activate_state(C, panel, PANEL_STATE_EXIT);
1217 else if(event->type == MOUSEMOVE) {
1218 if(data->state == PANEL_STATE_DRAG)
1219 ui_do_drag(C, event, panel);
1221 else if(event->type == TIMER && event->customdata == data->animtimer) {
1222 if(data->state == PANEL_STATE_ANIMATION)
1223 ui_do_animate(C, panel);
1224 else if(data->state == PANEL_STATE_DRAG)
1225 ui_do_drag(C, event, panel);
1228 data= panel->activedata;
1230 if(data && data->state == PANEL_STATE_ANIMATION)
1231 return WM_UI_HANDLER_CONTINUE;
1233 return WM_UI_HANDLER_BREAK;
1236 static void ui_handler_remove_panel(bContext *C, void *userdata)
1238 Panel *pa= userdata;
1240 panel_activate_state(C, pa, PANEL_STATE_EXIT);
1243 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
1245 uiHandlePanelData *data= pa->activedata;
1246 wmWindow *win= CTX_wm_window(C);
1247 ARegion *ar= CTX_wm_region(C);
1249 if(data && data->state == state)
1252 if(state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1253 if(data && data->state != PANEL_STATE_ANIMATION) {
1255 * - the panel tabbing function call below (test_add_new_tabs()) has been commented out
1256 * "It is too easy to do by accident when reordering panels, is very hard to control and use, and has no real benefit." - BillRey
1257 * Aligorith, 2009Sep
1259 //test_add_new_tabs(ar); // also copies locations of tabs in dragged panel
1260 check_panel_overlap(ar, NULL); // clears
1263 pa->flag &= ~PNL_SELECT;
1266 pa->flag |= PNL_SELECT;
1268 if(data && data->animtimer) {
1269 WM_event_remove_timer(CTX_wm_manager(C), win, data->animtimer);
1270 data->animtimer= NULL;
1273 if(state == PANEL_STATE_EXIT) {
1275 pa->activedata= NULL;
1277 WM_event_remove_ui_handler(&win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa, 0);
1281 data= MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1282 pa->activedata= data;
1284 WM_event_add_ui_handler(C, &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa);
1287 if(ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1288 data->animtimer= WM_event_add_timer(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
1291 data->startx= win->eventstate->x;
1292 data->starty= win->eventstate->y;
1293 data->startofsx= pa->ofsx;
1294 data->startofsy= pa->ofsy;
1295 data->startsizex= pa->sizex;
1296 data->startsizey= pa->sizey;
1297 data->starttime= PIL_check_seconds_timer();
1300 ED_region_tag_redraw(ar);
1302 /* XXX exception handling, 3d window preview panel */
1304 if(block->drawextra==BIF_view3d_previewdraw)
1305 BIF_view3d_previewrender_clear(curarea);
1308 /* XXX exception handling, 3d window preview panel */
1310 if(block->drawextra==BIF_view3d_previewdraw)
1311 BIF_view3d_previewrender_signal(curarea, PR_DISPRECT);
1312 else if(strcmp(block->name, "image_panel_preview")==0)
1313 image_preview_event(2);