http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.00.tar.gz
[xscreensaver] / driver / demo-Gtk-support.c
1 /*
2  * DO NOT EDIT THIS FILE - it is generated by Glade.
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #  include <config.h>
7 #endif
8
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <string.h>
13
14 #include <gtk/gtk.h>
15
16 #include "demo-Gtk-support.h"
17
18 /* This is an internally used function to check if a pixmap file exists. */
19 static gchar* check_file_exists        (const gchar     *directory,
20                                         const gchar     *filename);
21
22 /* This is an internally used function to create pixmaps. */
23 static GtkWidget* create_dummy_pixmap  (GtkWidget       *widget);
24
25 GtkWidget*
26 lookup_widget                          (GtkWidget       *widget,
27                                         const gchar     *widget_name)
28 {
29   GtkWidget *parent, *found_widget;
30
31   for (;;)
32     {
33       if (GTK_IS_MENU (widget))
34         parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
35       else
36         parent = widget->parent;
37       if (parent == NULL)
38         break;
39       widget = parent;
40     }
41
42   found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
43                                                    widget_name);
44   if (!found_widget)
45     g_warning ("Widget not found: %s", widget_name);
46   return found_widget;
47 }
48
49 /* This is a dummy pixmap we use when a pixmap can't be found. */
50 static char *dummy_pixmap_xpm[] = {
51 /* columns rows colors chars-per-pixel */
52 "1 1 1 1",
53 "  c None",
54 /* pixels */
55 " "
56 };
57
58 /* This is an internally used function to create pixmaps. */
59 static GtkWidget*
60 create_dummy_pixmap                    (GtkWidget       *widget)
61 {
62   GdkColormap *colormap;
63   GdkPixmap *gdkpixmap;
64   GdkBitmap *mask;
65   GtkWidget *pixmap;
66
67   colormap = gtk_widget_get_colormap (widget);
68   gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
69                                                      NULL, dummy_pixmap_xpm);
70   if (gdkpixmap == NULL)
71     g_error ("Couldn't create replacement pixmap.");
72   pixmap = gtk_pixmap_new (gdkpixmap, mask);
73   gdk_pixmap_unref (gdkpixmap);
74   gdk_bitmap_unref (mask);
75   return pixmap;
76 }
77
78 static GList *pixmaps_directories = NULL;
79
80 /* Use this function to set the directory containing installed pixmaps. */
81 void
82 add_pixmap_directory                   (const gchar     *directory)
83 {
84   pixmaps_directories = g_list_prepend (pixmaps_directories,
85                                         g_strdup (directory));
86 }
87
88 /* This is an internally used function to create pixmaps. */
89 GtkWidget*
90 create_pixmap                          (GtkWidget       *widget,
91                                         const gchar     *filename)
92 {
93   gchar *found_filename = NULL;
94   GdkColormap *colormap;
95   GdkPixmap *gdkpixmap;
96   GdkBitmap *mask;
97   GtkWidget *pixmap;
98   GList *elem;
99
100   if (!filename || !filename[0])
101       return create_dummy_pixmap (widget);
102
103   /* We first try any pixmaps directories set by the application. */
104   elem = pixmaps_directories;
105   while (elem)
106     {
107       found_filename = check_file_exists ((gchar*)elem->data, filename);
108       if (found_filename)
109         break;
110       elem = elem->next;
111     }
112
113   /* If we haven't found the pixmap, try the source directory. */
114   if (!found_filename)
115     {
116       found_filename = check_file_exists ("", filename);
117     }
118
119   if (!found_filename)
120     {
121       g_warning ("Couldn't find pixmap file: %s", filename);
122       return create_dummy_pixmap (widget);
123     }
124
125   colormap = gtk_widget_get_colormap (widget);
126   gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask,
127                                                    NULL, found_filename);
128   if (gdkpixmap == NULL)
129     {
130       g_warning ("Error loading pixmap file: %s", found_filename);
131       g_free (found_filename);
132       return create_dummy_pixmap (widget);
133     }
134   g_free (found_filename);
135   pixmap = gtk_pixmap_new (gdkpixmap, mask);
136   gdk_pixmap_unref (gdkpixmap);
137   gdk_bitmap_unref (mask);
138   return pixmap;
139 }
140
141 /* This is an internally used function to check if a pixmap file exists. */
142 gchar*
143 check_file_exists                      (const gchar     *directory,
144                                         const gchar     *filename)
145 {
146   gchar *full_filename;
147   struct stat s;
148   gint status;
149
150   full_filename = (gchar*) g_malloc (strlen (directory) + 1
151                                      + strlen (filename) + 1);
152   strcpy (full_filename, directory);
153   strcat (full_filename, G_DIR_SEPARATOR_S);
154   strcat (full_filename, filename);
155
156   status = stat (full_filename, &s);
157   if (status == 0 && S_ISREG (s.st_mode))
158     return full_filename;
159   g_free (full_filename);
160   return NULL;
161 }
162