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 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
109 /* generic notes first */
111 case WM_NOTE_WINDOW_REDRAW:
112 case WM_NOTE_SCREEN_CHANGED:
113 ED_region_tag_redraw(ar);
115 case WM_NOTE_GESTURE_REDRAW:
116 if(note->swinid==ar->swinid)
117 ED_region_tag_redraw(ar);
120 if(ar->type->listener)
121 ar->type->listener(ar, note);
125 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
126 void ED_area_overdraw_flush(bContext *C)
130 for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
133 for(ar= sa->regionbase.first; ar; ar= ar->next) {
137 for(az= sa->actionzones.first; az; az= az->next) {
138 int xs= (az->x1+az->x2)/2, ys= (az->y1+az->y2)/2;
141 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
150 void ED_area_overdraw(bContext *C)
152 wmWindow *win= CTX_wm_window(C);
153 bScreen *screen= CTX_wm_screen(C);
156 /* Draw AZones, in screenspace */
157 wmSubWindowSet(win, screen->mainwin);
159 glEnable( GL_BLEND );
160 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
162 for(sa= screen->areabase.first; sa; sa= sa->next) {
164 for(az= sa->actionzones.first; az; az= az->next) {
166 if(az->type==AZONE_TRI) {
167 glColor4ub(0, 0, 0, 70);
168 sdrawtrifill(az->x1, az->y1, az->x2, az->y2);
174 glDisable( GL_BLEND );
178 void ED_region_do_draw(bContext *C, ARegion *ar)
180 wmWindow *win= CTX_wm_window(C);
181 ScrArea *sa= CTX_wm_area(C);
182 ARegionType *at= ar->type;
184 /* note; this sets state, so we can use wmOrtho and friends */
185 wmSubWindowSet(win, ar->swinid);
188 /* optional header info instead? */
192 UI_GetThemeColor3fv(TH_HEADER, col);
193 glClearColor(col[0], col[1], col[2], 0.0);
194 glClear(GL_COLOR_BUFFER_BIT);
196 UI_ThemeColor(TH_MENU_TEXT);
197 glRasterPos2i(20, 6);
198 BMF_DrawString(G.font, ar->headerstr);
207 region_draw_emboss(ar);
209 /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
210 ED_region_pixelspace(ar);
216 /* **********************************
217 maybe silly, but let's try for now
218 to keep do_draw tags protected
219 ********************************** */
221 void ED_region_tag_redraw(ARegion *ar)
227 void ED_area_tag_redraw(ScrArea *sa)
232 for(ar= sa->regionbase.first; ar; ar= ar->next)
237 /* *************************************************************** */
239 /* use NULL to disable it */
240 void ED_area_headerprint(ScrArea *sa, const char *str)
244 for(ar= sa->regionbase.first; ar; ar= ar->next) {
245 if(ar->regiontype==RGN_TYPE_HEADER) {
247 if(ar->headerstr==NULL)
248 ar->headerstr= MEM_mallocN(256, "headerprint");
249 BLI_strncpy(ar->headerstr, str, 256);
251 else if(ar->headerstr) {
252 MEM_freeN(ar->headerstr);
255 ED_region_tag_redraw(ar);
260 /* *************************************************************** */
262 /* dir is direction to check, not the splitting edge direction! */
263 static int rct_fits(rcti *rect, char dir, int size)
266 return rect->xmax-rect->xmin - size;
269 return rect->ymax-rect->ymin - size;
273 static void region_rect_recursive(ARegion *ar, rcti *remainder)
275 int prefsizex, prefsizey;
280 /* clear state flags first */
281 ar->flag &= ~RGN_FLAG_TOO_SMALL;
283 ar->alignment= RGN_ALIGN_NONE;
285 prefsizex= ar->type->minsizex;
286 prefsizey= ar->type->minsizey;
288 /* hidden is user flag */
289 if(ar->flag & RGN_FLAG_HIDDEN);
290 /* XXX floating area region, not handled yet here */
291 else if(ar->alignment == RGN_ALIGN_FLOAT);
292 /* remainder is too small for any usage */
293 else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
294 ar->flag |= RGN_FLAG_TOO_SMALL;
296 else if(ar->alignment==RGN_ALIGN_NONE) {
297 /* typically last region */
298 ar->winrct= *remainder;
299 BLI_init_rcti(remainder, 0, 0, 0, 0);
301 else if(ar->alignment==RGN_ALIGN_TOP || ar->alignment==RGN_ALIGN_BOTTOM) {
303 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
304 ar->flag |= RGN_FLAG_TOO_SMALL;
307 int fac= rct_fits(remainder, 'v', prefsizey);
312 ar->winrct= *remainder;
314 if(ar->alignment==RGN_ALIGN_TOP) {
315 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
316 remainder->ymax= ar->winrct.ymin - 1;
319 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
320 remainder->ymin= ar->winrct.ymax + 1;
324 else if(ar->alignment==RGN_ALIGN_LEFT || ar->alignment==RGN_ALIGN_RIGHT) {
326 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
327 ar->flag |= RGN_FLAG_TOO_SMALL;
330 int fac= rct_fits(remainder, 'h', prefsizex);
335 ar->winrct= *remainder;
337 if(ar->alignment==RGN_ALIGN_RIGHT) {
338 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
339 remainder->xmax= ar->winrct.xmin - 1;
342 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
343 remainder->xmin= ar->winrct.xmax + 1;
348 /* percentage subdiv*/
349 ar->winrct= *remainder;
351 if(ar->alignment==RGN_ALIGN_HSPLIT) {
352 if( rct_fits(remainder, 'h', prefsizex) > 4) {
353 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
354 remainder->xmin= ar->winrct.xmax+1;
357 BLI_init_rcti(remainder, 0, 0, 0, 0);
361 if( rct_fits(remainder, 'v', prefsizey) > 4) {
362 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
363 remainder->ymin= ar->winrct.ymax+1;
366 BLI_init_rcti(remainder, 0, 0, 0, 0);
371 ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
372 ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
374 region_rect_recursive(ar->next, remainder);
377 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
380 if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
381 else sa->totrct.xmin= sa->v1->vec.x;
382 if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
383 else sa->totrct.xmax= sa->v4->vec.x;
385 if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
386 else sa->totrct.ymin= sa->v1->vec.y;
387 if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
388 else sa->totrct.ymax= sa->v2->vec.y;
391 sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
392 sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
397 void area_azone_initialize(ScrArea *sa)
400 if(sa->actionzones.first==NULL) {
401 /* set action zones - should these actually be ARegions? With these we can easier check area hotzones */
402 /* (ton) for time being just area, ARegion split is not foreseen on user level */
403 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
404 BLI_addtail(&(sa->actionzones), az);
408 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
409 BLI_addtail(&(sa->actionzones), az);
414 for(az= sa->actionzones.first; az; az= az->next) {
415 if(az->pos==AZONE_SW) {
416 az->x1= sa->v1->vec.x+1;
417 az->y1= sa->v1->vec.y+1;
418 az->x2= sa->v1->vec.x+AZONESPOT;
419 az->y2= sa->v1->vec.y+AZONESPOT;
421 else if (az->pos==AZONE_NE) {
422 az->x1= sa->v3->vec.x;
423 az->y1= sa->v3->vec.y;
424 az->x2= sa->v3->vec.x-AZONESPOT;
425 az->y2= sa->v3->vec.y-AZONESPOT;
430 /* used for area initialize below */
431 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
433 if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
435 wm_subwindow_close(win, ar->swinid);
438 else if(ar->swinid==0)
439 ar->swinid= wm_subwindow_open(win, &ar->winrct);
441 wm_subwindow_position(win, ar->swinid, &ar->winrct);
444 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
446 /* note, add-handler checks if it already exists */
448 // XXX it would be good to have boundbox checks for some of these...
449 if(flag & ED_KEYMAP_UI) {
450 UI_add_region_handlers(handlers);
452 if(flag & ED_KEYMAP_VIEW2D) {
453 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
454 WM_event_add_keymap_handler(handlers, keymap);
456 if(flag & ED_KEYMAP_MARKERS) {
457 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
458 WM_event_add_keymap_handler(handlers, keymap);
460 if(flag & ED_KEYMAP_ANIMATION) {
461 ListBase *keymap= WM_keymap_listbase(wm, "Animation", 0, 0);
462 WM_event_add_keymap_handler(handlers, keymap);
467 /* called in screen_refresh, or screens_init, also area size changes */
468 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
473 /* set typedefinitions */
474 sa->type= BKE_spacetype_from_id(sa->spacetype);
477 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
478 sa->type= BKE_spacetype_from_id(sa->spacetype);
481 for(ar= sa->regionbase.first; ar; ar= ar->next)
482 ar->type= ED_regiontype_from_id(sa->type, ar->regiontype);
485 area_calc_totrct(sa, win->sizex, win->sizey);
487 /* region rect sizes */
489 region_rect_recursive(sa->regionbase.first, &rect);
491 /* default area handlers */
492 ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
493 /* checks spacedata, adds own handlers */
495 sa->type->init(wm, sa);
497 /* region windows, default and own handlers */
498 for(ar= sa->regionbase.first; ar; ar= ar->next) {
499 region_subwindow(wm, win, ar);
501 /* default region handlers */
502 ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
505 ar->type->init(wm, ar);
508 area_azone_initialize(sa);
511 /* externally called for floating regions like menus */
512 void ED_region_init(bContext *C, ARegion *ar)
514 // ARegionType *at= ar->type;
516 /* refresh can be called before window opened */
517 region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
522 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
523 /* area vertices were set */
524 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
526 Panel *pa1, *pa2, *patab;
529 sa1->headertype= sa2->headertype;
530 sa1->spacetype= sa2->spacetype;
531 sa1->butspacetype= sa2->butspacetype;
534 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
535 /* exception: ensure preview is reset */
536 // if(sa1->spacetype==SPACE_VIEW3D)
537 // XXX BIF_view3d_previewrender_free(sa1->spacedata.first);
540 BKE_spacedata_freelist(&sa1->spacedata);
541 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
544 BLI_freelistN(&sa1->panels);
545 BLI_duplicatelist(&sa1->panels, &sa2->panels);
547 /* copy panel pointers */
548 for(pa1= sa1->panels.first; pa1; pa1= pa1->next) {
550 patab= sa1->panels.first;
551 pa2= sa2->panels.first;
553 if( pa1->paneltab == pa2) {
554 pa1->paneltab = patab;
563 BLI_freelistN(&sa1->regionbase);
565 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
566 ARegion *newar= BKE_area_region_copy(ar);
567 BLI_addtail(&sa1->regionbase, newar);
570 #ifndef DISABLE_PYTHON
572 BPY_free_scriptlink(&sa1->scriptlink);
573 sa1->scriptlink= sa2->scriptlink;
574 BPY_copy_scriptlink(&sa1->scriptlink); /* copies internal pointers */
578 /* *********** Space switching code, local now *********** */
579 /* XXX make operator for this */
581 void area_newspace(bContext *C, ScrArea *sa, int type)
583 if(sa->spacetype != type) {
590 st= BKE_spacetype_from_id(type);
591 slold= sa->spacedata.first;
594 sa->butspacetype= type;
597 /* check previously stored space */
598 for (sl= sa->spacedata.first; sl; sl= sl->next)
599 if(sl->spacetype==type)
602 /* old spacedata... happened during work on 2.50, remove */
603 if(sl && sl->regionbase.first==NULL) {
612 slold->regionbase= sa->regionbase;
613 sa->regionbase= sl->regionbase;
614 sl->regionbase.first= sl->regionbase.last= NULL;
616 /* put in front of list */
617 BLI_remlink(&sa->spacedata, sl);
618 BLI_addhead(&sa->spacedata, sl);
624 BLI_addhead(&sa->spacedata, sl);
628 slold->regionbase= sa->regionbase;
629 sa->regionbase= sl->regionbase;
630 sl->regionbase.first= sl->regionbase.last= NULL;
634 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
636 /* tell WM to refresh, cursor types etc */
637 WM_event_add_mousemove(C);
641 static char *windowtype_pup(void)
644 "Window type:%t" //14
649 "|Ipo Curve Editor %x2" //54
650 "|Action Editor %x12" //73
651 "|NLA Editor %x13" //94
655 "|UV/Image Editor %x6" //117
657 "|Video Sequence Editor %x8" //143
658 "|Timeline %x15" //163
659 "|Audio Window %x11" //163
660 "|Text Editor %x9" //179
665 "|User Preferences %x7" //213
666 "|Outliner %x3" //232
667 "|Buttons Window %x4" //251
671 "|File Browser %x5" //290
675 "|Scripts Window %x14"//313
679 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
681 area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
682 ED_area_tag_redraw(CTX_wm_area(C));
685 /* returns offset for next button in header */
686 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
688 ScrArea *sa= CTX_wm_area(C);
692 if(ED_screen_area_active(C)) uiBlockSetCol(block, TH_HEADER);
693 else uiBlockSetCol(block, TH_HEADERDESEL);
695 but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D,
696 windowtype_pup(), xco, yco, XIC+10, YIC,
697 &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0,
698 "Displays Current Window Type. "
699 "Click for menu of available types.");
700 uiButSetFunc(but, spacefunc, NULL, NULL);
704 uiBlockSetEmboss(block, UI_EMBOSSN);
705 if (sa->flag & HEADER_NO_PULLDOWN) {
706 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
707 ICON_DISCLOSURE_TRI_RIGHT,
709 &(sa->flag), 0, 0, 0, 0,
710 "Show pulldown menus");
713 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
714 ICON_DISCLOSURE_TRI_DOWN,
716 &(sa->flag), 0, 0, 0, 0,
717 "Hide pulldown menus");