Back to index

im-sdk  12.3.91
utils.c
Go to the documentation of this file.
00001 /* GIMLET: GNOME Input Method Language Enabing Tool
00002  *
00003  * Copyright (C) 2001, 2002 Havoc Pennington
00004  * Copyright (C) 2002 Red Hat, Inc.
00005  * Copyright (C) 2003 Sun Microsystems
00006  * Copyright (C) 2003 Mariano Suarez-Alvarez
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Library General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Library General Public
00019  * License along with this library; if not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021  * Boston, MA 02111-1307, USA.
00022  */
00023 
00024 #include <config.h>
00025 
00026 #include <gtk/gtkmain.h>
00027 #include <gtk/gtkdrawingarea.h>
00028 #include <gtk/gtkbutton.h>
00029 #include <gtk/gtksignal.h>
00030 #include <panel-applet.h>
00031 #include <gnome.h>
00032 
00033 #include "gnome-im-switcher.h"
00034 #include "utils.h"
00035 
00049 void
00050 gimlet_util_show_error_dialog (GtkWindow *transient_parent, GtkWidget **weak_ptr, const char *message_format, ...)
00051 {
00052   char *message;
00053   va_list args;
00054 
00055   if (message_format)
00056     {
00057       va_start (args, message_format);
00058       message = g_strdup_vprintf (message_format, args);
00059       va_end (args);
00060     }
00061   else message = NULL;
00062 
00063   if (weak_ptr == NULL || *weak_ptr == NULL)
00064     {
00065       GtkWidget *dialog;
00066       dialog = gtk_message_dialog_new (transient_parent,
00067                                        GTK_DIALOG_DESTROY_WITH_PARENT,
00068                                        GTK_MESSAGE_ERROR,
00069                                        GTK_BUTTONS_CLOSE,
00070                                        message);
00071 
00072       g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
00073 
00074       if (weak_ptr != NULL)
00075         {
00076         *weak_ptr = dialog;
00077         g_object_add_weak_pointer (G_OBJECT (dialog), (void**)weak_ptr);
00078         }
00079 
00080       gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
00081       
00082       gtk_widget_show_all (dialog);
00083     }
00084   else 
00085     {
00086       g_return_if_fail (GTK_IS_MESSAGE_DIALOG (*weak_ptr));
00087 
00088       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (*weak_ptr)->label), message);
00089 
00090       gtk_window_present (GTK_WINDOW (*weak_ptr));
00091     }
00092 }
00093 
00094 void
00095 gimlet_util_show_help (const char *topic, 
00096                      GtkWindow  *transient_parent)
00097 {
00098   GError *err = NULL;
00099 
00100   gnome_help_display_desktop_on_screen (
00101          NULL, "imswitcher", "imswitcher", topic,
00102          gtk_widget_get_screen (GTK_WIDGET (transient_parent)),
00103          &err);
00104 
00105   if (err)
00106     {
00107       gimlet_util_show_error_dialog (GTK_WINDOW (transient_parent), NULL,
00108                                  _("There was an error displaying help: %s"),
00109                                  err->message);
00110       g_error_free (err);
00111     }
00112 }
00113 
00114 void
00115 gimlet_util_set_unique_role (GtkWindow *window, const char *prefix)
00116 {
00117   char *role;
00118 
00119   role = g_strdup_printf ("%s-%d-%d-%d", prefix, getpid (), g_random_int (), (int) time (NULL));
00120   gtk_window_set_role (window, role);
00121   g_free (role);
00122 }
00123 
00124 GladeXML*
00125 gimlet_util_load_glade_file (const char *filename,
00126                           const char *widget_root,
00127                           GtkWindow  *error_dialog_parent)
00128 {
00129   char *path;
00130   GladeXML *xml;
00131 
00132   xml = NULL;
00133   path = g_strconcat ("./", filename, NULL);
00134   
00135   if (g_file_test (path,
00136                    G_FILE_TEST_EXISTS))
00137     {
00138       /* Try current dir, for debugging */
00139       xml = glade_xml_new (path,
00140                            widget_root,
00141                            GETTEXT_PACKAGE);
00142     }
00143   
00144   if (xml == NULL)
00145     {
00146       g_free (path);
00147       
00148       path = g_build_filename (GIMLET_GLADE_DIR, filename, NULL);
00149 
00150       xml = glade_xml_new (path,
00151                            widget_root,
00152                            GETTEXT_PACKAGE);
00153     }
00154 
00155   if (xml == NULL)
00156     {
00157       static GtkWidget *no_glade_dialog = NULL;
00158 
00159       gimlet_util_show_error_dialog (error_dialog_parent, &no_glade_dialog, 
00160                                  _("The file \"%s\" is missing. This indicates that the application is installed incorrectly."), path);
00161     }
00162 
00163   g_free (path);
00164 
00165   return xml;
00166 }