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