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