Back to index

im-sdk  12.3.91
main.c
Go to the documentation of this file.
00001 /* GNOME INPUT METHOD PROPERTIES
00002  *
00003  * Copyright 2004 Sun Microsystems Inc.
00004  *
00005  * This is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the
00017  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include <config.h>
00022 #include "status-placement.h"
00023 
00024 #include <string.h>
00025 #include <glib.h>
00026 #include <gtk/gtk.h>
00027 #include <gnome.h>
00028 #include <gconf/gconf-client.h>
00029 #include <glade/glade.h>
00030 #include <stdarg.h>
00031 
00032 /* Retrieve a widget from the Glade object */
00033 #define WID(s) glade_xml_get_widget (dialog, s)
00034 
00035 #define IM_KEY_DIR    "/desktop/gnome/input_methods"
00036 #define ENABLE_IM_KEY IM_KEY_DIR "/support"
00037 #define ENABLE_STATUS_KEY IM_KEY_DIR "/status"
00038 #define ENABLE_LOOKUP_KEY IM_KEY_DIR "/lookup"
00039 #define STATUS_PLACEMENT_KEY IM_KEY_DIR "/status_placement"
00040 #define GTK_STATUS_STYLE_KEY "/desktop/gnome/interface/gtk-im-status-style"
00041 
00042 typedef enum {
00043   IM_OFF = 0,
00044   IM_ON
00045 } IMEnabled;
00046 
00047 /* For gtk-im-status-style */
00048 static const char *nothing = "nothing"; /* XIMStatusNothing */
00049 static const char *callback = "callback"; /* XIMStatusCallbacks */
00050 static const char *none = "none";  /* XIMStatusNone */
00051 
00052 static const char *enabled = "enabled";
00053 static const char *disabled = "disabled";
00054 
00055 typedef struct {
00056   IMEnabled im_enabled;
00057   IMEnabled status_enabled;
00058   IMEnabled lookup_enabled;
00059   IMStatusPlacement status_placement;
00060 } IMSettings;
00061 
00062 static IMSettings current_setting;
00063 static GdkAtom im_settings_atom = GDK_NONE;
00064 static const char *_IM_SETTINGS = "_IM_SETTINGS";
00065 
00066 static GladeXML *main_dialog;
00067 
00068 static void setup_dialog (GladeXML *dialog);
00069 static GladeXML *create_dialog (void);
00070 
00071 static void
00072 set_im_settings_property (GladeXML *dialog)
00073 {
00074   GdkWindow *root_window = gdk_get_default_root_window ();
00075 
00076   if (im_settings_atom == GDK_NONE)
00077     im_settings_atom = gdk_atom_intern (_IM_SETTINGS, FALSE);
00078   if (im_settings_atom == GDK_NONE)
00079     return;
00080 
00081   gdk_property_change (root_window,
00082                      im_settings_atom, im_settings_atom,
00083                      8, GDK_PROP_MODE_REPLACE,
00084                      (guchar *)&current_setting, sizeof (IMSettings));
00085 }
00086 
00087 static void
00088 im_toggled (GtkToggleButton *toggle_button,
00089            GladeXML *dialog)
00090 {
00091   GConfClient *client = gconf_client_get_default ();
00092 
00093   if (toggle_button == GTK_TOGGLE_BUTTON (WID ("im_enable_toggle")))
00094     {
00095       gboolean im_enabled = gtk_toggle_button_get_active (toggle_button);
00096 
00097       if ((current_setting.im_enabled == IM_ON && !im_enabled) ||
00098          (current_setting.im_enabled == IM_OFF && im_enabled))
00099        {
00100          GtkWidget *widget = WID ("im_appearance_box");
00101          gtk_widget_set_sensitive (widget, im_enabled);
00102 #ifdef HIDE_WHEN_DISABLED
00103          if (im_enabled)
00104            gtk_widget_show (widget);
00105          else
00106            gtk_widget_hide (widget);
00107 #endif
00108          gconf_client_set_string (client,
00109                                ENABLE_IM_KEY, 
00110                                im_enabled ?
00111                                enabled : disabled,
00112                                NULL);
00113          current_setting.im_enabled = im_enabled ? IM_ON : IM_OFF;
00114        }
00115     }
00116   if (toggle_button == GTK_TOGGLE_BUTTON (WID ("im_status_toggle")))
00117     {
00118       gboolean status_enabled = gtk_toggle_button_get_active (toggle_button);
00119 
00120       if ((current_setting.status_enabled == IM_ON && !status_enabled) ||
00121          (current_setting.status_enabled == IM_OFF && status_enabled))
00122        {
00123          GtkWidget *widget = WID ("im_status_placement_box");
00124          gtk_widget_set_sensitive (widget, status_enabled);
00125          gconf_client_set_string (client,
00126                                ENABLE_STATUS_KEY,
00127                                status_enabled ?
00128                                enabled : disabled,
00129                                NULL);
00130          current_setting.status_enabled = status_enabled;
00131 
00132          if (status_enabled)
00133            gconf_client_set_string (client,
00134                                  GTK_STATUS_STYLE_KEY, 
00135                                  current_setting.status_placement == ON_DESKTOP_PANEL ?
00136                                  nothing : callback,
00137                                  NULL);
00138          else
00139            gconf_client_set_string (client,
00140                                  GTK_STATUS_STYLE_KEY, 
00141                                  none,
00142                                  NULL);
00143        }
00144     }
00145   if (toggle_button == GTK_TOGGLE_BUTTON (WID ("im_choice_toggle")))
00146     {
00147       gboolean lookup_enabled = gtk_toggle_button_get_active (toggle_button);
00148 
00149       if ((current_setting.lookup_enabled == IM_ON && !lookup_enabled) ||
00150          (current_setting.lookup_enabled == IM_OFF && lookup_enabled))
00151        {
00152          gconf_client_set_string (client,
00153                                ENABLE_LOOKUP_KEY,
00154                                lookup_enabled ?
00155                                enabled : disabled,
00156                                NULL);
00157          current_setting.lookup_enabled = lookup_enabled;
00158        }
00159     }
00160   set_im_settings_property (dialog);
00161 }
00162 
00163 static void
00164 im_status_placement_changed (GtkWidget *optionmenu,
00165                           GladeXML *dialog)
00166 {
00167   GConfClient *client = gconf_client_get_default ();
00168 
00169   int status_placement;
00170   status_placement = gtk_option_menu_get_history (GTK_OPTION_MENU (optionmenu));
00171   if (status_placement != current_setting.status_placement)
00172     {
00173       gconf_client_set_string (client,
00174                             STATUS_PLACEMENT_KEY, 
00175                             gimlet_status_placement_name (status_placement),
00176                             NULL);
00177       current_setting.status_placement = status_placement;
00178 
00179       gconf_client_set_string (client,
00180                             GTK_STATUS_STYLE_KEY, 
00181                             status_placement == ON_DESKTOP_PANEL ?
00182                             nothing : callback,
00183                             NULL);
00184     }
00185   set_im_settings_property (dialog);
00186 }
00187 
00188 static void
00189 revert_to_default (GtkDialog *dialog)
00190 {
00191   GConfClient *client;
00192   client = gconf_client_get_default ();
00193 
00194   gconf_client_set_string (client,
00195                         ENABLE_IM_KEY, 
00196                         enabled,
00197                         NULL);
00198   gconf_client_set_string (client,
00199                         ENABLE_STATUS_KEY,
00200                         enabled,
00201                         NULL);
00202   gconf_client_set_string (client,
00203                         STATUS_PLACEMENT_KEY, 
00204                         DEFAULT_STATUS_PLACEMENT,
00205                         NULL);
00206   gconf_client_set_string (client,
00207                         GTK_STATUS_STYLE_KEY, 
00208                         nothing,
00209                         NULL);
00210   gconf_client_set_string (client,
00211                         ENABLE_LOOKUP_KEY,
00212                         enabled,
00213                         NULL);
00214   current_setting.im_enabled = IM_ON;
00215   current_setting.status_enabled = IM_ON;
00216   current_setting.lookup_enabled = IM_ON;
00217   current_setting.status_placement = gimlet_status_placement (DEFAULT_STATUS_PLACEMENT);
00218 
00219   setup_dialog (main_dialog);
00220 }
00221 
00222 static void
00223 cb_dialog_response (GtkDialog *dialog, gint response_id)
00224 {
00225   if (response_id == GTK_RESPONSE_CLOSE)
00226     gtk_main_quit ();
00227   else if (response_id == GTK_RESPONSE_HELP)
00228         {
00229           GError *error = NULL;
00230 
00231           gnome_help_display_desktop (
00232                       NULL, "imswitcher", "imswitcher.xml", 
00233                       NULL, &error);
00234 
00235           if (error)
00236             {
00237               GtkWidget *message_dialog;
00238               message_dialog = gtk_message_dialog_new (NULL, 0,
00239                                                  GTK_MESSAGE_ERROR,
00240                                                  GTK_BUTTONS_OK,
00241                                                  _("There was an error displaying help: %s"),
00242                                                  error->message);
00243               g_error_free (error);
00244               gtk_dialog_run (GTK_DIALOG (message_dialog));
00245               gtk_widget_destroy (message_dialog);
00246             }
00247           }
00248   else
00249     revert_to_default (dialog);
00250 }
00251 
00252 static void
00253 setup_dialog (GladeXML *dialog)
00254 {
00255   GConfClient *client;
00256   GtkWidget *widget;
00257   GtkTooltips *tooltips;
00258   gboolean im_enabled, status_enabled, lookup_enabled;
00259   char *str = NULL;
00260 
00261   client = gconf_client_get_default ();
00262 
00263   str = NULL;
00264   str = gconf_client_get_string (client,
00265                              ENABLE_IM_KEY, NULL);
00266   if (str && strcmp (str, disabled) == 0)
00267     im_enabled = FALSE;
00268   else
00269     im_enabled = TRUE;
00270   g_free (str);
00271 
00272   str = NULL;
00273   str = gconf_client_get_string (client,
00274                              ENABLE_STATUS_KEY, NULL);
00275   if (str && strcmp (str, disabled) == 0)
00276     status_enabled = FALSE;
00277   else
00278     status_enabled = TRUE;
00279   g_free (str);
00280 
00281   str = NULL;
00282   str = gconf_client_get_string (client,
00283                              ENABLE_LOOKUP_KEY, NULL);
00284   if (str && strcmp (str, disabled) == 0)
00285     lookup_enabled = FALSE;
00286   else
00287     lookup_enabled = TRUE;
00288   g_free (str);
00289 
00290   current_setting.im_enabled = im_enabled ? IM_ON : IM_OFF;
00291   current_setting.status_enabled = status_enabled ? IM_ON : IM_OFF;
00292   current_setting.lookup_enabled = lookup_enabled ? IM_ON : IM_OFF;
00293 
00294   str = NULL;
00295   str = gconf_client_get_string (client,
00296                              STATUS_PLACEMENT_KEY, NULL);
00297   current_setting.status_placement = gimlet_status_placement (str);
00298   g_free (str);
00299 
00300   set_im_settings_property (dialog);
00301 
00302   tooltips = gtk_tooltips_new();
00303 
00304   widget = WID ("im_appearance_box");
00305   gtk_widget_set_sensitive (widget, im_enabled);
00306 
00307 #ifdef HIDE_WHEN_DISABLED
00308   if (im_enabled)
00309     gtk_widget_show (widget);
00310   else
00311     gtk_widget_hide (widget);
00312 #endif
00313 
00314   gtk_widget_set_sensitive (WID ("im_status_placement_box"), status_enabled);
00315 
00316   /* im enable toggle button */
00317   widget = WID ("im_enable_toggle");
00318   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), im_enabled);
00319   g_signal_connect (widget, "toggled",
00320                   G_CALLBACK (im_toggled), 
00321                   dialog);
00322   gtk_tooltips_set_tip (tooltips, widget,
00323                      _("Changes to this setting will immediately take effect on the current session."),
00324                      NULL);
00325   
00326   /* im status toggle button */
00327   widget = WID ("im_status_toggle");
00328   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), status_enabled);
00329   g_signal_connect (widget, "toggled",
00330                   G_CALLBACK (im_toggled), 
00331                   dialog);
00332   gtk_tooltips_set_tip (tooltips, widget,
00333                      _("Changes to this setting will immediately take effect on the current session."),
00334                      NULL);
00335 
00336   /* im status placement option menu */
00337   widget = WID ("status_placement_menu");
00338   gtk_option_menu_set_history (GTK_OPTION_MENU (widget),
00339                             current_setting.status_placement);
00340   g_signal_connect (widget, "changed",
00341                   G_CALLBACK (im_status_placement_changed),
00342                   dialog);
00343 
00344   /* im choice toggle button */
00345   widget = WID ("im_choice_toggle");
00346   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), lookup_enabled);
00347   g_signal_connect (widget, "toggled",
00348                   G_CALLBACK (im_toggled), 
00349                   dialog);
00350   gtk_tooltips_set_tip (tooltips, widget,
00351                      _("Changes to this setting may not be effective in some languages and input method editors."),
00352                      NULL);
00353 
00354   /* response close, help, revert button events */
00355   widget = WID ("im_properties_dialog");
00356   g_signal_connect (G_OBJECT (widget),
00357                   "response",
00358                   G_CALLBACK (cb_dialog_response), NULL);
00359 
00360   g_signal_connect (G_OBJECT(widget), "destroy", 
00361                   G_CALLBACK (gtk_main_quit), NULL);
00362 
00363   gtk_widget_show (widget);
00364 
00365 }
00366 
00367 static GladeXML *
00368 create_dialog (void)
00369 {
00370   GladeXML *dialog;
00371 
00372   dialog = glade_xml_new (GNOMECC_DATA_DIR "/interfaces/gnome-im-properties.glade2", "im_properties_dialog", NULL);
00373 
00374 #ifdef INCLUDE_ICON_IN_CAPPLET_WINDOW
00375   gtk_image_set_from_file (GTK_IMAGE (WID ("im_enable_image")),
00376                         PIXMAPDIR "/gnome-settings-im.png");
00377 #endif
00378 
00379   return dialog;
00380 }
00381 
00382 int
00383 main (int argc, char *argv[])
00384 {
00385   bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
00386   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
00387   textdomain (GETTEXT_PACKAGE);
00388 
00389   gnome_program_init ("gnome-im-properties", VERSION,
00390                     LIBGNOMEUI_MODULE, argc, argv,
00391                     GNOME_PARAM_APP_DATADIR, GNOMECC_DATA_DIR,
00392                     NULL);
00393 
00394   main_dialog = create_dialog ();
00395   setup_dialog (main_dialog);
00396 
00397   gtk_main ();
00398   return 0;
00399 }