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