Merged changes in the trunk up to revision 54110.
[blender.git] / source / blender / makesrna / intern / rna_main.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 (2008).
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/makesrna/intern/rna_main.c
24  *  \ingroup RNA
25  */
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "BLI_path_util.h"
31
32 #include "RNA_define.h"
33
34 #include "rna_internal.h"
35
36 #ifdef RNA_RUNTIME
37
38 #include "BKE_main.h"
39 #include "BKE_mesh.h"
40 #include "BKE_global.h"
41
42 /* all the list begin functions are added manually here, Main is not in SDNA */
43
44 static int rna_Main_is_saved_get(PointerRNA *UNUSED(ptr))
45 {
46         return G.relbase_valid;
47 }
48
49 static int rna_Main_is_dirty_get(PointerRNA *ptr)
50 {
51         /* XXX, not totally nice to do it this way, should store in main ? */
52         Main *bmain = (Main *)ptr->data;
53         wmWindowManager *wm;
54         if ((wm = bmain->wm.first)) {
55                 return !wm->file_saved;
56         }
57
58         return TRUE;
59 }
60
61 static void rna_Main_filepath_get(PointerRNA *ptr, char *value)
62 {
63         Main *bmain = (Main *)ptr->data;
64         BLI_strncpy(value, bmain->name, sizeof(bmain->name));
65 }
66
67 static int rna_Main_filepath_length(PointerRNA *ptr)
68 {
69         Main *bmain = (Main *)ptr->data;
70         return strlen(bmain->name);
71 }
72
73 #if 0
74 static void rna_Main_filepath_set(PointerRNA *ptr, const char *value)
75 {
76         Main *bmain = (Main *)ptr->data;
77         BLI_strncpy(bmain->name, value, sizeof(bmain->name));
78 }
79 #endif
80
81 static void rna_Main_scene_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
82 {
83         Main *bmain = (Main *)ptr->data;
84         rna_iterator_listbase_begin(iter, &bmain->scene, NULL);
85 }
86
87 static void rna_Main_object_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
88 {
89         Main *bmain = (Main *)ptr->data;
90         rna_iterator_listbase_begin(iter, &bmain->object, NULL);
91 }
92
93 static void rna_Main_lamp_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
94 {
95         Main *bmain = (Main *)ptr->data;
96         rna_iterator_listbase_begin(iter, &bmain->lamp, NULL);
97 }
98
99 static void rna_Main_library_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
100 {
101         Main *bmain = (Main *)ptr->data;
102         rna_iterator_listbase_begin(iter, &bmain->library, NULL);
103 }
104
105 static void rna_Main_mesh_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
106 {
107         Main *bmain = (Main *)ptr->data;
108         rna_iterator_listbase_begin(iter, &bmain->mesh, NULL);
109 }
110
111 static void rna_Main_curve_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
112 {
113         Main *bmain = (Main *)ptr->data;
114         rna_iterator_listbase_begin(iter, &bmain->curve, NULL);
115 }
116
117 static void rna_Main_mball_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
118 {
119         Main *bmain = (Main *)ptr->data;
120         rna_iterator_listbase_begin(iter, &bmain->mball, NULL);
121 }
122
123 static void rna_Main_mat_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
124 {
125         Main *bmain = (Main *)ptr->data;
126         rna_iterator_listbase_begin(iter, &bmain->mat, NULL);
127 }
128
129 static void rna_Main_tex_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
130 {
131         Main *bmain = (Main *)ptr->data;
132         rna_iterator_listbase_begin(iter, &bmain->tex, NULL);
133 }
134
135 static void rna_Main_image_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
136 {
137         Main *bmain = (Main *)ptr->data;
138         rna_iterator_listbase_begin(iter, &bmain->image, NULL);
139 }
140
141 static void rna_Main_latt_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
142 {
143         Main *bmain = (Main *)ptr->data;
144         rna_iterator_listbase_begin(iter, &bmain->latt, NULL);
145 }
146
147 static void rna_Main_camera_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
148 {
149         Main *bmain = (Main *)ptr->data;
150         rna_iterator_listbase_begin(iter, &bmain->camera, NULL);
151 }
152
153 static void rna_Main_key_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
154 {
155         Main *bmain = (Main *)ptr->data;
156         rna_iterator_listbase_begin(iter, &bmain->key, NULL);
157 }
158
159 static void rna_Main_world_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
160 {
161         Main *bmain = (Main *)ptr->data;
162         rna_iterator_listbase_begin(iter, &bmain->world, NULL);
163 }
164
165 static void rna_Main_screen_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
166 {
167         Main *bmain = (Main *)ptr->data;
168         rna_iterator_listbase_begin(iter, &bmain->screen, NULL);
169 }
170
171 static void rna_Main_script_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
172 {
173         Main *bmain = (Main *)ptr->data;
174         rna_iterator_listbase_begin(iter, &bmain->script, NULL);
175 }
176
177 static void rna_Main_font_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
178 {
179         Main *bmain = (Main *)ptr->data;
180         rna_iterator_listbase_begin(iter, &bmain->vfont, NULL);
181 }
182
183 static void rna_Main_text_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
184 {
185         Main *bmain = (Main *)ptr->data;
186         rna_iterator_listbase_begin(iter, &bmain->text, NULL);
187 }
188
189 static void rna_Main_speaker_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
190 {
191         Main *bmain = (Main *)ptr->data;
192         rna_iterator_listbase_begin(iter, &bmain->speaker, NULL);
193 }
194
195 static void rna_Main_sound_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
196 {
197         Main *bmain = (Main *)ptr->data;
198         rna_iterator_listbase_begin(iter, &bmain->sound, NULL);
199 }
200
201 static void rna_Main_group_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
202 {
203         Main *bmain = (Main *)ptr->data;
204         rna_iterator_listbase_begin(iter, &bmain->group, NULL);
205 }
206
207 static void rna_Main_armature_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
208 {
209         Main *bmain = (Main *)ptr->data;
210         rna_iterator_listbase_begin(iter, &bmain->armature, NULL);
211 }
212
213 static void rna_Main_action_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
214 {
215         Main *bmain = (Main *)ptr->data;
216         rna_iterator_listbase_begin(iter, &bmain->action, NULL);
217 }
218
219 static void rna_Main_nodetree_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
220 {
221         Main *bmain = (Main *)ptr->data;
222         rna_iterator_listbase_begin(iter, &bmain->nodetree, NULL);
223 }
224
225 static void rna_Main_brush_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
226 {
227         Main *bmain = (Main *)ptr->data;
228         rna_iterator_listbase_begin(iter, &bmain->brush, NULL);
229 }
230
231 static void rna_Main_particle_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
232 {
233         Main *bmain = (Main *)ptr->data;
234         rna_iterator_listbase_begin(iter, &bmain->particle, NULL);
235 }
236
237 static void rna_Main_gpencil_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
238 {
239         Main *bmain = (Main *)ptr->data;
240         rna_iterator_listbase_begin(iter, &bmain->gpencil, NULL);
241 }
242
243 static void rna_Main_wm_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
244 {
245         Main *bmain = (Main *)ptr->data;
246         rna_iterator_listbase_begin(iter, &bmain->wm, NULL);
247 }
248
249 static void rna_Main_movieclips_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
250 {
251         Main *bmain = (Main *)ptr->data;
252         rna_iterator_listbase_begin(iter, &bmain->movieclip, NULL);
253 }
254
255 static void rna_Main_masks_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
256 {
257         Main *bmain = (Main *)ptr->data;
258         rna_iterator_listbase_begin(iter, &bmain->mask, NULL);
259 }
260
261 #ifdef WITH_FREESTYLE
262 static void rna_Main_linestyle_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
263 {
264         Main *bmain = (Main*)ptr->data;
265         rna_iterator_listbase_begin(iter, &bmain->linestyle, NULL);
266 }
267 #endif
268
269 #ifdef UNIT_TEST
270
271 static PointerRNA rna_Test_test_get(PointerRNA *ptr)
272 {
273         PointerRNA ret = *ptr;
274         ret.type = &RNA_Test;
275
276         return ret;
277 }
278
279 #endif
280
281 #else
282
283 /* local convenience types */
284 typedef void (CollectionDefFunc)(struct BlenderRNA *brna, struct PropertyRNA *cprop);
285
286 typedef struct MainCollectionDef {
287         const char *identifier;
288         const char *type;
289         const char *iter_begin;
290         const char *name;
291         const char *description;
292         CollectionDefFunc *func;
293 } MainCollectionDef;
294
295 void RNA_def_main(BlenderRNA *brna)
296 {
297         StructRNA *srna;
298         PropertyRNA *prop;
299         CollectionDefFunc *func;
300
301         /* plural must match idtypes in readblenentry.c */
302         MainCollectionDef lists[] = {
303                 {"cameras", "Camera", "rna_Main_camera_begin", "Cameras", "Camera datablocks", RNA_def_main_cameras},
304                 {"scenes", "Scene", "rna_Main_scene_begin", "Scenes", "Scene datablocks", RNA_def_main_scenes},
305                 {"objects", "Object", "rna_Main_object_begin", "Objects", "Object datablocks", RNA_def_main_objects},
306                 {"materials", "Material", "rna_Main_mat_begin", "Materials", "Material datablocks", RNA_def_main_materials},
307                 {"node_groups", "NodeTree", "rna_Main_nodetree_begin", "Node Groups", "Node group datablocks", RNA_def_main_node_groups},
308                 {"meshes", "Mesh", "rna_Main_mesh_begin", "Meshes", "Mesh datablocks", RNA_def_main_meshes},
309                 {"lamps", "Lamp", "rna_Main_lamp_begin", "Lamps", "Lamp datablocks", RNA_def_main_lamps},
310                 {"libraries", "Library", "rna_Main_library_begin", "Libraries", "Library datablocks", RNA_def_main_libraries},
311                 {"screens", "Screen", "rna_Main_screen_begin", "Screens", "Screen datablocks", RNA_def_main_screens},
312                 {"window_managers", "WindowManager", "rna_Main_wm_begin", "Window Managers", "Window manager datablocks", RNA_def_main_window_managers},
313                 {"images", "Image", "rna_Main_image_begin", "Images", "Image datablocks", RNA_def_main_images},
314                 {"lattices", "Lattice", "rna_Main_latt_begin", "Lattices", "Lattice datablocks", RNA_def_main_lattices},
315                 {"curves", "Curve", "rna_Main_curve_begin", "Curves", "Curve datablocks", RNA_def_main_curves},
316                 {"metaballs", "MetaBall", "rna_Main_mball_begin", "Metaballs", "Metaball datablocks", RNA_def_main_metaballs},
317                 {"fonts", "VectorFont", "rna_Main_font_begin", "Vector Fonts", "Vector font datablocks", RNA_def_main_fonts},
318                 {"textures", "Texture", "rna_Main_tex_begin", "Textures", "Texture datablocks", RNA_def_main_textures},
319                 {"brushes", "Brush", "rna_Main_brush_begin", "Brushes", "Brush datablocks", RNA_def_main_brushes},
320                 {"worlds", "World", "rna_Main_world_begin", "Worlds", "World datablocks", RNA_def_main_worlds},
321                 {"groups", "Group", "rna_Main_group_begin", "Groups", "Group datablocks", RNA_def_main_groups},
322                 {"shape_keys", "Key", "rna_Main_key_begin", "Shape Keys", "Shape Key datablocks", NULL},
323                 {"scripts", "ID", "rna_Main_script_begin", "Scripts", "Script datablocks (DEPRECATED)", NULL},
324                 {"texts", "Text", "rna_Main_text_begin", "Texts", "Text datablocks", RNA_def_main_texts},
325                 {"speakers", "Speaker", "rna_Main_speaker_begin", "Speakers", "Speaker datablocks", RNA_def_main_speakers},
326                 {"sounds", "Sound", "rna_Main_sound_begin", "Sounds", "Sound datablocks", RNA_def_main_sounds},
327                 {"armatures", "Armature", "rna_Main_armature_begin", "Armatures", "Armature datablocks", RNA_def_main_armatures},
328                 {"actions", "Action", "rna_Main_action_begin", "Actions", "Action datablocks", RNA_def_main_actions},
329                 {"particles", "ParticleSettings", "rna_Main_particle_begin", "Particles", "Particle datablocks", RNA_def_main_particles},
330                 {"grease_pencil", "GreasePencil", "rna_Main_gpencil_begin", "Grease Pencil", "Grease Pencil datablocks", RNA_def_main_gpencil},
331                 {"movieclips", "MovieClip", "rna_Main_movieclips_begin", "Movie Clips", "Movie Clip datablocks", RNA_def_main_movieclips},
332                 {"masks", "Mask", "rna_Main_masks_begin", "Masks", "Masks datablocks", RNA_def_main_masks},
333 #ifdef WITH_FREESTYLE
334                 {"linestyles", "FreestyleLineStyle", "rna_Main_linestyle_begin", "Line Styles", "Line Style datablocks", RNA_def_main_linestyles},
335 #endif
336                 {NULL, NULL, NULL, NULL, NULL, NULL}
337         };
338
339         int i;
340         
341         srna = RNA_def_struct(brna, "BlendData", NULL);
342         RNA_def_struct_ui_text(srna, "Blendfile Data",
343                                "Main data structure representing a .blend file and all its datablocks");
344         RNA_def_struct_ui_icon(srna, ICON_BLENDER);
345
346         prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH);
347         RNA_def_property_string_maxlength(prop, FILE_MAX);
348         RNA_def_property_string_funcs(prop, "rna_Main_filepath_get", "rna_Main_filepath_length", "rna_Main_filepath_set");
349         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
350         RNA_def_property_ui_text(prop, "Filename", "Path to the .blend file");
351         
352         prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
353         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
354         RNA_def_property_boolean_funcs(prop, "rna_Main_is_dirty_get", NULL);
355         RNA_def_property_ui_text(prop, "File Has Unsaved Changes", "Have recent edits been saved to disk");
356
357         prop = RNA_def_property(srna, "is_saved", PROP_BOOLEAN, PROP_NONE);
358         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
359         RNA_def_property_boolean_funcs(prop, "rna_Main_is_saved_get", NULL);
360         RNA_def_property_ui_text(prop, "File is Saved", "Has the current session been saved to disk as a .blend file");
361
362         for (i = 0; lists[i].name; i++) {
363                 prop = RNA_def_property(srna, lists[i].identifier, PROP_COLLECTION, PROP_NONE);
364                 RNA_def_property_struct_type(prop, lists[i].type);
365                 RNA_def_property_collection_funcs(prop, lists[i].iter_begin, "rna_iterator_listbase_next",
366                                                   "rna_iterator_listbase_end", "rna_iterator_listbase_get",
367                                                   NULL, NULL, NULL, NULL);
368                 RNA_def_property_ui_text(prop, lists[i].name, lists[i].description);
369
370                 /* collection functions */
371                 func = lists[i].func;
372                 if (func)
373                         func(brna, prop);
374         }
375
376         RNA_api_main(srna);
377
378 #ifdef UNIT_TEST
379
380         RNA_define_verify_sdna(0);
381
382         prop = RNA_def_property(srna, "test", PROP_POINTER, PROP_NONE);
383         RNA_def_property_struct_type(prop, "Test");
384         RNA_def_property_pointer_funcs(prop, "rna_Test_test_get", NULL, NULL, NULL);
385
386         RNA_define_verify_sdna(1);
387
388 #endif
389 }
390
391 #endif