Fix T75088: Add tooltips for custom properties
[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_path_util.h"
25 #include "BLI_utildefines.h"
26
27 #include "RNA_access.h"
28 #include "RNA_define.h"
29
30 #include "rna_internal.h"
31
32 #ifdef RNA_RUNTIME
33
34 #  include "BKE_global.h"
35 #  include "BKE_main.h"
36 #  include "BKE_mesh.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
46   return 0;
47 }
48
49 static void rna_Main_use_autopack_set(PointerRNA *UNUSED(ptr), bool value)
50 {
51   if (value) {
52     G.fileflags |= G_FILE_AUTOPACK;
53   }
54   else {
55     G.fileflags &= ~G_FILE_AUTOPACK;
56   }
57 }
58
59 static bool rna_Main_is_saved_get(PointerRNA *UNUSED(ptr))
60 {
61   return G.relbase_valid;
62 }
63
64 static bool rna_Main_is_dirty_get(PointerRNA *ptr)
65 {
66   /* XXX, not totally nice to do it this way, should store in main ? */
67   Main *bmain = (Main *)ptr->data;
68   wmWindowManager *wm;
69   if ((wm = bmain->wm.first)) {
70     return !wm->file_saved;
71   }
72
73   return true;
74 }
75
76 static void rna_Main_filepath_get(PointerRNA *ptr, char *value)
77 {
78   Main *bmain = (Main *)ptr->data;
79   BLI_strncpy(value, bmain->name, sizeof(bmain->name));
80 }
81
82 static int rna_Main_filepath_length(PointerRNA *ptr)
83 {
84   Main *bmain = (Main *)ptr->data;
85   return strlen(bmain->name);
86 }
87
88 #  if 0
89 static void rna_Main_filepath_set(PointerRNA *ptr, const char *value)
90 {
91   Main *bmain = (Main *)ptr->data;
92   BLI_strncpy(bmain->name, value, sizeof(bmain->name));
93 }
94 #  endif
95
96 #  define RNA_MAIN_LISTBASE_FUNCS_DEF(_listbase_name) \
97     static void rna_Main_##_listbase_name##_begin(CollectionPropertyIterator *iter, \
98                                                   PointerRNA *ptr) \
99     { \
100       rna_iterator_listbase_begin(iter, &((Main *)ptr->data)->_listbase_name, NULL); \
101     }
102
103 RNA_MAIN_LISTBASE_FUNCS_DEF(actions)
104 RNA_MAIN_LISTBASE_FUNCS_DEF(armatures)
105 RNA_MAIN_LISTBASE_FUNCS_DEF(brushes)
106 RNA_MAIN_LISTBASE_FUNCS_DEF(cachefiles)
107 RNA_MAIN_LISTBASE_FUNCS_DEF(cameras)
108 RNA_MAIN_LISTBASE_FUNCS_DEF(collections)
109 RNA_MAIN_LISTBASE_FUNCS_DEF(curves)
110 RNA_MAIN_LISTBASE_FUNCS_DEF(fonts)
111 RNA_MAIN_LISTBASE_FUNCS_DEF(gpencils)
112 #  ifdef WITH_NEW_OBJECT_TYPES
113 RNA_MAIN_LISTBASE_FUNCS_DEF(hairs)
114 #  endif
115 RNA_MAIN_LISTBASE_FUNCS_DEF(images)
116 RNA_MAIN_LISTBASE_FUNCS_DEF(lattices)
117 RNA_MAIN_LISTBASE_FUNCS_DEF(libraries)
118 RNA_MAIN_LISTBASE_FUNCS_DEF(lightprobes)
119 RNA_MAIN_LISTBASE_FUNCS_DEF(lights)
120 RNA_MAIN_LISTBASE_FUNCS_DEF(linestyles)
121 RNA_MAIN_LISTBASE_FUNCS_DEF(masks)
122 RNA_MAIN_LISTBASE_FUNCS_DEF(materials)
123 RNA_MAIN_LISTBASE_FUNCS_DEF(meshes)
124 RNA_MAIN_LISTBASE_FUNCS_DEF(metaballs)
125 RNA_MAIN_LISTBASE_FUNCS_DEF(movieclips)
126 RNA_MAIN_LISTBASE_FUNCS_DEF(nodetrees)
127 RNA_MAIN_LISTBASE_FUNCS_DEF(objects)
128 RNA_MAIN_LISTBASE_FUNCS_DEF(paintcurves)
129 RNA_MAIN_LISTBASE_FUNCS_DEF(palettes)
130 RNA_MAIN_LISTBASE_FUNCS_DEF(particles)
131 #  ifdef WITH_NEW_OBJECT_TYPES
132 RNA_MAIN_LISTBASE_FUNCS_DEF(pointclouds)
133 #  endif
134 RNA_MAIN_LISTBASE_FUNCS_DEF(scenes)
135 RNA_MAIN_LISTBASE_FUNCS_DEF(screens)
136 RNA_MAIN_LISTBASE_FUNCS_DEF(shapekeys)
137 RNA_MAIN_LISTBASE_FUNCS_DEF(sounds)
138 RNA_MAIN_LISTBASE_FUNCS_DEF(speakers)
139 RNA_MAIN_LISTBASE_FUNCS_DEF(texts)
140 RNA_MAIN_LISTBASE_FUNCS_DEF(textures)
141 RNA_MAIN_LISTBASE_FUNCS_DEF(volumes)
142 RNA_MAIN_LISTBASE_FUNCS_DEF(wm)
143 RNA_MAIN_LISTBASE_FUNCS_DEF(workspaces)
144 RNA_MAIN_LISTBASE_FUNCS_DEF(worlds)
145
146 #  undef RNA_MAIN_LISTBASE_FUNCS_DEF
147
148 static void rna_Main_version_get(PointerRNA *ptr, int *value)
149 {
150   Main *bmain = (Main *)ptr->data;
151   value[0] = bmain->versionfile / 100;
152   value[1] = bmain->versionfile % 100;
153   value[2] = bmain->subversionfile;
154 }
155
156 #  ifdef UNIT_TEST
157
158 static PointerRNA rna_Test_test_get(PointerRNA *ptr)
159 {
160   PointerRNA ret = *ptr;
161   ret.type = &RNA_Test;
162
163   return ret;
164 }
165
166 #  endif
167
168 #else
169
170 /* local convenience types */
171 typedef void(CollectionDefFunc)(struct BlenderRNA *brna, struct PropertyRNA *cprop);
172
173 typedef struct MainCollectionDef {
174   const char *identifier;
175   const char *type;
176   const char *iter_begin;
177   const char *name;
178   const char *description;
179   CollectionDefFunc *func;
180 } MainCollectionDef;
181
182 void RNA_def_main(BlenderRNA *brna)
183 {
184   StructRNA *srna;
185   PropertyRNA *prop;
186   CollectionDefFunc *func;
187
188   /* plural must match idtypes in readblenentry.c */
189   MainCollectionDef lists[] = {
190       {"cameras",
191        "Camera",
192        "rna_Main_cameras_begin",
193        "Cameras",
194        "Camera data-blocks",
195        RNA_def_main_cameras},
196       {"scenes",
197        "Scene",
198        "rna_Main_scenes_begin",
199        "Scenes",
200        "Scene data-blocks",
201        RNA_def_main_scenes},
202       {"objects",
203        "Object",
204        "rna_Main_objects_begin",
205        "Objects",
206        "Object data-blocks",
207        RNA_def_main_objects},
208       {"materials",
209        "Material",
210        "rna_Main_materials_begin",
211        "Materials",
212        "Material data-blocks",
213        RNA_def_main_materials},
214       {"node_groups",
215        "NodeTree",
216        "rna_Main_nodetrees_begin",
217        "Node Groups",
218        "Node group data-blocks",
219        RNA_def_main_node_groups},
220       {"meshes",
221        "Mesh",
222        "rna_Main_meshes_begin",
223        "Meshes",
224        "Mesh data-blocks",
225        RNA_def_main_meshes},
226       {"lights",
227        "Light",
228        "rna_Main_lights_begin",
229        "Lights",
230        "Light data-blocks",
231        RNA_def_main_lights},
232       {"libraries",
233        "Library",
234        "rna_Main_libraries_begin",
235        "Libraries",
236        "Library data-blocks",
237        RNA_def_main_libraries},
238       {"screens",
239        "Screen",
240        "rna_Main_screens_begin",
241        "Screens",
242        "Screen data-blocks",
243        RNA_def_main_screens},
244       {"window_managers",
245        "WindowManager",
246        "rna_Main_wm_begin",
247        "Window Managers",
248        "Window manager data-blocks",
249        RNA_def_main_window_managers},
250       {"images",
251        "Image",
252        "rna_Main_images_begin",
253        "Images",
254        "Image data-blocks",
255        RNA_def_main_images},
256       {"lattices",
257        "Lattice",
258        "rna_Main_lattices_begin",
259        "Lattices",
260        "Lattice data-blocks",
261        RNA_def_main_lattices},
262       {"curves",
263        "Curve",
264        "rna_Main_curves_begin",
265        "Curves",
266        "Curve data-blocks",
267        RNA_def_main_curves},
268       {"metaballs",
269        "MetaBall",
270        "rna_Main_metaballs_begin",
271        "Metaballs",
272        "Metaball data-blocks",
273        RNA_def_main_metaballs},
274       {"fonts",
275        "VectorFont",
276        "rna_Main_fonts_begin",
277        "Vector Fonts",
278        "Vector font data-blocks",
279        RNA_def_main_fonts},
280       {"textures",
281        "Texture",
282        "rna_Main_textures_begin",
283        "Textures",
284        "Texture data-blocks",
285        RNA_def_main_textures},
286       {"brushes",
287        "Brush",
288        "rna_Main_brushes_begin",
289        "Brushes",
290        "Brush data-blocks",
291        RNA_def_main_brushes},
292       {"worlds",
293        "World",
294        "rna_Main_worlds_begin",
295        "Worlds",
296        "World data-blocks",
297        RNA_def_main_worlds},
298       {"collections",
299        "Collection",
300        "rna_Main_collections_begin",
301        "Collections",
302        "Collection data-blocks",
303        RNA_def_main_collections},
304       {"shape_keys",
305        "Key",
306        "rna_Main_shapekeys_begin",
307        "Shape Keys",
308        "Shape Key data-blocks",
309        NULL},
310       {"texts", "Text", "rna_Main_texts_begin", "Texts", "Text data-blocks", RNA_def_main_texts},
311       {"speakers",
312        "Speaker",
313        "rna_Main_speakers_begin",
314        "Speakers",
315        "Speaker data-blocks",
316        RNA_def_main_speakers},
317       {"sounds",
318        "Sound",
319        "rna_Main_sounds_begin",
320        "Sounds",
321        "Sound data-blocks",
322        RNA_def_main_sounds},
323       {"armatures",
324        "Armature",
325        "rna_Main_armatures_begin",
326        "Armatures",
327        "Armature data-blocks",
328        RNA_def_main_armatures},
329       {"actions",
330        "Action",
331        "rna_Main_actions_begin",
332        "Actions",
333        "Action data-blocks",
334        RNA_def_main_actions},
335       {"particles",
336        "ParticleSettings",
337        "rna_Main_particles_begin",
338        "Particles",
339        "Particle data-blocks",
340        RNA_def_main_particles},
341       {"palettes",
342        "Palette",
343        "rna_Main_palettes_begin",
344        "Palettes",
345        "Palette data-blocks",
346        RNA_def_main_palettes},
347       {"grease_pencils",
348        "GreasePencil",
349        "rna_Main_gpencils_begin",
350        "Grease Pencil",
351        "Grease Pencil data-blocks",
352        RNA_def_main_gpencil},
353       {"movieclips",
354        "MovieClip",
355        "rna_Main_movieclips_begin",
356        "Movie Clips",
357        "Movie Clip data-blocks",
358        RNA_def_main_movieclips},
359       {"masks", "Mask", "rna_Main_masks_begin", "Masks", "Masks data-blocks", RNA_def_main_masks},
360       {"linestyles",
361        "FreestyleLineStyle",
362        "rna_Main_linestyles_begin",
363        "Line Styles",
364        "Line Style data-blocks",
365        RNA_def_main_linestyles},
366       {"cache_files",
367        "CacheFile",
368        "rna_Main_cachefiles_begin",
369        "Cache Files",
370        "Cache Files data-blocks",
371        RNA_def_main_cachefiles},
372       {"paint_curves",
373        "PaintCurve",
374        "rna_Main_paintcurves_begin",
375        "Paint Curves",
376        "Paint Curves data-blocks",
377        RNA_def_main_paintcurves},
378       {"workspaces",
379        "WorkSpace",
380        "rna_Main_workspaces_begin",
381        "Workspaces",
382        "Workspace data-blocks",
383        RNA_def_main_workspaces},
384       {"lightprobes",
385        "LightProbe",
386        "rna_Main_lightprobes_begin",
387        "LightProbes",
388        "LightProbe data-blocks",
389        RNA_def_main_lightprobes},
390 #  ifdef WITH_NEW_OBJECT_TYPES
391       {"hairs", "Hair", "rna_Main_hairs_begin", "Hairs", "Hair data-blocks", RNA_def_main_hairs},
392       {"pointclouds",
393        "PointCloud",
394        "rna_Main_pointclouds_begin",
395        "Point Clouds",
396        "Point cloud data-blocks",
397        RNA_def_main_pointclouds},
398 #  endif
399       {"volumes",
400        "Volume",
401        "rna_Main_volumes_begin",
402        "Volumes",
403        "Volume data-blocks",
404        RNA_def_main_volumes},
405       {NULL, NULL, NULL, NULL, NULL, NULL},
406   };
407
408   int i;
409
410   srna = RNA_def_struct(brna, "BlendData", NULL);
411   RNA_def_struct_ui_text(srna,
412                          "Blendfile Data",
413                          "Main data structure representing a .blend file and all its data-blocks");
414   RNA_def_struct_ui_icon(srna, ICON_BLENDER);
415
416   prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH);
417   RNA_def_property_string_maxlength(prop, FILE_MAX);
418   RNA_def_property_string_funcs(
419       prop, "rna_Main_filepath_get", "rna_Main_filepath_length", "rna_Main_filepath_set");
420   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
421   RNA_def_property_ui_text(prop, "Filename", "Path to the .blend file");
422
423   prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
424   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
425   RNA_def_property_boolean_funcs(prop, "rna_Main_is_dirty_get", NULL);
426   RNA_def_property_ui_text(
427       prop, "File Has Unsaved Changes", "Have recent edits been saved to disk");
428
429   prop = RNA_def_property(srna, "is_saved", PROP_BOOLEAN, PROP_NONE);
430   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
431   RNA_def_property_boolean_funcs(prop, "rna_Main_is_saved_get", NULL);
432   RNA_def_property_ui_text(
433       prop, "File is Saved", "Has the current session been saved to disk as a .blend file");
434
435   prop = RNA_def_property(srna, "use_autopack", PROP_BOOLEAN, PROP_NONE);
436   RNA_def_property_boolean_funcs(prop, "rna_Main_use_autopack_get", "rna_Main_use_autopack_set");
437   RNA_def_property_ui_text(
438       prop, "Use Autopack", "Automatically pack all external data into .blend file");
439
440   prop = RNA_def_int_vector(srna,
441                             "version",
442                             3,
443                             NULL,
444                             0,
445                             INT_MAX,
446                             "Version",
447                             "Version of Blender the .blend was saved with",
448                             0,
449                             INT_MAX);
450   RNA_def_property_int_funcs(prop, "rna_Main_version_get", NULL, NULL);
451   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
452   RNA_def_property_flag(prop, PROP_THICK_WRAP);
453
454   for (i = 0; lists[i].name; i++) {
455     prop = RNA_def_property(srna, lists[i].identifier, PROP_COLLECTION, PROP_NONE);
456     RNA_def_property_struct_type(prop, lists[i].type);
457     RNA_def_property_collection_funcs(prop,
458                                       lists[i].iter_begin,
459                                       "rna_iterator_listbase_next",
460                                       "rna_iterator_listbase_end",
461                                       "rna_iterator_listbase_get",
462                                       NULL,
463                                       NULL,
464                                       NULL,
465                                       NULL);
466     RNA_def_property_ui_text(prop, lists[i].name, lists[i].description);
467
468     /* collection functions */
469     func = lists[i].func;
470     if (func) {
471       func(brna, prop);
472     }
473   }
474
475   RNA_api_main(srna);
476
477 #  ifdef UNIT_TEST
478
479   RNA_define_verify_sdna(0);
480
481   prop = RNA_def_property(srna, "test", PROP_POINTER, PROP_NONE);
482   RNA_def_property_struct_type(prop, "Test");
483   RNA_def_property_pointer_funcs(prop, "rna_Test_test_get", NULL, NULL, NULL);
484
485   RNA_define_verify_sdna(1);
486
487 #  endif
488 }
489
490 #endif