1 /*
2 
3     Euchre - a free as in freedom and as in beer version of the
4              euchre card game
5 
6     Copyright 2002 C Nathan Buckles (nbuckles@bigfoot.com)
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <string.h>
32 
33 #include <gtk/gtk.h>
34 
35 #include "gui/support.hpp"
36 
37 /* This is an internally used function to create pixmaps. */
38 static GtkWidget* create_dummy_pixmap  (GtkWidget       *widget);
39 
40 GtkWidget*
lookup_widget(GtkWidget * widget,const gchar * widget_name)41 lookup_widget                          (GtkWidget       *widget,
42                                         const gchar     *widget_name)
43 {
44   GtkWidget *parent, *found_widget;
45 
46   for (;;)
47     {
48       if (GTK_IS_MENU (widget))
49         parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
50       else
51         parent = widget->parent;
52       if (parent == NULL)
53         break;
54       widget = parent;
55     }
56 
57   found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
58                                                    widget_name);
59   if (!found_widget)
60     g_warning ("Widget not found: %s", widget_name);
61   return found_widget;
62 }
63 
64 /* This is a dummy pixmap we use when a pixmap can't be found. */
65 static const char *dummy_pixmap_xpm[] = {
66 /* columns rows colors chars-per-pixel */
67 "1 1 1 1",
68 "  c None",
69 /* pixels */
70 " ",
71 " "
72 };
73 
74 /* This is an internally used function to create pixmaps. */
75 static GtkWidget*
create_dummy_pixmap(GtkWidget * widget)76 create_dummy_pixmap                    (GtkWidget       *widget)
77 {
78     return create_pixmap_d(widget, (gchar**) dummy_pixmap_xpm);
79 }
80 
81 /* This is an internally used function to create pixmaps. */
82 GtkWidget*
create_pixmap(GtkWidget * widget,const gchar * filename)83 create_pixmap                          (GtkWidget       *widget,
84                                         const gchar     *filename)
85 {
86   GtkWidget *pixmap;
87   GdkColormap *colormap;
88   GdkPixmap *gdkpixmap;
89   GdkBitmap *mask;
90   gchar *pathname = NULL;
91 
92   if (!filename || !filename[0])
93     return create_dummy_pixmap (widget);
94 
95   colormap = gtk_widget_get_colormap (widget);
96   gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask,
97                                                    NULL, pathname);
98   if (gdkpixmap == NULL)
99     {
100       g_warning (("Couldn't create pixmap from file: %s"), pathname);
101       g_free (pathname);
102       return create_dummy_pixmap (widget);
103     }
104   g_free (pathname);
105 
106   pixmap = gtk_pixmap_new (gdkpixmap, mask);
107   gdk_pixmap_unref (gdkpixmap);
108   gdk_bitmap_unref (mask);
109   return pixmap;
110 }
111 
112 /* This is an internally used function to create pixmaps from data */
113 GtkWidget*
create_pixmap_d(GtkWidget * widget,gchar ** xpm_d)114 create_pixmap_d                        (GtkWidget       *widget,
115 					gchar          **xpm_d)
116 {
117   GdkColormap *colormap;
118   GdkPixmap *gdkpixmap;
119   GdkBitmap *mask;
120   GtkWidget *pixmap;
121 
122   colormap = gtk_widget_get_colormap (widget);
123   gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
124                                                      NULL, xpm_d);
125   if (gdkpixmap == NULL)
126     g_error ("Couldn't create replacement pixmap.");
127   pixmap = gtk_pixmap_new (gdkpixmap, mask);
128   gdk_pixmap_unref (gdkpixmap);
129   gdk_bitmap_unref (mask);
130   return pixmap;
131 }
132