*** empty log message ***
[m17n/m17n-im-config.git] / src / main.c
1 /* main.c -- M17N input method configuration tool
2    Copyright (C) 2007
3      National Institute of Advanced Industrial Science and Technology (AIST)
4      Registration Number H15PRO112
5
6    This file is part of the m17n-im-config package; a sub-part of the
7    m17n library.
8
9    The m17n library is free software; you can redistribute it and/or
10    modify it under the terms of the GNU Lesser General Public License
11    as published by the Free Software Foundation; either version 2.1 of
12    the License, or (at your option) any later version.
13
14    The m17n library is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    Lesser General Public License for more details.
18
19    You should have received a copy of the GNU Lesser General Public
20    License along with the m17n library; if not, write to the Free
21    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22    02111-1307, USA.  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <libintl.h>
27 #define _(String) gettext (String)
28 #include <gtk/gtk.h>
29 #include <m17n-im-config.h>
30
31 /* Argument to callback functions.  */
32
33 struct ConfigSaveRevert
34 {
35   /* IM configuration widget created by mim_config_widget ().  */
36   GtkWidget *config;
37   /* Button widget "revert" */
38   GtkWidget *revert;
39   /* Button widget "save" */
40   GtkWidget *save;
41 };
42
43
44 /* Called when the status of configuration (modified or not) is
45    changed.  */
46 void
47 status_changed_cb (gboolean modified, gpointer data)
48 {
49   struct ConfigSaveRevert *config_save_revert = data;
50
51   if (GTK_WIDGET_SENSITIVE (config_save_revert->save))
52     {
53       if (! modified)
54         {
55           gtk_widget_set_sensitive (config_save_revert->revert, FALSE);
56           gtk_widget_set_sensitive (config_save_revert->save, FALSE);
57         }
58     }
59  else
60    {
61       if (modified)
62         {
63           gtk_widget_set_sensitive (config_save_revert->revert, TRUE);
64           gtk_widget_set_sensitive (config_save_revert->save, TRUE);
65         }
66    }
67 }
68
69 static void
70 revert_clicked_cb (GtkButton *button, gpointer data)
71 {
72   struct ConfigSaveRevert *config_save_revert = data;
73
74   mim_config_revert (config_save_revert->config);
75   status_changed_cb (FALSE, data);
76 }
77
78 static void
79 save_clicked_cb (GtkButton *button, gpointer data)
80 {
81   struct ConfigSaveRevert *config_save_revert = data;
82
83   mim_config_save (config_save_revert->config);
84   status_changed_cb (FALSE, data);
85 }
86
87 static void
88 quit_clicked_cb (GtkButton *button, gpointer data)
89 {
90   struct ConfigSaveRevert *config_save_revert = data;
91
92   if (mim_config_modified (config_save_revert->config))
93     {
94       GtkWidget *dialog, *label;
95       gint response;
96
97       dialog = (gtk_dialog_new_with_buttons
98                 ("confirmation",
99                  GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (button))),
100                  GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
101                  GTK_STOCK_NO, GTK_RESPONSE_NO,
102                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
103                  GTK_STOCK_SAVE, GTK_RESPONSE_YES,
104                  NULL));
105       label = gtk_label_new (_("Save configuration?"));
106       gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), label);
107       gtk_widget_show_all (dialog);
108       response = gtk_dialog_run (GTK_DIALOG (dialog));
109       gtk_widget_destroy (dialog);
110       if (response == GTK_RESPONSE_CANCEL)
111         return;
112       if (response == GTK_RESPONSE_YES)
113         mim_config_save (config_save_revert->config);
114     }
115   gtk_main_quit ();      
116 }
117
118
119 static void
120 ok_clicked_cb (GtkButton *button, gpointer data)
121 {
122   struct ConfigSaveRevert *config_save_revert = data;
123
124   if (mim_config_modified (config_save_revert->config))
125     mim_config_save (config_save_revert->config);
126   gtk_main_quit ();      
127 }
128
129 int
130 main (int argc, char **argv)
131 {
132   GtkWidget *window, *vbox, *config, *hbox;
133   GtkWidget *revert, *save, *ok, *quit;
134   struct ConfigSaveRevert config_save_revert;
135   MimConfigCallback callback;
136     
137 #if ENABLE_NLS
138   bindtextdomain ("m17n-im-config", GETTEXTDIR);
139   bind_textdomain_codeset ("m17n-im-config", "UTF-8");
140 #endif
141   gtk_init (&argc, &argv);
142   if (mim_config_init () < 0)
143     {
144       g_print ("m17n-im-config initialization error\n");
145       exit (1);
146     }
147
148   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
149   gtk_widget_set_size_request (window, 500, 300);
150   gtk_window_set_title (GTK_WINDOW (window), _("M17N-IM Configuration"));
151   g_signal_connect (G_OBJECT (window), "destroy",
152                     G_CALLBACK (gtk_main_quit), NULL);
153     
154   /* We create these widgets:
155      +-vbox----------------------------------+
156      |+-config------------------------------+|
157      ||                                     ||
158      ||                                     ||
159      ||  M17N-IM Configuration              ||
160      ||                                     ||
161      ||                                     ||
162      |+-------------------------------------+|
163      |+-hbox--------------------------------+|
164      ||          +------+ +----+ +----+ +--+||
165      ||          |revert| |save| |quit| |ok|||
166      ||          +------+ +----+ +----+ +--+||
167      |+-------------------------------------+|
168      +---------------------------------------+
169   */
170
171   vbox = gtk_vbox_new (FALSE, 0);
172   gtk_container_add (GTK_CONTAINER (window), vbox);
173
174   revert = gtk_button_new_from_stock (GTK_STOCK_REVERT_TO_SAVED);
175   gtk_widget_set_sensitive (revert, FALSE);
176   save = gtk_button_new_from_stock (GTK_STOCK_SAVE);
177   gtk_widget_set_sensitive (save, FALSE);
178   quit = gtk_button_new_from_stock (GTK_STOCK_QUIT);
179   ok = gtk_button_new_from_stock (GTK_STOCK_OK);
180
181   config_save_revert.revert = revert;
182   config_save_revert.save = save;
183   callback.func = status_changed_cb;
184   callback.arg = &config_save_revert;
185   config = mim_config_widget (&callback);
186   config_save_revert.config = config;
187   gtk_box_pack_start (GTK_BOX (vbox), config, TRUE, TRUE, 0);
188
189   g_signal_connect (G_OBJECT (revert), "clicked",
190                     G_CALLBACK (revert_clicked_cb), &config_save_revert);
191   g_signal_connect (G_OBJECT (save), "clicked",
192                     G_CALLBACK (save_clicked_cb), &config_save_revert);
193   g_signal_connect (G_OBJECT (quit), "clicked",
194                     G_CALLBACK (quit_clicked_cb), &config_save_revert);
195   g_signal_connect (G_OBJECT (ok), "clicked",
196                     G_CALLBACK (ok_clicked_cb), &config_save_revert);
197
198   hbox = gtk_hbutton_box_new ();
199   gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_END);
200   gtk_box_set_spacing (GTK_BOX (hbox), 5);
201   gtk_container_add (GTK_CONTAINER (hbox), revert);
202   gtk_container_add (GTK_CONTAINER (hbox), save);
203   gtk_container_add (GTK_CONTAINER (hbox), quit);
204   gtk_container_add (GTK_CONTAINER (hbox), ok);
205   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 5);
206
207   gtk_widget_show_all (window);
208   gtk_main ();
209   mim_config_fini ();
210
211   return 0;
212 }