2.5
[blender.git] / source / blender / editors / space_api / spacetypes.c
1 /**
2  * $Id: spacetypes.c
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) Blender Foundation, 2008
21  *
22  * ***** END GPL/BL DUAL LICENSE BLOCK *****
23  */
24
25 #include <stdlib.h>
26
27 #include "MEM_guardedalloc.h"
28 #include "BLI_blenlib.h"
29
30 #include "DNA_object_types.h"
31 #include "DNA_scene_types.h"
32 #include "DNA_windowmanager_types.h"
33
34 #include "BKE_context.h"
35 #include "BKE_screen.h"
36
37 #include "UI_interface.h"
38 #include "UI_view2d.h"
39
40 #include "BIF_gl.h"
41
42 #include "ED_screen.h"
43 #include "ED_object.h"
44 #include "ED_mesh.h"
45 #include "ED_space_api.h"
46 #include "ED_anim_api.h"
47
48
49 ARegionType *ED_regiontype_from_id(SpaceType *st, int regionid)
50 {
51         ARegionType *art;
52         
53         for(art= st->regiontypes.first; art; art= art->next)
54                 if(art->regionid==regionid)
55                         return art;
56         
57         printf("Error, region type missing in %s\n", st->name);
58         return st->regiontypes.first;
59 }
60
61
62 /* only call once on startup, storage is global in BKE kernel listbase */
63 void ED_spacetypes_init(void)
64 {
65         const ListBase *spacetypes;
66         SpaceType *type;
67
68         /* create space types */
69         ED_spacetype_outliner();
70         ED_spacetype_time();
71         ED_spacetype_view3d();
72         ED_spacetype_ipo();
73         ED_spacetype_image();
74         ED_spacetype_node();
75         ED_spacetype_buttons();
76         ED_spacetype_info();
77         ED_spacetype_file();
78         ED_spacetype_sound();
79         ED_spacetype_action();
80         ED_spacetype_nla();
81         ED_spacetype_script();
82         ED_spacetype_text();
83         ED_spacetype_sequencer();
84 //      ...
85         
86         /* register operator types for screen and all spaces */
87         ED_operatortypes_screen();
88         ED_operatortypes_anim();
89         ED_operatortypes_animchannels(); // XXX have this as part of anim() ones instead?
90         ED_operatortypes_object();
91         ED_operatortypes_mesh();
92         ui_view2d_operatortypes();
93         
94         spacetypes = BKE_spacetypes_list();
95         for(type=spacetypes->first; type; type=type->next)
96                 type->operatortypes();
97 }
98
99 /* called in wm.c */
100 /* keymap definitions are registered only once per WM initialize, usually on file read,
101    using the keymap the actual areas/regions add the handlers */
102 void ED_spacetypes_keymap(wmWindowManager *wm)
103 {
104         const ListBase *spacetypes;
105         SpaceType *stype;
106         ARegionType *atype;
107
108         ED_keymap_screen(wm);
109         ED_keymap_anim(wm);
110         ED_keymap_animchannels(wm);
111         ED_keymap_object(wm);
112         ED_keymap_mesh(wm);
113         UI_view2d_keymap(wm);
114
115         spacetypes = BKE_spacetypes_list();
116         for(stype=spacetypes->first; stype; stype=stype->next) {
117                 if(stype->keymap)
118                         stype->keymap(wm);
119                 for(atype=stype->regiontypes.first; atype; atype=atype->next) {
120                         if(atype->keymap)
121                                 atype->keymap(wm);
122                 }
123         }
124 }
125
126 /* ********************** custom drawcall api ***************** */
127
128 /* type */
129 #define REGION_DRAW_PRE         1
130 #define REGION_DRAW_POST        0
131
132 typedef struct RegionDrawCB {
133         struct RegionDrawCB *next, *prev;
134         
135         void (*draw)(const struct bContext *, struct ARegion *, void *);        
136         void *customdata;
137         
138         int type;
139         
140 } RegionDrawCB;
141
142 void *ED_region_draw_cb_activate(ARegionType *art, 
143                                                                  void   (*draw)(const struct bContext *, struct ARegion *, void *),
144                                                                  void *customdata, int type)
145 {
146         RegionDrawCB *rdc= MEM_callocN(sizeof(RegionDrawCB), "RegionDrawCB");
147         
148         BLI_addtail(&art->drawcalls, rdc);
149         rdc->draw= draw;
150         rdc->customdata= customdata;
151         rdc->type= type;
152         
153         return rdc;
154 }
155
156 void ED_region_draw_cb_exit(ARegionType *art, void *handle)
157 {
158         RegionDrawCB *rdc;
159         
160         for(rdc= art->drawcalls.first; rdc; rdc= rdc->next) {
161                 if(rdc==(RegionDrawCB *)handle) {
162                         BLI_remlink(&art->drawcalls, rdc);
163                         MEM_freeN(rdc);
164                         return;
165                 }
166         }
167 }
168
169 void ED_region_draw_cb_draw(const bContext *C, ARegion *ar, int type)
170 {
171         RegionDrawCB *rdc;
172         
173         for(rdc= ar->type->drawcalls.first; rdc; rdc= rdc->next) {
174                 if(rdc->type==type)
175                         rdc->draw(C, ar, rdc->customdata);
176         }               
177 }
178
179
180
181 /* ********************* space template *********************** */
182
183 /* allocate and init some vars */
184 static SpaceLink *xxx_new(const bContext *C)
185 {
186         return NULL;
187 }
188
189 /* not spacelink itself */
190 static void xxx_free(SpaceLink *sl)
191 {
192
193 }
194
195 /* spacetype; init callback for usage, should be redoable */
196 static void xxx_init(wmWindowManager *wm, ScrArea *sa)
197 {
198         
199         /* link area to SpaceXXX struct */
200         
201         /* define how many regions, the order and types */
202         
203         /* add types to regions */
204 }
205
206 static SpaceLink *xxx_duplicate(SpaceLink *sl)
207 {
208         
209         return NULL;
210 }
211
212 static void xxx_operatortypes(void)
213 {
214         /* register operator types for this space */
215 }
216
217 static void xxx_keymap(wmWindowManager *wm)
218 {
219         /* add default items to keymap */
220 }
221
222 /* only called once, from screen/spacetypes.c */
223 void ED_spacetype_xxx(void)
224 {
225         static SpaceType st;
226         
227         st.spaceid= SPACE_VIEW3D;
228         
229         st.new= xxx_new;
230         st.free= xxx_free;
231         st.init= xxx_init;
232         st.duplicate= xxx_duplicate;
233         st.operatortypes= xxx_operatortypes;
234         st.keymap= xxx_keymap;
235         
236         BKE_spacetype_register(&st);
237 }
238
239 /* ****************************** end template *********************** */
240
241
242
243