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 "DNA_screen_types.h"
35 #include "DNA_userdef_types.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_arithb.h"
39 #include "BLI_rand.h"
40
41 #include "BKE_context.h"
42 #include "BKE_global.h"
43 #include "BKE_screen.h"
44 #include "BKE_utildefines.h"
45
46 #include "WM_api.h"
47 #include "WM_types.h"
48 #include "wm_subwindow.h"
49
50 #include "ED_screen.h"
51 #include "ED_screen_types.h"
52 #include "ED_types.h"
53
54 #include "BIF_gl.h"
55 #include "BIF_glutil.h"
56 #include "BLF_api.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #ifndef DISABLE_PYTHON
63 #include "BPY_extern.h"
64 #endif
65
66 #include "screen_intern.h"
67
68 /* general area and region code */
69
70 static void region_draw_emboss(ARegion *ar, rcti *scirct)
71 {
72         rcti rect;
73         
74         /* translate scissor rect to region space */
75         rect.xmin= scirct->xmin - ar->winrct.xmin;
76         rect.ymin= scirct->ymin - ar->winrct.ymin;
77         rect.xmax= scirct->xmax - ar->winrct.xmin;
78         rect.ymax= scirct->ymax - ar->winrct.ymin;
79         
80         /* set transp line */
81         glEnable( GL_BLEND );
82         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
83         
84         /* right  */
85         glColor4ub(0,0,0, 50);
86         sdrawline(rect.xmax, rect.ymin, rect.xmax, rect.ymax);
87         
88         /* bottom  */
89         glColor4ub(0,0,0, 80);
90         sdrawline(rect.xmin, rect.ymin, rect.xmax, rect.ymin);
91         
92         /* top  */
93         glColor4ub(255,255,255, 60);
94         sdrawline(rect.xmin, rect.ymax, rect.xmax, rect.ymax);
95
96         /* left  */
97         glColor4ub(255,255,255, 50);
98         sdrawline(rect.xmin, rect.ymin, rect.xmin, rect.ymax);
99         
100         glDisable( GL_BLEND );
101 }
102
103 void ED_region_pixelspace(ARegion *ar)
104 {
105         int width= ar->winrct.xmax-ar->winrct.xmin+1;
106         int height= ar->winrct.ymax-ar->winrct.ymin+1;
107         
108         wmOrtho2(-0.375, (float)width-0.375, -0.375, (float)height-0.375);
109         wmLoadIdentity();
110 }
111
112 /* only exported for WM */
113 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
114 {
115         /* generic notes first */
116         switch(note->category) {
117                 case NC_WM:
118                         if(note->data==ND_FILEREAD)
119                                 ED_region_tag_redraw(ar);
120                         break;
121                 case NC_WINDOW:
122                         ED_region_tag_redraw(ar);
123                         break;
124                 case NC_SCREEN:
125                         if(note->action==NA_EDITED)
126                                 ED_region_tag_redraw(ar);
127                         /* pass on */
128                 default:
129                         if(ar->type && ar->type->listener)
130                                 ar->type->listener(ar, note);
131         }
132 }
133
134 /* only exported for WM */
135 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
136 {
137         /* no generic notes? */
138         if(sa->type && sa->type->listener) {
139                 sa->type->listener(sa, note);
140         }
141 }
142
143 /* only exported for WM */
144 void ED_area_do_refresh(bContext *C, ScrArea *sa)
145 {
146         /* no generic notes? */
147         if(sa->type && sa->type->refresh) {
148                 sa->type->refresh(C, sa);
149         }
150         sa->do_refresh= 0;
151 }
152
153 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
154 /* only exported for WM */
155 void ED_area_overdraw_flush(bContext *C, ScrArea *sa, ARegion *ar)
156 {
157         AZone *az;
158         
159         for(az= sa->actionzones.first; az; az= az->next) {
160                 int xs= (az->x1+az->x2)/2, ys= (az->y1+az->y2)/2;
161
162                 /* test if inside */
163                 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
164                         az->do_draw= 1;
165                 }
166         }
167 }
168
169 static void area_draw_azone(short x1, short y1, short x2, short y2)
170 {
171         float xmin = x1;
172         float xmax = x2-2;
173         float ymin = y1-1;
174         float ymax = y2-3;
175         
176         float dx= 0.3f*(xmax-xmin);
177         float dy= 0.3f*(ymax-ymin);
178         
179         glColor4ub(255, 255, 255, 80);
180         fdrawline(xmin, ymax, xmax, ymin);
181         fdrawline(xmin, ymax-dy, xmax-dx, ymin);
182         fdrawline(xmin, ymax-2*dy, xmax-2*dx, ymin);
183         
184         glColor4ub(0, 0, 0, 150);
185         fdrawline(xmin, ymax+1, xmax+1, ymin);
186         fdrawline(xmin, ymax-dy+1, xmax-dx+1, ymin);
187         fdrawline(xmin, ymax-2*dy+1, xmax-2*dx+1, ymin);
188 }
189
190 /* only exported for WM */
191 void ED_area_overdraw(bContext *C)
192 {
193         wmWindow *win= CTX_wm_window(C);
194         bScreen *screen= CTX_wm_screen(C);
195         ScrArea *sa;
196         
197         /* Draw AZones, in screenspace */
198         wmSubWindowSet(win, screen->mainwin);
199
200         glEnable( GL_BLEND );
201         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
202         
203         for(sa= screen->areabase.first; sa; sa= sa->next) {
204                 AZone *az;
205                 for(az= sa->actionzones.first; az; az= az->next) {
206                         if(az->do_draw) {
207                                 if(az->type==AZONE_TRI) {
208                                         area_draw_azone(az->x1, az->y1, az->x2, az->y2);
209                                 }
210                                 az->do_draw= 0;
211                         }
212                 }
213         }       
214         glDisable( GL_BLEND );
215         
216 }
217
218 /* get scissor rect, checking overlapping regions */
219 static void region_scissor_winrct(ARegion *ar, rcti *winrct)
220 {
221         *winrct= ar->winrct;
222         
223         if(ELEM(ar->alignment, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT))
224                 return;
225
226         while(ar->prev) {
227                 ar= ar->prev;
228                 
229                 if(ar->flag & RGN_FLAG_HIDDEN);
230                 else if(ar->alignment==RGN_OVERLAP_LEFT) {
231                         winrct->xmin= ar->winrct.xmax + 1;
232                 }
233                 else if(ar->alignment==RGN_OVERLAP_RIGHT) {
234                         winrct->xmax= ar->winrct.xmin - 1;
235                 }
236                 else break;
237         }
238 }
239
240 /* only exported for WM */
241 void ED_region_do_draw(bContext *C, ARegion *ar)
242 {
243         wmWindow *win= CTX_wm_window(C);
244         ScrArea *sa= CTX_wm_area(C);
245         ARegionType *at= ar->type;
246         rcti winrct;
247         
248         /* checks other overlapping regions */
249         region_scissor_winrct(ar, &winrct);
250         
251         /* if no partial draw rect set, full rect */
252         if(ar->drawrct.xmin == ar->drawrct.xmax)
253                 ar->drawrct= winrct;
254         else {
255                 /* extra clip for safety */
256                 ar->drawrct.xmin= MAX2(winrct.xmin, ar->drawrct.xmin);
257                 ar->drawrct.ymin= MAX2(winrct.ymin, ar->drawrct.ymin);
258                 ar->drawrct.xmax= MIN2(winrct.xmax, ar->drawrct.xmax);
259                 ar->drawrct.ymax= MIN2(winrct.ymax, ar->drawrct.ymax);
260         }
261         
262         /* note; this sets state, so we can use wmOrtho and friends */
263         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
264         
265         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
266         
267         /* optional header info instead? */
268         if(ar->headerstr) {
269                 float col[3];
270                 UI_GetThemeColor3fv(TH_HEADER, col);
271                 glClearColor(col[0], col[1], col[2], 0.0);
272                 glClear(GL_COLOR_BUFFER_BIT);
273                 
274                 UI_ThemeColor(TH_TEXT);
275                 BLF_draw_default(20, 6, 0.0f, ar->headerstr);
276         }
277         else if(at->draw) {
278                 at->draw(C, ar);
279         }
280         
281         uiFreeInactiveBlocks(C, &ar->uiblocks);
282         
283         if(sa)
284                 region_draw_emboss(ar, &winrct);
285         
286         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
287         ED_region_pixelspace(ar);
288         
289         ar->do_draw= 0;
290         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
291 }
292
293 /* **********************************
294    maybe silly, but let's try for now
295    to keep these tags protected
296    ********************************** */
297
298 void ED_region_tag_redraw(ARegion *ar)
299 {
300         if(ar) {
301                 /* zero region means full region redraw */
302                 ar->do_draw= 1;
303                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
304         }
305 }
306
307 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
308 {
309         if(ar) {
310                 if(!ar->do_draw) {
311                         /* no redraw set yet, set partial region */
312                         ar->do_draw= 1;
313                         ar->drawrct= *rct;
314                 }
315                 else if(ar->drawrct.xmin != ar->drawrct.xmax) {
316                         /* partial redraw already set, expand region */
317                         ar->drawrct.xmin= MIN2(ar->drawrct.xmin, rct->xmin);
318                         ar->drawrct.ymin= MIN2(ar->drawrct.ymin, rct->ymin);
319                         ar->drawrct.xmax= MAX2(ar->drawrct.xmax, rct->xmax);
320                         ar->drawrct.ymax= MAX2(ar->drawrct.ymax, rct->ymax);
321                 }
322         }
323 }
324
325 void ED_area_tag_redraw(ScrArea *sa)
326 {
327         ARegion *ar;
328         
329         if(sa)
330                 for(ar= sa->regionbase.first; ar; ar= ar->next)
331                         ED_region_tag_redraw(ar);
332 }
333
334 void ED_area_tag_refresh(ScrArea *sa)
335 {
336         if(sa)
337                 sa->do_refresh= 1;
338 }
339
340 /* *************************************************************** */
341
342 /* use NULL to disable it */
343 void ED_area_headerprint(ScrArea *sa, const char *str)
344 {
345         ARegion *ar;
346         
347         for(ar= sa->regionbase.first; ar; ar= ar->next) {
348                 if(ar->regiontype==RGN_TYPE_HEADER) {
349                         if(str) {
350                                 if(ar->headerstr==NULL)
351                                         ar->headerstr= MEM_mallocN(256, "headerprint");
352                                 BLI_strncpy(ar->headerstr, str, 256);
353                         }
354                         else if(ar->headerstr) {
355                                 MEM_freeN(ar->headerstr);
356                                 ar->headerstr= NULL;
357                         }
358                         ED_region_tag_redraw(ar);
359                 }
360         }
361 }
362
363 /* *************************************************************** */
364
365 /* dir is direction to check, not the splitting edge direction! */
366 static int rct_fits(rcti *rect, char dir, int size)
367 {
368         if(dir=='h') {
369                 return rect->xmax-rect->xmin - size;
370         }
371         else { // 'v'
372                 return rect->ymax-rect->ymin - size;
373         }
374 }
375
376 static void region_rect_recursive(ARegion *ar, rcti *remainder, int quad)
377 {
378         rcti *remainder_prev= remainder;
379         int prefsizex, prefsizey;
380         int alignment;
381         
382         if(ar==NULL)
383                 return;
384         
385         BLI_init_rcti(&ar->winrct, 0, 0, 0, 0);
386         
387         /* for test; allow split of previously defined region */
388         if(ar->alignment & RGN_SPLIT_PREV)
389                 if(ar->prev)
390                         remainder= &ar->prev->winrct;
391         
392         alignment = ar->alignment & ~RGN_SPLIT_PREV;
393         
394         /* clear state flags first */
395         ar->flag &= ~RGN_FLAG_TOO_SMALL;
396         /* user errors */
397         if(ar->next==NULL && alignment!=RGN_ALIGN_QSPLIT)
398                 alignment= RGN_ALIGN_NONE;
399         
400         prefsizex= ar->type->minsizex;
401         prefsizey= ar->type->minsizey;
402         
403         /* hidden is user flag */
404         if(ar->flag & RGN_FLAG_HIDDEN);
405         /* XXX floating area region, not handled yet here */
406         else if(alignment == RGN_ALIGN_FLOAT);
407         /* remainder is too small for any usage */
408         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
409                 ar->flag |= RGN_FLAG_TOO_SMALL;
410         }
411         else if(alignment==RGN_ALIGN_NONE) {
412                 /* typically last region */
413                 ar->winrct= *remainder;
414                 BLI_init_rcti(remainder, 0, 0, 0, 0);
415         }
416         else if(alignment==RGN_ALIGN_TOP || alignment==RGN_ALIGN_BOTTOM) {
417                 
418                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
419                         ar->flag |= RGN_FLAG_TOO_SMALL;
420                 }
421                 else {
422                         int fac= rct_fits(remainder, 'v', prefsizey);
423
424                         if(fac < 0 )
425                                 prefsizey += fac;
426                         
427                         ar->winrct= *remainder;
428                         
429                         if(alignment==RGN_ALIGN_TOP) {
430                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
431                                 remainder->ymax= ar->winrct.ymin - 1;
432                         }
433                         else {
434                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
435                                 remainder->ymin= ar->winrct.ymax + 1;
436                         }
437                 }
438         }
439         else if( ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
440                 
441                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
442                         ar->flag |= RGN_FLAG_TOO_SMALL;
443                 }
444                 else {
445                         int fac= rct_fits(remainder, 'h', prefsizex);
446                         
447                         if(fac < 0 )
448                                 prefsizex += fac;
449                         
450                         ar->winrct= *remainder;
451                         
452                         if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
453                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
454                                 if(alignment==RGN_ALIGN_RIGHT)
455                                         remainder->xmax= ar->winrct.xmin - 1;
456                         }
457                         else {
458                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
459                                 if(alignment==RGN_ALIGN_LEFT)
460                                         remainder->xmin= ar->winrct.xmax + 1;
461                         }
462                 }
463         }
464         else if(alignment==RGN_ALIGN_VSPLIT || alignment==RGN_ALIGN_HSPLIT) {
465                 /* percentage subdiv*/
466                 ar->winrct= *remainder;
467                 
468                 if(alignment==RGN_ALIGN_HSPLIT) {
469                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
470                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
471                                 remainder->xmin= ar->winrct.xmax+1;
472                         }
473                         else {
474                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
475                         }
476                 }
477                 else {
478                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
479                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
480                                 remainder->ymin= ar->winrct.ymax+1;
481                         }
482                         else {
483                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
484                         }
485                 }
486         }
487         else if(alignment==RGN_ALIGN_QSPLIT) {
488                 ar->winrct= *remainder;
489                 
490                 /* test if there's still 4 regions left */
491                 if(quad==0) {
492                         ARegion *artest= ar->next;
493                         int count= 1;
494                         
495                         while(artest) {
496                                 artest->alignment= RGN_ALIGN_QSPLIT;
497                                 artest= artest->next;
498                                 count++;
499                         }
500                         
501                         if(count!=4) {
502                                 /* let's stop adding regions */
503                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
504                                 printf("region quadsplit failed\n");
505                         }
506                         else quad= 1;
507                 }
508                 if(quad) {
509                         if(quad==1) { /* left bottom */
510                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
511                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
512                         }
513                         else if(quad==2) { /* left top */
514                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
515                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
516                         }
517                         else if(quad==3) { /* right bottom */
518                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
519                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
520                         }
521                         else {  /* right top */
522                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
523                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
524                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
525                         }
526
527                         quad++;
528                 }
529         }
530         
531         /* for speedup */
532         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
533         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
534         
535         /* restore test exception */
536         if(ar->alignment & RGN_SPLIT_PREV) {
537                 if(ar->prev) {
538                         remainder= remainder_prev;
539                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
540                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
541                 }
542         }
543         region_rect_recursive(ar->next, remainder, quad);
544 }
545
546 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
547 {
548         
549         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
550         else sa->totrct.xmin= sa->v1->vec.x;
551         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
552         else sa->totrct.xmax= sa->v4->vec.x;
553         
554         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
555         else sa->totrct.ymin= sa->v1->vec.y;
556         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
557         else sa->totrct.ymax= sa->v2->vec.y;
558         
559         /* for speedup */
560         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
561         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
562 }
563
564
565 #define AZONESPOT               12
566 void area_azone_initialize(ScrArea *sa) 
567 {
568         AZone *az;
569         if(sa->actionzones.first==NULL) {
570                 /* set action zones - should these actually be ARegions? With these we can easier check area hotzones */
571                 /* (ton) for time being just area, ARegion split is not foreseen on user level */
572                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
573                 BLI_addtail(&(sa->actionzones), az);
574                 az->type= AZONE_TRI;
575                 az->pos= AZONE_SW;
576                 
577                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
578                 BLI_addtail(&(sa->actionzones), az);
579                 az->type= AZONE_TRI;
580                 az->pos= AZONE_NE;
581         }
582         
583         for(az= sa->actionzones.first; az; az= az->next) {
584                 if(az->pos==AZONE_SW) {
585                         az->x1= sa->v1->vec.x+1;
586                         az->y1= sa->v1->vec.y+1;
587                         az->x2= sa->v1->vec.x+AZONESPOT;
588                         az->y2= sa->v1->vec.y+AZONESPOT;
589                 } 
590                 else if (az->pos==AZONE_NE) {
591                         az->x1= sa->v3->vec.x;
592                         az->y1= sa->v3->vec.y;
593                         az->x2= sa->v3->vec.x-AZONESPOT;
594                         az->y2= sa->v3->vec.y-AZONESPOT;
595                 }
596         }
597 }
598
599 /* used for area initialize below */
600 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
601 {
602         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
603                 if(ar->swinid)
604                         wm_subwindow_close(win, ar->swinid);
605                 ar->swinid= 0;
606         }
607         else if(ar->swinid==0)
608                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
609         else 
610                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
611 }
612
613 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
614 {
615         /* note, add-handler checks if it already exists */
616         
617         // XXX it would be good to have boundbox checks for some of these...
618         if(flag & ED_KEYMAP_UI) {
619                 UI_add_region_handlers(handlers);
620         }
621         if(flag & ED_KEYMAP_VIEW2D) {
622                 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
623                 WM_event_add_keymap_handler(handlers, keymap);
624         }
625         if(flag & ED_KEYMAP_MARKERS) {
626                 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
627                 WM_event_add_keymap_handler(handlers, keymap);
628                 // XXX need boundbox check urgently!!!
629         }
630         if(flag & ED_KEYMAP_ANIMATION) {
631                 ListBase *keymap= WM_keymap_listbase(wm, "Animation", 0, 0);
632                 WM_event_add_keymap_handler(handlers, keymap);
633         }
634         if(flag & ED_KEYMAP_FRAMES) {
635                 ListBase *keymap= WM_keymap_listbase(wm, "Frames", 0, 0);
636                 WM_event_add_keymap_handler(handlers, keymap);
637         }
638 }
639
640
641 /* called in screen_refresh, or screens_init, also area size changes */
642 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
643 {
644         ARegion *ar;
645         rcti rect;
646         
647         /* set typedefinitions */
648         sa->type= BKE_spacetype_from_id(sa->spacetype);
649         
650         if(sa->type==NULL) {
651                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
652                 sa->type= BKE_spacetype_from_id(sa->spacetype);
653         }
654         
655         for(ar= sa->regionbase.first; ar; ar= ar->next)
656                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
657         
658         /* area sizes */
659         area_calc_totrct(sa, win->sizex, win->sizey);
660         
661         /* region rect sizes */
662         rect= sa->totrct;
663         region_rect_recursive(sa->regionbase.first, &rect, 0);
664         
665         /* default area handlers */
666         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
667         /* checks spacedata, adds own handlers */
668         if(sa->type->init)
669                 sa->type->init(wm, sa);
670         
671         /* region windows, default and own handlers */
672         for(ar= sa->regionbase.first; ar; ar= ar->next) {
673                 region_subwindow(wm, win, ar);
674                 
675                 if(ar->swinid) {
676                         /* default region handlers */
677                         ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
678
679                         if(ar->type->init)
680                                 ar->type->init(wm, ar);
681                 }
682                 else {
683                         /* prevent uiblocks to run */
684                         uiFreeBlocks(NULL, &ar->uiblocks);      
685                 }
686                 
687         }
688         area_azone_initialize(sa);
689 }
690
691 /* externally called for floating regions like menus */
692 void ED_region_init(bContext *C, ARegion *ar)
693 {
694 //      ARegionType *at= ar->type;
695         
696         /* refresh can be called before window opened */
697         region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
698         
699         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
700         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
701 }
702
703
704 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
705 /* area vertices were set */
706 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
707 {
708         SpaceType *st;
709         ARegion *ar;
710         
711         sa1->headertype= sa2->headertype;
712         sa1->spacetype= sa2->spacetype;
713         sa1->butspacetype= sa2->butspacetype;
714         
715         if(swap_space == 1) {
716                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
717                 /* exception: ensure preview is reset */
718 //              if(sa1->spacetype==SPACE_VIEW3D)
719 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
720         }
721         else if (swap_space == 2) {
722                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
723         }
724         else {
725                 BKE_spacedata_freelist(&sa1->spacedata);
726                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
727         }
728         
729         /* Note; SPACE_EMPTY is possible on new screens */
730         
731         /* regions */
732         if(swap_space<2) {
733                 st= BKE_spacetype_from_id(sa1->spacetype);
734                 for(ar= sa1->regionbase.first; ar; ar= ar->next)
735                         BKE_area_region_free(st, ar);
736                 BLI_freelistN(&sa1->regionbase);
737         }
738         
739         st= BKE_spacetype_from_id(sa2->spacetype);
740         for(ar= sa2->regionbase.first; ar; ar= ar->next) {
741                 ARegion *newar= BKE_area_region_copy(st, ar);
742                 BLI_addtail(&sa1->regionbase, newar);
743         }
744                 
745 #ifndef DISABLE_PYTHON
746         /* scripts */
747         BPY_free_scriptlink(&sa1->scriptlink);
748         sa1->scriptlink= sa2->scriptlink;
749         BPY_copy_scriptlink(&sa1->scriptlink);  /* copies internal pointers */
750 #endif
751 }
752
753 /* *********** Space switching code *********** */
754
755 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
756 {
757         ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
758
759         ED_area_exit(C, sa1);
760         ED_area_exit(C, sa2);
761
762         tmp->spacetype= sa1->spacetype;
763         tmp->butspacetype= sa1->butspacetype;
764         BKE_spacedata_copyfirst(&tmp->spacedata, &sa1->spacedata);
765
766         area_copy_data(tmp, sa1, 2);
767         area_copy_data(sa1, sa2, 0);
768         area_copy_data(sa2, tmp, 0);
769         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
770         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
771
772         BKE_screen_area_free(tmp);
773         MEM_freeN(tmp);
774
775         /* tell WM to refresh, cursor types etc */
776         WM_event_add_mousemove(C);
777         
778         ED_area_tag_redraw(sa1);
779         ED_area_tag_refresh(sa1);
780         ED_area_tag_redraw(sa2);
781         ED_area_tag_refresh(sa2);
782 }
783
784 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
785 {
786         if(sa->spacetype != type) {
787                 SpaceType *st;
788                 SpaceLink *slold;
789                 SpaceLink *sl;
790
791                 ED_area_exit(C, sa);
792
793                 st= BKE_spacetype_from_id(type);
794                 slold= sa->spacedata.first;
795
796                 sa->spacetype= type;
797                 sa->butspacetype= type;
798                 sa->type= st;
799                 
800                 /* check previously stored space */
801                 for (sl= sa->spacedata.first; sl; sl= sl->next)
802                         if(sl->spacetype==type)
803                                 break;
804                 
805                 /* old spacedata... happened during work on 2.50, remove */
806                 if(sl && sl->regionbase.first==NULL) {
807                         st->free(sl);
808                         BLI_freelinkN(&sa->spacedata, sl);
809                         sl= NULL;
810                 }
811                 
812                 if (sl) {
813                         
814                         /* swap regions */
815                         slold->regionbase= sa->regionbase;
816                         sa->regionbase= sl->regionbase;
817                         sl->regionbase.first= sl->regionbase.last= NULL;
818                         
819                         /* put in front of list */
820                         BLI_remlink(&sa->spacedata, sl);
821                         BLI_addhead(&sa->spacedata, sl);
822                 } 
823                 else {
824                         /* new space */
825                         if(st) {
826                                 sl= st->new(C);
827                                 BLI_addhead(&sa->spacedata, sl);
828                                 
829                                 /* swap regions */
830                                 if(slold)
831                                         slold->regionbase= sa->regionbase;
832                                 sa->regionbase= sl->regionbase;
833                                 sl->regionbase.first= sl->regionbase.last= NULL;
834                         }
835                 }
836                 
837                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
838                 
839                 /* tell WM to refresh, cursor types etc */
840                 WM_event_add_mousemove(C);
841                 
842                 ED_area_tag_redraw(sa);
843                 ED_area_tag_refresh(sa);
844         }
845 }
846
847 void ED_area_prevspace(bContext *C)
848 {
849         SpaceLink *sl= CTX_wm_space_data(C);
850         ScrArea *sa= CTX_wm_area(C);
851
852         /* cleanup */
853 #if 0 // XXX needs to be space type specific
854         if(sfile->spacetype==SPACE_FILE) {
855                 if(sfile->pupmenu) {
856                         MEM_freeN(sfile->pupmenu);
857                         sfile->pupmenu= NULL;
858                 }
859         }
860 #endif
861
862         if(sl->next) {
863
864 #if 0 // XXX check whether this is still needed
865                 if (sfile->spacetype == SPACE_SCRIPT) {
866                         SpaceScript *sc = (SpaceScript *)sfile;
867                         if (sc->script) sc->script->flags &=~SCRIPT_FILESEL;
868                 }
869 #endif
870
871                 ED_area_newspace(C, sa, sl->next->spacetype);
872         }
873         else {
874                 ED_area_newspace(C, sa, SPACE_INFO);
875         }
876         ED_area_tag_redraw(sa);
877 }
878
879 static char *windowtype_pup(void)
880 {
881         return(
882                    "Window type:%t" //14
883                    "|3D View %x1" //30
884                    
885                    "|%l" // 33
886                    
887                    "|Graph Editor %x2" //54
888                    "|DopeSheet %x12" //73
889                    "|NLA Editor %x13" //94
890                    
891                    "|%l" //97
892                    
893                    "|UV/Image Editor %x6" //117
894                    
895                    "|Video Sequence Editor %x8" //143
896                    "|Timeline %x15" //163
897                    "|Audio Window %x11" //163
898                    "|Text Editor %x9" //179
899                    
900                    "|%l" //192
901                    
902                    
903                    "|User Preferences %x7" //213
904                    "|Outliner %x3" //232
905                    "|Buttons Window %x4" //251
906                    "|Node Editor %x16"
907                    "|%l" //254
908                    
909                    "|File Browser %x5" //290
910                    
911                    "|%l" //293
912                    
913                    "|Scripts Window %x14"//313
914                    );
915 }
916
917 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
918 {
919         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
920         ED_area_tag_redraw(CTX_wm_area(C));
921 }
922
923 /* returns offset for next button in header */
924 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
925 {
926         ScrArea *sa= CTX_wm_area(C);
927         uiBut *but;
928         int xco= 8;
929         
930         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
931                                                    windowtype_pup(), xco, yco, XIC+10, YIC, 
932                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
933                                                    "Displays Current Window Type. "
934                                                    "Click for menu of available types.");
935         uiButSetFunc(but, spacefunc, NULL, NULL);
936         
937         xco += XIC + 14;
938         
939         uiBlockSetEmboss(block, UI_EMBOSSN);
940         if (sa->flag & HEADER_NO_PULLDOWN) {
941                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
942                                                  ICON_DISCLOSURE_TRI_RIGHT,
943                                                  xco,yco,XIC,YIC-2,
944                                                  &(sa->flag), 0, 0, 0, 0, 
945                                                  "Show pulldown menus");
946         }
947         else {
948                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
949                                                  ICON_DISCLOSURE_TRI_DOWN,
950                                                  xco,yco,XIC,YIC-2,
951                                                  &(sa->flag), 0, 0, 0, 0, 
952                                                  "Hide pulldown menus");
953         }
954         xco+=XIC;
955         
956         uiBlockSetEmboss(block, UI_EMBOSS);
957         
958         return xco;
959 }
960
961 /************************ standard UI regions ************************/
962
963 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, char *context)
964 {
965         uiStyle *style= U.uistyles.first;
966         uiBlock *block;
967         PanelType *pt;
968         Panel *panel;
969         View2D *v2d= &ar->v2d;
970         float col[3];
971         int xco, yco, x, y, miny=0, w, em, header, triangle, open;
972
973         if(vertical) {
974                 w= v2d->cur.xmax - v2d->cur.xmin;
975                 em= (ar->type->minsizex)? 10: 20;
976         }
977         else {
978                 w= UI_PANEL_WIDTH;
979                 em= (ar->type->minsizex)? 10: 20;
980         }
981
982         header= 20; // XXX
983         triangle= 22;
984         x= 0;
985         y= -style->panelouter;
986
987         /* create panels */
988         uiBeginPanels(C, ar);
989
990         /* set view2d view matrix for scrolling (without scrollers) */
991         UI_view2d_view_ortho(C, v2d);
992
993         for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
994                 /* verify context */
995                 if(context)
996                         if(!pt->context || strcmp(context, pt->context) != 0)
997                                 continue;
998
999                 /* draw panel */
1000                 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1001                         block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1002                         panel= uiBeginPanel(ar, block, pt, &open);
1003
1004                         if(vertical)
1005                                 y -= header;
1006
1007                         if(pt->draw_header && (open || vertical)) {
1008                                 /* for enabled buttons */
1009                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1010                                         triangle, header+style->panelspace, header, 1, style);
1011
1012                                 pt->draw_header(C, panel);
1013
1014                                 uiBlockLayoutResolve(C, block, &xco, &yco);
1015                                 panel->labelofs= xco - triangle;
1016                                 panel->layout= NULL;
1017                         }
1018
1019                         if(open) {
1020                                 panel->type= pt;
1021                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL,
1022                                         style->panelspace, 0, w-2*style->panelspace, em, style);
1023
1024                                 pt->draw(C, panel);
1025
1026                                 uiBlockLayoutResolve(C, block, &xco, &yco);
1027                                 panel->layout= NULL;
1028
1029                                 yco -= 2*style->panelspace;
1030                                 uiEndPanel(block, w, -yco);
1031                         }
1032                         else
1033                                 yco= 0;
1034
1035                         uiEndBlock(C, block);
1036
1037                         if(vertical) {
1038                                 y += yco-style->panelouter;
1039                         }
1040                         else {
1041                                 x += w;
1042                                 miny= MIN2(y, yco-style->panelouter-header);
1043                         }
1044                 }
1045         }
1046
1047         if(vertical)
1048                 x += w;
1049         else
1050                 y= miny;
1051         
1052         /* in case there are no panels */
1053         if(x == 0 || y == 0) {
1054                 x= UI_PANEL_WIDTH;
1055                 y= UI_PANEL_WIDTH;
1056         }
1057
1058         /* clear */
1059         UI_GetThemeColor3fv(TH_BACK, col);
1060         glClearColor(col[0], col[1], col[2], 0.0);
1061         glClear(GL_COLOR_BUFFER_BIT);
1062
1063         /* before setting the view */
1064         if(vertical) {
1065                 v2d->keepofs |= V2D_LOCKOFS_X;
1066                 v2d->keepofs &= ~V2D_LOCKOFS_Y;
1067         }
1068         else {
1069                 v2d->keepofs &= ~V2D_LOCKOFS_X;
1070                 v2d->keepofs |= V2D_LOCKOFS_Y;
1071         }
1072
1073         UI_view2d_totRect_set(v2d, x, -y);
1074
1075         /* set the view */
1076         UI_view2d_view_ortho(C, v2d);
1077
1078         /* this does the actual drawing! */
1079         uiEndPanels(C, ar);
1080         
1081         /* restore view matrix */
1082         UI_view2d_view_restore(C);
1083 }
1084
1085 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1086 {
1087         ListBase *keymap;
1088
1089         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1090
1091         keymap= WM_keymap_listbase(wm, "View2D Buttons List", 0, 0);
1092         WM_event_add_keymap_handler(&ar->handlers, keymap);
1093 }
1094
1095 void ED_region_header(const bContext *C, ARegion *ar)
1096 {
1097         uiStyle *style= U.uistyles.first;
1098         uiBlock *block;
1099         uiLayout *layout;
1100         HeaderType *ht;
1101         Header header = {0};
1102         float col[3];
1103         int xco, yco;
1104
1105         /* clear */
1106         if(ED_screen_area_active(C))
1107                 UI_GetThemeColor3fv(TH_HEADER, col);
1108         else
1109                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
1110         
1111         glClearColor(col[0], col[1], col[2], 0.0);
1112         glClear(GL_COLOR_BUFFER_BIT);
1113         
1114         /* set view2d view matrix for scrolling (without scrollers) */
1115         UI_view2d_view_ortho(C, &ar->v2d);
1116
1117         xco= 8;
1118         yco= HEADERY-3;
1119
1120         /* draw all headers types */
1121         for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1122                 block= uiBeginBlock(C, ar, "header buttons", UI_EMBOSS);
1123                 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, HEADERY-6, 1, style);
1124
1125                 if(ht->draw) {
1126                         header.type= ht;
1127                         header.layout= layout;
1128                         ht->draw(C, &header);
1129                 }
1130
1131                 uiBlockLayoutResolve(C, block, &xco, &yco);
1132                 uiEndBlock(C, block);
1133                 uiDrawBlock(C, block);
1134         }
1135
1136         /* always as last  */
1137         UI_view2d_totRect_set(&ar->v2d, xco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1138
1139         /* restore view matrix? */
1140         UI_view2d_view_restore(C);
1141 }
1142
1143 void ED_region_header_init(ARegion *ar)
1144 {
1145         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1146 }
1147