remove config.h references, was added for automake build system rev around 124-126...
[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
42 #include "BLI_ghash.h"
43
44 #include "BKE_icons.h"
45 #include "BKE_global.h" /* only for G.background test */
46
47 #include "BLO_sys_types.h" // for intptr_t support
48
49 #define GS(a)   (*((short *)(a)))
50
51 /* GLOBALS */
52
53 static GHash* gIcons = NULL;
54
55 static int gNextIconId = 1;
56
57 static int gFirstIconId = 1;
58
59
60 static void icon_free(void *val)
61 {
62         Icon* icon = val;
63
64         if (icon)
65         {
66                 if (icon->drawinfo_free) {              
67                         icon->drawinfo_free(icon->drawinfo);
68                 }
69                 else if (icon->drawinfo) {
70                         MEM_freeN(icon->drawinfo);
71                 }
72                 MEM_freeN(icon);
73         }
74 }
75
76 /* create an id for a new icon and make sure that ids from deleted icons get reused
77    after the integer number range is used up */
78 static int get_next_free_id()
79 {
80         int startId = gFirstIconId;
81
82         /* if we haven't used up the int number range, we just return the next int */
83         if (gNextIconId>=gFirstIconId)
84                 return gNextIconId++;
85         
86         /* now we try to find the smallest icon id not stored in the gIcons hash */
87         while (BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(startId)) && startId>=gFirstIconId) 
88                 startId++;
89
90         /* if we found a suitable one that isnt used yet, return it */
91         if (startId>=gFirstIconId)
92                 return startId;
93
94         /* fail */
95         return 0;
96 }
97
98 void BKE_icons_init(int first_dyn_id)
99 {
100         gNextIconId = first_dyn_id;
101         gFirstIconId = first_dyn_id;
102
103         if (!gIcons)
104                 gIcons = BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
105 }
106
107 void BKE_icons_free()
108 {
109         if(gIcons)
110                 BLI_ghash_free(gIcons, 0, icon_free);
111         gIcons = NULL;
112 }
113
114 struct PreviewImage* BKE_previewimg_create() 
115 {
116         PreviewImage* prv_img = NULL;
117         int i;
118
119         prv_img = MEM_callocN(sizeof(PreviewImage), "img_prv");
120
121         for (i=0; i<PREVIEW_MIPMAPS; ++i) {
122                 prv_img->changed[i] = 1;
123         }
124         return prv_img;
125 }
126
127 void BKE_previewimg_free(PreviewImage **prv)
128 {
129         if(prv && (*prv)) {
130                 int i;
131                 
132                 for (i=0; i<PREVIEW_MIPMAPS;++i) {
133                         if ((*prv)->rect[i]) {
134                                 MEM_freeN((*prv)->rect[i]);
135                                 (*prv)->rect[i] = NULL;
136                         }
137                 }
138                 MEM_freeN((*prv));
139                 *prv = NULL;
140         }
141 }
142
143 struct PreviewImage* BKE_previewimg_copy(PreviewImage *prv) 
144 {
145         PreviewImage* prv_img = NULL;
146         int i;
147
148         if (prv) {
149                 prv_img = MEM_dupallocN(prv);
150                 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
151                         if (prv->rect[i]) {
152                                 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
153                         } else {
154                                 prv_img->rect[i] = NULL;
155                         }
156                 }
157         }
158         return prv_img;
159 }
160
161 void BKE_previewimg_free_id(ID *id) 
162 {
163         if (GS(id->name) == ID_MA) {
164                 Material *mat = (Material*)id;  
165                 BKE_previewimg_free(&mat->preview);
166         } else if (GS(id->name) == ID_TE) {
167                 Tex *tex = (Tex*)id;
168                 BKE_previewimg_free(&tex->preview);
169         } else if (GS(id->name) == ID_WO) {
170                 World *wo = (World*)id;
171                 BKE_previewimg_free(&wo->preview);
172         } else if (GS(id->name) == ID_LA) {
173                 Lamp *la  = (Lamp*)id;
174                 BKE_previewimg_free(&la->preview);
175         } else if (GS(id->name) == ID_IM) {
176                 Image *img  = (Image*)id;
177                 BKE_previewimg_free(&img->preview);
178         }
179 }
180
181 PreviewImage* BKE_previewimg_get(ID *id) 
182 {
183         PreviewImage* prv_img = NULL;
184
185         if (GS(id->name) == ID_MA) {
186                 Material *mat = (Material*)id;  
187                 if (!mat->preview) mat->preview = BKE_previewimg_create();
188                 prv_img = mat->preview;
189         } else if (GS(id->name) == ID_TE) {
190                 Tex *tex = (Tex*)id;
191                 if (!tex->preview) tex->preview = BKE_previewimg_create();
192                 prv_img = tex->preview;
193         } else if (GS(id->name) == ID_WO) {
194                 World *wo = (World*)id;
195                 if (!wo->preview) wo->preview = BKE_previewimg_create();
196                 prv_img = wo->preview;
197         } else if (GS(id->name) == ID_LA) {
198                 Lamp *la  = (Lamp*)id;
199                 if (!la->preview) la->preview = BKE_previewimg_create();
200                 prv_img = la->preview;
201         } else if (GS(id->name) == ID_IM) {
202                 Image *img  = (Image*)id;
203                 if (!img->preview) img->preview = BKE_previewimg_create();
204                 prv_img = img->preview;
205         } 
206
207         return prv_img;
208 }
209
210 void BKE_icon_changed(int id)
211 {
212         Icon* icon = 0;
213         
214         if (!id || G.background) return;
215
216         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
217         
218         if (icon)
219         {
220                 PreviewImage *prv = BKE_previewimg_get((ID*)icon->obj);
221
222                 /* all previews changed */
223                 if (prv) {
224                         int i;
225                         for (i=0; i<PREVIEW_MIPMAPS; ++i) {
226                                 prv->changed[i] = 1;
227                         }
228                 }
229         }       
230 }
231
232 int BKE_icon_getid(struct ID* id)
233 {
234         Icon* new_icon = 0;
235
236         if (!id || G.background)
237                 return 0;
238
239         if (id->icon_id)
240                 return id->icon_id;
241
242         id->icon_id = get_next_free_id();
243
244         if (!id->icon_id){
245                 printf("BKE_icon_getid: Internal error - not enough IDs\n");
246                 return 0;
247         }
248
249         new_icon = MEM_callocN(sizeof(Icon), "texicon");
250
251         new_icon->obj = id;
252         new_icon->type = GS(id->name);
253         
254         /* next two lines make sure image gets created */
255         new_icon->drawinfo = 0;
256         new_icon->drawinfo_free = 0;
257
258         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
259         
260         return id->icon_id;
261 }
262
263 Icon* BKE_icon_get(int icon_id)
264 {
265         Icon* icon = 0;
266
267         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
268         
269         if (!icon) {
270                 printf("BKE_icon_get: Internal error, no icon for icon ID: %d\n", icon_id);
271                 return 0;
272         }
273
274         return icon;
275 }
276
277 void BKE_icon_set(int icon_id, struct Icon* icon)
278 {
279         Icon* old_icon = 0;
280
281         old_icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
282
283         if (old_icon)
284         {
285                 printf("BKE_icon_set: Internal error, icon already set: %d\n", icon_id);
286                 return;
287         }
288
289         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(icon_id), icon);
290 }
291
292 void BKE_icon_delete(struct ID* id)
293 {
294
295         if (!id->icon_id) return; /* no icon defined for library object */
296
297         BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), 0, icon_free);
298         id->icon_id = 0;
299 }