svn merge -r 30323:30417 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., 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         }
181 }
182
183 PreviewImage* BKE_previewimg_get(ID *id) 
184 {
185         PreviewImage* prv_img = NULL;
186
187         if (GS(id->name) == ID_MA) {
188                 Material *mat = (Material*)id;  
189                 if (!mat->preview) mat->preview = BKE_previewimg_create();
190                 prv_img = mat->preview;
191         } else if (GS(id->name) == ID_TE) {
192                 Tex *tex = (Tex*)id;
193                 if (!tex->preview) tex->preview = BKE_previewimg_create();
194                 prv_img = tex->preview;
195         } else if (GS(id->name) == ID_WO) {
196                 World *wo = (World*)id;
197                 if (!wo->preview) wo->preview = BKE_previewimg_create();
198                 prv_img = wo->preview;
199         } else if (GS(id->name) == ID_LA) {
200                 Lamp *la  = (Lamp*)id;
201                 if (!la->preview) la->preview = BKE_previewimg_create();
202                 prv_img = la->preview;
203         } else if (GS(id->name) == ID_IM) {
204                 Image *img  = (Image*)id;
205                 if (!img->preview) img->preview = BKE_previewimg_create();
206                 prv_img = img->preview;
207         }
208
209         return prv_img;
210 }
211
212 void BKE_icon_changed(int id)
213 {
214         Icon* icon = 0;
215         
216         if (!id || G.background) return;
217
218         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
219         
220         if (icon)
221         {
222                 PreviewImage *prv = BKE_previewimg_get((ID*)icon->obj);
223
224                 /* all previews changed */
225                 if (prv) {
226                         int i;
227                         for (i=0; i<PREVIEW_MIPMAPS; ++i) {
228                                 prv->changed[i] = 1;
229                                 prv->changed_timestamp[i]++;
230                         }
231                 }
232         }       
233 }
234
235 int BKE_icon_getid(struct ID* id)
236 {
237         Icon* new_icon = 0;
238
239         if (!id || G.background)
240                 return 0;
241
242         if (id->icon_id)
243                 return id->icon_id;
244
245         id->icon_id = get_next_free_id();
246
247         if (!id->icon_id){
248                 printf("BKE_icon_getid: Internal error - not enough IDs\n");
249                 return 0;
250         }
251
252         new_icon = MEM_callocN(sizeof(Icon), "texicon");
253
254         new_icon->obj = id;
255         new_icon->type = GS(id->name);
256         
257         /* next two lines make sure image gets created */
258         new_icon->drawinfo = 0;
259         new_icon->drawinfo_free = 0;
260
261         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
262         
263         return id->icon_id;
264 }
265
266 Icon* BKE_icon_get(int icon_id)
267 {
268         Icon* icon = 0;
269
270         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
271         
272         if (!icon) {
273                 printf("BKE_icon_get: Internal error, no icon for icon ID: %d\n", icon_id);
274                 return 0;
275         }
276
277         return icon;
278 }
279
280 void BKE_icon_set(int icon_id, struct Icon* icon)
281 {
282         Icon* old_icon = 0;
283
284         old_icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
285
286         if (old_icon)
287         {
288                 printf("BKE_icon_set: Internal error, icon already set: %d\n", icon_id);
289                 return;
290         }
291
292         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(icon_id), icon);
293 }
294
295 void BKE_icon_delete(struct ID* id)
296 {
297
298         if (!id->icon_id) return; /* no icon defined for library object */
299
300         BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), 0, icon_free);
301         id->icon_id = 0;
302 }