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