r291: Improved theme support (pixmap backgrounds work now).
[rox-filer/dt.git] / ROX-Filer / src / pixmaps.c
blobe2aaaafab18362c29c64e825b0494929fc55c179
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 *im_error;
75 MaskedPixmap *im_unknown;
76 MaskedPixmap *im_symlink;
78 MaskedPixmap *im_unmounted;
79 MaskedPixmap *im_mounted;
80 MaskedPixmap *im_multiple;
81 MaskedPixmap *im_exec_file;
82 MaskedPixmap *im_appdir;
84 MaskedPixmap *im_up_icon;
85 MaskedPixmap *im_home_icon;
86 MaskedPixmap *im_refresh_icon;
87 MaskedPixmap *im_help;
89 /* Static prototypes */
91 static MaskedPixmap *load_pixmap(char *name);
92 static void load_default_pixmaps(void);
93 static MaskedPixmap *load(char *pathname, gpointer data);
94 static void ref(MaskedPixmap *mp, gpointer data);
95 static void unref(MaskedPixmap *mp, gpointer data);
96 static int getref(MaskedPixmap *mp);
97 static gint purge(gpointer data);
98 static MaskedPixmap *image_from_file(char *path);
99 static MaskedPixmap *get_bad_image(void);
100 #ifdef HAVE_IMLIB
101 static GdkImlibImage *make_half_size(GdkImlibImage *big);
102 #endif
105 /****************************************************************
106 * EXTERNAL INTERFACE *
107 ****************************************************************/
109 void pixmaps_init(void)
111 #ifdef HAVE_IMLIB
112 gdk_imlib_init();
113 #endif
115 pixmap_cache = g_fscache_new((GFSLoadFunc) load,
116 (GFSRefFunc) ref,
117 (GFSRefFunc) unref,
118 (GFSGetRefFunc) getref,
119 NULL, /* Update func */
120 NULL);
122 gtk_timeout_add(10000, purge, NULL);
124 load_default_pixmaps();
127 /* 'name' is relative to APP_DIR. Always returns with a valid image. */
128 MaskedPixmap *load_pixmap(char *name)
130 MaskedPixmap *retval;
132 retval = image_from_file(make_path(getenv("APP_DIR"), name)->str);
133 if (!retval)
134 retval = get_bad_image();
135 return retval;
138 /* Load all the standard pixmaps */
139 static void load_default_pixmaps(void)
141 im_error = load_pixmap("pixmaps/error.xpm");
142 im_unknown = load_pixmap("pixmaps/unknown.xpm");
143 im_symlink = load_pixmap("pixmaps/symlink.xpm");
145 im_unmounted = load_pixmap("pixmaps/mount.xpm");
146 im_mounted = load_pixmap("pixmaps/mounted.xpm");
147 im_multiple = load_pixmap("pixmaps/multiple.xpm");
148 im_exec_file = load_pixmap("pixmaps/exec.xpm");
149 im_appdir = load_pixmap("pixmaps/application.xpm");
151 im_up_icon = load_pixmap("pixmaps/up.xpm");
152 im_home_icon = load_pixmap("pixmaps/home.xpm");
153 im_refresh_icon = load_pixmap("pixmaps/refresh.xpm");
154 im_help = load_pixmap("pixmaps/help.xpm");
157 void pixmap_ref(MaskedPixmap *mp)
159 ref(mp, pixmap_cache->user_data);
162 void pixmap_unref(MaskedPixmap *mp)
164 unref(mp, pixmap_cache->user_data);
167 void pixmap_make_small(MaskedPixmap *mp)
169 if (mp->sm_pixmap)
170 return;
172 #ifdef HAVE_IMLIB
173 if (mp->image)
175 GdkImlibImage *small;
177 small = make_half_size(mp->image);
179 if (small && gdk_imlib_render(small,
180 small->rgb_width, small->rgb_height))
182 mp->sm_pixmap = gdk_imlib_move_image(small);
183 mp->sm_mask = gdk_imlib_move_mask(small);
184 mp->sm_width = small->width;
185 mp->sm_height = small->height;
188 if (small)
189 gdk_imlib_kill_image(small);
190 if (mp->sm_pixmap)
191 return;
193 #endif
194 gdk_pixmap_ref(mp->pixmap);
195 if (mp->mask)
196 gdk_bitmap_ref(mp->mask);
197 mp->sm_pixmap = mp->pixmap;
198 mp->sm_mask = mp->mask;
199 mp->sm_width = mp->width;
200 mp->sm_height = mp->height;
203 /****************************************************************
204 * INTERNAL FUNCTIONS *
205 ****************************************************************/
207 /* Load the image 'path' and return a pointer to the resulting
208 * MaskedPixmap. NULL on failure.
210 static MaskedPixmap *image_from_file(char *path)
212 MaskedPixmap *mp;
213 GdkPixmap *pixmap;
214 GdkBitmap *mask;
215 int width;
216 int height;
217 #ifdef HAVE_IMLIB
218 GdkImlibImage *image;
220 image = gdk_imlib_load_image(path);
221 if (!image)
222 return NULL;
224 /* Avoid ImLib cache - ours is better! */
225 gdk_imlib_changed_image(image);
227 if (!gdk_imlib_render(image, image->rgb_width, image->rgb_height))
229 gdk_imlib_kill_image(image);
230 return NULL;
233 pixmap = image->pixmap;
234 mask = image->shape_mask;
235 width = image->width;
236 height = image->height;
237 #else
238 pixmap = gdk_pixmap_colormap_create_from_xpm(NULL,
239 gtk_widget_get_default_colormap(),
240 &mask,
242 path);
244 if (!pixmap)
245 return NULL;
246 width = ((GdkPixmapPrivate *) pixmap)->width;
247 height = ((GdkPixmapPrivate *) pixmap)->height;
248 #endif
250 mp = g_new(MaskedPixmap, 1);
251 mp->ref = 1;
252 mp->pixmap = pixmap;
253 mp->mask = mask;
254 mp->width = width;
255 mp->height = height;
256 #ifdef HAVE_IMLIB
257 mp->image = image;
258 #endif
259 mp->sm_pixmap = NULL;
260 mp->sm_mask = NULL;
262 return mp;
265 /* Return a pointer to the (static) bad image. The ref counter will ensure
266 * that the image is never freed.
268 static MaskedPixmap *get_bad_image(void)
270 static MaskedPixmap *image = NULL;
272 if (!image)
274 image = g_new(MaskedPixmap, 1);
275 image->ref = 1;
277 image->pixmap= gdk_pixmap_colormap_create_from_xpm_d(NULL,
278 gtk_widget_get_default_colormap(),
279 &image->mask, NULL, bad_xpm);
280 #ifdef HAVE_IMLIB
281 image->image = NULL;
282 #endif
285 image->ref++;
286 image->width = ((GdkPixmapPrivate *) image->pixmap)->width;
287 image->height = ((GdkPixmapPrivate *) image->pixmap)->height;
289 return image;
292 static MaskedPixmap *load(char *pathname, gpointer user_data)
294 return image_from_file(pathname);
297 static void ref(MaskedPixmap *mp, gpointer data)
299 /* printf("[ ref %p %d->%d ]\n", mp, mp->ref, mp->ref + 1); */
301 if (mp)
302 mp->ref++;
305 static void unref(MaskedPixmap *mp, gpointer data)
307 /* printf("[ unref %p %d->%d ]\n", mp, mp->ref, mp->ref - 1); */
309 if (mp && --mp->ref == 0)
311 #ifdef HAVE_IMLIB
312 if (mp->image)
314 gdk_imlib_kill_image(mp->image);
316 else
317 #endif
319 gdk_pixmap_unref(mp->pixmap);
320 if (mp->mask)
321 gdk_bitmap_unref(mp->mask);
323 if (mp->sm_pixmap)
324 gdk_pixmap_unref(mp->sm_pixmap);
325 if (mp->sm_mask)
326 gdk_bitmap_unref(mp->sm_mask);
327 g_free(mp);
331 static int getref(MaskedPixmap *mp)
333 return mp->ref;
336 /* Called now and then to clear out old pixmaps */
337 static gint purge(gpointer data)
339 g_fscache_purge(pixmap_cache, PIXMAP_PURGE_TIME);
341 return TRUE;
344 #ifdef HAVE_IMLIB
346 /* Returns data to make an 1/4 size image of 'big'. g_free() the result. */
347 static GdkImlibImage *make_half_size(GdkImlibImage *big)
349 int line_size = big->width * 3;
350 int sw = big->width >> 1;
351 int sh = big->height >> 1;
352 GdkImlibColor tr; /* Mask colour */
353 unsigned char *small_data, *in, *out;
354 GdkImlibImage *small;
355 int x, y;
356 GtkStyle *style = gtk_widget_get_default_style();
357 GdkColor *bg = &style->bg[GTK_STATE_INSENSITIVE];
359 gdk_imlib_get_image_shape(big, &tr);
360 small_data = g_malloc(sw * sh * 3);
362 out = small_data;
364 for (y = 0; y < sh; y++)
366 in = big->rgb_data + y * line_size * 2;
368 for (x = 0; x < sw; x++)
370 int r1 = in[0], r2 = in[3];
371 int r3 = in[0 + line_size], r4 = in[3 + line_size];
372 int g1 = in[1], g2 = in[4];
373 int g3 = in[1 + line_size], g4 = in[4 + line_size];
374 int b1 = in[2], b2 = in[5];
375 int b3 = in[2 + line_size], b4 = in[5 + line_size];
376 int m = 0; /* No. trans pixels */
378 if (r1 == tr.r && g1 == tr.g && b1 == tr.b)
380 r1 = bg->red;
381 g1 = bg->green;
382 b1 = bg->blue;
383 m++;
385 if (r2 == tr.r && g2 == tr.g && b2 == tr.b)
387 r2 = bg->red;
388 g2 = bg->green;
389 b2 = bg->blue;
390 m++;
392 if (r3 == tr.r && g3 == tr.g && b3 == tr.b)
394 r3 = bg->red;
395 g3 = bg->green;
396 b3 = bg->blue;
397 m++;
399 if (r4 == tr.r && g4 == tr.g && b4 == tr.b)
401 r4 = bg->red;
402 g4 = bg->green;
403 b4 = bg->blue;
404 m++;
407 if (m < 3)
409 out[0] = (r1 + r2 + r3 + r4) >> 2;
410 out[1] = (g1 + g2 + g3 + g4) >> 2;
411 out[2] = (b1 + b2 + b3 + b4) >> 2;
413 else
415 out[0] = tr.r;
416 out[1] = tr.g;
417 out[2] = tr.b;
420 in += 6;
421 out += 3;
425 small = gdk_imlib_create_image_from_data(small_data, NULL, sw, sh);
426 g_free(small_data);
428 if (small)
429 gdk_imlib_set_image_shape(small, &tr);
431 return small;
433 #endif