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)
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 */
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
34 #include <gdk/gdkprivate.h> /* XXX - find another way to do this */
35 #include "collection.h"
39 #include "gui_support.h"
42 GFSCache
*pixmap_cache
= NULL
;
44 static char * bad_xpm
[] = {
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
,
83 (GFSGetRefFunc
) getref
,
84 NULL
, /* Update func */
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(),
98 make_path(getenv("APP_DIR"), name
)->str
);
102 image
->pixmap
= gdk_pixmap_colormap_create_from_xpm_d(NULL
,
103 gtk_widget_get_default_colormap(),
104 &image
->mask
, NULL
, bad_xpm
);
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
)
159 MaskedPixmap
*masked_pixmap
;
161 pixmap
= gdk_pixmap_colormap_create_from_xpm(NULL
,
162 gtk_widget_get_default_colormap(),
169 masked_pixmap
= g_new(MaskedPixmap
, 1);
170 masked_pixmap
->pixmap
= pixmap
;
171 masked_pixmap
->mask
= mask
;
172 masked_pixmap
->ref
= 1;
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); */
186 gdk_pixmap_ref(mp
->pixmap
);
187 gdk_bitmap_ref(mp
->mask
);
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
);
204 static int getref(MaskedPixmap
*mp
)
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
);