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