Cleanup: style, use braces for editors
[blender.git] / source / blender / editors / space_api / spacetypes.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) Blender Foundation, 2008
17  */
18
19 /** \file
20  * \ingroup spapi
21  */
22
23 #include <stdlib.h>
24
25 #include "MEM_guardedalloc.h"
26
27 #include "BLI_blenlib.h"
28 #include "BLI_utildefines.h"
29
30 #include "DNA_scene_types.h"
31 #include "DNA_windowmanager_types.h"
32
33 #include "BKE_context.h"
34 #include "BKE_screen.h"
35
36 #include "UI_interface.h"
37 #include "UI_view2d.h"
38
39 #include "ED_anim_api.h"
40 #include "ED_armature.h"
41 #include "ED_curve.h"
42 #include "ED_fileselect.h"
43 #include "ED_gpencil.h"
44 #include "ED_markers.h"
45 #include "ED_mesh.h"
46 #include "ED_node.h"
47 #include "ED_object.h"
48 #include "ED_paint.h"
49 #include "ED_physics.h"
50 #include "ED_render.h"
51 #include "ED_scene.h"
52 #include "ED_screen.h"
53 #include "ED_sculpt.h"
54 #include "ED_space_api.h"
55 #include "ED_sound.h"
56 #include "ED_uvedit.h"
57 #include "ED_userpref.h"
58 #include "ED_lattice.h"
59 #include "ED_mball.h"
60 #include "ED_logic.h"
61 #include "ED_clip.h"
62 #include "ED_mask.h"
63 #include "ED_sequencer.h"
64 #include "ED_gizmo_library.h"
65 #include "ED_transform.h"
66
67 #include "io_ops.h"
68
69 /* only call once on startup, storage is global in BKE kernel listbase */
70 void ED_spacetypes_init(void)
71 {
72   const ListBase *spacetypes;
73   SpaceType *type;
74
75   /* UI_UNIT_X is now a variable, is used in some spacetype inits? */
76   U.widget_unit = 20;
77
78   /* create space types */
79   ED_spacetype_outliner();
80   ED_spacetype_view3d();
81   ED_spacetype_ipo();
82   ED_spacetype_image();
83   ED_spacetype_node();
84   ED_spacetype_buttons();
85   ED_spacetype_info();
86   ED_spacetype_file();
87   ED_spacetype_action();
88   ED_spacetype_nla();
89   ED_spacetype_script();
90   ED_spacetype_text();
91   ED_spacetype_sequencer();
92   ED_spacetype_console();
93   ED_spacetype_userpref();
94   ED_spacetype_clip();
95   ED_spacetype_statusbar();
96   ED_spacetype_topbar();
97   //  ...
98
99   /* register operator types for screen and all spaces */
100   ED_operatortypes_userpref();
101   ED_operatortypes_workspace();
102   ED_operatortypes_scene();
103   ED_operatortypes_screen();
104   ED_operatortypes_anim();
105   ED_operatortypes_animchannels();
106   ED_operatortypes_gpencil();
107   ED_operatortypes_object();
108   ED_operatortypes_lattice();
109   ED_operatortypes_mesh();
110   ED_operatortypes_sculpt();
111   ED_operatortypes_uvedit();
112   ED_operatortypes_paint();
113   ED_operatortypes_physics();
114   ED_operatortypes_curve();
115   ED_operatortypes_armature();
116   ED_operatortypes_marker();
117   ED_operatortypes_metaball();
118   ED_operatortypes_sound();
119   ED_operatortypes_render();
120   ED_operatortypes_mask();
121   ED_operatortypes_io();
122
123   ED_operatortypes_view2d();
124   ED_operatortypes_ui();
125
126   ED_screen_user_menu_register();
127
128   /* gizmo types */
129   ED_gizmotypes_button_2d();
130   ED_gizmotypes_dial_3d();
131   ED_gizmotypes_move_3d();
132   ED_gizmotypes_arrow_2d();
133   ED_gizmotypes_arrow_3d();
134   ED_gizmotypes_preselect_3d();
135   ED_gizmotypes_primitive_3d();
136   ED_gizmotypes_blank_3d();
137   ED_gizmotypes_cage_2d();
138   ED_gizmotypes_cage_3d();
139   ED_gizmotypes_value_2d();
140
141   /* gizmo group types */
142   ED_gizmogrouptypes_value_2d();
143
144   /* register types for operators and gizmos */
145   spacetypes = BKE_spacetypes_list();
146   for (type = spacetypes->first; type; type = type->next) {
147     /* init gizmo types first, operator-types need them */
148     if (type->gizmos) {
149       type->gizmos();
150     }
151     if (type->operatortypes) {
152       type->operatortypes();
153     }
154   }
155 }
156
157 void ED_spacemacros_init(void)
158 {
159   const ListBase *spacetypes;
160   SpaceType *type;
161
162   /* Macros's must go last since they reference other operators.
163    * We need to have them go after python operators too */
164   ED_operatormacros_armature();
165   ED_operatormacros_mesh();
166   ED_operatormacros_metaball();
167   ED_operatormacros_node();
168   ED_operatormacros_object();
169   ED_operatormacros_file();
170   ED_operatormacros_graph();
171   ED_operatormacros_action();
172   ED_operatormacros_clip();
173   ED_operatormacros_curve();
174   ED_operatormacros_mask();
175   ED_operatormacros_sequencer();
176   ED_operatormacros_paint();
177   ED_operatormacros_gpencil();
178
179   /* register dropboxes (can use macros) */
180   spacetypes = BKE_spacetypes_list();
181   for (type = spacetypes->first; type; type = type->next) {
182     if (type->dropboxes) {
183       type->dropboxes();
184     }
185   }
186 }
187
188 /* called in wm.c */
189 /* keymap definitions are registered only once per WM initialize, usually on file read,
190  * using the keymap the actual areas/regions add the handlers */
191 void ED_spacetypes_keymap(wmKeyConfig *keyconf)
192 {
193   const ListBase *spacetypes;
194   SpaceType *stype;
195   ARegionType *atype;
196
197   ED_keymap_screen(keyconf);
198   ED_keymap_anim(keyconf);
199   ED_keymap_animchannels(keyconf);
200   ED_keymap_gpencil(keyconf);
201   ED_keymap_object(keyconf);
202   ED_keymap_lattice(keyconf);
203   ED_keymap_mesh(keyconf);
204   ED_keymap_uvedit(keyconf);
205   ED_keymap_curve(keyconf);
206   ED_keymap_armature(keyconf);
207   ED_keymap_physics(keyconf);
208   ED_keymap_metaball(keyconf);
209   ED_keymap_paint(keyconf);
210   ED_keymap_mask(keyconf);
211   ED_keymap_marker(keyconf);
212
213   ED_keymap_view2d(keyconf);
214   ED_keymap_ui(keyconf);
215
216   ED_keymap_transform(keyconf);
217
218   spacetypes = BKE_spacetypes_list();
219   for (stype = spacetypes->first; stype; stype = stype->next) {
220     if (stype->keymap) {
221       stype->keymap(keyconf);
222     }
223     for (atype = stype->regiontypes.first; atype; atype = atype->next) {
224       if (atype->keymap) {
225         atype->keymap(keyconf);
226       }
227     }
228   }
229 }
230
231 /* ********************** custom drawcall api ***************** */
232
233 typedef struct RegionDrawCB {
234   struct RegionDrawCB *next, *prev;
235
236   void (*draw)(const struct bContext *, struct ARegion *, void *);
237   void *customdata;
238
239   int type;
240
241 } RegionDrawCB;
242
243 void *ED_region_draw_cb_activate(ARegionType *art,
244                                  void (*draw)(const struct bContext *, struct ARegion *, void *),
245                                  void *customdata,
246                                  int type)
247 {
248   RegionDrawCB *rdc = MEM_callocN(sizeof(RegionDrawCB), "RegionDrawCB");
249
250   BLI_addtail(&art->drawcalls, rdc);
251   rdc->draw = draw;
252   rdc->customdata = customdata;
253   rdc->type = type;
254
255   return rdc;
256 }
257
258 void ED_region_draw_cb_exit(ARegionType *art, void *handle)
259 {
260   RegionDrawCB *rdc;
261
262   for (rdc = art->drawcalls.first; rdc; rdc = rdc->next) {
263     if (rdc == (RegionDrawCB *)handle) {
264       BLI_remlink(&art->drawcalls, rdc);
265       MEM_freeN(rdc);
266       return;
267     }
268   }
269 }
270
271 void ED_region_draw_cb_draw(const bContext *C, ARegion *ar, int type)
272 {
273   RegionDrawCB *rdc;
274
275   for (rdc = ar->type->drawcalls.first; rdc; rdc = rdc->next) {
276     if (rdc->type == type) {
277       rdc->draw(C, ar, rdc->customdata);
278     }
279   }
280 }
281
282 /* ********************* space template *********************** */
283 /* forward declare */
284 void ED_spacetype_xxx(void);
285
286 /* allocate and init some vars */
287 static SpaceLink *xxx_new(const ScrArea *UNUSED(sa), const Scene *UNUSED(scene))
288 {
289   return NULL;
290 }
291
292 /* not spacelink itself */
293 static void xxx_free(SpaceLink *UNUSED(sl))
294 {
295 }
296
297 /* spacetype; init callback for usage, should be redoable */
298 static void xxx_init(wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
299 {
300
301   /* link area to SpaceXXX struct */
302
303   /* define how many regions, the order and types */
304
305   /* add types to regions */
306 }
307
308 static SpaceLink *xxx_duplicate(SpaceLink *UNUSED(sl))
309 {
310
311   return NULL;
312 }
313
314 static void xxx_operatortypes(void)
315 {
316   /* register operator types for this space */
317 }
318
319 static void xxx_keymap(wmKeyConfig *UNUSED(keyconf))
320 {
321   /* add default items to keymap */
322 }
323
324 /* only called once, from screen/spacetypes.c */
325 void ED_spacetype_xxx(void)
326 {
327   static SpaceType st;
328
329   st.spaceid = SPACE_VIEW3D;
330
331   st.new = xxx_new;
332   st.free = xxx_free;
333   st.init = xxx_init;
334   st.duplicate = xxx_duplicate;
335   st.operatortypes = xxx_operatortypes;
336   st.keymap = xxx_keymap;
337
338   BKE_spacetype_register(&st);
339 }
340
341 /* ****************************** end template *********************** */