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
|
/* GTK - The GTK+ Toolkit
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Theppitak Karoonboonyanan <thep@linux.thai.net>
*
*/
#include <string.h>
#include <gdk/gdkkeysyms.h>
#include "gtkimcontextthai.h"
#include "thai-charprop.h"
static void gtk_im_context_thai_class_init (GtkIMContextThaiClass *class);
static void gtk_im_context_thai_init (GtkIMContextThai *im_context_thai);
static gboolean gtk_im_context_thai_filter_keypress (GtkIMContext *context,
GdkEventKey *key);
#ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
static void forget_previous_chars (GtkIMContextThai *context_thai);
static void remember_previous_char (GtkIMContextThai *context_thai,
gunichar new_char);
#endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
static GObjectClass *parent_class;
GType gtk_type_im_context_thai = 0;
void
gtk_im_context_thai_register_type (GTypeModule *type_module)
{
static const GTypeInfo im_context_thai_info =
{
sizeof (GtkIMContextThaiClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gtk_im_context_thai_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GtkIMContextThai),
0,
(GInstanceInitFunc) gtk_im_context_thai_init,
};
gtk_type_im_context_thai =
g_type_module_register_type (type_module,
GTK_TYPE_IM_CONTEXT,
"GtkIMContextThai",
&im_context_thai_info, 0);
}
static void
gtk_im_context_thai_class_init (GtkIMContextThaiClass *class)
{
GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
parent_class = g_type_class_peek_parent (class);
im_context_class->filter_keypress = gtk_im_context_thai_filter_keypress;
}
static void
gtk_im_context_thai_init (GtkIMContextThai *context_thai)
{
#ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
forget_previous_chars (context_thai);
#endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
context_thai->isc_mode = ISC_BASICCHECK;
}
GtkIMContext *
gtk_im_context_thai_new (void)
{
GtkIMContextThai *result;
result = GTK_IM_CONTEXT_THAI (g_object_new (GTK_TYPE_IM_CONTEXT_THAI, NULL));
return GTK_IM_CONTEXT (result);
}
GtkIMContextThaiISCMode
gtk_im_context_thai_get_isc_mode (GtkIMContextThai *context_thai)
{
return context_thai->isc_mode;
}
GtkIMContextThaiISCMode
gtk_im_context_thai_set_isc_mode (GtkIMContextThai *context_thai,
GtkIMContextThaiISCMode mode)
{
GtkIMContextThaiISCMode prev_mode = context_thai->isc_mode;
context_thai->isc_mode = mode;
return prev_mode;
}
static gboolean
is_context_lost_key(guint keyval)
{
return ((keyval & 0xFF00) == 0xFF00) &&
(keyval == GDK_BackSpace ||
keyval == GDK_Tab ||
keyval == GDK_Linefeed ||
keyval == GDK_Clear ||
keyval == GDK_Return ||
keyval == GDK_Pause ||
keyval == GDK_Scroll_Lock ||
keyval == GDK_Sys_Req ||
keyval == GDK_Escape ||
keyval == GDK_Delete ||
(GDK_Home <= keyval && keyval <= GDK_Begin) || /* IsCursorkey */
(GDK_KP_Space <= keyval && keyval <= GDK_KP_Equal) || /* IsKeypadKey */
(GDK_Select <= keyval && keyval <= GDK_Break) || /* IsMiscFunctionKey */
(GDK_F1 <= keyval && keyval <= GDK_F35)); /* IsFunctionKey */
}
static gboolean
is_context_intact_key(guint keyval)
{
return (((keyval & 0xFF00) == 0xFF00) &&
((GDK_Shift_L <= keyval && keyval <= GDK_Hyper_R) || /* IsModifierKey */
(keyval == GDK_Mode_switch) ||
(keyval == GDK_Num_Lock))) ||
(((keyval & 0xFE00) == 0xFE00) &&
(GDK_ISO_Lock <= keyval && keyval <= GDK_ISO_Last_Group_Lock));
}
static gboolean
thai_is_accept (gunichar new_char, gunichar prev_char, gint isc_mode)
{
switch (isc_mode)
{
case ISC_PASSTHROUGH:
return TRUE;
case ISC_BASICCHECK:
return TAC_compose_input (prev_char, new_char) != 'R';
case ISC_STRICT:
{
int op = TAC_compose_input (prev_char, new_char);
return op != 'R' && op != 'S';
}
default:
return FALSE;
}
}
#define thai_is_composible(n,p) (TAC_compose_input ((p), (n)) == 'C')
#ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
static void
forget_previous_chars (GtkIMContextThai *context_thai)
{
memset (context_thai->char_buff, 0, sizeof (context_thai->char_buff));
}
static void
remember_previous_char (GtkIMContextThai *context_thai, gunichar new_char)
{
memmove (context_thai->char_buff + 1, context_thai->char_buff,
(GTK_IM_CONTEXT_THAI_BUFF_SIZE - 1) * sizeof (context_thai->char_buff[0]));
context_thai->char_buff[0] = new_char;
}
#endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
static gunichar
get_previous_char (GtkIMContextThai *context_thai, gint offset)
{
gchar *surrounding;
gint cursor_index;
if (gtk_im_context_get_surrounding ((GtkIMContext *)context_thai,
&surrounding, &cursor_index))
{
gunichar prev_char;
gchar *p, *q;
prev_char = 0;
p = surrounding + cursor_index;
for (q = p; offset < 0 && q > surrounding; ++offset)
q = g_utf8_prev_char (q);
if (offset == 0)
{
prev_char = g_utf8_get_char_validated (q, p - q);
if (prev_char < 0)
prev_char = 0;
}
g_free (surrounding);
return prev_char;
}
#ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
else
{
offset = -offset - 1;
if (0 <= offset && offset < GTK_IM_CONTEXT_THAI_BUFF_SIZE)
return context_thai->char_buff[offset];
}
#endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
return 0;
}
static gboolean
gtk_im_context_thai_commit_chars (GtkIMContextThai *context_thai,
gunichar *s, gsize len)
{
gchar *utf8;
utf8 = g_ucs4_to_utf8 (s, len, NULL, NULL, NULL);
if (!utf8)
return FALSE;
g_signal_emit_by_name (context_thai, "commit", utf8);
g_free (utf8);
return TRUE;
}
static gboolean
accept_input (GtkIMContextThai *context_thai, gunichar new_char)
{
#ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
remember_previous_char (context_thai, new_char);
#endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
return gtk_im_context_thai_commit_chars (context_thai, &new_char, 1);
}
static gboolean
reorder_input (GtkIMContextThai *context_thai,
gunichar prev_char, gunichar new_char)
{
gunichar buf[2];
if (!gtk_im_context_delete_surrounding (GTK_IM_CONTEXT (context_thai), -1, 1))
return FALSE;
#ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
forget_previous_chars (context_thai);
remember_previous_char (context_thai, new_char);
remember_previous_char (context_thai, prev_char);
#endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
buf[0] = new_char;
buf[1] = prev_char;
return gtk_im_context_thai_commit_chars (context_thai, buf, 2);
}
static gboolean
replace_input (GtkIMContextThai *context_thai, gunichar new_char)
{
if (!gtk_im_context_delete_surrounding (GTK_IM_CONTEXT (context_thai), -1, 1))
return FALSE;
#ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
forget_previous_chars (context_thai);
remember_previous_char (context_thai, new_char);
#endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
return gtk_im_context_thai_commit_chars (context_thai, &new_char, 1);
}
static gboolean
gtk_im_context_thai_filter_keypress (GtkIMContext *context,
GdkEventKey *event)
{
GtkIMContextThai *context_thai = GTK_IM_CONTEXT_THAI (context);
gunichar prev_char, new_char;
gboolean is_reject;
GtkIMContextThaiISCMode isc_mode;
if (event->type != GDK_KEY_PRESS)
return FALSE;
if (event->state & (GDK_MODIFIER_MASK
& ~(GDK_SHIFT_MASK | GDK_LOCK_MASK | GDK_MOD2_MASK)) ||
is_context_lost_key (event->keyval))
{
#ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
forget_previous_chars (context_thai);
#endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
return FALSE;
}
if (event->keyval == 0 || is_context_intact_key (event->keyval))
{
return FALSE;
}
prev_char = get_previous_char (context_thai, -1);
if (!prev_char)
prev_char = ' ';
new_char = gdk_keyval_to_unicode (event->keyval);
is_reject = TRUE;
isc_mode = gtk_im_context_thai_get_isc_mode (context_thai);
if (thai_is_accept (new_char, prev_char, isc_mode))
{
accept_input (context_thai, new_char);
is_reject = FALSE;
}
else
{
gunichar context_char;
/* rejected, trying to correct */
context_char = get_previous_char (context_thai, -2);
if (context_char)
{
if (thai_is_composible (new_char, context_char))
{
if (thai_is_composible (prev_char, new_char))
is_reject = !reorder_input (context_thai, prev_char, new_char);
else if (thai_is_composible (prev_char, context_char))
is_reject = !replace_input (context_thai, new_char);
else if ((TAC_char_class (prev_char) == FV1
|| TAC_char_class (prev_char) == AM)
&& TAC_char_class (new_char) == TONE)
is_reject = !reorder_input (context_thai, prev_char, new_char);
}
else if (thai_is_accept (new_char, context_char, isc_mode))
is_reject = !replace_input (context_thai, new_char);
}
}
if (is_reject)
{
/* reject character */
gdk_beep ();
}
return TRUE;
}
|