4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2006-2007 Blender Foundation.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
39 #include "MEM_guardedalloc.h"
41 #include "DNA_lamp_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_texture_types.h"
44 #include "DNA_world_types.h"
46 #include "BLI_ghash.h"
48 #include "BKE_icons.h"
49 #include "BKE_global.h" /* only for G.background test */
51 #include "BLO_sys_types.h" // for intptr_t support
53 #define GS(a) (*((short *)(a)))
57 static GHash* gIcons = NULL;
59 static int gNextIconId = 1;
61 static int gFirstIconId = 1;
64 static void icon_free(void *val)
70 if (icon->drawinfo_free) {
71 icon->drawinfo_free(icon->drawinfo);
73 else if (icon->drawinfo) {
74 MEM_freeN(icon->drawinfo);
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()
84 int startId = gFirstIconId;
86 /* if we haven't used up the int number range, we just return the next int */
87 if (gNextIconId>=gFirstIconId)
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)
94 /* if we found a suitable one that isnt used yet, return it */
95 if (startId>=gFirstIconId)
102 void BKE_icons_init(int first_dyn_id)
104 gNextIconId = first_dyn_id;
105 gFirstIconId = first_dyn_id;
108 gIcons = BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
111 void BKE_icons_free()
114 BLI_ghash_free(gIcons, 0, icon_free);
118 struct PreviewImage* BKE_previewimg_create()
120 PreviewImage* prv_img = NULL;
123 prv_img = MEM_callocN(sizeof(PreviewImage), "img_prv");
125 for (i=0; i<PREVIEW_MIPMAPS; ++i) {
126 prv_img->changed[i] = 1;
131 void BKE_previewimg_free(PreviewImage **prv)
136 for (i=0; i<PREVIEW_MIPMAPS;++i) {
137 if ((*prv)->rect[i]) {
138 MEM_freeN((*prv)->rect[i]);
139 (*prv)->rect[i] = NULL;
147 struct PreviewImage* BKE_previewimg_copy(PreviewImage *prv)
149 PreviewImage* prv_img = NULL;
153 prv_img = MEM_dupallocN(prv);
154 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
156 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
158 prv_img->rect[i] = NULL;
165 void BKE_previewimg_free_id(ID *id)
167 if (GS(id->name) == ID_MA) {
168 Material *mat = (Material*)id;
169 BKE_previewimg_free(&mat->preview);
170 } else if (GS(id->name) == ID_TE) {
172 BKE_previewimg_free(&tex->preview);
173 } else if (GS(id->name) == ID_WO) {
174 World *wo = (World*)id;
175 BKE_previewimg_free(&wo->preview);
176 } else if (GS(id->name) == ID_LA) {
177 Lamp *la = (Lamp*)id;
178 BKE_previewimg_free(&la->preview);
179 } else if (GS(id->name) == ID_IM) {
180 Image *img = (Image*)id;
181 BKE_previewimg_free(&img->preview);
185 PreviewImage* BKE_previewimg_get(ID *id)
187 PreviewImage* prv_img = NULL;
189 if (GS(id->name) == ID_MA) {
190 Material *mat = (Material*)id;
191 if (!mat->preview) mat->preview = BKE_previewimg_create();
192 prv_img = mat->preview;
193 } else if (GS(id->name) == ID_TE) {
195 if (!tex->preview) tex->preview = BKE_previewimg_create();
196 prv_img = tex->preview;
197 } else if (GS(id->name) == ID_WO) {
198 World *wo = (World*)id;
199 if (!wo->preview) wo->preview = BKE_previewimg_create();
200 prv_img = wo->preview;
201 } else if (GS(id->name) == ID_LA) {
202 Lamp *la = (Lamp*)id;
203 if (!la->preview) la->preview = BKE_previewimg_create();
204 prv_img = la->preview;
205 } else if (GS(id->name) == ID_IM) {
206 Image *img = (Image*)id;
207 if (!img->preview) img->preview = BKE_previewimg_create();
208 prv_img = img->preview;
214 void BKE_icon_changed(int id)
218 if (!id || G.background) return;
220 icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
224 PreviewImage *prv = BKE_previewimg_get((ID*)icon->obj);
226 /* all previews changed */
229 for (i=0; i<PREVIEW_MIPMAPS; ++i) {
236 int BKE_icon_getid(struct ID* id)
240 if (!id || G.background)
246 id->icon_id = get_next_free_id();
249 printf("BKE_icon_getid: Internal error - not enough IDs\n");
253 new_icon = MEM_callocN(sizeof(Icon), "texicon");
256 new_icon->type = GS(id->name);
258 /* next two lines make sure image gets created */
259 new_icon->drawinfo = 0;
260 new_icon->drawinfo_free = 0;
262 BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
267 Icon* BKE_icon_get(int icon_id)
271 icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
274 printf("BKE_icon_get: Internal error, no icon for icon ID: %d\n", icon_id);
281 void BKE_icon_set(int icon_id, struct Icon* icon)
285 old_icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
289 printf("BKE_icon_set: Internal error, icon already set: %d\n", icon_id);
293 BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(icon_id), icon);
296 void BKE_icon_delete(struct ID* id)
299 if (!id->icon_id) return; /* no icon defined for library object */
301 BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), 0, icon_free);