1 /*
2 Copyright (C) 1994-2008 Edward Der-Hua Liu, Hsin-Chu, Taiwan
3 */
4
5 #define InAreaX (0)
6
7 #include "gcin.h"
8 #include "intcode.h"
9 #include "pho.h"
10 #include "gst.h"
11 #include "im-client/gcin-im-client-attr.h"
12 #include "win1.h"
13 #include "gcin-module.h"
14 #include "gcin-module-cb.h"
15
16 extern GtkWidget *gwin_int;
17 GCIN_module_main_functions gmf;
18 int current_intcode = INTCODE_UTF32;
19
20 static char inch[MAX_INTCODE];
21 int intcode_cin;
22
23 void create_win_intcode();
24 void module_show_win();
25
module_init_win(GCIN_module_main_functions * funcs)26 int module_init_win(GCIN_module_main_functions *funcs)
27 {
28 intcode_cin=0;
29 gmf = *funcs;
30 create_win_intcode();
31 return TRUE;
32 }
33
h2i(int x)34 static int h2i(int x)
35 {
36 return (x<='9'?x-'0':x-'A'+10);
37 }
38
utf32to8(char * t,char * s)39 static void utf32to8(char *t, char *s)
40 {
41 gsize rn,wn=0;
42 GError *err = NULL;
43 char *utf8 = g_convert(s, 4, "UTF-8", "UTF-32", &rn, &wn, &err);
44
45 if (utf8) {
46 memcpy(t, utf8, wn);
47 g_free(utf8);
48 }
49
50 t[wn]=0;
51 }
52
53
big5_utf8_n(char * s,int len,char out[])54 void big5_utf8_n(char *s, int len, char out[])
55 {
56 out[0]=0;
57
58 GError *err = NULL;
59 gsize rn, wn;
60 char *utf8 = g_convert(s, len, "UTF-8", "Big5", &rn, &wn, &err);
61
62 if (err) {
63 dbg("big5_utf8 convert error\n");
64 out[0]=0;
65 // abort();
66 return;
67 }
68
69 strcpy(out, utf8);
70 g_free(utf8);
71 }
72
73
big5_utf8(char * s,char out[])74 void big5_utf8(char *s, char out[])
75 {
76 big5_utf8_n(s, strlen(s), out);
77 }
78
79
80 static char *dstr[]={
81 "0", "1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
82 };
83
84 void disp_int(int index, char *intcode);
85 void clear_int_code_all();
86
module_feedkey(int key,int kvstate)87 gboolean module_feedkey(int key, int kvstate)
88 {
89 int i;
90 #if 0
91 if (key <= XK_KP_9 && key >= XK_KP_0)
92 key -= XK_KP_0 - '0';
93 #endif
94 key=toupper(key);
95 if (key==XK_BackSpace||key==XK_Delete) {
96 #if WIN32
97 if (*gmf.mf_test_mode)
98 return intcode_cin>0;
99 #endif
100 if (intcode_cin)
101 intcode_cin--;
102 else
103 return 0;
104
105 goto dispIn;
106 }
107 else
108 if ((key<'0'||key>'F'||(key>'9' && key<'A')) && (key!=' ')){
109 return 0;
110 }
111
112 if (current_intcode==INTCODE_BIG5) {
113 if (intcode_cin==0 && key<'8')
114 return 1;
115 if (intcode_cin==1 && inch[0]=='F' && key=='F')
116 return 1;
117 if (intcode_cin==2 && (key<'4' || (key>'7' && key<'A')))
118 return 1;
119 if (intcode_cin==3 && (inch[2]=='7'||inch[2]=='F') && key=='F')
120 return 1;
121 }
122
123 if (!intcode_cin && key==' ')
124 return 0;
125 #if WIN32
126 if (*gmf.mf_test_mode)
127 return 1;
128 #endif
129 if ((intcode_cin<MAX_INTCODE-1 || (current_intcode!=INTCODE_BIG5 && intcode_cin < MAX_INTCODE)) && key!=' ')
130 inch[intcode_cin++]=key;
131
132 dispIn:
133 clear_int_code_all();
134
135 #if 1
136 if (intcode_cin)
137 module_show_win();
138 #endif
139
140 for(i=0;i<intcode_cin;i++) {
141 disp_int(i, dstr[h2i(inch[i])]);
142 }
143
144 if ((current_intcode==INTCODE_BIG5 && intcode_cin==4 ||
145 current_intcode==INTCODE_UTF32 && intcode_cin==6) || key==' ') {
146 u_char utf8[CH_SZ+1];
147
148 if (current_intcode==INTCODE_BIG5) {
149 u_char ttt[3];
150 ttt[2]=ttt[3]=0;
151 ttt[0]=(h2i(inch[0])<<4)+h2i(inch[1]);
152 ttt[1]=(h2i(inch[2])<<4)+h2i(inch[3]);
153 big5_utf8((char *)ttt, (char *)utf8);
154 } else {
155 int i;
156 u_int v = 0;
157
158 for(i=0; i < intcode_cin; i++) {
159 v <<= 4;
160 v |= h2i(inch[i]);
161 }
162
163 utf32to8((char *)utf8, (char *)&v);
164 }
165
166 gmf.mf_send_utf8_ch((char *)utf8);
167 intcode_cin=0;
168
169 clear_int_code_all();
170 }
171
172 return 1;
173 }
174
175 extern GtkWidget *gwin_int;
176
module_get_preedit(char * str,GCIN_PREEDIT_ATTR attr[],int * cursor,int * comp_flag)177 int module_get_preedit(char *str, GCIN_PREEDIT_ATTR attr[], int *cursor, int *comp_flag)
178 {
179 #if WIN32 || 1
180 *comp_flag = intcode_cin>0;
181 #if 1
182 if (gwin_int && GTK_WIDGET_VISIBLE(gwin_int))
183 *comp_flag|=2;
184 #endif
185 // dbg("comp_len %x\n", *sub_comp_len);
186 #endif
187 str[0]=0;
188 *cursor=0;
189 return 0;
190 }
191
module_feedkey_release(KeySym xkey,int kbstate)192 int module_feedkey_release(KeySym xkey, int kbstate)
193 {
194 return 0;
195 }
196
module_flush_input()197 int module_flush_input()
198 {
199 return FALSE;
200 }
201
module_reset()202 int module_reset()
203 {
204 return TRUE;
205 }
206