12 /* This is an internally used function to check if a pixmap file exists. */
13 static gchar* check_file_exists (const gchar *directory,
14 const gchar *filename);
16 /* This is an internally used function to create pixmaps. */
17 static GtkWidget* create_dummy_pixmap (GtkWidget *widget);
20 lookup_widget (GtkWidget *widget,
21 const gchar *widget_name)
23 GtkWidget *parent, *found_widget;
27 if (GTK_IS_MENU (widget))
28 parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
30 parent = widget->parent;
36 found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
39 g_warning ("Widget not found: %s", widget_name);
43 /* This is a dummy pixmap we use when a pixmap can't be found. */
44 static char *dummy_pixmap_xpm[] = {
45 /* columns rows colors chars-per-pixel */
52 /* This is an internally used function to create pixmaps. */
54 create_dummy_pixmap (GtkWidget *widget)
56 GdkColormap *colormap;
61 colormap = gtk_widget_get_colormap (widget);
62 gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
63 NULL, dummy_pixmap_xpm);
64 if (gdkpixmap == NULL)
65 g_error ("Couldn't create replacement pixmap.");
66 pixmap = gtk_pixmap_new (gdkpixmap, mask);
67 gdk_pixmap_unref (gdkpixmap);
68 gdk_bitmap_unref (mask);
72 static GList *pixmaps_directories = NULL;
74 /* Use this function to set the directory containing installed pixmaps. */
76 add_pixmap_directory (const gchar *directory)
78 pixmaps_directories = g_list_prepend (pixmaps_directories,
79 g_strdup (directory));
82 /* This is an internally used function to create pixmaps. */
84 create_pixmap (GtkWidget *widget,
85 const gchar *filename)
87 gchar *found_filename = NULL;
88 GdkColormap *colormap;
94 if (!filename || !filename[0])
95 return create_dummy_pixmap (widget);
97 /* We first try any pixmaps directories set by the application. */
98 elem = pixmaps_directories;
101 found_filename = check_file_exists ((gchar*)elem->data, filename);
107 /* If we haven't found the pixmap, try the source directory. */
110 found_filename = check_file_exists ("../pixmaps", filename);
115 g_warning ("Couldn't find pixmap file: %s", filename);
116 return create_dummy_pixmap (widget);
119 colormap = gtk_widget_get_colormap (widget);
120 gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask,
121 NULL, found_filename);
122 if (gdkpixmap == NULL)
124 g_warning ("Error loading pixmap file: %s", found_filename);
125 g_free (found_filename);
126 return create_dummy_pixmap (widget);
128 g_free (found_filename);
129 pixmap = gtk_pixmap_new (gdkpixmap, mask);
130 gdk_pixmap_unref (gdkpixmap);
131 gdk_bitmap_unref (mask);
135 /* This is an internally used function to check if a pixmap file exists. */
137 check_file_exists (const gchar *directory,
138 const gchar *filename)
140 gchar *full_filename;
144 full_filename = (gchar*) g_malloc (strlen (directory) + 1
145 + strlen (filename) + 1);
146 strcpy (full_filename, directory);
147 strcat (full_filename, G_DIR_SEPARATOR_S);
148 strcat (full_filename, filename);
150 status = stat (full_filename, &s);
151 if (status == 0 && S_ISREG (s.st_mode))
152 return full_filename;
153 g_free (full_filename);