4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation, 2003-2009 full recode.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/interface/interface_panel.c
29 * \ingroup edinterface
33 /* a full doc with API notes can be found in bf-blender/trunk/blender/doc/guides/interface_API.txt */
40 #include "MEM_guardedalloc.h"
44 #include "BLI_blenlib.h"
46 #include "BLI_utildefines.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"
63 #include "interface_intern.h"
65 /*********************** defines and structs ************************/
67 #define ANIMATION_TIME 0.30
68 #define ANIMATION_INTERVAL 0.02
70 #define PNL_LAST_ADDED 1
72 #define PNL_WAS_ACTIVE 4
73 #define PNL_ANIM_ALIGN 8
74 #define PNL_NEW_ADDED 16
77 typedef enum uiHandlePanelState {
79 PANEL_STATE_DRAG_SCALE,
80 PANEL_STATE_WAIT_UNTAB,
81 PANEL_STATE_ANIMATION,
85 typedef struct uiHandlePanelData {
86 uiHandlePanelState state;
94 int startofsx, startofsy;
95 int startsizex, startsizey;
98 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state);
100 /*********************** space specific code ************************/
101 /* temporary code to remove all sbuts stuff from panel code */
103 static int panel_aligned(ScrArea *sa, ARegion *ar)
105 if(sa->spacetype==SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
106 SpaceButs *sbuts= sa->spacedata.first;
109 else if(sa->spacetype==SPACE_USERPREF && ar->regiontype == RGN_TYPE_WINDOW)
111 else if(sa->spacetype==SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS)
113 else if(sa->spacetype==SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW)
115 else if(ELEM3(ar->regiontype, RGN_TYPE_UI, RGN_TYPE_TOOLS, RGN_TYPE_TOOL_PROPS))
121 static int panels_re_align(ScrArea *sa, ARegion *ar, Panel **r_pa)
128 if(sa->spacetype==SPACE_BUTS && ar->regiontype == RGN_TYPE_WINDOW) {
129 SpaceButs *sbuts= sa->spacedata.first;
132 if(sbuts->re_align || sbuts->mainbo!=sbuts->mainb)
135 else if(ar->regiontype==RGN_TYPE_UI)
137 else if(sa->spacetype==SPACE_IMAGE && ar->regiontype == RGN_TYPE_PREVIEW)
139 else if(sa->spacetype==SPACE_FILE && ar->regiontype == RGN_TYPE_CHANNELS)
142 /* in case panel is added or disappears */
143 for(pa=ar->panels.first; pa; pa=pa->next) {
144 if((pa->runtime_flag & PNL_WAS_ACTIVE) && !(pa->runtime_flag & PNL_ACTIVE))
146 if(!(pa->runtime_flag & PNL_WAS_ACTIVE) && (pa->runtime_flag & PNL_ACTIVE))
152 /* in case we need to do an animation (size changes) */
153 for(pa=ar->panels.first; pa; pa=pa->next) {
154 if(pa->runtime_flag & PNL_ANIM_ALIGN) {
164 /****************************** panels ******************************/
166 static void ui_panel_copy_offset(Panel *pa, Panel *papar)
168 /* with respect to sizes... papar is parent */
170 pa->ofsx= papar->ofsx;
171 pa->ofsy= papar->ofsy + papar->sizey-pa->sizey;
174 Panel *uiBeginPanel(ScrArea *sa, ARegion *ar, uiBlock *block, PanelType *pt, int *open)
176 uiStyle *style= UI_GetStyle();
177 Panel *pa, *patab, *palast, *panext;
178 char *drawname= pt->label;
179 char *idname= pt->idname;
180 char *tabname= pt->idname;
181 char *hookname= NULL;
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;
211 pa->ofsy= style->panelouter;
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 /* NOTE - this seems to be only used for hiding nodes now */
334 void UI_DrawTriIcon(float x, float y, char dir)
337 ui_draw_anti_tria( x-3, y-5, x-3, y+5, x+7,y );
340 ui_draw_anti_tria( x-5, y-7, x+5, y-7, x, y+3);
342 else { /* 'v' = vertical, down */
343 ui_draw_anti_tria( x-5, y+3, x+5, y+3, x, y-7);
347 /* triangle 'icon' inside rect */
348 static void ui_draw_tria_rect(rctf *rect, char dir)
351 float half= 0.5f*(rect->ymax - rect->ymin);
352 ui_draw_anti_tria(rect->xmin, rect->ymin, rect->xmin, rect->ymax, rect->xmax, rect->ymin+half);
355 float half= 0.5f*(rect->xmax - rect->xmin);
356 ui_draw_anti_tria(rect->xmin, rect->ymax, rect->xmax, rect->ymax, rect->xmin+half, rect->ymin);
360 static void ui_draw_anti_x(float x1, float y1, float x2, float y2)
363 /* set antialias line */
364 glEnable(GL_LINE_SMOOTH);
369 fdrawline(x1, y1, x2, y2);
370 fdrawline(x1, y2, x2, y1);
374 glDisable(GL_LINE_SMOOTH);
379 /* x 'icon' for panel header */
380 static void ui_draw_x_icon(float x, float y)
383 ui_draw_anti_x(x, y, x+9.375f, y+9.375f);
387 #define PNL_ICON UI_UNIT_X /* could be UI_UNIT_Y too */
389 static void ui_draw_panel_scalewidget(rcti *rect)
391 float xmin, xmax, dx;
392 float ymin, ymax, dy;
394 xmin= rect->xmax-PNL_HEADER+2;
397 ymax= rect->ymin+PNL_HEADER-2;
399 dx= 0.5f*(xmax-xmin);
400 dy= 0.5f*(ymax-ymin);
403 glColor4ub(255, 255, 255, 50);
404 fdrawline(xmin, ymin, xmax, ymax);
405 fdrawline(xmin+dx, ymin, xmax, ymax-dy);
407 glColor4ub(0, 0, 0, 50);
408 fdrawline(xmin, ymin+1, xmax, ymax+1);
409 fdrawline(xmin+dx, ymin+1, xmax, ymax-dy+1);
413 static void ui_draw_panel_dragwidget(rctf *rect)
415 float xmin, xmax, dx;
416 float ymin, ymax, dy;
423 dx= 0.333f*(xmax-xmin);
424 dy= 0.333f*(ymax-ymin);
427 glColor4ub(255, 255, 255, 20);
428 fdrawline(xmin, ymax, xmax, ymin);
429 fdrawline(xmin+dx, ymax, xmax, ymin+dy);
430 fdrawline(xmin+2*dx, ymax, xmax, ymin+2*dy);
432 glColor4ub(0, 0, 0, 20);
433 fdrawline(xmin, ymax+1, xmax, ymin+1);
434 fdrawline(xmin+dx, ymax+1, xmax, ymin+dy+1);
435 fdrawline(xmin+2*dx, ymax+1, xmax, ymin+2*dy+1);
440 static void ui_draw_aligned_panel_header(uiStyle *style, uiBlock *block, rcti *rect, char dir)
442 Panel *panel= block->panel;
445 const char *activename= IFACE_(panel->drawname[0] ? panel->drawname : panel->panelname);
447 /* + 0.001f to avoid flirting with float inaccuracy */
448 if(panel->control & UI_PNL_CLOSE) pnl_icons=(panel->labelofs+2*PNL_ICON+5)/block->aspect + 0.001f;
449 else pnl_icons= (panel->labelofs+PNL_ICON+5)/block->aspect + 0.001f;
452 /* draw text label */
453 UI_ThemeColor(TH_TITLE);
457 hrect.xmin= rect->xmin+pnl_icons;
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 Panel *panel= block->panel;
490 if(panel->paneltab) return;
491 if(panel->type && (panel->type->flag & PNL_NO_HEADER)) return;
493 /* calculate header rect */
494 /* + 0.001f to prevent flicker due to float inaccuracy */
496 headrect.ymin= headrect.ymax;
497 headrect.ymax= headrect.ymin + floor(PNL_HEADER/block->aspect + 0.001f);
500 float minx= rect->xmin/block->aspect;
501 float maxx= rect->xmax/block->aspect;
502 float y= headrect.ymax;
505 glColor4f(0.0f, 0.0f, 0.0f, 0.1f);
507 uiDrawBox(GL_POLYGON, minx, headrect.ymin, maxx, y+1, 4);
511 /* horizontal title */
512 if(!(panel->flag & PNL_CLOSEDX)) {
513 ui_draw_aligned_panel_header(style, block, &headrect, 'h');
515 /* itemrect smaller */
516 itemrect.xmax= headrect.xmax - 5.0f/block->aspect;
517 itemrect.xmin= itemrect.xmax - (headrect.ymax-headrect.ymin);
518 itemrect.ymin= headrect.ymin;
519 itemrect.ymax= headrect.ymax;
520 rectf_scale(&itemrect, 0.8f);
521 ui_draw_panel_dragwidget(&itemrect);
524 /* if the panel is minimized vertically:
527 if(panel->flag & PNL_CLOSEDY) {
530 else if(panel->flag & PNL_CLOSEDX) {
531 /* draw vertical title */
532 ui_draw_aligned_panel_header(style, block, &headrect, 'v');
537 /* in some occasions, draw a border */
538 if(panel->flag & PNL_SELECT) {
539 if(panel->control & UI_PNL_SOLID) uiSetRoundBox(UI_CNR_ALL);
540 else uiSetRoundBox(UI_CNR_NONE);
542 UI_ThemeColorShade(TH_BACK, -120);
543 uiRoundRect(0.5f + rect->xmin, 0.5f + rect->ymin, 0.5f + rect->xmax, 0.5f + headrect.ymax+1, 8);
546 if(panel->control & UI_PNL_SCALE)
547 ui_draw_panel_scalewidget(rect);
550 /* draw optional close icon */
553 if(panel->control & UI_PNL_CLOSE) {
555 UI_ThemeColor(TH_TEXT);
556 ui_draw_x_icon(rect->xmin+2+ofsx, rect->ymax+2);
560 /* draw collapse icon */
561 UI_ThemeColor(TH_TEXT);
563 /* itemrect smaller */
564 itemrect.xmin= headrect.xmin + 5.0f/block->aspect;
565 itemrect.xmax= itemrect.xmin + (headrect.ymax-headrect.ymin);
566 itemrect.ymin= headrect.ymin;
567 itemrect.ymax= headrect.ymax;
569 rectf_scale(&itemrect, 0.35f);
571 if(panel->flag & PNL_CLOSEDY)
572 ui_draw_tria_rect(&itemrect, 'h');
573 else if(panel->flag & PNL_CLOSEDX)
574 ui_draw_tria_rect(&itemrect, 'h');
576 ui_draw_tria_rect(&itemrect, 'v');
581 /************************** panel alignment *************************/
583 static int get_panel_header(Panel *pa)
585 if(pa->type && (pa->type->flag & PNL_NO_HEADER))
591 static int get_panel_size_y(uiStyle *style, Panel *pa)
593 if(pa->type && (pa->type->flag & PNL_NO_HEADER))
596 return PNL_HEADER + pa->sizey + style->panelouter;
599 /* this function is needed because uiBlock and Panel itself dont
600 change sizey or location when closed */
601 static int get_panel_real_ofsy(Panel *pa)
603 if(pa->flag & PNL_CLOSEDY) return pa->ofsy+pa->sizey;
604 else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDY)) return pa->ofsy+pa->sizey;
605 else if(pa->paneltab) return pa->paneltab->ofsy;
606 else return pa->ofsy;
609 static int get_panel_real_ofsx(Panel *pa)
611 if(pa->flag & PNL_CLOSEDX) return pa->ofsx+get_panel_header(pa);
612 else if(pa->paneltab && (pa->paneltab->flag & PNL_CLOSEDX)) return pa->ofsx+get_panel_header(pa);
613 else return pa->ofsx+pa->sizex;
616 typedef struct PanelSort {
620 /* note about sorting;
621 the sortorder has a lower value for new panels being added.
622 however, that only works to insert a single panel, when more new panels get
623 added the coordinates of existing panels and the previously stored to-be-insterted
624 panels do not match for sorting */
626 static int find_leftmost_panel(const void *a1, const void *a2)
628 const PanelSort *ps1=a1, *ps2=a2;
630 if(ps1->pa->ofsx > ps2->pa->ofsx) return 1;
631 else if(ps1->pa->ofsx < ps2->pa->ofsx) return -1;
632 else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
633 else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
639 static int find_highest_panel(const void *a1, const void *a2)
641 const PanelSort *ps1=a1, *ps2=a2;
643 /* stick uppermost header-less panels to the top of the region -
644 * prevent them from being sorted */
645 if (ps1->pa->sortorder < ps2->pa->sortorder && ps1->pa->type->flag & PNL_NO_HEADER) return -1;
647 if(ps1->pa->ofsy+ps1->pa->sizey < ps2->pa->ofsy+ps2->pa->sizey) return 1;
648 else if(ps1->pa->ofsy+ps1->pa->sizey > ps2->pa->ofsy+ps2->pa->sizey) return -1;
649 else if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
650 else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
655 static int compare_panel(const void *a1, const void *a2)
657 const PanelSort *ps1=a1, *ps2=a2;
659 if(ps1->pa->sortorder > ps2->pa->sortorder) return 1;
660 else if(ps1->pa->sortorder < ps2->pa->sortorder) return -1;
665 /* this doesnt draw */
666 /* returns 1 when it did something */
667 static int uiAlignPanelStep(ScrArea *sa, ARegion *ar, float fac, int drag)
669 uiStyle *style= UI_GetStyle();
671 PanelSort *ps, *panelsort, *psnext;
673 int align= panel_aligned(sa, ar);
675 /* count active, not tabbed panels */
676 for(pa= ar->panels.first; pa; pa= pa->next)
677 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL)
682 /* extra; change close direction? */
683 for(pa= ar->panels.first; pa; pa= pa->next) {
684 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
685 if((pa->flag & PNL_CLOSEDX) && (align==BUT_VERTICAL))
686 pa->flag ^= PNL_CLOSED;
687 else if((pa->flag & PNL_CLOSEDY) && (align==BUT_HORIZONTAL))
688 pa->flag ^= PNL_CLOSED;
693 panelsort= MEM_callocN(tot*sizeof(PanelSort), "panelsort");
696 for(pa= ar->panels.first; pa; pa= pa->next) {
697 if((pa->runtime_flag & PNL_ACTIVE) && pa->paneltab==NULL) {
698 ps->pa= MEM_dupallocN(pa);
705 /* while we are dragging, we sort on location and update sortorder */
706 if(align==BUT_VERTICAL)
707 qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
709 qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
711 for(ps=panelsort, a=0; a<tot; a++, ps++)
712 ps->orig->sortorder= a;
715 /* otherwise use sortorder */
716 qsort(panelsort, tot, sizeof(PanelSort), compare_panel);
718 /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
721 ps->pa->ofsy= -get_panel_size_y(style, ps->pa);
723 for(a=0; a<tot-1; a++, ps++) {
726 if(align==BUT_VERTICAL) {
727 psnext->pa->ofsx= ps->pa->ofsx;
728 psnext->pa->ofsy= get_panel_real_ofsy(ps->pa) - get_panel_size_y(style, psnext->pa);
731 psnext->pa->ofsx= get_panel_real_ofsx(ps->pa);
732 psnext->pa->ofsy= ps->pa->ofsy + get_panel_size_y(style, ps->pa) - get_panel_size_y(style, psnext->pa);
739 for(a=0; a<tot; a++, ps++) {
740 if((ps->pa->flag & PNL_SELECT)==0) {
741 if((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) {
742 ps->orig->ofsx= floorf(0.5f + fac*(float)ps->pa->ofsx + (1.0f-fac)*(float)ps->orig->ofsx);
743 ps->orig->ofsy= floorf(0.5f + fac*(float)ps->pa->ofsy + (1.0f-fac)*(float)ps->orig->ofsy);
749 /* copy locations to tabs */
750 for(pa= ar->panels.first; pa; pa= pa->next)
751 if(pa->paneltab && (pa->runtime_flag & PNL_ACTIVE))
752 ui_panel_copy_offset(pa, pa->paneltab);
754 /* free panelsort array */
755 for(ps= panelsort, a=0; a<tot; a++, ps++)
757 MEM_freeN(panelsort);
763 static void ui_do_animate(const bContext *C, Panel *panel)
765 uiHandlePanelData *data= panel->activedata;
766 ScrArea *sa= CTX_wm_area(C);
767 ARegion *ar= CTX_wm_region(C);
770 fac= (PIL_check_seconds_timer()-data->starttime)/ANIMATION_TIME;
772 fac= MIN2(fac, 1.0f);
774 /* for max 1 second, interpolate positions */
775 if(uiAlignPanelStep(sa, ar, fac, 0))
776 ED_region_tag_redraw(ar);
781 panel_activate_state(C, panel, PANEL_STATE_EXIT);
786 void uiBeginPanels(const bContext *UNUSED(C), ARegion *ar)
790 /* set all panels as inactive, so that at the end we know
791 * which ones were used */
792 for(pa=ar->panels.first; pa; pa=pa->next) {
793 if(pa->runtime_flag & PNL_ACTIVE)
794 pa->runtime_flag= PNL_WAS_ACTIVE;
800 /* only draws blocks with panels */
801 void uiEndPanels(const bContext *C, ARegion *ar)
803 ScrArea *sa= CTX_wm_area(C);
805 Panel *panot, *panew, *patest, *pa, *firstpa;
807 /* offset contents */
808 for(block= ar->uiblocks.first; block; block= block->next)
809 if(block->active && block->panel)
810 ui_offset_panel_block(block);
812 /* consistency; are panels not made, whilst they have tabs */
813 for(panot= ar->panels.first; panot; panot= panot->next) {
814 if((panot->runtime_flag & PNL_ACTIVE)==0) { // not made
816 for(panew= ar->panels.first; panew; panew= panew->next) {
817 if((panew->runtime_flag & PNL_ACTIVE)) {
818 if(panew->paneltab==panot) { // panew is tab in notmade pa
823 /* now panew can become the new parent, check all other tabs */
825 for(patest= ar->panels.first; patest; patest= patest->next) {
826 if(patest->paneltab == panot) {
827 patest->paneltab= panew;
830 panot->paneltab= panew;
831 panew->paneltab= NULL;
832 ED_region_tag_redraw(ar); // the buttons panew were not made
837 /* re-align, possibly with animation */
838 if(panels_re_align(sa, ar, &pa)) {
840 panel_activate_state(C, pa, PANEL_STATE_ANIMATION);
842 uiAlignPanelStep(sa, ar, 1.0, 0);
845 /* tag first panel */
847 for(block= ar->uiblocks.first; block; block=block->next)
848 if(block->active && block->panel)
849 if(!firstpa || block->panel->sortorder < firstpa->sortorder)
850 firstpa= block->panel;
853 firstpa->runtime_flag |= PNL_FIRST;
856 void uiDrawPanels(const bContext *C, ARegion *ar)
860 UI_ThemeClearColor(TH_BACK);
862 /* draw panels, selected on top */
863 for(block= ar->uiblocks.first; block; block=block->next) {
864 if(block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
865 uiDrawBlock(C, block);
869 for(block= ar->uiblocks.first; block; block=block->next) {
870 if(block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
871 uiDrawBlock(C, block);
876 /* ------------ panel merging ---------------- */
878 static void check_panel_overlap(ARegion *ar, Panel *panel)
882 /* also called with panel==NULL for clear */
884 for(pa=ar->panels.first; pa; pa=pa->next) {
885 pa->flag &= ~PNL_OVERLAP;
886 if(panel && (pa != panel)) {
887 if(pa->paneltab==NULL && (pa->runtime_flag & PNL_ACTIVE)) {
888 float safex= 0.2, safey= 0.2;
890 if(pa->flag & PNL_CLOSEDX) safex= 0.05;
891 else if(pa->flag & PNL_CLOSEDY) safey= 0.05;
892 else if(panel->flag & PNL_CLOSEDX) safex= 0.05;
893 else if(panel->flag & PNL_CLOSEDY) safey= 0.05;
895 if(pa->ofsx > panel->ofsx- safex*panel->sizex)
896 if(pa->ofsx+pa->sizex < panel->ofsx+ (1.0f+safex)*panel->sizex)
897 if(pa->ofsy > panel->ofsy- safey*panel->sizey)
898 if(pa->ofsy+pa->sizey < panel->ofsy+ (1.0f+safey)*panel->sizey)
899 pa->flag |= PNL_OVERLAP;
905 /************************ panel dragging ****************************/
907 static void ui_do_drag(const bContext *C, wmEvent *event, Panel *panel)
909 uiHandlePanelData *data= panel->activedata;
910 ScrArea *sa= CTX_wm_area(C);
911 ARegion *ar= CTX_wm_region(C);
912 short align= panel_aligned(sa, ar), dx=0, dy=0;
914 /* first clip for window, no dragging outside */
915 if(!BLI_in_rcti(&ar->winrct, event->x, event->y))
918 dx= (event->x-data->startx) & ~(PNL_GRID-1);
919 dy= (event->y-data->starty) & ~(PNL_GRID-1);
921 dx *= (float)(ar->v2d.cur.xmax - ar->v2d.cur.xmin)/(float)(ar->winrct.xmax - ar->winrct.xmin);
922 dy *= (float)(ar->v2d.cur.ymax - ar->v2d.cur.ymin)/(float)(ar->winrct.ymax - ar->winrct.ymin);
924 if(data->state == PANEL_STATE_DRAG_SCALE) {
925 panel->sizex = MAX2(data->startsizex+dx, UI_PANEL_MINX);
927 if(data->startsizey-dy < UI_PANEL_MINY)
928 dy= -UI_PANEL_MINY+data->startsizey;
930 panel->sizey= data->startsizey-dy;
931 panel->ofsy= data->startofsy+dy;
934 /* reset the panel snapping, to allow dragging away from snapped edges */
935 panel->snap = PNL_SNAP_NONE;
937 panel->ofsx = data->startofsx+dx;
938 panel->ofsy = data->startofsy+dy;
939 check_panel_overlap(ar, panel);
941 if(align) uiAlignPanelStep(sa, ar, 0.2, 1);
944 ED_region_tag_redraw(ar);
947 /******************* region level panel interaction *****************/
950 /* this function is supposed to call general window drawing too */
951 /* also it supposes a block has panel, and isnt a menu */
952 static void ui_handle_panel_header(const bContext *C, uiBlock *block, int mx, int my, int event)
954 ScrArea *sa= CTX_wm_area(C);
955 ARegion *ar= CTX_wm_region(C);
957 int align= panel_aligned(sa, ar), button= 0;
959 /* mouse coordinates in panel space! */
961 /* XXX weak code, currently it assumes layout style for location of widgets */
963 /* check open/collapsed button */
968 else if(block->panel->flag & PNL_CLOSEDX) {
969 if(my >= block->maxy) button= 1;
971 else if(block->panel->control & UI_PNL_CLOSE) {
972 /* whole of header can be used to collapse panel (except top-right corner) */
973 if(mx <= block->maxx-8-PNL_ICON) button= 2;
974 //else if(mx <= block->minx+10+2*PNL_ICON+2) button= 1;
976 else if(mx <= block->maxx-PNL_ICON-12) {
981 if(button==2) { // close
982 ED_region_tag_redraw(ar);
985 if(block->panel->flag & PNL_CLOSED) {
986 block->panel->flag &= ~PNL_CLOSED;
987 /* snap back up so full panel aligns with screen edge */
988 if (block->panel->snap & PNL_SNAP_BOTTOM)
989 block->panel->ofsy= 0;
991 else if(align==BUT_HORIZONTAL) {
992 block->panel->flag |= PNL_CLOSEDX;
995 /* snap down to bottom screen edge*/
996 block->panel->flag |= PNL_CLOSEDY;
997 if (block->panel->snap & PNL_SNAP_BOTTOM)
998 block->panel->ofsy= -block->panel->sizey;
1001 for(pa= ar->panels.first; pa; pa= pa->next) {
1002 if(pa->paneltab==block->panel) {
1003 if(block->panel->flag & PNL_CLOSED) pa->flag |= PNL_CLOSED;
1004 else pa->flag &= ~PNL_CLOSED;
1010 panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
1012 ED_region_tag_redraw(ar);
1014 else if(mx <= (block->maxx-PNL_ICON-12)+PNL_ICON+2) {
1015 panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
1019 /* XXX should become modal keymap */
1020 /* AKey is opening/closing panels, independent of button state now */
1022 int ui_handler_panel_region(bContext *C, wmEvent *event)
1024 ARegion *ar= CTX_wm_region(C);
1027 int retval, mx, my, inside_header= 0, inside_scale= 0, inside;
1029 retval= WM_UI_HANDLER_CONTINUE;
1030 for(block=ar->uiblocks.last; block; block=block->prev) {
1033 ui_window_to_block(ar, block, &mx, &my);
1035 /* check if inside boundbox */
1039 if(!pa || pa->paneltab!=NULL)
1041 if(pa->type && pa->type->flag & PNL_NO_HEADER) // XXX - accessed freed panels when scripts reload, need to fix.
1044 if(block->minx <= mx && block->maxx >= mx)
1045 if(block->miny <= my && block->maxy+PNL_HEADER >= my)
1048 if(inside && event->val==KM_PRESS) {
1049 if(event->type == AKEY && !ELEM4(KM_MOD_FIRST, event->ctrl, event->oskey, event->shift, event->alt)) {
1051 if(pa->flag & PNL_CLOSEDY) {
1052 if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my))
1053 ui_handle_panel_header(C, block, mx, my, event->type);
1056 ui_handle_panel_header(C, block, mx, my, event->type);
1062 /* on active button, do not handle panels */
1063 if(ui_button_is_active(ar))
1067 /* clicked at panel header? */
1068 if(pa->flag & PNL_CLOSEDX) {
1069 if(block->minx <= mx && block->minx+PNL_HEADER >= mx)
1072 else if((block->maxy <= my) && (block->maxy+PNL_HEADER >= my)) {
1075 else if(pa->control & UI_PNL_SCALE) {
1076 if(block->maxx-PNL_HEADER <= mx)
1077 if(block->miny+PNL_HEADER >= my)
1081 if(event->val==KM_PRESS) {
1082 /* open close on header */
1083 if(ELEM(event->type, RETKEY, PADENTER)) {
1085 ui_handle_panel_header(C, block, mx, my, RETKEY);
1089 else if(event->type == LEFTMOUSE) {
1091 ui_handle_panel_header(C, block, mx, my, 0);
1094 else if(inside_scale && !(pa->flag & PNL_CLOSED)) {
1095 panel_activate_state(C, pa, PANEL_STATE_DRAG_SCALE);
1099 else if(event->type == ESCKEY) {
1100 /*XXX 2.50 if(block->handler) {
1101 rem_blockhandler(sa, block->handler);
1102 ED_region_tag_redraw(ar);
1103 retval= WM_UI_HANDLER_BREAK;
1106 else if(event->type==PADPLUSKEY || event->type==PADMINUS) {
1107 #if 0 // XXX make float panel exception?
1110 /* if panel is closed, only zoom if mouse is over the header */
1111 if (pa->flag & (PNL_CLOSEDX|PNL_CLOSEDY)) {
1119 ScrArea *sa= CTX_wm_area(C);
1120 SpaceLink *sl= sa->spacedata.first;
1122 if(sa->spacetype!=SPACE_BUTS) {
1123 if(!(pa->control & UI_PNL_SCALE)) {
1124 if(event->type==PADPLUSKEY) sl->blockscale+= 0.1;
1125 else sl->blockscale-= 0.1;
1126 CLAMP(sl->blockscale, 0.6, 1.0);
1128 ED_region_tag_redraw(ar);
1129 retval= WM_UI_HANDLER_BREAK;
1142 /**************** window level modal panel interaction **************/
1144 /* note, this is modal handler and should not swallow events for animation */
1145 static int ui_handler_panel(bContext *C, wmEvent *event, void *userdata)
1147 Panel *panel= userdata;
1148 uiHandlePanelData *data= panel->activedata;
1150 /* verify if we can stop */
1151 if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1152 ScrArea *sa= CTX_wm_area(C);
1153 ARegion *ar= CTX_wm_region(C);
1154 int align= panel_aligned(sa, ar);
1157 panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
1159 panel_activate_state(C, panel, PANEL_STATE_EXIT);
1161 else if(event->type == MOUSEMOVE) {
1162 if(data->state == PANEL_STATE_DRAG)
1163 ui_do_drag(C, event, panel);
1165 else if(event->type == TIMER && event->customdata == data->animtimer) {
1166 if(data->state == PANEL_STATE_ANIMATION)
1167 ui_do_animate(C, panel);
1168 else if(data->state == PANEL_STATE_DRAG)
1169 ui_do_drag(C, event, panel);
1172 data= panel->activedata;
1174 if(data && data->state == PANEL_STATE_ANIMATION)
1175 return WM_UI_HANDLER_CONTINUE;
1177 return WM_UI_HANDLER_BREAK;
1180 static void ui_handler_remove_panel(bContext *C, void *userdata)
1182 Panel *pa= userdata;
1184 panel_activate_state(C, pa, PANEL_STATE_EXIT);
1187 static void panel_activate_state(const bContext *C, Panel *pa, uiHandlePanelState state)
1189 uiHandlePanelData *data= pa->activedata;
1190 wmWindow *win= CTX_wm_window(C);
1191 ARegion *ar= CTX_wm_region(C);
1193 if(data && data->state == state)
1196 if(state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
1197 if(data && data->state != PANEL_STATE_ANIMATION) {
1199 * - the panel tabbing function call below (test_add_new_tabs()) has been commented out
1200 * "It is too easy to do by accident when reordering panels, is very hard to control and use, and has no real benefit." - BillRey
1201 * Aligorith, 2009Sep
1203 //test_add_new_tabs(ar); // also copies locations of tabs in dragged panel
1204 check_panel_overlap(ar, NULL); // clears
1207 pa->flag &= ~PNL_SELECT;
1210 pa->flag |= PNL_SELECT;
1212 if(data && data->animtimer) {
1213 WM_event_remove_timer(CTX_wm_manager(C), win, data->animtimer);
1214 data->animtimer= NULL;
1217 if(state == PANEL_STATE_EXIT) {
1219 pa->activedata= NULL;
1221 WM_event_remove_ui_handler(&win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa, 0);
1225 data= MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
1226 pa->activedata= data;
1228 WM_event_add_ui_handler(C, &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, pa);
1231 if(ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG))
1232 data->animtimer= WM_event_add_timer(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
1235 data->startx= win->eventstate->x;
1236 data->starty= win->eventstate->y;
1237 data->startofsx= pa->ofsx;
1238 data->startofsy= pa->ofsy;
1239 data->startsizex= pa->sizex;
1240 data->startsizey= pa->sizey;
1241 data->starttime= PIL_check_seconds_timer();
1244 ED_region_tag_redraw(ar);
1246 /* XXX exception handling, 3d window preview panel */
1247 /* if(block->drawextra==BIF_view3d_previewdraw)
1248 BIF_view3d_previewrender_clear(curarea);*/
1250 /* XXX exception handling, 3d window preview panel */
1251 /* if(block->drawextra==BIF_view3d_previewdraw)
1252 BIF_view3d_previewrender_signal(curarea, PR_DISPRECT);
1253 else if(strcmp(block->name, "image_panel_preview")==0)
1254 image_preview_event(2); */