r241: Small icons are now drawn at quarter size (super-sampled :-)
[rox-filer/ma.git] / ROX-Filer / src / pixmaps.c
blobdb6192d2cd9fd692986e819ca96fc876697a01d2
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* pixmaps.c - code for handling pixmaps */
25 * 2000/04/11 Imlib support added
26 * Christiansen Merel <c.merel@wanadoo.fr>
29 #include "config.h"
30 #define PIXMAPS_C
32 /* Remove pixmaps from the cache when they haven't been accessed for
33 * this period of time (seconds).
36 #define PIXMAP_PURGE_TIME 1200
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <errno.h>
42 #include <gtk/gtk.h>
43 #include <gdk/gdkprivate.h> /* XXX - find another way to do this */
44 #include "collection.h"
45 #ifdef HAVE_IMLIB
46 # include <gdk_imlib.h>
47 #endif
49 #include "fscache.h"
50 #include "support.h"
51 #include "gui_support.h"
52 #include "pixmaps.h"
54 GFSCache *pixmap_cache = NULL;
56 static char * bad_xpm[] = {
57 "12 12 3 1",
58 " c #000000000000",
59 ". c #FFFF00000000",
60 "X c #FFFFFFFFFFFF",
61 " ",
62 " ..XXXXXX.. ",
63 " ...XXXX... ",
64 " X...XX...X ",
65 " XX......XX ",
66 " XXX....XXX ",
67 " XXX....XXX ",
68 " XX......XX ",
69 " X...XX...X ",
70 " ...XXXX... ",
71 " ..XXXXXX.. ",
72 " "};
74 MaskedPixmap *default_pixmap[LAST_DEFAULT_PIXMAP];
76 /* Static prototypes */
78 static MaskedPixmap *load_pixmap(char *name);
79 static void load_default_pixmaps(void);
80 static MaskedPixmap *load(char *pathname, gpointer data);
81 static void ref(MaskedPixmap *mp, gpointer data);
82 static void unref(MaskedPixmap *mp, gpointer data);
83 static int getref(MaskedPixmap *mp);
84 static gint purge(gpointer data);
85 static MaskedPixmap *image_from_file(char *path);
86 static MaskedPixmap *get_bad_image(void);
87 static GdkImlibImage *make_half_size(GdkImlibImage *big);
90 /****************************************************************
91 * EXTERNAL INTERFACE *
92 ****************************************************************/
94 void pixmaps_init(void)
96 #ifdef HAVE_IMLIB
97 gdk_imlib_init();
98 #endif
100 pixmap_cache = g_fscache_new((GFSLoadFunc) load,
101 (GFSRefFunc) ref,
102 (GFSRefFunc) unref,
103 (GFSGetRefFunc) getref,
104 NULL, /* Update func */
105 NULL);
107 gtk_timeout_add(10000, purge, NULL);
109 load_default_pixmaps();
112 /* 'name' is relative to APP_DIR. Always returns with a valid image. */
113 MaskedPixmap *load_pixmap(char *name)
115 MaskedPixmap *retval;
117 retval = image_from_file(make_path(getenv("APP_DIR"), name)->str);
118 if (!retval)
119 retval = get_bad_image();
120 return retval;
123 /* Load all the standard pixmaps */
124 static void load_default_pixmaps(void)
126 default_pixmap[TYPE_ERROR] = load_pixmap("pixmaps/error.xpm");
127 default_pixmap[TYPE_UNKNOWN] = load_pixmap("pixmaps/unknown.xpm");
128 default_pixmap[TYPE_SYMLINK] = load_pixmap("pixmaps/symlink.xpm");
129 default_pixmap[TYPE_FILE] = load_pixmap("pixmaps/file.xpm");
130 default_pixmap[TYPE_DIRECTORY] = load_pixmap("pixmaps/directory.xpm");
131 default_pixmap[TYPE_CHAR_DEVICE] = load_pixmap("pixmaps/char.xpm");
132 default_pixmap[TYPE_BLOCK_DEVICE] = load_pixmap("pixmaps/block.xpm");
133 default_pixmap[TYPE_PIPE] = load_pixmap("pixmaps/pipe.xpm");
134 default_pixmap[TYPE_SOCKET] = load_pixmap("pixmaps/socket.xpm");
136 default_pixmap[TYPE_UNMOUNTED] = load_pixmap("pixmaps/mount.xpm");
137 default_pixmap[TYPE_MOUNTED] = load_pixmap("pixmaps/mounted.xpm");
138 default_pixmap[TYPE_MULTIPLE] = load_pixmap("pixmaps/multiple.xpm");
139 default_pixmap[TYPE_EXEC_FILE] = load_pixmap("pixmaps/exec.xpm");
140 default_pixmap[TYPE_APPDIR] = load_pixmap("pixmaps/application.xpm");
142 default_pixmap[TOOLBAR_UP_ICON] = load_pixmap("pixmaps/up.xpm");
143 default_pixmap[TOOLBAR_HOME_ICON] = load_pixmap("pixmaps/home.xpm");
144 default_pixmap[TOOLBAR_REFRESH_ICON] =
145 load_pixmap("pixmaps/refresh.xpm");
148 void pixmap_ref(MaskedPixmap *mp)
150 ref(mp, pixmap_cache->user_data);
153 void pixmap_unref(MaskedPixmap *mp)
155 unref(mp, pixmap_cache->user_data);
158 void pixmap_make_small(MaskedPixmap *mp)
160 if (mp->sm_pixmap)
161 return;
163 #ifdef HAVE_IMLIB
164 if (mp->image)
166 GdkImlibImage *small;
168 small = make_half_size(mp->image);
170 if (small && gdk_imlib_render(small,
171 small->rgb_width, small->rgb_height))
173 mp->sm_pixmap = gdk_imlib_move_image(small);
174 mp->sm_mask = gdk_imlib_move_mask(small);
175 mp->sm_width = small->width;
176 mp->sm_height = small->height;
179 if (small)
180 gdk_imlib_kill_image(small);
181 if (mp->sm_pixmap)
182 return;
184 #endif
185 mp->sm_pixmap = mp->pixmap;
186 mp->sm_mask = mp->mask;
187 mp->sm_width = mp->width;
188 mp->sm_height = mp->height;
191 /****************************************************************
192 * INTERNAL FUNCTIONS *
193 ****************************************************************/
195 /* Load the image 'path' and return a pointer to the resulting
196 * MaskedPixmap. NULL on failure.
198 static MaskedPixmap *image_from_file(char *path)
200 MaskedPixmap *mp;
201 GdkPixmap *pixmap;
202 GdkBitmap *mask;
203 int width;
204 int height;
205 #ifdef HAVE_IMLIB
206 GdkImlibImage *image;
208 image = gdk_imlib_load_image(path);
209 if (!image)
210 return NULL;
212 if (!gdk_imlib_render(image, image->rgb_width, image->rgb_height))
214 gdk_imlib_kill_image(image);
215 return NULL;
218 pixmap = image->pixmap;
219 mask = image->shape_mask;
220 width = image->width;
221 height = image->height;
222 #else
223 pixmap = gdk_pixmap_colormap_create_from_xpm(NULL,
224 gtk_widget_get_default_colormap(),
225 &mask,
227 path);
229 if (!pixmap)
230 return NULL;
231 width = ((GdkPixmapPrivate *) mp->pixmap)->width;
232 height = ((GdkPixmapPrivate *) mp->pixmap)->height;
233 #endif
235 mp = g_new(MaskedPixmap, 1);
236 mp->ref = 1;
237 mp->pixmap = pixmap;
238 mp->mask = mask;
239 mp->width = width;
240 mp->height = height;
241 #ifdef HAVE_IMLIB
242 mp->image = image;
243 mp->sm_pixmap = NULL;
244 mp->sm_mask = NULL;
245 #else
246 mp->sm_pixmap = mp->pixmap;
247 mp->sm_mask = mp->mask;
248 mp->sm_width = mp->width;
249 mp->sm_height = mp->height;
250 #endif
252 return mp;
255 /* Return a pointer to the (static) bad image. The ref counter will ensure
256 * that the image is never freed.
258 static MaskedPixmap *get_bad_image(void)
260 static MaskedPixmap *image = NULL;
262 if (!image)
264 image = g_new(MaskedPixmap, 1);
265 image->ref = 1;
267 image->pixmap= gdk_pixmap_colormap_create_from_xpm_d(NULL,
268 gtk_widget_get_default_colormap(),
269 &image->mask, NULL, bad_xpm);
270 #ifdef HAVE_IMLIB
271 image->image = NULL;
272 #endif
275 image->ref++;
276 image->width = ((GdkPixmapPrivate *) image->pixmap)->width;
277 image->height = ((GdkPixmapPrivate *) image->pixmap)->height;
279 return image;
282 static MaskedPixmap *load(char *pathname, gpointer user_data)
284 return image_from_file(pathname);
287 static void ref(MaskedPixmap *mp, gpointer data)
289 /* printf("[ ref %p %d->%d ]\n", mp, mp->ref, mp->ref + 1); */
291 if (mp)
292 mp->ref++;
295 static void unref(MaskedPixmap *mp, gpointer data)
297 /* printf("[ unref %p %d->%d ]\n", mp, mp->ref, mp->ref - 1); */
299 if (mp && --mp->ref == 0)
301 #ifdef HAVE_IMLIB
302 if (mp->image)
303 gdk_imlib_destroy_image(mp->image);
304 else
305 #endif
307 gdk_pixmap_unref(mp->pixmap);
308 gdk_bitmap_unref(mp->mask);
310 g_free(mp);
314 static int getref(MaskedPixmap *mp)
316 return mp->ref;
319 /* Called now and then to clear out old pixmaps */
320 static gint purge(gpointer data)
322 g_fscache_purge(pixmap_cache, PIXMAP_PURGE_TIME);
324 return TRUE;
327 #ifdef HAVE_IMLIB
329 #define GREY_BG 0xd8
331 /* Returns data to make an 1/4 size image of 'big'. g_free() the result. */
332 static GdkImlibImage *make_half_size(GdkImlibImage *big)
334 int line_size = big->width * 3;
335 int sw = big->width >> 1;
336 int sh = big->height >> 1;
337 GdkImlibColor tr; /* Mask colour */
338 unsigned char *small_data, *in, *out;
339 GdkImlibImage *small;
340 int x, y;
342 gdk_imlib_get_image_shape(big, &tr);
343 small_data = g_malloc(sw * sh * 3);
345 out = small_data;
347 for (y = 0; y < sh; y++)
349 in = big->rgb_data + y * line_size * 2;
351 for (x = 0; x < sw; x++)
353 int r1 = in[0], r2 = in[3];
354 int r3 = in[0 + line_size], r4 = in[3 + line_size];
355 int g1 = in[1], g2 = in[4];
356 int g3 = in[1 + line_size], g4 = in[4 + line_size];
357 int b1 = in[2], b2 = in[5];
358 int b3 = in[2 + line_size], b4 = in[5 + line_size];
359 int m = 0; /* No. trans pixels */
361 if (r1 == tr.r && g1 == tr.g && b1 == tr.b)
362 r1 = g1 = b1 = GREY_BG, m++;
363 if (r2 == tr.r && g2 == tr.g && b2 == tr.b)
364 r2 = g2 = b2 = GREY_BG, m++;
365 if (r3 == tr.r && g3 == tr.g && b3 == tr.b)
366 r3 = g3 = b3 = GREY_BG, m++;
367 if (r4 == tr.r && g4 == tr.g && b4 == tr.b)
368 r4 = g4 = b4 = GREY_BG, m++;
370 if (m < 3)
372 out[0] = (r1 + r2 + r3 + r4) >> 2;
373 out[1] = (g1 + g2 + g3 + g4) >> 2;
374 out[2] = (b1 + b2 + b3 + b4) >> 2;
376 else
378 out[0] = tr.r;
379 out[1] = tr.g;
380 out[2] = tr.b;
383 in += 6;
384 out += 3;
388 small = gdk_imlib_create_image_from_data(small_data, NULL, sw, sh);
389 g_free(small_data);
391 if (small)
392 gdk_imlib_set_image_shape(small, &tr);
394 return small;
396 #endif