*** 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 /* Common argument to all callback functions.  */
32
33 struct CallbackArgument
34 {
35   /* IM configuration widget created by mim_config_new ().  */
36   GtkWidget *config;
37   /* Button widget "default" */
38   GtkWidget *default_;
39   /* Button widget "revert" */
40   GtkWidget *revert;
41   /* Button widget "save" */
42   GtkWidget *save;
43 };
44
45
46 /* Called when the status of configuration is changed.  */
47 void
48 status_changed_cb (GtkWidget *config, gpointer data)
49 {
50   struct CallbackArgument *arg = data;
51   gboolean modified = mim_config_modified (config);
52
53   if (GTK_WIDGET_SENSITIVE (arg->save))
54     {
55       if (! modified)
56         {
57           gtk_widget_set_sensitive (arg->revert, FALSE);
58           gtk_widget_set_sensitive (arg->save, FALSE);
59         }
60     }
61  else
62    {
63       if (modified)
64         {
65           gtk_widget_set_sensitive (arg->revert, TRUE);
66           gtk_widget_set_sensitive (arg->save, TRUE);
67         }
68    }
69 }
70
71 /* Called when "default" button is clicked.  */
72 static void
73 default_clicked_cb (GtkButton *button, gpointer data)
74 {
75   struct CallbackArgument *arg = data;
76
77   mim_config_default (arg->config);
78   status_changed_cb (arg->config, data);
79 }
80
81 /* Called when "revert" button is clicked.  */
82 static void
83 revert_clicked_cb (GtkButton *button, gpointer data)
84 {
85   struct CallbackArgument *arg = data;
86
87   mim_config_revert (arg->config);
88   status_changed_cb (arg->config, data);
89 }
90
91 /* Called when "save" button is clicked.  */
92 static void
93 save_clicked_cb (GtkButton *button, gpointer data)
94 {
95   struct CallbackArgument *arg = data;
96
97   mim_config_save (arg->config);
98   status_changed_cb (arg->config, data);
99 }
100
101 /* Called when "quit" button is clicked.  */
102 static void
103 quit_clicked_cb (GtkButton *button, gpointer data)
104 {
105   struct CallbackArgument *arg = data;
106
107   if (mim_config_modified (arg->config))
108     {
109       GtkWidget *dialog, *label;
110       gint response;
111
112       dialog = (gtk_dialog_new_with_buttons
113                 ("confirmation",
114                  GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (button))),
115                  GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
116                  GTK_STOCK_NO, GTK_RESPONSE_NO,
117                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
118                  GTK_STOCK_SAVE, GTK_RESPONSE_YES,
119                  NULL));
120       label = gtk_label_new (_("Save configuration?"));
121       gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), label);
122       gtk_widget_show_all (dialog);
123       response = gtk_dialog_run (GTK_DIALOG (dialog));
124       gtk_widget_destroy (dialog);
125       if (response == GTK_RESPONSE_CANCEL)
126         return;
127       if (response == GTK_RESPONSE_YES)
128         mim_config_save (arg->config);
129     }
130   gtk_widget_destroy (arg->config);
131   gtk_main_quit ();      
132 }
133
134 /* Called when "ok" button is clicked.  */
135 static void
136 ok_clicked_cb (GtkButton *button, gpointer data)
137 {
138   struct CallbackArgument *arg = data;
139
140   if (mim_config_modified (arg->config))
141     mim_config_save (arg->config);
142   gtk_widget_destroy (arg->config);
143   gtk_main_quit ();      
144 }
145
146 int
147 main (int argc, char **argv)
148 {
149   GtkWidget *window, *vbox, *hbox;
150   GtkWidget *default_, *revert, *save, *ok, *quit;
151   struct CallbackArgument arg;
152     
153 #if ENABLE_NLS
154   bindtextdomain ("m17n-im-config", GETTEXTDIR);
155   bind_textdomain_codeset ("m17n-im-config", "UTF-8");
156 #endif
157   gtk_init (&argc, &argv);
158
159   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
160   gtk_widget_set_size_request (window, 500, 300);
161   gtk_window_set_title (GTK_WINDOW (window), _("M17N-IM Configuration"));
162   g_signal_connect (G_OBJECT (window), "destroy",
163                     G_CALLBACK (gtk_main_quit), NULL);
164     
165   /* We create these widgets:
166      +-vbox----------------------------------+
167      |+-config------------------------------+|
168      ||                                     ||
169      ||                                     ||
170      ||  M17N-IM Configuration              ||
171      ||                                     ||
172      ||                                     ||
173      |+-------------------------------------+|
174      |+-hbox--------------------------------+|
175      ||+-------+ +------+ +----+ +----+ +--+||
176      |||default| |revert| |save| |quit| |ok|||
177      ||+-------+ +------+ +----+ +----+ +--+||
178      |+-------------------------------------+|
179      +---------------------------------------+
180   */
181
182   vbox = gtk_vbox_new (FALSE, 12);
183   gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
184   gtk_container_add (GTK_CONTAINER (window), vbox);
185
186   default_ = gtk_button_new_from_stock (_("_Default"));
187   revert = gtk_button_new_from_stock (GTK_STOCK_REVERT_TO_SAVED);
188   gtk_widget_set_sensitive (revert, FALSE);
189   save = gtk_button_new_from_stock (GTK_STOCK_SAVE);
190   gtk_widget_set_sensitive (save, FALSE);
191   quit = gtk_button_new_from_stock (GTK_STOCK_QUIT);
192   ok = gtk_button_new_from_stock (GTK_STOCK_OK);
193
194   arg.revert = revert;
195   arg.save = save;
196   arg.config = mim_config_new (G_CALLBACK (status_changed_cb), &arg);
197   gtk_box_pack_start (GTK_BOX (vbox), arg.config, TRUE, TRUE, 0);
198
199   g_signal_connect (G_OBJECT (default_), "clicked",
200                     G_CALLBACK (default_clicked_cb), &arg);
201   g_signal_connect (G_OBJECT (revert), "clicked",
202                     G_CALLBACK (revert_clicked_cb), &arg);
203   g_signal_connect (G_OBJECT (save), "clicked",
204                     G_CALLBACK (save_clicked_cb), &arg);
205   g_signal_connect (G_OBJECT (quit), "clicked",
206                     G_CALLBACK (quit_clicked_cb), &arg);
207   g_signal_connect (G_OBJECT (ok), "clicked",
208                     G_CALLBACK (ok_clicked_cb), &arg);
209
210   hbox = gtk_hbutton_box_new ();
211   gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_END);
212   gtk_box_set_spacing (GTK_BOX (hbox), 5);
213   gtk_container_add (GTK_CONTAINER (hbox), default_);
214   gtk_container_add (GTK_CONTAINER (hbox), revert);
215   gtk_container_add (GTK_CONTAINER (hbox), save);
216   gtk_container_add (GTK_CONTAINER (hbox), quit);
217   gtk_container_add (GTK_CONTAINER (hbox), ok);
218   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 5);
219
220   gtk_widget_show_all (window);
221   gtk_main ();
222
223   return 0;
224 }