1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * resources.h
4 * Copyright (C) Naba Kumar <naba@gnome.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * SECTION:resources
23 * @title: Program resources
24 * @short_description: Application resource management
25 * @see_also:
26 * @stability: Unstable
27 * @include: libanjuta/resources.h
28 *
29 */
30
31 #ifdef HAVE_CONFIG_H
32 # include <config.h>
33 #endif
34
35 #include <unistd.h>
36 #include <glib/gi18n.h>
37 #include <gtk/gtk.h>
38
39 #include <libanjuta/anjuta-utils.h>
40 #include <libanjuta/resources.h>
41
42 GtkWidget *
anjuta_res_lookup_widget(GtkWidget * widget,const gchar * widget_name)43 anjuta_res_lookup_widget (GtkWidget * widget, const gchar * widget_name)
44 {
45 GtkWidget *parent, *found_widget;
46
47 for (;;)
48 {
49 if (GTK_IS_MENU (widget))
50 parent =
51 gtk_menu_get_attach_widget (GTK_MENU
52 (widget));
53 else
54 parent = gtk_widget_get_parent (widget);
55 if (parent == NULL)
56 break;
57 widget = parent;
58 }
59
60 found_widget = (GtkWidget *) g_object_get_data (G_OBJECT (widget),
61 widget_name);
62 if (!found_widget)
63 g_warning (_("Widget not found: %s"), widget_name);
64 return found_widget;
65 }
66
67 GtkWidget *
anjuta_res_get_image(const gchar * pixfile)68 anjuta_res_get_image (const gchar * pixfile)
69 {
70 GtkWidget *pixmap;
71 gchar *pathname;
72
73 if (!pixfile || !pixfile[0])
74 return gtk_image_new ();
75
76 pathname = anjuta_res_get_pixmap_file (pixfile);
77 if (!pathname)
78 {
79 g_warning (_("Could not find application pixmap file: %s"),
80 pixfile);
81 return gtk_image_new ();
82 }
83 pixmap = gtk_image_new_from_file (pathname);
84 g_free (pathname);
85 return pixmap;
86 }
87
88 GtkWidget *
anjuta_res_get_image_sized(const gchar * pixfile,gint width,gint height)89 anjuta_res_get_image_sized (const gchar * pixfile, gint width, gint height)
90 {
91 GtkWidget *pixmap;
92 GdkPixbuf *pixbuf;
93 gchar *pathname;
94
95 if (!pixfile || !pixfile[0])
96 return gtk_image_new ();
97
98 pathname = anjuta_res_get_pixmap_file (pixfile);
99 if (!pathname)
100 {
101 g_warning (_("Could not find application pixmap file: %s"),
102 pixfile);
103 return gtk_image_new ();
104 }
105 pixbuf = gdk_pixbuf_new_from_file_at_size (pathname, width, height, NULL);
106 pixmap = gtk_image_new_from_pixbuf (pixbuf);
107 g_object_unref (pixbuf);
108 g_free (pathname);
109 return pixmap;
110 }
111
112 /* All the return strings MUST be freed */
113 gchar *
anjuta_res_get_pixmap_dir()114 anjuta_res_get_pixmap_dir ()
115 {
116 gchar* path;
117 path = g_strdup (PACKAGE_PIXMAPS_DIR);
118 if (g_file_test (path, G_FILE_TEST_IS_DIR))
119 return path;
120 g_free (path);
121 return NULL;
122 }
123
124 gchar *
anjuta_res_get_data_dir()125 anjuta_res_get_data_dir ()
126 {
127 gchar* path;
128 path = g_strdup (PACKAGE_DATA_DIR);
129 if (g_file_test (path, G_FILE_TEST_IS_DIR))
130 return path;
131 g_free (path);
132 return NULL;
133 }
134
135 gchar *
anjuta_res_get_help_dir()136 anjuta_res_get_help_dir ()
137 {
138 gchar* path;
139 path = g_strdup (PACKAGE_HELP_DIR);
140 if (g_file_test (path, G_FILE_TEST_IS_DIR))
141 return path;
142 g_free (path);
143 return NULL;
144 }
145
146 gchar *
anjuta_res_get_help_dir_locale(const gchar * locale)147 anjuta_res_get_help_dir_locale (const gchar * locale)
148 {
149 gchar* path;
150 if (locale)
151 path = g_strconcat (PACKAGE_HELP_DIR, "/", locale, NULL);
152 else
153 path = g_strdup (PACKAGE_HELP_DIR);
154 if (g_file_test (path, G_FILE_TEST_IS_DIR))
155 return path;
156 g_free (path);
157 return NULL;
158 }
159
160 gchar *
anjuta_res_get_doc_dir()161 anjuta_res_get_doc_dir ()
162 {
163 gchar* path;
164 path = g_strdup (PACKAGE_DOC_DIR);
165 if (g_file_test (path, G_FILE_TEST_IS_DIR))
166 return path;
167 g_free (path);
168 return NULL;
169 }
170
171 /* All the return strings MUST be freed */
172 gchar *
anjuta_res_get_pixmap_file(const gchar * pixfile)173 anjuta_res_get_pixmap_file (const gchar * pixfile)
174 {
175 gchar* path;
176 g_return_val_if_fail (pixfile != NULL, NULL);
177 path = g_strconcat (PACKAGE_PIXMAPS_DIR, "/", pixfile, NULL);
178 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
179 return path;
180 g_warning ("Pixmap file not found: %s", path);
181 g_free (path);
182 return NULL;
183 }
184
185 gchar *
anjuta_res_get_data_file(const gchar * datafile)186 anjuta_res_get_data_file (const gchar * datafile)
187 {
188 gchar* path;
189 g_return_val_if_fail (datafile != NULL, NULL);
190 path = g_strconcat (PACKAGE_DATA_DIR, "/", datafile, NULL);
191 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
192 return path;
193 g_free (path);
194 return NULL;
195 }
196
197 gchar *
anjuta_res_get_help_file(const gchar * helpfile)198 anjuta_res_get_help_file (const gchar * helpfile)
199 {
200 gchar* path;
201 g_return_val_if_fail (helpfile != NULL, NULL);
202 path = g_strconcat (PACKAGE_HELP_DIR, "/", helpfile, NULL);
203 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
204 return path;
205 g_free (path);
206 return NULL;
207 }
208
209 gchar *
anjuta_res_get_help_file_locale(const gchar * helpfile,const gchar * locale)210 anjuta_res_get_help_file_locale (const gchar * helpfile, const gchar * locale)
211 {
212 gchar* path;
213 g_return_val_if_fail (helpfile != NULL, NULL);
214 if (locale)
215 path = g_strconcat (PACKAGE_HELP_DIR, "/", locale, "/",
216 helpfile, NULL);
217 else
218 path = g_strconcat (PACKAGE_HELP_DIR, "/", helpfile, NULL);
219 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
220 return path;
221 g_free (path);
222 return NULL;
223 }
224
225 gchar *
anjuta_res_get_doc_file(const gchar * docfile)226 anjuta_res_get_doc_file (const gchar * docfile)
227 {
228 gchar* path;
229 g_return_val_if_fail (docfile != NULL, NULL);
230 path = g_strconcat (PACKAGE_DOC_DIR, "/", docfile, NULL);
231 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
232 return path;
233 g_free (path);
234 return NULL;
235 }
236
237 #if 0
238 /* File type icons 16x16 */
239 GdkPixbuf *
240 anjuta_res_get_icon_for_file (PropsID props, const gchar *filename)
241 {
242 gchar *value;
243 GdkPixbuf *pixbuf;
244 gchar *file;
245
246 g_return_val_if_fail (filename != NULL, NULL);
247 file = g_path_get_basename (filename);
248 value = prop_get_new_expand (props, "icon.", file);
249 if (value == NULL)
250 value = g_strdup ("file_text.png");
251 pixbuf = anjuta_res_get_pixbuf (value);
252 g_free (value);
253 g_free (file);
254 return pixbuf;
255 }
256 #endif
257
258 void
anjuta_res_help_search(const gchar * word)259 anjuta_res_help_search (const gchar * word)
260 {
261 GError *error = NULL;
262 gchar **argv = g_new0 (gchar *, 4);
263
264 argv[0] = g_strdup ("devhelp");
265
266 if(word)
267 {
268 argv[1] = g_strdup ("-s");
269 argv[2] = g_strdup (word);
270
271 fprintf(stderr, "Word is %s\n", word);
272 }
273
274 if (g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
275 NULL, NULL, NULL, &error))
276 {
277 g_warning (_("Cannot execute command \"%s\": %s"), "devhelp", error->message);
278 g_error_free (error);
279 }
280
281 g_strfreev (argv);
282 }
283
284 void
anjuta_res_url_show(const gchar * url)285 anjuta_res_url_show (const gchar *url)
286 {
287 gchar *open[3];
288
289 if (!anjuta_util_prog_is_installed ("xdg-open", TRUE))
290 return;
291
292 open[0] = "xdg-open";
293 open[1] = (gchar *)url;
294 open[2] = NULL;
295
296 g_spawn_async (NULL, open, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
297 }
298