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
/* CTK - The GIMP 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, see <http://www.gnu.org/licenses/>.
 *
 * Author:  Theppitak Karoonboonyanan <thep@linux.thai.net>
 *
 */

#include <string.h>

#include <cdk/cdkkeysyms.h>
#include "ctkimcontextthai.h"
#include "thai-charprop.h"

static void     ctk_im_context_thai_class_init          (CtkIMContextThaiClass *class);
static void     ctk_im_context_thai_init                (CtkIMContextThai      *im_context_thai);
static gboolean ctk_im_context_thai_filter_keypress     (CtkIMContext          *context,
						         CdkEventKey           *key);

#ifndef CTK_IM_CONTEXT_THAI_NO_FALLBACK
static void     forget_previous_chars (CtkIMContextThai *context_thai);
static void     remember_previous_char (CtkIMContextThai *context_thai,
                                        gunichar new_char);
#endif /* !CTK_IM_CONTEXT_THAI_NO_FALLBACK */

static GObjectClass *parent_class;

GType ctk_type_im_context_thai = 0;

void
ctk_im_context_thai_register_type (GTypeModule *type_module)
{
  const GTypeInfo im_context_thai_info =
  {
    .class_size = sizeof (CtkIMContextThaiClass),
    .base_init = (GBaseInitFunc) NULL,
    .base_finalize = (GBaseFinalizeFunc) NULL,
    .class_init = (GClassInitFunc) ctk_im_context_thai_class_init,
    .instance_size = sizeof (CtkIMContextThai),
    .n_preallocs = 0,
    .instance_init = (GInstanceInitFunc) ctk_im_context_thai_init,
  };

  ctk_type_im_context_thai = 
    g_type_module_register_type (type_module,
                                 CTK_TYPE_IM_CONTEXT,
                                 "CtkIMContextThai",
                                 &im_context_thai_info, 0);
}

static void
ctk_im_context_thai_class_init (CtkIMContextThaiClass *class)
{
  CtkIMContextClass *im_context_class = CTK_IM_CONTEXT_CLASS (class);

  parent_class = g_type_class_peek_parent (class);

  im_context_class->filter_keypress = ctk_im_context_thai_filter_keypress;
}

static void
ctk_im_context_thai_init (CtkIMContextThai *context_thai)
{
#ifndef CTK_IM_CONTEXT_THAI_NO_FALLBACK
  forget_previous_chars (context_thai);
#endif /* !CTK_IM_CONTEXT_THAI_NO_FALLBACK */
  context_thai->isc_mode = ISC_BASICCHECK;
}

CtkIMContext *
ctk_im_context_thai_new (void)
{
  CtkIMContextThai *result;

  result = CTK_IM_CONTEXT_THAI (g_object_new (CTK_TYPE_IM_CONTEXT_THAI, NULL));

  return CTK_IM_CONTEXT (result);
}

CtkIMContextThaiISCMode
ctk_im_context_thai_get_isc_mode (CtkIMContextThai *context_thai)<--- Parameter 'context_thai' can be declared as pointer to const
{
  return context_thai->isc_mode;
}

CtkIMContextThaiISCMode
ctk_im_context_thai_set_isc_mode (CtkIMContextThai *context_thai,
                                  CtkIMContextThaiISCMode mode)
{
  CtkIMContextThaiISCMode 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 == CDK_KEY_BackSpace ||
          keyval == CDK_KEY_Tab ||
          keyval == CDK_KEY_Linefeed ||
          keyval == CDK_KEY_Clear ||
          keyval == CDK_KEY_Return ||
          keyval == CDK_KEY_Pause ||
          keyval == CDK_KEY_Scroll_Lock ||
          keyval == CDK_KEY_Sys_Req ||
          keyval == CDK_KEY_Escape ||
          keyval == CDK_KEY_Delete ||
          (CDK_KEY_Home <= keyval && keyval <= CDK_KEY_Begin) || /* IsCursorkey */
          (CDK_KEY_KP_Space <= keyval && keyval <= CDK_KEY_KP_Delete) || /* IsKeypadKey, non-chars only */
          (CDK_KEY_Select <= keyval && keyval <= CDK_KEY_Break) || /* IsMiscFunctionKey */
          (CDK_KEY_F1 <= keyval && keyval <= CDK_KEY_F35)); /* IsFunctionKey */
}

