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