Cycles: svn merge -r36495:36747 https://svn.blender.org/svnroot/bf-blender/trunk...
[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_free(PreviewImage **prv)
136 {
137         if(prv && (*prv)) {
138                 int i;
139                 
140                 for (i=0; i<NUM_ICON_SIZES;++i) {
141                         if ((*prv)->rect[i]) {
142                                 MEM_freeN((*prv)->rect[i]);
143                                 (*prv)->rect[i] = NULL;
144                         }
145                 }
146                 MEM_freeN((*prv));
147                 *prv = NULL;
148         }
149 }
150
151 struct PreviewImage* BKE_previewimg_copy(PreviewImage *prv) 
152 {
153         PreviewImage* prv_img = NULL;
154         int i;
155
156         if (prv) {
157                 prv_img = MEM_dupallocN(prv);
158                 for (i=0; i < NUM_ICON_SIZES; ++i) {
159                         if (prv->rect[i]) {
160                                 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
161                         } else {
162                                 prv_img->rect[i] = NULL;
163                         }
164                 }
165         }
166         return prv_img;
167 }
168
169 void BKE_previewimg_free_id(ID *id) 
170 {
171         if (GS(id->name) == ID_MA) {
172                 Material *mat = (Material*)id;  
173                 BKE_previewimg_free(&mat->preview);
174         } else if (GS(id->name) == ID_TE) {
175                 Tex *tex = (Tex*)id;
176                 BKE_previewimg_free(&tex->preview);
177         } else if (GS(id->name) == ID_WO) {
178                 World *wo = (World*)id;
179                 BKE_previewimg_free(&wo->preview);
180         } else if (GS(id->name) == ID_LA) {
181                 Lamp *la  = (Lamp*)id;
182                 BKE_previewimg_free(&la->preview);
183         } else if (GS(id->name) == ID_IM) {
184                 Image *img  = (Image*)id;
185                 BKE_previewimg_free(&img->preview);
186         } else if (GS(id->name) == ID_BR) {
187                 Brush *br  = (Brush*)id;
188                 BKE_previewimg_free(&br->preview);
189         }
190 }
191
192 PreviewImage* BKE_previewimg_get(ID *id) 
193 {
194         PreviewImage* prv_img = NULL;
195
196         if (GS(id->name) == ID_MA) {
197                 Material *mat = (Material*)id;  
198                 if (!mat->preview) mat->preview = BKE_previewimg_create();
199                 prv_img = mat->preview;
200         } else if (GS(id->name) == ID_TE) {
201                 Tex *tex = (Tex*)id;
202                 if (!tex->preview) tex->preview = BKE_previewimg_create();
203                 prv_img = tex->preview;
204         } else if (GS(id->name) == ID_WO) {
205                 World *wo = (World*)id;
206                 if (!wo->preview) wo->preview = BKE_previewimg_create();
207                 prv_img = wo->preview;
208         } else if (GS(id->name) == ID_LA) {
209                 Lamp *la  = (Lamp*)id;
210                 if (!la->preview) la->preview = BKE_previewimg_create();
211                 prv_img = la->preview;
212         } else if (GS(id->name) == ID_IM) {
213                 Image *img  = (Image*)id;
214                 if (!img->preview) img->preview = BKE_previewimg_create();
215                 prv_img = img->preview;
216         } else if (GS(id->name) == ID_BR) {
217                 Brush *br  = (Brush*)id;
218                 if (!br->preview) br->preview = BKE_previewimg_create();
219                 prv_img = br->preview;
220         }
221
222         return prv_img;
223 }
224
225 void BKE_icon_changed(int id)
226 {
227         Icon* icon = NULL;
228         
229         if (!id || G.background) return;
230
231         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
232         
233         if (icon)
234         {
235                 PreviewImage *prv = BKE_previewimg_get((ID*)icon->obj);
236
237                 /* all previews changed */
238                 if (prv) {
239                         int i;
240                         for (i=0; i<NUM_ICON_SIZES; ++i) {
241                                 prv->changed[i] = 1;
242                                 prv->changed_timestamp[i]++;
243                         }
244                 }
245         }       
246 }
247
248 int BKE_icon_getid(struct ID* id)
249 {
250         Icon* new_icon = NULL;
251
252         if (!id || G.background)
253                 return 0;
254
255         if (id->icon_id)
256                 return id->icon_id;
257
258         id->icon_id = get_next_free_id();
259
260         if (!id->icon_id){
261                 printf("BKE_icon_getid: Internal error - not enough IDs\n");
262                 return 0;
263         }
264
265         new_icon = MEM_callocN(sizeof(Icon), "texicon");
266
267         new_icon->obj = id;
268         new_icon->type = GS(id->name);
269         
270         /* next two lines make sure image gets created */
271         new_icon->drawinfo = NULL;
272         new_icon->drawinfo_free = NULL;
273
274         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
275         
276         return id->icon_id;
277 }
278
279 Icon* BKE_icon_get(int icon_id)
280 {
281         Icon* icon = NULL;
282
283         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
284         
285         if (!icon) {
286                 printf("BKE_icon_get: Internal error, no icon for icon ID: %d\n", icon_id);
287                 return NULL;
288         }
289
290         return icon;
291 }
292
293 void BKE_icon_set(int icon_id, struct Icon* icon)
294 {
295         Icon* old_icon = NULL;
296
297         old_icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
298
299         if (old_icon)
300         {
301                 printf("BKE_icon_set: Internal error, icon already set: %d\n", icon_id);
302                 return;
303         }
304
305         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(icon_id), icon);
306 }
307
308 void BKE_icon_delete(struct ID* id)
309 {
310
311         if (!id->icon_id) return; /* no icon defined for library object */
312
313         BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), NULL, icon_free);
314         id->icon_id = 0;
315 }