8ce3847bf08327707c043f86abfc66a0a5f4c73d
[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 #include "MEM_guardedalloc.h"
36
37 #include "DNA_lamp_types.h"
38 #include "DNA_material_types.h"
39 #include "DNA_texture_types.h"
40 #include "DNA_world_types.h"
41 #include "DNA_brush_types.h"
42
43 #include "BLI_utildefines.h"
44 #include "BLI_ghash.h"
45
46 #include "BKE_icons.h"
47 #include "BKE_global.h" /* only for G.background test */
48
49 #include "BLO_sys_types.h" // for intptr_t support
50
51 #define GS(a)   (*((short *)(a)))
52
53 /* GLOBALS */
54
55 static GHash* gIcons = NULL;
56
57 static int gNextIconId = 1;
58
59 static int gFirstIconId = 1;
60
61
62 static void icon_free(void *val)
63 {
64         Icon* icon = val;
65
66         if (icon)
67         {
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 isnt 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_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp, "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 struct 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<PREVIEW_MIPMAPS; ++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_free(PreviewImage **prv)
131 {
132         if(prv && (*prv)) {
133                 int i;
134                 
135                 for (i=0; i<PREVIEW_MIPMAPS;++i) {
136                         if ((*prv)->rect[i]) {
137                                 MEM_freeN((*prv)->rect[i]);
138                                 (*prv)->rect[i] = NULL;
139                         }
140                 }
141                 MEM_freeN((*prv));
142                 *prv = NULL;
143         }
144 }
145
146 struct PreviewImage* BKE_previewimg_copy(PreviewImage *prv) 
147 {
148         PreviewImage* prv_img = NULL;
149         int i;
150
151         if (prv) {
152                 prv_img = MEM_dupallocN(prv);
153                 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
154                         if (prv->rect[i]) {
155                                 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
156                         } else {
157                                 prv_img->rect[i] = NULL;
158                         }
159                 }
160         }
161         return prv_img;
162 }
163
164 void BKE_previewimg_free_id(ID *id) 
165 {
166         if (GS(id->name) == ID_MA) {
167                 Material *mat = (Material*)id;  
168                 BKE_previewimg_free(&mat->preview);
169         } else if (GS(id->name) == ID_TE) {
170                 Tex *tex = (Tex*)id;
171                 BKE_previewimg_free(&tex->preview);
172         } else if (GS(id->name) == ID_WO) {
173                 World *wo = (World*)id;
174                 BKE_previewimg_free(&wo->preview);
175         } else if (GS(id->name) == ID_LA) {
176                 Lamp *la  = (Lamp*)id;
177                 BKE_previewimg_free(&la->preview);
178         } else if (GS(id->name) == ID_IM) {
179                 Image *img  = (Image*)id;
180                 BKE_previewimg_free(&img->preview);
181         } else if (GS(id->name) == ID_BR) {
182                 Brush *br  = (Brush*)id;
183                 BKE_previewimg_free(&br->preview);
184         }
185 }
186
187 PreviewImage* BKE_previewimg_get(ID *id) 
188 {
189         PreviewImage* prv_img = NULL;
190
191         if (GS(id->name) == ID_MA) {
192                 Material *mat = (Material*)id;  
193                 if (!mat->preview) mat->preview = BKE_previewimg_create();
194                 prv_img = mat->preview;
195         } else if (GS(id->name) == ID_TE) {
196                 Tex *tex = (Tex*)id;
197                 if (!tex->preview) tex->preview = BKE_previewimg_create();
198                 prv_img = tex->preview;
199         } else if (GS(id->name) == ID_WO) {
200                 World *wo = (World*)id;
201                 if (!wo->preview) wo->preview = BKE_previewimg_create();
202                 prv_img = wo->preview;
203         } else if (GS(id->name) == ID_LA) {
204                 Lamp *la  = (Lamp*)id;
205                 if (!la->preview) la->preview = BKE_previewimg_create();
206                 prv_img = la->preview;
207         } else if (GS(id->name) == ID_IM) {
208                 Image *img  = (Image*)id;
209                 if (!img->preview) img->preview = BKE_previewimg_create();
210                 prv_img = img->preview;
211         } else if (GS(id->name) == ID_BR) {
212                 Brush *br  = (Brush*)id;
213                 if (!br->preview) br->preview = BKE_previewimg_create();
214                 prv_img = br->preview;
215         }
216
217         return prv_img;
218 }
219
220 void BKE_icon_changed(int id)
221 {
222         Icon* icon = NULL;
223         
224         if (!id || G.background) return;
225
226         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
227         
228         if (icon)
229         {
230                 PreviewImage *prv = BKE_previewimg_get((ID*)icon->obj);
231
232                 /* all previews changed */
233                 if (prv) {
234                         int i;
235                         for (i=0; i<PREVIEW_MIPMAPS; ++i) {
236                                 prv->changed[i] = 1;
237                                 prv->changed_timestamp[i]++;
238                         }
239                 }
240         }       
241 }
242
243 int BKE_icon_getid(struct ID* id)
244 {
245         Icon* new_icon = NULL;
246
247         if (!id || G.background)
248                 return 0;
249
250         if (id->icon_id)
251                 return id->icon_id;
252
253         id->icon_id = get_next_free_id();
254
255         if (!id->icon_id){
256                 printf("BKE_icon_getid: Internal error - not enough IDs\n");
257                 return 0;
258         }
259
260         new_icon = MEM_callocN(sizeof(Icon), "texicon");
261
262         new_icon->obj = id;
263         new_icon->type = GS(id->name);
264         
265         /* next two lines make sure image gets created */
266         new_icon->drawinfo = NULL;
267         new_icon->drawinfo_free = NULL;
268
269         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
270         
271         return id->icon_id;
272 }
273
274 Icon* BKE_icon_get(int icon_id)
275 {
276         Icon* icon = NULL;
277
278         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
279         
280         if (!icon) {
281                 printf("BKE_icon_get: Internal error, no icon for icon ID: %d\n", icon_id);
282                 return NULL;
283         }
284
285         return icon;
286 }
287
288 void BKE_icon_set(int icon_id, struct Icon* icon)
289 {
290         Icon* old_icon = NULL;
291
292         old_icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
293
294         if (old_icon)
295         {
296                 printf("BKE_icon_set: Internal error, icon already set: %d\n", icon_id);
297                 return;
298         }
299
300         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(icon_id), icon);
301 }
302
303 void BKE_icon_delete(struct ID* id)
304 {
305
306         if (!id->icon_id) return; /* no icon defined for library object */
307
308         BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), NULL, icon_free);
309         id->icon_id = 0;
310 }