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