1 /*
2 * support.cxx
3 * Andrew Sayman - 10/11/04
4 *
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (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 * Daniel Nelson - aluminumangel.org
23 * 174 W. 18th Ave.
24 * Columbus, OH 43210
25 */
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <stdio.h>
35
36 #include <gtk/gtk.h>
37
38 #include "support.h"
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)
53 parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget), "GladeParentKey");
54 if (parent == NULL)
55 break;
56 widget = parent;
57 }
58
59 found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
60 widget_name);
61 if (!found_widget)
62 g_warning ("Widget not found: %s", widget_name);
63 return found_widget;
64 }
65
66 static GList *pixmaps_directories = NULL;
67
68 /* Use this function to set the directory containing installed pixmaps. */
69 void
add_pixmap_directory(const gchar * directory)70 add_pixmap_directory (const gchar *directory)
71 {
72 pixmaps_directories = g_list_prepend (pixmaps_directories,
73 g_strdup (directory));
74 }
75
76 /* This is an internally used function to find pixmap files. */
77 gchar*
find_pixmap_file(const gchar * filename)78 find_pixmap_file (const gchar *filename)
79 {
80 GList *elem;
81
82 /* We step through each of the pixmaps directory to find it. */
83 elem = pixmaps_directories;
84 while (elem)
85 {
86 gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data,
87 G_DIR_SEPARATOR_S, filename);
88 if (g_file_test (pathname, G_FILE_TEST_EXISTS))
89 return pathname;
90 g_free (pathname);
91 elem = elem->next;
92 }
93 return NULL;
94 }
95
96 /* This is an internally used function to create pixmaps. */
97 GtkWidget*
create_pixmap(GtkWidget * widget,const gchar * filename)98 create_pixmap (GtkWidget *widget,
99 const gchar *filename)
100 {
101 gchar *pathname = NULL;
102 GtkWidget *pixmap;
103
104 if (!filename || !filename[0])
105 return gtk_image_new ();
106
107 pathname = find_pixmap_file (filename);
108
109 if (!pathname)
110 {
111 g_warning (("Couldn't find pixmap file: %s"), filename);
112 return gtk_image_new ();
113 }
114
115 pixmap = gtk_image_new_from_file (pathname);
116 g_free (pathname);
117 return pixmap;
118 }
119
120 /* This is an internally used function to create pixmaps. */
121 GdkPixbuf*
create_pixbuf(const gchar * filename)122 create_pixbuf (const gchar *filename)
123 {
124 gchar *pathname = NULL;
125 GdkPixbuf *pixbuf;
126 GError *error = NULL;
127
128 if (!filename || !filename[0])
129 return NULL;
130
131 pathname = find_pixmap_file (filename);
132
133 if (!pathname)
134 {
135 g_warning (_("Couldn't find pixmap file: %s"), filename);
136 return NULL;
137 }
138
139 pixbuf = gdk_pixbuf_new_from_file (pathname, &error);
140 if (!pixbuf)
141 {
142 fprintf (stderr, "Failed to load pixbuf file: %s: %s\n",
143 pathname, error->message);
144 g_error_free (error);
145 }
146 g_free (pathname);
147 return pixbuf;
148 }
149
150 /* This is used to set ATK action descriptions. */
151 void
glade_set_atk_action_description(AtkAction * action,const gchar * action_name,const gchar * description)152 glade_set_atk_action_description (AtkAction *action,
153 const gchar *action_name,
154 const gchar *description)
155 {
156 gint n_actions, i;
157
158 n_actions = atk_action_get_n_actions (action);
159 for (i = 0; i < n_actions; i++)
160 {
161 if (!strcmp (atk_action_get_name (action, i), action_name))
162 atk_action_set_description (action, i, description);
163 }
164 }
165
166