~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to ve/glade-helper.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Glade helper routines
 
2
 *
 
3
 * Author: George Lebl
 
4
 * (c) 2000 Eazel, Inc.
 
5
 * (c) 2002 George Lebl
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with this library; if not, write to the
 
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
#include <config.h>
 
24
 
 
25
#include <stdlib.h>
 
26
#include <gtk/gtk.h>
 
27
#include <glade/glade.h>
 
28
 
 
29
#include "ve-i18n.h"
 
30
 
 
31
#include "ve-misc.h"
 
32
#include "ve-miscui.h"
 
33
 
 
34
#include "glade-helper.h"
 
35
 
 
36
static void glade_helper_no_interface (const char *filename);
 
37
static void glade_helper_bad_interface (const char *filename,
 
38
                                        const char *widget,
 
39
                                        GType type);
 
40
static void glade_helper_bad_columns (const char *filename,
 
41
                                      const char *widget,
 
42
                                      GType type,
 
43
                                      int columns);
 
44
 
 
45
static GList *ve_glade_directories = NULL;
 
46
static gboolean ve_search_gnome_dirs = TRUE;
 
47
 
 
48
GladeXML *
 
49
glade_helper_load (const char *file, const char *widget,
 
50
                   GType expected_type,
 
51
                   gboolean dump_on_destroy)
 
52
{
 
53
        char *f;
 
54
        GladeXML *xml;
 
55
        GtkWidget *w;
 
56
 
 
57
        g_return_val_if_fail (file != NULL, NULL);
 
58
 
 
59
        f = glade_helper_find_glade_file (file);
 
60
 
 
61
        if (f == NULL) {
 
62
                glade_helper_no_interface (file);
 
63
                exit (1);
 
64
        }
 
65
 
 
66
        /* FIXME: eek use of PACKAGE */
 
67
        xml = glade_xml_new (f, widget, GETTEXT_PACKAGE);
 
68
 
 
69
        g_free (f);
 
70
 
 
71
        /* in case we can't load the interface, bail */
 
72
        if (xml == NULL) {
 
73
                glade_helper_no_interface (file);
 
74
                exit (1);
 
75
        }
 
76
 
 
77
        w = glade_xml_get_widget (xml, widget);
 
78
        if (w == NULL ||
 
79
            ! G_TYPE_CHECK_INSTANCE_TYPE (w, expected_type)) {
 
80
                glade_helper_bad_interface (xml->filename != NULL ?
 
81
                                              xml->filename :
 
82
                                              _("(memory buffer)"),
 
83
                                            widget,
 
84
                                            expected_type);
 
85
                exit (1);
 
86
        }
 
87
 
 
88
        if (dump_on_destroy) {
 
89
                g_object_set_data_full (G_OBJECT (w),
 
90
                                        "GladeXML",
 
91
                                        xml,
 
92
                                        (GtkDestroyNotify) g_object_unref);
 
93
        }
 
94
 
 
95
        return xml;
 
96
}
 
97
 
 
98
GtkWidget *
 
99
glade_helper_load_widget (const char *file, const char *widget,
 
100
                          GType expected_type)
 
101
{
 
102
        GladeXML *xml;
 
103
        GtkWidget *w;
 
104
 
 
105
        /* this is guaranteed to return non-NULL, otherwise we
 
106
         * would have exited */
 
107
        xml = glade_helper_load (file, widget, expected_type, FALSE);
 
108
 
 
109
        w = glade_xml_get_widget (xml, widget);
 
110
        if (w == NULL ||
 
111
            ! G_TYPE_CHECK_INSTANCE_TYPE (w, expected_type)) {
 
112
                glade_helper_bad_interface (xml->filename != NULL ?
 
113
                                              xml->filename :
 
114
                                              _("(memory buffer)"),
 
115
                                            widget,
 
116
                                            expected_type);
 
117
                exit (1);
 
118
        }
 
119
 
 
120
        g_object_unref (G_OBJECT (xml));
 
121
        
 
122
        return w;
 
123
}
 
124
 
 
125
GtkWidget *
 
126
glade_helper_get (GladeXML *xml, const char *widget, GType expected_type)
 
127
{
 
128
        GtkWidget *w;
 
129
 
 
130
        w = glade_xml_get_widget (xml, widget);
 
131
 
 
132
        if (w == NULL ||
 
133
            ! G_TYPE_CHECK_INSTANCE_TYPE (w, expected_type)) {
 
134
                glade_helper_bad_interface (xml->filename != NULL ?
 
135
                                              xml->filename :
 
136
                                              _("(memory buffer)"),
 
137
                                            widget,
 
138
                                            expected_type);
 
139
                exit (1);
 
140
        }
 
141
 
 
142
        return w;
 
143
}
 
