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>
51 #define UI_FILE SUNPINYIN_XIM_SETTING_DIR"/settings_ui.xml"
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;
77 #define RETRIEVE(name, macro) \
78 name = macro(gtk_builder_get_object(builder, # name))
80 static const char* ui_shuangpin_schemes[] =
82 "MS2003", "ABC", "ZiRanMa", "PinYin++", "ZiGuang", "XiaoHe",
85 #define UI_SHUANGPIN_SCHEMES_NUM 6
87 static const int ui_keysym_model[] =
89 XK_space, XK_Control_L, XK_Control_R, XK_Shift_L, XK_Shift_R
92 #define UI_KEYSYM_MODEL_NUM 5
94 #define INIT_KEY_SETTING(prefix) \
96 if (hk.modifiers & ControlMask) { \
97 gtk_toggle_button_set_active(prefix##_ctrl_check, TRUE); \
99 if (hk.modifiers & ShiftMask) \
100 gtk_toggle_button_set_active(prefix##_shift_check, TRUE); \
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); \
110 #define INIT_COLOR_SETTING(widget_name) \
113 gdk_color_parse(colorstr, &color); \
114 gtk_color_button_set_color((widget_name), &color); \
118 fill_skin_list(const char* dirpath, varchar names[], int idx)
120 DIR* dir = opendir(dirpath);
122 mkdir(dirpath, 0644);
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;
131 strncpy(names[idx], ent->d_name, sizeof(varchar));
138 #define SYSTEM_SKIN_DIR SUNPINYIN_XIM_SETTING_DIR"/skins"
139 #define USER_SKIN_DIR "%s/.sunpinyin/xim_skins"
140 #define MAX_SKINS 256
143 list_skins(const char* current_skin_name)
147 varchar skins[MAX_SKINS];
149 GtkListStore* model = GTK_LIST_STORE(gtk_combo_box_get_model(skin_combo));
151 gtk_list_store_append(model, &iter);
152 gtk_list_store_set(model, &iter, 0, "classic", -1);
154 skin_count = fill_skin_list(SYSTEM_SKIN_DIR, skins, skin_count);
156 snprintf(dirpath, sizeof(varchar), USER_SKIN_DIR, getenv("HOME"));
157 skin_count = fill_skin_list(dirpath, skins, skin_count);
159 /* sort and unique the names */
160 qsort(skins, skin_count, sizeof(varchar),
161 (int (*)(const void*, const void*)) strcmp);
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) {
172 if (strcmp(skins[i], current_skin_name) == 0) {
175 gtk_list_store_append(model, &iter);
176 gtk_list_store_set(model, &iter, 0, skins[i], -1);
190 settings_get(TRIGGER_KEY, &hk);
191 INIT_KEY_SETTING(trigger);
193 settings_get(ENG_KEY, &hk);
194 INIT_KEY_SETTING(english);
197 settings_get(PREEDIT_COLOR, colorstr);
198 INIT_COLOR_SETTING(background_color_btn);
200 settings_get(PREEDIT_FONT_COLOR, colorstr);
201 INIT_COLOR_SETTING(font_color_btn);
204 settings_get(PREEDIT_FONT, fontstr);
205 gtk_font_button_set_font_name(font_btn, fontstr);
208 gtk_adjustment_set_value(opacity_value,
209 settings_get_double(PREEDIT_OPACITY));
211 gtk_adjustment_set_value(ncandidates, settings_get_int(CANDIDATES_SIZE));
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));
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));
225 gtk_toggle_button_set_active(cancel_on_backspace_check,
226 settings_get_int(CANCEL_ON_BACKSPACE));
228 gtk_toggle_button_set_active(smart_punct_check,
229 settings_get_int(SMART_PUNCT));
231 gtk_toggle_button_set_active(shuangpin_check,
232 settings_get_int(SHUANGPIN));
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);
245 settings_get(SKIN_NAME, skin_name);
246 int idx = list_skins(skin_name);
247 gtk_combo_box_set_active(skin_combo, idx);
249 gtk_toggle_button_set_active(hide_icbar_check,
250 settings_get_int(HIDE_ICBAR));
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"));
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);
285 gtk_builder_connect_signals(builder, NULL);
287 g_object_unref(builder);
290 #define REFRESH_KEY_SETTING(prefix) \
292 hk.modifiers = hk.keysym = 0; \
293 if (gtk_toggle_button_get_active(prefix##_ctrl_check)) { \
294 hk.modifiers |= ControlMask; \
296 if (gtk_toggle_button_get_active(prefix##_shift_check)) { \
297 hk.modifiers |= ShiftMask; \
299 int idx = gtk_combo_box_get_active(prefix##_combo); \
301 hk.keysym = ui_keysym_model[idx]; \
304 #define REFRESH_COLOR_SETTING(widget_name) \
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); \
315 /* notify all running xsunpinyin with this user */
317 snprintf(cmd, 256, "/usr/bin/pkill -10 '^xsunpinyin$' -u %d", getuid());
325 REFRESH_KEY_SETTING(trigger);
326 settings_set(TRIGGER_KEY, &hk);
328 REFRESH_KEY_SETTING(english);
329 settings_set(ENG_KEY, &hk);
332 REFRESH_COLOR_SETTING(background_color_btn);
333 settings_set(PREEDIT_COLOR, colorstr);
335 REFRESH_COLOR_SETTING(font_color_btn);
336 settings_set(PREEDIT_FONT_COLOR, colorstr);
338 /* font and size information */
339 settings_set(PREEDIT_FONT, (void*) gtk_font_button_get_font_name(font_btn));
341 /* font color information */
342 settings_set_double(PREEDIT_OPACITY,
343 gtk_adjustment_get_value(opacity_value));
345 settings_set_int(CANDIDATES_SIZE,
346 gtk_adjustment_get_value(ncandidates));
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));
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));
362 /* cancel on backspace */
363 settings_set_int(CANCEL_ON_BACKSPACE,
364 gtk_toggle_button_get_active(cancel_on_backspace_check));
366 /* smart punctuation */
367 settings_set_int(SMART_PUNCT,
368 gtk_toggle_button_get_active(smart_punct_check));
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]);
376 settings_set_string(SKIN_NAME, gtk_combo_box_get_active_text(skin_combo));
378 /* whether hide icbar */
379 settings_set_int(HIDE_ICBAR, gtk_toggle_button_get_active(hide_icbar_check));
385 int main(int argc, char *argv[])
387 init_display(&argc, &argv);
390 gtk_widget_show(main_wnd);