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)
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
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>
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
43 #include <gdk/gdkprivate.h> /* XXX - find another way to do this */
44 #include "collection.h"
46 # include <gdk_imlib.h>
51 #include "gui_support.h"
54 GFSCache
*pixmap_cache
= NULL
;
56 static char * bad_xpm
[] = {
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);
101 static GdkImlibImage
*make_half_size(GdkImlibImage
*big
);
105 /****************************************************************
106 * EXTERNAL INTERFACE *
107 ****************************************************************/
109 void pixmaps_init(void)
114 gtk_widget_push_visual(gdk_imlib_get_visual());
115 gtk_widget_push_colormap(gdk_imlib_get_colormap());
118 pixmap_cache
= g_fscache_new((GFSLoadFunc
) load
,
121 (GFSGetRefFunc
) getref
,
122 NULL
, /* Update func */
125 gtk_timeout_add(10000, purge
, NULL
);
127 load_default_pixmaps();
130 /* 'name' is relative to APP_DIR. Always returns with a valid image. */
131 MaskedPixmap
*load_pixmap(char *name
)
133 MaskedPixmap
*retval
;
135 retval
= image_from_file(make_path(getenv("APP_DIR"), name
)->str
);
137 retval
= get_bad_image();
141 /* Load all the standard pixmaps */
142 static void load_default_pixmaps(void)
144 im_error
= load_pixmap("pixmaps/error.xpm");
145 im_unknown
= load_pixmap("pixmaps/unknown.xpm");
146 im_symlink
= load_pixmap("pixmaps/symlink.xpm");
148 im_unmounted
= load_pixmap("pixmaps/mount.xpm");
149 im_mounted
= load_pixmap("pixmaps/mounted.xpm");
150 im_multiple
= load_pixmap("pixmaps/multiple.xpm");
151 im_exec_file
= load_pixmap("pixmaps/exec.xpm");
152 im_appdir
= load_pixmap("pixmaps/application.xpm");
154 im_up_icon
= load_pixmap("pixmaps/up.xpm");
155 im_home_icon
= load_pixmap("pixmaps/home.xpm");
156 im_refresh_icon
= load_pixmap("pixmaps/refresh.xpm");
157 im_help
= load_pixmap("pixmaps/help.xpm");
160 void pixmap_ref(MaskedPixmap
*mp
)
162 ref(mp
, pixmap_cache
->user_data
);
165 void pixmap_unref(MaskedPixmap
*mp
)
167 unref(mp
, pixmap_cache
->user_data
);
170 void pixmap_make_small(MaskedPixmap
*mp
)
178 GdkImlibImage
*small
;
180 small
= make_half_size(mp
->image
);
182 if (small
&& gdk_imlib_render(small
,
183 small
->rgb_width
, small
->rgb_height
))
185 mp
->sm_pixmap
= gdk_imlib_move_image(small
);
186 mp
->sm_mask
= gdk_imlib_move_mask(small
);
187 mp
->sm_width
= small
->width
;
188 mp
->sm_height
= small
->height
;
192 gdk_imlib_kill_image(small
);
197 gdk_pixmap_ref(mp
->pixmap
);
199 gdk_bitmap_ref(mp
->mask
);
200 mp
->sm_pixmap
= mp
->pixmap
;
201 mp
->sm_mask
= mp
->mask
;
202 mp
->sm_width
= mp
->width
;
203 mp
->sm_height
= mp
->height
;
206 /****************************************************************
207 * INTERNAL FUNCTIONS *
208 ****************************************************************/
210 /* Load the image 'path' and return a pointer to the resulting
211 * MaskedPixmap. NULL on failure.
213 static MaskedPixmap
*image_from_file(char *path
)
221 GdkImlibImage
*image
;
223 image
= gdk_imlib_load_image(path
);
227 /* Avoid ImLib cache - ours is better! */
228 gdk_imlib_changed_image(image
);
230 if (!gdk_imlib_render(image
, image
->rgb_width
, image
->rgb_height
))
232 gdk_imlib_kill_image(image
);
236 pixmap
= image
->pixmap
;
237 mask
= image
->shape_mask
;
238 width
= image
->width
;
239 height
= image
->height
;
241 pixmap
= gdk_pixmap_colormap_create_from_xpm(NULL
,
242 gtk_widget_get_default_colormap(),
249 width
= ((GdkPixmapPrivate
*) pixmap
)->width
;
250 height
= ((GdkPixmapPrivate
*) pixmap
)->height
;
253 mp
= g_new(MaskedPixmap
, 1);
262 mp
->sm_pixmap
= NULL
;
268 /* Return a pointer to the (static) bad image. The ref counter will ensure
269 * that the image is never freed.
271 static MaskedPixmap
*get_bad_image(void)
273 static MaskedPixmap
*image
= NULL
;
277 image
= g_new(MaskedPixmap
, 1);
280 image
->pixmap
= gdk_pixmap_colormap_create_from_xpm_d(NULL
,
281 gtk_widget_get_default_colormap(),
282 &image
->mask
, NULL
, bad_xpm
);
289 image
->width
= ((GdkPixmapPrivate
*) image
->pixmap
)->width
;
290 image
->height
= ((GdkPixmapPrivate
*) image
->pixmap
)->height
;
295 static MaskedPixmap
*load(char *pathname
, gpointer user_data
)
297 return image_from_file(pathname
);
300 static void ref(MaskedPixmap
*mp
, gpointer data
)
302 /* printf("[ ref %p %d->%d ]\n", mp, mp->ref, mp->ref + 1); */
308 static void unref(MaskedPixmap
*mp
, gpointer data
)
310 /* printf("[ unref %p %d->%d ]\n", mp, mp->ref, mp->ref - 1); */
312 if (mp
&& --mp
->ref
== 0)
317 gdk_imlib_kill_image(mp
->image
);
322 gdk_pixmap_unref(mp
->pixmap
);
324 gdk_bitmap_unref(mp
->mask
);
327 gdk_pixmap_unref(mp
->sm_pixmap
);
329 gdk_bitmap_unref(mp
->sm_mask
);
334 static int getref(MaskedPixmap
*mp
)
339 /* Called now and then to clear out old pixmaps */
340 static gint
purge(gpointer data
)
342 g_fscache_purge(pixmap_cache
, PIXMAP_PURGE_TIME
);
349 /* Returns data to make an 1/4 size image of 'big'. g_free() the result. */
350 static GdkImlibImage
*make_half_size(GdkImlibImage
*big
)
352 int line_size
= big
->width
* 3;
353 int sw
= big
->width
>> 1;
354 int sh
= big
->height
>> 1;
355 GdkImlibColor tr
; /* Mask colour */
356 unsigned char *small_data
, *in
, *out
;
357 GdkImlibImage
*small
;
359 GtkStyle
*style
= gtk_widget_get_default_style();
360 GdkColor
*bg
= &style
->bg
[GTK_STATE_INSENSITIVE
];
362 gdk_imlib_get_image_shape(big
, &tr
);
363 small_data
= g_malloc(sw
* sh
* 3);
367 for (y
= 0; y
< sh
; y
++)
369 in
= big
->rgb_data
+ y
* line_size
* 2;
371 for (x
= 0; x
< sw
; x
++)
373 int r1
= in
[0], r2
= in
[3];
374 int r3
= in
[0 + line_size
], r4
= in
[3 + line_size
];
375 int g1
= in
[1], g2
= in
[4];
376 int g3
= in
[1 + line_size
], g4
= in
[4 + line_size
];
377 int b1
= in
[2], b2
= in
[5];
378 int b3
= in
[2 + line_size
], b4
= in
[5 + line_size
];
379 int m
= 0; /* No. trans pixels */
381 if (r1
== tr
.r
&& g1
== tr
.g
&& b1
== tr
.b
)
388 if (r2
== tr
.r
&& g2
== tr
.g
&& b2
== tr
.b
)
395 if (r3
== tr
.r
&& g3
== tr
.g
&& b3
== tr
.b
)
402 if (r4
== tr
.r
&& g4
== tr
.g
&& b4
== tr
.b
)
412 out
[0] = (r1
+ r2
+ r3
+ r4
) >> 2;
413 out
[1] = (g1
+ g2
+ g3
+ g4
) >> 2;
414 out
[2] = (b1
+ b2
+ b3
+ b4
) >> 2;
428 small
= gdk_imlib_create_image_from_data(small_data
, NULL
, sw
, sh
);
432 gdk_imlib_set_image_shape(small
, &tr
);