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