144
 
 
145
static void
 
146
glade_helper_bad_interface (const char *filename, const char *widget,
 
147
                            GType type)
 
148
{
 
149
        GtkWidget *dlg;
 
150
        char *typestring;
 
151
        const char *typename;
 
152
        char *tmp;
 
153
 
 
154
        if (type != 0)
 
155
                typename = g_type_name (type);
 
156
        else
 
157
                typename = NULL;
 
158
 
 
159
        if (typename == NULL)
 
160
                typestring = g_strdup_printf (" (%s)", typename);
 
161
        else
 
162
                typestring = g_strdup ("");
 
163
 
 
164
        tmp = ve_filename_to_utf8 (filename);
 
165
        
 
166
        dlg = ve_hig_dialog_new (NULL /* parent */,
 
167
                                 0 /* flags */,
 
168
                                 GTK_MESSAGE_ERROR,
 
169
                                 GTK_BUTTONS_OK,
 
170
                                 FALSE /* markup */,
 
171
                                 _("Cannot load user interface"),
 
172
                                 _("An error occurred while loading user "
 
173
                                   "interface element %s%s from file %s.  "
 
174
                                   "Possibly the glade interface description was "
 
175
                                   "corrupted.  "
 
176
                                   "%s cannot continue and will exit now.  "
 
177
                                   "You should check your "
 
178
                                   "installation of %s or reinstall %s."),
 
179
                                 widget,
 
180
                                 typestring,
 
181
                                 tmp,
 
182
                                 PACKAGE,
 
183
                                 PACKAGE,
 
184
                                 PACKAGE);
 
185
 
 
186
        g_free (typestring);
 
187
 
 
188
        g_warning (_("Glade file is on crack! Make sure the correct "
 
189
                     "file is installed!\nfile: %s widget: %s"),
 
190
                   tmp, widget);
 
191
 
 
192
        g_free (tmp);
 
193
 
 
194
        gtk_dialog_run (GTK_DIALOG (dlg));
 
195
        gtk_widget_destroy (dlg);
 
196
}
 
197
 
 
198
static void
 
199
glade_helper_bad_columns (const char *filename, const char *widget,
 
200
                          GType type, int columns)
 
201
{
 
202
        GtkWidget *dlg;
 
203
        char *typestring;
 
204
        const char *typename;
 
205
        char *tmp;
 
206
 
 
207
        if (type != 0)
 
208
                typename = g_type_name (type);
 
209
        else
 
210
                typename = NULL;
 
211
 
 
212
        if (typename == NULL)
 
213
                typestring = g_strdup_printf (" (%s)", typename);
 
214
        else
 
215
                typestring = g_strdup ("");
 
216
 
 
217
        tmp = ve_filename_to_utf8 (filename);
 
218
        
 
219
        dlg = ve_hig_dialog_new (NULL /* parent */,
 
220
                                 0 /* flags */,
 
221
                                 GTK_MESSAGE_ERROR,
 
222
                                 GTK_BUTTONS_OK,
 
223
                                 FALSE /* markup */,
 
224
                                 _("Cannot load user interface"),
 
225
                                 ngettext ("An error occurred while loading the "
 
226
                                           "user interface element %s%s from "
 
227
                                           "file %s. CList type widget should "
 
228
                                           "have %d column. Possibly the glade "
 
229
                                           "interface description was "
 
230
                                           "corrupted. %s cannot continue and "
 
231
                                           "will exit now. You should check "
 
232
                                           "your installation of %s or "
 
233
                                           "reinstall %s.",
 
234
                                           "An error occurred while loading the "
 
235
                                           "user interface element %s%s from "
 
236
                                           "file %s. CList type widget should "
 
237
                                           "have %d columns. Possibly the "
 
238
                                           "glade interface description was "
 
239
                                           "corrupted. %s cannot continue and "
 
240
                                           "will exit now. You should check "
 
241
                                           "your installation of %s or "
 
242
                                           "reinstall %s.",
 
243
                                           columns),
 
244
                                 widget,
 
245
                                 typestring,
 
246
                                 tmp,
 
247
                                 columns,
 
248
                                 PACKAGE,
 
249
                                 PACKAGE,
 
250
                                 PACKAGE);
 
251
 
 
252
        g_free (typestring);
 
253
 
 
254
        g_warning (_("Glade file is on crack! Make sure the correct "
 
255
                     "file is installed!\nfile: %s widget: %s "
 
256
                     "expected clist columns: %d"),
 
257
                   tmp, widget, columns);
 
258
 
 
259
        g_free (tmp);
 
260
 
 
261
        gtk_dialog_run (GTK_DIALOG (dlg));
 
262
        gtk_widget_destroy (dlg);
 
263
}
 
