r214: Added support for mc's Virtual File System.
[rox-filer.git] / ROX-Filer / src / pixmaps.c
blobe33c71378f7b4f197c82f8d9237d26135641140c
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 */
24 #include "config.h"
26 /* Remove pixmaps from the cache when they haven't been accessed for
27 * this period of time (seconds).
30 #define PIXMAP_PURGE_TIME 1200
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <errno.h>
36 #include <gtk/gtk.h>
37 #include <gdk/gdkprivate.h> /* XXX - find another way to do this */
38 #include "collection.h"
40 #include "fscache.h"
41 #include "support.h"
42 #include "gui_support.h"
43 #include "pixmaps.h"
45 GFSCache *pixmap_cache = NULL;
47 static char * bad_xpm[] = {
48 "12 12 3 1",
49 " c #000000000000",
50 ". c #FFFF00000000",
51 "X c #FFFFFFFFFFFF",
52 " ",
53 " ..XXXXXX.. ",
54 " ...XXXX... ",
55 " X...XX...X ",
56 " XX......XX ",
57 " XXX....XXX ",
58 " XXX....XXX ",
59 " XX......XX ",
60 " X...XX...X ",
61 " ...XXXX... ",
62 " ..XXXXXX.. ",
63 " "};
65 MaskedPixmap default_pixmap[LAST_DEFAULT_PIXMAP];
67 /* Static prototypes */
69 static void load_default_pixmaps(void);
70 static MaskedPixmap *load(char *pathname, gpointer data);
71 static void ref(MaskedPixmap *mp, gpointer data);
72 static void unref(MaskedPixmap *mp, gpointer data);
73 static int getref(MaskedPixmap *mp);
74 static gint purge(gpointer data);
77 /****************************************************************
78 * EXTERNAL INTERFACE *
79 ****************************************************************/
81 void pixmaps_init(void)
83 pixmap_cache = g_fscache_new((GFSLoadFunc) load,
84 (GFSRefFunc) ref,
85 (GFSRefFunc) unref,
86 (GFSGetRefFunc) getref,
87 NULL, /* Update func */
88 NULL);
90 gtk_timeout_add(10000, purge, NULL);
92 load_default_pixmaps();
95 void load_pixmap(char *name, MaskedPixmap *image)
97 image->pixmap = gdk_pixmap_colormap_create_from_xpm(NULL,
98 gtk_widget_get_default_colormap(),
99 &image->mask,
101 make_path(getenv("APP_DIR"), name)->str);
103 if (!image->pixmap)
105 image->pixmap= gdk_pixmap_colormap_create_from_xpm_d(NULL,
106 gtk_widget_get_default_colormap(),
107 &image->mask, NULL, bad_xpm);
110 image->ref = 1;
111 /* XXX: ugly */
112 image->width = ((GdkPixmapPrivate *) image->pixmap)->width;
113 image->height = ((GdkPixmapPrivate *) image->pixmap)->height;
116 /* Load all the standard pixmaps */
117 static void load_default_pixmaps(void)
119 load_pixmap("pixmaps/error.xpm", default_pixmap + TYPE_ERROR);
120 load_pixmap("pixmaps/unknown.xpm", default_pixmap + TYPE_UNKNOWN);
121 load_pixmap("pixmaps/symlink.xpm", default_pixmap + TYPE_SYMLINK);
122 load_pixmap("pixmaps/file.xpm", default_pixmap + TYPE_FILE);
123 load_pixmap("pixmaps/directory.xpm", default_pixmap + TYPE_DIRECTORY);
124 load_pixmap("pixmaps/char.xpm", default_pixmap + TYPE_CHAR_DEVICE);
125 load_pixmap("pixmaps/block.xpm", default_pixmap + TYPE_BLOCK_DEVICE);
126 load_pixmap("pixmaps/pipe.xpm", default_pixmap + TYPE_PIPE);
127 load_pixmap("pixmaps/socket.xpm", default_pixmap + TYPE_SOCKET);
129 load_pixmap("pixmaps/mount.xpm", default_pixmap + TYPE_UNMOUNTED);
130 load_pixmap("pixmaps/mounted.xpm", default_pixmap + TYPE_MOUNTED);
131 load_pixmap("pixmaps/multiple.xpm", default_pixmap + TYPE_MULTIPLE);
132 load_pixmap("pixmaps/exec.xpm", default_pixmap + TYPE_EXEC_FILE);
133 load_pixmap("pixmaps/application.xpm", default_pixmap + TYPE_APPDIR);
135 load_pixmap("pixmaps/up.xpm", default_pixmap + TOOLBAR_UP_ICON);
136 load_pixmap("pixmaps/home.xpm", default_pixmap + TOOLBAR_HOME_ICON);
137 load_pixmap("pixmaps/refresh.xpm", default_pixmap +
138 TOOLBAR_REFRESH_ICON);
141 void pixmap_ref(MaskedPixmap *mp)
143 ref(mp, pixmap_cache->user_data);
146 void pixmap_unref(MaskedPixmap *mp)
148 unref(mp, pixmap_cache->user_data);
151 /****************************************************************
152 * INTERNAL FUNCTIONS *
153 ****************************************************************/
155 /* Try to load the pixmap from the given path, allocate a MaskedPixmap
156 * structure for it and return a pointer to the structure. NULL on failure.
158 static MaskedPixmap *load(char *pathname, gpointer user_data)
160 GdkPixmap *pixmap;
161 GdkBitmap *mask;
162 MaskedPixmap *masked_pixmap;
164 pixmap = gdk_pixmap_colormap_create_from_xpm(NULL,
165 gtk_widget_get_default_colormap(),
166 &mask,
168 pathname);
169 if (!pixmap)
170 return NULL;
172 masked_pixmap = g_new(MaskedPixmap, 1);
173 masked_pixmap->pixmap = pixmap;
174 masked_pixmap->mask = mask;
175 masked_pixmap->ref = 1;
176 /* XXX: ugly */
177 masked_pixmap->width = ((GdkPixmapPrivate *) pixmap)->width;
178 masked_pixmap->height = ((GdkPixmapPrivate *) pixmap)->height;
180 return masked_pixmap;
183 static void ref(MaskedPixmap *mp, gpointer data)
185 /* printf("[ ref %p %d->%d ]\n", mp, mp->ref, mp->ref + 1); */
187 if (mp)
189 gdk_pixmap_ref(mp->pixmap);
190 gdk_bitmap_ref(mp->mask);
191 mp->ref++;
195 static void unref(MaskedPixmap *mp, gpointer data)
197 /* printf("[ unref %p %d->%d ]\n", mp, mp->ref, mp->ref - 1); */
199 if (mp && --mp->ref == 0)
201 gdk_pixmap_unref(mp->pixmap);
202 gdk_bitmap_unref(mp->mask);
203 g_free(mp);
207 static int getref(MaskedPixmap *mp)
209 return mp->ref;
212 /* Called now and then to clear out old pixmaps */
213 static gint purge(gpointer data)
215 g_fscache_purge(pixmap_cache, PIXMAP_PURGE_TIME);
217 return TRUE;