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