Particles
[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
52 #define GS(a)   (*((short *)(a)))
53
54 /* GLOBALS */
55
56 static GHash* gIcons = NULL;
57
58 static int gNextIconId = 1;
59
60 static int gFirstIconId = 1;
61
62
63 static void icon_free(void *val)
64 {
65         Icon* icon = val;
66
67         if (icon)
68         {
69                 if (icon->drawinfo_free) {              
70                         icon->drawinfo_free(icon->drawinfo);
71                 }
72                 else if (icon->drawinfo) {
73                         MEM_freeN(icon->drawinfo);
74                 }
75                 MEM_freeN(icon);
76         }
77 }
78
79 /* create an id for a new icon and make sure that ids from deleted icons get reused
80    after the integer number range is used up */
81 static int get_next_free_id()
82 {
83         int startId = gFirstIconId;
84
85         /* if we haven't used up the int number range, we just return the next int */
86         if (gNextIconId>=gFirstIconId)
87                 return gNextIconId++;
88         
89         /* now we try to find the smallest icon id not stored in the gIcons hash */
90         while (BLI_ghash_lookup(gIcons, (void *)startId) && startId>=gFirstIconId) 
91                 startId++;
92
93         /* if we found a suitable one that isnt used yet, return it */
94         if (startId>=gFirstIconId)
95                 return startId;
96
97         /* fail */
98         return 0;
99 }
100
101 void BKE_icons_init(int first_dyn_id)
102 {
103         gNextIconId = first_dyn_id;
104         gFirstIconId = first_dyn_id;
105
106         if (!gIcons)
107                 gIcons = BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
108 }
109
110 void BKE_icons_free()
111 {
112         if(gIcons)
113                 BLI_ghash_free(gIcons, 0, icon_free);
114         gIcons = NULL;
115 }
116
117 struct PreviewImage* BKE_previewimg_create() 
118 {
119         PreviewImage* prv_img = NULL;
120         int i;
121
122         prv_img = MEM_callocN(sizeof(PreviewImage), "img_prv");
123
124         for (i=0; i<PREVIEW_MIPMAPS; ++i) {
125                 prv_img->changed[i] = 1;
126         }
127         return prv_img;
128 }
129
130 void BKE_previewimg_free(PreviewImage **prv)
131 {
132         if(prv && (*prv)) {
133                 int i;
134                 
135                 for (i=0; i<PREVIEW_MIPMAPS;++i) {
136                         if ((*prv)->rect[i]) {
137                                 MEM_freeN((*prv)->rect[i]);
138                                 (*prv)->rect[i] = NULL;
139                         }
140                 }
141                 MEM_freeN((*prv));
142                 *prv = NULL;
143         }
144 }
145
146 struct PreviewImage* BKE_previewimg_copy(PreviewImage *prv) 
147 {
148         PreviewImage* prv_img = NULL;
149         int i;
150
151         if (prv) {
152                 prv_img = MEM_dupallocN(prv);
153                 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
154                         if (prv->rect[i]) {
155                                 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
156                         } else {
157                                 prv_img->rect[i] = NULL;
158                         }
159                 }
160         }
161         return prv_img;
162 }
163
164 void BKE_previewimg_free_id(ID *id) 
165 {
166         if (GS(id->name) == ID_MA) {
167                 Material *mat = (Material*)id;  
168                 BKE_previewimg_free(&mat->preview);
169         } else if (GS(id->name) == ID_TE) {
170                 Tex *tex = (Tex*)id;
171                 BKE_previewimg_free(&tex->preview);
172         } else if (GS(id->name) == ID_WO) {
173                 World *wo = (World*)id;
174                 BKE_previewimg_free(&wo->preview);
175         } else if (GS(id->name) == ID_LA) {
176                 Lamp *la  = (Lamp*)id;
177                 BKE_previewimg_free(&la->preview);
178         } else if (GS(id->name) == ID_IM) {
179                 Image *img  = (Image*)id;
180                 BKE_previewimg_free(&img->preview);
181         }
182 }
183
184 PreviewImage* BKE_previewimg_get(ID *id) 
185 {
186         PreviewImage* prv_img = NULL;
187
188         if (GS(id->name) == ID_MA) {
189                 Material *mat = (Material*)id;  
190                 if (!mat->preview) mat->preview = BKE_previewimg_create();
191                 prv_img = mat->preview;
192         } else if (GS(id->name) == ID_TE) {
193                 Tex *tex = (Tex*)id;
194                 if (!tex->preview) tex->preview = BKE_previewimg_create();
195                 prv_img = tex->preview;
196         } else if (GS(id->name) == ID_WO) {
197                 World *wo = (World*)id;
198                 if (!wo->preview) wo->preview = BKE_previewimg_create();
199                 prv_img = wo->preview;
200         } else if (GS(id->name) == ID_LA) {
201                 Lamp *la  = (Lamp*)id;
202                 if (!la->preview) la->preview = BKE_previewimg_create();
203                 prv_img = la->preview;
204         } else if (GS(id->name) == ID_IM) {
205                 Image *img  = (Image*)id;
206                 if (!img->preview) img->preview = BKE_previewimg_create();
207                 prv_img = img->preview;
208         } 
209
210         return prv_img;
211 }
212
213 void BKE_icon_changed(int id)
214 {
215         Icon* icon = 0;
216         
217         if (!id) return;
218
219         icon = BLI_ghash_lookup(gIcons, (void *)id);
220         
221         if (icon)
222         {
223                 PreviewImage *prv = BKE_previewimg_get((ID*)icon->obj);
224
225                 /* all previews changed */
226                 if (prv) {
227                         int i;
228                         for (i=0; i<PREVIEW_MIPMAPS; ++i) {
229                                 prv->changed[i] = 1;
230                         }
231                 }
232         }       
233 }
234
235 int BKE_icon_getid(struct ID* id)
236 {
237         Icon* new_icon = 0;
238
239         if (!id)
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, (void *)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, (void*)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, (void*)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, (void *)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, (void*)id->icon_id, 0, icon_free);
301         id->icon_id = 0;
302 }