BGE Button types panel, can edit existing buttons but not add new ones yet.
[blender.git] / source / blender / blenkernel / intern / screen.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) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31 #include <stdio.h>
32 #include <math.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_screen_types.h"
37 #include "DNA_space_types.h"
38
39 #include "BLI_blenlib.h"
40
41 #include "BKE_screen.h"
42
43 #ifndef DISABLE_PYTHON
44 #include "BPY_extern.h"
45 #endif
46
47 /* ************ Spacetype/regiontype handling ************** */
48
49 /* keep global; this has to be accessible outside of windowmanager */
50 static ListBase spacetypes= {NULL, NULL};
51
52 /* not SpaceType itself */
53 static void spacetype_free(SpaceType *st)
54 {
55         ARegionType *art;
56         PanelType *pt;
57         HeaderType *ht;
58         MenuType *mt;
59         
60         for(art= st->regiontypes.first; art; art= art->next) {
61                 BLI_freelistN(&art->drawcalls);
62
63                 for(pt= art->paneltypes.first; pt; pt= pt->next)
64                         if(pt->py_free)
65                                 pt->py_free(pt->py_data);
66
67                 for(ht= art->headertypes.first; ht; ht= ht->next)
68                         if(ht->py_free)
69                                 ht->py_free(ht->py_data);
70
71                 for(mt= art->menutypes.first; mt; mt= mt->next)
72                         if(mt->py_free)
73                                 mt->py_free(mt->py_data);
74
75                 BLI_freelistN(&art->paneltypes);
76                 BLI_freelistN(&art->headertypes);
77                 BLI_freelistN(&art->menutypes);
78         }
79         
80         BLI_freelistN(&st->regiontypes);
81         BLI_freelistN(&st->toolshelf);
82
83 }
84
85 void BKE_spacetypes_free(void)
86 {
87         SpaceType *st;
88         
89         for(st= spacetypes.first; st; st= st->next) {
90                 spacetype_free(st);
91         }
92         
93         BLI_freelistN(&spacetypes);
94 }
95
96 SpaceType *BKE_spacetype_from_id(int spaceid)
97 {
98         SpaceType *st;
99         
100         for(st= spacetypes.first; st; st= st->next) {
101                 if(st->spaceid==spaceid)
102                         return st;
103         }
104         return NULL;
105 }
106
107 ARegionType *BKE_regiontype_from_id(SpaceType *st, int regionid)
108 {
109         ARegionType *art;
110         
111         for(art= st->regiontypes.first; art; art= art->next)
112                 if(art->regionid==regionid)
113                         return art;
114         
115         printf("Error, region type missing in %s\n", st->name);
116         return st->regiontypes.first;
117 }
118
119
120 const ListBase *BKE_spacetypes_list()
121 {
122         return &spacetypes;
123 }
124
125 void BKE_spacetype_register(SpaceType *st)
126 {
127         SpaceType *stype;
128         
129         /* sanity check */
130         stype= BKE_spacetype_from_id(st->spaceid);
131         if(stype) {
132                 printf("error: redefinition of spacetype %s\n", stype->name);
133                 spacetype_free(stype);
134                 MEM_freeN(stype);
135         }
136         
137         BLI_addtail(&spacetypes, st);
138 }
139
140 /* ***************** Space handling ********************** */
141
142 void BKE_spacedata_freelist(ListBase *lb)
143 {
144         SpaceLink *sl;
145         ARegion *ar;
146         
147         for (sl= lb->first; sl; sl= sl->next) {
148                 SpaceType *st= BKE_spacetype_from_id(sl->spacetype);
149                 
150                 /* free regions for pushed spaces */
151                 for(ar=sl->regionbase.first; ar; ar=ar->next)
152                         BKE_area_region_free(st, ar);
153
154                 BLI_freelistN(&sl->regionbase);
155                 
156                 if(st && st->free) 
157                         st->free(sl);
158         }
159         
160         BLI_freelistN(lb);
161 }
162
163 ARegion *BKE_area_region_copy(SpaceType *st, ARegion *ar)
164 {
165         ARegion *newar= MEM_dupallocN(ar);
166         Panel *pa, *newpa, *patab;
167         
168         newar->prev= newar->next= NULL;
169         newar->handlers.first= newar->handlers.last= NULL;
170         newar->uiblocks.first= newar->uiblocks.last= NULL;
171         newar->swinid= 0;
172         
173         /* use optional regiondata callback */
174         if(ar->regiondata) {
175                 ARegionType *art= BKE_regiontype_from_id(st, ar->regiontype);
176
177                 if(art && art->duplicate)
178                         newar->regiondata= art->duplicate(ar->regiondata);
179                 else
180                         newar->regiondata= MEM_dupallocN(ar->regiondata);
181         }
182         
183         newar->panels.first= newar->panels.last= NULL;
184         BLI_duplicatelist(&newar->panels, &ar->panels);
185         
186         /* copy panel pointers */
187         for(newpa= newar->panels.first; newpa; newpa= newpa->next) {
188                 patab= newar->panels.first;
189                 pa= ar->panels.first;
190                 while(patab) {
191                         if(newpa->paneltab == pa) {
192                                 newpa->paneltab = patab;
193                                 break;
194                         }
195                         patab= patab->next;
196                         pa= pa->next;
197                 }
198         }
199         
200         return newar;
201 }
202
203
204 /* from lb2 to lb1, lb1 is supposed to be free'd */
205 static void region_copylist(SpaceType *st, ListBase *lb1, ListBase *lb2)
206 {
207         ARegion *ar;
208         
209         /* to be sure */
210         lb1->first= lb1->last= NULL;
211         
212         for(ar= lb2->first; ar; ar= ar->next) {
213                 ARegion *arnew= BKE_area_region_copy(st, ar);
214                 BLI_addtail(lb1, arnew);
215         }
216 }
217
218
219 /* lb1 should be empty */
220 void BKE_spacedata_copylist(ListBase *lb1, ListBase *lb2)
221 {
222         SpaceLink *sl;
223         
224         lb1->first= lb1->last= NULL;    /* to be sure */
225         
226         for (sl= lb2->first; sl; sl= sl->next) {
227                 SpaceType *st= BKE_spacetype_from_id(sl->spacetype);
228                 
229                 if(st && st->duplicate) {
230                         SpaceLink *slnew= st->duplicate(sl);
231                         
232                         BLI_addtail(lb1, slnew);
233                         
234                         region_copylist(st, &slnew->regionbase, &sl->regionbase);
235                 }
236         }
237 }
238
239 /* lb1 should be empty */
240 void BKE_spacedata_copyfirst(ListBase *lb1, ListBase *lb2)
241 {
242         SpaceLink *sl;
243         
244         lb1->first= lb1->last= NULL;    /* to be sure */
245         
246         sl= lb2->first;
247         if(sl) {
248                 SpaceType *st= BKE_spacetype_from_id(sl->spacetype);
249
250                 if(st && st->duplicate) {
251                         SpaceLink *slnew= st->duplicate(sl);
252
253                         BLI_addtail(lb1, slnew);
254
255                         region_copylist(st, &slnew->regionbase, &sl->regionbase);
256                 }
257         }
258 }
259
260 /* not region itself */
261 void BKE_area_region_free(SpaceType *st, ARegion *ar)
262 {
263         if(st) {
264                 ARegionType *art= BKE_regiontype_from_id(st, ar->regiontype);
265                 
266                 if(art && art->free)
267                         art->free(ar);
268                 
269                 if(ar->regiondata)
270                         printf("regiondata free error\n");
271         }
272         else if(ar->type && ar->type->free)
273                 ar->type->free(ar);
274
275         if(ar) {
276                 BLI_freelistN(&ar->panels);
277         }
278 }
279
280 /* not area itself */
281 void BKE_screen_area_free(ScrArea *sa)
282 {
283         SpaceType *st= BKE_spacetype_from_id(sa->spacetype);
284         ARegion *ar;
285         
286         for(ar=sa->regionbase.first; ar; ar=ar->next)
287                 BKE_area_region_free(st, ar);
288
289         BLI_freelistN(&sa->regionbase);
290         
291         BKE_spacedata_freelist(&sa->spacedata);
292         
293         BLI_freelistN(&sa->actionzones);
294         
295 #ifndef DISABLE_PYTHON
296         BPY_free_scriptlink(&sa->scriptlink);
297 #endif
298 }
299
300 /* don't free screen itself */
301 void free_screen(bScreen *sc)
302 {
303         ScrArea *sa, *san;
304         ARegion *ar;
305         
306         for(ar=sc->regionbase.first; ar; ar=ar->next)
307                 BKE_area_region_free(NULL, ar);
308
309         BLI_freelistN(&sc->regionbase);
310         
311         for(sa= sc->areabase.first; sa; sa= san) {
312                 san= sa->next;
313                 BKE_screen_area_free(sa);
314         }
315         
316         BLI_freelistN(&sc->vertbase);
317         BLI_freelistN(&sc->edgebase);
318         BLI_freelistN(&sc->areabase);
319 }
320
321