I made multitude of fixes based on the comments provided online:
[blender.git] / source / blender / blenkernel / intern / icons.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. 
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 * The Original Code is Copyright (C) 2006-2007 Blender Foundation.
21 * All rights reserved.
22
23 * The Original Code is: all of this file.
24
25 * Contributor(s): none yet.
26
27 * ***** END GPL LICENSE BLOCK *****
28 *
29 */
30
31 /** \file blender/blenkernel/intern/icons.c
32  *  \ingroup bke
33  */
34
35
36 #include <math.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_lamp_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_texture_types.h"
45 #include "DNA_world_types.h"
46 #include "DNA_brush_types.h"
47
48 #include "BLI_utildefines.h"
49 #include "BLI_ghash.h"
50
51 #include "BKE_icons.h"
52 #include "BKE_global.h" /* only for G.background test */
53
54 #include "BLO_sys_types.h" // for intptr_t support
55
56 #define GS(a)   (*((short *)(a)))
57
58 /* GLOBALS */
59
60 static GHash* gIcons = NULL;
61
62 static int gNextIconId = 1;
63
64 static int gFirstIconId = 1;
65
66
67 static void icon_free(void *val)
68 {
69         Icon* icon = val;
70
71         if (icon)
72         {
73                 if (icon->drawinfo_free) {              
74                         icon->drawinfo_free(icon->drawinfo);
75                 }
76                 else if (icon->drawinfo) {
77                         MEM_freeN(icon->drawinfo);
78                 }
79                 MEM_freeN(icon);
80         }
81 }
82
83 /* create an id for a new icon and make sure that ids from deleted icons get reused
84    after the integer number range is used up */
85 static int get_next_free_id(void)
86 {
87         int startId = gFirstIconId;
88
89         /* if we haven't used up the int number range, we just return the next int */
90         if (gNextIconId>=gFirstIconId)
91                 return gNextIconId++;
92         
93         /* now we try to find the smallest icon id not stored in the gIcons hash */
94         while (BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(startId)) && startId>=gFirstIconId) 
95                 startId++;
96
97         /* if we found a suitable one that isnt used yet, return it */
98         if (startId>=gFirstIconId)
99                 return startId;
100
101         /* fail */
102         return 0;
103 }
104
105 void BKE_icons_init(int first_dyn_id)
106 {
107         gNextIconId = first_dyn_id;
108         gFirstIconId = first_dyn_id;
109
110         if (!gIcons)
111                 gIcons = BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp, "icons_init gh");
112 }
113
114 void BKE_icons_free(void)
115 {
116         if(gIcons)
117                 BLI_ghash_free(gIcons, NULL, icon_free);
118         gIcons = NULL;
119 }
120
121 struct PreviewImage* BKE_previewimg_create(void) 
122 {
123         PreviewImage* prv_img = NULL;
124         int i;
125
126         prv_img = MEM_callocN(sizeof(PreviewImage), "img_prv");
127
128         for (i=0; i<NUM_ICON_SIZES; ++i) {
129                 prv_img->changed[i] = 1;
130                 prv_img->changed_timestamp[i] = 0;
131         }
132         return prv_img;
133 }
134
135 void BKE_previewimg_freefunc(void *link)
136 {
137         PreviewImage *prv = (PreviewImage *)link;
138         if (prv) {
139                 int i;
140
141                 for (i=0; i<NUM_ICON_SIZES;++i) {
142                         if (prv->rect[i]) {
143                                 MEM_freeN(prv->rect[i]);
144                                 prv->rect[i] = NULL;
145                         }
146                 }
147                 MEM_freeN(prv);
148         }
149 }
150
151 void BKE_previewimg_free(PreviewImage **prv)
152 {
153         if(prv && (*prv)) {
154                 BKE_previewimg_freefunc(*prv);
155                 *prv = NULL;
156         }
157 }
158
159 struct PreviewImage* BKE_previewimg_copy(PreviewImage *prv) 
160 {
161         PreviewImage* prv_img = NULL;
162         int i;
163
164         if (prv) {
165                 prv_img = MEM_dupallocN(prv);
166                 for (i=0; i < NUM_ICON_SIZES; ++i) {
167                         if (prv->rect[i]) {
168                                 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
169                         } else {
170                                 prv_img->rect[i] = NULL;
171                         }
172                 }
173         }
174         return prv_img;
175 }
176
177 void BKE_previewimg_free_id(ID *id) 
178 {
179         if (GS(id->name) == ID_MA) {
180                 Material *mat = (Material*)id;  
181                 BKE_previewimg_free(&mat->preview);
182         } else if (GS(id->name) == ID_TE) {
183                 Tex *tex = (Tex*)id;
184                 BKE_previewimg_free(&tex->preview);
185         } else if (GS(id->name) == ID_WO) {
186                 World *wo = (World*)id;
187                 BKE_previewimg_free(&wo->preview);
188         } else if (GS(id->name) == ID_LA) {
189                 Lamp *la  = (Lamp*)id;
190                 BKE_previewimg_free(&la->preview);
191         } else if (GS(id->name) == ID_IM) {
192                 Image *img  = (Image*)id;
193                 BKE_previewimg_free(&img->preview);
194         } else if (GS(id->name) == ID_BR) {
195                 Brush *br  = (Brush*)id;
196                 BKE_previewimg_free(&br->preview);
197         }
198 }
199
200 PreviewImage* BKE_previewimg_get(ID *id) 
201 {
202         PreviewImage* prv_img = NULL;
203
204         if (GS(id->name) == ID_MA) {
205                 Material *mat = (Material*)id;  
206                 if (!mat->preview) mat->preview = BKE_previewimg_create();
207                 prv_img = mat->preview;
208         } else if (GS(id->name) == ID_TE) {
209                 Tex *tex = (Tex*)id;
210                 if (!tex->preview) tex->preview = BKE_previewimg_create();
211                 prv_img = tex->preview;
212         } else if (GS(id->name) == ID_WO) {
213                 World *wo = (World*)id;
214                 if (!wo->preview) wo->preview = BKE_previewimg_create();
215                 prv_img = wo->preview;
216         } else if (GS(id->name) == ID_LA) {
217                 Lamp *la  = (Lamp*)id;
218                 if (!la->preview) la->preview = BKE_previewimg_create();
219                 prv_img = la->preview;
220         } else if (GS(id->name) == ID_IM) {
221                 Image *img  = (Image*)id;
222                 if (!img->preview) img->preview = BKE_previewimg_create();
223                 prv_img = img->preview;
224         } else if (GS(id->name) == ID_BR) {
225                 Brush *br  = (Brush*)id;
226                 if (!br->preview) br->preview = BKE_previewimg_create();
227                 prv_img = br->preview;
228         }
229
230         return prv_img;
231 }
232
233 void BKE_icon_changed(int id)
234 {
235         Icon* icon = NULL;
236         
237         if (!id || G.background) return;
238
239         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
240         
241         if (icon)
242         {
243                 PreviewImage *prv = BKE_previewimg_get((ID*)icon->obj);
244
245                 /* all previews changed */
246                 if (prv) {
247                         int i;
248                         for (i=0; i<NUM_ICON_SIZES; ++i) {
249                                 prv->changed[i] = 1;
250                                 prv->changed_timestamp[i]++;
251                         }
252                 }
253         }       
254 }
255
256 int BKE_icon_getid(struct ID* id)
257 {
258         Icon* new_icon = NULL;
259
260         if (!id || G.background)
261                 return 0;
262
263         if (id->icon_id)
264                 return id->icon_id;
265
266         id->icon_id = get_next_free_id();
267
268         if (!id->icon_id){
269                 printf("BKE_icon_getid: Internal error - not enough IDs\n");
270                 return 0;
271         }
272
273         new_icon = MEM_callocN(sizeof(Icon), "texicon");
274
275         new_icon->obj = id;
276         new_icon->type = GS(id->name);
277         
278         /* next two lines make sure image gets created */
279         new_icon->drawinfo = NULL;
280         new_icon->drawinfo_free = NULL;
281
282         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
283         
284         return id->icon_id;
285 }
286
287 Icon* BKE_icon_get(int icon_id)
288 {
289         Icon* icon = NULL;
290
291         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
292         
293         if (!icon) {
294                 printf("BKE_icon_get: Internal error, no icon for icon ID: %d\n", icon_id);
295                 return NULL;
296         }
297
298         return icon;
299 }
300
301 void BKE_icon_set(int icon_id, struct Icon* icon)
302 {
303         Icon* old_icon = NULL;
304
305         old_icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
306
307         if (old_icon)
308         {
309                 printf("BKE_icon_set: Internal error, icon already set: %d\n", icon_id);
310                 return;
311         }
312
313         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(icon_id), icon);
314 }
315
316 void BKE_icon_delete(struct ID* id)
317 {
318
319         if (!id->icon_id) return; /* no icon defined for library object */
320
321         BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), NULL, icon_free);
322         id->icon_id = 0;
323 }