00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <unistd.h>
00032 #include <string.h>
00033
00034 #include <gtk/gtk.h>
00035
00036 #include "support.h"
00037
00038
00039 static gchar* check_file_exists (const gchar *directory,
00040 const gchar *filename);
00041
00042
00043 static GtkWidget* create_dummy_pixmap (GtkWidget *widget);
00044
00045 GtkWidget*
00046 lookup_widget (GtkWidget *widget,
00047 const gchar *widget_name)
00048 {
00049 GtkWidget *parent, *found_widget;
00050
00051 for (;;)
00052 {
00053 if (GTK_IS_MENU (widget))
00054 parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
00055 else
00056 parent = widget->parent;
00057 if (parent == NULL)
00058 break;
00059 widget = parent;
00060 }
00061
00062 found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
00063 widget_name);
00064 if (!found_widget)
00065 g_warning ("Widget not found: %s", widget_name);
00066 return found_widget;
00067 }
00068
00069
00070 static char *dummy_pixmap_xpm[] = {
00071
00072 "1 1 1 1",
00073 " c None",
00074
00075 " "
00076 };
00077
00078
00079 static GtkWidget*
00080 create_dummy_pixmap (GtkWidget *widget)
00081 {
00082 GdkColormap *colormap;
00083 GdkPixmap *gdkpixmap;
00084 GdkBitmap *mask;
00085 GtkWidget *pixmap;
00086
00087 colormap = gtk_widget_get_colormap (widget);
00088 gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
00089 NULL, dummy_pixmap_xpm);
00090 if (gdkpixmap == NULL)
00091 g_error ("Couldn't create replacement pixmap.");
00092 pixmap = gtk_pixmap_new (gdkpixmap, mask);
00093 gdk_pixmap_unref (gdkpixmap);
00094 gdk_bitmap_unref (mask);
00095 return pixmap;
00096 }
00097
00098 static GList *pixmaps_directories = NULL;
00099
00100
00101 void
00102 add_pixmap_directory (const gchar *directory)
00103 {
00104 pixmaps_directories = g_list_prepend (pixmaps_directories,
00105 g_strdup (directory));
00106 }
00107
00108
00109 GtkWidget*
00110 create_pixmap (GtkWidget *widget,
00111 const gchar *filename)
00112 {
00113 gchar *found_filename = NULL;
00114 GdkColormap *colormap;
00115 GdkPixmap *gdkpixmap;
00116 GdkBitmap *mask;
00117 GtkWidget *pixmap;
00118 GList *elem;
00119
00120 if (!filename || !filename[0])
00121 return create_dummy_pixmap (widget);
00122
00123
00124 elem = pixmaps_directories;
00125 while (elem)
00126 {
00127 found_filename = check_file_exists ((gchar*)elem->data, filename);
00128 if (found_filename)
00129 break;
00130 elem = elem->next;
00131 }
00132
00133
00134 if (!found_filename)
00135 {
00136 found_filename = check_file_exists ("../pixmaps", filename);
00137 }
00138
00139 if (!found_filename)
00140 {
00141 g_warning ("Couldn't find pixmap file: %s", filename);
00142 return create_dummy_pixmap (widget);
00143 }
00144
00145 colormap = gtk_widget_get_colormap (widget);
00146 gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask,
00147 NULL, found_filename);
00148 if (gdkpixmap == NULL)
00149 {
00150 g_warning ("Error loading pixmap file: %s", found_filename);
00151 g_free (found_filename);
00152 return create_dummy_pixmap (widget);
00153 }
00154 g_free (found_filename);
00155 pixmap = gtk_pixmap_new (gdkpixmap, mask);
00156 gdk_pixmap_unref (gdkpixmap);
00157 gdk_bitmap_unref (mask);
00158 return pixmap;
00159 }
00160
00161
00162 static gchar*
00163 check_file_exists (const gchar *directory,
00164 const gchar *filename)
00165 {
00166 gchar *full_filename;
00167 struct stat s;
00168 gint status;
00169
00170 full_filename = (gchar*) g_malloc (strlen (directory) + 1
00171 + strlen (filename) + 1);
00172 strcpy (full_filename, directory);
00173 strcat (full_filename, G_DIR_SEPARATOR_S);
00174 strcat (full_filename, filename);
00175
00176 status = stat (full_filename, &s);
00177 if (status == 0 && S_ISREG (s.st_mode))
00178 return full_filename;
00179 g_free (full_filename);
00180 return NULL;
00181 }
00182