264
 
 
265
static void
 
266
glade_helper_no_interface (const char *filename)
 
267
{
 
268
        GtkWidget *dlg;
 
269
        char *tmp;
 
270
 
 
271
        tmp = ve_filename_to_utf8 (filename);
 
272
        
 
273
        dlg = ve_hig_dialog_new (NULL /* parent */,
 
274
                                 0 /* flags */,
 
275
                                 GTK_MESSAGE_ERROR,
 
276
                                 GTK_BUTTONS_OK,
 
277
                                 FALSE /* markup */,
 
278
                                 _("Cannot load user interface"),
 
279
                                 _("An error occurred while loading the user "
 
280
                                   "interface from file %s.  "
 
281
                                   "Possibly the glade interface description was "
 
282
                                   "not found.  "
 
283
                                   "%s cannot continue and will exit now.  "
 
284
                                   "You should check your "
 
285
                                   "installation of %s or reinstall %s."),
 
286
                                 tmp,
 
287
                                 PACKAGE,
 
288
                                 PACKAGE,
 
289
                                 PACKAGE);
 
290
        gtk_dialog_set_has_separator (GTK_DIALOG (dlg), FALSE);
 
291
        g_warning (_("No interface could be loaded. This is BAD! (file: %s)"),
 
292
                   tmp);
 
293
 
 
294
        g_free (tmp);
 
295
 
 
296
        gtk_dialog_run (GTK_DIALOG (dlg));
 
297
        gtk_widget_destroy (dlg);
 
298
}
 
299
 
 
300
/**
 
301
 * glade_helper_find_glade_file:
 
302
 * @file:  glade filename
 
303
 *
 
304
 * Description:  Finds a glade file in the same directories that
 
305
 * glade_helper would look in.  A utility if you use glade elsewhere in your
 
306
 * application to make using glade simpler.
 
307
 *
 
308
 * Returns:  Newly allocated string with the full path, or %NULL
 
309
 * if not found.
 
310
 **/
 
311
char *
 
312
glade_helper_find_glade_file (const char *file)
 
313
{
 
314
        g_return_val_if_fail (file != NULL, NULL);
 
315
 
 
316
        if (ve_search_gnome_dirs)
 
317
                return ve_find_file (file, ve_glade_directories);
 
318
        else
 
319
                return ve_find_file_simple (file, ve_glade_directories);
 
320
}
 
321
 
 
322
/**
 
323
 * glade_helper_add_glade_directory:
 
324
 * @directory:  directory with glade files
 
325
 *
 
326
 * Description:  Add a standard glade directory to search for
 
327
 * glade files.
 
328
 **/
 
329
void
 
330
glade_helper_add_glade_directory (const char *directory)
 
331
{
 
332
        ve_glade_directories = g_list_append (ve_glade_directories,
 
333
                                              g_strdup (directory));
 
334
}
 
335
 
 
336
/**
 
337
 * glade_helper_seach_gnome_dirs:
 
338
 * @search_gnome_dirs:  boolean
 
339
 *
 
340
 * Description:  Sets if the gnome directories are searched.  Useful if
 
341
 * your program does not use gnome_program_ and you don't want glade_helper
 
342
 * to invoke gnome_program initialization by mistake.  This is because
 
343
 * it is possible for glade helper to try to use gnome_program_ routines
 
344
 * by default.
 
345
 **/
 
346
void
 
347
glade_helper_search_gnome_dirs (gboolean search_gnome_dirs)
 
348
{
 
349
        ve_search_gnome_dirs = search_gnome_dirs;
 
350
}
 
351
 
 
352
/* Make label surrounded by tag (if tag = "tag" add <tag>text</tag>) */
 
353
void
 
354
glade_helper_tagify_label (GladeXML *xml,
 
355
                           const char *name,
 
356
                           const char *tag)
 
357
{
 
358
        const char *lbl;
 
359
        char *s;
 
360
        GtkWidget *label = glade_helper_get (xml, name, GTK_TYPE_WIDGET);
 
361
        if (GTK_IS_BUTTON (label)) {
 
362
                label = GTK_BIN(label)->child;
 
363
        }
 
364
        if ( ! GTK_IS_LABEL (label)) {
 
365
                glade_helper_bad_interface (xml->filename,
 
366
                                            name,
 
367
                                            GTK_TYPE_LABEL);
 
368
                exit (1);
 
369
        }
 
370
        lbl = gtk_label_get_text (GTK_LABEL (label));
 
371
        s = g_strdup_printf ("<%s>%s</%s>", tag, lbl, tag);
 
372
        gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
 
373
        gtk_label_set_label (GTK_LABEL (label), s);
 
374
        g_free (s);
 
375
}