]> Pileus Git - ~andy/gtk/blob - gtk/gtkimcontextsimple.c
GtkIMContextSimple: avoid gdk_window_get_user_data
[~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 #include <stdlib.h>
22 #include <string.h>
23
24 #include <gdk/gdkkeysyms-compat.h>
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 23 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_Shift_L,
91   GDK_Shift_R,
92   GDK_Control_L,
93   GDK_Control_R,
94   GDK_Caps_Lock,
95   GDK_Shift_Lock,
96   GDK_Meta_L,
97   GDK_Meta_R,
98   GDK_Alt_L,
99   GDK_Alt_R,
100   GDK_Super_L,
101   GDK_Super_R,
102   GDK_Hyper_L,
103   GDK_Hyper_R,
104   GDK_Mode_switch,
105   GDK_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 their 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_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_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_dead_grave && (k) <= (GDK_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_space)
327     {
328       gunichar value = 0;
329
330       switch (priv->compose_buffer[0])
331         {
332         case GDK_dead_acute:
333           value = 0x00B4; break;
334         case GDK_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   if (combination_buffer[0] >= 0x390 && combination_buffer[0] <= 0x3FF)
485     {
486       for (i = 1; i < n_compose; i++ )
487         if (combination_buffer[i] == 0x303)
488           combination_buffer[i] = 0x342;
489     }
490
491   memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
492
493   for (i = 0; i < n_combinations; i++ )
494     {
495       g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
496       combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
497       nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);         
498
499       if (g_utf8_strlen (nfc_temp, -1) == 1)
500         {
501           memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
502
503           g_free (combination_utf8_temp);
504           g_free (nfc_temp);
505
506           return TRUE;
507         }
508
509       g_free (combination_utf8_temp);
510       g_free (nfc_temp);
511
512       if (n_compose > 2)
513         {
514           temp_swap = combination_buffer_temp[i % (n_compose - 1) + 1];
515           combination_buffer_temp[i % (n_compose - 1) + 1] = combination_buffer_temp[(i+1) % (n_compose - 1) + 1];
516           combination_buffer_temp[(i+1) % (n_compose - 1) + 1] = temp_swap;
517         }
518       else
519         break;
520     }
521
522   return FALSE;
523 }
524
525 static gboolean
526 check_algorithmically (GtkIMContextSimple    *context_simple,
527                        gint                   n_compose)
528
529 {
530   GtkIMContextSimplePrivate *priv = context_simple->priv;
531   gint i;
532   gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
533   gchar *combination_utf8, *nfc;
534
535   if (n_compose >= GTK_MAX_COMPOSE_LEN)
536     return FALSE;
537
538   for (i = 0; i < n_compose && IS_DEAD_KEY (priv->compose_buffer[i]); i++)
539     ;
540   if (i == n_compose)
541     return TRUE;
542
543   if (i > 0 && i == n_compose - 1)
544     {
545       combination_buffer[0] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
546       combination_buffer[n_compose] = 0;
547       i--;
548       while (i >= 0)
549         {
550           switch (priv->compose_buffer[i])
551             {
552 #define CASE(keysym, unicode) \
553             case GDK_dead_##keysym: combination_buffer[i+1] = unicode; break
554
555             CASE (grave, 0x0300);
556             CASE (acute, 0x0301);
557             CASE (circumflex, 0x0302);
558             CASE (tilde, 0x0303);       /* Also used with perispomeni, 0x342. */
559             CASE (macron, 0x0304);
560             CASE (breve, 0x0306);
561             CASE (abovedot, 0x0307);
562             CASE (diaeresis, 0x0308);
563             CASE (hook, 0x0309);
564             CASE (abovering, 0x030A);
565             CASE (doubleacute, 0x030B);
566             CASE (caron, 0x030C);
567             CASE (abovecomma, 0x0313);         /* Equivalent to psili */
568             CASE (abovereversedcomma, 0x0314); /* Equivalent to dasia */
569             CASE (horn, 0x031B);        /* Legacy use for psili, 0x313 (or 0x343). */
570             CASE (belowdot, 0x0323);
571             CASE (cedilla, 0x0327);
572             CASE (ogonek, 0x0328);      /* Legacy use for dasia, 0x314.*/
573             CASE (iota, 0x0345);
574             CASE (voiced_sound, 0x3099);        /* Per Markus Kuhn keysyms.txt file. */
575             CASE (semivoiced_sound, 0x309A);    /* Per Markus Kuhn keysyms.txt file. */
576
577             /* The following cases are to be removed once xkeyboard-config,
578              * xorg are fully updated.
579              */
580             /* Workaround for typo in 1.4.x xserver-xorg */
581             case 0xfe66: combination_buffer[i+1] = 0x314; break;
582             /* CASE (dasia, 0x314); */
583             /* CASE (perispomeni, 0x342); */
584             /* CASE (psili, 0x343); */
585 #undef CASE
586             default:
587               combination_buffer[i+1] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
588             }
589           i--;
590         }
591       
592       /* If the buffer normalizes to a single character, 
593        * then modify the order of combination_buffer accordingly, if necessary,
594        * 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 #define HEX_MOD_MASK (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)
632
633 static gboolean
634 check_hex (GtkIMContextSimple *context_simple,
635            gint                n_compose)
636 {
637   GtkIMContextSimplePrivate *priv = context_simple->priv;
638   /* See if this is a hex sequence, return TRUE if so */
639   gint i;
640   GString *str;
641   gulong n;
642   gchar *nptr = NULL;
643   gchar buf[7];
644
645   priv->tentative_match = 0;
646   priv->tentative_match_len = 0;
647
648   str = g_string_new (NULL);
649   
650   i = 0;
651   while (i < n_compose)
652     {
653       gunichar ch;
654       
655       ch = gdk_keyval_to_unicode (priv->compose_buffer[i]);
656       
657       if (ch == 0)
658         return FALSE;
659
660       if (!g_unichar_isxdigit (ch))
661         return FALSE;
662
663       buf[g_unichar_to_utf8 (ch, buf)] = '\0';
664
665       g_string_append (str, buf);
666       
667       ++i;
668     }
669
670   n = strtoul (str->str, &nptr, 16);
671
672   /* if strtoul fails it probably means non-latin digits were used;
673    * we should in principle handle that, but we probably don't.
674    */
675   if (nptr - str->str < str->len)
676     {
677       g_string_free (str, TRUE);
678       return FALSE;
679     }
680   else
681     g_string_free (str, TRUE);
682
683   if (g_unichar_validate (n))
684     {
685       priv->tentative_match = n;
686       priv->tentative_match_len = n_compose;
687     }
688   
689   return TRUE;
690 }
691
692 static void
693 beep_window (GdkWindow *window)
694 {
695   GdkScreen *screen = gdk_window_get_screen (GDK_DRAWABLE (window));
696   gboolean   beep;
697
698   g_object_get (gtk_settings_get_for_screen (screen),
699                 "gtk-error-bell", &beep,
700                 NULL);
701
702   if (beep)
703     gdk_window_beep (window);
704 }
705
706 static gboolean
707 no_sequence_matches (GtkIMContextSimple *context_simple,
708                      gint                n_compose,
709                      GdkEventKey        *event)
710 {
711   GtkIMContextSimplePrivate *priv = context_simple->priv;
712   GtkIMContext *context;
713   gunichar ch;
714   
715   context = GTK_IM_CONTEXT (context_simple);
716   
717   /* No compose sequences found, check first if we have a partial
718    * match pending.
719    */
720   if (priv->tentative_match)
721     {
722       gint len = priv->tentative_match_len;
723       int i;
724       
725       gtk_im_context_simple_commit_char (context, priv->tentative_match);
726       priv->compose_buffer[0] = 0;
727       
728       for (i=0; i < n_compose - len - 1; i++)
729         {
730           GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
731           tmp_event->key.keyval = priv->compose_buffer[len + i];
732           
733           gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
734           gdk_event_free (tmp_event);
735         }
736
737       return gtk_im_context_filter_keypress (context, event);
738     }
739   else
740     {
741       priv->compose_buffer[0] = 0;
742       if (n_compose > 1)                /* Invalid sequence */
743         {
744           beep_window (event->window);
745           return TRUE;
746         }
747   
748       ch = gdk_keyval_to_unicode (event->keyval);
749       if (ch != 0)
750         {
751           gtk_im_context_simple_commit_char (context, ch);
752           return TRUE;
753         }
754       else
755         return FALSE;
756     }
757 }
758
759 static gboolean
760 is_hex_keyval (guint keyval)
761 {
762   gunichar ch = gdk_keyval_to_unicode (keyval);
763
764   return g_unichar_isxdigit (ch);
765 }
766
767 static guint
768 canonical_hex_keyval (GdkEventKey *event)
769 {
770   GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_window_get_display (event->window));
771   guint keyval;
772   guint *keyvals = NULL;
773   gint n_vals = 0;
774   gint i;
775   
776   /* See if the keyval is already a hex digit */
777   if (is_hex_keyval (event->keyval))
778     return event->keyval;
779
780   /* See if this key would have generated a hex keyval in
781    * any other state, and return that hex keyval if so
782    */
783   gdk_keymap_get_entries_for_keycode (keymap,
784                                       event->hardware_keycode,
785                                       NULL,
786                                       &keyvals, &n_vals);
787
788   keyval = 0;
789   i = 0;
790   while (i < n_vals)
791     {
792       if (is_hex_keyval (keyvals[i]))
793         {
794           keyval = keyvals[i];
795           break;
796         }
797
798       ++i;
799     }
800
801   g_free (keyvals);
802   
803   if (keyval)
804     return keyval;
805   else
806     /* No way to make it a hex digit
807      */
808     return 0;
809 }
810
811 static gboolean
812 gtk_im_context_simple_filter_keypress (GtkIMContext *context,
813                                        GdkEventKey  *event)
814 {
815   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
816   GtkIMContextSimplePrivate *priv = context_simple->priv;
817   GSList *tmp_list;  
818   int n_compose = 0;
819   gboolean have_hex_mods;
820   gboolean is_hex_start;
821   gboolean is_hex_end;
822   gboolean is_backspace;
823   gboolean is_escape;
824   guint hex_keyval;
825   int i;
826
827   while (priv->compose_buffer[n_compose] != 0)
828     n_compose++;
829
830   if (event->type == GDK_KEY_RELEASE)
831     {
832       if (priv->in_hex_sequence &&
833           (event->keyval == GDK_Control_L || event->keyval == GDK_Control_R ||
834            event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R))
835         {
836           if (priv->tentative_match &&
837               g_unichar_validate (priv->tentative_match))
838             {
839               gtk_im_context_simple_commit_char (context, priv->tentative_match);
840               priv->compose_buffer[0] = 0;
841
842             }
843           else if (n_compose == 0)
844             {
845               priv->modifiers_dropped = TRUE;
846             }
847           else
848             {
849               /* invalid hex sequence */
850               beep_window (event->window);
851               
852               priv->tentative_match = 0;
853               priv->in_hex_sequence = FALSE;
854               priv->compose_buffer[0] = 0;
855               
856               g_signal_emit_by_name (context_simple, "preedit-changed");
857               g_signal_emit_by_name (context_simple, "preedit-end");
858             }
859
860           return TRUE;
861         }
862       else
863         return FALSE;
864     }
865
866   /* Ignore modifier key presses */
867   for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
868     if (event->keyval == gtk_compose_ignore[i])
869       return FALSE;
870
871   if (priv->in_hex_sequence && priv->modifiers_dropped)
872     have_hex_mods = TRUE;
873   else
874     have_hex_mods = (event->state & (HEX_MOD_MASK)) == HEX_MOD_MASK;
875   is_hex_start = event->keyval == GDK_U;
876   is_hex_end = (event->keyval == GDK_space || 
877                 event->keyval == GDK_KP_Space ||
878                 event->keyval == GDK_Return || 
879                 event->keyval == GDK_ISO_Enter ||
880                 event->keyval == GDK_KP_Enter);
881   is_backspace = event->keyval == GDK_BackSpace;
882   is_escape = event->keyval == GDK_Escape;
883   hex_keyval = canonical_hex_keyval (event);
884
885   /* If we are already in a non-hex sequence, or
886    * this keystroke is not hex modifiers + hex digit, don't filter
887    * key events with accelerator modifiers held down. We only treat
888    * Control and Alt as accel modifiers here, since Super, Hyper and
889    * Meta are often co-located with Mode_Switch, Multi_Key or
890    * ISO_Level3_Switch.
891    */
892   if (!have_hex_mods ||
893       (n_compose > 0 && !priv->in_hex_sequence) ||
894       (n_compose == 0 && !priv->in_hex_sequence && !is_hex_start) ||
895       (priv->in_hex_sequence && !hex_keyval &&
896        !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
897     {
898       if (event->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK) ||
899           (priv->in_hex_sequence && priv->modifiers_dropped &&
900            (event->keyval == GDK_Return || 
901             event->keyval == GDK_ISO_Enter ||
902             event->keyval == GDK_KP_Enter)))
903         {
904           return FALSE;
905         }
906     }
907   
908   /* Handle backspace */
909   if (priv->in_hex_sequence && have_hex_mods && is_backspace)
910     {
911       if (n_compose > 0)
912         {
913           n_compose--;
914           priv->compose_buffer[n_compose] = 0;
915           check_hex (context_simple, n_compose);
916         }
917       else
918         {
919           priv->in_hex_sequence = FALSE;
920         }
921
922       g_signal_emit_by_name (context_simple, "preedit-changed");
923
924       if (!priv->in_hex_sequence)
925         g_signal_emit_by_name (context_simple, "preedit-end");
926       
927       return TRUE;
928     }
929
930   /* Check for hex sequence restart */
931   if (priv->in_hex_sequence && have_hex_mods && is_hex_start)
932     {
933       if (priv->tentative_match &&
934           g_unichar_validate (priv->tentative_match))
935         {
936           gtk_im_context_simple_commit_char (context, priv->tentative_match);
937           priv->compose_buffer[0] = 0;
938         }
939       else 
940         {
941           /* invalid hex sequence */
942           if (n_compose > 0)
943             beep_window (event->window);
944           
945           priv->tentative_match = 0;
946           priv->in_hex_sequence = FALSE;
947           priv->compose_buffer[0] = 0;
948         }
949     }
950   
951   /* Check for hex sequence start */
952   if (!priv->in_hex_sequence && have_hex_mods && is_hex_start)
953     {
954       priv->compose_buffer[0] = 0;
955       priv->in_hex_sequence = TRUE;
956       priv->modifiers_dropped = FALSE;
957       priv->tentative_match = 0;
958
959       g_signal_emit_by_name (context_simple, "preedit-start");
960       g_signal_emit_by_name (context_simple, "preedit-changed");
961   
962       return TRUE;
963     }
964   
965   /* Then, check for compose sequences */
966   if (priv->in_hex_sequence)
967     {
968       if (hex_keyval)
969         priv->compose_buffer[n_compose++] = hex_keyval;
970       else if (is_escape)
971         {
972           gtk_im_context_simple_reset (context);
973           
974           return TRUE;
975         }
976       else if (!is_hex_end)
977         {
978           /* non-hex character in hex sequence */
979           beep_window (event->window);
980           
981           return TRUE;
982         }
983     }
984   else
985     priv->compose_buffer[n_compose++] = event->keyval;
986
987   priv->compose_buffer[n_compose] = 0;
988
989   if (priv->in_hex_sequence)
990     {
991       /* If the modifiers are still held down, consider the sequence again */
992       if (have_hex_mods)
993         {
994           /* space or return ends the sequence, and we eat the key */
995           if (n_compose > 0 && is_hex_end)
996             {
997               if (priv->tentative_match &&
998                   g_unichar_validate (priv->tentative_match))
999                 {
1000                   gtk_im_context_simple_commit_char (context, priv->tentative_match);
1001                   priv->compose_buffer[0] = 0;
1002                 }
1003               else
1004                 {
1005                   /* invalid hex sequence */
1006                   beep_window (event->window);
1007
1008                   priv->tentative_match = 0;
1009                   priv->in_hex_sequence = FALSE;
1010                   priv->compose_buffer[0] = 0;
1011                 }
1012             }
1013           else if (!check_hex (context_simple, n_compose))
1014             beep_window (event->window);
1015           
1016           g_signal_emit_by_name (context_simple, "preedit-changed");
1017
1018           if (!priv->in_hex_sequence)
1019             g_signal_emit_by_name (context_simple, "preedit-end");
1020
1021           return TRUE;
1022         }
1023     }
1024   else
1025     {
1026       tmp_list = priv->tables;
1027       while (tmp_list)
1028         {
1029           if (check_table (context_simple, tmp_list->data, n_compose))
1030             return TRUE;
1031           tmp_list = tmp_list->next;
1032         }
1033
1034       GTK_NOTE (MISC, {
1035           g_print ("[ ");
1036           for (i = 0; i < n_compose; i++)
1037             {
1038               const gchar *keyval_name = gdk_keyval_name (priv->compose_buffer[i]);
1039               
1040               if (keyval_name != NULL)
1041                 g_print ("%s ", keyval_name);
1042               else
1043                 g_print ("%04x ", priv->compose_buffer[i]);
1044             }
1045           g_print ("] ");
1046         });
1047
1048 #ifdef GDK_WINDOWING_WIN32
1049       if (check_win32_special_cases (context_simple, n_compose))
1050         return TRUE;
1051 #endif
1052
1053       if (check_compact_table (context_simple, &gtk_compose_table_compact, n_compose))
1054         return TRUE;
1055   
1056       if (check_algorithmically (context_simple, n_compose))
1057         return TRUE;
1058     }
1059   
1060   /* The current compose_buffer doesn't match anything */
1061   return no_sequence_matches (context_simple, n_compose, event);
1062 }
1063
1064 static void
1065 gtk_im_context_simple_reset (GtkIMContext *context)
1066 {
1067   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1068   GtkIMContextSimplePrivate *priv = context_simple->priv;
1069
1070   priv->compose_buffer[0] = 0;
1071
1072   if (priv->tentative_match || priv->in_hex_sequence)
1073     {
1074       priv->in_hex_sequence = FALSE;
1075       priv->tentative_match = 0;
1076       priv->tentative_match_len = 0;
1077       g_signal_emit_by_name (context_simple, "preedit-changed");
1078       g_signal_emit_by_name (context_simple, "preedit-end");
1079     }
1080 }
1081
1082 static void     
1083 gtk_im_context_simple_get_preedit_string (GtkIMContext   *context,
1084                                           gchar         **str,
1085                                           PangoAttrList **attrs,
1086                                           gint           *cursor_pos)
1087 {
1088   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1089   GtkIMContextSimplePrivate *priv = context_simple->priv;
1090   char outbuf[37]; /* up to 6 hex digits */
1091   int len = 0;
1092
1093   if (priv->in_hex_sequence)
1094     {
1095       int hexchars = 0;
1096          
1097       outbuf[0] = 'u';
1098       len = 1;
1099
1100       while (priv->compose_buffer[hexchars] != 0)
1101         {
1102           len += g_unichar_to_utf8 (gdk_keyval_to_unicode (priv->compose_buffer[hexchars]),
1103                                     outbuf + len);
1104           ++hexchars;
1105         }
1106
1107       g_assert (len < 25);
1108     }
1109   else if (priv->tentative_match)
1110     len = g_unichar_to_utf8 (priv->tentative_match, outbuf);
1111       
1112   outbuf[len] = '\0';      
1113
1114   if (str)
1115     *str = g_strdup (outbuf);
1116
1117   if (attrs)
1118     {
1119       *attrs = pango_attr_list_new ();
1120       
1121       if (len)
1122         {
1123           PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
1124           attr->start_index = 0;
1125           attr->end_index = len;
1126           pango_attr_list_insert (*attrs, attr);
1127         }
1128     }
1129
1130   if (cursor_pos)
1131     *cursor_pos = len;
1132 }
1133
1134 /**
1135  * gtk_im_context_simple_add_table:
1136  * @context_simple: A #GtkIMContextSimple
1137  * @data: the table 
1138  * @max_seq_len: Maximum length of a sequence in the table
1139  *               (cannot be greater than #GTK_MAX_COMPOSE_LEN)
1140  * @n_seqs: number of sequences in the table
1141  * 
1142  * Adds an additional table to search to the input context.
1143  * Each row of the table consists of @max_seq_len key symbols
1144  * followed by two #guint16 interpreted as the high and low
1145  * words of a #gunicode value. Tables are searched starting
1146  * from the last added.
1147  *
1148  * The table must be sorted in dictionary order on the
1149  * numeric value of the key symbol fields. (Values beyond
1150  * the length of the sequence should be zero.)
1151  **/
1152 void
1153 gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
1154                                  guint16            *data,
1155                                  gint                max_seq_len,
1156                                  gint                n_seqs)
1157 {
1158   GtkIMContextSimplePrivate *priv = context_simple->priv;
1159   GtkComposeTable *table;
1160
1161   g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
1162   g_return_if_fail (data != NULL);
1163   g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
1164   
1165   table = g_new (GtkComposeTable, 1);
1166   table->data = data;
1167   table->max_seq_len = max_seq_len;
1168   table->n_seqs = n_seqs;
1169
1170   priv->tables = g_slist_prepend (priv->tables, table);
1171 }