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