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_context.h"
39 #include "BKE_global.h"
40 #include "BKE_screen.h"
41 #include "BKE_utildefines.h"
45 #include "wm_subwindow.h"
47 #include "ED_screen.h"
48 #include "ED_screen_types.h"
52 #include "BIF_glutil.h"
54 #include "UI_interface.h"
55 #include "UI_resources.h"
56 #include "UI_view2d.h"
60 #ifndef DISABLE_PYTHON
61 #include "BPY_extern.h"
64 #include "screen_intern.h"
66 /* general area and region code */
68 static void region_draw_emboss(ARegion *ar)
72 winx= ar->winrct.xmax-ar->winrct.xmin;
73 winy= ar->winrct.ymax-ar->winrct.ymin;
77 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
80 glColor4ub(0,0,0, 50);
81 sdrawline(winx, 0, winx, winy);
84 glColor4ub(0,0,0, 80);
85 sdrawline(0, 0, winx, 0);
88 glColor4ub(255,255,255, 60);
89 sdrawline(0, winy, winx, winy);
92 glColor4ub(255,255,255, 50);
93 sdrawline(0, 0, 0, winy);
95 glDisable( GL_BLEND );
98 void ED_region_pixelspace(ARegion *ar)
100 int width= ar->winrct.xmax-ar->winrct.xmin+1;
101 int height= ar->winrct.ymax-ar->winrct.ymin+1;
103 wmOrtho2(-0.375, (float)width-0.375, -0.375, (float)height-0.375);
107 /* only exported for WM */
108 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
110 /* generic notes first */
111 switch(note->category) {
113 ED_region_tag_redraw(ar);
116 if(note->action==NA_EDITED)
117 ED_region_tag_redraw(ar);
120 if(ar->type && ar->type->listener)
121 ar->type->listener(ar, note);
125 /* only exported for WM */
126 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
128 /* no generic notes? */
129 if(sa->type && sa->type->listener) {
130 sa->type->listener(sa, note);
134 /* only exported for WM */
135 void ED_area_do_refresh(bContext *C, ScrArea *sa)
137 /* no generic notes? */
138 if(sa->type && sa->type->refresh) {
139 sa->type->refresh(C, sa);
144 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
145 /* only exported for WM */
146 void ED_area_overdraw_flush(bContext *C)
150 for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
153 for(ar= sa->regionbase.first; ar; ar= ar->next) {
157 for(az= sa->actionzones.first; az; az= az->next) {
158 int xs= (az->x1+az->x2)/2, ys= (az->y1+az->y2)/2;
161 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
170 /* only exported for WM */
171 void ED_area_overdraw(bContext *C)
173 wmWindow *win= CTX_wm_window(C);
174 bScreen *screen= CTX_wm_screen(C);
177 /* Draw AZones, in screenspace */
178 wmSubWindowSet(win, screen->mainwin);
180 glEnable( GL_BLEND );
181 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
183 for(sa= screen->areabase.first; sa; sa= sa->next) {
185 for(az= sa->actionzones.first; az; az= az->next) {
187 if(az->type==AZONE_TRI) {
188 glColor4ub(0, 0, 0, 70);
189 sdrawtrifill(az->x1, az->y1, az->x2, az->y2);
195 glDisable( GL_BLEND );
199 /* only exported for WM */
200 void ED_region_do_draw(bContext *C, ARegion *ar)
202 wmWindow *win= CTX_wm_window(C);
203 ScrArea *sa= CTX_wm_area(C);
204 ARegionType *at= ar->type;
206 /* note; this sets state, so we can use wmOrtho and friends */
207 wmSubWindowSet(win, ar->swinid);
209 /* optional header info instead? */
213 UI_GetThemeColor3fv(TH_HEADER, col);
214 glClearColor(col[0], col[1], col[2], 0.0);
215 glClear(GL_COLOR_BUFFER_BIT);
217 UI_ThemeColor(TH_MENU_TEXT);
218 glRasterPos2i(20, 6);
219 BMF_DrawString(G.font, ar->headerstr);
228 region_draw_emboss(ar);
230 uiFreeInactiveBlocks(C, &ar->uiblocks);
232 /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
233 ED_region_pixelspace(ar);
236 ar->swap= WIN_BACK_OK;
239 /* **********************************
240 maybe silly, but let's try for now
241 to keep these tags protected
242 ********************************** */
244 void ED_region_tag_redraw(ARegion *ar)
250 void ED_area_tag_redraw(ScrArea *sa)
255 for(ar= sa->regionbase.first; ar; ar= ar->next)
259 void ED_area_tag_refresh(ScrArea *sa)
265 /* *************************************************************** */
267 /* use NULL to disable it */
268 void ED_area_headerprint(ScrArea *sa, const char *str)
272 for(ar= sa->regionbase.first; ar; ar= ar->next) {
273 if(ar->regiontype==RGN_TYPE_HEADER) {
275 if(ar->headerstr==NULL)
276 ar->headerstr= MEM_mallocN(256, "headerprint");
277 BLI_strncpy(ar->headerstr, str, 256);
279 else if(ar->headerstr) {
280 MEM_freeN(ar->headerstr);
283 ED_region_tag_redraw(ar);
288 /* *************************************************************** */
290 /* dir is direction to check, not the splitting edge direction! */
291 static int rct_fits(rcti *rect, char dir, int size)
294 return rect->xmax-rect->xmin - size;
297 return rect->ymax-rect->ymin - size;
301 static void region_rect_recursive(ARegion *ar, rcti *remainder)
303 int prefsizex, prefsizey;
308 /* clear state flags first */
309 ar->flag &= ~RGN_FLAG_TOO_SMALL;
311 ar->alignment= RGN_ALIGN_NONE;
313 prefsizex= ar->type->minsizex;
314 prefsizey= ar->type->minsizey;
316 /* hidden is user flag */
317 if(ar->flag & RGN_FLAG_HIDDEN);
318 /* XXX floating area region, not handled yet here */
319 else if(ar->alignment == RGN_ALIGN_FLOAT);
320 /* remainder is too small for any usage */
321 else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
322 ar->flag |= RGN_FLAG_TOO_SMALL;
324 else if(ar->alignment==RGN_ALIGN_NONE) {
325 /* typically last region */
326 ar->winrct= *remainder;
327 BLI_init_rcti(remainder, 0, 0, 0, 0);
329 else if(ar->alignment==RGN_ALIGN_TOP || ar->alignment==RGN_ALIGN_BOTTOM) {
331 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
332 ar->flag |= RGN_FLAG_TOO_SMALL;
335 int fac= rct_fits(remainder, 'v', prefsizey);
340 ar->winrct= *remainder;
342 if(ar->alignment==RGN_ALIGN_TOP) {
343 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
344 remainder->ymax= ar->winrct.ymin - 1;
347 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
348 remainder->ymin= ar->winrct.ymax + 1;
352 else if(ar->alignment==RGN_ALIGN_LEFT || ar->alignment==RGN_ALIGN_RIGHT) {
354 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
355 ar->flag |= RGN_FLAG_TOO_SMALL;
358 int fac= rct_fits(remainder, 'h', prefsizex);
363 ar->winrct= *remainder;
365 if(ar->alignment==RGN_ALIGN_RIGHT) {
366 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
367 remainder->xmax= ar->winrct.xmin - 1;
370 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
371 remainder->xmin= ar->winrct.xmax + 1;
376 /* percentage subdiv*/
377 ar->winrct= *remainder;
379 if(ar->alignment==RGN_ALIGN_HSPLIT) {
380 if( rct_fits(remainder, 'h', prefsizex) > 4) {
381 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
382 remainder->xmin= ar->winrct.xmax+1;
385 BLI_init_rcti(remainder, 0, 0, 0, 0);
389 if( rct_fits(remainder, 'v', prefsizey) > 4) {
390 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
391 remainder->ymin= ar->winrct.ymax+1;
394 BLI_init_rcti(remainder, 0, 0, 0, 0);
399 ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
400 ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
402 region_rect_recursive(ar->next, remainder);
405 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
408 if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
409 else sa->totrct.xmin= sa->v1->vec.x;
410 if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
411 else sa->totrct.xmax= sa->v4->vec.x;
413 if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
414 else sa->totrct.ymin= sa->v1->vec.y;
415 if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
416 else sa->totrct.ymax= sa->v2->vec.y;
419 sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
420 sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
425 void area_azone_initialize(ScrArea *sa)
428 if(sa->actionzones.first==NULL) {
429 /* set action zones - should these actually be ARegions? With these we can easier check area hotzones */
430 /* (ton) for time being just area, ARegion split is not foreseen on user level */
431 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
432 BLI_addtail(&(sa->actionzones), az);
436 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
437 BLI_addtail(&(sa->actionzones), az);
442 for(az= sa->actionzones.first; az; az= az->next) {
443 if(az->pos==AZONE_SW) {
444 az->x1= sa->v1->vec.x+1;
445 az->y1= sa->v1->vec.y+1;
446 az->x2= sa->v1->vec.x+AZONESPOT;
447 az->y2= sa->v1->vec.y+AZONESPOT;
449 else if (az->pos==AZONE_NE) {
450 az->x1= sa->v3->vec.x;
451 az->y1= sa->v3->vec.y;
452 az->x2= sa->v3->vec.x-AZONESPOT;
453 az->y2= sa->v3->vec.y-AZONESPOT;
458 /* used for area initialize below */
459 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
461 if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
463 wm_subwindow_close(win, ar->swinid);
466 else if(ar->swinid==0)
467 ar->swinid= wm_subwindow_open(win, &ar->winrct);
469 wm_subwindow_position(win, ar->swinid, &ar->winrct);
472 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
474 /* note, add-handler checks if it already exists */
476 // XXX it would be good to have boundbox checks for some of these...
477 if(flag & ED_KEYMAP_UI) {
478 UI_add_region_handlers(handlers);
480 if(flag & ED_KEYMAP_VIEW2D) {
481 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
482 WM_event_add_keymap_handler(handlers, keymap);
484 if(flag & ED_KEYMAP_MARKERS) {
485 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
486 WM_event_add_keymap_handler(handlers, keymap);
487 // XXX need boundbox check urgently!!!
489 if(flag & ED_KEYMAP_ANIMATION) {
490 ListBase *keymap= WM_keymap_listbase(wm, "Animation", 0, 0);
491 WM_event_add_keymap_handler(handlers, keymap);
496 /* called in screen_refresh, or screens_init, also area size changes */
497 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
502 /* set typedefinitions */
503 sa->type= BKE_spacetype_from_id(sa->spacetype);
506 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
507 sa->type= BKE_spacetype_from_id(sa->spacetype);
510 for(ar= sa->regionbase.first; ar; ar= ar->next)
511 ar->type= ED_regiontype_from_id(sa->type, ar->regiontype);
514 area_calc_totrct(sa, win->sizex, win->sizey);
516 /* region rect sizes */
518 region_rect_recursive(sa->regionbase.first, &rect);
520 /* default area handlers */
521 ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
522 /* checks spacedata, adds own handlers */
524 sa->type->init(wm, sa);
526 /* region windows, default and own handlers */
527 for(ar= sa->regionbase.first; ar; ar= ar->next) {
528 region_subwindow(wm, win, ar);
530 /* default region handlers */
531 ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
534 ar->type->init(wm, ar);
537 area_azone_initialize(sa);
540 /* externally called for floating regions like menus */
541 void ED_region_init(bContext *C, ARegion *ar)
543 // ARegionType *at= ar->type;
545 /* refresh can be called before window opened */
546 region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
551 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
552 /* area vertices were set */
553 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
557 sa1->headertype= sa2->headertype;
558 sa1->spacetype= sa2->spacetype;
559 sa1->butspacetype= sa2->butspacetype;
562 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
563 /* exception: ensure preview is reset */
564 // if(sa1->spacetype==SPACE_VIEW3D)
565 // XXX BIF_view3d_previewrender_free(sa1->spacedata.first);
568 BKE_spacedata_freelist(&sa1->spacedata);
569 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
573 BLI_freelistN(&sa1->regionbase);
575 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
576 ARegion *newar= BKE_area_region_copy(ar);
577 BLI_addtail(&sa1->regionbase, newar);
580 #ifndef DISABLE_PYTHON
582 BPY_free_scriptlink(&sa1->scriptlink);
583 sa1->scriptlink= sa2->scriptlink;
584 BPY_copy_scriptlink(&sa1->scriptlink); /* copies internal pointers */
588 /* *********** Space switching code *********** */
590 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
592 if(sa->spacetype != type) {
599 st= BKE_spacetype_from_id(type);
600 slold= sa->spacedata.first;
603 sa->butspacetype= type;
606 /* check previously stored space */
607 for (sl= sa->spacedata.first; sl; sl= sl->next)
608 if(sl->spacetype==type)
611 /* old spacedata... happened during work on 2.50, remove */
612 if(sl && sl->regionbase.first==NULL) {
614 BLI_freelinkN(&sa->spacedata, sl);
621 slold->regionbase= sa->regionbase;
622 sa->regionbase= sl->regionbase;
623 sl->regionbase.first= sl->regionbase.last= NULL;
625 /* put in front of list */
626 BLI_remlink(&sa->spacedata, sl);
627 BLI_addhead(&sa->spacedata, sl);
633 BLI_addhead(&sa->spacedata, sl);
637 slold->regionbase= sa->regionbase;
638 sa->regionbase= sl->regionbase;
639 sl->regionbase.first= sl->regionbase.last= NULL;
643 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
645 /* tell WM to refresh, cursor types etc */
646 WM_event_add_mousemove(C);
648 ED_area_tag_redraw(sa);
652 void ED_area_prevspace(bContext *C)
654 SpaceLink *sl= CTX_wm_space_data(C);
655 ScrArea *sa= CTX_wm_area(C);
658 #if 0 // XXX needs to be space type specific
659 if(sfile->spacetype==SPACE_FILE) {
661 MEM_freeN(sfile->pupmenu);
662 sfile->pupmenu= NULL;
669 #if 0 // XXX check whether this is still needed
670 if (sfile->spacetype == SPACE_SCRIPT) {
671 SpaceScript *sc = (SpaceScript *)sfile;
672 if (sc->script) sc->script->flags &=~SCRIPT_FILESEL;
676 ED_area_newspace(C, sa, sl->next->spacetype);
679 ED_area_newspace(C, sa, SPACE_INFO);
681 ED_area_tag_redraw(sa);
684 static char *windowtype_pup(void)
687 "Window type:%t" //14
692 "|Ipo Curve Editor %x2" //54
693 "|Action Editor %x12" //73
694 "|NLA Editor %x13" //94
698 "|UV/Image Editor %x6" //117
700 "|Video Sequence Editor %x8" //143
701 "|Timeline %x15" //163
702 "|Audio Window %x11" //163
703 "|Text Editor %x9" //179
708 "|User Preferences %x7" //213
709 "|Outliner %x3" //232
710 "|Buttons Window %x4" //251
714 "|File Browser %x5" //290
718 "|Scripts Window %x14"//313
722 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
724 ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
725 ED_area_tag_redraw(CTX_wm_area(C));
728 /* returns offset for next button in header */
729 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
731 ScrArea *sa= CTX_wm_area(C);
735 if(ED_screen_area_active(C)) uiBlockSetCol(block, TH_HEADER);
736 else uiBlockSetCol(block, TH_HEADERDESEL);
738 but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D,
739 windowtype_pup(), xco, yco, XIC+10, YIC,
740 &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0,
741 "Displays Current Window Type. "
742 "Click for menu of available types.");
743 uiButSetFunc(but, spacefunc, NULL, NULL);
747 uiBlockSetEmboss(block, UI_EMBOSSN);
748 if (sa->flag & HEADER_NO_PULLDOWN) {
749 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
750 ICON_DISCLOSURE_TRI_RIGHT,
752 &(sa->flag), 0, 0, 0, 0,
753 "Show pulldown menus");
756 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
757 ICON_DISCLOSURE_TRI_DOWN,
759 &(sa->flag), 0, 0, 0, 0,
760 "Hide pulldown menus");