2.5
[blender.git] / source / blender / editors / screen / area.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
36 #include "BLI_rand.h"
37
38 #include "BKE_global.h"
39 #include "BKE_screen.h"
40 #include "BKE_utildefines.h"
41
42 #include "ED_screen.h"
43 #include "ED_screen_types.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47 #include "wm_subwindow.h"
48
49 #include "BIF_gl.h"
50 #include "BIF_glutil.h"
51
52 #include "UI_interface.h"
53 #include "UI_resources.h"
54 #include "UI_view2d.h"
55
56 #ifndef DISABLE_PYTHON
57 #include "BPY_extern.h"
58 #endif
59
60 #include "ED_util.h"
61
62 #include "screen_intern.h"
63
64 /* general area and region code */
65
66 static void region_draw_emboss(ARegion *ar)
67 {
68         short winx, winy;
69         
70         winx= ar->winrct.xmax-ar->winrct.xmin;
71         winy= ar->winrct.ymax-ar->winrct.ymin;
72         
73         /* set transp line */
74         glEnable( GL_BLEND );
75         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
76         
77         /* right  */
78         glColor4ub(0,0,0, 50);
79         sdrawline(winx, 0, winx, winy);
80         
81         /* bottom  */
82         glColor4ub(0,0,0, 80);
83         sdrawline(0, 0, winx, 0);
84         
85         /* top  */
86         glColor4ub(255,255,255, 60);
87         sdrawline(0, winy, winx, winy);
88
89         /* left  */
90         glColor4ub(255,255,255, 50);
91         sdrawline(0, 0, 0, winy);
92         
93         glDisable( GL_BLEND );
94 }
95
96 void ED_region_pixelspace(const bContext *C, ARegion *ar)
97 {
98         int width= ar->winrct.xmax-ar->winrct.xmin+1;
99         int height= ar->winrct.ymax-ar->winrct.ymin+1;
100         
101         wmOrtho2(C->window, -0.375, (float)width-0.375, -0.375, (float)height-0.375);
102         wmLoadIdentity(C->window);      
103 }
104
105 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
106 {
107         
108         /* generic notes first */
109         switch(note->type) {
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:
115                         ar->do_draw= 1;
116                         break;
117                 default:
118                         if(ar->type->listener)
119                                 ar->type->listener(ar, note);
120         }
121 }
122
123 /* only internal decoration, AZone for now */
124 void ED_area_do_draw(bContext *C, ScrArea *sa)
125 {
126         AZone *az;
127         
128         /* hrmf, screenspace for zones */
129         wm_subwindow_set(C->window, C->window->screen->mainwin);
130         
131         /* temporary viz for 'action corner' */
132         for(az= sa->actionzones.first; az; az= az->next) {
133                 
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 );
140         }
141         
142 }
143
144 void ED_region_do_draw(bContext *C, ARegion *ar)
145 {
146         ARegionType *at= ar->type;
147         
148         wm_subwindow_set(C->window, ar->swinid);
149         
150         if(ar->swinid && at->draw) {
151                 UI_SetTheme(C->area);
152                 at->draw(C, ar);
153                 UI_SetTheme(NULL);
154         }
155         else {
156                 float fac= 0.1*ar->swinid;
157                 
158                 fac= fac - (int)fac;
159                 
160                 glClearColor(0.5, fac, 1.0f-fac, 0.0); 
161                 glClear(GL_COLOR_BUFFER_BIT);
162                 
163                 /* swapbuffers indicator */
164                 fac= BLI_frand();
165                 glColor3f(fac, fac, fac);
166                 glRecti(20,  2,  30,  12);
167         }
168
169         if(C->area)
170                 region_draw_emboss(ar);
171         
172         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
173         ED_region_pixelspace(C, ar);
174         
175         ar->do_draw= 0;
176 }
177
178 /* *************************************************************** */
179
180 /* dir is direction to check, not the splitting edge direction! */
181 static int rct_fits(rcti *rect, char dir, int size)
182 {
183         if(dir=='h') {
184                 return rect->xmax-rect->xmin - size;
185         }
186         else { // 'v'
187                 return rect->ymax-rect->ymin - size;
188         }
189 }
190
191 static void region_rect_recursive(ARegion *ar, rcti *remainder)
192 {
193         int prefsizex, prefsizey;
194         
195         if(ar==NULL)
196                 return;
197         
198         /* clear state flags first */
199         ar->flag &= ~RGN_FLAG_TOO_SMALL;
200         if(ar->next==NULL)
201                 ar->alignment= RGN_ALIGN_NONE;
202         
203         prefsizex= ar->type->minsizex;
204         prefsizey= ar->type->minsizey;
205         
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;
213         }
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);
218         }
219         else if(ar->alignment==RGN_ALIGN_TOP || ar->alignment==RGN_ALIGN_BOTTOM) {
220                 
221                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
222                         ar->flag |= RGN_FLAG_TOO_SMALL;
223                 }
224                 else {
225                         int fac= rct_fits(remainder, 'v', prefsizey);
226
227                         if(fac < 0 )
228                                 prefsizey += fac;
229                         
230                         ar->winrct= *remainder;
231                         
232                         if(ar->alignment==RGN_ALIGN_TOP) {
233                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
234                                 remainder->ymax= ar->winrct.ymin - 1;
235                         }
236                         else {
237                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
238                                 remainder->ymin= ar->winrct.ymax + 1;
239                         }
240                 }
241         }
242         else if(ar->alignment==RGN_ALIGN_LEFT || ar->alignment==RGN_ALIGN_RIGHT) {
243                 
244                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
245                         ar->flag |= RGN_FLAG_TOO_SMALL;
246                 }
247                 else {
248                         int fac= rct_fits(remainder, 'h', prefsizex);
249                         
250                         if(fac < 0 )
251                                 prefsizex += fac;
252                         
253                         ar->winrct= *remainder;
254                         
255                         if(ar->alignment==RGN_ALIGN_RIGHT) {
256                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
257                                 remainder->xmax= ar->winrct.xmin - 1;
258                         }
259                         else {
260                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
261                                 remainder->xmin= ar->winrct.xmax + 1;
262                         }
263                 }
264         }
265         else {
266                 /* percentage subdiv*/
267                 ar->winrct= *remainder;
268                 
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;
273                         }
274                         else {
275                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
276                         }
277                 }
278                 else {
279                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
280                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
281                                 remainder->ymin= ar->winrct.ymax+1;
282                         }
283                         else {
284                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
285                         }
286                 }
287         }
288         /* for speedup */
289         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
290         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
291         
292         region_rect_recursive(ar->next, remainder);
293 }
294
295 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
296 {
297         
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;
302         
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;
307         
308         /* for speedup */
309         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
310         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
311 }
312
313 #define AZONESPOT               12
314 void area_azone_initialize(ScrArea *sa) 
315 {
316         AZone *az;
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);
322                 az->type= AZONE_TRI;
323                 az->pos= AZONE_SW;
324                 
325                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
326                 BLI_addtail(&(sa->actionzones), az);
327                 az->type= AZONE_TRI;
328                 az->pos= AZONE_NE;
329         }
330         
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;
337                 } 
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;
343                 }
344         }
345 }
346
347 /* used for area initialize below */
348 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
349 {
350         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
351                 if(ar->swinid)
352                         wm_subwindow_close(win, ar->swinid);
353                 ar->swinid= 0;
354         }
355         else if(ar->swinid==0)
356                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
357         else 
358                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
359 }
360
361 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
362 {
363         /* note, add-handler checks if it already exists */
364         
365         if(flag & ED_KEYMAP_UI) {
366                 UI_add_region_handlers(handlers);
367         }
368         if(flag & ED_KEYMAP_VIEW2D) {
369                 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
370                 WM_event_add_keymap_handler(handlers, keymap);
371         }
372         if(flag & ED_KEYMAP_MARKERS) {
373                 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
374                 WM_event_add_keymap_handler(handlers, keymap);
375         }
376 }
377
378
379 /* called in screen_refresh, or screens_init, also area size changes */
380 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
381 {
382         ARegion *ar;
383         rcti rect;
384         
385         /* set typedefinitions */
386         sa->type= BKE_spacetype_from_id(sa->spacetype);
387         
388         if(sa->type==NULL) {
389                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
390                 sa->type= BKE_spacetype_from_id(sa->spacetype);
391         }
392         
393         for(ar= sa->regionbase.first; ar; ar= ar->next)
394                 ar->type= ED_regiontype_from_id(sa->type, ar->regiontype);
395         
396         /* area sizes */
397         area_calc_totrct(sa, win->sizex, win->sizey);
398         
399         /* region rect sizes */
400         rect= sa->totrct;
401         region_rect_recursive(sa->regionbase.first, &rect);
402         
403         /* default area handlers */
404         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
405         /* checks spacedata, adds own handlers */
406         if(sa->type->init)
407                 sa->type->init(wm, sa);
408         
409         /* region windows, default and own handlers */
410         for(ar= sa->regionbase.first; ar; ar= ar->next) {
411                 region_subwindow(wm, win, ar);
412                 
413                 /* default region handlers */
414                 ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
415
416                 if(ar->type->init)
417                         ar->type->init(wm, ar);
418                 
419         }
420         area_azone_initialize(sa);
421 }
422
423 /* externally called for floating regions like menus */
424 void ED_region_init(bContext *C, ARegion *ar)
425 {
426 //      ARegionType *at= ar->type;
427         
428         /* refresh can be called before window opened */
429         region_subwindow(C->wm, C->window, ar);
430         
431 }
432
433
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)
437 {
438         Panel *pa1, *pa2, *patab;
439         ARegion *ar;
440         
441         sa1->headertype= sa2->headertype;
442         sa1->spacetype= sa2->spacetype;
443         
444         if(swap_space) {
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);
449         }
450         else {
451                 BKE_spacedata_freelist(&sa1->spacedata);
452                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
453         }
454         
455         BLI_freelistN(&sa1->panels);
456         BLI_duplicatelist(&sa1->panels, &sa2->panels);
457         
458         /* copy panel pointers */
459         for(pa1= sa1->panels.first; pa1; pa1= pa1->next) {
460                 
461                 patab= sa1->panels.first;
462                 pa2= sa2->panels.first;
463                 while(patab) {
464                         if( pa1->paneltab == pa2) {
465                                 pa1->paneltab = patab;
466                                 break;
467                         }
468                         patab= patab->next;
469                         pa2= pa2->next;
470                 }
471         }
472         
473         /* regions... XXX */
474         BLI_freelistN(&sa1->regionbase);
475         
476         for(ar= sa2->regionbase.first; ar; ar= ar->next) {
477                 ARegion *newar= BKE_area_region_copy(ar);
478                 BLI_addtail(&sa1->regionbase, newar);
479         }
480                 
481 #ifndef DISABLE_PYTHON
482         /* scripts */
483         BPY_free_scriptlink(&sa1->scriptlink);
484         sa1->scriptlink= sa2->scriptlink;
485         BPY_copy_scriptlink(&sa1->scriptlink);  /* copies internal pointers */
486 #endif
487 }
488
489 /* *********** Space switching code, local now *********** */
490 /* XXX make operator for this */
491
492 static void newspace(bContext *C, ScrArea *sa, int type)
493 {
494         if(sa->spacetype != type) {
495                 SpaceType *st;
496                 SpaceLink *slold;
497                 SpaceLink *sl;
498
499                 ED_area_exit(C, sa);
500
501                 st= BKE_spacetype_from_id(type);
502                 slold= sa->spacedata.first;
503
504                 sa->spacetype= type;
505                 sa->butspacetype= type;
506                 sa->type= st;
507                 
508                 /* check previously stored space */
509                 for (sl= sa->spacedata.first; sl; sl= sl->next)
510                         if(sl->spacetype==type)
511                                 break;
512                 
513                 /* old spacedata... happened during work on 2.50, remove */
514                 if(sl && sl->regionbase.first==NULL) {
515                         st->free(sl);
516                         MEM_freeN(sl);
517                         sl= NULL;
518                 }
519                 
520                 if (sl) {
521                         
522                         /* swap regions */
523                         slold->regionbase= sa->regionbase;
524                         sa->regionbase= sl->regionbase;
525                         sl->regionbase.first= sl->regionbase.last= NULL;
526                         
527                         /* put in front of list */
528                         BLI_remlink(&sa->spacedata, sl);
529                         BLI_addhead(&sa->spacedata, sl);
530                 } 
531                 else {
532                         /* new space */
533                         if(st) {
534                                 sl= st->new();
535                                 BLI_addhead(&sa->spacedata, sl);
536                                 
537                                 /* swap regions */
538                                 slold->regionbase= sa->regionbase;
539                                 sa->regionbase= sl->regionbase;
540                                 sl->regionbase.first= sl->regionbase.last= NULL;
541                         }
542                 }
543                 
544         }
545 }
546
547 static char *windowtype_pup(void)
548 {
549         return(
550                    "Window type:%t" //14
551                    "|3D View %x1" //30
552                    
553                    "|%l" // 33
554                    
555                    "|Ipo Curve Editor %x2" //54
556                    "|Action Editor %x12" //73
557                    "|NLA Editor %x13" //94
558                    
559                    "|%l" //97
560                    
561                    "|UV/Image Editor %x6" //117
562                    
563                    "|Video Sequence Editor %x8" //143
564                    "|Timeline %x15" //163
565                    "|Audio Window %x11" //163
566                    "|Text Editor %x9" //179
567                    
568                    "|%l" //192
569                    
570                    
571                    "|User Preferences %x7" //213
572                    "|Outliner %x3" //232
573                    "|Buttons Window %x4" //251
574                    "|Node Editor %x16"
575                    "|%l" //254
576                    
577                    "|Image Browser %x10" //273
578                    "|File Browser %x5" //290
579                    
580                    "|%l" //293
581                    
582                    "|Scripts Window %x14"//313
583                    );
584 }
585
586 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
587 {
588         newspace(C, C->area, C->area->butspacetype);
589         WM_event_add_notifier(C, WM_NOTE_SCREEN_CHANGED, 0, NULL);
590 }
591
592 /* returns offset for next button in header */
593 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
594 {
595         uiBut *but;
596         int xco= 8;
597         
598         if(ED_screen_area_active(C)) uiBlockSetCol(block, TH_HEADER);
599         else uiBlockSetCol(block, TH_HEADERDESEL);
600         
601         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
602                                                    windowtype_pup(), xco, yco, XIC+10, YIC, 
603                                                    &(C->area->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
604                                                    "Displays Current Window Type. "
605                                                    "Click for menu of available types.");
606         uiButSetFunc(but, spacefunc, NULL, NULL);
607         
608         xco += XIC + 14;
609         
610         uiBlockSetEmboss(block, UI_EMBOSSN);
611         if (C->area->flag & HEADER_NO_PULLDOWN) {
612                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
613                                                  ICON_DISCLOSURE_TRI_RIGHT,
614                                                  xco,yco,XIC,YIC-2,
615                                                  &(C->area->flag), 0, 0, 0, 0, 
616                                                  "Show pulldown menus");
617         }
618         else {
619                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
620                                                  ICON_DISCLOSURE_TRI_DOWN,
621                                                  xco,yco,XIC,YIC-2,
622                                                  &(C->area->flag), 0, 0, 0, 0, 
623                                                  "Hide pulldown menus");
624         }
625         xco+=XIC;
626         
627         return xco;
628 }
629