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