]> Pileus Git - ~andy/gtk/blob - gtk/gtkimcontextsimple.c
Cosmetic changes to comments
[~andy/gtk] / gtk / gtkimcontextsimple.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "gtkprivate.h"
26 #include "gtkaccelgroup.h"
27 #include "gtkimcontextsimple.h"
28 #include "gtksettings.h"
29 #include "gtkwidget.h"
30 #include "gtkdebug.h"
31 #include "gtkintl.h"
32
33
34 /**
35  * SECTION:gtkimcontextsimple
36  * @Short_description: An input method context supporting table-based input methods
37  * @Title: GtkIMContextSimple
38  */
39
40
41 typedef struct _GtkComposeTable GtkComposeTable;
42 typedef struct _GtkComposeTableCompact GtkComposeTableCompact;
43
44 struct _GtkIMContextSimplePrivate
45 {
46   GSList        *tables;
47
48   guint          compose_buffer[GTK_MAX_COMPOSE_LEN + 1];
49   gunichar       tentative_match;
50   gint           tentative_match_len;
51
52   guint          in_hex_sequence : 1;
53   guint          modifiers_dropped : 1;
54 };
55
56 struct _GtkComposeTable 
57 {
58   const guint16 *data;
59   gint max_seq_len;
60   gint n_seqs;
61 };
62
63 struct _GtkComposeTableCompact
64 {
65   const guint16 *data;
66   gint max_seq_len;
67   gint n_index_size;
68   gint n_index_stride;
69 };
70
71 /* This file contains the table of the compose sequences,
72  * static const guint16 gtk_compose_seqs_compact[] = {}
73  * It is generated from the compose-parse.py script.
74  */
75 #include "gtkimcontextsimpleseqs.h"
76
77 /* From the values below, the value 24 means the number of different first keysyms
78  * that exist in the Compose file (from Xorg). When running compose-parse.py without
79  * parameters, you get the count that you can put here. Needed when updating the
80  * gtkimcontextsimpleseqs.h header file (contains the compose sequences).
81  */
82 static const GtkComposeTableCompact gtk_compose_table_compact = {
83   gtk_compose_seqs_compact,
84   5,
85   24,
86   6
87 };
88
89 static const guint16 gtk_compose_ignore[] = {
90   GDK_KEY_Shift_L,
91   GDK_KEY_Shift_R,
92   GDK_KEY_Control_L,
93   GDK_KEY_Control_R,
94   GDK_KEY_Caps_Lock,
95   GDK_KEY_Shift_Lock,
96   GDK_KEY_Meta_L,
97   GDK_KEY_Meta_R,
98   GDK_KEY_Alt_L,
99   GDK_KEY_Alt_R,
100   GDK_KEY_Super_L,
101   GDK_KEY_Super_R,
102   GDK_KEY_Hyper_L,
103   GDK_KEY_Hyper_R,
104   GDK_KEY_Mode_switch,
105   GDK_KEY_ISO_Level3_Shift
106 };
107
108 static void     gtk_im_context_simple_finalize           (GObject                  *obj);
109 static gboolean gtk_im_context_simple_filter_keypress    (GtkIMContext             *context,
110                                                           GdkEventKey              *key);
111 static void     gtk_im_context_simple_reset              (GtkIMContext             *context);
112 static void     gtk_im_context_simple_get_preedit_string (GtkIMContext             *context,
113                                                           gchar                   **str,
114                                                           PangoAttrList           **attrs,
115                                                           gint                     *cursor_pos);
116
117 G_DEFINE_TYPE (GtkIMContextSimple, gtk_im_context_simple, GTK_TYPE_IM_CONTEXT)
118
119 static void
120 gtk_im_context_simple_class_init (GtkIMContextSimpleClass *class)
121 {
122   GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
123   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
124
125   im_context_class->filter_keypress = gtk_im_context_simple_filter_keypress;
126   im_context_class->reset = gtk_im_context_simple_reset;
127   im_context_class->get_preedit_string = gtk_im_context_simple_get_preedit_string;
128   gobject_class->finalize = gtk_im_context_simple_finalize;
129
130   g_type_class_add_private (class, sizeof (GtkIMContextSimplePrivate));
131 }
132
133 static void
134 gtk_im_context_simple_init (GtkIMContextSimple *im_context_simple)
135 {
136   im_context_simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (im_context_simple,
137                                                          GTK_TYPE_IM_CONTEXT_SIMPLE,
138                                                          GtkIMContextSimplePrivate);
139 }
140
141 static void
142 gtk_im_context_simple_finalize (GObject *obj)
143 {
144   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (obj);
145   GtkIMContextSimplePrivate *priv = context_simple->priv;
146
147   if (priv->tables)
148     {
149       g_slist_foreach (priv->tables, (GFunc)g_free, NULL);
150       g_slist_free (priv->tables);
151
152       priv->tables = NULL;
153     }
154
155   G_OBJECT_CLASS (gtk_im_context_simple_parent_class)->finalize (obj);
156 }
157
158 /**
159  * gtk_im_context_simple_new:
160  * 
161  * Creates a new #GtkIMContextSimple.
162  *
163  * Returns: a new #GtkIMContextSimple.
164  **/
165 GtkIMContext *
166 gtk_im_context_simple_new (void)
167 {
168   return g_object_new (GTK_TYPE_IM_CONTEXT_SIMPLE, NULL);
169 }
170
171 static void
172 gtk_im_context_simple_commit_char (GtkIMContext *context,
173                                    gunichar ch)
174 {
175   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
176   GtkIMContextSimplePrivate *priv = context_simple->priv;
177   gchar buf[10];
178   gint len;
179
180   g_return_if_fail (g_unichar_validate (ch));
181
182   len = g_unichar_to_utf8 (ch, buf);
183   buf[len] = '\0';
184
185   if (priv->tentative_match || priv->in_hex_sequence)
186     {
187       priv->in_hex_sequence = FALSE;
188       priv->tentative_match = 0;
189       priv->tentative_match_len = 0;
190       g_signal_emit_by_name (context_simple, "preedit-changed");
191       g_signal_emit_by_name (context_simple, "preedit-end");
192     }
193
194   g_signal_emit_by_name (context, "commit", &buf);
195 }
196
197 static int
198 compare_seq_index (const void *key, const void *value)
199 {
200   const guint *keysyms = key;
201   const guint16 *seq = value;
202
203   if (keysyms[0] < seq[0])
204     return -1;
205   else if (keysyms[0] > seq[0])
206     return 1;
207
208   return 0;
209 }
210
211 static int
212 compare_seq (const void *key, const void *value)
213 {
214   int i = 0;
215   const guint *keysyms = key;
216   const guint16 *seq = value;
217
218   while (keysyms[i])
219     {
220       if (keysyms[i] < seq[i])
221         return -1;
222       else if (keysyms[i] > seq[i])
223         return 1;
224
225       i++;
226     }
227
228   return 0;
229 }
230
231 static gboolean
232 check_table (GtkIMContextSimple    *context_simple,
233              const GtkComposeTable *table,
234              gint                   n_compose)
235 {
236   GtkIMContextSimplePrivate *priv = context_simple->priv;
237   gint row_stride = table->max_seq_len + 2; 
238   guint16 *seq; 
239   
240   /* Will never match, if the sequence in the compose buffer is longer
241    * than the sequences in the table.  Further, compare_seq (key, val)
242    * will overrun val if key is longer than val. */
243   if (n_compose > table->max_seq_len)
244     return FALSE;
245   
246   seq = bsearch (priv->compose_buffer,
247                  table->data, table->n_seqs,
248                  sizeof (guint16) *  row_stride, 
249                  compare_seq);
250
251   if (seq)
252     {
253       guint16 *prev_seq;
254
255       /* Back up to the first sequence that matches to make sure
256        * we find the exact match if there is one.
257        */
258       while (seq > table->data)
259         {
260           prev_seq = seq - row_stride;
261           if (compare_seq (priv->compose_buffer, prev_seq) != 0)
262             break;
263           seq = prev_seq;
264         }
265       
266       if (n_compose == table->max_seq_len ||
267           seq[n_compose] == 0) /* complete sequence */
268         {
269           guint16 *next_seq;
270           gunichar value = 
271             0x10000 * seq[table->max_seq_len] + seq[table->max_seq_len + 1];
272
273           
274           /* We found a tentative match. See if there are any longer
275            * sequences containing this subsequence
276            */
277           next_seq = seq + row_stride;
278           if (next_seq < table->data + row_stride * table->n_seqs)
279             {
280               if (compare_seq (priv->compose_buffer, next_seq) == 0)
281                 {
282                   priv->tentative_match = value;
283                   priv->tentative_match_len = n_compose;
284                 
285                   g_signal_emit_by_name (context_simple, "preedit-changed");
286
287                   return TRUE;
288                 }
289             }
290
291           gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
292           priv->compose_buffer[0] = 0;
293         }
294       
295       return TRUE;
296     }
297
298   return FALSE;
299 }
300
301 /* Checks if a keysym is a dead key. Dead key keysym values are defined in
302  * ../gdk/gdkkeysyms.h and the first is GDK_KEY_dead_grave. As X.Org is updated,
303  * more dead keys are added and we need to update the upper limit.
304  * Currently, the upper limit is GDK_KEY_dead_dasia+1. The +1 has to do with
305  * a temporary issue in the X.Org header files.
306  * In future versions it will be just the keysym (no +1).
307  */
308 #define IS_DEAD_KEY(k) \
309     ((k) >= GDK_KEY_dead_grave && (k) <= (GDK_KEY_dead_dasia+1))
310
311 #ifdef GDK_WINDOWING_WIN32
312
313 /* On Windows, user expectation is that typing a dead accent followed
314  * by space will input the corresponding spacing character. The X
315  * compose tables are different for dead acute and diaeresis, which
316  * when followed by space produce a plain ASCII apostrophe and double
317  * quote respectively. So special-case those.
318  */
319
320 static gboolean
321 check_win32_special_cases (GtkIMContextSimple    *context_simple,
322                            gint                   n_compose)
323 {
324   GtkIMContextSimplePrivate *priv = context_simple->priv;
325   if (n_compose == 2 &&
326       priv->compose_buffer[1] == GDK_KEY_space)
327     {
328       gunichar value = 0;
329
330       switch (priv->compose_buffer[0])
331         {
332         case GDK_KEY_dead_acute:
333           value = 0x00B4; break;
334         case GDK_KEY_dead_diaeresis:
335           value = 0x00A8; break;
336         }
337       if (value > 0)
338         {
339           gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
340           priv->compose_buffer[0] = 0;
341
342           GTK_NOTE (MISC, g_print ("win32: U+%04X\n", value));
343           return TRUE;
344         }
345     }
346   return FALSE;
347 }
348
349 static void
350 check_win32_special_case_after_compact_match (GtkIMContextSimple    *context_simple,
351                                               gint                   n_compose,
352                                               guint                  value)
353 {
354   GtkIMContextSimplePrivate *priv = context_simple->priv;
355
356   /* On Windows user expectation is that typing two dead accents will input
357    * two corresponding spacing accents.
358    */
359   if (n_compose == 2 &&
360       priv->compose_buffer[0] == priv->compose_buffer[1] &&
361       IS_DEAD_KEY (priv->compose_buffer[0]))
362     {
363       gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
364       GTK_NOTE (MISC, g_print ("win32: U+%04X ", value));
365     }
366 }
367
368 #endif
369
370 static gboolean
371 check_compact_table (GtkIMContextSimple    *context_simple,
372              const GtkComposeTableCompact *table,
373              gint                   n_compose)
374 {
375   GtkIMContextSimplePrivate *priv = context_simple->priv;
376   gint row_stride;
377   guint16 *seq_index;
378   guint16 *seq; 
379   gint i;
380
381   /* Will never match, if the sequence in the compose buffer is longer
382    * than the sequences in the table.  Further, compare_seq (key, val)
383    * will overrun val if key is longer than val. */
384   if (n_compose > table->max_seq_len)
385     return FALSE;
386
387   seq_index = bsearch (priv->compose_buffer,
388                  table->data, table->n_index_size,
389                  sizeof (guint16) *  table->n_index_stride, 
390                  compare_seq_index);
391
392   if (!seq_index)
393     {
394       GTK_NOTE (MISC, g_print ("compact: no\n"));
395       return FALSE;
396     }
397
398   if (seq_index && n_compose == 1)
399     {
400       GTK_NOTE (MISC, g_print ("compact: yes\n"));
401       return TRUE;
402     }
403
404   GTK_NOTE (MISC, g_print ("compact: %d ", *seq_index));
405   seq = NULL;
406
407   for (i = n_compose-1; i < table->max_seq_len; i++)
408     {
409       row_stride = i + 1;
410
411       if (seq_index[i+1] - seq_index[i] > 0)
412         {
413           seq = bsearch (priv->compose_buffer + 1,
414                  table->data + seq_index[i], (seq_index[i+1] - seq_index[i]) / row_stride,
415                  sizeof (guint16) *  row_stride, 
416                  compare_seq);
417
418           if (seq)
419             {
420               if (i == n_compose - 1)
421                 break;
422               else
423                 {
424                   g_signal_emit_by_name (context_simple, "preedit-changed");
425
426                   GTK_NOTE (MISC, g_print ("yes\n"));
427                   return TRUE;
428                 }
429              }
430         }
431     }
432
433   if (!seq)
434     {
435       GTK_NOTE (MISC, g_print ("no\n"));
436       return FALSE;
437     }
438   else
439     {
440       gunichar value;
441
442       value = seq[row_stride - 1];
443
444       gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
445 #ifdef G_OS_WIN32
446       check_win32_special_case_after_compact_match (context_simple, n_compose, value);
447 #endif
448       priv->compose_buffer[0] = 0;
449
450       GTK_NOTE (MISC, g_print ("U+%04X\n", value));
451       return TRUE;
452     }
453
454   GTK_NOTE (MISC, g_print ("no\n"));
455   return FALSE;
456 }
457
458 /* This function receives a sequence of Unicode characters and tries to
459  * normalize it (NFC). We check for the case the the resulting string
460  * has length 1 (single character).
461  * NFC normalisation normally rearranges diacritic marks, unless these
462  * belong to the same Canonical Combining Class.
463  * If they belong to the same canonical combining class, we produce all
464  * permutations of the diacritic marks, then attempt to normalize.
465  */
466 static gboolean
467 check_normalize_nfc (gunichar* combination_buffer, gint n_compose)
468 {
469   gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
470   gchar *combination_utf8_temp = NULL;
471   gchar *nfc_temp = NULL;
472   gint n_combinations;
473   gunichar temp_swap;
474   gint i;
475
476   n_combinations = 1;
477
478   for (i = 1; i < n_compose; i++ )
479      n_combinations *= i;
480
481   /* Xorg reuses dead_tilde for the perispomeni diacritic mark.
482    * We check if base character belongs to Greek Unicode block,
483    * and if so, we replace tilde with perispomeni.
484    */
485   if (combination_buffer[0] >= 0x390 && combination_buffer[0] <= 0x3FF)
486     {
487       for (i = 1; i < n_compose; i++ )
488         if (combination_buffer[i] == 0x303)
489           combination_buffer[i] = 0x342;
490     }
491
492   memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
493
494   for (i = 0; i < n_combinations; i++ )
495     {
496       g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
497       combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
498       nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);
499
500       if (g_utf8_strlen (nfc_temp, -1) == 1)
501         {
502           memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
503
504           g_free (combination_utf8_temp);
505           g_free (nfc_temp);
506
507           return TRUE;
508         }
509
510       g_free (combination_utf8_temp);
511       g_free (nfc_temp);
512
513       if (n_compose > 2)
514         {
515           temp_swap = combination_buffer_temp[i % (n_compose - 1) + 1];
516           combination_buffer_temp[i % (n_compose - 1) + 1] = combination_buffer_temp[(i+1) % (n_compose - 1) + 1];
517           combination_buffer_temp[(i+1) % (n_compose - 1) + 1] = temp_swap;
518         }
519       else
520         break;
521     }
522
523   return FALSE;
524 }
525
526 static gboolean
527 check_algorithmically (GtkIMContextSimple *context_simple,
528                        gint                n_compose)
529
530 {
531   GtkIMContextSimplePrivate *priv = context_simple->priv;
532   gint i;
533   gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
534   gchar *combination_utf8, *nfc;
535
536   if (n_compose >= GTK_MAX_COMPOSE_LEN)
537     return FALSE;
538
539   for (i = 0; i < n_compose && IS_DEAD_KEY (priv->compose_buffer[i]); i++)
540     ;
541   if (i == n_compose)
542     return TRUE;
543
544   if (i > 0 && i == n_compose - 1)
545     {
546       combination_buffer[0] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
547       combination_buffer[n_compose] = 0;
548       i--;
549       while (i >= 0)
550         {
551           switch (priv->compose_buffer[i])
552             {
553 #define CASE(keysym, unicode) \
554             case GDK_KEY_dead_##keysym: combination_buffer[i+1] = unicode; break
555
556             CASE (grave, 0x0300);
557             CASE (acute, 0x0301);
558             CASE (circumflex, 0x0302);
559             CASE (tilde, 0x0303);       /* Also used with perispomeni, 0x342. */
560             CASE (macron, 0x0304);
561             CASE (breve, 0x0306);
562             CASE (abovedot, 0x0307);
563             CASE (diaeresis, 0x0308);
564             CASE (hook, 0x0309);
565             CASE (abovering, 0x030A);
566             CASE (doubleacute, 0x030B);
567             CASE (caron, 0x030C);
568             CASE (abovecomma, 0x0313);         /* Equivalent to psili */
569             CASE (abovereversedcomma, 0x0314); /* Equivalent to dasia */
570             CASE (horn, 0x031B);        /* Legacy use for psili, 0x313 (or 0x343). */
571             CASE (belowdot, 0x0323);
572             CASE (cedilla, 0x0327);
573             CASE (ogonek, 0x0328);      /* Legacy use for dasia, 0x314.*/
574             CASE (iota, 0x0345);
575             CASE (voiced_sound, 0x3099);        /* Per Markus Kuhn keysyms.txt file. */
576             CASE (semivoiced_sound, 0x309A);    /* Per Markus Kuhn keysyms.txt file. */
577
578             /* The following cases are to be removed once xkeyboard-config,
579              * xorg are fully updated.
580              */
581             /* Workaround for typo in 1.4.x xserver-xorg */
582             case 0xfe66: combination_buffer[i+1] = 0x314; break;
583             /* CASE (dasia, 0x314); */
584             /* CASE (perispomeni, 0x342); */
585             /* CASE (psili, 0x343); */
586 #undef CASE
587             default:
588               combination_buffer[i+1] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
589             }
590           i--;
591         }
592       
593       /* If the buffer normalizes to a single character, then modify the order
594        * of combination_buffer accordingly, if necessary, and return TRUE.
595        */
596       if (check_normalize_nfc (combination_buffer, n_compose))
597         {
598           gunichar value;
599           combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
600           nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
601
602           value = g_utf8_get_char (nfc);
603           gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
604           priv->compose_buffer[0] = 0;
605
606           g_free (combination_utf8);
607           g_free (nfc);
608
609           return TRUE;
610         }
611     }
612
613   return FALSE;
614 }
615
616 /* In addition to the table-driven sequences, we allow Unicode hex
617  * codes to be entered. The method chosen here is similar to the
618  * one recommended in ISO 14755, but not exactly the same, since we
619  * don't want to steal 16 valuable key combinations.
620  *
621  * A hex Unicode sequence must be started with Ctrl-Shift-U, followed
622  * by a sequence of hex digits entered with Ctrl-Shift still held.
623  * Releasing one of the modifiers or pressing space while the modifiers
624  * are still held commits the character. It is possible to erase
625  * digits using backspace.
626  *
627  * As an extension to the above, we also allow to start the sequence
628  * with Ctrl-Shift-U, then release the modifiers before typing any
629  * digits, and enter the digits without modifiers.
630  */
631
632 static gboolean
633 check_hex (GtkIMContextSimple *context_simple,
634            gint                n_compose)
635 {
636   GtkIMContextSimplePrivate *priv = context_simple->priv;
637   /* See if this is a hex sequence, return TRUE if so */
638   gint i;
639   GString *str;
640   gulong n;
641   gchar *nptr = NULL;
642   gchar buf[7];
643
644   priv->tentative_match = 0;
645   priv->tentative_match_len = 0;
646
647   str = g_string_new (NULL);
648   
649   i = 0;
650   while (i < n_compose)
651     {
652       gunichar ch;
653       
654       ch = gdk_keyval_to_unicode (priv->compose_buffer[i]);
655       
656       if (ch == 0)
657         return FALSE;
658
659       if (!g_unichar_isxdigit (ch))
660         return FALSE;
661
662       buf[g_unichar_to_utf8 (ch, buf)] = '\0';
663
664       g_string_append (str, buf);
665       
666       ++i;
667     }
668
669   n = strtoul (str->str, &nptr, 16);
670
671   /* If strtoul fails it probably means non-latin digits were used;
672    * we should in principle handle that, but we probably don't.
673    */
674   if (nptr - str->str < str->len)
675     {
676       g_string_free (str, TRUE);
677       return FALSE;
678     }
679   else
680     g_string_free (str, TRUE);
681
682   if (g_unichar_validate (n))
683     {
684       priv->tentative_match = n;
685       priv->tentative_match_len = n_compose;
686     }
687   
688   return TRUE;
689 }
690
691 static void
692 beep_window (GdkWindow *window)
693 {
694   GdkScreen *screen = gdk_window_get_screen (window);
695   gboolean   beep;
696
697   g_object_get (gtk_settings_get_for_screen (screen),
698                 "gtk-error-bell", &beep,
699                 NULL);
700
701   if (beep)
702     gdk_window_beep (window);
703 }
704
705 static gboolean
706 no_sequence_matches (GtkIMContextSimple *context_simple,
707                      gint                n_compose,
708                      GdkEventKey        *event)
709 {
710   GtkIMContextSimplePrivate *priv = context_simple->priv;
711   GtkIMContext *context;
712   gunichar ch;
713   
714   context = GTK_IM_CONTEXT (context_simple);
715   
716   /* No compose sequences found, check first if we have a partial
717    * match pending.
718    */
719   if (priv->tentative_match)
720     {
721       gint len = priv->tentative_match_len;
722       int i;
723       
724       gtk_im_context_simple_commit_char (context, priv->tentative_match);
725       priv->compose_buffer[0] = 0;
726       
727       for (i=0; i < n_compose - len - 1; i++)
728         {
729           GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
730           tmp_event->key.keyval = priv->compose_buffer[len + i];
731           
732           gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
733           gdk_event_free (tmp_event);
734         }
735
736       return gtk_im_context_filter_keypress (context, event);
737     }
738   else
739     {
740       priv->compose_buffer[0] = 0;
741       if (n_compose > 1)                /* Invalid sequence */
742         {
743           beep_window (event->window);
744           return TRUE;
745         }
746   
747       ch = gdk_keyval_to_unicode (event->keyval);
748       if (ch != 0 && !g_unichar_iscntrl (ch))
749         {
750           gtk_im_context_simple_commit_char (context, ch);
751           return TRUE;
752         }
753       else
754         return FALSE;
755     }
756 }
757
758 static gboolean
759 is_hex_keyval (guint keyval)
760 {
761   gunichar ch = gdk_keyval_to_unicode (keyval);
762
763   return g_unichar_isxdigit (ch);
764 }
765
766 static guint
767 canonical_hex_keyval (GdkEventKey *event)
768 {
769   GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_window_get_display (event->window));
770   guint keyval;
771   guint *keyvals = NULL;
772   gint n_vals = 0;
773   gint i;
774   
775   /* See if the keyval is already a hex digit */
776   if (is_hex_keyval (event->keyval))
777     return event->keyval;
778
779   /* See if this key would have generated a hex keyval in
780    * any other state, and return that hex keyval if so
781    */
782   gdk_keymap_get_entries_for_keycode (keymap,
783                                       event->hardware_keycode,
784                                       NULL,
785                                       &keyvals, &n_vals);
786
787   keyval = 0;
788   i = 0;
789   while (i < n_vals)
790     {
791       if (is_hex_keyval (keyvals[i]))
792         {
793           keyval = keyvals[i];
794           break;
795         }
796
797       ++i;
798     }
799
800   g_free (keyvals);
801   
802   if (keyval)
803     return keyval;
804   else
805     /* No way to make it a hex digit
806      */
807     return 0;
808 }
809
810 static gboolean
811 gtk_im_context_simple_filter_keypress (GtkIMContext *context,
812                                        GdkEventKey  *event)
813 {
814   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
815   GtkIMContextSimplePrivate *priv = context_simple->priv;
816   GdkDisplay *display = gdk_window_get_display (event->window);
817   GSList *tmp_list;  
818   int n_compose = 0;
819   GdkModifierType hex_mod_mask;
820   gboolean have_hex_mods;
821   gboolean is_hex_start;
822   gboolean is_hex_end;
823   gboolean is_backspace;
824   gboolean is_escape;
825   guint hex_keyval;
826   int i;
827
828   while (priv->compose_buffer[n_compose] != 0)
829     n_compose++;
830
831   if (event->type == GDK_KEY_RELEASE)
832     {
833       if (priv->in_hex_sequence &&
834           (event->keyval == GDK_KEY_Control_L || event->keyval == GDK_KEY_Control_R ||
835            event->keyval == GDK_KEY_Shift_L || event->keyval == GDK_KEY_Shift_R))
836         {
837           if (priv->tentative_match &&
838               g_unichar_validate (priv->tentative_match))
839             {
840               gtk_im_context_simple_commit_char (context, priv->tentative_match);
841               priv->compose_buffer[0] = 0;
842
843             }
844           else if (n_compose == 0)
845             {
846               priv->modifiers_dropped = TRUE;
847             }
848           else
849             {
850               /* invalid hex sequence */
851               beep_window (event->window);
852               
853               priv->tentative_match = 0;
854               priv->in_hex_sequence = FALSE;
855               priv->compose_buffer[0] = 0;
856               
857               g_signal_emit_by_name (context_simple, "preedit-changed");
858               g_signal_emit_by_name (context_simple, "preedit-end");
859             }
860
861           return TRUE;
862         }
863       else
864         return FALSE;
865     }
866
867   /* Ignore modifier key presses */
868   for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
869     if (event->keyval == gtk_compose_ignore[i])
870       return FALSE;
871
872   hex_mod_mask = gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
873                                                GDK_MODIFIER_INTENT_PRIMARY_ACCELERATOR);
874   hex_mod_mask |= GDK_SHIFT_MASK;
875
876   if (priv->in_hex_sequence && priv->modifiers_dropped)
877     have_hex_mods = TRUE;
878   else
879     have_hex_mods = (event->state & (hex_mod_mask)) == hex_mod_mask;
880   is_hex_start = event->keyval == GDK_KEY_U;
881   is_hex_end = (event->keyval == GDK_KEY_space ||
882                 event->keyval == GDK_KEY_KP_Space ||
883                 event->keyval == GDK_KEY_Return ||
884                 event->keyval == GDK_KEY_ISO_Enter ||
885                 event->keyval == GDK_KEY_KP_Enter);
886   is_backspace = event->keyval == GDK_KEY_BackSpace;
887   is_escape = event->keyval == GDK_KEY_Escape;
888   hex_keyval = canonical_hex_keyval (event);
889
890   /* If we are already in a non-hex sequence, or
891    * this keystroke is not hex modifiers + hex digit, don't filter
892    * key events with accelerator modifiers held down. We only treat
893    * Control and Alt as accel modifiers here, since Super, Hyper and
894    * Meta are often co-located with Mode_Switch, Multi_Key or
895    * ISO_Level3_Switch.
896    */
897   if (!have_hex_mods ||
898       (n_compose > 0 && !priv->in_hex_sequence) ||
899       (n_compose == 0 && !priv->in_hex_sequence && !is_hex_start) ||
900       (priv->in_hex_sequence && !hex_keyval &&
901        !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
902     {
903       GdkDisplay *display;
904       GdkModifierType no_text_input_mask;
905
906       display = gdk_window_get_display (event->window);
907
908       no_text_input_mask =
909         gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
910                                       GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
911
912       if (event->state & no_text_input_mask ||
913           (priv->in_hex_sequence && priv->modifiers_dropped &&
914            (event->keyval == GDK_KEY_Return ||
915             event->keyval == GDK_KEY_ISO_Enter ||
916             event->keyval == GDK_KEY_KP_Enter)))
917         {
918           return FALSE;
919         }
920     }
921   
922   /* Handle backspace */
923   if (priv->in_hex_sequence && have_hex_mods && is_backspace)
924     {
925       if (n_compose > 0)
926         {
927           n_compose--;
928           priv->compose_buffer[n_compose] = 0;
929           check_hex (context_simple, n_compose);
930         }
931       else
932         {
933           priv->in_hex_sequence = FALSE;
934         }
935
936       g_signal_emit_by_name (context_simple, "preedit-changed");
937
938       if (!priv->in_hex_sequence)
939         g_signal_emit_by_name (context_simple, "preedit-end");
940       
941       return TRUE;
942     }
943
944   /* Check for hex sequence restart */
945   if (priv->in_hex_sequence && have_hex_mods && is_hex_start)
946     {
947       if (priv->tentative_match &&
948           g_unichar_validate (priv->tentative_match))
949         {
950           gtk_im_context_simple_commit_char (context, priv->tentative_match);
951           priv->compose_buffer[0] = 0;
952         }
953       else 
954         {
955           /* invalid hex sequence */
956           if (n_compose > 0)
957             beep_window (event->window);
958           
959           priv->tentative_match = 0;
960           priv->in_hex_sequence = FALSE;
961           priv->compose_buffer[0] = 0;
962         }
963     }
964   
965   /* Check for hex sequence start */
966   if (!priv->in_hex_sequence && have_hex_mods && is_hex_start)
967     {
968       priv->compose_buffer[0] = 0;
969       priv->in_hex_sequence = TRUE;
970       priv->modifiers_dropped = FALSE;
971       priv->tentative_match = 0;
972
973       g_signal_emit_by_name (context_simple, "preedit-start");
974       g_signal_emit_by_name (context_simple, "preedit-changed");
975   
976       return TRUE;
977     }
978   
979   /* Then, check for compose sequences */
980   if (priv->in_hex_sequence)
981     {
982       if (hex_keyval)
983         priv->compose_buffer[n_compose++] = hex_keyval;
984       else if (is_escape)
985         {
986           gtk_im_context_simple_reset (context);
987           
988           return TRUE;
989         }
990       else if (!is_hex_end)
991         {
992           /* non-hex character in hex sequence */
993           beep_window (event->window);
994           
995           return TRUE;
996         }
997     }
998   else
999     priv->compose_buffer[n_compose++] = event->keyval;
1000
1001   priv->compose_buffer[n_compose] = 0;
1002
1003   if (priv->in_hex_sequence)
1004     {
1005       /* If the modifiers are still held down, consider the sequence again */
1006       if (have_hex_mods)
1007         {
1008           /* space or return ends the sequence, and we eat the key */
1009           if (n_compose > 0 && is_hex_end)
1010             {
1011               if (priv->tentative_match &&
1012                   g_unichar_validate (priv->tentative_match))
1013                 {
1014                   gtk_im_context_simple_commit_char (context, priv->tentative_match);
1015                   priv->compose_buffer[0] = 0;
1016                 }
1017               else
1018                 {
1019                   /* invalid hex sequence */
1020                   beep_window (event->window);
1021
1022                   priv->tentative_match = 0;
1023                   priv->in_hex_sequence = FALSE;
1024                   priv->compose_buffer[0] = 0;
1025                 }
1026             }
1027           else if (!check_hex (context_simple, n_compose))
1028             beep_window (event->window);
1029           
1030           g_signal_emit_by_name (context_simple, "preedit-changed");
1031
1032           if (!priv->in_hex_sequence)
1033             g_signal_emit_by_name (context_simple, "preedit-end");
1034
1035           return TRUE;
1036         }
1037     }
1038   else
1039     {
1040       tmp_list = priv->tables;
1041       while (tmp_list)
1042         {
1043           if (check_table (context_simple, tmp_list->data, n_compose))
1044             return TRUE;
1045           tmp_list = tmp_list->next;
1046         }
1047
1048       GTK_NOTE (MISC, {
1049           g_print ("[ ");
1050           for (i = 0; i < n_compose; i++)
1051             {
1052               const gchar *keyval_name = gdk_keyval_name (priv->compose_buffer[i]);
1053               
1054               if (keyval_name != NULL)
1055                 g_print ("%s ", keyval_name);
1056               else
1057                 g_print ("%04x ", priv->compose_buffer[i]);
1058             }
1059           g_print ("] ");
1060         });
1061
1062 #ifdef GDK_WINDOWING_WIN32
1063       if (check_win32_special_cases (context_simple, n_compose))
1064         return TRUE;
1065 #endif
1066
1067       if (check_compact_table (context_simple, &gtk_compose_table_compact, n_compose))
1068         return TRUE;
1069   
1070       if (check_algorithmically (context_simple, n_compose))
1071         return TRUE;
1072     }
1073   
1074   /* The current compose_buffer doesn't match anything */
1075   return no_sequence_matches (context_simple, n_compose, event);
1076 }
1077
1078 static void
1079 gtk_im_context_simple_reset (GtkIMContext *context)
1080 {
1081   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1082   GtkIMContextSimplePrivate *priv = context_simple->priv;
1083
1084   priv->compose_buffer[0] = 0;
1085
1086   if (priv->tentative_match || priv->in_hex_sequence)
1087     {
1088       priv->in_hex_sequence = FALSE;
1089       priv->tentative_match = 0;
1090       priv->tentative_match_len = 0;
1091       g_signal_emit_by_name (context_simple, "preedit-changed");
1092       g_signal_emit_by_name (context_simple, "preedit-end");
1093     }
1094 }
1095
1096 static void     
1097 gtk_im_context_simple_get_preedit_string (GtkIMContext   *context,
1098                                           gchar         **str,
1099                                           PangoAttrList **attrs,
1100                                           gint           *cursor_pos)
1101 {
1102   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1103   GtkIMContextSimplePrivate *priv = context_simple->priv;
1104   char outbuf[37]; /* up to 6 hex digits */
1105   int len = 0;
1106
1107   if (priv->in_hex_sequence)
1108     {
1109       int hexchars = 0;
1110          
1111       outbuf[0] = 'u';
1112       len = 1;
1113
1114       while (priv->compose_buffer[hexchars] != 0)
1115         {
1116           len += g_unichar_to_utf8 (gdk_keyval_to_unicode (priv->compose_buffer[hexchars]),
1117                                     outbuf + len);
1118           ++hexchars;
1119         }
1120
1121       g_assert (len < 25);
1122     }
1123   else if (priv->tentative_match)
1124     len = g_unichar_to_utf8 (priv->tentative_match, outbuf);
1125       
1126   outbuf[len] = '\0';      
1127
1128   if (str)
1129     *str = g_strdup (outbuf);
1130
1131   if (attrs)
1132     {
1133       *attrs = pango_attr_list_new ();
1134       
1135       if (len)
1136         {
1137           PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
1138           attr->start_index = 0;
1139           attr->end_index = len;
1140           pango_attr_list_insert (*attrs, attr);
1141         }
1142     }
1143
1144   if (cursor_pos)
1145     *cursor_pos = len;
1146 }
1147
1148 /**
1149  * gtk_im_context_simple_add_table: (skip)
1150  * @context_simple: A #GtkIMContextSimple
1151  * @data: the table 
1152  * @max_seq_len: Maximum length of a sequence in the table
1153  *               (cannot be greater than #GTK_MAX_COMPOSE_LEN)
1154  * @n_seqs: number of sequences in the table
1155  * 
1156  * Adds an additional table to search to the input context.
1157  * Each row of the table consists of @max_seq_len key symbols
1158  * followed by two #guint16 interpreted as the high and low
1159  * words of a #gunicode value. Tables are searched starting
1160  * from the last added.
1161  *
1162  * The table must be sorted in dictionary order on the
1163  * numeric value of the key symbol fields. (Values beyond
1164  * the length of the sequence should be zero.)
1165  **/
1166 void
1167 gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
1168                                  guint16            *data,
1169                                  gint                max_seq_len,
1170                                  gint                n_seqs)
1171 {
1172   GtkIMContextSimplePrivate *priv = context_simple->priv;
1173   GtkComposeTable *table;
1174
1175   g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
1176   g_return_if_fail (data != NULL);
1177   g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
1178   
1179   table = g_new (GtkComposeTable, 1);
1180   table->data = data;
1181   table->max_seq_len = max_seq_len;
1182   table->n_seqs = n_seqs;
1183
1184   priv->tables = g_slist_prepend (priv->tables, table);
1185 }