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