Port of part of the Interface code to 2.50.
[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_area.h"
43 #include "ED_screen.h"
44 #include "ED_screen_types.h"
45
46 #include "WM_api.h"
47 #include "WM_types.h"
48 #include "wm_subwindow.h"
49
50 #include "BIF_gl.h"
51 #include "BIF_glutil.h"
52
53 #include "UI_resources.h"
54
55 #include "BPY_extern.h"
56
57 #include "screen_intern.h"
58
59 /* general area and region code */
60
61 static void region_draw_emboss(ARegion *ar)
62 {
63         short winx, winy;
64         
65         winx= ar->winrct.xmax-ar->winrct.xmin;
66         winy= ar->winrct.ymax-ar->winrct.ymin;
67         
68         /* set transp line */
69         glEnable( GL_BLEND );
70         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
71         
72         /* right  */
73         glColor4ub(0,0,0, 50);
74         sdrawline(winx, 0, winx, winy);
75         
76         /* bottom  */
77         glColor4ub(0,0,0, 80);
78         sdrawline(0, 0, winx, 0);
79         
80         /* top  */
81         glColor4ub(255,255,255, 60);
82         sdrawline(0, winy, winx, winy);
83
84         /* left  */
85         glColor4ub(255,255,255, 50);
86         sdrawline(0, 0, 0, winy);
87         
88         glDisable( GL_BLEND );
89 }
90
91
92 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
93 {
94         
95         /* generic notes first */
96         switch(note->type) {
97                 case WM_NOTE_WINDOW_REDRAW:
98                         ar->do_draw= 1;
99                         break;
100                 case WM_NOTE_SCREEN_CHANGED:
101                         ar->do_draw= ar->do_refresh= 1;
102                         break;
103                 default:
104                         if(ar->type->listener)
105                                 ar->type->listener(ar, note);
106         }
107 }
108
109 void ED_region_do_draw(bContext *C, ARegion *ar)
110 {
111         ARegionType *at= ar->type;
112         
113         wm_subwindow_set(C->window, ar->swinid);
114         
115         if(ar->swinid && at->draw) {
116                 UI_SetTheme(C->area);
117                 at->draw(C, ar);
118                 UI_SetTheme(NULL);
119         }
120         else {
121                 float fac= 0.1*ar->swinid;
122                 
123                 glClearColor(0.5, fac, 1.0f-fac, 0.0); 
124                 glClear(GL_COLOR_BUFFER_BIT);
125                 
126                 fac= BLI_frand();
127                 glColor3f(fac, fac, fac);
128                 glRecti(2,  2,  12,  12);
129                 
130                 region_draw_emboss(ar);
131         }
132         
133         ar->do_draw= 0;
134 }
135
136 void ED_region_do_refresh(bContext *C, ARegion *ar)
137 {
138         ARegionType *at= ar->type;
139
140         /* refresh can be called before window opened */
141         if(ar->swinid)
142                 wm_subwindow_set(C->window, ar->swinid);
143         
144         if (at->refresh) {
145                 at->refresh(C, ar);
146         }
147         
148         ar->do_refresh= 0;
149 }
150
151 /* *************************************************************** */
152
153
154 static int rct_fits(rcti *rect, char dir, int size)
155 {
156         if(dir=='h') {
157                 return rect->xmax-rect->xmin - size;
158         }
159         else { // 'v'
160                 return rect->ymax-rect->ymin - size;
161         }
162 }
163
164 static void region_rect_recursive(ARegion *ar, rcti *remainder)
165 {
166         if(ar==NULL)
167                 return;
168         
169         /* clear state flag first */
170         ar->flag &= ~RGN_FLAG_TOO_SMALL;
171         
172         if(ar->size<ar->minsize)
173                 ar->size= ar->minsize;
174         
175         /* hidden is user flag */
176         if(ar->flag & RGN_FLAG_HIDDEN);
177         /* XXX floating area region, not handled yet here */
178         else if(ar->alignment == RGN_ALIGN_FLOAT);
179         /* remainder is too small for any usage */
180         else if( rct_fits(remainder, 'v', 1)==0 || rct_fits(remainder, 'h', 1) < 0 ) {
181                 ar->flag |= RGN_FLAG_TOO_SMALL;
182         }
183         else if(ar->alignment==RGN_ALIGN_NONE) {
184                 /* typically last region */
185                 ar->winrct= *remainder;
186                 BLI_init_rcti(remainder, 0, 0, 0, 0);
187         }
188         else if(ar->alignment==RGN_ALIGN_TOP || ar->alignment==RGN_ALIGN_BOTTOM) {
189                 
190                 if( rct_fits(remainder, 'v', ar->minsize) < 0 ) {
191                         ar->flag |= RGN_FLAG_TOO_SMALL;
192                 }
193                 else {
194                         int fac= rct_fits(remainder, 'v', ar->size);
195
196                         if(fac < 0 )
197                                 ar->size += fac;
198                         
199                         ar->winrct= *remainder;
200                         
201                         if(ar->alignment==RGN_ALIGN_TOP) {
202                                 ar->winrct.ymin= ar->winrct.ymax - ar->size;
203                                 remainder->ymax= ar->winrct.ymin-1;
204                         }
205                         else {
206                                 ar->winrct.ymax= ar->winrct.ymin + ar->size;
207                                 remainder->ymin= ar->winrct.ymax+1;
208                         }
209                 }
210         }
211         else if(ar->alignment==RGN_ALIGN_LEFT || ar->alignment==RGN_ALIGN_RIGHT) {
212                 
213                 if( rct_fits(remainder, 'h', ar->minsize) < 0 ) {
214                         ar->flag |= RGN_FLAG_TOO_SMALL;
215                 }
216                 else {
217                         int fac= rct_fits(remainder, 'h', ar->size);
218                         
219                         if(fac < 0 )
220                                 ar->size += fac;
221                         
222                         ar->winrct= *remainder;
223                         
224                         if(ar->alignment==RGN_ALIGN_RIGHT) {
225                                 ar->winrct.xmin= ar->winrct.xmax - ar->size;
226                                 remainder->xmax= ar->winrct.xmin-1;
227                         }
228                         else {
229                                 ar->winrct.xmax= ar->winrct.xmin + ar->size;
230                                 remainder->xmin= ar->winrct.xmax+1;
231                         }
232                 }
233         }
234         else {
235                 /* percentage subdiv*/
236                 ar->winrct= *remainder;
237                 
238                 if(ar->alignment==RGN_ALIGN_HSPLIT) {
239                         ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
240                         remainder->xmin= ar->winrct.xmax+1;
241                 }
242                 else {
243                         ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
244                         remainder->ymin= ar->winrct.ymax+1;
245                 }
246         }
247         
248         region_rect_recursive(ar->next, remainder);
249 }
250
251 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
252 {
253         
254         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
255         else sa->totrct.xmin= sa->v1->vec.x;
256         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
257         else sa->totrct.xmax= sa->v4->vec.x;
258         
259         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
260         else sa->totrct.ymin= sa->v1->vec.y;
261         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
262         else sa->totrct.ymax= sa->v2->vec.y;
263         
264         /* for speedup */
265         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
266         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
267 }
268
269 #define AZONESPOT               12
270 void area_azone_initialize(ScrArea *sa) {
271         AZone *az;
272         if(sa->actionzones.first==NULL) {
273                 /* set action zones - should these actually be ARegions? With these we can easier check area hotzones */
274                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
275                 BLI_addtail(&(sa->actionzones), az);
276                 az->type= AZONE_TRI;
277                 az->x1= sa->v1->vec.x+1;
278                 az->y1= sa->v1->vec.y+1;
279                 az->x2= sa->v1->vec.x+AZONESPOT;
280                 az->y2= sa->v1->vec.y+AZONESPOT;
281                 az->pos= AZONE_SW;
282                 az->action= AZONE_SPLIT;
283                 
284                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
285                 BLI_addtail(&(sa->actionzones), az);
286                 az->type= AZONE_TRI;
287                 az->x1= sa->v3->vec.x-1;
288                 az->y1= sa->v3->vec.y-1;
289                 az->x2= sa->v3->vec.x-AZONESPOT;
290                 az->y2= sa->v3->vec.y-AZONESPOT;
291                 az->pos= AZONE_NE;
292                 az->action= AZONE_DRAG;
293                 
294                 /*az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
295                 BLI_addtail(&sa->azones, az);
296                 az->type= AZONE_TRI;
297                 az->x1= as->v1->vec.x;
298                 az->y1= as->v1->vec.y;
299                 az->x2= as->v1->vec.x+AZONESPOT;
300                 az->y2= as->v1->vec.y+AZONESPOT;
301                 
302                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
303                 BLI_addtail(&sa->azones, az);
304                 az->type= AZONE_TRI;
305                 az->x1= as->v1->vec.x;
306                 az->y1= as->v1->vec.y;
307                 az->x2= as->v1->vec.x+AZONESPOT;
308                 az->y2= as->v1->vec.y+AZONESPOT;
309                 
310                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
311                 BLI_addtail(&sa->azones, az);
312                 az->type= AZONE_QUAD;
313                 az->x1= as->v1->vec.x;
314                 az->y1= as->v1->vec.y;
315                 az->x2= as->v1->vec.x+AZONESPOT;
316                 az->y2= as->v1->vec.y+AZONESPOT;
317                 
318                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
319                 BLI_addtail(&sa->azones, az);
320                 az->type= AZONE_QUAD;
321                 az->x1= as->v1->vec.x;
322                 az->y1= as->v1->vec.y;
323                 az->x2= as->v1->vec.x+AZONESPOT;
324                 az->y2= as->v1->vec.y+AZONESPOT;
325                 
326                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
327                 BLI_addtail(&sa->azones, az);
328                 az->type= AZONE_QUAD;
329                 az->x1= as->v1->vec.x;
330                 az->y1= as->v1->vec.y;
331                 az->x2= as->v1->vec.x+AZONESPOT;
332                 az->y2= as->v1->vec.y+AZONESPOT;
333                 
334                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
335                 BLI_addtail(&sa->azones, az);
336                 az->type= AZONE_QUAD;
337                 az->x1= as->v1->vec.x;
338                 az->y1= as->v1->vec.y;
339                 az->x2= as->v1->vec.x+AZONESPOT;
340                 az->y2= as->v1->vec.y+AZONESPOT;*/
341         }
342         
343         for(az= sa->actionzones.first; az; az= az->next) {
344                 if(az->pos==AZONE_SW) {
345                         az->x1= sa->v1->vec.x+1;
346                         az->y1= sa->v1->vec.y+1;
347                         az->x2= sa->v1->vec.x+AZONESPOT;
348                         az->y2= sa->v1->vec.y+AZONESPOT;
349                 } else if (az->pos==AZONE_NE) {
350                         az->x1= sa->v3->vec.x-1;
351                         az->y1= sa->v3->vec.y-1;
352                         az->x2= sa->v3->vec.x-AZONESPOT;
353                         az->y2= sa->v3->vec.y-AZONESPOT;
354                 }
355         }
356 }
357
358 /* used for area and screen regions */
359 void ED_region_initialize(wmWindowManager *wm, wmWindow *win, ARegion *ar)
360 {
361         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
362                 if(ar->swinid)
363                         wm_subwindow_close(win, ar->swinid);
364                 ar->swinid= 0;
365         }
366         else if(ar->swinid==0)
367                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
368         else 
369                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
370 }
371
372 /* called in screen_refresh, or screens_init */
373 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
374 {
375         ARegion *ar;
376         rcti rect;
377         
378         /* set typedefinitions */
379         sa->type= BKE_spacetype_from_id(sa->spacetype);
380         if(sa->type==NULL) {
381                 sa->spacetype= SPACE_VIEW3D;
382                 sa->type= BKE_spacetype_from_id(sa->spacetype);
383         }
384         
385         area_calc_totrct(sa, win->sizex, win->sizey);
386         
387         /* regiontype callback, it should create/verify the amount of subregions with minsizes etc */
388         if(sa->type->init)
389                 sa->type->init(wm, sa);
390         
391         /* region rect sizes */
392         rect= sa->totrct;
393         region_rect_recursive(sa->regionbase.first, &rect);
394         
395         /* region windows */
396         for(ar= sa->regionbase.first; ar; ar= ar->next)
397                 ED_region_initialize(wm, win, ar);
398         
399         area_azone_initialize(sa);
400 }
401
402 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
403 /* area vertices were set */
404
405 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
406 {
407         Panel *pa1, *pa2, *patab;
408         ARegion *ar;
409         AZone *az;
410         
411         sa1->headertype= sa2->headertype;
412         sa1->spacetype= sa2->spacetype;
413         
414         if(swap_space) {
415                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
416                 /* exception: ensure preview is reset */
417 //              if(sa1->spacetype==SPACE_VIEW3D)
418 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
419         }
420         else {
421                 BKE_spacedata_freelist(&sa1->spacedata);
422                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
423         }
424         
425         BLI_freelistN(&sa1->panels);
426         BLI_duplicatelist(&sa1->panels, &sa2->panels);
427         
428         /* copy panel pointers */
429         for(pa1= sa1->panels.first; pa1; pa1= pa1->next) {
430                 
431                 patab= sa1->panels.first;
432                 pa2= sa2->panels.first;
433                 while(patab) {
434                         if( pa1->paneltab == pa2) {
435                                 pa1->paneltab = patab;
436                                 break;
437                         }
438                         patab= patab->next;
439                         pa2= pa2->next;
440                 }
441         }
442         
443         /* regions */
444         BLI_freelistN(&sa1->regionbase);
445         BLI_duplicatelist(&sa1->regionbase, &sa2->regionbase);
446         for(ar= sa1->regionbase.first; ar; ar= ar->next) {
447                 ar->handlers.first= ar->handlers.last= NULL;
448                 ar->uiblocks.first= ar->uiblocks.last= NULL;
449                 ar->swinid= 0;
450         }
451                 
452         /* scripts */
453         BPY_free_scriptlink(&sa1->scriptlink);
454         sa1->scriptlink= sa2->scriptlink;
455         BPY_copy_scriptlink(&sa1->scriptlink);  /* copies internal pointers */
456         
457 }
458
459