svn merge ^/trunk/blender -r48592:HEAD
[blender.git] / source / blender / blenkernel / intern / icons.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) 2006-2007 Blender Foundation.
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  *
27  */
28
29 /** \file blender/blenkernel/intern/icons.c
30  *  \ingroup bke
31  */
32
33
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_lamp_types.h"
41 #include "DNA_material_types.h"
42 #include "DNA_texture_types.h"
43 #include "DNA_world_types.h"
44 #include "DNA_brush_types.h"
45
46 #include "BLI_utildefines.h"
47 #include "BLI_ghash.h"
48
49 #include "BKE_icons.h"
50 #include "BKE_global.h" /* only for G.background test */
51
52 #include "BLO_sys_types.h" // for intptr_t support
53
54 /* GLOBALS */
55
56 static GHash *gIcons = NULL;
57
58 static int gNextIconId = 1;
59
60 static int gFirstIconId = 1;
61
62
63 static void icon_free(void *val)
64 {
65         Icon *icon = val;
66
67         if (icon) {
68                 if (icon->drawinfo_free) {
69                         icon->drawinfo_free(icon->drawinfo);
70                 }
71                 else if (icon->drawinfo) {
72                         MEM_freeN(icon->drawinfo);
73                 }
74                 MEM_freeN(icon);
75         }
76 }
77
78 /* create an id for a new icon and make sure that ids from deleted icons get reused
79  * after the integer number range is used up */
80 static int get_next_free_id(void)
81 {
82         int startId = gFirstIconId;
83
84         /* if we haven't used up the int number range, we just return the next int */
85         if (gNextIconId >= gFirstIconId)
86                 return gNextIconId++;
87         
88         /* now we try to find the smallest icon id not stored in the gIcons hash */
89         while (BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(startId)) && startId >= gFirstIconId)
90                 startId++;
91
92         /* if we found a suitable one that isn't used yet, return it */
93         if (startId >= gFirstIconId)
94                 return startId;
95
96         /* fail */
97         return 0;
98 }
99
100 void BKE_icons_init(int first_dyn_id)
101 {
102         gNextIconId = first_dyn_id;
103         gFirstIconId = first_dyn_id;
104
105         if (!gIcons)
106                 gIcons = BLI_ghash_int_new("icons_init gh");
107 }
108
109 void BKE_icons_free(void)
110 {
111         if (gIcons)
112                 BLI_ghash_free(gIcons, NULL, icon_free);
113         gIcons = NULL;
114 }
115
116 PreviewImage *BKE_previewimg_create(void)
117 {
118         PreviewImage *prv_img = NULL;
119         int i;
120
121         prv_img = MEM_callocN(sizeof(PreviewImage), "img_prv");
122
123         for (i = 0; i < NUM_ICON_SIZES; ++i) {
124                 prv_img->changed[i] = 1;
125                 prv_img->changed_timestamp[i] = 0;
126         }
127         return prv_img;
128 }
129
130 void BKE_previewimg_freefunc(void *link)
131 {
132         PreviewImage *prv = (PreviewImage *)link;
133         if (prv) {
134                 int i;
135
136                 for (i = 0; i < NUM_ICON_SIZES; ++i) {
137                         if (prv->rect[i]) {
138                                 MEM_freeN(prv->rect[i]);
139                                 prv->rect[i] = NULL;
140                         }
141                 }
142                 MEM_freeN(prv);
143         }
144 }
145
146 void BKE_previewimg_free(PreviewImage **prv)
147 {
148         if (prv && (*prv)) {
149                 BKE_previewimg_freefunc(*prv);
150                 *prv = NULL;
151         }
152 }
153
154 PreviewImage *BKE_previewimg_copy(PreviewImage *prv)
155 {
156         PreviewImage *prv_img = NULL;
157         int i;
158
159         if (prv) {
160                 prv_img = MEM_dupallocN(prv);
161                 for (i = 0; i < NUM_ICON_SIZES; ++i) {
162                         if (prv->rect[i]) {
163                                 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
164                         }
165                         else {
166                                 prv_img->rect[i] = NULL;
167                         }
168                 }
169         }
170         return prv_img;
171 }
172
173 void BKE_previewimg_free_id(ID *id) 
174 {
175         if (GS(id->name) == ID_MA) {
176                 Material *mat = (Material *)id;
177                 BKE_previewimg_free(&mat->preview);
178         }
179         else if (GS(id->name) == ID_TE) {
180                 Tex *tex = (Tex *)id;
181                 BKE_previewimg_free(&tex->preview);
182         }
183         else if (GS(id->name) == ID_WO) {
184                 World *wo = (World *)id;
185                 BKE_previewimg_free(&wo->preview);
186         }
187         else if (GS(id->name) == ID_LA) {
188                 Lamp *la  = (Lamp *)id;
189                 BKE_previewimg_free(&la->preview);
190         }
191         else if (GS(id->name) == ID_IM) {
192                 Image *img  = (Image *)id;
193                 BKE_previewimg_free(&img->preview);
194         }
195         else if (GS(id->name) == ID_BR) {
196                 Brush *br  = (Brush *)id;
197                 BKE_previewimg_free(&br->preview);
198         }
199 }
200
201 PreviewImage *BKE_previewimg_get(ID *id)
202 {
203         PreviewImage *prv_img = NULL;
204
205         if (GS(id->name) == ID_MA) {
206                 Material *mat = (Material *)id;
207                 if (!mat->preview) mat->preview = BKE_previewimg_create();
208                 prv_img = mat->preview;
209         }
210         else if (GS(id->name) == ID_TE) {
211                 Tex *tex = (Tex *)id;
212                 if (!tex->preview) tex->preview = BKE_previewimg_create();
213                 prv_img = tex->preview;
214         }
215         else if (GS(id->name) == ID_WO) {
216                 World *wo = (World *)id;
217                 if (!wo->preview) wo->preview = BKE_previewimg_create();
218                 prv_img = wo->preview;
219         }
220         else if (GS(id->name) == ID_LA) {
221                 Lamp *la  = (Lamp *)id;
222                 if (!la->preview) la->preview = BKE_previewimg_create();
223                 prv_img = la->preview;
224         }
225         else if (GS(id->name) == ID_IM) {
226                 Image *img  = (Image *)id;
227                 if (!img->preview) img->preview = BKE_previewimg_create();
228                 prv_img = img->preview;
229         }
230         else if (GS(id->name) == ID_BR) {
231                 Brush *br  = (Brush *)id;
232                 if (!br->preview) br->preview = BKE_previewimg_create();
233                 prv_img = br->preview;
234         }
235
236         return prv_img;
237 }
238
239 void BKE_icon_changed(int id)
240 {
241         Icon *icon = NULL;
242         
243         if (!id || G.background) return;
244
245         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
246         
247         if (icon) {
248                 PreviewImage *prv = BKE_previewimg_get((ID *)icon->obj);
249
250                 /* all previews changed */
251                 if (prv) {
252                         int i;
253                         for (i = 0; i < NUM_ICON_SIZES; ++i) {
254                                 prv->changed[i] = 1;
255                                 prv->changed_timestamp[i]++;
256                         }
257                 }
258         }
259 }
260
261 int BKE_icon_getid(struct ID *id)
262 {
263         Icon *new_icon = NULL;
264
265         if (!id || G.background)
266                 return 0;
267
268         if (id->icon_id)
269                 return id->icon_id;
270
271         id->icon_id = get_next_free_id();
272
273         if (!id->icon_id) {
274                 printf("BKE_icon_getid: Internal error - not enough IDs\n");
275                 return 0;
276         }
277
278         new_icon = MEM_callocN(sizeof(Icon), "texicon");
279
280         new_icon->obj = id;
281         new_icon->type = GS(id->name);
282         
283         /* next two lines make sure image gets created */
284         new_icon->drawinfo = NULL;
285         new_icon->drawinfo_free = NULL;
286
287         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
288         
289         return id->icon_id;
290 }
291
292 Icon *BKE_icon_get(int icon_id)
293 {
294         Icon *icon = NULL;
295
296         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
297         
298         if (!icon) {
299                 printf("BKE_icon_get: Internal error, no icon for icon ID: %d\n", icon_id);
300                 return NULL;
301         }
302
303         return icon;
304 }
305
306 void BKE_icon_set(int icon_id, struct Icon *icon)
307 {
308         Icon *old_icon = NULL;
309
310         old_icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
311
312         if (old_icon) {
313                 printf("BKE_icon_set: Internal error, icon already set: %d\n", icon_id);
314                 return;
315         }
316
317         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(icon_id), icon);
318 }
319
320 void BKE_icon_delete(struct ID *id)
321 {
322
323         if (!id->icon_id) return;  /* no icon defined for library object */
324
325         BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), NULL, icon_free);
326         id->icon_id = 0;
327 }