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) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/editors/screen/area.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_userdef_types.h"
41 #include "BLI_blenlib.h"
44 #include "BLI_utildefines.h"
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_screen.h"
52 #include "wm_subwindow.h"
54 #include "ED_screen.h"
55 #include "ED_screen_types.h"
56 #include "ED_space_api.h"
58 #include "ED_fileselect.h"
61 #include "BIF_glutil.h"
64 #include "UI_interface.h"
65 #include "UI_resources.h"
66 #include "UI_view2d.h"
68 #include "screen_intern.h"
70 /* general area and region code */
72 static void region_draw_emboss(ARegion *ar, rcti *scirct)
76 /* translate scissor rect to region space */
77 rect.xmin= scirct->xmin - ar->winrct.xmin;
78 rect.ymin= scirct->ymin - ar->winrct.ymin;
79 rect.xmax= scirct->xmax - ar->winrct.xmin;
80 rect.ymax= scirct->ymax - ar->winrct.ymin;
84 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
87 glColor4ub(0,0,0, 50);
88 sdrawline(rect.xmax, rect.ymin, rect.xmax, rect.ymax);
91 glColor4ub(0,0,0, 80);
92 sdrawline(rect.xmin, rect.ymin, rect.xmax, rect.ymin);
95 glColor4ub(255,255,255, 60);
96 sdrawline(rect.xmin, rect.ymax, rect.xmax, rect.ymax);
99 glColor4ub(255,255,255, 50);
100 sdrawline(rect.xmin, rect.ymin, rect.xmin, rect.ymax);
102 glDisable( GL_BLEND );
105 void ED_region_pixelspace(ARegion *ar)
107 int width= ar->winrct.xmax-ar->winrct.xmin+1;
108 int height= ar->winrct.ymax-ar->winrct.ymin+1;
110 wmOrtho2(-0.375f, (float)width-0.375f, -0.375f, (float)height-0.375f);
114 /* only exported for WM */
115 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
117 /* generic notes first */
118 switch(note->category) {
120 if(note->data==ND_FILEREAD)
121 ED_region_tag_redraw(ar);
124 ED_region_tag_redraw(ar);
128 if(ar->type && ar->type->listener)
129 ar->type->listener(ar, note);
132 /* only exported for WM */
133 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
135 /* no generic notes? */
136 if(sa->type && sa->type->listener) {
137 sa->type->listener(sa, note);
141 /* only exported for WM */
142 void ED_area_do_refresh(bContext *C, ScrArea *sa)
144 /* no generic notes? */
145 if(sa->type && sa->type->refresh) {
146 sa->type->refresh(C, sa);
151 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
152 /* only exported for WM */
153 void ED_area_overdraw_flush(ScrArea *sa, ARegion *ar)
157 for(az= sa->actionzones.first; az; az= az->next) {
160 xs= (az->x1+az->x2)/2;
161 ys= (az->y1+az->y2)/2;
164 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
170 static void area_draw_azone(short x1, short y1, short x2, short y2)
172 int dx= floor(0.3f*(x2-x1));
173 int dy= floor(0.3f*(y2-y1));
175 glColor4ub(255, 255, 255, 180);
176 fdrawline(x1, y2, x2, y1);
177 glColor4ub(255, 255, 255, 130);
178 fdrawline(x1, y2-dy, x2-dx, y1);
179 glColor4ub(255, 255, 255, 80);
180 fdrawline(x1, y2-2*dy, x2-2*dx, y1);
182 glColor4ub(0, 0, 0, 210);
183 fdrawline(x1, y2+1, x2+1, y1);
184 glColor4ub(0, 0, 0, 180);
185 fdrawline(x1, y2-dy+1, x2-dx+1, y1);
186 glColor4ub(0, 0, 0, 150);
187 fdrawline(x1, y2-2*dy+1, x2-2*dx+1, y1);
191 static void region_draw_azone(AZone *az)
193 GLUquadricObj *qobj = NULL;
194 short midx = az->x1 + (az->x2 - az->x1)/2;
195 short midy = az->y1 + (az->y2 - az->y1)/2;
197 if(az->ar==NULL) return;
199 /* only display action zone icons when the region is hidden */
200 if (!(az->ar->flag & RGN_FLAG_HIDDEN)) return;
202 qobj = gluNewQuadric();
205 glTranslatef(midx, midy, 0.);
207 /* outlined circle */
208 glEnable(GL_LINE_SMOOTH);
210 glColor4f(1.f, 1.f, 1.f, 0.8f);
212 gluQuadricDrawStyle(qobj, GLU_FILL);
213 gluDisk( qobj, 0.0, 4.25f, 16, 1);
215 glColor4f(0.2f, 0.2f, 0.2f, 0.9f);
217 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE);
218 gluDisk( qobj, 0.0, 4.25f, 16, 1);
220 glDisable(GL_LINE_SMOOTH);
223 gluDeleteQuadric(qobj);
226 sdrawline(midx, midy-2, midx, midy+3);
227 sdrawline(midx-2, midy, midx+3, midy);
231 /* only exported for WM */
232 void ED_area_overdraw(bContext *C)
234 wmWindow *win= CTX_wm_window(C);
235 bScreen *screen= CTX_wm_screen(C);
238 /* Draw AZones, in screenspace */
239 wmSubWindowSet(win, screen->mainwin);
241 glEnable( GL_BLEND );
242 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
244 for(sa= screen->areabase.first; sa; sa= sa->next) {
246 for(az= sa->actionzones.first; az; az= az->next) {
248 if(az->type==AZONE_AREA) {
249 area_draw_azone(az->x1, az->y1, az->x2, az->y2);
250 } else if(az->type==AZONE_REGION) {
251 region_draw_azone(az);
258 glDisable( GL_BLEND );
262 /* get scissor rect, checking overlapping regions */
263 void region_scissor_winrct(ARegion *ar, rcti *winrct)
267 if(ELEM(ar->alignment, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT))
273 if(BLI_isect_rcti(winrct, &ar->winrct, NULL)) {
274 if(ar->flag & RGN_FLAG_HIDDEN);
275 else if(ar->alignment & RGN_SPLIT_PREV);
276 else if(ar->alignment==RGN_OVERLAP_LEFT) {
277 winrct->xmin= ar->winrct.xmax + 1;
279 else if(ar->alignment==RGN_OVERLAP_RIGHT) {
280 winrct->xmax= ar->winrct.xmin - 1;
287 /* only exported for WM */
288 /* makes region ready for drawing, sets pixelspace */
289 void ED_region_set(const bContext *C, ARegion *ar)
291 wmWindow *win= CTX_wm_window(C);
292 ScrArea *sa= CTX_wm_area(C);
295 /* checks other overlapping regions */
296 region_scissor_winrct(ar, &winrct);
300 /* note; this sets state, so we can use wmOrtho and friends */
301 wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
303 UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
305 ED_region_pixelspace(ar);
309 /* only exported for WM */
310 void ED_region_do_draw(bContext *C, ARegion *ar)
312 wmWindow *win= CTX_wm_window(C);
313 ScrArea *sa= CTX_wm_area(C);
314 ARegionType *at= ar->type;
317 /* see BKE_spacedata_draw_locks() */
321 /* checks other overlapping regions */
322 region_scissor_winrct(ar, &winrct);
324 /* if no partial draw rect set, full rect */
325 if(ar->drawrct.xmin == ar->drawrct.xmax)
328 /* extra clip for safety */
329 ar->drawrct.xmin= MAX2(winrct.xmin, ar->drawrct.xmin);
330 ar->drawrct.ymin= MAX2(winrct.ymin, ar->drawrct.ymin);
331 ar->drawrct.xmax= MIN2(winrct.xmax, ar->drawrct.xmax);
332 ar->drawrct.ymax= MIN2(winrct.ymax, ar->drawrct.ymax);
335 /* note; this sets state, so we can use wmOrtho and friends */
336 wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
338 UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
340 /* optional header info instead? */
342 UI_ThemeClearColor(TH_HEADER);
343 glClear(GL_COLOR_BUFFER_BIT);
345 UI_ThemeColor(TH_TEXT);
346 BLF_draw_default(20, 8, 0.0f, ar->headerstr, 65535); /* XXX, use real length */
352 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
354 uiFreeInactiveBlocks(C, &ar->uiblocks);
357 region_draw_emboss(ar, &winrct);
359 /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
360 ED_region_pixelspace(ar);
363 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
366 /* **********************************
367 maybe silly, but let's try for now
368 to keep these tags protected
369 ********************************** */
371 void ED_region_tag_redraw(ARegion *ar)
374 /* zero region means full region redraw */
375 ar->do_draw= RGN_DRAW;
376 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
380 void ED_region_tag_redraw_overlay(ARegion *ar)
383 ar->do_draw_overlay= RGN_DRAW;
386 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
390 /* no redraw set yet, set partial region */
391 ar->do_draw= RGN_DRAW_PARTIAL;
394 else if(ar->drawrct.xmin != ar->drawrct.xmax) {
395 /* partial redraw already set, expand region */
396 ar->drawrct.xmin= MIN2(ar->drawrct.xmin, rct->xmin);
397 ar->drawrct.ymin= MIN2(ar->drawrct.ymin, rct->ymin);
398 ar->drawrct.xmax= MAX2(ar->drawrct.xmax, rct->xmax);
399 ar->drawrct.ymax= MAX2(ar->drawrct.ymax, rct->ymax);
404 void ED_area_tag_redraw(ScrArea *sa)
409 for(ar= sa->regionbase.first; ar; ar= ar->next)
410 ED_region_tag_redraw(ar);
413 void ED_area_tag_redraw_regiontype(ScrArea *sa, int regiontype)
418 for(ar= sa->regionbase.first; ar; ar= ar->next) {
419 if(ar->regiontype == regiontype) {
420 ED_region_tag_redraw(ar);
426 void ED_area_tag_refresh(ScrArea *sa)
432 /* *************************************************************** */
434 /* use NULL to disable it */
435 void ED_area_headerprint(ScrArea *sa, const char *str)
439 /* happens when running transform operators in backround mode */
443 for(ar= sa->regionbase.first; ar; ar= ar->next) {
444 if(ar->regiontype==RGN_TYPE_HEADER) {
446 if(ar->headerstr==NULL)
447 ar->headerstr= MEM_mallocN(256, "headerprint");
448 BLI_strncpy(ar->headerstr, str, 256);
450 else if(ar->headerstr) {
451 MEM_freeN(ar->headerstr);
454 ED_region_tag_redraw(ar);
459 /* ************************************************************ */
462 static void area_azone_initialize(ScrArea *sa)
466 /* reinitalize entirely, regions add azones too */
467 BLI_freelistN(&sa->actionzones);
469 /* set area action zones */
470 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
471 BLI_addtail(&(sa->actionzones), az);
472 az->type= AZONE_AREA;
473 az->x1= sa->totrct.xmin;
474 az->y1= sa->totrct.ymin;
475 az->x2= sa->totrct.xmin + AZONESPOT;
476 az->y2= sa->totrct.ymin + AZONESPOT;
477 BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
479 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
480 BLI_addtail(&(sa->actionzones), az);
481 az->type= AZONE_AREA;
482 az->x1= sa->totrct.xmax+1;
483 az->y1= sa->totrct.ymax+1;
484 az->x2= sa->totrct.xmax-AZONESPOT;
485 az->y2= sa->totrct.ymax-AZONESPOT;
486 BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
489 #define AZONEPAD_EDGE 4
490 #define AZONEPAD_ICON 9
491 static void region_azone_edge(AZone *az, ARegion *ar)
494 case AE_TOP_TO_BOTTOMRIGHT:
495 az->x1= ar->winrct.xmin;
496 az->y1= ar->winrct.ymax - AZONEPAD_EDGE;
497 az->x2= ar->winrct.xmax;
498 az->y2= ar->winrct.ymax;
500 case AE_BOTTOM_TO_TOPLEFT:
501 az->x1= ar->winrct.xmin;
502 az->y1= ar->winrct.ymin + AZONEPAD_EDGE;
503 az->x2= ar->winrct.xmax;
504 az->y2= ar->winrct.ymin;
506 case AE_LEFT_TO_TOPRIGHT:
507 az->x1= ar->winrct.xmin;
508 az->y1= ar->winrct.ymin;
509 az->x2= ar->winrct.xmin + AZONEPAD_EDGE;
510 az->y2= ar->winrct.ymax;
512 case AE_RIGHT_TO_TOPLEFT:
513 az->x1= ar->winrct.xmax;
514 az->y1= ar->winrct.ymin;
515 az->x2= ar->winrct.xmax - AZONEPAD_EDGE;
516 az->y2= ar->winrct.ymax;
520 BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
523 static void region_azone_icon(ScrArea *sa, AZone *az, ARegion *ar)
528 /* count how many actionzones with along same edge are available.
529 This allows for adding more action zones in the future without
530 having to worry about correct offset */
531 for(azt= sa->actionzones.first; azt; azt= azt->next) {
532 if(azt->edge == az->edge) tot++;
536 case AE_TOP_TO_BOTTOMRIGHT:
537 az->x1= ar->winrct.xmax - tot*2*AZONEPAD_ICON;
538 az->y1= ar->winrct.ymax + AZONEPAD_ICON;
539 az->x2= ar->winrct.xmax - tot*AZONEPAD_ICON;
540 az->y2= ar->winrct.ymax + 2*AZONEPAD_ICON;
542 case AE_BOTTOM_TO_TOPLEFT:
543 az->x1= ar->winrct.xmin + AZONEPAD_ICON;
544 az->y1= ar->winrct.ymin - 2*AZONEPAD_ICON;
545 az->x2= ar->winrct.xmin + 2*AZONEPAD_ICON;
546 az->y2= ar->winrct.ymin - AZONEPAD_ICON;
548 case AE_LEFT_TO_TOPRIGHT:
549 az->x1= ar->winrct.xmin - 2*AZONEPAD_ICON;
550 az->y1= ar->winrct.ymax - tot*2*AZONEPAD_ICON;
551 az->x2= ar->winrct.xmin - AZONEPAD_ICON;
552 az->y2= ar->winrct.ymax - tot*AZONEPAD_ICON;
554 case AE_RIGHT_TO_TOPLEFT:
555 az->x1= ar->winrct.xmax + AZONEPAD_ICON;
556 az->y1= ar->winrct.ymax - tot*2*AZONEPAD_ICON;
557 az->x2= ar->winrct.xmax + 2*AZONEPAD_ICON;
558 az->y2= ar->winrct.ymax - tot*AZONEPAD_ICON;
562 BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
564 /* if more azones on 1 spot, set offset */
565 for(azt= sa->actionzones.first; azt; azt= azt->next) {
567 if( ABS(az->x1-azt->x1) < 2 && ABS(az->y1-azt->y1) < 2) {
568 if(az->edge==AE_TOP_TO_BOTTOMRIGHT || az->edge==AE_BOTTOM_TO_TOPLEFT) {
576 BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
582 static void region_azone_initialize(ScrArea *sa, ARegion *ar, AZEdge edge)
586 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
587 BLI_addtail(&(sa->actionzones), az);
588 az->type= AZONE_REGION;
592 if (ar->flag & RGN_FLAG_HIDDEN) {
593 region_azone_icon(sa, az, ar);
595 region_azone_edge(az, ar);
601 /* *************************************************************** */
603 static void region_azone_add(ScrArea *sa, ARegion *ar, int alignment)
605 /* edge code (t b l r) is along which area edge azone will be drawn */
607 if(alignment==RGN_ALIGN_TOP)
608 region_azone_initialize(sa, ar, AE_BOTTOM_TO_TOPLEFT);
609 else if(alignment==RGN_ALIGN_BOTTOM)
610 region_azone_initialize(sa, ar, AE_TOP_TO_BOTTOMRIGHT);
611 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
612 region_azone_initialize(sa, ar, AE_LEFT_TO_TOPRIGHT);
613 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
614 region_azone_initialize(sa, ar, AE_RIGHT_TO_TOPLEFT);
617 /* dir is direction to check, not the splitting edge direction! */
618 static int rct_fits(rcti *rect, char dir, int size)
621 return rect->xmax-rect->xmin - size;
624 return rect->ymax-rect->ymin - size;
628 static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
630 rcti *remainder_prev= remainder;
631 int prefsizex, prefsizey;
637 /* no returns in function, winrct gets set in the end again */
638 BLI_init_rcti(&ar->winrct, 0, 0, 0, 0);
640 /* for test; allow split of previously defined region */
641 if(ar->alignment & RGN_SPLIT_PREV)
643 remainder= &ar->prev->winrct;
645 alignment = ar->alignment & ~RGN_SPLIT_PREV;
647 /* clear state flags first */
648 ar->flag &= ~RGN_FLAG_TOO_SMALL;
650 if(ar->next==NULL && alignment!=RGN_ALIGN_QSPLIT)
651 alignment= RGN_ALIGN_NONE;
653 prefsizex= ar->sizex?ar->sizex:ar->type->prefsizex;
654 prefsizey= ar->sizey?ar->sizey:ar->type->prefsizey;
656 /* hidden is user flag */
657 if(ar->flag & RGN_FLAG_HIDDEN);
658 /* XXX floating area region, not handled yet here */
659 else if(alignment == RGN_ALIGN_FLOAT);
660 /* remainder is too small for any usage */
661 else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
662 ar->flag |= RGN_FLAG_TOO_SMALL;
664 else if(alignment==RGN_ALIGN_NONE) {
665 /* typically last region */
666 ar->winrct= *remainder;
667 BLI_init_rcti(remainder, 0, 0, 0, 0);
669 else if(alignment==RGN_ALIGN_TOP || alignment==RGN_ALIGN_BOTTOM) {
671 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
672 ar->flag |= RGN_FLAG_TOO_SMALL;
675 int fac= rct_fits(remainder, 'v', prefsizey);
680 ar->winrct= *remainder;
682 if(alignment==RGN_ALIGN_TOP) {
683 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
684 remainder->ymax= ar->winrct.ymin - 1;
687 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
688 remainder->ymin= ar->winrct.ymax + 1;
692 else if( ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
694 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
695 ar->flag |= RGN_FLAG_TOO_SMALL;
698 int fac= rct_fits(remainder, 'h', prefsizex);
703 ar->winrct= *remainder;
705 if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
706 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
707 if(alignment==RGN_ALIGN_RIGHT)
708 remainder->xmax= ar->winrct.xmin - 1;
711 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
712 if(alignment==RGN_ALIGN_LEFT)
713 remainder->xmin= ar->winrct.xmax + 1;
717 else if(alignment==RGN_ALIGN_VSPLIT || alignment==RGN_ALIGN_HSPLIT) {
718 /* percentage subdiv*/
719 ar->winrct= *remainder;
721 if(alignment==RGN_ALIGN_HSPLIT) {
722 if( rct_fits(remainder, 'h', prefsizex) > 4) {
723 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
724 remainder->xmin= ar->winrct.xmax+1;
727 BLI_init_rcti(remainder, 0, 0, 0, 0);
731 if( rct_fits(remainder, 'v', prefsizey) > 4) {
732 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
733 remainder->ymin= ar->winrct.ymax+1;
736 BLI_init_rcti(remainder, 0, 0, 0, 0);
740 else if(alignment==RGN_ALIGN_QSPLIT) {
741 ar->winrct= *remainder;
743 /* test if there's still 4 regions left */
745 ARegion *artest= ar->next;
749 artest->alignment= RGN_ALIGN_QSPLIT;
750 artest= artest->next;
755 /* let's stop adding regions */
756 BLI_init_rcti(remainder, 0, 0, 0, 0);
758 printf("region quadsplit failed\n");
763 if(quad==1) { /* left bottom */
764 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
765 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
767 else if(quad==2) { /* left top */
768 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
769 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
771 else if(quad==3) { /* right bottom */
772 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
773 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
775 else { /* right top */
776 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
777 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
778 BLI_init_rcti(remainder, 0, 0, 0, 0);
786 ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
787 ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
789 /* restore test exception */
790 if(ar->alignment & RGN_SPLIT_PREV) {
792 remainder= remainder_prev;
793 ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
794 ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
798 /* set winrect for azones */
799 if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
800 ar->winrct= *remainder;
802 if(alignment==RGN_ALIGN_TOP)
803 ar->winrct.ymin= ar->winrct.ymax;
804 else if(alignment==RGN_ALIGN_BOTTOM)
805 ar->winrct.ymax= ar->winrct.ymin;
806 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
807 ar->winrct.xmin= ar->winrct.xmax;
808 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
809 ar->winrct.xmax= ar->winrct.xmin;
810 else /* prevent winrct to be valid */
811 ar->winrct.xmax= ar->winrct.xmin;
813 /* in end, add azones, where appropriate */
814 region_azone_add(sa, ar, alignment);
817 region_rect_recursive(sa, ar->next, remainder, quad);
820 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
822 short rt= CLAMPIS(G.rt, 0, 16);
824 if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1+rt;
825 else sa->totrct.xmin= sa->v1->vec.x;
826 if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1-rt;
827 else sa->totrct.xmax= sa->v4->vec.x;
829 if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1+rt;
830 else sa->totrct.ymin= sa->v1->vec.y;
831 if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1-rt;
832 else sa->totrct.ymax= sa->v2->vec.y;
835 sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
836 sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
840 /* used for area initialize below */
841 static void region_subwindow(wmWindow *win, ARegion *ar)
843 if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
845 wm_subwindow_close(win, ar->swinid);
848 else if(ar->swinid==0)
849 ar->swinid= wm_subwindow_open(win, &ar->winrct);
851 wm_subwindow_position(win, ar->swinid, &ar->winrct);
854 static void ed_default_handlers(wmWindowManager *wm, ScrArea *sa, ListBase *handlers, int flag)
856 /* note, add-handler checks if it already exists */
858 // XXX it would be good to have boundbox checks for some of these...
859 if(flag & ED_KEYMAP_UI) {
860 /* user interface widgets */
861 UI_add_region_handlers(handlers);
863 if(flag & ED_KEYMAP_VIEW2D) {
864 /* 2d-viewport handling+manipulation */
865 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
866 WM_event_add_keymap_handler(handlers, keymap);
868 if(flag & ED_KEYMAP_MARKERS) {
870 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
872 /* time space only has this keymap, the others get a boundbox restricted map */
873 if(sa->spacetype!=SPACE_TIME) {
875 static rcti rect= {0, 10000, 0, 30}; /* same local check for all areas */
877 for(ar= sa->regionbase.first; ar; ar= ar->next)
878 if(ar->regiontype == RGN_TYPE_WINDOW)
881 WM_event_add_keymap_handler_bb(handlers, keymap, &rect, &ar->winrct);
884 WM_event_add_keymap_handler(handlers, keymap);
886 if(flag & ED_KEYMAP_ANIMATION) {
887 /* frame changing and timeline operators (for time spaces) */
888 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
889 WM_event_add_keymap_handler(handlers, keymap);
891 if(flag & ED_KEYMAP_FRAMES) {
892 /* frame changing/jumping (for all spaces) */
893 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
894 WM_event_add_keymap_handler(handlers, keymap);
896 if(flag & ED_KEYMAP_GPENCIL) {
898 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
899 WM_event_add_keymap_handler(handlers, keymap);
901 if(flag & ED_KEYMAP_HEADER) {
902 /* standard keymap for headers regions */
903 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Header", 0, 0);
904 WM_event_add_keymap_handler(handlers, keymap);
909 /* called in screen_refresh, or screens_init, also area size changes */
910 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
915 /* set typedefinitions */
916 sa->type= BKE_spacetype_from_id(sa->spacetype);
919 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
920 sa->type= BKE_spacetype_from_id(sa->spacetype);
923 for(ar= sa->regionbase.first; ar; ar= ar->next)
924 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
927 area_calc_totrct(sa, win->sizex, win->sizey);
929 /* clear all azones, add the area triange widgets */
930 area_azone_initialize(sa);
932 /* region rect sizes */
934 region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
936 /* default area handlers */
937 ed_default_handlers(wm, sa, &sa->handlers, sa->type->keymapflag);
938 /* checks spacedata, adds own handlers */
940 sa->type->init(wm, sa);
942 /* region windows, default and own handlers */
943 for(ar= sa->regionbase.first; ar; ar= ar->next) {
944 region_subwindow(win, ar);
947 /* default region handlers */
948 ed_default_handlers(wm, sa, &ar->handlers, ar->type->keymapflag);
951 ar->type->init(wm, ar);
954 /* prevent uiblocks to run */
955 uiFreeBlocks(NULL, &ar->uiblocks);
961 /* externally called for floating regions like menus */
962 void ED_region_init(bContext *C, ARegion *ar)
964 // ARegionType *at= ar->type;
966 /* refresh can be called before window opened */
967 region_subwindow(CTX_wm_window(C), ar);
969 ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
970 ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
973 wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
977 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
979 ScrArea *sa= CTX_wm_area(C);
981 ar->flag ^= RGN_FLAG_HIDDEN;
983 if(ar->flag & RGN_FLAG_HIDDEN)
984 WM_event_remove_handlers(C, &ar->handlers);
986 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
987 ED_area_tag_redraw(sa);
990 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
991 /* area vertices were set */
992 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
996 int spacetype= sa1->spacetype;
998 sa1->headertype= sa2->headertype;
999 sa1->spacetype= sa2->spacetype;
1000 sa1->butspacetype= sa2->butspacetype;
1002 if(swap_space == 1) {
1003 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
1004 /* exception: ensure preview is reset */
1005 // if(sa1->spacetype==SPACE_VIEW3D)
1006 // XXX BIF_view3d_previewrender_free(sa1->spacedata.first);
1008 else if (swap_space == 2) {
1009 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
1012 BKE_spacedata_freelist(&sa1->spacedata);
1013 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
1016 /* Note; SPACE_EMPTY is possible on new screens */
1019 if(swap_space == 1) {
1020 SWAP(ListBase, sa1->regionbase, sa2->regionbase);
1024 st= BKE_spacetype_from_id(spacetype);
1025 for(ar= sa1->regionbase.first; ar; ar= ar->next)
1026 BKE_area_region_free(st, ar);
1027 BLI_freelistN(&sa1->regionbase);
1030 st= BKE_spacetype_from_id(sa2->spacetype);
1031 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
1032 ARegion *newar= BKE_area_region_copy(st, ar);
1033 BLI_addtail(&sa1->regionbase, newar);
1038 /* *********** Space switching code *********** */
1040 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
1042 ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
1044 ED_area_exit(C, sa1);
1045 ED_area_exit(C, sa2);
1047 area_copy_data(tmp, sa1, 2);
1048 area_copy_data(sa1, sa2, 0);
1049 area_copy_data(sa2, tmp, 0);
1050 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
1051 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
1053 BKE_screen_area_free(tmp);
1056 /* tell WM to refresh, cursor types etc */
1057 WM_event_add_mousemove(C);
1059 ED_area_tag_redraw(sa1);
1060 ED_area_tag_refresh(sa1);
1061 ED_area_tag_redraw(sa2);
1062 ED_area_tag_refresh(sa2);
1065 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
1067 if(sa->spacetype != type) {
1072 ED_area_exit(C, sa);
1074 st= BKE_spacetype_from_id(type);
1075 slold= sa->spacedata.first;
1077 sa->spacetype= type;
1078 sa->butspacetype= type;
1081 /* check previously stored space */
1082 for (sl= sa->spacedata.first; sl; sl= sl->next)
1083 if(sl->spacetype==type)
1086 /* old spacedata... happened during work on 2.50, remove */
1087 if(sl && sl->regionbase.first==NULL) {
1089 BLI_freelinkN(&sa->spacedata, sl);
1099 slold->regionbase= sa->regionbase;
1100 sa->regionbase= sl->regionbase;
1101 sl->regionbase.first= sl->regionbase.last= NULL;
1103 /* put in front of list */
1104 BLI_remlink(&sa->spacedata, sl);
1105 BLI_addhead(&sa->spacedata, sl);
1111 BLI_addhead(&sa->spacedata, sl);
1115 slold->regionbase= sa->regionbase;
1116 sa->regionbase= sl->regionbase;
1117 sl->regionbase.first= sl->regionbase.last= NULL;
1121 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1123 /* tell WM to refresh, cursor types etc */
1124 WM_event_add_mousemove(C);
1126 /*send space change notifyer*/
1127 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1129 ED_area_tag_refresh(sa);
1132 /* also redraw when re-used */
1133 ED_area_tag_redraw(sa);
1136 void ED_area_prevspace(bContext *C, ScrArea *sa)
1138 SpaceLink *sl = (sa) ? sa->spacedata.first : CTX_wm_space_data(C);
1141 /* workaround for case of double prevspace, render window
1142 with a file browser on top of it */
1143 if(sl->next->spacetype == SPACE_FILE && sl->next->next)
1144 ED_area_newspace(C, sa, sl->next->next->spacetype);
1146 ED_area_newspace(C, sa, sl->next->spacetype);
1149 ED_area_newspace(C, sa, SPACE_INFO);
1151 ED_area_tag_redraw(sa);
1153 /*send space change notifyer*/
1154 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1157 static const char *editortype_pup(void)
1172 "|UV/Image Editor %x6"
1174 "|Video Sequence Editor %x8"
1177 "|Logic Editor %x17"
1183 "|User Preferences %x19"
1192 "|Python Console %x18"
1196 static void spacefunc(struct bContext *C, void *UNUSED(arg1), void *UNUSED(arg2))
1198 ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1199 ED_area_tag_redraw(CTX_wm_area(C));
1201 /*send space change notifyer*/
1202 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, CTX_wm_area(C));
1205 /* returns offset for next button in header */
1206 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1208 ScrArea *sa= CTX_wm_area(C);
1212 but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D,
1213 editortype_pup(), xco, yco, XIC+10, YIC,
1214 &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0,
1215 "Displays current editor type. "
1216 "Click for menu of available types");
1217 uiButSetFunc(but, spacefunc, NULL, NULL);
1219 return xco + XIC + 14;
1222 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1224 ScrArea *sa= CTX_wm_area(C);
1228 xco= ED_area_header_switchbutton(C, block, yco);
1230 uiBlockSetEmboss(block, UI_EMBOSSN);
1232 if (sa->flag & HEADER_NO_PULLDOWN) {
1233 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
1234 ICON_DISCLOSURE_TRI_RIGHT,
1236 &(sa->flag), 0, 0, 0, 0,
1237 "Show pulldown menus");
1240 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
1241 ICON_DISCLOSURE_TRI_DOWN,
1243 &(sa->flag), 0, 0, 0, 0,
1244 "Hide pulldown menus");
1247 uiBlockSetEmboss(block, UI_EMBOSS);
1252 /************************ standard UI regions ************************/
1254 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *context, int contextnr)
1256 ScrArea *sa= CTX_wm_area(C);
1257 uiStyle *style= U.uistyles.first;
1261 View2D *v2d= &ar->v2d;
1262 View2DScrollers *scrollers;
1263 int xco, yco, x, y, miny=0, w, em, header, triangle, open, newcontext= 0;
1266 newcontext= UI_view2d_tab_set(v2d, contextnr);
1269 w= v2d->cur.xmax - v2d->cur.xmin;
1270 em= (ar->type->prefsizex)? 10: 20;
1274 em= (ar->type->prefsizex)? 10: 20;
1278 y= -style->panelouter;
1281 uiBeginPanels(C, ar);
1283 /* set view2d view matrix for scrolling (without scrollers) */
1284 UI_view2d_view_ortho(v2d);
1286 for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
1287 /* verify context */
1289 if(pt->context[0] && strcmp(context, pt->context) != 0)
1293 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1294 block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1295 panel= uiBeginPanel(sa, ar, block, pt, &open);
1297 /* bad fixed values */
1298 header= (pt->flag & PNL_NO_HEADER)? 0: 20;
1304 if(pt->draw_header && header && (open || vertical)) {
1305 /* for enabled buttons */
1306 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1307 triangle, header+style->panelspace, header, 1, style);
1309 pt->draw_header(C, panel);
1311 uiBlockLayoutResolve(block, &xco, &yco);
1312 panel->labelofs= xco - triangle;
1313 panel->layout= NULL;
1322 /* panel context can either be toolbar region or normal panels region */
1323 if (ar->regiontype == RGN_TYPE_TOOLS)
1324 panelContext= UI_LAYOUT_TOOLBAR;
1326 panelContext= UI_LAYOUT_PANEL;
1328 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1329 style->panelspace, 0, w-2*style->panelspace, em, style);
1333 uiBlockLayoutResolve(block, &xco, &yco);
1334 panel->layout= NULL;
1336 yco -= 2*style->panelspace;
1337 uiEndPanel(block, w, -yco);
1341 uiEndPanel(block, w, 0);
1344 uiEndBlock(C, block);
1347 if(pt->flag & PNL_NO_HEADER)
1350 y += yco-style->panelouter;
1354 miny= MIN2(y, yco-style->panelouter-header);
1364 /* in case there are no panels */
1365 if(x == 0 || y == 0) {
1371 UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW)?TH_PREVIEW_BACK:TH_BACK);
1372 glClear(GL_COLOR_BUFFER_BIT);
1374 /* before setting the view */
1376 /* only allow scrolling in vertical direction */
1377 v2d->keepofs |= V2D_LOCKOFS_X|V2D_KEEPOFS_Y;
1378 v2d->keepofs &= ~(V2D_LOCKOFS_Y|V2D_KEEPOFS_X);
1379 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1380 v2d->scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1382 // don't jump back when panels close or hide
1384 y= MAX2(-y, -v2d->cur.ymin);
1389 /* for now, allow scrolling in both directions (since layouts are optimised for vertical,
1390 * they often don't fit in horizontal layout)
1392 v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_LOCKOFS_Y|V2D_KEEPOFS_X|V2D_KEEPOFS_Y);
1393 //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
1394 //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
1395 v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
1396 v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_HIDE;
1398 // don't jump back when panels close or hide
1400 x= MAX2(x, v2d->cur.xmax);
1404 // +V2D_SCROLL_HEIGHT is workaround to set the actual height
1405 UI_view2d_totRect_set(v2d, x+V2D_SCROLL_WIDTH, y+V2D_SCROLL_HEIGHT);
1408 UI_view2d_view_ortho(v2d);
1410 /* this does the actual drawing! */
1413 /* restore view matrix */
1414 UI_view2d_view_restore(C);
1417 scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1418 UI_view2d_scrollers_draw(C, v2d, scrollers);
1419 UI_view2d_scrollers_free(scrollers);
1422 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1426 // XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1427 // scrollbars for button regions
1428 ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
1429 ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1430 ar->v2d.scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1431 ar->v2d.keepzoom |= V2D_KEEPZOOM;
1433 // correctly initialised User-Prefs?
1434 if(!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1435 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1437 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1439 keymap= WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1440 WM_event_add_keymap_handler(&ar->handlers, keymap);
1443 void ED_region_header(const bContext *C, ARegion *ar)
1445 uiStyle *style= U.uistyles.first;
1449 Header header = {NULL};
1450 int maxco, xco, yco;
1453 UI_ThemeClearColor((ED_screen_area_active(C))?TH_HEADER:TH_HEADERDESEL);
1454 glClear(GL_COLOR_BUFFER_BIT);
1456 /* set view2d view matrix for scrolling (without scrollers) */
1457 UI_view2d_view_ortho(&ar->v2d);
1462 /* draw all headers types */
1463 for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1464 block= uiBeginBlock(C, ar, ht->idname, UI_EMBOSS);
1465 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, HEADERY-6, 1, style);
1469 header.layout= layout;
1470 ht->draw(C, &header);
1473 xco= uiLayoutGetWidth(layout);
1478 uiBlockLayoutResolve(block, &xco, &yco);
1484 uiEndBlock(C, block);
1485 uiDrawBlock(C, block);
1488 /* always as last */
1489 UI_view2d_totRect_set(&ar->v2d, maxco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1491 /* restore view matrix? */
1492 UI_view2d_view_restore(C);
1495 void ED_region_header_init(ARegion *ar)
1497 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);