merge from trunk r37405
[blender.git] / source / blender / editors / space_api / spacetypes.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) Blender Foundation, 2008
21  *
22  * ***** END GPL/BL DUAL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/space_api/spacetypes.c
26  *  \ingroup spapi
27  */
28
29
30 #include <stdlib.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_utildefines.h"
36
37 #include "DNA_object_types.h"
38 #include "DNA_windowmanager_types.h"
39
40
41 #include "BKE_context.h"
42 #include "BKE_screen.h"
43
44 #include "UI_interface.h"
45 #include "UI_view2d.h"
46
47
48 #include "ED_anim_api.h"
49 #include "ED_armature.h"
50 #include "ED_curve.h"
51 #include "ED_fileselect.h"
52 #include "ED_gpencil.h"
53 #include "ED_markers.h"
54 #include "ED_mesh.h"
55 #include "ED_node.h"
56 #include "ED_object.h"
57 #include "ED_physics.h"
58 #include "ED_render.h"
59 #include "ED_screen.h"
60 #include "ED_sculpt.h"
61 #include "ED_space_api.h"
62 #include "ED_sound.h"
63 #include "ED_uvedit.h"
64 #include "ED_mball.h"
65 #include "ED_logic.h"
66
67 /* only call once on startup, storage is global in BKE kernel listbase */
68 void ED_spacetypes_init(void)
69 {
70         const ListBase *spacetypes;
71         SpaceType *type;
72
73         /* UI_UNIT_X is now a variable, is used in some spacetype inits? */
74         U.widget_unit= 20;
75         
76         /* create space types */
77         ED_spacetype_outliner();
78         ED_spacetype_time();
79         ED_spacetype_view3d();
80         ED_spacetype_ipo();
81         ED_spacetype_image();
82         ED_spacetype_node();
83         ED_spacetype_buttons();
84         ED_spacetype_info();
85         ED_spacetype_file();
86         ED_spacetype_sound();
87         ED_spacetype_action();
88         ED_spacetype_nla();
89         ED_spacetype_script();
90         ED_spacetype_text();
91         ED_spacetype_sequencer();
92         ED_spacetype_logic();
93         ED_spacetype_console();
94         ED_spacetype_userpref();
95 //      ...
96         
97         /* register operator types for screen and all spaces */
98         ED_operatortypes_screen();
99         ED_operatortypes_anim();
100         ED_operatortypes_animchannels();
101         ED_operatortypes_gpencil();
102         ED_operatortypes_object();
103         ED_operatortypes_mesh();
104         ED_operatortypes_sculpt();
105         ED_operatortypes_uvedit();
106         ED_operatortypes_paint();
107         ED_operatortypes_physics();
108         ED_operatortypes_curve();
109         ED_operatortypes_armature();
110         ED_operatortypes_marker();
111         ED_operatortypes_metaball();
112         ED_operatortypes_sound();
113         ED_operatortypes_render();
114         ED_operatortypes_logic();
115         
116         UI_view2d_operatortypes();
117         UI_buttons_operatortypes();
118         
119         /* register operators */
120         spacetypes = BKE_spacetypes_list();
121         for(type=spacetypes->first; type; type=type->next) {
122                 if(type->operatortypes)
123                         type->operatortypes();
124         }
125
126         /* Macros's must go last since they reference other operators
127          * maybe we'll need to have them go after python operators too? */
128         ED_operatormacros_armature();
129         ED_operatormacros_mesh();
130         ED_operatormacros_node();
131         ED_operatormacros_object();
132         ED_operatormacros_file();
133         ED_operatormacros_graph();
134         ED_operatormacros_action();
135         
136         /* register dropboxes (can use macros) */
137         spacetypes = BKE_spacetypes_list();
138         for(type=spacetypes->first; type; type=type->next) {
139                 if(type->dropboxes)
140                         type->dropboxes();
141         }
142         
143 }
144
145 /* called in wm.c */
146 /* keymap definitions are registered only once per WM initialize, usually on file read,
147    using the keymap the actual areas/regions add the handlers */
148 void ED_spacetypes_keymap(wmKeyConfig *keyconf)
149 {
150         const ListBase *spacetypes;
151         SpaceType *stype;
152         ARegionType *atype;
153
154         ED_keymap_screen(keyconf);
155         ED_keymap_anim(keyconf);
156         ED_keymap_animchannels(keyconf);
157         ED_keymap_gpencil(keyconf);
158         ED_keymap_object(keyconf); /* defines lattice also */
159         ED_keymap_mesh(keyconf);
160         ED_keymap_uvedit(keyconf);
161         ED_keymap_curve(keyconf);
162         ED_keymap_armature(keyconf);
163         ED_keymap_physics(keyconf);
164         ED_keymap_metaball(keyconf);
165         ED_keymap_paint(keyconf);
166         ED_marker_keymap(keyconf);
167
168         UI_view2d_keymap(keyconf);
169
170         spacetypes = BKE_spacetypes_list();
171         for(stype=spacetypes->first; stype; stype=stype->next) {
172                 if(stype->keymap)
173                         stype->keymap(keyconf);
174                 for(atype=stype->regiontypes.first; atype; atype=atype->next) {
175                         if(atype->keymap)
176                                 atype->keymap(keyconf);
177                 }
178         }
179 }
180
181 /* ********************** custom drawcall api ***************** */
182
183 typedef struct RegionDrawCB {
184         struct RegionDrawCB *next, *prev;
185         
186         void (*draw)(const struct bContext *, struct ARegion *, void *);        
187         void *customdata;
188         
189         int type;
190         
191 } RegionDrawCB;
192
193 void *ED_region_draw_cb_activate(ARegionType *art, 
194                                                                  void   (*draw)(const struct bContext *, struct ARegion *, void *),
195                                                                  void *customdata, int type)
196 {
197         RegionDrawCB *rdc= MEM_callocN(sizeof(RegionDrawCB), "RegionDrawCB");
198         
199         BLI_addtail(&art->drawcalls, rdc);
200         rdc->draw= draw;
201         rdc->customdata= customdata;
202         rdc->type= type;
203         
204         return rdc;
205 }
206
207 void ED_region_draw_cb_exit(ARegionType *art, void *handle)
208 {
209         RegionDrawCB *rdc;
210         
211         for(rdc= art->drawcalls.first; rdc; rdc= rdc->next) {
212                 if(rdc==(RegionDrawCB *)handle) {
213                         BLI_remlink(&art->drawcalls, rdc);
214                         MEM_freeN(rdc);
215                         return;
216                 }
217         }
218 }
219
220 void *ED_region_draw_cb_customdata(void *handle)
221 {
222         return ((RegionDrawCB *)handle)->customdata;
223 }
224
225 void ED_region_draw_cb_draw(const bContext *C, ARegion *ar, int type)
226 {
227         RegionDrawCB *rdc;
228         
229         for(rdc= ar->type->drawcalls.first; rdc; rdc= rdc->next) {
230                 if(rdc->type==type)
231                         rdc->draw(C, ar, rdc->customdata);
232         }               
233 }
234
235
236
237 /* ********************* space template *********************** */
238 /* forward declare */
239 void ED_spacetype_xxx(void);
240
241 /* allocate and init some vars */
242 static SpaceLink *xxx_new(const bContext *UNUSED(C))
243 {
244         return NULL;
245 }
246
247 /* not spacelink itself */
248 static void xxx_free(SpaceLink *UNUSED(sl))
249 {
250
251 }
252
253 /* spacetype; init callback for usage, should be redoable */
254 static void xxx_init(wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
255 {
256         
257         /* link area to SpaceXXX struct */
258         
259         /* define how many regions, the order and types */
260         
261         /* add types to regions */
262 }
263
264 static SpaceLink *xxx_duplicate(SpaceLink *UNUSED(sl))
265 {
266         
267         return NULL;
268 }
269
270 static void xxx_operatortypes(void)
271 {
272         /* register operator types for this space */
273 }
274
275 static void xxx_keymap(wmKeyConfig *UNUSED(keyconf))
276 {
277         /* add default items to keymap */
278 }
279
280 /* only called once, from screen/spacetypes.c */
281 void ED_spacetype_xxx(void)
282 {
283         static SpaceType st;
284         
285         st.spaceid= SPACE_VIEW3D;
286         
287         st.new= xxx_new;
288         st.free= xxx_free;
289         st.init= xxx_init;
290         st.duplicate= xxx_duplicate;
291         st.operatortypes= xxx_operatortypes;
292         st.keymap= xxx_keymap;
293         
294         BKE_spacetype_register(&st);
295 }
296
297 /* ****************************** end template *********************** */
298
299
300
301