Cycles: add basic backwards compatibility for device selection, move to System tab.
[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_particle_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
243 {
244         Main *bmain = (Main *)ptr->data;
245         rna_iterator_listbase_begin(iter, &bmain->particle, NULL);
246 }
247
248 static void rna_Main_palettes_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
249 {
250         Main *bmain = (Main *)ptr->data;
251         rna_iterator_listbase_begin(iter, &bmain->palettes, NULL);
252 }
253
254 static void rna_Main_gpencil_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
255 {
256         Main *bmain = (Main *)ptr->data;
257         rna_iterator_listbase_begin(iter, &bmain->gpencil, NULL);
258 }
259
260 static void rna_Main_wm_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
261 {
262         Main *bmain = (Main *)ptr->data;
263         rna_iterator_listbase_begin(iter, &bmain->wm, NULL);
264 }
265
266 static void rna_Main_movieclips_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
267 {
268         Main *bmain = (Main *)ptr->data;
269         rna_iterator_listbase_begin(iter, &bmain->movieclip, NULL);
270 }
271
272 static void rna_Main_masks_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
273 {
274         Main *bmain = (Main *)ptr->data;
275         rna_iterator_listbase_begin(iter, &bmain->mask, NULL);
276 }
277
278 static void rna_Main_linestyle_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
279 {
280         Main *bmain = (Main *)ptr->data;
281         rna_iterator_listbase_begin(iter, &bmain->linestyle, NULL);
282 }
283
284 static void rna_Main_cachefiles_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
285 {
286         Main *bmain = (Main *)ptr->data;
287         rna_iterator_listbase_begin(iter, &bmain->cachefiles, NULL);
288 }
289
290 static void rna_Main_paintcurves_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
291 {
292         Main *bmain = (Main *)ptr->data;
293         rna_iterator_listbase_begin(iter, &bmain->paintcurves, NULL);
294 }
295
296 static void rna_Main_version_get(PointerRNA *ptr, int *value)
297 {
298         Main *bmain = (Main *)ptr->data;
299         value[0] = bmain->versionfile / 100;
300         value[1] = bmain->versionfile % 100;
301         value[2] = bmain->subversionfile;
302 }
303
304 #ifdef UNIT_TEST
305
306 static PointerRNA rna_Test_test_get(PointerRNA *ptr)
307 {
308         PointerRNA ret = *ptr;
309         ret.type = &RNA_Test;
310
311         return ret;
312 }
313
314 #endif
315
316 #else
317
318 /* local convenience types */
319 typedef void (CollectionDefFunc)(struct BlenderRNA *brna, struct PropertyRNA *cprop);
320
321 typedef struct MainCollectionDef {
322         const char *identifier;
323         const char *type;
324         const char *iter_begin;
325         const char *name;
326         const char *description;
327         CollectionDefFunc *func;
328 } MainCollectionDef;
329
330 void RNA_def_main(BlenderRNA *brna)
331 {
332         StructRNA *srna;
333         PropertyRNA *prop;
334         CollectionDefFunc *func;
335
336         /* plural must match idtypes in readblenentry.c */
337         MainCollectionDef lists[] = {
338                 {"cameras", "Camera", "rna_Main_camera_begin", "Cameras", "Camera data-blocks", RNA_def_main_cameras},
339                 {"scenes", "Scene", "rna_Main_scene_begin", "Scenes", "Scene data-blocks", RNA_def_main_scenes},
340                 {"objects", "Object", "rna_Main_object_begin", "Objects", "Object data-blocks", RNA_def_main_objects},
341                 {"materials", "Material", "rna_Main_mat_begin", "Materials", "Material data-blocks", RNA_def_main_materials},
342                 {"node_groups", "NodeTree", "rna_Main_nodetree_begin", "Node Groups", "Node group data-blocks", RNA_def_main_node_groups},
343                 {"meshes", "Mesh", "rna_Main_mesh_begin", "Meshes", "Mesh data-blocks", RNA_def_main_meshes},
344                 {"lamps", "Lamp", "rna_Main_lamp_begin", "Lamps", "Lamp data-blocks", RNA_def_main_lamps},
345                 {"libraries", "Library", "rna_Main_library_begin", "Libraries", "Library data-blocks", RNA_def_main_libraries},
346                 {"screens", "Screen", "rna_Main_screen_begin", "Screens", "Screen data-blocks", RNA_def_main_screens},
347                 {"window_managers", "WindowManager", "rna_Main_wm_begin", "Window Managers", "Window manager data-blocks", RNA_def_main_window_managers},
348                 {"images", "Image", "rna_Main_image_begin", "Images", "Image data-blocks", RNA_def_main_images},
349                 {"lattices", "Lattice", "rna_Main_latt_begin", "Lattices", "Lattice data-blocks", RNA_def_main_lattices},
350                 {"curves", "Curve", "rna_Main_curve_begin", "Curves", "Curve data-blocks", RNA_def_main_curves},
351                 {"metaballs", "MetaBall", "rna_Main_mball_begin", "Metaballs", "Metaball data-blocks", RNA_def_main_metaballs},
352                 {"fonts", "VectorFont", "rna_Main_font_begin", "Vector Fonts", "Vector font data-blocks", RNA_def_main_fonts},
353                 {"textures", "Texture", "rna_Main_tex_begin", "Textures", "Texture data-blocks", RNA_def_main_textures},
354                 {"brushes", "Brush", "rna_Main_brush_begin", "Brushes", "Brush data-blocks", RNA_def_main_brushes},
355                 {"worlds", "World", "rna_Main_world_begin", "Worlds", "World data-blocks", RNA_def_main_worlds},
356                 {"groups", "Group", "rna_Main_group_begin", "Groups", "Group data-blocks", RNA_def_main_groups},
357                 {"shape_keys", "Key", "rna_Main_key_begin", "Shape Keys", "Shape Key data-blocks", NULL},
358                 {"texts", "Text", "rna_Main_text_begin", "Texts", "Text data-blocks", RNA_def_main_texts},
359                 {"speakers", "Speaker", "rna_Main_speaker_begin", "Speakers", "Speaker data-blocks", RNA_def_main_speakers},
360                 {"sounds", "Sound", "rna_Main_sound_begin", "Sounds", "Sound data-blocks", RNA_def_main_sounds},
361                 {"armatures", "Armature", "rna_Main_armature_begin", "Armatures", "Armature data-blocks", RNA_def_main_armatures},
362                 {"actions", "Action", "rna_Main_action_begin", "Actions", "Action data-blocks", RNA_def_main_actions},
363                 {"particles", "ParticleSettings", "rna_Main_particle_begin", "Particles", "Particle data-blocks", RNA_def_main_particles},
364                 {"palettes", "Palette", "rna_Main_palettes_begin", "Palettes", "Palette data-blocks", RNA_def_main_palettes},
365                 {"grease_pencil", "GreasePencil", "rna_Main_gpencil_begin", "Grease Pencil", "Grease Pencil data-blocks", RNA_def_main_gpencil},
366                 {"movieclips", "MovieClip", "rna_Main_movieclips_begin", "Movie Clips", "Movie Clip data-blocks", RNA_def_main_movieclips},
367                 {"masks", "Mask", "rna_Main_masks_begin", "Masks", "Masks data-blocks", RNA_def_main_masks},
368                 {"linestyles", "FreestyleLineStyle", "rna_Main_linestyle_begin", "Line Styles", "Line Style data-blocks", RNA_def_main_linestyles},
369                 {"cache_files", "CacheFile", "rna_Main_cachefiles_begin", "Cache Files", "Cache Files data-blocks", RNA_def_main_cachefiles},
370                 {"paint_curves", "PaintCurve", "rna_Main_paintcurves_begin", "Paint Curves", "Paint Curves data-blocks", RNA_def_main_paintcurves},
371                 {NULL, NULL, NULL, NULL, NULL, NULL}
372         };
373
374         int i;
375         
376         srna = RNA_def_struct(brna, "BlendData", NULL);
377         RNA_def_struct_ui_text(srna, "Blendfile Data",
378                                "Main data structure representing a .blend file and all its data-blocks");
379         RNA_def_struct_ui_icon(srna, ICON_BLENDER);
380
381         prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH);
382         RNA_def_property_string_maxlength(prop, FILE_MAX);
383         RNA_def_property_string_funcs(prop, "rna_Main_filepath_get", "rna_Main_filepath_length", "rna_Main_filepath_set");
384         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
385         RNA_def_property_ui_text(prop, "Filename", "Path to the .blend file");
386         
387         prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
388         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
389         RNA_def_property_boolean_funcs(prop, "rna_Main_is_dirty_get", NULL);
390         RNA_def_property_ui_text(prop, "File Has Unsaved Changes", "Have recent edits been saved to disk");
391
392         prop = RNA_def_property(srna, "is_saved", PROP_BOOLEAN, PROP_NONE);
393         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
394         RNA_def_property_boolean_funcs(prop, "rna_Main_is_saved_get", NULL);
395         RNA_def_property_ui_text(prop, "File is Saved", "Has the current session been saved to disk as a .blend file");
396
397         prop = RNA_def_property(srna, "use_autopack", PROP_BOOLEAN, PROP_NONE);
398         RNA_def_property_boolean_funcs(prop, "rna_Main_use_autopack_get", "rna_Main_use_autopack_set");
399         RNA_def_property_ui_text(prop, "Use Autopack", "Automatically pack all external data into .blend file");
400
401         prop = RNA_def_int_vector(srna, "version", 3, NULL, 0, INT_MAX,
402                            "Version", "Version of Blender the .blend was saved with", 0, INT_MAX);
403         RNA_def_property_int_funcs(prop, "rna_Main_version_get", NULL, NULL);
404         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
405         RNA_def_property_flag(prop, PROP_THICK_WRAP);
406
407         for (i = 0; lists[i].name; i++) {
408                 prop = RNA_def_property(srna, lists[i].identifier, PROP_COLLECTION, PROP_NONE);
409                 RNA_def_property_struct_type(prop, lists[i].type);
410                 RNA_def_property_collection_funcs(prop, lists[i].iter_begin, "rna_iterator_listbase_next",
411                                                   "rna_iterator_listbase_end", "rna_iterator_listbase_get",
412                                                   NULL, NULL, NULL, NULL);
413                 RNA_def_property_ui_text(prop, lists[i].name, lists[i].description);
414
415                 /* collection functions */
416                 func = lists[i].func;
417                 if (func)
418                         func(brna, prop);
419         }
420
421         RNA_api_main(srna);
422
423 #ifdef UNIT_TEST
424
425         RNA_define_verify_sdna(0);
426
427         prop = RNA_def_property(srna, "test", PROP_POINTER, PROP_NONE);
428         RNA_def_property_struct_type(prop, "Test");
429         RNA_def_property_pointer_funcs(prop, "rna_Test_test_get", NULL, NULL, NULL);
430
431         RNA_define_verify_sdna(1);
432
433 #endif
434 }
435
436 #endif