3df114282fd40d7fe6041a345d143ec2f523992e
[blender.git] / source / blender / makesrna / intern / rna_context.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  * Contributor(s): Blender Foundation (2009).
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/makesrna/intern/rna_context.c
24  *  \ingroup RNA
25  */
26
27 #include <stdlib.h>
28
29 #include "DNA_ID.h"
30 #include "DNA_userdef_types.h"
31
32 #include "BLI_utildefines.h"
33 #include "BKE_context.h"
34
35 #include "RNA_access.h"
36 #include "RNA_define.h"
37
38 #include "rna_internal.h" /* own include */
39
40 #ifdef RNA_RUNTIME
41
42 #include "RE_engine.h"
43
44 static PointerRNA rna_Context_manager_get(PointerRNA *ptr)
45 {
46         bContext *C = (bContext *)ptr->data;
47         return rna_pointer_inherit_refine(ptr, &RNA_WindowManager, CTX_wm_manager(C));
48 }
49
50 static PointerRNA rna_Context_window_get(PointerRNA *ptr)
51 {
52         bContext *C = (bContext *)ptr->data;
53         return rna_pointer_inherit_refine(ptr, &RNA_Window, CTX_wm_window(C));
54 }
55
56 static PointerRNA rna_Context_workspace_get(PointerRNA *ptr)
57 {
58         bContext *C = (bContext *)ptr->data;
59         return rna_pointer_inherit_refine(ptr, &RNA_WorkSpace, CTX_wm_workspace(C));
60 }
61
62 static PointerRNA rna_Context_screen_get(PointerRNA *ptr)
63 {
64         bContext *C = (bContext *)ptr->data;
65         return rna_pointer_inherit_refine(ptr, &RNA_Screen, CTX_wm_screen(C));
66 }
67
68 static PointerRNA rna_Context_area_get(PointerRNA *ptr)
69 {
70         bContext *C = (bContext *)ptr->data;
71         PointerRNA newptr;
72         RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Area, CTX_wm_area(C), &newptr);
73         return newptr;
74 }
75
76 static PointerRNA rna_Context_space_data_get(PointerRNA *ptr)
77 {
78         bContext *C = (bContext *)ptr->data;
79         PointerRNA newptr;
80         RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Space, CTX_wm_space_data(C), &newptr);
81         return newptr;
82 }
83
84 static PointerRNA rna_Context_region_get(PointerRNA *ptr)
85 {
86         bContext *C = (bContext *)ptr->data;
87         PointerRNA newptr;
88         RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Region, CTX_wm_region(C), &newptr);
89         return newptr;
90 }
91
92 static PointerRNA rna_Context_region_data_get(PointerRNA *ptr)
93 {
94         bContext *C = (bContext *)ptr->data;
95
96         /* only exists for one space still, no generic system yet */
97         if (CTX_wm_view3d(C)) {
98                 PointerRNA newptr;
99                 RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_RegionView3D, CTX_wm_region_data(C), &newptr);
100                 return newptr;
101         }
102
103         return PointerRNA_NULL;
104 }
105
106 static PointerRNA rna_Context_manipulator_group_get(PointerRNA *ptr)
107 {
108         bContext *C = (bContext *)ptr->data;
109         PointerRNA newptr;
110         RNA_pointer_create(NULL, &RNA_ManipulatorGroup, CTX_wm_manipulator_group(C), &newptr);
111         return newptr;
112 }
113
114 static PointerRNA rna_Context_main_get(PointerRNA *ptr)
115 {
116         bContext *C = (bContext *)ptr->data;
117         return rna_pointer_inherit_refine(ptr, &RNA_BlendData, CTX_data_main(C));
118 }
119
120 static PointerRNA rna_Context_depsgraph_get(PointerRNA *ptr)
121 {
122         bContext *C = (bContext *)ptr->data;
123         return rna_pointer_inherit_refine(ptr, &RNA_Depsgraph, CTX_data_depsgraph(C));
124 }
125
126 static PointerRNA rna_Context_scene_get(PointerRNA *ptr)
127 {
128         bContext *C = (bContext *)ptr->data;
129         return rna_pointer_inherit_refine(ptr, &RNA_Scene, CTX_data_scene(C));
130 }
131
132 static PointerRNA rna_Context_view_layer_get(PointerRNA *ptr)
133 {
134         bContext *C = (bContext *)ptr->data;
135         return rna_pointer_inherit_refine(ptr, &RNA_ViewLayer, CTX_data_view_layer(C));
136 }
137
138 static PointerRNA rna_Context_view_render_get(PointerRNA *ptr)
139 {
140         bContext *C = (bContext *)ptr->data;
141         return rna_pointer_inherit_refine(ptr, &RNA_ViewRenderSettings, CTX_data_view_render(C));
142 }
143
144 static void rna_Context_engine_get(PointerRNA *ptr, char *value)
145  {
146         bContext *C = (bContext *)ptr->data;
147         RenderEngineType *engine_type = CTX_data_engine_type(C);
148         strcpy(value, engine_type->idname);
149 }
150
151 static int rna_Context_engine_length(PointerRNA *ptr)
152 {
153         bContext *C = (bContext *)ptr->data;
154         RenderEngineType *engine_type = CTX_data_engine_type(C);
155         return strlen(engine_type->idname);
156 }
157
158 static PointerRNA rna_Context_scene_collection_get(PointerRNA *ptr)
159 {
160         bContext *C = (bContext *)ptr->data;
161         ptr->id.data = CTX_data_scene(C);
162         return rna_pointer_inherit_refine(ptr, &RNA_SceneCollection, CTX_data_scene_collection(C));
163 }
164
165 static PointerRNA rna_Context_layer_collection_get(PointerRNA *ptr)
166 {
167         bContext *C = (bContext *)ptr->data;
168         ptr->id.data = CTX_data_scene(C);
169         return rna_pointer_inherit_refine(ptr, &RNA_LayerCollection, CTX_data_layer_collection(C));
170 }
171
172 static PointerRNA rna_Context_tool_settings_get(PointerRNA *ptr)
173 {
174         bContext *C = (bContext *)ptr->data;
175         ptr->id.data = CTX_data_scene(C);
176         return rna_pointer_inherit_refine(ptr, &RNA_ToolSettings, CTX_data_tool_settings(C));
177 }
178
179 static PointerRNA rna_Context_user_preferences_get(PointerRNA *UNUSED(ptr))
180 {
181         PointerRNA newptr;
182         RNA_pointer_create(NULL, &RNA_UserPreferences, &U, &newptr);
183         return newptr;
184 }
185
186 static int rna_Context_mode_get(PointerRNA *ptr)
187 {
188         bContext *C = (bContext *)ptr->data;
189         return CTX_data_mode_enum(C);
190 }
191
192 #else
193
194 void RNA_def_context(BlenderRNA *brna)
195 {
196         StructRNA *srna;
197         PropertyRNA *prop;
198
199         static const EnumPropertyItem mode_items[] = {
200                 {CTX_MODE_EDIT_MESH, "EDIT_MESH", 0, "Mesh Edit", ""},
201                 {CTX_MODE_EDIT_CURVE, "EDIT_CURVE", 0, "Curve Edit", ""},
202                 {CTX_MODE_EDIT_SURFACE, "EDIT_SURFACE", 0, "Surface Edit", ""},
203                 {CTX_MODE_EDIT_TEXT, "EDIT_TEXT", 0, "Edit Edit", ""},
204                 {CTX_MODE_EDIT_ARMATURE, "EDIT_ARMATURE", 0, "Armature Edit", ""}, /* PARSKEL reuse will give issues */
205                 {CTX_MODE_EDIT_METABALL, "EDIT_METABALL", 0, "Metaball Edit", ""},
206                 {CTX_MODE_EDIT_LATTICE, "EDIT_LATTICE", 0, "Lattice Edit", ""},
207                 {CTX_MODE_POSE, "POSE", 0, "Pose ", ""},
208                 {CTX_MODE_SCULPT, "SCULPT", 0, "Sculpt", ""},
209                 {CTX_MODE_PAINT_WEIGHT, "PAINT_WEIGHT", 0, "Weight Paint", ""},
210                 {CTX_MODE_PAINT_VERTEX, "PAINT_VERTEX", 0, "Vertex Paint", ""},
211                 {CTX_MODE_PAINT_TEXTURE, "PAINT_TEXTURE", 0, "Texture Paint", ""},
212                 {CTX_MODE_PARTICLE, "PARTICLE", 0, "Particle", ""},
213                 {CTX_MODE_OBJECT, "OBJECT", 0, "Object", ""},
214                 {0, NULL, 0, NULL, NULL}
215         };
216
217         srna = RNA_def_struct(brna, "Context", NULL);
218         RNA_def_struct_ui_text(srna, "Context", "Current windowmanager and data context");
219         RNA_def_struct_sdna(srna, "bContext");
220
221         /* WM */
222         prop = RNA_def_property(srna, "window_manager", PROP_POINTER, PROP_NONE);
223         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
224         RNA_def_property_struct_type(prop, "WindowManager");
225         RNA_def_property_pointer_funcs(prop, "rna_Context_manager_get", NULL, NULL, NULL);
226
227         prop = RNA_def_property(srna, "window", PROP_POINTER, PROP_NONE);
228         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
229         RNA_def_property_struct_type(prop, "Window");
230         RNA_def_property_pointer_funcs(prop, "rna_Context_window_get", NULL, NULL, NULL);
231
232         prop = RNA_def_property(srna, "workspace", PROP_POINTER, PROP_NONE);
233         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
234         RNA_def_property_struct_type(prop, "WorkSpace");
235         RNA_def_property_pointer_funcs(prop, "rna_Context_workspace_get", NULL, NULL, NULL);
236
237         prop = RNA_def_property(srna, "screen", PROP_POINTER, PROP_NONE);
238         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
239         RNA_def_property_struct_type(prop, "Screen");
240         RNA_def_property_pointer_funcs(prop, "rna_Context_screen_get", NULL, NULL, NULL);
241
242         prop = RNA_def_property(srna, "area", PROP_POINTER, PROP_NONE);
243         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
244         RNA_def_property_struct_type(prop, "Area");
245         RNA_def_property_pointer_funcs(prop, "rna_Context_area_get", NULL, NULL, NULL);
246
247         prop = RNA_def_property(srna, "space_data", PROP_POINTER, PROP_NONE);
248         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
249         RNA_def_property_struct_type(prop, "Space");
250         RNA_def_property_pointer_funcs(prop, "rna_Context_space_data_get", NULL, NULL, NULL);
251
252         prop = RNA_def_property(srna, "region", PROP_POINTER, PROP_NONE);
253         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
254         RNA_def_property_struct_type(prop, "Region");
255         RNA_def_property_pointer_funcs(prop, "rna_Context_region_get", NULL, NULL, NULL);
256
257         prop = RNA_def_property(srna, "region_data", PROP_POINTER, PROP_NONE);
258         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
259         RNA_def_property_struct_type(prop, "RegionView3D");
260         RNA_def_property_pointer_funcs(prop, "rna_Context_region_data_get", NULL, NULL, NULL);
261
262         prop = RNA_def_property(srna, "manipulator_group", PROP_POINTER, PROP_NONE);
263         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
264         RNA_def_property_struct_type(prop, "ManipulatorGroup");
265         RNA_def_property_pointer_funcs(prop, "rna_Context_manipulator_group_get", NULL, NULL, NULL);
266
267         /* Data */
268         prop = RNA_def_property(srna, "blend_data", PROP_POINTER, PROP_NONE);
269         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
270         RNA_def_property_struct_type(prop, "BlendData");
271         RNA_def_property_pointer_funcs(prop, "rna_Context_main_get", NULL, NULL, NULL);
272
273         prop = RNA_def_property(srna, "depsgraph", PROP_POINTER, PROP_NONE);
274         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
275         RNA_def_property_struct_type(prop, "Depsgraph");
276         RNA_def_property_pointer_funcs(prop, "rna_Context_depsgraph_get", NULL, NULL, NULL);
277
278         prop = RNA_def_property(srna, "scene", PROP_POINTER, PROP_NONE);
279         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
280         RNA_def_property_struct_type(prop, "Scene");
281         RNA_def_property_pointer_funcs(prop, "rna_Context_scene_get", NULL, NULL, NULL);
282
283         prop = RNA_def_property(srna, "view_layer", PROP_POINTER, PROP_NONE);
284         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
285         RNA_def_property_struct_type(prop, "ViewLayer");
286         RNA_def_property_pointer_funcs(prop, "rna_Context_view_layer_get", NULL, NULL, NULL);
287
288         prop = RNA_def_property(srna, "view_render", PROP_POINTER, PROP_NONE);
289         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
290         RNA_def_property_struct_type(prop, "ViewRenderSettings");
291         RNA_def_property_pointer_funcs(prop, "rna_Context_view_render_get", NULL, NULL, NULL);
292
293         prop = RNA_def_property(srna, "engine", PROP_STRING, PROP_NONE);
294         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
295         RNA_def_property_string_funcs(prop, "rna_Context_engine_get", "rna_Context_engine_length", NULL);
296
297         prop = RNA_def_property(srna, "scene_collection", PROP_POINTER, PROP_NONE);
298         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
299         RNA_def_property_struct_type(prop, "SceneCollection");
300         RNA_def_property_pointer_funcs(prop, "rna_Context_scene_collection_get", NULL, NULL, NULL);
301
302         prop = RNA_def_property(srna, "layer_collection", PROP_POINTER, PROP_NONE);
303         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
304         RNA_def_property_struct_type(prop, "LayerCollection");
305         RNA_def_property_pointer_funcs(prop, "rna_Context_layer_collection_get", NULL, NULL, NULL);
306
307         prop = RNA_def_property(srna, "tool_settings", PROP_POINTER, PROP_NONE);
308         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
309         RNA_def_property_struct_type(prop, "ToolSettings");
310         RNA_def_property_pointer_funcs(prop, "rna_Context_tool_settings_get", NULL, NULL, NULL);
311
312         prop = RNA_def_property(srna, "user_preferences", PROP_POINTER, PROP_NONE);
313         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
314         RNA_def_property_struct_type(prop, "UserPreferences");
315         RNA_def_property_pointer_funcs(prop, "rna_Context_user_preferences_get", NULL, NULL, NULL);
316
317         prop = RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
318         RNA_def_property_enum_items(prop, mode_items);
319         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
320         RNA_def_property_enum_funcs(prop, "rna_Context_mode_get", NULL, NULL);
321 }
322
323 #endif
324