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