forked from solomonhuang/gcin
/
win-sym.cpp
535 lines (423 loc) · 10.5 KB
/
win-sym.cpp
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#include <sys/stat.h>
#include "gcin.h"
#include "pho.h"
#include "gtab.h"
#include "win-sym.h"
#include "gtab-buf.h"
static GtkWidget *gwin_sym = NULL;
static int cur_in_method;
gboolean win_sym_enabled=0;
typedef struct {
char **sym;
int symN;
} SYM_ROW;
static SYM_ROW *syms;
static int symsN;
typedef struct {
SYM_ROW *syms;
int symsN;
} PAGE;
static PAGE *pages;
static int pagesN;
static int idx;
extern char *TableDir;
void get_gcin_user_or_sys_fname(char *name, char fname[]);
FILE *watch_fopen(char *filename, time_t *pfile_modify_time)
{
FILE *fp;
char fname[256];
get_gcin_user_or_sys_fname(filename, fname);
if ((fp=fopen(fname, "rb"))==NULL) {
#if UNIX
strcat(strcat(strcpy(fname, TableDir), "/"), filename);
#else
strcat(strcat(strcpy(fname, TableDir), "\\"), filename);
#endif
if ((fp=fopen(fname, "rb"))==NULL)
return NULL;
}
#if UNIX
struct stat st;
fstat(fileno(fp), &st);
#else
struct _stat st;
_fstat(fileno(fp), &st);
#endif
if (st.st_mtime == *pfile_modify_time) {
fclose(fp);
return NULL;
}
*pfile_modify_time = st.st_mtime;
return fp;
}
static void save_page()
{
if (!symsN)
return;
pages=trealloc(pages, PAGE, pagesN+1);
pages[pagesN].syms = syms;
pages[pagesN].symsN = symsN;
pagesN++;
syms = NULL;
symsN = 0;
}
static gboolean read_syms()
{
FILE *fp;
static char symbol_table[] = "symbol-table";
static time_t file_modify_time;
if ((fp=watch_fopen(symbol_table, &file_modify_time))==NULL)
return FALSE;
skip_utf8_sigature(fp);
int pg;
for(pg=0; pg < pagesN; pg++) {
syms = pages[pg].syms;
symsN = pages[pg].symsN;
int i;
for(i=0; i < symsN; i++) {
int j;
for(j=0; j < syms[i].symN; j++)
if (syms[i].sym[j])
free(syms[i].sym[j]);
}
free(syms);
}
pagesN = 0; pages = NULL;
syms = NULL; symsN = 0;
while (!feof(fp)) {
char tt[1024];
bzero(tt, sizeof(tt));
myfgets(tt, sizeof(tt), fp);
// dbg("%d] %s\n",strlen(tt), tt);
int len=strlen(tt);
#if 0
if (!len)
continue;
if (tt[len-1]=='\n') {
tt[len-1]=0;
}
#endif
if (tt[0]==0)
save_page();
if (tt[0]=='#')
continue;
char *p=tt;
syms=trealloc(syms, SYM_ROW, symsN+1);
SYM_ROW *psym = &syms[symsN++];
bzero(psym, sizeof(SYM_ROW));
while (*p) {
char *n = p;
while (*n && *n!='\t')
n++;
*n = 0;
psym->sym=trealloc(psym->sym, char *, psym->symN+1);
psym->sym[psym->symN++] = strdup(p);
p = n + 1;
}
if (!psym->symN) {
free(syms);
syms=NULL;
symsN=0;
}
}
if (symsN)
save_page();
fclose(fp);
idx = 0;
syms = pages[idx].syms;
symsN = pages[idx].symsN;
return TRUE;
}
gboolean add_to_tsin_buf(char *str, phokey_t *pho, int len);
void send_text_call_back(char *text);
void tsin_reset_in_pho(), reset_gtab_all(), clr_in_area_pho();
void force_preedit_shift();
gboolean output_gbuf();
void output_buffer_call_back();
gboolean gtab_cursor_end(),gtab_phrase_on();
void flush_tsin_buffer();
gboolean tsin_cursor_end();
void add_to_tsin_buf_str(char *str);
extern int c_len;
extern short gbufN;
static void cb_button_sym(GtkButton *button, GtkWidget *label)
{
// dbg("cb_button_sym\n");
char *str = (char *) gtk_label_get_text(GTK_LABEL(label));
#if USE_TSIN
if (current_method_type() == method_type_TSIN && current_CS->im_state == GCIN_STATE_CHINESE) {
add_to_tsin_buf_str(str);
if (tsin_cursor_end()) {
flush_tsin_buffer();
output_buffer_call_back();
} else {
force_preedit_shift();
}
}
else
#endif
if (gtab_phrase_on()) {
insert_gbuf_nokey(str);
if (gtab_cursor_end()) {
output_gbuf();
output_buffer_call_back();
} else
force_preedit_shift();
} else {
send_text_call_back(str);
}
switch (current_method_type()) {
case method_type_PHO:
clr_in_area_pho();
break;
#if USE_TSIN
case method_type_TSIN:
tsin_reset_in_pho();
break;
#endif
#if USE_ANTHY
case method_type_ANTHY:
break;
#endif
default:
reset_gtab_all();
break;
}
if (gcin_win_sym_click_close) {
win_sym_enabled=0;
hide_win_sym();
}
}
void update_active_in_win_geom();
extern int win_status_y;
void move_win_sym()
{
#if 0
dbg("move_win_sym %d\n", current_CS->in_method);
#endif
if (!gwin_sym)
return;
int wx, wy;
#if 0
if (gcin_pop_up_win) {
wx = dpy_xl;
} else
#endif
{
// dbg("win_y: %d %d\n", win_y, win_yl);
update_active_in_win_geom();
wx = win_x; wy = win_y + win_yl;
}
int winsym_xl, winsym_yl;
get_win_size(gwin_sym, &winsym_xl, &winsym_yl);
if (wx + winsym_xl > dpy_xl)
wx = dpy_xl - winsym_xl;
if (wx < 0)
wx = 0;
#if 0
if (gcin_pop_up_win) {
wy = win_status_y - winsym_yl;
} else
#endif
{
if (wy + winsym_yl > dpy_yl)
wy = win_y - winsym_yl;
if (wy < 0)
wy = 0;
}
gtk_window_move(GTK_WINDOW(gwin_sym), wx, wy);
}
void hide_win_sym()
{
if (!gwin_sym)
return;
gtk_widget_hide(gwin_sym);
}
void show_win_sym()
{
if (!current_CS)
return;
if (!gwin_sym || !win_sym_enabled || current_CS->im_state == GCIN_STATE_DISABLED)
return;
#if 0
dbg("show_win_sym\n");
#endif
gtk_widget_show_all(gwin_sym);
move_win_sym();
}
void lookup_gtab_out(char *ch, char *out);
void str_to_all_phokey_chars(char *b5_str, char *out);
static void sym_lookup_key(char *instr, char *outstr)
{
if (current_method_type() == method_type_PHO || current_method_type() == method_type_TSIN) {
str_to_all_phokey_chars(instr, outstr);
} else {
outstr[0]=0;
while (*instr) {
char tt[512];
tt[0]=0;
lookup_gtab_out(instr, tt);
strcat(outstr, tt);
instr+= utf8_sz(instr);
if (*instr)
strcat(outstr, " | ");
}
}
}
static void destory_win()
{
if (gwin_sym)
gtk_widget_destroy(gwin_sym);
gwin_sym = NULL;
}
static void disp_win_sym()
{
syms = pages[idx].syms;
symsN = pages[idx].symsN;
destory_win();
// win_sym_enabled = 0;
create_win_sym();
}
gboolean win_sym_page_up()
{
if (!win_sym_enabled)
return FALSE;
idx--;
if (idx < 0)
idx = pagesN - 1;
disp_win_sym();
return TRUE;
}
gboolean win_sym_page_down()
{
// dbg("win_sym_page_down\n");
if (!win_sym_enabled)
return FALSE;
idx = (idx+1) % pagesN;
disp_win_sym();
return TRUE;
}
static gboolean button_scroll_event(GtkWidget *widget,GdkEventScroll *event, gpointer user_data)
{
if (pagesN < 2)
return TRUE;
switch (event->direction) {
case GDK_SCROLL_UP:
win_sym_page_up();
break;
case GDK_SCROLL_DOWN:
win_sym_page_down();
break;
default:
break;
}
return TRUE;
}
static void mouse_button_callback_up_down( GtkWidget *widget,GdkEventButton *event, gpointer data)
{
GdkEventScroll sc;
sc.direction = data ? GDK_SCROLL_UP : GDK_SCROLL_DOWN;
button_scroll_event(NULL, &sc, NULL);
}
void create_win_sym()
{
if (!current_CS) {
dbg("create_win_sym, null CS\n");
return;
}
if (current_CS->in_method < 0 || current_CS->in_method >= MAX_GTAB_NUM_KEY) {
p_err("bad current_CS %d\n", current_CS->in_method);
}
if (current_method_type() != method_type_PHO && current_method_type() != method_type_TSIN && current_method_type() != method_type_ANTHY && !cur_inmd)
return;
if (read_syms() || cur_in_method != current_CS->in_method) {
destory_win();
} else {
if (!syms)
return;
}
if (gwin_sym) {
if (win_sym_enabled)
show_win_sym();
else
hide_win_sym();
return;
}
gwin_sym = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_has_resize_grip(GTK_WINDOW(gwin_sym), FALSE);
#if WIN32
set_no_focus(gwin_sym);
#endif
cur_in_method = current_CS->in_method;
GtkWidget *hbox_top = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (gwin_sym), hbox_top);
GtkWidget *vbox_top = gtk_vbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (hbox_top), vbox_top, TRUE, TRUE, 0);
gtk_container_set_border_width (GTK_CONTAINER (vbox_top), 0);
int i;
for(i=0; i < symsN; i++) {
SYM_ROW *psym = &syms[i];
GtkWidget *hbox_row = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox_top), hbox_row, FALSE, FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (hbox_row), 0);
int j;
for(j=0; j < psym->symN; j++) {
char *str = psym->sym[j];
if (!str[0])
continue;
GtkWidget *button = gtk_button_new();
GtkWidget *label = gtk_label_new(str);
gtk_container_add(GTK_CONTAINER(button), label);
set_label_font_size(label, gcin_font_size_symbol);
gtk_container_set_border_width (GTK_CONTAINER (button), 0);
gtk_box_pack_start (GTK_BOX (hbox_row), button, FALSE, FALSE, 0);
if (utf8_str_N(str) > 0) {
char phos[512];
sym_lookup_key(str, phos);
int phos_len = strlen(phos);
if (phos_len) {
#if GTK_CHECK_VERSION(2,12,0)
gtk_widget_set_tooltip_text (button, phos);
#else
GtkTooltips *button_pho_tips = gtk_tooltips_new ();
gtk_tooltips_set_tip (GTK_TOOLTIPS (button_pho_tips), button, phos, NULL);
#endif
}
}
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (cb_button_sym), label);
}
}
gtk_box_pack_start (GTK_BOX (hbox_top), gtk_vseparator_new(), FALSE, FALSE, 0);
GtkWidget *vbox_arrow = gtk_vbox_new (TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox_top), vbox_arrow, TRUE, TRUE, 0);
GtkWidget *eve_up=gtk_event_box_new(), *eve_down=gtk_event_box_new();
gtk_box_pack_start (GTK_BOX (vbox_arrow), eve_up, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(eve_up), gtk_arrow_new(GTK_ARROW_UP, GTK_SHADOW_IN));
gtk_box_pack_start (GTK_BOX (vbox_arrow), eve_down, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(eve_down), gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_IN));
g_signal_connect(G_OBJECT(eve_up),"button-press-event", G_CALLBACK(mouse_button_callback_up_down), (gpointer)1);
g_signal_connect(G_OBJECT(eve_down),"button-press-event", G_CALLBACK(mouse_button_callback_up_down), NULL);
gtk_widget_realize (gwin_sym);
#if UNIX
GdkWindow *gdkwin_sym = gtk_widget_get_window(gwin_sym);
set_no_focus(gwin_sym);
#else
win32_init_win(gwin_sym);
#endif
if (win_sym_enabled)
gtk_widget_show_all(gwin_sym);
g_signal_connect (G_OBJECT (gwin_sym), "scroll-event", G_CALLBACK (button_scroll_event), NULL);
move_win_sym();
#if 0
dbg("in_method:%d\n", current_CS->in_method);
#endif
return;
}
void toggle_win_sym()
{
win_sym_enabled^=1;
create_win_sym();
}
void change_win_sym_font_size()
{
}