2.5
[blender-staging.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         /* generic notes first */
108         switch(note->type) {
109                 case WM_NOTE_WINDOW_REDRAW:
110                 case WM_NOTE_GESTURE_REDRAW:
111                 case WM_NOTE_SCREEN_CHANGED:
112                         ED_region_tag_redraw(ar);
113                         break;
114                 default:
115                         if(ar->type->listener)
116                                 ar->type->listener(ar, note);
117         }
118 }
119
120 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
121 void ED_area_overdraw_flush(bContext *C)
122 {
123         ScrArea *sa;
124         
125         for(sa= C->screen->areabase.first; sa; sa= sa->next) {
126                 ARegion *ar;
127                 
128                 for(ar= sa->regionbase.first; ar; ar= ar->next) {
129                         if(ar->do_draw) {
130                                 AZone *az;
131                                 
132                                 for(az= sa->actionzones.first; az; az= az->next) {
133                                         int xs= (az->x1+az->x2)/2, ys= (az->y1+az->y2)/2;
134                 
135                                         /* test if inside */
136                                         if(BLI_in_rcti(&ar->winrct, xs, ys)) {
137                                                 az->do_draw= 1;
138                                         }
139                                 }
140                         }
141                 }
142         }       
143 }
144
145 void ED_area_overdraw(bContext *C)
146 {
147         ScrArea *sa;
148         
149         /* Draw AZones, in screenspace */
150         wm_subwindow_set(C->window, C->window->screen->mainwin);
151
152         glEnable( GL_BLEND );
153         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
154         
155         for(sa= C->screen->areabase.first; sa; sa= sa->next) {
156                 AZone *az;
157                 for(az= sa->actionzones.first; az; az= az->next) {
158                         if(az->do_draw) {
159                                 if(az->type==AZONE_TRI) {
160                                         glColor4ub(0, 0, 0, 70);
161                                         sdrawtrifill(az->x1, az->y1, az->x2, az->y2);
162                                 }
163                                 az->do_draw= 0;
164                         }
165                 }
166         }       
167         glDisable( GL_BLEND );
168         
169 }
170
171 void ED_region_do_draw(bContext *C, ARegion *ar)
172 {
173         ARegionType *at= ar->type;
174         
175         wm_subwindow_set(C->window, ar->swinid);
176         
177         if(ar->swinid && at->draw) {
178                 UI_SetTheme(C->area);
179                 at->draw(C, ar);
180                 UI_SetTheme(NULL);
181         }
182         else {
183                 float fac= 0.1*ar->swinid;
184                 
185                 fac= fac - (int)fac;
186                 
187                 glClearColor(0.5, fac, 1.0f-fac, 0.0); 
188                 glClear(GL_COLOR_BUFFER_BIT);
189                 
190                 /* swapbuffers indicator */
191                 fac= BLI_frand();
192                 glColor3f(fac, fac, fac);
193                 glRecti(20,  2,  30,  12);
194         }
195
196         if(C->area)
197                 region_draw_emboss(ar);
198         
199         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
200         ED_region_pixelspace(C, ar);
201         
202         ar->do_draw= 0;
203 }
204
205 /* **********************************
206    maybe silly, but let's try for now
207    to keep do_draw tags protected
208    ********************************** */
209
210 void ED_region_tag_redraw(ARegion *ar)
211 {
212         if(ar)
213                 ar->do_draw= 1;
214 }
215
216 void ED_area_tag_redraw(ScrArea *sa)
217 {
218         ARegion *ar;
219         
220         if(sa)
221                 for(ar= sa->regionbase.first; ar; ar= ar->next)
222                         ar->do_draw= 1;
223 }
224
225
226
227 /* *************************************************************** */
228
229 /* dir is direction to check, not the splitting edge direction! */
230 static int rct_fits(rcti *rect, char dir, int size)
231 {
232         if(dir=='h') {
233                 return rect->xmax-rect->xmin - size;
234         }
235         else { // 'v'
236                 return rect->ymax-rect->ymin - size;
237         }
238 }
239
240 static void region_rect_recursive(ARegion *ar, rcti *remainder)
241 {
242         int prefsizex, prefsizey;
243         
244         if(ar==NULL)
245                 return;
246         
247         /* clear state flags first */
248         ar->flag &= ~RGN_FLAG_TOO_SMALL;
249         if(ar->next==NULL)
250                 ar->alignment= RGN_ALIGN_NONE;
251         
252         prefsizex= ar->type->minsizex;
253         prefsizey= ar->type->minsizey;
254         
255         /* hidden is user flag */
256         if(ar->flag & RGN_FLAG_HIDDEN);
257         /* XXX floating area region, not handled yet here */
258         else if(ar->alignment == RGN_ALIGN_FLOAT);
259         /* remainder is too small for any usage */
260         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
261                 ar->flag |= RGN_FLAG_TOO_SMALL;
262         }
263         else if(ar->alignment==RGN_ALIGN_NONE) {
264                 /* typically last region */
265                 ar->winrct= *remainder;
266                 BLI_init_rcti(remainder, 0, 0, 0, 0);
267         }
268         else if(ar->alignment==RGN_ALIGN_TOP || ar->alignment==RGN_ALIGN_BOTTOM) {
269                 
270                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
271                         ar->flag |= RGN_FLAG_TOO_SMALL;
272                 }
273                 else {
274                         int fac= rct_fits(remainder, 'v', prefsizey);
275
276                         if(fac < 0 )
277                                 prefsizey += fac;
278                         
279                         ar->winrct= *remainder;
280                         
281                         if(ar->alignment==RGN_ALIGN_TOP) {
282                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
283                                 remainder->ymax= ar->winrct.ymin - 1;
284                         }
285                         else {
286                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
287                                 remainder->ymin= ar->winrct.ymax + 1;
288                         }
289                 }
290         }
291         else if(ar->alignment==RGN_ALIGN_LEFT || ar->alignment==RGN_ALIGN_RIGHT) {
292                 
293                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
294                         ar->flag |= RGN_FLAG_TOO_SMALL;
295                 }
296                 else {
297                         int fac= rct_fits(remainder, 'h', prefsizex);
298                         
299                         if(fac < 0 )
300                                 prefsizex += fac;
301                         
302                         ar->winrct= *remainder;
303                         
304                         if(ar->alignment==RGN_ALIGN_RIGHT) {
305                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
306                                 remainder->xmax= ar->winrct.xmin - 1;
307                         }
308                         else {
309                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
310                                 remainder->xmin= ar->winrct.xmax + 1;
311                         }
312                 }
313         }
314         else {
315                 /* percentage subdiv*/
316                 ar->winrct= *remainder;
317                 
318                 if(ar->alignment==RGN_ALIGN_HSPLIT) {
319                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
320                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
321                                 remainder->xmin= ar->winrct.xmax+1;
322                         }
323                         else {
324                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
325                         }
326                 }
327                 else {
328                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
329                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
330                                 remainder->ymin= ar->winrct.ymax+1;
331                         }
332                         else {
333                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
334                         }
335                 }
336         }
337         /* for speedup */
338         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
339         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
340         
341         region_rect_recursive(ar->next, remainder);
342 }
343
344 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
345 {
346         
347         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
348         else sa->totrct.xmin= sa->v1->vec.x;
349         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
350         else sa->totrct.xmax= sa->v4->vec.x;
351         
352         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
353         else sa->totrct.ymin= sa->v1->vec.y;
354         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
355         else sa->totrct.ymax= sa->v2->vec.y;
356         
357         /* for speedup */
358         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
359         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
360 }
361
362
363 #define AZONESPOT               12
364 void area_azone_initialize(ScrArea *sa) 
365 {
366         AZone *az;
367         if(sa->actionzones.first==NULL) {
368                 /* set action zones - should these actually be ARegions? With these we can easier check area hotzones */
369                 /* (ton) for time being just area, ARegion split is not foreseen on user level */
370                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
371                 BLI_addtail(&(sa->actionzones), az);
372                 az->type= AZONE_TRI;
373                 az->pos= AZONE_SW;
374                 
375                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
376                 BLI_addtail(&(sa->actionzones), az);
377                 az->type= AZONE_TRI;
378                 az->pos= AZONE_NE;
379         }
380         
381         for(az= sa->actionzones.first; az; az= az->next) {
382                 if(az->pos==AZONE_SW) {
383                         az->x1= sa->v1->vec.x+1;
384                         az->y1= sa->v1->vec.y+1;
385                         az->x2= sa->v1->vec.x+AZONESPOT;
386                         az->y2= sa->v1->vec.y+AZONESPOT;
387                 } 
388                 else if (az->pos==AZONE_NE) {
389                         az->x1= sa->v3->vec.x;
390                         az->y1= sa->v3->vec.y;
391                         az->x2= sa->v3->vec.x-AZONESPOT;
392                         az->y2= sa->v3->vec.y-AZONESPOT;
393                 }
394         }
395 }
396
397 /* used for area initialize below */
398 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
399 {
400         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
401                 if(ar->swinid)
402                         wm_subwindow_close(win, ar->swinid);
403                 ar->swinid= 0;
404         }
405         else if(ar->swinid==0)
406                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
407         else 
408                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
409 }
410
411 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
412 {
413         /* note, add-handler checks if it already exists */
414         
415         if(flag & ED_KEYMAP_UI) {
416                 UI_add_region_handlers(handlers);
417         }
418         if(flag & ED_KEYMAP_VIEW2D) {
419                 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
420                 WM_event_add_keymap_handler(handlers, keymap);
421         }
422         if(flag & ED_KEYMAP_MARKERS) {
423                 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
424                 WM_event_add_keymap_handler(handlers, keymap);
425         }
426 }
427
428
429 /* called in screen_refresh, or screens_init, also area size changes */
430 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
431 {
432         ARegion *ar;
433         rcti rect;
434         
435         /* set typedefinitions */
436         sa->type= BKE_spacetype_from_id(sa->spacetype);
437         
438         if(sa->type==NULL) {
439                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
440                 sa->type= BKE_spacetype_from_id(sa->spacetype);
441         }
442         
443         for(ar= sa->regionbase.first; ar; ar= ar->next)
444                 ar->type= ED_regiontype_from_id(sa->type, ar->regiontype);
445         
446         /* area sizes */
447         area_calc_totrct(sa, win->sizex, win->sizey);
448         
449         /* region rect sizes */
450         rect= sa->totrct;
451         region_rect_recursive(sa->regionbase.first, &rect);
452         
453         /* default area handlers */
454         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
455         /* checks spacedata, adds own handlers */
456         if(sa->type->init)
457                 sa->type->init(wm, sa);
458         
459         /* region windows, default and own handlers */
460         for(ar= sa->regionbase.first; ar; ar= ar->next) {
461                 region_subwindow(wm, win, ar);
462                 
463                 /* default region handlers */
464                 ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
465
466                 if(ar->type->init)
467                         ar->type->init(wm, ar);
468                 
469         }
470         area_azone_initialize(sa);
471 }
472
473 /* externally called for floating regions like menus */
474 void ED_region_init(bContext *C, ARegion *ar)
475 {
476 //      ARegionType *at= ar->type;
477         
478         /* refresh can be called before window opened */
479         region_subwindow(C->wm, C->window, ar);
480         
481 }
482
483
484 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
485 /* area vertices were set */
486 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
487 {
488         Panel *pa1, *pa2, *patab;
489         ARegion *ar;
490         
491         sa1->headertype= sa2->headertype;
492         sa1->spacetype= sa2->spacetype;
493         
494         if(swap_space) {
495                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
496                 /* exception: ensure preview is reset */
497 //              if(sa1->spacetype==SPACE_VIEW3D)
498 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
499         }
500         else {
501                 BKE_spacedata_freelist(&sa1->spacedata);
502                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
503         }
504         
505         BLI_freelistN(&sa1->panels);
506         BLI_duplicatelist(&sa1->panels, &sa2->panels);
507         
508         /* copy panel pointers */
509         for(pa1= sa1->panels.first; pa1; pa1= pa1->next) {
510                 
511                 patab= sa1->panels.first;
512                 pa2= sa2->panels.first;
513                 while(patab) {
514                         if( pa1->paneltab == pa2) {
515                                 pa1->paneltab = patab;
516                                 break;
517                         }
518                         patab= patab->next;
519                         pa2= pa2->next;
520                 }
521         }
522         
523         /* regions... XXX */
524         BLI_freelistN(&sa1->regionbase);
525         
526         for(ar= sa2->regionbase.first; ar; ar= ar->next) {
527                 ARegion *newar= BKE_area_region_copy(ar);
528                 BLI_addtail(&sa1->regionbase, newar);
529         }
530                 
531 #ifndef DISABLE_PYTHON
532         /* scripts */
533         BPY_free_scriptlink(&sa1->scriptlink);
534         sa1->scriptlink= sa2->scriptlink;
535         BPY_copy_scriptlink(&sa1->scriptlink);  /* copies internal pointers */
536 #endif
537 }
538
539 /* *********** Space switching code, local now *********** */
540 /* XXX make operator for this */
541
542 static void area_newspace(bContext *C, ScrArea *sa, int type)
543 {
544         if(sa->spacetype != type) {
545                 SpaceType *st;
546                 SpaceLink *slold;
547                 SpaceLink *sl;
548
549                 ED_area_exit(C, sa);
550
551                 st= BKE_spacetype_from_id(type);
552                 slold= sa->spacedata.first;
553
554                 sa->spacetype= type;
555                 sa->butspacetype= type;
556                 sa->type= st;
557                 
558                 /* check previously stored space */
559                 for (sl= sa->spacedata.first; sl; sl= sl->next)
560                         if(sl->spacetype==type)
561                                 break;
562                 
563                 /* old spacedata... happened during work on 2.50, remove */
564                 if(sl && sl->regionbase.first==NULL) {
565                         st->free(sl);
566                         MEM_freeN(sl);
567                         sl= NULL;
568                 }
569                 
570                 if (sl) {
571                         
572                         /* swap regions */
573                         slold->regionbase= sa->regionbase;
574                         sa->regionbase= sl->regionbase;
575                         sl->regionbase.first= sl->regionbase.last= NULL;
576                         
577                         /* put in front of list */
578                         BLI_remlink(&sa->spacedata, sl);
579                         BLI_addhead(&sa->spacedata, sl);
580                 } 
581                 else {
582                         /* new space */
583                         if(st) {
584                                 sl= st->new();
585                                 BLI_addhead(&sa->spacedata, sl);
586                                 
587                                 /* swap regions */
588                                 slold->regionbase= sa->regionbase;
589                                 sa->regionbase= sl->regionbase;
590                                 sl->regionbase.first= sl->regionbase.last= NULL;
591                         }
592                 }
593                 
594                 ED_area_initialize(C->wm, C->window, sa);
595                 
596                 /* tell WM to refresh, cursor types etc */
597                 WM_event_add_mousemove(C);
598         }
599 }
600
601 static char *windowtype_pup(void)
602 {
603         return(
604                    "Window type:%t" //14
605                    "|3D View %x1" //30
606                    
607                    "|%l" // 33
608                    
609                    "|Ipo Curve Editor %x2" //54
610                    "|Action Editor %x12" //73
611                    "|NLA Editor %x13" //94
612                    
613                    "|%l" //97
614                    
615                    "|UV/Image Editor %x6" //117
616                    
617                    "|Video Sequence Editor %x8" //143
618                    "|Timeline %x15" //163
619                    "|Audio Window %x11" //163
620                    "|Text Editor %x9" //179
621                    
622                    "|%l" //192
623                    
624                    
625                    "|User Preferences %x7" //213
626                    "|Outliner %x3" //232
627                    "|Buttons Window %x4" //251
628                    "|Node Editor %x16"
629                    "|%l" //254
630                    
631                    "|File Browser %x5" //290
632                    
633                    "|%l" //293
634                    
635                    "|Scripts Window %x14"//313
636                    );
637 }
638
639 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
640 {
641         area_newspace(C, C->area, C->area->butspacetype);
642         ED_area_tag_redraw(C->area);
643 }
644
645 /* returns offset for next button in header */
646 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
647 {
648         uiBut *but;
649         int xco= 8;
650         
651         if(ED_screen_area_active(C)) uiBlockSetCol(block, TH_HEADER);
652         else uiBlockSetCol(block, TH_HEADERDESEL);
653         
654         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
655                                                    windowtype_pup(), xco, yco, XIC+10, YIC, 
656                                                    &(C->area->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
657                                                    "Displays Current Window Type. "
658                                                    "Click for menu of available types.");
659         uiButSetFunc(but, spacefunc, NULL, NULL);
660         
661         xco += XIC + 14;
662         
663         uiBlockSetEmboss(block, UI_EMBOSSN);
664         if (C->area->flag & HEADER_NO_PULLDOWN) {
665                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
666                                                  ICON_DISCLOSURE_TRI_RIGHT,
667                                                  xco,yco,XIC,YIC-2,
668                                                  &(C->area->flag), 0, 0, 0, 0, 
669                                                  "Show pulldown menus");
670         }
671         else {
672                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
673                                                  ICON_DISCLOSURE_TRI_DOWN,
674                                                  xco,yco,XIC,YIC-2,
675                                                  &(C->area->flag), 0, 0, 0, 0, 
676                                                  "Hide pulldown menus");
677         }
678         xco+=XIC;
679         
680         return xco;
681 }
682