Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / blenkernel / intern / idcode.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  * return info about ID types
19  */
20
21 /** \file \ingroup bke
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "DNA_ID.h"
28
29 #include "BLI_utildefines.h"
30
31 #include "BLT_translation.h"
32
33 #include "BKE_main.h"
34 #include "BKE_idcode.h"
35
36 typedef struct {
37         unsigned short code;
38         const char *name, *plural;
39
40         const char *i18n_context;
41
42         int flags;
43 #define IDTYPE_FLAGS_ISLINKABLE (1 << 0)
44 } IDType;
45
46 /* plural need to match rna_main.c's MainCollectionDef */
47 /* WARNING! Keep it in sync with i18n contexts in BLT_translation.h */
48 static IDType idtypes[] = {
49         /** ID's directly below must all be in #Main, and be kept in sync with #MAX_LIBARRAY (membership, not order) */
50         {ID_AC,   "Action",             "actions",         BLT_I18NCONTEXT_ID_ACTION,             IDTYPE_FLAGS_ISLINKABLE},
51         {ID_AR,   "Armature",           "armatures",       BLT_I18NCONTEXT_ID_ARMATURE,           IDTYPE_FLAGS_ISLINKABLE},
52         {ID_BR,   "Brush",              "brushes",         BLT_I18NCONTEXT_ID_BRUSH,              IDTYPE_FLAGS_ISLINKABLE},
53         {ID_CA,   "Camera",             "cameras",         BLT_I18NCONTEXT_ID_CAMERA,             IDTYPE_FLAGS_ISLINKABLE},
54         {ID_CF,   "CacheFile",          "cache_files",     BLT_I18NCONTEXT_ID_CACHEFILE,          IDTYPE_FLAGS_ISLINKABLE},
55         {ID_GR,   "Collection",         "collections",     BLT_I18NCONTEXT_ID_COLLECTION,         IDTYPE_FLAGS_ISLINKABLE},
56         {ID_CU,   "Curve",              "curves",          BLT_I18NCONTEXT_ID_CURVE,              IDTYPE_FLAGS_ISLINKABLE},
57         {ID_GD,   "GPencil",            "grease_pencil",   BLT_I18NCONTEXT_ID_GPENCIL,            IDTYPE_FLAGS_ISLINKABLE}, /* rename gpencil */
58         {ID_IM,   "Image",              "images",          BLT_I18NCONTEXT_ID_IMAGE,              IDTYPE_FLAGS_ISLINKABLE},
59         {ID_IP,   "Ipo",                "ipos",            "",                                    IDTYPE_FLAGS_ISLINKABLE}, /* deprecated */
60         {ID_KE,   "Key",                "shape_keys",      BLT_I18NCONTEXT_ID_SHAPEKEY,           0                      },
61         {ID_LA,   "Light",              "lights",          BLT_I18NCONTEXT_ID_LAMP,               IDTYPE_FLAGS_ISLINKABLE},
62         {ID_LI,   "Library",            "libraries",       BLT_I18NCONTEXT_ID_LIBRARY,            0                      },
63         {ID_LS,   "FreestyleLineStyle", "linestyles",      BLT_I18NCONTEXT_ID_FREESTYLELINESTYLE, IDTYPE_FLAGS_ISLINKABLE},
64         {ID_LT,   "Lattice",            "lattices",        BLT_I18NCONTEXT_ID_LATTICE,            IDTYPE_FLAGS_ISLINKABLE},
65         {ID_MA,   "Material",           "materials",       BLT_I18NCONTEXT_ID_MATERIAL,           IDTYPE_FLAGS_ISLINKABLE},
66         {ID_MB,   "Metaball",           "metaballs",       BLT_I18NCONTEXT_ID_METABALL,           IDTYPE_FLAGS_ISLINKABLE},
67         {ID_MC,   "MovieClip",          "movieclips",      BLT_I18NCONTEXT_ID_MOVIECLIP,          IDTYPE_FLAGS_ISLINKABLE},
68         {ID_ME,   "Mesh",               "meshes",          BLT_I18NCONTEXT_ID_MESH,               IDTYPE_FLAGS_ISLINKABLE},
69         {ID_MSK,  "Mask",               "masks",           BLT_I18NCONTEXT_ID_MASK,               IDTYPE_FLAGS_ISLINKABLE},
70         {ID_NT,   "NodeTree",           "node_groups",     BLT_I18NCONTEXT_ID_NODETREE,           IDTYPE_FLAGS_ISLINKABLE},
71         {ID_OB,   "Object",             "objects",         BLT_I18NCONTEXT_ID_OBJECT,             IDTYPE_FLAGS_ISLINKABLE},
72         {ID_PA,   "ParticleSettings",   "particles",       BLT_I18NCONTEXT_ID_PARTICLESETTINGS,   IDTYPE_FLAGS_ISLINKABLE},
73         {ID_PAL,  "Palettes",           "palettes",        BLT_I18NCONTEXT_ID_PALETTE,            IDTYPE_FLAGS_ISLINKABLE},
74         {ID_PC,   "PaintCurve",         "paint_curves",    BLT_I18NCONTEXT_ID_PAINTCURVE,         IDTYPE_FLAGS_ISLINKABLE},
75         {ID_LP,   "LightProbe",         "light_probes",    BLT_I18NCONTEXT_ID_LIGHTPROBE,         IDTYPE_FLAGS_ISLINKABLE},
76         {ID_SCE,  "Scene",              "scenes",          BLT_I18NCONTEXT_ID_SCENE,              IDTYPE_FLAGS_ISLINKABLE},
77         {ID_SCR,  "Screen",             "screens",         BLT_I18NCONTEXT_ID_SCREEN,             IDTYPE_FLAGS_ISLINKABLE},
78         {ID_SEQ,  "Sequence",           "sequences",       BLT_I18NCONTEXT_ID_SEQUENCE,           0                      }, /* not actually ID data */
79         {ID_SPK,  "Speaker",            "speakers",        BLT_I18NCONTEXT_ID_SPEAKER,            IDTYPE_FLAGS_ISLINKABLE},
80         {ID_SO,   "Sound",              "sounds",          BLT_I18NCONTEXT_ID_SOUND,              IDTYPE_FLAGS_ISLINKABLE},
81         {ID_TE,   "Texture",            "textures",        BLT_I18NCONTEXT_ID_TEXTURE,            IDTYPE_FLAGS_ISLINKABLE},
82         {ID_TXT,  "Text",               "texts",           BLT_I18NCONTEXT_ID_TEXT,               IDTYPE_FLAGS_ISLINKABLE},
83         {ID_VF,   "VFont",              "fonts",           BLT_I18NCONTEXT_ID_VFONT,              IDTYPE_FLAGS_ISLINKABLE},
84         {ID_WO,   "World",              "worlds",          BLT_I18NCONTEXT_ID_WORLD,              IDTYPE_FLAGS_ISLINKABLE},
85         {ID_WM,   "WindowManager",      "window_managers", BLT_I18NCONTEXT_ID_WINDOWMANAGER,      0                       },
86         {ID_WS,   "WorkSpace",          "workspaces",      BLT_I18NCONTEXT_ID_WORKSPACE,          IDTYPE_FLAGS_ISLINKABLE},
87
88         /** Keep last, not an ID exactly, only include for completeness */
89         {ID_ID,   "ID",                 "ids",             BLT_I18NCONTEXT_ID_ID,                 0                       }, /* plural is fake */
90 };
91
92 /* -1 for ID_ID */
93 BLI_STATIC_ASSERT((ARRAY_SIZE(idtypes) - 1 == MAX_LIBARRAY), "Missing IDType");
94
95 static IDType *idtype_from_name(const char *str)
96 {
97         int i = ARRAY_SIZE(idtypes);
98
99         while (i--) {
100                 if (STREQ(str, idtypes[i].name)) {
101                         return &idtypes[i];
102                 }
103         }
104
105         return NULL;
106 }
107 static IDType *idtype_from_code(short idcode)
108 {
109         int i = ARRAY_SIZE(idtypes);
110
111         while (i--)
112                 if (idcode == idtypes[i].code)
113                         return &idtypes[i];
114
115         return NULL;
116 }
117
118 /**
119  * Return if the ID code is a valid ID code.
120  *
121  * \param idcode: The code to check.
122  * \return Boolean, 0 when invalid.
123  */
124 bool BKE_idcode_is_valid(short idcode)
125 {
126         return idtype_from_code(idcode) ? true : false;
127 }
128
129 /**
130  * Return non-zero when an ID type is linkable.
131  *
132  * \param idcode: The code to check.
133  * \return Boolean, 0 when non linkable.
134  */
135 bool BKE_idcode_is_linkable(short idcode)
136 {
137         IDType *idt = idtype_from_code(idcode);
138         BLI_assert(idt);
139         return idt ? ((idt->flags & IDTYPE_FLAGS_ISLINKABLE) != 0) : false;
140 }
141
142 /**
143  * Convert an idcode into a name.
144  *
145  * \param idcode: The code to convert.
146  * \return A static string representing the name of
147  * the code.
148  */
149 const char *BKE_idcode_to_name(short idcode)
150 {
151         IDType *idt = idtype_from_code(idcode);
152         BLI_assert(idt);
153         return idt ? idt->name : NULL;
154 }
155
156 /**
157  * Convert a name into an idcode (ie. ID_SCE)
158  *
159  * \param name: The name to convert.
160  * \return The code for the name, or 0 if invalid.
161  */
162 short BKE_idcode_from_name(const char *name)
163 {
164         IDType *idt = idtype_from_name(name);
165         BLI_assert(idt);
166         return idt ? idt->code : 0;
167 }
168
169 /**
170  * Convert an idcode into an idfilter (e.g. ID_OB -> FILTER_ID_OB).
171  */
172 int BKE_idcode_to_idfilter(const short idcode)
173 {
174 #define CASE_IDFILTER(_id) case ID_##_id: return FILTER_ID_##_id
175
176         switch (idcode) {
177                 CASE_IDFILTER(AC);
178                 CASE_IDFILTER(AR);
179                 CASE_IDFILTER(BR);
180                 CASE_IDFILTER(CA);
181                 CASE_IDFILTER(CF);
182                 CASE_IDFILTER(CU);
183                 CASE_IDFILTER(GD);
184                 CASE_IDFILTER(GR);
185                 CASE_IDFILTER(IM);
186                 CASE_IDFILTER(LA);
187                 CASE_IDFILTER(LS);
188                 CASE_IDFILTER(LT);
189                 CASE_IDFILTER(MA);
190                 CASE_IDFILTER(MB);
191                 CASE_IDFILTER(MC);
192                 CASE_IDFILTER(ME);
193                 CASE_IDFILTER(MSK);
194                 CASE_IDFILTER(NT);
195                 CASE_IDFILTER(OB);
196                 CASE_IDFILTER(PA);
197                 CASE_IDFILTER(PAL);
198                 CASE_IDFILTER(PC);
199                 CASE_IDFILTER(LP);
200                 CASE_IDFILTER(SCE);
201                 CASE_IDFILTER(SPK);
202                 CASE_IDFILTER(SO);
203                 CASE_IDFILTER(TE);
204                 CASE_IDFILTER(TXT);
205                 CASE_IDFILTER(VF);
206                 CASE_IDFILTER(WO);
207                 CASE_IDFILTER(WS);
208                 default:
209                         return 0;
210         }
211
212 #undef CASE_IDFILTER
213 }
214
215 /**
216  * Convert an idfilter into an idcode (e.g. FILTER_ID_OB -> ID_OB).
217  */
218 short BKE_idcode_from_idfilter(const int idfilter)
219 {
220 #define CASE_IDFILTER(_id) case FILTER_ID_##_id: return ID_##_id
221
222         switch (idfilter) {
223                 CASE_IDFILTER(AC);
224                 CASE_IDFILTER(AR);
225                 CASE_IDFILTER(BR);
226                 CASE_IDFILTER(CA);
227                 CASE_IDFILTER(CF);
228                 CASE_IDFILTER(CU);
229                 CASE_IDFILTER(GD);
230                 CASE_IDFILTER(GR);
231                 CASE_IDFILTER(IM);
232                 CASE_IDFILTER(LA);
233                 CASE_IDFILTER(LS);
234                 CASE_IDFILTER(LT);
235                 CASE_IDFILTER(MA);
236                 CASE_IDFILTER(MB);
237                 CASE_IDFILTER(MC);
238                 CASE_IDFILTER(ME);
239                 CASE_IDFILTER(MSK);
240                 CASE_IDFILTER(NT);
241                 CASE_IDFILTER(OB);
242                 CASE_IDFILTER(PA);
243                 CASE_IDFILTER(PAL);
244                 CASE_IDFILTER(PC);
245                 CASE_IDFILTER(LP);
246                 CASE_IDFILTER(SCE);
247                 CASE_IDFILTER(SPK);
248                 CASE_IDFILTER(SO);
249                 CASE_IDFILTER(TE);
250                 CASE_IDFILTER(TXT);
251                 CASE_IDFILTER(VF);
252                 CASE_IDFILTER(WO);
253                 default:
254                         return 0;
255         }
256
257 #undef CASE_IDFILTER
258 }
259
260 /**
261  * Convert an idcode into an index (e.g. ID_OB -> INDEX_ID_OB).
262  */
263 int BKE_idcode_to_index(const short idcode)
264 {
265 #define CASE_IDINDEX(_id) case ID_##_id: return INDEX_ID_##_id
266
267         switch ((ID_Type)idcode) {
268                 CASE_IDINDEX(AC);
269                 CASE_IDINDEX(AR);
270                 CASE_IDINDEX(BR);
271                 CASE_IDINDEX(CA);
272                 CASE_IDINDEX(CF);
273                 CASE_IDINDEX(CU);
274                 CASE_IDINDEX(GD);
275                 CASE_IDINDEX(GR);
276                 CASE_IDINDEX(IM);
277                 CASE_IDINDEX(KE);
278                 CASE_IDINDEX(IP);
279                 CASE_IDINDEX(LA);
280                 CASE_IDINDEX(LI);
281                 CASE_IDINDEX(LS);
282                 CASE_IDINDEX(LT);
283                 CASE_IDINDEX(MA);
284                 CASE_IDINDEX(MB);
285                 CASE_IDINDEX(MC);
286                 CASE_IDINDEX(ME);
287                 CASE_IDINDEX(MSK);
288                 CASE_IDINDEX(NT);
289                 CASE_IDINDEX(OB);
290                 CASE_IDINDEX(PA);
291                 CASE_IDINDEX(PAL);
292                 CASE_IDINDEX(PC);
293                 CASE_IDINDEX(LP);
294                 CASE_IDINDEX(SCE);
295                 CASE_IDINDEX(SCR);
296                 CASE_IDINDEX(SPK);
297                 CASE_IDINDEX(SO);
298                 CASE_IDINDEX(TE);
299                 CASE_IDINDEX(TXT);
300                 CASE_IDINDEX(VF);
301                 CASE_IDINDEX(WM);
302                 CASE_IDINDEX(WO);
303                 CASE_IDINDEX(WS);
304         }
305
306         BLI_assert(0);
307         return -1;
308
309 #undef CASE_IDINDEX
310 }
311
312 /**
313  * Convert an idcode into a name (plural).
314  *
315  * \param idcode: The code to convert.
316  * \return A static string representing the name of
317  * the code.
318  */
319 const char *BKE_idcode_to_name_plural(short idcode)
320 {
321         IDType *idt = idtype_from_code(idcode);
322         BLI_assert(idt);
323         return idt ? idt->plural : NULL;
324 }
325
326 /**
327  * Convert an idcode into its translations' context.
328  *
329  * \param idcode: The code to convert.
330  * \return A static string representing the i18n context of the code.
331  */
332 const char *BKE_idcode_to_translation_context(short idcode)
333 {
334         IDType *idt = idtype_from_code(idcode);
335         BLI_assert(idt);
336         return idt ? idt->i18n_context : BLT_I18NCONTEXT_DEFAULT;
337 }
338
339 /**
340  * Return an ID code and steps the index forward 1.
341  *
342  * \param index: start as 0.
343  * \return the code, 0 when all codes have been returned.
344  */
345 short BKE_idcode_iter_step(int *index)
346 {
347         return (*index < ARRAY_SIZE(idtypes)) ? idtypes[(*index)++].code : 0;
348 }