r121: Added purging to fscache. Unused pixmaps are now removed after 10mins.
[rox-filer.git] / ROX-Filer / src / pixmaps.c
blob3470887210bd286b4e2debdd31ec43f7bfb09c2d
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, 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 */
24 /* Remove pixmaps from the cache when they haven't been accessed for
25 * this period of time (seconds).
27 #define PIXMAP_PURGE_TIME 600
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <errno.h>
33 #include <gtk/gtk.h>
34 #include "collection.h"
36 #include "fscache.h"
37 #include "support.h"
38 #include "gui_support.h"
39 #include "pixmaps.h"
41 static GFSCache *pixmap_cache = NULL;
43 static char * bad_xpm[] = {
44 "12 12 3 1",
45 " c #000000000000",
46 ". c #FFFF00000000",
47 "X c #FFFFFFFFFFFF",
48 " ",
49 " ..XXXXXX.. ",
50 " ...XXXX... ",
51 " X...XX...X ",
52 " XX......XX ",
53 " XXX....XXX ",
54 " XXX....XXX ",
55 " XX......XX ",
56 " X...XX...X ",
57 " ...XXXX... ",
58 " ..XXXXXX.. ",
59 " "};
61 MaskedPixmap default_pixmap[LAST_DEFAULT_PIXMAP];
63 /* Static prototypes */
65 static MaskedPixmap *load(char *pathname, gpointer data);
66 static void ref(MaskedPixmap *mp, gpointer data);
67 static void unref(MaskedPixmap *mp, gpointer data);
68 static gint purge(gpointer data);
71 /****************************************************************
72 * EXTERNAL INTERFACE *
73 ****************************************************************/
75 void pixmaps_init(void)
77 pixmap_cache = g_fscache_new((GFSLoadFunc) load,
78 (GFSFunc) ref,
79 (GFSFunc) unref,
80 NULL);
82 gtk_timeout_add(10000, purge, NULL);
85 /* Try to load the pixmap from the given path, allocate a MaskedPixmap
86 * structure for it and return a pointer to the structure. NULL on failure.
88 MaskedPixmap *load_pixmap_from(GtkWidget *window, char *path)
90 MaskedPixmap *masked_pixmap;
92 pixmap_cache->user_data = window;
93 masked_pixmap = g_fscache_lookup(pixmap_cache, path);
94 pixmap_cache->user_data = NULL;
96 return masked_pixmap;
99 void load_pixmap(GdkWindow *window, char *name, MaskedPixmap *image)
101 image->pixmap = gdk_pixmap_create_from_xpm(window,
102 &image->mask,
104 make_path(getenv("APP_DIR"), name)->str);
106 if (!image->pixmap)
108 image->pixmap= gdk_pixmap_create_from_xpm_d(window,
109 &image->mask, NULL, bad_xpm);
113 /* Load all the standard pixmaps */
114 void load_default_pixmaps(GdkWindow *window)
116 static gboolean loaded = FALSE;
118 if (loaded)
119 return;
121 load_pixmap(window, "pixmaps/error.xpm",
122 default_pixmap + TYPE_ERROR);
123 load_pixmap(window, "pixmaps/unknown.xpm",
124 default_pixmap + TYPE_UNKNOWN);
125 load_pixmap(window, "pixmaps/symlink.xpm",
126 default_pixmap + TYPE_SYMLINK);
127 load_pixmap(window, "pixmaps/file.xpm",
128 default_pixmap + TYPE_FILE);
129 load_pixmap(window, "pixmaps/directory.xpm",
130 default_pixmap + TYPE_DIRECTORY);
131 load_pixmap(window, "pixmaps/char.xpm",
132 default_pixmap + TYPE_CHAR_DEVICE);
133 load_pixmap(window, "pixmaps/block.xpm",
134 default_pixmap + TYPE_BLOCK_DEVICE);
135 load_pixmap(window, "pixmaps/pipe.xpm",
136 default_pixmap + TYPE_PIPE);
137 load_pixmap(window, "pixmaps/socket.xpm",
138 default_pixmap + TYPE_SOCKET);
140 load_pixmap(window, "pixmaps/mount.xpm",
141 default_pixmap + TYPE_UNMOUNTED);
142 load_pixmap(window, "pixmaps/mounted.xpm",
143 default_pixmap + TYPE_MOUNTED);
144 load_pixmap(window, "pixmaps/multiple.xpm",
145 default_pixmap + TYPE_MULTIPLE);
146 load_pixmap(window, "pixmaps/exec.xpm",
147 default_pixmap + TYPE_EXEC_FILE);
148 load_pixmap(window, "pixmaps/application.xpm",
149 default_pixmap + TYPE_APPDIR);
151 loaded = TRUE;
154 void pixmap_unref(MaskedPixmap *mp)
156 unref(mp, pixmap_cache->user_data);
159 /****************************************************************
160 * INTERNAL FUNCTIONS *
161 ****************************************************************/
163 /* Try to load the pixmap from the given path, allocate a MaskedPixmap
164 * structure for it and return a pointer to the structure. NULL on failure.
166 static MaskedPixmap *load(char *pathname, gpointer user_data)
168 GtkWidget *window = (GtkWidget *) user_data;
169 GdkPixmap *pixmap;
170 GdkBitmap *mask;
171 GtkStyle *style;
172 MaskedPixmap *masked_pixmap;
174 style = gtk_widget_get_style(window);
176 pixmap = gdk_pixmap_create_from_xpm(window->window,
177 &mask,
178 &style->bg[GTK_STATE_NORMAL],
179 pathname);
180 if (!pixmap)
181 return NULL;
183 masked_pixmap = g_new(MaskedPixmap, 1);
184 masked_pixmap->pixmap = pixmap;
185 masked_pixmap->mask = mask;
186 masked_pixmap->ref = 1;
188 return masked_pixmap;
191 static void ref(MaskedPixmap *mp, gpointer data)
193 if (mp)
194 mp->ref++;
197 static void unref(MaskedPixmap *mp, gpointer data)
199 if (mp && --mp->ref == 0)
201 gdk_pixmap_unref(mp->pixmap);
202 gdk_bitmap_unref(mp->mask);
203 g_free(mp);
207 /* Called now and then to clear out old pixmaps */
208 static gint purge(gpointer data)
210 g_fscache_purge(pixmap_cache, PIXMAP_PURGE_TIME);
212 return TRUE;