1 /* -*- Mode: C; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */
2 
3 /*
4  * GImageView
5  * Copyright (C) 2001 Takuro Ashie
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * $Id: gimv_icon_stock.c,v 1.2 2003/06/13 09:43:30 makeinu Exp $
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27 
28 #include <string.h>
29 #include <gtk/gtk.h>
30 
31 #include "fileutil.h"
32 #include "gimv_icon_stock.h"
33 
34 #ifndef EXCLUDE_ICONS
35 /* common icons */
36 #include "pixmaps/gimv_icon.xpm"
37 #include "pixmaps/nfolder.xpm"
38 #include "pixmaps/paper.xpm"
39 #include "pixmaps/prefs.xpm"
40 #include "pixmaps/alert.xpm"
41 #include "pixmaps/question.xpm"
42 #include "pixmaps/image.xpm"
43 #include "pixmaps/archive.xpm"
44 #include "pixmaps/small_archive.xpm"
45 #include "pixmaps/folder48.xpm"
46 #ifdef ENABLE_SPLASH
47 #   include "pixmaps/gimageview.xpm"
48 #endif
49 /* icons for image window */
50 #include "pixmaps/back.xpm"
51 #include "pixmaps/forward.xpm"
52 #include "pixmaps/no_zoom.xpm"
53 #include "pixmaps/zoom_in.xpm"
54 #include "pixmaps/zoom_out.xpm"
55 #include "pixmaps/zoom_fit.xpm"
56 #include "pixmaps/zoom.xpm"
57 #include "pixmaps/rotate.xpm"
58 #if 0
59 #include "pixmaps/rotate0.xpm"
60 #include "pixmaps/rotate1.xpm"
61 #include "pixmaps/rotate2.xpm"
62 #include "pixmaps/rotate3.xpm"
63 #endif
64 #include "pixmaps/resize.xpm"
65 #include "pixmaps/fullscreen.xpm"
66 #include "pixmaps/nav_button.xpm"
67 
68 /* player */
69 #include "pixmaps/play.xpm"
70 #include "pixmaps/pause.xpm"
71 #include "pixmaps/stop2.xpm"
72 #include "pixmaps/ff.xpm"
73 #include "pixmaps/rw.xpm"
74 #include "pixmaps/next_t.xpm"
75 #include "pixmaps/prev_t.xpm"
76 #include "pixmaps/eject.xpm"
77 
78 /* icons for thumbnail window */
79 #include "pixmaps/refresh.xpm"
80 #include "pixmaps/skip.xpm"
81 #include "pixmaps/stop.xpm"
82 #include "pixmaps/leftarrow.xpm"
83 #include "pixmaps/rightarrow.xpm"
84 #include "pixmaps/close.xpm"
85 #include "pixmaps/small_close.xpm"
86 
87 /* icons for directory view */
88 #include "pixmaps/folder.xpm"
89 #include "pixmaps/folder-link.xpm"
90 #include "pixmaps/folder-open.xpm"
91 #include "pixmaps/folder-link-open.xpm"
92 #include "pixmaps/folder-go.xpm"
93 #include "pixmaps/folder-up.xpm"
94 #include "pixmaps/folder-lock.xpm"
95 #include "pixmaps/small_home.xpm"
96 #include "pixmaps/small_up.xpm"
97 #include "pixmaps/small_refresh.xpm"
98 #include "pixmaps/dotfile.xpm"
99 #endif /* EXCLUDE_ICONS */
100 
101 
102 #ifndef EXCLUDE_ICONS
103 static GimvIconStockEntry default_icons [] = {
104    {"gimv_icon",         gimv_icon_xpm},
105    {"nfolder",           nfolder_xpm},
106    {"paper",             paper_xpm},
107    {"prefs",             prefs_xpm},
108    {"alert",             alert_xpm},
109    {"question",          question_xpm},
110    {"image",             image_xpm},
111    {"archive",           archive_xpm},
112    {"small_archive",     small_archive_xpm},
113    {"folder48",          folder48_xpm},
114 #ifdef ENABLE_SPLASH
115    {"gimageview",        gimageview_xpm},
116 #endif
117 
118    {"back",              back_xpm},
119    {"forward",           forward_xpm},
120    {"no_zoom",           no_zoom_xpm},
121    {"zoom_in",           zoom_in_xpm},
122    {"zoom_out",          zoom_out_xpm},
123    {"zoom_fit",          zoom_fit_xpm},
124    {"zoom",              zoom_xpm},
125    {"rotate",            rotate_xpm},
126 #if 0
127    {"rotate0",           rotate0_xpm},
128    {"rotate1",           rotate1_xpm},
129    {"rotate2",           rotate2_xpm},
130    {"rotate3",           rotate3_xpm},
131 #endif
132    {"resize",            resize_xpm},
133    {"fullscreen",        fullscreen_xpm},
134    {"nav-button",        nav_button_xpm},
135 
136    {"play",              play_xpm},
137    {"pause",             pause_xpm},
138    {"stop2",             stop2_xpm},
139    {"ff",                ff_xpm},
140    {"rw",                rw_xpm},
141    {"next_t",            next_t_xpm},
142    {"prev_t",            prev_t_xpm},
143    {"eject",             eject_xpm},
144 
145    {"refresh",           refresh_xpm},
146    {"skip",              skip_xpm},
147    {"stop",              stop_xpm},
148    {"leftarrow",         leftarrow_xpm},
149    {"rightarrow",        rightarrow_xpm},
150    {"close",             close_xpm},
151    {"small_close",       small_close_xpm},
152 
153    {"folder",            folder_xpm},
154    {"folder-go",         folder_go_xpm},
155    {"folder-up",         folder_up_xpm},
156    {"folder-link",       folder_link_xpm},
157    {"folder-open",       folder_open_xpm},
158    {"folder-link-open",  folder_link_open_xpm},
159    {"folder-lock",       folder_lock_xpm},
160    {"small_home",        small_home_xpm},
161    {"small_up",          small_up_xpm},
162    {"small_refresh",     small_refresh_xpm},
163    {"dotfile",           dotfile_xpm},
164 };
165 static gint default_icons_num
166 = sizeof (default_icons) / sizeof (default_icons[0]);
167 #endif /* EXCLUDE_ICONS */
168 
169 
170 static gchar       *icondir         = NULL,
171                    *default_icondir = NULL;
172 static GdkColormap *sys_colormap    = NULL;
173 static GHashTable  *icons           = NULL;
174 
175 
176 gboolean
gimv_icon_stock_init(const gchar * iconset)177 gimv_icon_stock_init (const gchar *iconset)
178 {
179    GimvIcon *icon;
180 
181    if (!default_icondir)
182       default_icondir = g_strconcat (ICONDIR, "/", DEFAULT_ICONSET, NULL);
183    if (icondir)
184       g_free (icondir);
185    if (iconset)
186       icondir = g_strconcat (ICONDIR, "/", iconset, NULL);
187    /* if (!file_exists (icondir)) return FALSE; */
188 
189    icons = g_hash_table_new (g_str_hash, g_str_equal);
190 
191    if (!sys_colormap)
192       sys_colormap = gdk_colormap_get_system ();
193 
194    /* set drag icon */
195    icon = gimv_icon_stock_get_icon ("image");
196    if (icon)
197       gtk_drag_set_default_icon (sys_colormap,
198                                  icon->pixmap, icon->mask,
199                                  0, 0);
200 
201    return TRUE;
202 }
203 
204 
205 GimvIcon *
gimv_icon_stock_get_icon(const gchar * icon_name)206 gimv_icon_stock_get_icon (const gchar *icon_name)
207 {
208    gchar *path = NULL;
209    GimvIcon *icon;
210 
211    g_return_val_if_fail (icon_name, NULL);;
212 
213    icon = g_hash_table_lookup (icons, icon_name);
214 
215    if (icon) {
216       return icon;
217    }
218 
219    icon = g_new0 (GimvIcon, 1);
220    icon->pixmap = NULL;
221    icon->mask = NULL;
222 #ifdef USE_GTK2
223    icon->pixbuf = NULL;
224 #endif /* USE_GTK2 */
225 
226    if (icondir) {
227       path = g_strconcat (icondir, "/", icon_name, ".xpm", NULL);
228       if (file_exists (path))
229          icon->pixmap = gdk_pixmap_colormap_create_from_xpm (NULL, sys_colormap,
230                                                              &icon->mask, NULL, path);
231    }
232 
233    if (!icon->pixmap) {
234       if (path)
235          g_free (path);
236       path = g_strconcat (default_icondir, "/", icon_name, ".xpm", NULL);
237       if (file_exists (path))
238          icon->pixmap = gdk_pixmap_colormap_create_from_xpm (NULL, sys_colormap,
239                                                              &icon->mask, NULL, path);
240    }
241 
242    g_free (path);
243 
244 #ifndef EXCLUDE_ICONS
245    if (!icon->pixmap) {
246       gint i;
247 
248       for (i = 0; i < default_icons_num; i++) {
249          if (!strcmp (icon_name, default_icons[i].name))
250             icon->pixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, sys_colormap,
251                                                                   &icon->mask, NULL,
252                                                                   default_icons[i].xpm_data);
253       }
254    }
255 #endif /* EXCLUDE_ICONS */
256 
257    if (icon->pixmap) {
258       g_hash_table_insert (icons, (gchar *) icon_name, icon);
259    } else {
260       if (icon->mask) gdk_bitmap_unref (icon->mask);
261       g_free (icon);
262       return NULL;
263    }
264 
265    return icon;
266 }
267 
268 
269 GtkWidget *
gimv_icon_stock_get_widget(const gchar * icon_name)270 gimv_icon_stock_get_widget (const gchar *icon_name)
271 {
272    GimvIcon *icon;
273    GtkWidget *widget = NULL;
274 
275    g_return_val_if_fail (icon_name, NULL);
276 
277    icon = gimv_icon_stock_get_icon (icon_name);
278 
279    if (icon)
280 #ifdef USE_GTK2
281       widget = gtk_image_new_from_pixmap (icon->pixmap, icon->mask);
282 #else
283       widget = gtk_pixmap_new (icon->pixmap, icon->mask);
284 #endif
285 
286    return widget;
287 }
288 
289 
290 void
gimv_icon_stock_change_widget_icon(GtkWidget * widget,const gchar * icon_name)291 gimv_icon_stock_change_widget_icon (GtkWidget *widget, const gchar *icon_name)
292 {
293    GimvIcon *icon;
294 
295    g_return_if_fail (widget);
296    g_return_if_fail (icon_name && *icon_name);
297 
298 #ifdef USE_GTK2
299       g_return_if_fail (GTK_IS_IMAGE (widget));
300 #else
301       g_return_if_fail (GTK_IS_PIXMAP (widget));
302 #endif
303 
304    icon = gimv_icon_stock_get_icon (icon_name);
305    if (!icon) return;
306 
307 #ifdef USE_GTK2
308    gtk_image_set_from_pixmap (GTK_IMAGE (widget), icon->pixmap, icon->mask);
309 #else
310    gtk_pixmap_set (GTK_PIXMAP (widget), icon->pixmap, icon->mask);
311 #endif
312 }
313 
314 
315 void
gimv_icon_stock_set_window_icon(GdkWindow * window,gchar * name)316 gimv_icon_stock_set_window_icon (GdkWindow *window, gchar *name)
317 {
318    GimvIcon *icon;
319 
320    icon = gimv_icon_stock_get_icon (name);
321    if (icon)
322       gdk_window_set_icon (window, NULL,
323                            icon->pixmap, icon->mask);
324 }
325 
326 
327 void
gimv_icon_stock_free_icon(const gchar * icon_name)328 gimv_icon_stock_free_icon (const gchar *icon_name)
329 {
330    GimvIcon *icon;
331 
332    g_return_if_fail (icon_name);
333 
334    icon = gimv_icon_stock_get_icon (icon_name);
335    if (!icon) return;
336 
337    g_hash_table_remove (icons, icon_name);
338    gdk_pixmap_unref (icon->pixmap);
339 #ifdef USE_GTK2
340    if (icon->pixbuf)
341       g_object_unref (icon->pixbuf);
342 #endif /* USE_GTK2 */
343    g_free (icon);
344 }
345 
346 
347 #ifdef USE_GTK2
348 GdkPixbuf *
gimv_icon_stock_get_pixbuf(const gchar * icon_name)349 gimv_icon_stock_get_pixbuf  (const gchar *icon_name)
350 {
351    GimvIcon *icon;
352    gchar *path = NULL;
353 
354    g_return_val_if_fail (icon_name, NULL);
355 
356    icon = gimv_icon_stock_get_icon (icon_name);
357    if (!icon) return NULL;
358    if (icon->pixbuf) return icon->pixbuf;
359 
360    if (icondir) {
361       path = g_strconcat (icondir, "/", icon_name, ".xpm", NULL);
362       if (file_exists (path))
363          icon->pixbuf = gdk_pixbuf_new_from_file (path, NULL);
364    }
365 
366    if (!icon->pixbuf) {
367       g_free (path);
368       path = g_strconcat (default_icondir, "/", icon_name, ".xpm", NULL);
369       if (file_exists (path))
370          icon->pixbuf = gdk_pixbuf_new_from_file (path, NULL);
371    }
372 
373    g_free (path);
374    path = NULL;
375 
376 #ifndef EXCLUDE_ICONS
377    if (!icon->pixbuf) {
378       gint i;
379 
380       for (i = 0; i < default_icons_num; i++) {
381          if (!strcmp (icon_name, default_icons[i].name))
382             icon->pixbuf = gdk_pixbuf_new_from_xpm_data (
383                (const char **)default_icons[i].xpm_data);
384       }
385    }
386 #endif /* EXCLUDE_ICONS */
387 
388    return icon->pixbuf;
389 }
390 
391 
392 void
gimv_icon_stock_free_pixbuf(const gchar * icon_name)393 gimv_icon_stock_free_pixbuf (const gchar *icon_name)
394 {
395    GimvIcon *icon;
396 
397    g_return_if_fail (icon_name);
398 
399    icon = gimv_icon_stock_get_icon (icon_name);
400    if (!icon) return;
401 
402    g_object_unref (icon->pixbuf);
403    icon->pixbuf = NULL;
404 }
405 #endif /* USE_GTK2 */
406