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)
115 pixmap_cache
= g_fscache_new((GFSLoadFunc
) load
,
118 (GFSGetRefFunc
) getref
,
119 NULL
, /* Update func */
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
);
134 retval
= get_bad_image();
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
)
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
;
189 gdk_imlib_kill_image(small
);
194 gdk_pixmap_ref(mp
->pixmap
);
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
)
218 GdkImlibImage
*image
;
220 image
= gdk_imlib_load_image(path
);
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
);
233 pixmap
= image
->pixmap
;
234 mask
= image
->shape_mask
;
235 width
= image
->width
;
236 height
= image
->height
;
238 pixmap
= gdk_pixmap_colormap_create_from_xpm(NULL
,
239 gtk_widget_get_default_colormap(),
246 width
= ((GdkPixmapPrivate
*) pixmap
)->width
;
247 height
= ((GdkPixmapPrivate
*) pixmap
)->height
;
250 mp
= g_new(MaskedPixmap
, 1);
259 mp
->sm_pixmap
= NULL
;
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
;
274 image
= g_new(MaskedPixmap
, 1);
277 image
->pixmap
= gdk_pixmap_colormap_create_from_xpm_d(NULL
,
278 gtk_widget_get_default_colormap(),
279 &image
->mask
, NULL
, bad_xpm
);
286 image
->width
= ((GdkPixmapPrivate
*) image
->pixmap
)->width
;
287 image
->height
= ((GdkPixmapPrivate
*) image
->pixmap
)->height
;
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); */
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)
314 gdk_imlib_kill_image(mp
->image
);
319 gdk_pixmap_unref(mp
->pixmap
);
321 gdk_bitmap_unref(mp
->mask
);
324 gdk_pixmap_unref(mp
->sm_pixmap
);
326 gdk_bitmap_unref(mp
->sm_mask
);
331 static int getref(MaskedPixmap
*mp
)
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
);
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
;
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);
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
)
385 if (r2
== tr
.r
&& g2
== tr
.g
&& b2
== tr
.b
)
392 if (r3
== tr
.r
&& g3
== tr
.g
&& b3
== tr
.b
)
399 if (r4
== tr
.r
&& g4
== tr
.g
&& b4
== tr
.b
)
409 out
[0] = (r1
+ r2
+ r3
+ r4
) >> 2;
410 out
[1] = (g1
+ g2
+ g3
+ g4
) >> 2;
411 out
[2] = (b1
+ b2
+ b3
+ b4
) >> 2;
425 small
= gdk_imlib_create_image_from_data(small_data
, NULL
, sw
, sh
);
429 gdk_imlib_set_image_shape(small
, &tr
);