r210: Added 'Permissions' (chmod) feature.
[rox-filer.git] / ROX-Filer / src / pixmaps.c
blob03f940c7ffd65b0ae0127d0dbdea6729b8f98d44
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 /* Remove pixmaps from the cache when they haven't been accessed for
25 * this period of time (seconds).
27 #define PIXMAP_PURGE_TIME 1200
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <errno.h>
33 #include <gtk/gtk.h>
34 #include <gdk/gdkprivate.h> /* XXX - find another way to do this */
35 #include "collection.h"
37 #include "fscache.h"
38 #include "support.h"
39 #include "gui_support.h"
40 #include "pixmaps.h"
42 GFSCache *pixmap_cache = NULL;
44 static char * bad_xpm[] = {
45 "12 12 3 1",
46 " c #000000000000",
47 ". c #FFFF00000000",
48 "X c #FFFFFFFFFFFF",
49 " ",
50 " ..XXXXXX.. ",
51 " ...XXXX... ",
52 " X...XX...X ",
53 " XX......XX ",
54 " XXX....XXX ",
55 " XXX....XXX ",
56 " XX......XX ",
57 " X...XX...X ",
58 " ...XXXX... ",
59 " ..XXXXXX.. ",
60 " "};
62 MaskedPixmap default_pixmap[LAST_DEFAULT_PIXMAP];
64 /* Static prototypes */
66 static void load_default_pixmaps(void);
67 static MaskedPixmap *load(char *pathname, gpointer data);
68 static void ref(MaskedPixmap *mp, gpointer data);
69 static void unref(MaskedPixmap *mp, gpointer data);
70 static int getref(MaskedPixmap *mp);
71 static gint purge(gpointer data);
74 /****************************************************************
75 * EXTERNAL INTERFACE *
76 ****************************************************************/
78 void pixmaps_init(void)
80 pixmap_cache = g_fscache_new((GFSLoadFunc) load,
81 (GFSRefFunc) ref,
82 (GFSRefFunc) unref,
83 (GFSGetRefFunc) getref,
84 NULL, /* Update func */
85 NULL);
87 gtk_timeout_add(10000, purge, NULL);
89 load_default_pixmaps();
92 void load_pixmap(char *name, MaskedPixmap *image)
94 image->pixmap = gdk_pixmap_colormap_create_from_xpm(NULL,
95 gtk_widget_get_default_colormap(),
96 &image->mask,
98 make_path(getenv("APP_DIR"), name)->str);
100 if (!image->pixmap)
102 image->pixmap= gdk_pixmap_colormap_create_from_xpm_d(NULL,
103 gtk_widget_get_default_colormap(),
104 &image->mask, NULL, bad_xpm);
107 image->ref = 1;
108 /* XXX: ugly */
109 image->width = ((GdkPixmapPrivate *) image->pixmap)->width;
110 image->height = ((GdkPixmapPrivate *) image->pixmap)->height;
113 /* Load all the standard pixmaps */
114 static void load_default_pixmaps(void)
116 load_pixmap("pixmaps/error.xpm", default_pixmap + TYPE_ERROR);
117 load_pixmap("pixmaps/unknown.xpm", default_pixmap + TYPE_UNKNOWN);
118 load_pixmap("pixmaps/symlink.xpm", default_pixmap + TYPE_SYMLINK);
119 load_pixmap("pixmaps/file.xpm", default_pixmap + TYPE_FILE);
120 load_pixmap("pixmaps/directory.xpm", default_pixmap + TYPE_DIRECTORY);
121 load_pixmap("pixmaps/char.xpm", default_pixmap + TYPE_CHAR_DEVICE);
122 load_pixmap("pixmaps/block.xpm", default_pixmap + TYPE_BLOCK_DEVICE);
123 load_pixmap("pixmaps/pipe.xpm", default_pixmap + TYPE_PIPE);
124 load_pixmap("pixmaps/socket.xpm", default_pixmap + TYPE_SOCKET);
126 load_pixmap("pixmaps/mount.xpm", default_pixmap + TYPE_UNMOUNTED);
127 load_pixmap("pixmaps/mounted.xpm", default_pixmap + TYPE_MOUNTED);
128 load_pixmap("pixmaps/multiple.xpm", default_pixmap + TYPE_MULTIPLE);
129 load_pixmap("pixmaps/exec.xpm", default_pixmap + TYPE_EXEC_FILE);
130 load_pixmap("pixmaps/application.xpm", default_pixmap + TYPE_APPDIR);
132 load_pixmap("pixmaps/up.xpm", default_pixmap + TOOLBAR_UP_ICON);
133 load_pixmap("pixmaps/home.xpm", default_pixmap + TOOLBAR_HOME_ICON);
134 load_pixmap("pixmaps/refresh.xpm", default_pixmap +
135 TOOLBAR_REFRESH_ICON);
138 void pixmap_ref(MaskedPixmap *mp)
140 ref(mp, pixmap_cache->user_data);
143 void pixmap_unref(MaskedPixmap *mp)
145 unref(mp, pixmap_cache->user_data);
148 /****************************************************************
149 * INTERNAL FUNCTIONS *
150 ****************************************************************/
152 /* Try to load the pixmap from the given path, allocate a MaskedPixmap
153 * structure for it and return a pointer to the structure. NULL on failure.
155 static MaskedPixmap *load(char *pathname, gpointer user_data)
157 GdkPixmap *pixmap;
158 GdkBitmap *mask;
159 MaskedPixmap *masked_pixmap;
161 pixmap = gdk_pixmap_colormap_create_from_xpm(NULL,
162 gtk_widget_get_default_colormap(),
163 &mask,
165 pathname);
166 if (!pixmap)
167 return NULL;
169 masked_pixmap = g_new(MaskedPixmap, 1);
170 masked_pixmap->pixmap = pixmap;
171 masked_pixmap->mask = mask;
172 masked_pixmap->ref = 1;
173 /* XXX: ugly */
174 masked_pixmap->width = ((GdkPixmapPrivate *) pixmap)->width;
175 masked_pixmap->height = ((GdkPixmapPrivate *) pixmap)->height;
177 return masked_pixmap;
180 static void ref(MaskedPixmap *mp, gpointer data)
182 /* printf("[ ref %p %d->%d ]\n", mp, mp->ref, mp->ref + 1); */
184 if (mp)
186 gdk_pixmap_ref(mp->pixmap);
187 gdk_bitmap_ref(mp->mask);
188 mp->ref++;
192 static void unref(MaskedPixmap *mp, gpointer data)
194 /* printf("[ unref %p %d->%d ]\n", mp, mp->ref, mp->ref - 1); */
196 if (mp && --mp->ref == 0)
198 gdk_pixmap_unref(mp->pixmap);
199 gdk_bitmap_unref(mp->mask);
200 g_free(mp);
204 static int getref(MaskedPixmap *mp)
206 return mp->ref;
209 /* Called now and then to clear out old pixmaps */
210 static gint purge(gpointer data)
212 g_fscache_purge(pixmap_cache, PIXMAP_PURGE_TIME);
214 return TRUE;