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