6b83ab7a737523c8b28edcd6fbe57eada433612f
[platform/core/uifw/ise-engine-sunpinyin.git] / wrapper / xim / preferences.c
1 /*
2  * Copyright (c) 2010 Mike Qin <mikeandmore@gmail.com>
3  *
4  * The contents of this file are subject to the terms of either the GNU Lesser
5  * General Public License Version 2.1 only ("LGPL") or the Common Development and
6  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
7  * file except in compliance with the License. You can obtain a copy of the CDDL at
8  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
9  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
10  * specific language governing permissions and limitations under the License. When
11  * distributing the software, include this License Header Notice in each file and
12  * include the full text of the License in the License file as well as the
13  * following notice:
14  *
15  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
16  * (CDDL)
17  * For Covered Software in this distribution, this License shall be governed by the
18  * laws of the State of California (excluding conflict-of-law provisions).
19  * Any litigation relating to this License shall be subject to the jurisdiction of
20  * the Federal Courts of the Northern District of California and the state courts
21  * of the State of California, with venue lying in Santa Clara County, California.
22  *
23  * Contributor(s):
24  *
25  * If you wish your version of this file to be governed by only the CDDL or only
26  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
27  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
28  * license." If you don't indicate a single choice of license, a recipient has the
29  * option to distribute your version of this file under either the CDDL or the LGPL
30  * Version 2.1, or to extend the choice of license to its licensees as provided
31  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
32  * Version 2 license, then the option applies only if the new code is made subject
33  * to such option by the copyright holder.
34  */
35
36 #include <gtk/gtk.h>
37
38 #include <X11/X.h>
39 #include <X11/Xproto.h>
40 #include <X11/Xlib.h>
41 #include <X11/keysym.h>
42 #include <X11/Xutil.h>
43
44 #include <unistd.h>
45 #include <dirent.h>
46 #include <sys/types.h>
47
48 #include "settings.h"
49 #include "xmisc.h"
50
51 #define UI_FILE SUNPINYIN_XIM_SETTING_DIR"/settings_ui.xml"
52
53 static GtkWidget* main_wnd = NULL;
54 static GtkToggleButton* trigger_ctrl_check = NULL;
55 static GtkToggleButton* english_ctrl_check = NULL;
56 static GtkToggleButton* trigger_shift_check = NULL;
57 static GtkToggleButton* english_shift_check = NULL;
58 static GtkComboBox* trigger_combo = NULL;
59 static GtkComboBox* english_combo = NULL;
60 static GtkColorButton* background_color_btn = NULL;
61 static GtkFontButton* font_btn = NULL;
62 static GtkColorButton* font_color_btn = NULL;
63 static GtkAdjustment* opacity_value = NULL;
64 static GtkAdjustment* ncandidates = NULL;
65 static GtkToggleButton* minus_plus_check = NULL;
66 static GtkToggleButton* comma_period_check = NULL;
67 static GtkToggleButton* paren_check = NULL;
68 static GtkToggleButton* fuzzy_seg_check = NULL;
69 static GtkToggleButton* fuzzy_inner_seg_check = NULL;
70 static GtkToggleButton* cancel_on_backspace_check = NULL;
71 static GtkToggleButton* smart_punct_check = NULL;
72 static GtkToggleButton* shuangpin_check = NULL;
73 static GtkComboBox* shuangpin_combo = NULL;
74 static GtkComboBox* skin_combo = NULL;
75 static GtkToggleButton* hide_icbar_check = NULL;
76
77 #define RETRIEVE(name, macro)                                   \
78     name = macro(gtk_builder_get_object(builder, # name))
79
80 static const char* ui_shuangpin_schemes[] =
81 {
82     "MS2003", "ABC", "ZiRanMa", "PinYin++", "ZiGuang", "XiaoHe",
83 };
84
85 #define UI_SHUANGPIN_SCHEMES_NUM 6
86
87 static const int ui_keysym_model[] =
88 {
89     XK_space, XK_Control_L, XK_Control_R, XK_Shift_L, XK_Shift_R
90 };
91
92 #define UI_KEYSYM_MODEL_NUM 5
93
94 #define INIT_KEY_SETTING(prefix)                                        \
95     do {                                                                \
96         if (hk.modifiers & ControlMask) {                               \
97             gtk_toggle_button_set_active(prefix##_ctrl_check, TRUE);    \
98         }                                                               \
99         if (hk.modifiers & ShiftMask)                                   \
100             gtk_toggle_button_set_active(prefix##_shift_check, TRUE);   \
101         int i;                                                          \
102         for (i = 0; i < UI_KEYSYM_MODEL_NUM; i++) {                     \
103             if (hk.keysym == ui_keysym_model[i]) {                      \
104                 gtk_combo_box_set_active(prefix##_combo, i);            \
105                 break;                                                  \
106             }                                                           \
107         }                                                               \
108     } while(0)
109
110 #define INIT_COLOR_SETTING(widget_name)                         \
111     do {                                                        \
112         GdkColor color;                                         \
113         gdk_color_parse(colorstr, &color);                      \
114         gtk_color_button_set_color((widget_name), &color);      \
115     } while(0)
116
117 static int
118 fill_skin_list(const char* dirpath, varchar names[], int idx)
119 {
120     DIR* dir = opendir(dirpath);
121     if (!dir) {
122         mkdir(dirpath, 0644);
123         return idx;
124     }
125
126     struct dirent* ent = NULL;
127     while ((ent = readdir(dir))) {
128         if (ent->d_name[0] == '.') continue;
129         if (strcmp(ent->d_name, "classic") == 0) continue;
130
131         strncpy(names[idx], ent->d_name, sizeof(varchar));
132         idx++;
133     }
134     closedir(dir);
135     return idx;
136 }
137
138 #define SYSTEM_SKIN_DIR SUNPINYIN_XIM_SETTING_DIR"/skins"
139 #define USER_SKIN_DIR "%s/.sunpinyin/xim_skins"
140 #define MAX_SKINS 256
141
142 static int
143 list_skins(const char* current_skin_name)
144 {
145     int idx_ret = 0;
146     int skin_count = 0;
147     varchar skins[MAX_SKINS];
148
149     GtkListStore* model = GTK_LIST_STORE(gtk_combo_box_get_model(skin_combo));
150     GtkTreeIter iter;
151     gtk_list_store_append(model, &iter);
152     gtk_list_store_set(model, &iter, 0, "classic", -1);
153
154     skin_count = fill_skin_list(SYSTEM_SKIN_DIR, skins, skin_count);
155     varchar dirpath;
156     snprintf(dirpath, sizeof(varchar), USER_SKIN_DIR, getenv("HOME"));
157     skin_count = fill_skin_list(dirpath, skins, skin_count);
158
159     /* sort and unique the names */
160     qsort(skins, skin_count, sizeof(varchar),
161           (int (*)(const void*, const void*)) strcmp);
162
163     int i, j, idx = 1;
164     for (i = 0; i < skin_count; i++) {
165         for (j = i + 1; j < skin_count; j++) {
166             if (strcmp(skins[i], skins[j]) == 0) {
167                 i = j;
168             } else {
169                 break;
170             }
171         }
172         if (strcmp(skins[i], current_skin_name) == 0) {
173             idx_ret = idx;
174         }
175         gtk_list_store_append(model, &iter);
176         gtk_list_store_set(model, &iter, 0, skins[i], -1);
177         idx++;
178     }
179
180     return idx_ret;
181 }
182
183 static void
184 init_settings(void)
185 {
186     settings_init();
187     settings_load();
188
189     hotkey_t hk;
190     settings_get(TRIGGER_KEY, &hk);
191     INIT_KEY_SETTING(trigger);
192
193     settings_get(ENG_KEY, &hk);
194     INIT_KEY_SETTING(english);
195
196     varchar colorstr;
197     settings_get(PREEDIT_COLOR, colorstr);
198     INIT_COLOR_SETTING(background_color_btn);
199
200     settings_get(PREEDIT_FONT_COLOR, colorstr);
201     INIT_COLOR_SETTING(font_color_btn);
202
203     varchar fontstr;
204     settings_get(PREEDIT_FONT, fontstr);
205     gtk_font_button_set_font_name(font_btn, fontstr);
206
207
208     gtk_adjustment_set_value(opacity_value,
209                              settings_get_double(PREEDIT_OPACITY));
210
211     gtk_adjustment_set_value(ncandidates, settings_get_int(CANDIDATES_SIZE));
212
213     gtk_toggle_button_set_active(minus_plus_check,
214                                  settings_get_int(PAGE_MINUS_PLUS));
215     gtk_toggle_button_set_active(comma_period_check,
216                                  settings_get_int(PAGE_COMMA_PERIOD));
217     gtk_toggle_button_set_active(paren_check,
218                                  settings_get_int(PAGE_PAREN));
219
220     gtk_toggle_button_set_active(fuzzy_seg_check,
221                                  settings_get_int(FUZZY_SEGMENTATION));
222     gtk_toggle_button_set_active(fuzzy_inner_seg_check,
223                                  settings_get_int(FUZZY_INNER_SEGMENTATION));
224
225     gtk_toggle_button_set_active(cancel_on_backspace_check,
226                                  settings_get_int(CANCEL_ON_BACKSPACE));
227
228     gtk_toggle_button_set_active(smart_punct_check,
229                                  settings_get_int(SMART_PUNCT));
230
231     gtk_toggle_button_set_active(shuangpin_check,
232                                  settings_get_int(SHUANGPIN));
233     varchar scheme;
234     int i;
235     settings_get(SHUANGPIN_SCHEME, scheme);
236     for (i = 0; i < UI_SHUANGPIN_SCHEMES_NUM; i++) {
237         if (strcmp(ui_shuangpin_schemes[i], scheme) == 0) {
238             gtk_combo_box_set_active(shuangpin_combo, i);
239             break;
240         }
241     }
242
243     /* skin */
244     varchar skin_name;
245     settings_get(SKIN_NAME, skin_name);
246     int idx = list_skins(skin_name);
247     gtk_combo_box_set_active(skin_combo, idx);
248
249     gtk_toggle_button_set_active(hide_icbar_check,
250                                  settings_get_int(HIDE_ICBAR));
251 }
252
253 static void
254 init(void)
255 {
256     GtkBuilder* builder = gtk_builder_new();
257     gtk_builder_add_from_file(builder, UI_FILE, NULL);
258     main_wnd = GTK_WIDGET(gtk_builder_get_object(builder, "settings_dialog"));
259
260     RETRIEVE(trigger_ctrl_check, GTK_TOGGLE_BUTTON);
261     RETRIEVE(english_ctrl_check, GTK_TOGGLE_BUTTON);
262     RETRIEVE(trigger_shift_check, GTK_TOGGLE_BUTTON);
263     RETRIEVE(english_shift_check, GTK_TOGGLE_BUTTON);
264     RETRIEVE(trigger_combo, GTK_COMBO_BOX);
265     RETRIEVE(english_combo, GTK_COMBO_BOX);
266     RETRIEVE(background_color_btn, GTK_COLOR_BUTTON);
267     RETRIEVE(font_btn, GTK_FONT_BUTTON);
268     RETRIEVE(font_color_btn, GTK_COLOR_BUTTON);
269     RETRIEVE(opacity_value, GTK_ADJUSTMENT);
270     RETRIEVE(ncandidates, GTK_ADJUSTMENT);
271     RETRIEVE(minus_plus_check, GTK_TOGGLE_BUTTON);
272     RETRIEVE(comma_period_check, GTK_TOGGLE_BUTTON);
273     RETRIEVE(paren_check, GTK_TOGGLE_BUTTON);
274     RETRIEVE(fuzzy_seg_check, GTK_TOGGLE_BUTTON);
275     RETRIEVE(fuzzy_inner_seg_check, GTK_TOGGLE_BUTTON);
276     RETRIEVE(cancel_on_backspace_check, GTK_TOGGLE_BUTTON);
277     RETRIEVE(smart_punct_check, GTK_TOGGLE_BUTTON);
278     RETRIEVE(shuangpin_check, GTK_TOGGLE_BUTTON);
279     RETRIEVE(shuangpin_combo, GTK_COMBO_BOX);
280     RETRIEVE(skin_combo, GTK_COMBO_BOX);
281     RETRIEVE(hide_icbar_check, GTK_TOGGLE_BUTTON);
282
283     init_settings();
284
285     gtk_builder_connect_signals(builder, NULL);
286
287     g_object_unref(builder);
288 }
289
290 #define REFRESH_KEY_SETTING(prefix)                                     \
291     do {                                                                \
292         hk.modifiers = hk.keysym = 0;                                   \
293         if (gtk_toggle_button_get_active(prefix##_ctrl_check)) {        \
294             hk.modifiers |= ControlMask;                                \
295         }                                                               \
296         if (gtk_toggle_button_get_active(prefix##_shift_check)) {       \
297             hk.modifiers |= ShiftMask;                                  \
298         }                                                               \
299         int idx = gtk_combo_box_get_active(prefix##_combo);             \
300         if (idx >= 0)                                                   \
301             hk.keysym = ui_keysym_model[idx];                           \
302     } while (0)
303
304 #define REFRESH_COLOR_SETTING(widget_name)                              \
305     do {                                                                \
306         GdkColor color;                                                 \
307         gtk_color_button_get_color((widget_name), &color);              \
308         snprintf(colorstr, sizeof(varchar), "#%.2X%.2X%.2X",            \
309                  color.red >> 8, color.green >> 8, color.blue >> 8);    \
310     } while(0)
311
312 static void
313 send_reload()
314 {
315     /* notify all running xsunpinyin with this user */
316     char cmd[256];
317     snprintf(cmd, 256, "/usr/bin/pkill -10 '^xsunpinyin$' -u %d", getuid());
318     system(cmd);
319 }
320
321 void
322 state_changed()
323 {
324     hotkey_t hk;
325     REFRESH_KEY_SETTING(trigger);
326     settings_set(TRIGGER_KEY, &hk);
327
328     REFRESH_KEY_SETTING(english);
329     settings_set(ENG_KEY, &hk);
330
331     varchar colorstr;
332     REFRESH_COLOR_SETTING(background_color_btn);
333     settings_set(PREEDIT_COLOR, colorstr);
334
335     REFRESH_COLOR_SETTING(font_color_btn);
336     settings_set(PREEDIT_FONT_COLOR, colorstr);
337
338     /* font and size information */
339     settings_set(PREEDIT_FONT, (void*) gtk_font_button_get_font_name(font_btn));
340
341     /* font color information */
342     settings_set_double(PREEDIT_OPACITY,
343                         gtk_adjustment_get_value(opacity_value));
344
345     settings_set_int(CANDIDATES_SIZE,
346                      gtk_adjustment_get_value(ncandidates));
347
348     /* page up and down trigger */
349     settings_set_int(PAGE_MINUS_PLUS,
350                      gtk_toggle_button_get_active(minus_plus_check));
351     settings_set_int(PAGE_COMMA_PERIOD,
352                      gtk_toggle_button_get_active(comma_period_check));
353     settings_set_int(PAGE_PAREN,
354                      gtk_toggle_button_get_active(paren_check));
355
356     /* fuzzy segmentation */
357     settings_set_int(FUZZY_SEGMENTATION,
358                      gtk_toggle_button_get_active(fuzzy_seg_check));
359     settings_set_int(FUZZY_INNER_SEGMENTATION,
360                      gtk_toggle_button_get_active(fuzzy_inner_seg_check));
361
362     /* cancel on backspace */
363     settings_set_int(CANCEL_ON_BACKSPACE,
364                      gtk_toggle_button_get_active(cancel_on_backspace_check));
365
366     /* smart punctuation */
367     settings_set_int(SMART_PUNCT,
368                      gtk_toggle_button_get_active(smart_punct_check));
369
370     settings_set_int(SHUANGPIN, gtk_toggle_button_get_active(shuangpin_check));
371     int sche_idx = gtk_combo_box_get_active(shuangpin_combo);
372     if (sche_idx < UI_SHUANGPIN_SCHEMES_NUM)
373         settings_set_string(SHUANGPIN_SCHEME, ui_shuangpin_schemes[sche_idx]);
374
375     /* skins */
376     settings_set_string(SKIN_NAME, gtk_combo_box_get_active_text(skin_combo));
377
378     /* whether hide icbar */
379     settings_set_int(HIDE_ICBAR, gtk_toggle_button_get_active(hide_icbar_check));
380
381     settings_save();
382     send_reload();
383 }
384
385 int main(int argc, char *argv[])
386 {
387     init_display(&argc, &argv);
388     init();
389
390     gtk_widget_show(main_wnd);
391
392     gtk_main();
393     return 0;
394 }