aboutsummaryrefslogtreecommitdiff
path: root/src/support.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/support.c')
-rw-r--r--src/support.c160
1 files changed, 71 insertions, 89 deletions
diff --git a/src/support.c b/src/support.c
index ad5ec442..00aff298 100644
--- a/src/support.c
+++ b/src/support.c
@@ -10,18 +10,12 @@
10#include <sys/stat.h> 10#include <sys/stat.h>
11#include <unistd.h> 11#include <unistd.h>
12#include <string.h> 12#include <string.h>
13#include <stdio.h>
13 14
14#include <gtk/gtk.h> 15#include <gtk/gtk.h>
15 16
16#include "support.h" 17#include "support.h"
17 18
18/* This is an internally used function to check if a pixmap file exists. */
19static gchar* check_file_exists (const gchar *directory,
20 const gchar *filename);
21
22/* This is an internally used function to create pixmaps. */
23static GtkWidget* create_dummy_pixmap (GtkWidget *widget);
24
25GtkWidget* 19GtkWidget*
26lookup_widget (GtkWidget *widget, 20lookup_widget (GtkWidget *widget,
27 const gchar *widget_name) 21 const gchar *widget_name)
@@ -34,47 +28,20 @@ lookup_widget (GtkWidget *widget,
34 parent = gtk_menu_get_attach_widget (GTK_MENU (widget)); 28 parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
35 else 29 else
36 parent = widget->parent; 30 parent = widget->parent;
31 if (!parent)
32 parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget), "GladeParentKey");
37 if (parent == NULL) 33 if (parent == NULL)
38 break; 34 break;
39 widget = parent; 35 widget = parent;
40 } 36 }
41 37
42 found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget), 38 found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
43 widget_name); 39 widget_name);
44 if (!found_widget) 40 if (!found_widget)
45 g_warning ("Widget not found: %s", widget_name); 41 g_warning ("Widget not found: %s", widget_name);
46 return found_widget; 42 return found_widget;
47} 43}
48 44
49/* This is a dummy pixmap we use when a pixmap can't be found. */
50static 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. */
59static GtkWidget*
60create_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
78static GList *pixmaps_directories = NULL; 45static GList *pixmaps_directories = NULL;
79 46
80/* Use this function to set the directory containing installed pixmaps. */ 47/* Use this function to set the directory containing installed pixmaps. */
@@ -85,78 +52,93 @@ add_pixmap_directory (const gchar *directory)
85 g_strdup (directory)); 52 g_strdup (directory));
86} 53}
87 54
55/* This is an internally used function to find pixmap files. */
56static gchar*
57find_pixmap_file (const gchar *filename)
58{
59 GList *elem;
60
61 /* We step through each of the pixmaps directory to find it. */
62 elem = pixmaps_directories;
63 while (elem)
64 {
65 gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data,
66 G_DIR_SEPARATOR_S, filename);
67 if (g_file_test (pathname, G_FILE_TEST_EXISTS))
68 return pathname;
69 g_free (pathname);
70 elem = elem->next;
71 }
72 return NULL;
73}
74
88/* This is an internally used function to create pixmaps. */ 75/* This is an internally used function to create pixmaps. */
89GtkWidget* 76GtkWidget*
90create_pixmap (GtkWidget *widget, 77create_pixmap (GtkWidget *widget,
91 const gchar *filename) 78 const gchar *filename)
92{ 79{
93 gchar *found_filename = NULL; 80 gchar *pathname = NULL;
94 GdkColormap *colormap;
95 GdkPixmap *gdkpixmap;
96 GdkBitmap *mask;
97 GtkWidget *pixmap; 81 GtkWidget *pixmap;
98 GList *elem;
99 82
100 if (!filename || !filename[0]) 83 if (!filename || !filename[0])
101 return create_dummy_pixmap (widget); 84 return gtk_image_new ();
102 85
103 /* We first try any pixmaps directories set by the application. */ 86 pathname = find_pixmap_file (filename);
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 87
113 /* If we haven't found the pixmap, try the source directory. */ 88 if (!pathname)
114 if (!found_filename)
115 { 89 {
116 found_filename = check_file_exists ("../pixmaps", filename); 90 g_warning (_("Couldn't find pixmap file: %s"), filename);
91 return gtk_image_new ();
117 } 92 }
118 93
119 if (!found_filename) 94 pixmap = gtk_image_new_from_file (pathname);
95 g_free (pathname);
96 return pixmap;
97}
98
99/* This is an internally used function to create pixmaps. */
100GdkPixbuf*
101create_pixbuf (const gchar *filename)
102{
103 gchar *pathname = NULL;
104 GdkPixbuf *pixbuf;
105 GError *error = NULL;
106
107 if (!filename || !filename[0])
108 return NULL;
109
110 pathname = find_pixmap_file (filename);
111
112 if (!pathname)
120 { 113 {
121 g_warning (_("Couldn't find pixmap file: %s"), filename); 114 g_warning (_("Couldn't find pixmap file: %s"), filename);
122 return create_dummy_pixmap (widget); 115 return NULL;
123 } 116 }
124 117
125 colormap = gtk_widget_get_colormap (widget); 118 pixbuf = gdk_pixbuf_new_from_file (pathname, &error);
126 gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask, 119 if (!pixbuf)
127 NULL, found_filename);
128 if (gdkpixmap == NULL)
129 { 120 {
130 g_warning (_("Error loading pixmap file: %s"), found_filename); 121 fprintf (stderr, "Failed to load pixbuf file: %s: %s\n",
131 g_free (found_filename); 122 pathname, error->message);
132 return create_dummy_pixmap (widget); 123 g_error_free (error);
133 } 124 }
134 g_free (found_filename); 125 g_free (pathname);
135 pixmap = gtk_pixmap_new (gdkpixmap, mask); 126 return pixbuf;
136 gdk_pixmap_unref (gdkpixmap);
137 gdk_bitmap_unref (mask);
138 return pixmap;
139} 127}
140 128
141/* This is an internally used function to check if a pixmap file exists. */ 129/* This is used to set ATK action descriptions. */
142static gchar* 130void
143check_file_exists (const gchar *directory, 131glade_set_atk_action_description (AtkAction *action,
144 const gchar *filename) 132 const gchar *action_name,
133 const gchar *description)
145{ 134{
146 gchar *full_filename; 135 gint n_actions, i;
147 struct stat s; 136
148 gint status; 137 n_actions = atk_action_get_n_actions (action);
149 138 for (i = 0; i < n_actions; i++)
150 full_filename = (gchar*) g_malloc (strlen (directory) + 1 139 {
151 + strlen (filename) + 1); 140 if (!strcmp (atk_action_get_name (action, i), action_name))
152 strcpy (full_filename, directory); 141 atk_action_set_description (action, i, description);
153 strcat (full_filename, G_DIR_SEPARATOR_S); 142 }
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} 143}
162 144