2
* $Id: icons.c 16148 2008-08-17 17:11:00Z genscher $
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
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"
48
#include "BLI_ghash.h"
50
#include "BKE_icons.h"
51
#include "BKE_utildefines.h"
53
#include "BLO_sys_types.h" // for intptr_t support
55
#define GS(a) (*((short *)(a)))
59
static GHash* gIcons = NULL;
61
static int gNextIconId = 1;
63
static int gFirstIconId = 1;
66
static void icon_free(void *val)
72
if (icon->drawinfo_free) {
73
icon->drawinfo_free(icon->drawinfo);
75
else if (icon->drawinfo) {
76
MEM_freeN(icon->drawinfo);
82
/* create an id for a new icon and make sure that ids from deleted icons get reused
83
after the integer number range is used up */
84
static int get_next_free_id()
86
int startId = gFirstIconId;
88
/* if we haven't used up the int number range, we just return the next int */
89
if (gNextIconId>=gFirstIconId)
92
/* now we try to find the smallest icon id not stored in the gIcons hash */
93
while (BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(startId)) && startId>=gFirstIconId)
96
/* if we found a suitable one that isnt used yet, return it */
97
if (startId>=gFirstIconId)
104
void BKE_icons_init(int first_dyn_id)
106
gNextIconId = first_dyn_id;
107
gFirstIconId = first_dyn_id;
110
gIcons = BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
113
void BKE_icons_free()
116
BLI_ghash_free(gIcons, 0, icon_free);
120
struct PreviewImage* BKE_previewimg_create()
122
PreviewImage* prv_img = NULL;
125
prv_img = MEM_callocN(sizeof(PreviewImage), "img_prv");
127
for (i=0; i<PREVIEW_MIPMAPS; ++i) {
128
prv_img->changed[i] = 1;
133
void BKE_previewimg_free(PreviewImage **prv)
138
for (i=0; i<PREVIEW_MIPMAPS;++i) {
139
if ((*prv)->rect[i]) {
140
MEM_freeN((*prv)->rect[i]);
141
(*prv)->rect[i] = NULL;
149
struct PreviewImage* BKE_previewimg_copy(PreviewImage *prv)
151
PreviewImage* prv_img = NULL;
155
prv_img = MEM_dupallocN(prv);
156
for (i=0; i < PREVIEW_MIPMAPS; ++i) {
158
prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
160
prv_img->rect[i] = NULL;
167
void BKE_previewimg_free_id(ID *id)
169
if (GS(id->name) == ID_MA) {
170
Material *mat = (Material*)id;
171
BKE_previewimg_free(&mat->preview);
172
} else if (GS(id->name) == ID_TE) {
174
BKE_previewimg_free(&tex->preview);
175
} else if (GS(id->name) == ID_WO) {
176
World *wo = (World*)id;
177
BKE_previewimg_free(&wo->preview);
178
} else if (GS(id->name) == ID_LA) {
179
Lamp *la = (Lamp*)id;
180
BKE_previewimg_free(&la->preview);
181
} else if (GS(id->name) == ID_IM) {
182
Image *img = (Image*)id;
183
BKE_previewimg_free(&img->preview);
187
PreviewImage* BKE_previewimg_get(ID *id)
189
PreviewImage* prv_img = NULL;
191
if (GS(id->name) == ID_MA) {
192
Material *mat = (Material*)id;
193
if (!mat->preview) mat->preview = BKE_previewimg_create();
194
prv_img = mat->preview;
195
} else if (GS(id->name) == ID_TE) {
197
if (!tex->preview) tex->preview = BKE_previewimg_create();
198
prv_img = tex->preview;
199
} else if (GS(id->name) == ID_WO) {
200
World *wo = (World*)id;
201
if (!wo->preview) wo->preview = BKE_previewimg_create();
202
prv_img = wo->preview;
203
} else if (GS(id->name) == ID_LA) {
204
Lamp *la = (Lamp*)id;
205
if (!la->preview) la->preview = BKE_previewimg_create();
206
prv_img = la->preview;
207
} else if (GS(id->name) == ID_IM) {
208
Image *img = (Image*)id;
209
if (!img->preview) img->preview = BKE_previewimg_create();
210
prv_img = img->preview;
216
void BKE_icon_changed(int id)
222
icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
226
PreviewImage *prv = BKE_previewimg_get((ID*)icon->obj);
228
/* all previews changed */
231
for (i=0; i<PREVIEW_MIPMAPS; ++i) {
238
int BKE_icon_getid(struct ID* id)
248
id->icon_id = get_next_free_id();
251
printf("BKE_icon_getid: Internal error - not enough IDs\n");
255
new_icon = MEM_callocN(sizeof(Icon), "texicon");
258
new_icon->type = GS(id->name);
260
/* next two lines make sure image gets created */
261
new_icon->drawinfo = 0;
262
new_icon->drawinfo_free = 0;
264
BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
269
Icon* BKE_icon_get(int icon_id)
273
icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
276
printf("BKE_icon_get: Internal error, no icon for icon ID: %d\n", icon_id);
283
void BKE_icon_set(int icon_id, struct Icon* icon)
287
old_icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
291
printf("BKE_icon_set: Internal error, icon already set: %d\n", icon_id);
295
BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(icon_id), icon);
298
void BKE_icon_delete(struct ID* id)
301
if (!id->icon_id) return; /* no icon defined for library object */
303
BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), 0, icon_free);