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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
32 #include "MEM_guardedalloc.h"
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
38 #include "BKE_global.h"
39 #include "BKE_screen.h"
40 #include "BKE_utildefines.h"
42 #include "ED_screen.h"
43 #include "ED_screen_types.h"
47 #include "wm_subwindow.h"
50 #include "BIF_glutil.h"
52 #include "UI_interface.h"
53 #include "UI_resources.h"
54 #include "UI_view2d.h"
56 #ifndef DISABLE_PYTHON
57 #include "BPY_extern.h"
62 #include "screen_intern.h"
64 /* general area and region code */
66 static void region_draw_emboss(ARegion *ar)
70 winx= ar->winrct.xmax-ar->winrct.xmin;
71 winy= ar->winrct.ymax-ar->winrct.ymin;
75 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
78 glColor4ub(0,0,0, 50);
79 sdrawline(winx, 0, winx, winy);
82 glColor4ub(0,0,0, 80);
83 sdrawline(0, 0, winx, 0);
86 glColor4ub(255,255,255, 60);
87 sdrawline(0, winy, winx, winy);
90 glColor4ub(255,255,255, 50);
91 sdrawline(0, 0, 0, winy);
93 glDisable( GL_BLEND );
96 void ED_region_pixelspace(const bContext *C, ARegion *ar)
98 int width= ar->winrct.xmax-ar->winrct.xmin+1;
99 int height= ar->winrct.ymax-ar->winrct.ymin+1;
101 wmOrtho2(C->window, -0.375, (float)width-0.375, -0.375, (float)height-0.375);
102 wmLoadIdentity(C->window);
105 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
108 /* generic notes first */
110 case WM_NOTE_WINDOW_REDRAW:
111 case WM_NOTE_AREA_REDRAW:
112 case WM_NOTE_REGION_REDRAW:
113 case WM_NOTE_GESTURE_REDRAW:
114 case WM_NOTE_SCREEN_CHANGED:
118 if(ar->type->listener)
119 ar->type->listener(ar, note);
123 /* only internal decoration, AZone for now */
124 void ED_area_do_draw(bContext *C, ScrArea *sa)
128 /* hrmf, screenspace for zones */
129 wm_subwindow_set(C->window, C->window->screen->mainwin);
131 /* temporary viz for 'action corner' */
132 for(az= sa->actionzones.first; az; az= az->next) {
134 glEnable( GL_BLEND );
135 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
136 glColor4ub(0, 0, 0, 80);
137 if(az->type==AZONE_TRI) sdrawtrifill(az->x1, az->y1, az->x2, az->y2);
138 //if(az->type==AZONE_TRI) sdrawtri(az->x1, az->y1, az->x2, az->y2);
139 glDisable( GL_BLEND );
144 void ED_region_do_draw(bContext *C, ARegion *ar)
146 ARegionType *at= ar->type;
148 wm_subwindow_set(C->window, ar->swinid);
150 if(ar->swinid && at->draw) {
151 UI_SetTheme(C->area);
156 float fac= 0.1*ar->swinid;
160 glClearColor(0.5, fac, 1.0f-fac, 0.0);
161 glClear(GL_COLOR_BUFFER_BIT);
163 /* swapbuffers indicator */
165 glColor3f(fac, fac, fac);
166 glRecti(20, 2, 30, 12);
170 region_draw_emboss(ar);
172 /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
173 ED_region_pixelspace(C, ar);
178 /* *************************************************************** */
180 /* dir is direction to check, not the splitting edge direction! */
181 static int rct_fits(rcti *rect, char dir, int size)
184 return rect->xmax-rect->xmin - size;
187 return rect->ymax-rect->ymin - size;
191 static void region_rect_recursive(ARegion *ar, rcti *remainder)
193 int prefsizex, prefsizey;
198 /* clear state flags first */
199 ar->flag &= ~RGN_FLAG_TOO_SMALL;
201 ar->alignment= RGN_ALIGN_NONE;
203 prefsizex= ar->type->minsizex;
204 prefsizey= ar->type->minsizey;
206 /* hidden is user flag */
207 if(ar->flag & RGN_FLAG_HIDDEN);
208 /* XXX floating area region, not handled yet here */
209 else if(ar->alignment == RGN_ALIGN_FLOAT);
210 /* remainder is too small for any usage */
211 else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
212 ar->flag |= RGN_FLAG_TOO_SMALL;
214 else if(ar->alignment==RGN_ALIGN_NONE) {
215 /* typically last region */
216 ar->winrct= *remainder;
217 BLI_init_rcti(remainder, 0, 0, 0, 0);
219 else if(ar->alignment==RGN_ALIGN_TOP || ar->alignment==RGN_ALIGN_BOTTOM) {
221 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
222 ar->flag |= RGN_FLAG_TOO_SMALL;
225 int fac= rct_fits(remainder, 'v', prefsizey);
230 ar->winrct= *remainder;
232 if(ar->alignment==RGN_ALIGN_TOP) {
233 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
234 remainder->ymax= ar->winrct.ymin - 1;
237 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
238 remainder->ymin= ar->winrct.ymax + 1;
242 else if(ar->alignment==RGN_ALIGN_LEFT || ar->alignment==RGN_ALIGN_RIGHT) {
244 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
245 ar->flag |= RGN_FLAG_TOO_SMALL;
248 int fac= rct_fits(remainder, 'h', prefsizex);
253 ar->winrct= *remainder;
255 if(ar->alignment==RGN_ALIGN_RIGHT) {
256 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
257 remainder->xmax= ar->winrct.xmin - 1;
260 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
261 remainder->xmin= ar->winrct.xmax + 1;
266 /* percentage subdiv*/
267 ar->winrct= *remainder;
269 if(ar->alignment==RGN_ALIGN_HSPLIT) {
270 if( rct_fits(remainder, 'h', prefsizex) > 4) {
271 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
272 remainder->xmin= ar->winrct.xmax+1;
275 BLI_init_rcti(remainder, 0, 0, 0, 0);
279 if( rct_fits(remainder, 'v', prefsizey) > 4) {
280 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
281 remainder->ymin= ar->winrct.ymax+1;
284 BLI_init_rcti(remainder, 0, 0, 0, 0);
289 ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
290 ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
292 region_rect_recursive(ar->next, remainder);
295 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
298 if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
299 else sa->totrct.xmin= sa->v1->vec.x;
300 if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
301 else sa->totrct.xmax= sa->v4->vec.x;
303 if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
304 else sa->totrct.ymin= sa->v1->vec.y;
305 if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
306 else sa->totrct.ymax= sa->v2->vec.y;
309 sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
310 sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
314 void area_azone_initialize(ScrArea *sa)
317 if(sa->actionzones.first==NULL) {
318 /* set action zones - should these actually be ARegions? With these we can easier check area hotzones */
319 /* (ton) for time being just area, ARegion split is not foreseen on user level */
320 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
321 BLI_addtail(&(sa->actionzones), az);
325 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
326 BLI_addtail(&(sa->actionzones), az);
331 for(az= sa->actionzones.first; az; az= az->next) {
332 if(az->pos==AZONE_SW) {
333 az->x1= sa->v1->vec.x+1;
334 az->y1= sa->v1->vec.y+1;
335 az->x2= sa->v1->vec.x+AZONESPOT;
336 az->y2= sa->v1->vec.y+AZONESPOT;
338 else if (az->pos==AZONE_NE) {
339 az->x1= sa->v3->vec.x;
340 az->y1= sa->v3->vec.y;
341 az->x2= sa->v3->vec.x-AZONESPOT;
342 az->y2= sa->v3->vec.y-AZONESPOT;
347 /* used for area initialize below */
348 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
350 if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
352 wm_subwindow_close(win, ar->swinid);
355 else if(ar->swinid==0)
356 ar->swinid= wm_subwindow_open(win, &ar->winrct);
358 wm_subwindow_position(win, ar->swinid, &ar->winrct);
361 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
363 /* note, add-handler checks if it already exists */
365 if(flag & ED_KEYMAP_UI) {
366 UI_add_region_handlers(handlers);
368 if(flag & ED_KEYMAP_VIEW2D) {
369 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
370 WM_event_add_keymap_handler(handlers, keymap);
372 if(flag & ED_KEYMAP_MARKERS) {
373 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
374 WM_event_add_keymap_handler(handlers, keymap);
379 /* called in screen_refresh, or screens_init, also area size changes */
380 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
385 /* set typedefinitions */
386 sa->type= BKE_spacetype_from_id(sa->spacetype);
389 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
390 sa->type= BKE_spacetype_from_id(sa->spacetype);
393 for(ar= sa->regionbase.first; ar; ar= ar->next)
394 ar->type= ED_regiontype_from_id(sa->type, ar->regiontype);
397 area_calc_totrct(sa, win->sizex, win->sizey);
399 /* region rect sizes */
401 region_rect_recursive(sa->regionbase.first, &rect);
403 /* default area handlers */
404 ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
405 /* checks spacedata, adds own handlers */
407 sa->type->init(wm, sa);
409 /* region windows, default and own handlers */
410 for(ar= sa->regionbase.first; ar; ar= ar->next) {
411 region_subwindow(wm, win, ar);
413 /* default region handlers */
414 ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
417 ar->type->init(wm, ar);
420 area_azone_initialize(sa);
423 /* externally called for floating regions like menus */
424 void ED_region_init(bContext *C, ARegion *ar)
426 // ARegionType *at= ar->type;
428 /* refresh can be called before window opened */
429 region_subwindow(C->wm, C->window, ar);
434 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
435 /* area vertices were set */
436 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
438 Panel *pa1, *pa2, *patab;
441 sa1->headertype= sa2->headertype;
442 sa1->spacetype= sa2->spacetype;
445 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
446 /* exception: ensure preview is reset */
447 // if(sa1->spacetype==SPACE_VIEW3D)
448 // XXX BIF_view3d_previewrender_free(sa1->spacedata.first);
451 BKE_spacedata_freelist(&sa1->spacedata);
452 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
455 BLI_freelistN(&sa1->panels);
456 BLI_duplicatelist(&sa1->panels, &sa2->panels);
458 /* copy panel pointers */
459 for(pa1= sa1->panels.first; pa1; pa1= pa1->next) {
461 patab= sa1->panels.first;
462 pa2= sa2->panels.first;
464 if( pa1->paneltab == pa2) {
465 pa1->paneltab = patab;
474 BLI_freelistN(&sa1->regionbase);
476 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
477 ARegion *newar= BKE_area_region_copy(ar);
478 BLI_addtail(&sa1->regionbase, newar);
481 #ifndef DISABLE_PYTHON
483 BPY_free_scriptlink(&sa1->scriptlink);
484 sa1->scriptlink= sa2->scriptlink;
485 BPY_copy_scriptlink(&sa1->scriptlink); /* copies internal pointers */
489 /* *********** Space switching code, local now *********** */
490 /* XXX make operator for this */
492 static void newspace(bContext *C, ScrArea *sa, int type)
494 if(sa->spacetype != type) {
501 st= BKE_spacetype_from_id(type);
502 slold= sa->spacedata.first;
505 sa->butspacetype= type;
507 /* check previously stored space */
508 for (sl= sa->spacedata.first; sl; sl= sl->next)
509 if(sl->spacetype==type)
512 /* old spacedata... happened during work on 2.50, remove */
513 if(sl && sl->regionbase.first==NULL) {
522 slold->regionbase= sa->regionbase;
523 sa->regionbase= sl->regionbase;
524 sl->regionbase.first= sl->regionbase.last= NULL;
526 /* put in front of list */
527 BLI_remlink(&sa->spacedata, sl);
528 BLI_addhead(&sa->spacedata, sl);
534 BLI_addhead(&sa->spacedata, sl);
537 slold->regionbase= sa->regionbase;
538 sa->regionbase= sl->regionbase;
539 sl->regionbase.first= sl->regionbase.last= NULL;
546 static char *windowtype_pup(void)
549 "Window type:%t" //14
554 "|Ipo Curve Editor %x2" //54
555 "|Action Editor %x12" //73
556 "|NLA Editor %x13" //94
560 "|UV/Image Editor %x6" //117
562 "|Video Sequence Editor %x8" //143
563 "|Timeline %x15" //163
564 "|Audio Window %x11" //163
565 "|Text Editor %x9" //179
570 "|User Preferences %x7" //213
571 "|Outliner %x3" //232
572 "|Buttons Window %x4" //251
576 "|Image Browser %x10" //273
577 "|File Browser %x5" //290
581 "|Scripts Window %x14"//313
585 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
587 newspace(C, C->area, C->area->butspacetype);
588 WM_event_add_notifier(C, WM_NOTE_SCREEN_CHANGED, 0, NULL);
591 /* returns offset for next button in header */
592 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
597 if(ED_screen_area_active(C)) uiBlockSetCol(block, TH_HEADER);
598 else uiBlockSetCol(block, TH_HEADERDESEL);
600 but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D,
601 windowtype_pup(), xco, yco, XIC+10, YIC,
602 &(C->area->butspacetype), 1.0, SPACEICONMAX, 0, 0,
603 "Displays Current Window Type. "
604 "Click for menu of available types.");
605 uiButSetFunc(but, spacefunc, NULL, NULL);
609 uiBlockSetEmboss(block, UI_EMBOSSN);
610 if (C->area->flag & HEADER_NO_PULLDOWN) {
611 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
612 ICON_DISCLOSURE_TRI_RIGHT,
614 &(C->area->flag), 0, 0, 0, 0,
615 "Show pulldown menus");
618 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
619 ICON_DISCLOSURE_TRI_DOWN,
621 &(C->area->flag), 0, 0, 0, 0,
622 "Hide pulldown menus");