]> Pileus Git - ~andy/gtk/blob - modules/input/gtkimcontextthai.c
Remove the broken Thai input method and add a functional Thai and Lao
[~andy/gtk] / modules / input / gtkimcontextthai.c
1 /* GTK - The GIMP Toolkit
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 02111-1307, USA.
17  *
18  * Author:  Theppitak Karoonboonyanan <thep@linux.thai.net>
19  *
20  */
21
22 #include <string.h>
23
24 #include <gdk/gdkkeysyms.h>
25 #include <gdk/gdkkeys.h>
26 #include "gtkimcontextthai.h"
27 #include "thai-charprop.h"
28
29 static void     gtk_im_context_thai_class_init          (GtkIMContextThaiClass *class);
30 static void     gtk_im_context_thai_init                (GtkIMContextThai      *im_context_thai);
31 static gboolean gtk_im_context_thai_filter_keypress     (GtkIMContext          *context,
32                                                          GdkEventKey           *key);
33
34 #ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
35 static void     forget_previous_chars (GtkIMContextThai *context_thai);
36 static void     remember_previous_char (GtkIMContextThai *context_thai,
37                                         gunichar new_char);
38 #endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
39
40 static GObjectClass *parent_class;
41
42 GType gtk_type_im_context_thai = 0;
43
44 void
45 gtk_im_context_thai_register_type (GTypeModule *type_module)
46 {
47   static const GTypeInfo im_context_thai_info =
48   {
49     sizeof (GtkIMContextThaiClass),
50     (GBaseInitFunc) NULL,
51     (GBaseFinalizeFunc) NULL,
52     (GClassInitFunc) gtk_im_context_thai_class_init,
53     NULL,           /* class_finalize */    
54     NULL,           /* class_data */
55     sizeof (GtkIMContextThai),
56     0,
57     (GInstanceInitFunc) gtk_im_context_thai_init,
58   };
59
60   gtk_type_im_context_thai = 
61     g_type_module_register_type (type_module,
62                                  GTK_TYPE_IM_CONTEXT,
63                                  "GtkIMContextThai",
64                                  &im_context_thai_info, 0);
65 }
66
67 static void
68 gtk_im_context_thai_class_init (GtkIMContextThaiClass *class)
69 {
70   GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
71
72   parent_class = g_type_class_peek_parent (class);
73
74   im_context_class->filter_keypress = gtk_im_context_thai_filter_keypress;
75 }
76
77 static void
78 gtk_im_context_thai_init (GtkIMContextThai *context_thai)
79 {
80 #ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
81   forget_previous_chars (context_thai);
82 #endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
83   context_thai->isc_mode = ISC_BASICCHECK;
84 }
85
86 GtkIMContext *
87 gtk_im_context_thai_new (void)
88 {
89   GtkIMContextThai *result;
90
91   result = GTK_IM_CONTEXT_THAI (g_object_new (GTK_TYPE_IM_CONTEXT_THAI, NULL));
92
93   return GTK_IM_CONTEXT (result);
94 }
95
96 GtkIMContextThaiISCMode
97 gtk_im_context_thai_get_isc_mode (GtkIMContextThai *context_thai)
98 {
99   return context_thai->isc_mode;
100 }
101
102 GtkIMContextThaiISCMode
103 gtk_im_context_thai_set_isc_mode (GtkIMContextThai *context_thai,
104                                   GtkIMContextThaiISCMode mode)
105 {
106   GtkIMContextThaiISCMode prev_mode = context_thai->isc_mode;
107   context_thai->isc_mode = mode;
108   return prev_mode;
109 }
110
111 static gboolean
112 is_context_lost_key(guint keyval)
113 {
114   return ((keyval & 0xFF00) == 0xFF00) &&
115          (keyval == GDK_BackSpace ||
116           keyval == GDK_Tab ||
117           keyval == GDK_Linefeed ||
118           keyval == GDK_Clear ||
119           keyval == GDK_Return ||
120           keyval == GDK_Pause ||
121           keyval == GDK_Scroll_Lock ||
122           keyval == GDK_Sys_Req ||
123           keyval == GDK_Escape ||
124           keyval == GDK_Delete ||
125           (GDK_Home <= keyval && keyval <= GDK_Begin) || /* IsCursorkey */
126           (GDK_KP_Space <= keyval && keyval <= GDK_KP_Equal) || /* IsKeypadKey */
127           (GDK_Select <= keyval && keyval <= GDK_Break) || /* IsMiscFunctionKey */
128           (GDK_F1 <= keyval && keyval <= GDK_F35)); /* IsFunctionKey */
129 }
130
131 static gboolean
132 is_context_intact_key(guint keyval)
133 {
134   return (((keyval & 0xFF00) == 0xFF00) &&
135            ((GDK_Shift_L <= keyval && keyval <= GDK_Hyper_R) || /* IsModifierKey */
136             (keyval == GDK_Mode_switch) ||
137             (keyval == GDK_Num_Lock))) ||
138          (((keyval & 0xFE00) == 0xFE00) &&
139           (GDK_ISO_Lock <= keyval && keyval <= GDK_ISO_Last_Group_Lock));
140 }
141
142 static gboolean
143 thai_is_accept (gunichar new_char, gunichar prev_char, gint isc_mode)
144 {
145   switch (isc_mode)
146     {
147     case ISC_PASSTHROUGH:
148       return TRUE;
149
150     case ISC_BASICCHECK:
151       return TAC_compose_input (prev_char, new_char) != 'R';
152
153     case ISC_STRICT:
154       {
155         int op = TAC_compose_input (prev_char, new_char);
156         return op != 'R' && op != 'S';
157       }
158
159     default:
160       return FALSE;
161     }
162 }
163
164 #define thai_is_composible(n,p)  (TAC_compose_input ((p), (n)) == 'C')
165
166 #ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
167 static void
168 forget_previous_chars (GtkIMContextThai *context_thai)
169 {
170   memset (context_thai->char_buff, 0, sizeof (context_thai->char_buff));
171 }
172
173 static void
174 remember_previous_char (GtkIMContextThai *context_thai, gunichar new_char)
175 {
176   memmove (context_thai->char_buff + 1, context_thai->char_buff,
177            (GTK_IM_CONTEXT_THAI_BUFF_SIZE - 1) * sizeof (context_thai->char_buff[0]));
178   context_thai->char_buff[0] = new_char;
179 }
180 #endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
181
182 static gunichar
183 get_previous_char (GtkIMContextThai *context_thai, gint offset)
184 {
185   gchar *surrounding;
186   gint  cursor_index;
187
188   if (gtk_im_context_get_surrounding ((GtkIMContext *)context_thai,
189                                       &surrounding, &cursor_index))
190     {
191       gunichar prev_char;
192       gchar *p, *q;
193
194       prev_char = 0;
195       p = surrounding + cursor_index;
196       for (q = p; offset < 0 && q > surrounding; ++offset)
197         q = g_utf8_prev_char (q);
198       if (offset == 0)
199         {
200           prev_char = g_utf8_get_char_validated (q, p - q);
201           if (prev_char < 0)
202             prev_char = 0;
203         }
204       g_free (surrounding);
205       return prev_char;
206     }
207 #ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
208   else
209     {
210       offset = -offset - 1;
211       if (0 <= offset && offset < GTK_IM_CONTEXT_THAI_BUFF_SIZE)
212         return context_thai->char_buff[offset];
213     }
214 #endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
215
216     return 0;
217 }
218
219 static gboolean
220 gtk_im_context_thai_commit_chars (GtkIMContextThai *context_thai,
221                                   gunichar *s, gsize len)
222 {
223   gchar *utf8;
224
225   utf8 = g_ucs4_to_utf8 (s, len, NULL, NULL, NULL);
226   if (!utf8)
227     return FALSE;
228
229   g_signal_emit_by_name (context_thai, "commit", utf8);
230
231   g_free (utf8);
232   return TRUE;
233 }
234
235 static gboolean
236 accept_input (GtkIMContextThai *context_thai, gunichar new_char)
237 {
238 #ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
239   remember_previous_char (context_thai, new_char);
240 #endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
241
242   return gtk_im_context_thai_commit_chars (context_thai, &new_char, 1);
243 }
244
245 static gboolean
246 reorder_input (GtkIMContextThai *context_thai,
247                gunichar prev_char, gunichar new_char)
248 {
249   gunichar buf[2];
250
251   if (!gtk_im_context_delete_surrounding (GTK_IM_CONTEXT (context_thai), -1, 1))
252     return FALSE;
253
254 #ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
255   forget_previous_chars (context_thai);
256   remember_previous_char (context_thai, new_char);
257   remember_previous_char (context_thai, prev_char);
258 #endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
259
260   buf[0] = new_char;
261   buf[1] = prev_char;
262   return gtk_im_context_thai_commit_chars (context_thai, buf, 2);
263 }
264
265 static gboolean
266 replace_input (GtkIMContextThai *context_thai, gunichar new_char)
267 {
268   if (!gtk_im_context_delete_surrounding (GTK_IM_CONTEXT (context_thai), -1, 1))
269     return FALSE;
270
271 #ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
272   forget_previous_chars (context_thai);
273   remember_previous_char (context_thai, new_char);
274 #endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
275
276   return gtk_im_context_thai_commit_chars (context_thai, &new_char, 1);
277 }
278
279 static gboolean
280 gtk_im_context_thai_filter_keypress (GtkIMContext *context,
281                                      GdkEventKey  *event)
282 {
283   GtkIMContextThai *context_thai = GTK_IM_CONTEXT_THAI (context);
284   gunichar prev_char, new_char;
285   gboolean is_reject;
286   GtkIMContextThaiISCMode isc_mode;
287
288   if (event->type != GDK_KEY_PRESS)
289     return FALSE;
290
291   if (event->state & (GDK_MODIFIER_MASK & ~GDK_SHIFT_MASK) ||
292       is_context_lost_key (event->keyval))
293     {
294 #ifndef GTK_IM_CONTEXT_THAI_NO_FALLBACK
295       forget_previous_chars (context_thai);
296 #endif /* !GTK_IM_CONTEXT_THAI_NO_FALLBACK */
297       return FALSE;
298     }
299   if (event->keyval == 0 || is_context_intact_key (event->keyval))
300     {
301       return FALSE;
302     }
303
304   prev_char = get_previous_char (context_thai, -1);
305   if (!prev_char)
306     prev_char = ' ';
307   new_char = gdk_keyval_to_unicode (event->keyval);
308   is_reject = TRUE;
309   isc_mode = gtk_im_context_thai_get_isc_mode (context_thai);
310   if (thai_is_accept (new_char, prev_char, isc_mode))
311     {
312       accept_input (context_thai, new_char);
313       is_reject = FALSE;
314     }
315   else
316     {
317       gunichar context_char;
318
319       /* rejected, trying to correct */
320       context_char = get_previous_char (context_thai, -2);
321       if (context_char)
322         {
323           if (thai_is_composible (new_char, context_char))
324             {
325               if (thai_is_composible (prev_char, new_char))
326                 is_reject = !reorder_input (context_thai, prev_char, new_char);
327               else if (thai_is_composible (prev_char, context_char))
328                 is_reject = !replace_input (context_thai, new_char);
329               else if ((TAC_char_class (prev_char) == FV1
330                         || TAC_char_class (prev_char) == AM)
331                        && TAC_char_class (new_char) == TONE)
332                 is_reject = !reorder_input (context_thai, prev_char, new_char);
333             }
334           else if (thai_is_accept (new_char, context_char, isc_mode))
335             is_reject = !replace_input (context_thai, new_char);
336         }
337     }
338   if (is_reject)
339     {
340       /* reject character */
341       gdk_beep ();
342     }
343   return TRUE;
344 }
345