http://www.uw-madison.lkams.kernel.org/pub/mirrors/fink/distfiles/xscreensaver-4...
[xscreensaver] / driver / demo-Gtk-support.c
1 /*
2  * DO NOT EDIT THIS FILE - it is generated by Glade.
3    WARNING: I did edit this file!  Be careful!  -jwz
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #  include <config.h>
8 #endif
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <string.h>
14
15 #include <gtk/gtk.h>
16
17 #include "demo-Gtk-support.h"
18
19 /* jwz -- dumbass Glade1 doesn't emit code that can read PNGs. this does. */
20 #ifdef HAVE_GDK_PIXBUF
21 # ifdef HAVE_GTK2
22 #  include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
23 # else  /* !HAVE_GTK2 */
24 #  include <gdk-pixbuf/gdk-pixbuf-xlib.h>
25 # endif /* !HAVE_GTK2 */
26 #endif /* HAVE_GDK_PIXBUF */
27
28
29 /* This is an internally used function to check if a pixmap file exists. */
30 static gchar* check_file_exists        (const gchar     *directory,
31                                         const gchar     *filename);
32
33 /* This is an internally used function to create pixmaps. */
34 static GtkWidget* create_dummy_pixmap  (GtkWidget       *widget);
35
36 GtkWidget*
37 lookup_widget                          (GtkWidget       *widget,
38                                         const gchar     *widget_name)
39 {
40   GtkWidget *parent, *found_widget;
41
42   for (;;)
43     {
44       if (GTK_IS_MENU (widget))
45         parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
46       else
47         parent = widget->parent;
48       if (parent == NULL)
49         break;
50       widget = parent;
51     }
52
53   found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
54                                                    widget_name);
55   if (!found_widget)
56     g_warning ("Widget not found: %s", widget_name);
57   return found_widget;
58 }
59
60 /* This is a dummy pixmap we use when a pixmap can't be found. */
61 static char *dummy_pixmap_xpm[] = {
62 /* columns rows colors chars-per-pixel */
63 "1 1 1 1",
64 "  c None",
65 /* pixels */
66 " "
67 };
68
69 /* This is an internally used function to create pixmaps. */
70 static GtkWidget*
71 create_dummy_pixmap                    (GtkWidget       *widget)
72 {
73   GdkColormap *colormap;
74   GdkPixmap *gdkpixmap;
75   GdkBitmap *mask;
76   GtkWidget *pixmap;
77
78   colormap = gtk_widget_get_colormap (widget);
79   gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
80                                                      NULL, dummy_pixmap_xpm);
81   if (gdkpixmap == NULL)
82     g_error ("Couldn't create replacement pixmap.");
83   pixmap = gtk_pixmap_new (gdkpixmap, mask);
84   gdk_pixmap_unref (gdkpixmap);
85   gdk_bitmap_unref (mask);
86   return pixmap;
87 }
88
89 static GList *pixmaps_directories = NULL;
90
91 /* Use this function to set the directory containing installed pixmaps. */
92 void
93 add_pixmap_directory                   (const gchar     *directory)
94 {
95   pixmaps_directories = g_list_prepend (pixmaps_directories,
96                                         g_strdup (directory));
97 }
98
99 /* This is an internally used function to create pixmaps. */
100 /* #### Warning: this version of this function hacked by jwz to
101    support PNGs.  Don't let Glade1 overwrite this!
102  */
103 GtkWidget*
104 create_pixmap                          (GtkWidget       *widget,
105                                         const gchar     *filename)
106 {
107   gchar *found_filename = NULL;
108   GdkColormap *colormap = 0;
109   GdkPixmap *gdkpixmap = 0;
110   GdkBitmap *mask = 0;
111   GtkWidget *pixmap = 0;
112   GList *elem = 0;
113
114   if (!filename || !filename[0])
115       return create_dummy_pixmap (widget);
116
117   /* We first try any pixmaps directories set by the application. */
118   elem = pixmaps_directories;
119   while (elem)
120     {
121       found_filename = check_file_exists ((gchar*)elem->data, filename);
122       if (found_filename)
123         break;
124       elem = elem->next;
125     }
126
127   /* If we haven't found the pixmap, try the source directory. */
128   if (!found_filename)
129     {
130       found_filename = check_file_exists ("../utils/images", filename);
131     }
132
133   if (!found_filename)
134     {
135       g_warning (_("Couldn't find pixmap file: %s"), filename);
136       return create_dummy_pixmap (widget);
137     }
138
139   colormap = gtk_widget_get_colormap (widget);
140
141 # ifndef HAVE_GDK_PIXBUF
142
143   gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask,
144                                                    NULL, found_filename);
145   if (gdkpixmap == NULL)
146     {
147       g_warning (_("Error loading pixmap file: %s"), found_filename);
148       g_free (found_filename);
149       return create_dummy_pixmap (widget);
150     }
151
152 # else /* HAVE_GDK_PIXBUF */
153
154   /* jwz -- dumbass Glade1 doesn't emit code that can read PNGs.
155      This code does... */
156
157   /* #### Danger: we aren't using `colormap'... */
158
159   {
160     GdkPixbuf *pb;
161 #  ifdef HAVE_GTK2
162     GError *gerr = 0;
163 #  endif /* HAVE_GTK2 */
164
165     pb = gdk_pixbuf_new_from_file (found_filename
166 #  ifdef HAVE_GTK2
167                                    , &gerr
168 #  endif /* HAVE_GTK2 */
169                                    );
170
171     if (pb)
172       {
173         gdkpixmap = 0;
174         mask = 0;
175         gdk_pixbuf_render_pixmap_and_mask (pb, &gdkpixmap, &mask, 128);
176       }
177     else
178       {
179         g_warning (_("Error loading pixmap file: %s"), found_filename);
180 #  ifdef HAVE_GTK2
181         if (gerr && gerr->message && *gerr->message)
182           g_warning (_("reason: %s\n"), gerr->message);
183 #  endif /* HAVE_GTK2 */
184
185         return create_dummy_pixmap (widget);
186       }
187   }
188 # endif /* HAVE_GDK_PIXBUF */
189
190   g_free (found_filename);
191   pixmap = gtk_pixmap_new (gdkpixmap, mask);
192   gdk_pixmap_unref (gdkpixmap);
193   gdk_bitmap_unref (mask);
194
195   return pixmap;
196 }
197
198 /* This is an internally used function to check if a pixmap file exists. */
199 static gchar*
200 check_file_exists                      (const gchar     *directory,
201                                         const gchar     *filename)
202 {
203   gchar *full_filename;
204   struct stat s;
205   gint status;
206
207   full_filename = (gchar*) g_malloc (strlen (directory) + 1
208                                      + strlen (filename) + 1);
209   strcpy (full_filename, directory);
210   strcat (full_filename, G_DIR_SEPARATOR_S);
211   strcat (full_filename, filename);
212
213   status = stat (full_filename, &s);
214   if (status == 0 && S_ISREG (s.st_mode))
215     return full_filename;
216   g_free (full_filename);
217   return NULL;
218 }
219