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 "collection.h"
38 #include "gui_support.h"
41 static GFSCache
*pixmap_cache
= NULL
;
43 static char * bad_xpm
[] = {
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
,
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
;
99 void load_pixmap(GdkWindow
*window
, char *name
, MaskedPixmap
*image
)
101 image
->pixmap
= gdk_pixmap_create_from_xpm(window
,
104 make_path(getenv("APP_DIR"), name
)->str
);
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
;
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
);
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
;
172 MaskedPixmap
*masked_pixmap
;
174 style
= gtk_widget_get_style(window
);
176 pixmap
= gdk_pixmap_create_from_xpm(window
->window
,
178 &style
->bg
[GTK_STATE_NORMAL
],
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
)
195 gdk_pixmap_ref(mp
->pixmap
);
196 gdk_bitmap_ref(mp
->mask
);
201 static void unref(MaskedPixmap
*mp
, gpointer data
)
203 if (mp
&& --mp
->ref
== 0)
205 gdk_pixmap_unref(mp
->pixmap
);
206 gdk_bitmap_unref(mp
->mask
);
211 /* Called now and then to clear out old pixmaps */
212 static gint
purge(gpointer data
)
214 g_fscache_purge(pixmap_cache
, PIXMAP_PURGE_TIME
);