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