static gboolean
is_context_intact_key(guint keyval)
{
  return (((keyval & 0xFF00) == 0xFF00) &&
           ((CDK_KEY_Shift_L <= keyval && keyval <= CDK_KEY_Hyper_R) || /* IsModifierKey */
            (keyval == CDK_KEY_Mode_switch) ||
            (keyval == CDK_KEY_Num_Lock))) ||
         (((keyval & 0xFE00) == 0xFE00) &&
          (CDK_KEY_ISO_Lock <= keyval && keyval <= CDK_KEY_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 CTK_IM_CONTEXT_THAI_NO_FALLBACK
static void
forget_previous_chars (CtkIMContextThai *context_thai)
{
  memset (context_thai->char_buff, 0, sizeof (context_thai->char_buff));
}

static void
remember_previous_char (CtkIMContextThai *context_thai, gunichar new_char)
{
  memmove (context_thai->char_buff + 1, context_thai->char_buff,
           (CTK_IM_CONTEXT_THAI_BUFF_SIZE - 1) * sizeof (context_thai->char_buff[0]));
  context_thai->char_buff[0] = new_char;
}
#endif /* !CTK_IM_CONTEXT_THAI_NO_FALLBACK */

static gunichar
get_previous_char (CtkIMContextThai *context_thai, gint offset)
{
  gchar *surrounding;
  gint  cursor_index;

  if (ctk_im_context_get_surrounding ((CtkIMContext *)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 == (gunichar)-1 || prev_char == (gunichar)-2)
            prev_char = 0;
        }
      g_free (surrounding);
      return prev_char;
    }
#ifndef CTK_IM_CONTEXT_THAI_NO_FALLBACK
  else
    {
      offset = -offset - 1;
      if (0 <= offset && offset < CTK_IM_CONTEXT_THAI_BUFF_SIZE)
        return context_thai->char_buff[offset];
    }
#endif /* !CTK_IM_CONTEXT_THAI_NO_FALLBACK */

    return 0;
}

static gboolean
ctk_im_context_thai_commit_chars (CtkIMContextThai *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 (CtkIMContextThai *context_thai, gunichar new_char)
{
#ifndef CTK_IM_CONTEXT_THAI_NO_FALLBACK
  remember_previous_char (context_thai, new_char);
#endif /* !CTK_IM_CONTEXT_THAI_NO_FALLBACK */

  return ctk_im_context_thai_commit_chars (context_thai, &new_char, 1);
}

static gboolean
reorder_input (CtkIMContextThai *context_thai,
               gunichar prev_char, gunichar new_char)
{
  gunichar buf[2];

  if (!ctk_im_context_delete_surrounding (CTK_IM_CONTEXT (context_thai), -1, 1))
    return FALSE;

#ifndef CTK_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 /* !CTK_IM_CONTEXT_THAI_NO_FALLBACK */

  buf[0] = new_char;
  buf[1] = prev_char;
  return ctk_im_context_thai_commit_chars (context_thai, buf, 2);
}

static gboolean
replace_input (CtkIMContextThai *context_thai, gunichar new_char)
{
  if (!ctk_im_context_delete_surrounding (CTK_IM_CONTEXT (context_thai), -1, 1))
    return FALSE;

#ifndef CTK_IM_CONTEXT_THAI_NO_FALLBACK
  forget_previous_chars (context_thai);
  remember_previous_char (context_thai, new_char);
#endif /* !CTK_IM_CONTEXT_THAI_NO_FALLBACK */

  return ctk_im_context_thai_commit_chars (context_thai, &new_char, 1);
}

static gboolean
ctk_im_context_thai_filter_keypress (CtkIMContext *context,
                                     CdkEventKey  *event)
{
  CtkIMContextThai *context_thai = CTK_IM_CONTEXT_THAI (context);
  gunichar prev_char, new_char;
  gboolean is_reject;
  CtkIMContextThaiISCMode isc_mode;

  if (event->type != CDK_KEY_PRESS)
    return FALSE;

  if (event->state & (CDK_MODIFIER_MASK
                      & ~(CDK_SHIFT_MASK | CDK_LOCK_MASK | CDK_MOD2_MASK)) ||
      is_context_lost_key (event->keyval))
    {
#ifndef CTK_IM_CONTEXT_THAI_NO_FALLBACK
      forget_previous_chars (context_thai);
#endif /* !CTK_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 = cdk_keyval_to_unicode (event->keyval);
  is_reject = TRUE;
  isc_mode = ctk_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 */
      CdkDisplay *display = cdk_display_get_default ();
      cdk_display_beep (display);
    }
  return TRUE;
}