2 * Copyright (c) 2010 Mike Qin <mikeandmore@gmail.com>
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
15 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
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.
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.
39 #include <X11/Xproto.h>
41 #include <X11/keysym.h>
42 #include <X11/Xutil.h>
46 #include <sys/types.h>
52 #define UI_FILE SUNPINYIN_XIM_SETTING_DIR"/settings_ui.xml"
54 static GtkWidget* main_wnd = NULL;
55 static GtkToggleButton* trigger_ctrl_check = NULL;
56 static GtkToggleButton* english_ctrl_check = NULL;
57 static GtkToggleButton* trigger_shift_check = NULL;
58 static GtkToggleButton* english_shift_check = NULL;
59 static GtkComboBox* trigger_combo = NULL;
60 static GtkComboBox* english_combo = NULL;
61 static GtkColorButton* background_color_btn = NULL;
62 static GtkFontButton* font_btn = NULL;
63 static GtkColorButton* font_color_btn = NULL;
64 static GtkAdjustment* opacity_value = NULL;
65 static GtkAdjustment* ncandidates = NULL;
66 static GtkToggleButton* minus_plus_check = NULL;
67 static GtkToggleButton* comma_period_check = NULL;
68 static GtkToggleButton* paren_check = NULL;
69 static GtkToggleButton* fuzzy_seg_check = NULL;
70 static GtkToggleButton* fuzzy_inner_seg_check = NULL;
71 static GtkToggleButton* cancel_on_backspace_check = NULL;
72 static GtkToggleButton* smart_punct_check = NULL;
73 static GtkToggleButton* shuangpin_check = NULL;
74 static GtkComboBox* shuangpin_combo = NULL;
75 static GtkComboBox* skin_combo = NULL;
76 static GtkToggleButton* hide_icbar_check = NULL;
78 #define RETRIEVE(name, macro) \
79 name = macro(gtk_builder_get_object(builder, # name))
81 static const char* ui_shuangpin_schemes[] =
83 "MS2003", "ABC", "ZiRanMa", "PinYin++", "ZiGuang", "XiaoHe",
86 #define UI_SHUANGPIN_SCHEMES_NUM 6
88 static const int ui_keysym_model[] =
90 XK_space, XK_Control_L, XK_Control_R, XK_Shift_L, XK_Shift_R
93 #define UI_KEYSYM_MODEL_NUM 5
95 #define INIT_KEY_SETTING(prefix) \
97 if (hk.modifiers & ControlMask) { \
98 gtk_toggle_button_set_active(prefix##_ctrl_check, TRUE); \
100 if (hk.modifiers & ShiftMask) \
101 gtk_toggle_button_set_active(prefix##_shift_check, TRUE); \
103 for (i = 0; i < UI_KEYSYM_MODEL_NUM; i++) { \
104 if (hk.keysym == ui_keysym_model[i]) { \
105 gtk_combo_box_set_active(prefix##_combo, i); \
111 #define INIT_COLOR_SETTING(widget_name) \
114 gdk_color_parse(colorstr, &color); \
115 gtk_color_button_set_color((widget_name), &color); \
119 fill_skin_list(const char* dirpath, varchar names[], int idx)
121 DIR* dir = opendir(dirpath);
123 mkdir(dirpath, 0644);
127 struct dirent* ent = NULL;
128 while ((ent = readdir(dir))) {
129 if (ent->d_name[0] == '.') continue;
130 if (strcmp(ent->d_name, "classic") == 0) continue;
132 strncpy(names[idx], ent->d_name, sizeof(varchar));
139 #define SYSTEM_SKIN_DIR SUNPINYIN_XIM_SETTING_DIR"/skins"
140 #define USER_SKIN_DIR "%s/.sunpinyin/xim_skins"
141 #define MAX_SKINS 256
144 list_skins(const char* current_skin_name)
148 varchar skins[MAX_SKINS];
150 GtkListStore* model = GTK_LIST_STORE(gtk_combo_box_get_model(skin_combo));
152 gtk_list_store_append(model, &iter);
153 gtk_list_store_set(model, &iter, 0, "classic", -1);
155 skin_count = fill_skin_list(SYSTEM_SKIN_DIR, skins, skin_count);
157 snprintf(dirpath, sizeof(varchar), USER_SKIN_DIR, getenv("HOME"));
158 skin_count = fill_skin_list(dirpath, skins, skin_count);
160 /* sort and unique the names */
161 qsort(skins, skin_count, sizeof(varchar),
162 (int (*)(const void*, const void*)) strcmp);
165 for (i = 0; i < skin_count; i++) {
166 for (j = i + 1; j < skin_count; j++) {
167 if (strcmp(skins[i], skins[j]) == 0) {
173 if (strcmp(skins[i], current_skin_name) == 0) {
176 gtk_list_store_append(model, &iter);
177 gtk_list_store_set(model, &iter, 0, skins[i], -1);
191 settings_get(TRIGGER_KEY, &hk);
192 INIT_KEY_SETTING(trigger);
194 settings_get(ENG_KEY, &hk);
195 INIT_KEY_SETTING(english);
198 settings_get(PREEDIT_COLOR, colorstr);
199 INIT_COLOR_SETTING(background_color_btn);
201 settings_get(PREEDIT_FONT_COLOR, colorstr);
202 INIT_COLOR_SETTING(font_color_btn);
205 settings_get(PREEDIT_FONT, fontstr);
206 gtk_font_button_set_font_name(font_btn, fontstr);
209 gtk_adjustment_set_value(opacity_value,
210 settings_get_double(PREEDIT_OPACITY));
212 gtk_adjustment_set_value(ncandidates, settings_get_int(CANDIDATES_SIZE));
214 gtk_toggle_button_set_active(minus_plus_check,
215 settings_get_int(PAGE_MINUS_PLUS));
216 gtk_toggle_button_set_active(comma_period_check,
217 settings_get_int(PAGE_COMMA_PERIOD));
218 gtk_toggle_button_set_active(paren_check,
219 settings_get_int(PAGE_PAREN));
221 gtk_toggle_button_set_active(fuzzy_seg_check,
222 settings_get_int(FUZZY_SEGMENTATION));
223 gtk_toggle_button_set_active(fuzzy_inner_seg_check,
224 settings_get_int(FUZZY_INNER_SEGMENTATION));
226 gtk_toggle_button_set_active(cancel_on_backspace_check,
227 settings_get_int(CANCEL_ON_BACKSPACE));
229 gtk_toggle_button_set_active(smart_punct_check,
230 settings_get_int(SMART_PUNCT));
232 gtk_toggle_button_set_active(shuangpin_check,
233 settings_get_int(SHUANGPIN));
236 settings_get(SHUANGPIN_SCHEME, scheme);
237 for (i = 0; i < UI_SHUANGPIN_SCHEMES_NUM; i++) {
238 if (strcmp(ui_shuangpin_schemes[i], scheme) == 0) {
239 gtk_combo_box_set_active(shuangpin_combo, i);
246 settings_get(SKIN_NAME, skin_name);
247 int idx = list_skins(skin_name);
248 gtk_combo_box_set_active(skin_combo, idx);
250 gtk_toggle_button_set_active(hide_icbar_check,
251 settings_get_int(HIDE_ICBAR));
257 GtkBuilder* builder = gtk_builder_new();
258 gtk_builder_add_from_file(builder, UI_FILE, NULL);
259 main_wnd = GTK_WIDGET(gtk_builder_get_object(builder, "settings_dialog"));
261 RETRIEVE(trigger_ctrl_check, GTK_TOGGLE_BUTTON);
262 RETRIEVE(english_ctrl_check, GTK_TOGGLE_BUTTON);
263 RETRIEVE(trigger_shift_check, GTK_TOGGLE_BUTTON);
264 RETRIEVE(english_shift_check, GTK_TOGGLE_BUTTON);
265 RETRIEVE(trigger_combo, GTK_COMBO_BOX);
266 RETRIEVE(english_combo, GTK_COMBO_BOX);
267 RETRIEVE(background_color_btn, GTK_COLOR_BUTTON);
268 RETRIEVE(font_btn, GTK_FONT_BUTTON);
269 RETRIEVE(font_color_btn, GTK_COLOR_BUTTON);
270 RETRIEVE(opacity_value, GTK_ADJUSTMENT);
271 RETRIEVE(ncandidates, GTK_ADJUSTMENT);
272 RETRIEVE(minus_plus_check, GTK_TOGGLE_BUTTON);
273 RETRIEVE(comma_period_check, GTK_TOGGLE_BUTTON);
274 RETRIEVE(paren_check, GTK_TOGGLE_BUTTON);
275 RETRIEVE(fuzzy_seg_check, GTK_TOGGLE_BUTTON);
276 RETRIEVE(fuzzy_inner_seg_check, GTK_TOGGLE_BUTTON);
277 RETRIEVE(cancel_on_backspace_check, GTK_TOGGLE_BUTTON);
278 RETRIEVE(smart_punct_check, GTK_TOGGLE_BUTTON);
279 RETRIEVE(shuangpin_check, GTK_TOGGLE_BUTTON);
280 RETRIEVE(shuangpin_combo, GTK_COMBO_BOX);
281 RETRIEVE(skin_combo, GTK_COMBO_BOX);
282 RETRIEVE(hide_icbar_check, GTK_TOGGLE_BUTTON);
286 gtk_builder_connect_signals(builder, NULL);
288 g_object_unref(builder);
291 #define REFRESH_KEY_SETTING(prefix) \
293 hk.modifiers = hk.keysym = 0; \
294 if (gtk_toggle_button_get_active(prefix##_ctrl_check)) { \
295 hk.modifiers |= ControlMask; \
297 if (gtk_toggle_button_get_active(prefix##_shift_check)) { \
298 hk.modifiers |= ShiftMask; \
300 int idx = gtk_combo_box_get_active(prefix##_combo); \
302 hk.keysym = ui_keysym_model[idx]; \
305 #define REFRESH_COLOR_SETTING(widget_name) \
308 gtk_color_button_get_color((widget_name), &color); \
309 snprintf(colorstr, sizeof(varchar), "#%.2X%.2X%.2X", \
310 color.red >> 8, color.green >> 8, color.blue >> 8); \
316 /* notify all running xsunpinyin with this user */
317 pid_t child_pid = fork();
321 snprintf(cmd, 256, "%d", getuid());
322 execl("/usr/bin/pkill", "/usr/bin/pkill", "-10", "^xsunpinyin$", "-u", cmd, (char*)0);
331 REFRESH_KEY_SETTING(trigger);
332 settings_set(TRIGGER_KEY, &hk);
334 REFRESH_KEY_SETTING(english);
335 settings_set(ENG_KEY, &hk);
338 REFRESH_COLOR_SETTING(background_color_btn);
339 settings_set(PREEDIT_COLOR, colorstr);
341 REFRESH_COLOR_SETTING(font_color_btn);
342 settings_set(PREEDIT_FONT_COLOR, colorstr);
344 /* font and size information */
345 settings_set(PREEDIT_FONT, (void*) gtk_font_button_get_font_name(font_btn));
347 /* font color information */
348 settings_set_double(PREEDIT_OPACITY,
349 gtk_adjustment_get_value(opacity_value));
351 settings_set_int(CANDIDATES_SIZE,
352 gtk_adjustment_get_value(ncandidates));
354 /* page up and down trigger */
355 settings_set_int(PAGE_MINUS_PLUS,
356 gtk_toggle_button_get_active(minus_plus_check));
357 settings_set_int(PAGE_COMMA_PERIOD,
358 gtk_toggle_button_get_active(comma_period_check));
359 settings_set_int(PAGE_PAREN,
360 gtk_toggle_button_get_active(paren_check));
362 /* fuzzy segmentation */
363 settings_set_int(FUZZY_SEGMENTATION,
364 gtk_toggle_button_get_active(fuzzy_seg_check));
365 settings_set_int(FUZZY_INNER_SEGMENTATION,
366 gtk_toggle_button_get_active(fuzzy_inner_seg_check));
368 /* cancel on backspace */
369 settings_set_int(CANCEL_ON_BACKSPACE,
370 gtk_toggle_button_get_active(cancel_on_backspace_check));
372 /* smart punctuation */
373 settings_set_int(SMART_PUNCT,
374 gtk_toggle_button_get_active(smart_punct_check));
376 settings_set_int(SHUANGPIN, gtk_toggle_button_get_active(shuangpin_check));
377 int sche_idx = gtk_combo_box_get_active(shuangpin_combo);
378 if (sche_idx < UI_SHUANGPIN_SCHEMES_NUM)
379 settings_set_string(SHUANGPIN_SCHEME, ui_shuangpin_schemes[sche_idx]);
382 settings_set_string(SKIN_NAME, gtk_combo_box_get_active_text(skin_combo));
384 /* whether hide icbar */
385 settings_set_int(HIDE_ICBAR, gtk_toggle_button_get_active(hide_icbar_check));
391 int main(int argc, char *argv[])
393 init_display(&argc, &argv);
396 gtk_widget_show(main_wnd);