9e6909823be8f3a2afcda1fa6c846cc0dd63d14b
[platform/core/uifw/ise-engine-sunpinyin.git] / wrapper / scim / src / sunpinyin_imengine_setup.cpp
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  * 
4  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
5  * 
6  * The contents of this file are subject to the terms of either the GNU Lesser
7  * General Public License Version 2.1 only ("LGPL") or the Common Development and
8  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
9  * file except in compliance with the License. You can obtain a copy of the CDDL at
10  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the 
12  * specific language governing permissions and limitations under the License. When
13  * distributing the software, include this License Header Notice in each file and
14  * include the full text of the License in the License file as well as the
15  * following notice:
16  * 
17  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
18  * (CDDL)
19  * For Covered Software in this distribution, this License shall be governed by the
20  * laws of the State of California (excluding conflict-of-law provisions).
21  * Any litigation relating to this License shall be subject to the jurisdiction of
22  * the Federal Courts of the Northern District of California and the state courts
23  * of the State of California, with venue lying in Santa Clara County, California.
24  * 
25  * Contributor(s):
26  * 
27  * If you wish your version of this file to be governed by only the CDDL or only
28  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
30  * license." If you don't indicate a single choice of license, a recipient has the
31  * option to distribute your version of this file under either the CDDL or the LGPL
32  * Version 2.1, or to extend the choice of license to its licensees as provided
33  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34  * Version 2 license, then the option applies only if the new code is made subject
35  * to such option by the copyright holder. 
36  */
37 #define Uses_SCIM_CONFIG_BASE
38
39 #include <gtk/gtk.h>
40
41 #include <scim.h>
42 #include <gtk/scimkeyselection.h>
43
44 #include "sunpinyin_private.h"
45 #include "sunpinyin_imengine_config_keys.h"
46
47 using namespace scim;
48
49 #if !GTK_CHECK_VERSION(2, 12, 0)
50     #define SUNPINYIN_USE_GTK_TOOLTIPS
51 #endif
52
53 #if GTK_CHECK_VERSION(2, 24, 0)
54     #define SUNPINYIN_USE_GTK_COMBO_BOX_TEXT
55 #endif
56
57 #if GTK_CHECK_VERSION(3, 0, 0)
58     #define SUNPINYIN_USE_GTK_BOX
59 #endif
60
61 #define scim_module_init sunpinyin_imengine_setup_LTX_scim_module_init
62 #define scim_module_exit sunpinyin_imengine_setup_LTX_scim_module_exit
63
64 #define scim_setup_module_create_ui       sunpinyin_imengine_setup_LTX_scim_setup_module_create_ui
65 #define scim_setup_module_get_category    sunpinyin_imengine_setup_LTX_scim_setup_module_get_category
66 #define scim_setup_module_get_name        sunpinyin_imengine_setup_LTX_scim_setup_module_get_name
67 #define scim_setup_module_get_description sunpinyin_imengine_setup_LTX_scim_setup_module_get_description
68 #define scim_setup_module_load_config     sunpinyin_imengine_setup_LTX_scim_setup_module_load_config
69 #define scim_setup_module_save_config     sunpinyin_imengine_setup_LTX_scim_setup_module_save_config
70 #define scim_setup_module_query_changed   sunpinyin_imengine_setup_LTX_scim_setup_module_query_changed
71
72 static GtkWidget * input_style_combo = NULL;
73 static GtkWidget * charset_combo = NULL;
74 static GtkWidget * minus_pageup_button = NULL;
75 static GtkWidget * bracket_pageup_button = NULL;
76 static GtkWidget * comma_pageup_button = NULL;
77 static GtkWidget * memory_power_button = NULL;
78
79 static GtkWidget * create_setup_window ();
80 static void        load_config (const ConfigPointer &config);
81 static void        save_config (const ConfigPointer &config);
82 static bool        query_changed ();
83
84 // Module Interface.
85 extern "C" {
86     void scim_module_init (void)
87     {
88         bindtextdomain (GETTEXT_PACKAGE, SCIM_SUNPINYIN_LOCALEDIR);
89         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
90     }
91
92     void scim_module_exit (void)
93     {
94     }
95
96     GtkWidget * scim_setup_module_create_ui (void)
97     {
98         static GtkWidget *setup_ui = NULL;
99         if (setup_ui == NULL)
100             setup_ui = create_setup_window ();
101         return setup_ui;
102     }
103
104     String scim_setup_module_get_category (void)
105     {
106         return String ("IMEngine");
107     }
108
109     String scim_setup_module_get_name (void)
110     {
111         return String (_("SunPinyin"));
112     }
113
114     String scim_setup_module_get_description (void)
115     {
116         return String (_("A Statistical Language Model Based Intelligent IMEngine Module for Simplified Chinese."));
117     }
118
119     void scim_setup_module_load_config (const ConfigPointer &config)
120     {
121         load_config (config);
122     }
123
124     void scim_setup_module_save_config (const ConfigPointer &config)
125     {
126         save_config (config);
127     }
128
129     bool scim_setup_module_query_changed ()
130     {
131         return query_changed ();
132     }
133 } // extern "C"
134
135 static bool __have_changed                 = false;
136
137 static void
138 on_value_changed(GtkWidget *  widget,
139                  gpointer     user_data)
140 {
141     __have_changed = true;
142 }
143
144 static GtkWidget *
145 #ifdef SUNPINYIN_USE_GTK_TOOLTIPS
146 create_options_page(GtkTooltips *tooltips)
147 #else
148 create_options_page()
149 #endif
150 {
151     GtkWidget *vbox;
152     GtkWidget *label;
153     GtkWidget *button;
154
155 #ifdef SUNPINYIN_USE_GTK_BOX
156     vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
157 #else
158     vbox = gtk_vbox_new (FALSE, 12);
159 #endif
160
161     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
162
163     GtkWidget *table = gtk_table_new (2, 2, FALSE);
164     gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, TRUE, 0);
165
166     label = gtk_label_new (_("Input Style:"));
167     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
168     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
169                       (GtkAttachOptions) (GTK_FILL),
170                       (GtkAttachOptions) (GTK_FILL), 4, 4);
171
172
173 #ifdef SUNPINYIN_USE_GTK_COMBO_BOX_TEXT
174     GtkWidget *combo_box = gtk_combo_box_text_new();
175 #else
176     GtkWidget *combo_box = gtk_combo_box_new_text();
177 #endif
178
179     gtk_table_attach (GTK_TABLE (table), combo_box, 1, 2, 0, 1,
180                       (GtkAttachOptions) (GTK_FILL),
181                       (GtkAttachOptions) (GTK_FILL), 4, 4);
182
183 #ifdef SUNPINYIN_USE_GTK_COMBO_BOX_TEXT
184     gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), _("Classic Style"));
185     gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), _("Instant Style"));
186 #else
187     gtk_combo_box_append_text(GTK_COMBO_BOX(combo_box), _("Classic Style"));
188     gtk_combo_box_append_text(GTK_COMBO_BOX(combo_box), _("Instant Style"));
189 #endif
190
191     const gchar *input_style_tooltip = _("You may feel more comfortable in classic style, "
192                                          "if you are used to input methods like scim-pinyin."
193                                          " In instant style, the most possible candidate "
194                                          "word will show up in the preedit area right after "
195                                          "its pinyin is input.");
196 #ifdef SUNPINYIN_USE_GTK_TOOLTIPS
197     gtk_tooltips_set_tip(tooltips, combo_box, input_style_tooltip, NULL);
198 #else
199     gtk_widget_set_tooltip_text(combo_box, input_style_tooltip);
200 #endif
201
202     g_signal_connect(G_OBJECT(combo_box), "changed",
203                      G_CALLBACK(on_value_changed), NULL);
204     input_style_combo = combo_box;
205
206     label = gtk_label_new (_("Character Set:"));
207     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
208     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
209                       (GtkAttachOptions) (GTK_FILL),
210                       (GtkAttachOptions) (GTK_FILL), 4, 4);
211
212 #ifdef SUNPINYIN_USE_GTK_COMBO_BOX_TEXT
213     combo_box = gtk_combo_box_text_new();
214 #else
215     combo_box = gtk_combo_box_new_text();
216 #endif
217
218     gtk_table_attach (GTK_TABLE (table), combo_box, 1, 2, 1, 2,
219                       (GtkAttachOptions) (GTK_FILL),
220                       (GtkAttachOptions) (GTK_FILL), 4, 4);
221
222 #ifdef SUNPINYIN_USE_GTK_COMBO_BOX_TEXT
223     gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), _("GB2312"));
224     gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), _("GBK"));
225 #else
226     gtk_combo_box_append_text(GTK_COMBO_BOX(combo_box), _("GB2312"));
227     gtk_combo_box_append_text(GTK_COMBO_BOX(combo_box), _("GBK"));
228 #endif
229
230     const gchar *charset_tooltip = _("Choosing GBK over GB2312 will trade some speed "
231                                      "with a larger charset in which I search candidate "
232                                      "words/characters for you.");
233 #ifdef SUNPINYIN_USE_GTK_TOOLTIPS
234     gtk_tooltips_set_tip(tooltips, combo_box, charset_tooltip, NULL);
235 #else
236     gtk_widget_set_tooltip_text(combo_box, charset_tooltip);
237 #endif
238
239     g_signal_connect(G_OBJECT(combo_box), "changed",
240                      G_CALLBACK(on_value_changed), NULL);
241     charset_combo = combo_box;
242
243     button = gtk_check_button_new_with_mnemonic (_("Use -/= for paging down/up"));
244     gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
245     g_signal_connect(G_OBJECT(button), "toggled",
246                      G_CALLBACK(on_value_changed), NULL);
247     minus_pageup_button = button;
248
249     button = gtk_check_button_new_with_mnemonic (_("Use [/] for paging down/up"));
250     gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
251     g_signal_connect(G_OBJECT(button), "toggled",
252                      G_CALLBACK(on_value_changed), NULL);
253     bracket_pageup_button = button;
254
255     button = gtk_check_button_new_with_mnemonic (_("Use ,/. for paging down/up"));
256     gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
257     g_signal_connect(G_OBJECT(button), "toggled",
258                      G_CALLBACK(on_value_changed), NULL);
259     comma_pageup_button = button;
260
261     // MemoryPower
262     GtkWidget *hbox;
263
264 #ifdef SUNPINYIN_USE_GTK_BOX
265     hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
266 #else
267     hbox = gtk_hbox_new (FALSE, 0);
268 #endif
269
270     gtk_widget_show (hbox);
271     gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
272
273     label = gtk_label_new (NULL);
274     gtk_label_set_text_with_mnemonic (GTK_LABEL (label), _("M_emory power:"));
275     gtk_widget_show (label);
276     gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 2);
277     gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
278     gtk_misc_set_padding (GTK_MISC (label), 2, 0);
279
280     button = gtk_spin_button_new_with_range (0, 10, 1);
281
282     const gchar *memory_power_tooltip =  _("The larger this number is, the faster I "
283                                            "memorize/forget new words.");
284 #ifdef SUNPINYIN_USE_GTK_TOOLTIPS
285     gtk_tooltips_set_tip(tooltips, button, memory_power_tooltip, NULL);
286 #else
287     gtk_widget_set_tooltip_text(button, memory_power_tooltip);
288 #endif
289
290     g_signal_connect (G_OBJECT(button), "value_changed",
291                       G_CALLBACK(on_value_changed), NULL);
292     gtk_widget_show (button);
293     gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
294     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (button), TRUE);
295     gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (button), TRUE);
296     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (button), 0);
297     gtk_label_set_mnemonic_widget (GTK_LABEL (label), button);
298     memory_power_button = button;
299     
300     return vbox;
301 }
302
303 static GtkWidget *
304 create_setup_window ()
305 {
306     GtkWidget *notebook;
307     GtkWidget *label;
308     GtkWidget *page;
309 #ifdef SUNPINYIN_USE_GTK_TOOLTIPS
310     GtkTooltips *tooltips;
311
312     // Create the shared tooltips.
313     tooltips = gtk_tooltips_new ();
314 #endif
315
316     notebook = gtk_notebook_new ();
317
318     // Create the option page. 
319 #ifdef SUNPINYIN_USE_GTK_TOOLTIPS
320     page = create_options_page(tooltips);
321 #else
322     page = create_options_page();
323 #endif
324     label = gtk_label_new (_("Options"));
325     gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page, label);
326
327     gtk_notebook_set_current_page(GTK_NOTEBOOK (notebook), 0);
328
329     gtk_widget_show_all(notebook);
330     return notebook;
331 }
332
333 static void
334 load_config (const ConfigPointer &config)
335 {
336     if (config.null())
337         return;
338
339     int viewtype = config->read (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_VIEW_TYPE), 0);
340     gtk_combo_box_set_active (GTK_COMBO_BOX(input_style_combo), viewtype);
341
342     int charset  = config->read (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_CHARHSET), 1);
343     gtk_combo_box_set_active (GTK_COMBO_BOX(charset_combo), charset);
344
345     bool stat;
346     stat = config->read (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_PAGE_MINUS), true);
347     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(minus_pageup_button), stat);
348
349     stat = config->read (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_PAGE_BRACKET), true);
350     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(bracket_pageup_button), stat);
351
352     stat = config->read (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_PAGE_COMMA), false);
353     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(comma_pageup_button), stat);
354
355     gint value;
356     value = config->read (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_MEMORY_POWER), 5);
357     gtk_spin_button_set_value(GTK_SPIN_BUTTON(memory_power_button), value);
358
359     __have_changed = false;
360 }
361
362 static void
363 save_config (const ConfigPointer &config)
364 {
365     if (config.null())
366         return;
367
368     int no = gtk_combo_box_get_active(GTK_COMBO_BOX(input_style_combo));
369     config->write (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_VIEW_TYPE), no);
370
371     no = gtk_combo_box_get_active(GTK_COMBO_BOX(charset_combo));
372     config->write (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_CHARHSET), no);
373
374     gboolean stat;
375     stat = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(minus_pageup_button));
376     config->write (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_PAGE_MINUS), (bool)stat);
377
378     stat = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(bracket_pageup_button));
379     config->write (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_PAGE_BRACKET), (bool)stat);
380
381     stat = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(comma_pageup_button));
382     config->write (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_PAGE_COMMA), (bool)stat);
383
384     gint value;
385     value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(memory_power_button));
386     config->write (String (SCIM_CONFIG_IMENGINE_SUNPINYIN_USER_MEMORY_POWER), value);
387     __have_changed = false;
388 }
389
390 bool
391 query_changed ()
392 {
393     return __have_changed;
394 }