5acc3091364abc16e3b1fae5ae81e7bda9822610
[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
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 "BPY_extern.h"
53
54 #include "screen_intern.h"
55
56 /* general area and region code */
57
58 static void region_draw_emboss(ARegion *ar)
59 {
60         short winx, winy;
61         
62         winx= ar->winrct.xmax-ar->winrct.xmin;
63         winy= ar->winrct.ymax-ar->winrct.ymin;
64         
65         /* set transp line */
66         glEnable( GL_BLEND );
67         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
68         
69         /* right  */
70         glColor4ub(0,0,0, 50);
71         sdrawline(winx, 0, winx, winy);
72         
73         /* bottom  */
74         glColor4ub(0,0,0, 80);
75         sdrawline(0, 0, winx, 0);
76         
77         /* top  */
78         glColor4ub(255,255,255, 60);
79         sdrawline(0, winy, winx, winy);
80
81         /* left  */
82         glColor4ub(255,255,255, 50);
83         sdrawline(0, 0, 0, winy);
84         
85         glDisable( GL_BLEND );
86 }
87
88
89 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
90 {
91         if(ar->type->listener)
92                 ar->type->listener(ar, note);
93         
94         /* generic notes */
95         if(note->type==WM_NOTE_REDRAW)
96                 ar->do_draw= 1;
97         if(note->type==WM_NOTE_REFRESH)
98                 ar->do_refresh= 1;
99         
100 }
101
102 void ED_region_do_draw(bContext *C, ARegion *ar)
103 {
104         ARegionType *at= ar->type;
105         
106         wm_subwindow_set(C->window, ar->swinid);
107         
108         if(ar->swinid && at->draw) {
109                 at->draw(C, ar);
110         }
111         else {
112                 float fac= 0.1*ar->swinid;
113                 
114                 glClearColor(0.5, fac, 1.0f-fac, 0.0); 
115                 glClear(GL_COLOR_BUFFER_BIT);
116                 
117                 fac= BLI_frand();
118                 glColor3f(fac, fac, fac);
119                 glRecti(2,  2,  12,  12);
120                 
121                 region_draw_emboss(ar);
122         }
123         
124         ar->do_draw= 0;
125 }
126
127 void ED_region_do_refresh(bContext *C, ARegion *ar)
128 {
129         ARegionType *at= ar->type;
130
131         /* refresh can be called before window opened */
132         if(ar->swinid)
133                 wm_subwindow_set(C->window, ar->swinid);
134         
135         if (at->refresh) {
136                 at->refresh(C, ar);
137         }
138         
139         ar->do_refresh= 0;
140 }
141
142 /* *************************************************************** */
143
144
145 static int rct_fits(rcti *rect, char dir, int size)
146 {
147         if(dir=='h') {
148                 return rect->xmax-rect->xmin - size;
149         }
150         else { // 'v'
151                 return rect->ymax-rect->ymin - size;
152         }
153 }
154
155 static void region_rect_recursive(ARegion *ar, rcti *remainder)
156 {
157         if(ar==NULL)
158                 return;
159         
160         /* clear state flag first */
161         ar->flag &= ~RGN_FLAG_TOO_SMALL;
162         
163         if(ar->size<ar->minsize)
164                 ar->size= ar->minsize;
165         
166         /* hidden is user flag */
167         if(ar->flag & RGN_FLAG_HIDDEN);
168         /* remainder is too small for any usage */
169         else if( rct_fits(remainder, 'v', 1)==0 || rct_fits(remainder, 'h', 1) < 0 ) {
170                 ar->flag |= RGN_FLAG_TOO_SMALL;
171         }
172         else if(ar->alignment==RGN_ALIGN_NONE) {
173                 /* typically last region */
174                 ar->winrct= *remainder;
175                 BLI_init_rcti(remainder, 0, 0, 0, 0);
176         }
177         else if(ar->alignment==RGN_ALIGN_TOP || ar->alignment==RGN_ALIGN_BOTTOM) {
178                 
179                 if( rct_fits(remainder, 'v', ar->minsize) < 0 ) {
180                         ar->flag |= RGN_FLAG_TOO_SMALL;
181                 }
182                 else {
183                         int fac= rct_fits(remainder, 'v', ar->size);
184                         
185                         if(fac < 0 )
186                                 ar->size += fac;
187                         
188                         ar->winrct= *remainder;
189                         
190                         if(ar->alignment==RGN_ALIGN_TOP) {
191                                 ar->winrct.ymin= ar->winrct.ymax - ar->size;
192                                 remainder->ymax= ar->winrct.ymin-1;
193                         }
194                         else {
195                                 ar->winrct.ymax= ar->winrct.ymin + ar->size;
196                                 remainder->ymin= ar->winrct.ymax+1;
197                         }
198                 }
199         }
200         else if(ar->alignment==RGN_ALIGN_LEFT || ar->alignment==RGN_ALIGN_RIGHT) {
201                 
202                 if( rct_fits(remainder, 'h', ar->minsize) < 0 ) {
203                         ar->flag |= RGN_FLAG_TOO_SMALL;
204                 }
205                 else {
206                         int fac= rct_fits(remainder, 'h', ar->size);
207                         
208                         if(fac < 0 )
209                                 ar->size += fac;
210                         
211                         ar->winrct= *remainder;
212                         
213                         if(ar->alignment==RGN_ALIGN_RIGHT) {
214                                 ar->winrct.xmin= ar->winrct.xmax - ar->size;
215                                 remainder->xmax= ar->winrct.xmin-1;
216                         }
217                         else {
218                                 ar->winrct.xmax= ar->winrct.xmin + ar->size;
219                                 remainder->xmin= ar->winrct.xmax+1;
220                         }
221                 }
222         }
223         else {
224                 /* percentage subdiv*/
225                 ar->winrct= *remainder;
226                 
227                 if(ar->alignment==RGN_ALIGN_HSPLIT) {
228                         ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
229                         remainder->xmin= ar->winrct.xmax+1;
230                 }
231                 else {
232                         ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
233                         remainder->ymin= ar->winrct.ymax+1;
234                 }
235         }
236         
237         region_rect_recursive(ar->next, remainder);
238 }
239
240 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
241 {
242         
243         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
244         else sa->totrct.xmin= sa->v1->vec.x;
245         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
246         else sa->totrct.xmax= sa->v4->vec.x;
247         
248         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
249         else sa->totrct.ymin= sa->v1->vec.y;
250         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
251         else sa->totrct.ymax= sa->v2->vec.y;
252         
253         /* for speedup */
254         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
255         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
256 }
257
258 /* called in screen_refresh, or screens_init */
259 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
260 {
261         ARegion *ar;
262         rcti rect;
263         
264         /* set typedefinitions */
265         sa->type= BKE_spacetype_from_id(sa->spacetype);
266         if(sa->type==NULL) {
267                 sa->spacetype= SPACE_VIEW3D;
268                 sa->type= BKE_spacetype_from_id(sa->spacetype);
269         }
270         
271         area_calc_totrct(sa, win->sizex, win->sizey);
272         
273         /* regiontype callback, it should create/verify the amount of subregions with minsizes etc */
274         if(sa->type->init)
275                 sa->type->init(sa);
276         
277         /* region rect sizes */
278         rect= sa->totrct;
279         region_rect_recursive(sa->regionbase.first, &rect);
280         
281         /* region windows */
282         for(ar= sa->regionbase.first; ar; ar= ar->next) {
283                 if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
284                         if(ar->swinid)
285                                 wm_subwindow_close(win, ar->swinid);
286                         ar->swinid= 0;
287                 }
288                 else if(ar->swinid==0)
289                         ar->swinid= wm_subwindow_open(win, &ar->winrct);
290                 else 
291                         wm_subwindow_position(win, ar->swinid, &ar->winrct);
292         }
293 }
294
295 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
296 /* area vertices were set */
297
298 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
299 {
300         Panel *pa1, *pa2, *patab;
301         ARegion *ar;
302         
303         sa1->headertype= sa2->headertype;
304         sa1->spacetype= sa2->spacetype;
305         
306         if(swap_space) {
307                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
308                 /* exception: ensure preview is reset */
309 //              if(sa1->spacetype==SPACE_VIEW3D)
310 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
311         }
312         else {
313                 BKE_spacedata_freelist(&sa1->spacedata);
314                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
315         }
316         
317         BLI_freelistN(&sa1->panels);
318         BLI_duplicatelist(&sa1->panels, &sa2->panels);
319         
320         /* copy panel pointers */
321         for(pa1= sa1->panels.first; pa1; pa1= pa1->next) {
322                 
323                 patab= sa1->panels.first;
324                 pa2= sa2->panels.first;
325                 while(patab) {
326                         if( pa1->paneltab == pa2) {
327                                 pa1->paneltab = patab;
328                                 break;
329                         }
330                         patab= patab->next;
331                         pa2= pa2->next;
332                 }
333         }
334         
335         /* regions */
336         BLI_freelistN(&sa1->regionbase);
337         BLI_duplicatelist(&sa1->regionbase, &sa2->regionbase);
338         for(ar= sa1->regionbase.first; ar; ar= ar->next)
339                 ar->swinid= 0;
340         
341         /* scripts */
342         BPY_free_scriptlink(&sa1->scriptlink);
343         sa1->scriptlink= sa2->scriptlink;
344         BPY_copy_scriptlink(&sa1->scriptlink);  /* copies internal pointers */
345         
346 }
347
348