*** 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 (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 (arg->config, 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 (arg->config, 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, 12);
171   gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
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   arg.revert = revert;
182   arg.save = save;
183   arg.config = mim_config_new (G_CALLBACK (status_changed_cb), &arg);
184   gtk_box_pack_start (GTK_BOX (vbox), arg.config, TRUE, TRUE, 0);
185
186   g_signal_connect (G_OBJECT (revert), "clicked",
187                     G_CALLBACK (revert_clicked_cb), &arg);
188   g_signal_connect (G_OBJECT (save), "clicked",
189                     G_CALLBACK (save_clicked_cb), &arg);
190   g_signal_connect (G_OBJECT (quit), "clicked",
191                     G_CALLBACK (quit_clicked_cb), &arg);
192   g_signal_connect (G_OBJECT (ok), "clicked",
193                     G_CALLBACK (ok_clicked_cb), &arg);
194
195   hbox = gtk_hbutton_box_new ();
196   gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_END);
197   gtk_box_set_spacing (GTK_BOX (hbox), 5);
198   gtk_container_add (GTK_CONTAINER (hbox), revert);
199   gtk_container_add (GTK_CONTAINER (hbox), save);
200   gtk_container_add (GTK_CONTAINER (hbox), quit);
201   gtk_container_add (GTK_CONTAINER (hbox), ok);
202   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 5);
203
204   gtk_widget_show_all (window);
205   gtk_main ();
206
207   return 0;
208 }