Cleanup: use macro for listbase collection begin functions
[blender.git] / source / blender / makesrna / intern / rna_main.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup RNA
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "BLI_utildefines.h"
25 #include "BLI_path_util.h"
26
27 #include "RNA_define.h"
28 #include "RNA_access.h"
29
30 #include "rna_internal.h"
31
32 #ifdef RNA_RUNTIME
33
34 #include "BKE_main.h"
35 #include "BKE_mesh.h"
36 #include "BKE_global.h"
37
38 /* all the list begin functions are added manually here, Main is not in SDNA */
39
40 static bool rna_Main_use_autopack_get(PointerRNA *UNUSED(ptr))
41 {
42         if (G.fileflags & G_FILE_AUTOPACK)
43                 return 1;
44
45         return 0;
46 }
47
48 static void rna_Main_use_autopack_set(PointerRNA *UNUSED(ptr), bool value)
49 {
50         if (value)
51                 G.fileflags |= G_FILE_AUTOPACK;
52         else
53                 G.fileflags &= ~G_FILE_AUTOPACK;
54 }
55
56 static bool rna_Main_is_saved_get(PointerRNA *UNUSED(ptr))
57 {
58         return G.relbase_valid;
59 }
60
61 static bool rna_Main_is_dirty_get(PointerRNA *ptr)
62 {
63         /* XXX, not totally nice to do it this way, should store in main ? */
64         Main *bmain = (Main *)ptr->data;
65         wmWindowManager *wm;
66         if ((wm = bmain->wm.first)) {
67                 return !wm->file_saved;
68         }
69
70         return true;
71 }
72
73 static void rna_Main_filepath_get(PointerRNA *ptr, char *value)
74 {
75         Main *bmain = (Main *)ptr->data;
76         BLI_strncpy(value, bmain->name, sizeof(bmain->name));
77 }
78
79 static int rna_Main_filepath_length(PointerRNA *ptr)
80 {
81         Main *bmain = (Main *)ptr->data;
82         return strlen(bmain->name);
83 }
84
85 #if 0
86 static void rna_Main_filepath_set(PointerRNA *ptr, const char *value)
87 {
88         Main *bmain = (Main *)ptr->data;
89         BLI_strncpy(bmain->name, value, sizeof(bmain->name));
90 }
91 #endif
92
93 #define RNA_MAIN_LISTBASE_FUNCS_DEF(_listbase_name) \
94         static void rna_Main_##_listbase_name##_begin(CollectionPropertyIterator *iter, PointerRNA *ptr) \
95         { \
96                 rna_iterator_listbase_begin(iter, &((Main *)ptr->data)->_listbase_name, NULL); \
97         }
98
99 RNA_MAIN_LISTBASE_FUNCS_DEF(actions)
100 RNA_MAIN_LISTBASE_FUNCS_DEF(armatures)
101 RNA_MAIN_LISTBASE_FUNCS_DEF(brushes)
102 RNA_MAIN_LISTBASE_FUNCS_DEF(cachefiles)
103 RNA_MAIN_LISTBASE_FUNCS_DEF(cameras)
104 RNA_MAIN_LISTBASE_FUNCS_DEF(collections)
105 RNA_MAIN_LISTBASE_FUNCS_DEF(curves)
106 RNA_MAIN_LISTBASE_FUNCS_DEF(fonts)
107 RNA_MAIN_LISTBASE_FUNCS_DEF(gpencils)
108 RNA_MAIN_LISTBASE_FUNCS_DEF(images)
109 RNA_MAIN_LISTBASE_FUNCS_DEF(lattices)
110 RNA_MAIN_LISTBASE_FUNCS_DEF(libraries)
111 RNA_MAIN_LISTBASE_FUNCS_DEF(lightprobes)
112 RNA_MAIN_LISTBASE_FUNCS_DEF(lights)
113 RNA_MAIN_LISTBASE_FUNCS_DEF(linestyles)
114 RNA_MAIN_LISTBASE_FUNCS_DEF(masks)
115 RNA_MAIN_LISTBASE_FUNCS_DEF(materials)
116 RNA_MAIN_LISTBASE_FUNCS_DEF(meshes)
117 RNA_MAIN_LISTBASE_FUNCS_DEF(metaballs)
118 RNA_MAIN_LISTBASE_FUNCS_DEF(movieclips)
119 RNA_MAIN_LISTBASE_FUNCS_DEF(nodetrees)
120 RNA_MAIN_LISTBASE_FUNCS_DEF(objects)
121 RNA_MAIN_LISTBASE_FUNCS_DEF(paintcurves)
122 RNA_MAIN_LISTBASE_FUNCS_DEF(palettes)
123 RNA_MAIN_LISTBASE_FUNCS_DEF(particles)
124 RNA_MAIN_LISTBASE_FUNCS_DEF(scenes)
125 RNA_MAIN_LISTBASE_FUNCS_DEF(screens)
126 RNA_MAIN_LISTBASE_FUNCS_DEF(shapekeys)
127 RNA_MAIN_LISTBASE_FUNCS_DEF(sounds)
128 RNA_MAIN_LISTBASE_FUNCS_DEF(speakers)
129 RNA_MAIN_LISTBASE_FUNCS_DEF(texts)
130 RNA_MAIN_LISTBASE_FUNCS_DEF(textures)
131 RNA_MAIN_LISTBASE_FUNCS_DEF(wm)
132 RNA_MAIN_LISTBASE_FUNCS_DEF(workspaces)
133 RNA_MAIN_LISTBASE_FUNCS_DEF(worlds)
134
135 #undef RNA_MAIN_LISTBASE_FUNCS_DEF
136
137 static void rna_Main_version_get(PointerRNA *ptr, int *value)
138 {
139         Main *bmain = (Main *)ptr->data;
140         value[0] = bmain->versionfile / 100;
141         value[1] = bmain->versionfile % 100;
142         value[2] = bmain->subversionfile;
143 }
144
145 #ifdef UNIT_TEST
146
147 static PointerRNA rna_Test_test_get(PointerRNA *ptr)
148 {
149         PointerRNA ret = *ptr;
150         ret.type = &RNA_Test;
151
152         return ret;
153 }
154
155 #endif
156
157 #else
158
159 /* local convenience types */
160 typedef void (CollectionDefFunc)(struct BlenderRNA *brna, struct PropertyRNA *cprop);
161
162 typedef struct MainCollectionDef {
163         const char *identifier;
164         const char *type;
165         const char *iter_begin;
166         const char *name;
167         const char *description;
168         CollectionDefFunc *func;
169 } MainCollectionDef;
170
171 void RNA_def_main(BlenderRNA *brna)
172 {
173         StructRNA *srna;
174         PropertyRNA *prop;
175         CollectionDefFunc *func;
176
177         /* plural must match idtypes in readblenentry.c */
178         MainCollectionDef lists[] = {
179                 {"cameras", "Camera", "rna_Main_cameras_begin", "Cameras", "Camera data-blocks", RNA_def_main_cameras},
180                 {"scenes", "Scene", "rna_Main_scenes_begin", "Scenes", "Scene data-blocks", RNA_def_main_scenes},
181                 {"objects", "Object", "rna_Main_objects_begin", "Objects", "Object data-blocks", RNA_def_main_objects},
182                 {"materials", "Material", "rna_Main_materials_begin", "Materials", "Material data-blocks", RNA_def_main_materials},
183                 {"node_groups", "NodeTree", "rna_Main_nodetrees_begin", "Node Groups", "Node group data-blocks", RNA_def_main_node_groups},
184                 {"meshes", "Mesh", "rna_Main_meshes_begin", "Meshes", "Mesh data-blocks", RNA_def_main_meshes},
185                 {"lights", "Light", "rna_Main_lights_begin", "Lights", "Light data-blocks", RNA_def_main_lights},
186                 {"libraries", "Library", "rna_Main_libraries_begin", "Libraries", "Library data-blocks", RNA_def_main_libraries},
187                 {"screens", "Screen", "rna_Main_screens_begin", "Screens", "Screen data-blocks", RNA_def_main_screens},
188                 {"window_managers", "WindowManager", "rna_Main_wm_begin", "Window Managers", "Window manager data-blocks", RNA_def_main_window_managers},
189                 {"images", "Image", "rna_Main_images_begin", "Images", "Image data-blocks", RNA_def_main_images},
190                 {"lattices", "Lattice", "rna_Main_lattices_begin", "Lattices", "Lattice data-blocks", RNA_def_main_lattices},
191                 {"curves", "Curve", "rna_Main_curves_begin", "Curves", "Curve data-blocks", RNA_def_main_curves},
192                 {"metaballs", "MetaBall", "rna_Main_metaballs_begin", "Metaballs", "Metaball data-blocks", RNA_def_main_metaballs},
193                 {"fonts", "VectorFont", "rna_Main_fonts_begin", "Vector Fonts", "Vector font data-blocks", RNA_def_main_fonts},
194                 {"textures", "Texture", "rna_Main_textures_begin", "Textures", "Texture data-blocks", RNA_def_main_textures},
195                 {"brushes", "Brush", "rna_Main_brushes_begin", "Brushes", "Brush data-blocks", RNA_def_main_brushes},
196                 {"worlds", "World", "rna_Main_worlds_begin", "Worlds", "World data-blocks", RNA_def_main_worlds},
197                 {"collections", "Collection", "rna_Main_collections_begin", "Collections", "Collection data-blocks", RNA_def_main_collections},
198                 {"shape_keys", "Key", "rna_Main_shapekeys_begin", "Shape Keys", "Shape Key data-blocks", NULL},
199                 {"texts", "Text", "rna_Main_texts_begin", "Texts", "Text data-blocks", RNA_def_main_texts},
200                 {"speakers", "Speaker", "rna_Main_speakers_begin", "Speakers", "Speaker data-blocks", RNA_def_main_speakers},
201                 {"sounds", "Sound", "rna_Main_sounds_begin", "Sounds", "Sound data-blocks", RNA_def_main_sounds},
202                 {"armatures", "Armature", "rna_Main_armatures_begin", "Armatures", "Armature data-blocks", RNA_def_main_armatures},
203                 {"actions", "Action", "rna_Main_actions_begin", "Actions", "Action data-blocks", RNA_def_main_actions},
204                 {"particles", "ParticleSettings", "rna_Main_particles_begin", "Particles", "Particle data-blocks", RNA_def_main_particles},
205                 {"palettes", "Palette", "rna_Main_palettes_begin", "Palettes", "Palette data-blocks", RNA_def_main_palettes},
206                 {"grease_pencils", "GreasePencil", "rna_Main_gpencils_begin", "Grease Pencil", "Grease Pencil data-blocks", RNA_def_main_gpencil},
207                 {"movieclips", "MovieClip", "rna_Main_movieclips_begin", "Movie Clips", "Movie Clip data-blocks", RNA_def_main_movieclips},
208                 {"masks", "Mask", "rna_Main_masks_begin", "Masks", "Masks data-blocks", RNA_def_main_masks},
209                 {"linestyles", "FreestyleLineStyle", "rna_Main_linestyles_begin", "Line Styles", "Line Style data-blocks", RNA_def_main_linestyles},
210                 {"cache_files", "CacheFile", "rna_Main_cachefiles_begin", "Cache Files", "Cache Files data-blocks", RNA_def_main_cachefiles},
211                 {"paint_curves", "PaintCurve", "rna_Main_paintcurves_begin", "Paint Curves", "Paint Curves data-blocks", RNA_def_main_paintcurves},
212                 {"workspaces", "WorkSpace", "rna_Main_workspaces_begin", "Workspaces", "Workspace data-blocks", RNA_def_main_workspaces},
213                 {"lightprobes", "LightProbe", "rna_Main_lightprobes_begin", "LightProbes", "LightProbe data-blocks", RNA_def_main_lightprobes},
214                 {NULL, NULL, NULL, NULL, NULL, NULL},
215         };
216
217         int i;
218
219         srna = RNA_def_struct(brna, "BlendData", NULL);
220         RNA_def_struct_ui_text(srna, "Blendfile Data",
221                                "Main data structure representing a .blend file and all its data-blocks");
222         RNA_def_struct_ui_icon(srna, ICON_BLENDER);
223
224         prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH);
225         RNA_def_property_string_maxlength(prop, FILE_MAX);
226         RNA_def_property_string_funcs(prop, "rna_Main_filepath_get", "rna_Main_filepath_length", "rna_Main_filepath_set");
227         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
228         RNA_def_property_ui_text(prop, "Filename", "Path to the .blend file");
229
230         prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
231         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
232         RNA_def_property_boolean_funcs(prop, "rna_Main_is_dirty_get", NULL);
233         RNA_def_property_ui_text(prop, "File Has Unsaved Changes", "Have recent edits been saved to disk");
234
235         prop = RNA_def_property(srna, "is_saved", PROP_BOOLEAN, PROP_NONE);
236         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
237         RNA_def_property_boolean_funcs(prop, "rna_Main_is_saved_get", NULL);
238         RNA_def_property_ui_text(prop, "File is Saved", "Has the current session been saved to disk as a .blend file");
239
240         prop = RNA_def_property(srna, "use_autopack", PROP_BOOLEAN, PROP_NONE);
241         RNA_def_property_boolean_funcs(prop, "rna_Main_use_autopack_get", "rna_Main_use_autopack_set");
242         RNA_def_property_ui_text(prop, "Use Autopack", "Automatically pack all external data into .blend file");
243
244         prop = RNA_def_int_vector(srna, "version", 3, NULL, 0, INT_MAX,
245                            "Version", "Version of Blender the .blend was saved with", 0, INT_MAX);
246         RNA_def_property_int_funcs(prop, "rna_Main_version_get", NULL, NULL);
247         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
248         RNA_def_property_flag(prop, PROP_THICK_WRAP);
249
250         for (i = 0; lists[i].name; i++) {
251                 prop = RNA_def_property(srna, lists[i].identifier, PROP_COLLECTION, PROP_NONE);
252                 RNA_def_property_struct_type(prop, lists[i].type);
253                 RNA_def_property_collection_funcs(prop, lists[i].iter_begin, "rna_iterator_listbase_next",
254                                                   "rna_iterator_listbase_end", "rna_iterator_listbase_get",
255                                                   NULL, NULL, NULL, NULL);
256                 RNA_def_property_ui_text(prop, lists[i].name, lists[i].description);
257
258                 /* collection functions */
259                 func = lists[i].func;
260                 if (func)
261                         func(brna, prop);
262         }
263
264         RNA_api_main(srna);
265
266 #ifdef UNIT_TEST
267
268         RNA_define_verify_sdna(0);
269
270         prop = RNA_def_property(srna, "test", PROP_POINTER, PROP_NONE);
271         RNA_def_property_struct_type(prop, "Test");
272         RNA_def_property_pointer_funcs(prop, "rna_Test_test_get", NULL, NULL, NULL);
273
274         RNA_define_verify_sdna(1);
275
276 #endif
277 }
278
279 #endif