merge with trunk at r27259 and commit of a patch by anthony jones to fix msvc (though...
[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 #include <math.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_ID.h"
42 #include "DNA_image_types.h"
43 #include "DNA_lamp_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_texture_types.h"
46 #include "DNA_world_types.h"
47
48 #include "BLI_ghash.h"
49
50 #include "BKE_icons.h"
51 #include "BKE_utildefines.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()
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);
112 }
113
114 void BKE_icons_free()
115 {
116         if(gIcons)
117                 BLI_ghash_free(gIcons, 0, icon_free);
118         gIcons = NULL;
119 }
120
121 struct PreviewImage* BKE_previewimg_create() 
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<PREVIEW_MIPMAPS; ++i) {
129                 prv_img->changed[i] = 1;
130         }
131         return prv_img;
132 }
133
134 void BKE_previewimg_free(PreviewImage **prv)
135 {
136         if(prv && (*prv)) {
137                 int i;
138                 
139                 for (i=0; i<PREVIEW_MIPMAPS;++i) {
140                         if ((*prv)->rect[i]) {
141                                 MEM_freeN((*prv)->rect[i]);
142                                 (*prv)->rect[i] = NULL;
143                         }
144                 }
145                 MEM_freeN((*prv));
146                 *prv = NULL;
147         }
148 }
149
150 struct PreviewImage* BKE_previewimg_copy(PreviewImage *prv) 
151 {
152         PreviewImage* prv_img = NULL;
153         int i;
154
155         if (prv) {
156                 prv_img = MEM_dupallocN(prv);
157                 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
158                         if (prv->rect[i]) {
159                                 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
160                         } else {
161                                 prv_img->rect[i] = NULL;
162                         }
163                 }
164         }
165         return prv_img;
166 }
167
168 void BKE_previewimg_free_id(ID *id) 
169 {
170         if (GS(id->name) == ID_MA) {
171                 Material *mat = (Material*)id;  
172                 BKE_previewimg_free(&mat->preview);
173         } else if (GS(id->name) == ID_TE) {
174                 Tex *tex = (Tex*)id;
175                 BKE_previewimg_free(&tex->preview);
176         } else if (GS(id->name) == ID_WO) {
177                 World *wo = (World*)id;
178                 BKE_previewimg_free(&wo->preview);
179         } else if (GS(id->name) == ID_LA) {
180                 Lamp *la  = (Lamp*)id;
181                 BKE_previewimg_free(&la->preview);
182         } else if (GS(id->name) == ID_IM) {
183                 Image *img  = (Image*)id;
184                 BKE_previewimg_free(&img->preview);
185         }
186 }
187
188 PreviewImage* BKE_previewimg_get(ID *id) 
189 {
190         PreviewImage* prv_img = NULL;
191
192         if (GS(id->name) == ID_MA) {
193                 Material *mat = (Material*)id;  
194                 if (!mat->preview) mat->preview = BKE_previewimg_create();
195                 prv_img = mat->preview;
196         } else if (GS(id->name) == ID_TE) {
197                 Tex *tex = (Tex*)id;
198                 if (!tex->preview) tex->preview = BKE_previewimg_create();
199                 prv_img = tex->preview;
200         } else if (GS(id->name) == ID_WO) {
201                 World *wo = (World*)id;
202                 if (!wo->preview) wo->preview = BKE_previewimg_create();
203                 prv_img = wo->preview;
204         } else if (GS(id->name) == ID_LA) {
205                 Lamp *la  = (Lamp*)id;
206                 if (!la->preview) la->preview = BKE_previewimg_create();
207                 prv_img = la->preview;
208         } else if (GS(id->name) == ID_IM) {
209                 Image *img  = (Image*)id;
210                 if (!img->preview) img->preview = BKE_previewimg_create();
211                 prv_img = img->preview;
212         } 
213
214         return prv_img;
215 }
216
217 void BKE_icon_changed(int id)
218 {
219         Icon* icon = 0;
220         
221         if (!id || G.background) return;
222
223         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
224         
225         if (icon)
226         {
227                 PreviewImage *prv = BKE_previewimg_get((ID*)icon->obj);
228
229                 /* all previews changed */
230                 if (prv) {
231                         int i;
232                         for (i=0; i<PREVIEW_MIPMAPS; ++i) {
233                                 prv->changed[i] = 1;
234                         }
235                 }
236         }       
237 }
238
239 int BKE_icon_getid(struct ID* id)
240 {
241         Icon* new_icon = 0;
242
243         if (!id || G.background)
244                 return 0;
245
246         if (id->icon_id)
247                 return id->icon_id;
248
249         id->icon_id = get_next_free_id();
250
251         if (!id->icon_id){
252                 printf("BKE_icon_getid: Internal error - not enough IDs\n");
253                 return 0;
254         }
255
256         new_icon = MEM_callocN(sizeof(Icon), "texicon");
257
258         new_icon->obj = id;
259         new_icon->type = GS(id->name);
260         
261         /* next two lines make sure image gets created */
262         new_icon->drawinfo = 0;
263         new_icon->drawinfo_free = 0;
264
265         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
266         
267         return id->icon_id;
268 }
269
270 Icon* BKE_icon_get(int icon_id)
271 {
272         Icon* icon = 0;
273
274         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
275         
276         if (!icon) {
277                 printf("BKE_icon_get: Internal error, no icon for icon ID: %d\n", icon_id);
278                 return 0;
279         }
280
281         return icon;
282 }
283
284 void BKE_icon_set(int icon_id, struct Icon* icon)
285 {
286         Icon* old_icon = 0;
287
288         old_icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
289
290         if (old_icon)
291         {
292                 printf("BKE_icon_set: Internal error, icon already set: %d\n", icon_id);
293                 return;
294         }
295
296         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(icon_id), icon);
297 }
298
299 void BKE_icon_delete(struct ID* id)
300 {
301
302         if (!id->icon_id) return; /* no icon defined for library object */
303
304         BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), 0, icon_free);
305         id->icon_id = 0;
306 }