]> Pileus Git - ~andy/gtk/blob - gtk/gtkimcontextsimple.c
Emit preedit_start/_end as appropriate. (#521934, Huang Peng)
[~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   21,
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 /* When updating the table of the compose sequences, also update here.
344  */
345 #define IS_DEAD_KEY(k) \
346     (((k) >= GDK_dead_grave && (k) <= GDK_dead_stroke) || \
347      g_unichar_type (gdk_keyval_to_unicode (k)) == G_UNICODE_NON_SPACING_MARK)
348
349 static gboolean
350 check_algorithmically (GtkIMContextSimple    *context_simple,
351                        gint                   n_compose)
352
353 {
354   int i;
355   int k;
356   gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
357   gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
358   gchar *combination_utf8, *nfc;
359   gchar *combination_utf8_temp = NULL, *nfc_temp = NULL;
360
361   if (n_compose >= GTK_MAX_COMPOSE_LEN)
362     return FALSE;
363
364   for (i = 0; i < n_compose && IS_DEAD_KEY (context_simple->compose_buffer[i]); i++)
365     ;
366   if (i == n_compose)
367     return TRUE;
368
369   if (i > 0 && i == n_compose - 1)
370     {
371       combination_buffer[0] = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
372       combination_buffer[n_compose] = 0;
373       i--;
374       while (i >= 0)
375         {
376           switch (context_simple->compose_buffer[i])
377             {
378 #define CASE(keysym, unicode) \
379             case GDK_dead_##keysym: combination_buffer[i+1] = unicode; break
380
381             CASE (grave, 0x0300);
382             CASE (acute, 0x0301);
383             CASE (circumflex, 0x0302);
384             CASE (tilde, 0x0303);       /* Normally is 0x303; Greek Polytonic needs 0x342.
385                                          * We modified the compose sequences for now
386                                          * so that for Greek we don't apply algorithmic
387                                          * when perispomeni (0x342) is required
388                                          * Filed report; pending, bug 14013 (Freedesktop).
389                                          */
390             CASE (macron, 0x0304);
391             CASE (breve, 0x0306);
392             CASE (abovedot, 0x0307);
393             CASE (diaeresis, 0x0308);
394             CASE (abovering, 0x030A);
395             CASE (doubleacute, 0x030B);
396             CASE (caron, 0x030C);
397             CASE (cedilla, 0x0327);
398             CASE (ogonek, 0x0328);      /* Normally is 0x328; Greek Polytonic needs 0x314.
399                                          * We modified the compose sequences for now to 
400                                          * so that for Greek we don't apply algorithmic
401                                          * when dasia (0x314) is required
402                                          * Patch accepted in Xorg/GIT, may take a bit to propagate.
403                                          */
404             CASE (dasia, 0x314);
405             CASE (iota, 0x0345); /* Used by Greek Polytonic layout only; "ypogegrammeni" */
406             CASE (voiced_sound, 0x3099);        /* Per Marcus Khun keysyms.txt file. */
407             CASE (semivoiced_sound, 0x309a);    /* Per Marcus Khun keysyms.txt file. */
408             CASE (belowdot, 0x0323);
409             CASE (hook, 0x0309);
410             CASE (horn, 0x031b);        /* Normally is 0x31b; Greek Polytonic needs 0x313 (or 0x343).
411                                          * We modified the compose sequences for now to 
412                                          * so that for Greek we don't apply algorithmic
413                                          * when psili (0x343) is required
414                                          * Patch accepted in Xorg/GIT, may take a bit to propagate.
415                                          */
416             CASE (psili, 0x343);
417 #undef CASE
418             default:
419               combination_buffer[i+1] = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
420             }
421           i--;
422         }
423       
424       if (n_compose > 2)
425         {
426           gint n_combinations;
427           gunichar temp_swap;
428
429           /* We calculate the number of permutations of the diacritic marks, factorial(n_compose-1).
430            * When diacritic marks belong to the same Canonical Combining Class, 
431            * a normalisation does not attempt reorder them, thus we do this ourselves.
432            */
433           n_combinations = 1;
434           for (k = 1; k < n_compose; k++ )
435              n_combinations *= k;
436
437           memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
438
439           for (k = 0; k < n_combinations; k++ )
440              {
441                g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
442                combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
443                nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);                
444
445                if (g_utf8_strlen (nfc_temp, -1) == 1)
446                  {
447                    memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
448                    break;
449                  }
450
451                temp_swap = combination_buffer_temp[k % (n_compose - 1) + 1];
452                combination_buffer_temp[k % (n_compose - 1) + 1] = combination_buffer_temp[(k+1) % (n_compose - 1) + 1];
453                combination_buffer_temp[(k+1) % (n_compose - 1) + 1] = temp_swap;
454              }
455
456           g_free (combination_utf8_temp);
457           g_free (nfc_temp);
458         }
459
460       combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
461       nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
462       if (g_utf8_strlen (nfc, -1) == 1)
463         {
464           gunichar value = g_utf8_get_char (nfc);
465           gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
466           context_simple->compose_buffer[0] = 0;
467
468           g_free (combination_utf8);
469           g_free (nfc);
470           return TRUE;
471         }
472       g_free (combination_utf8);
473       g_free (nfc);
474     }
475
476   return FALSE;
477 }
478
479 /* In addition to the table-driven sequences, we allow Unicode hex
480  * codes to be entered. The method chosen here is similar to the
481  * one recommended in ISO 14755, but not exactly the same, since we
482  * don't want to steal 16 valuable key combinations. 
483  * 
484  * A hex Unicode sequence must be started with Ctrl-Shift-U, followed
485  * by a sequence of hex digits entered with Ctrl-Shift still held.
486  * Releasing one of the modifiers or pressing space while the modifiers
487  * are still held commits the character. It is possible to erase
488  * digits using backspace.
489  *
490  * As an extension to the above, we also allow to start the sequence
491  * with Ctrl-Shift-U, then release the modifiers before typing any
492  * digits, and enter the digits without modifiers.
493  */
494 #define HEX_MOD_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK)
495
496 static gboolean
497 check_hex (GtkIMContextSimple *context_simple,
498            gint                n_compose)
499 {
500   /* See if this is a hex sequence, return TRUE if so */
501   gint i;
502   GString *str;
503   gulong n;
504   gchar *nptr = NULL;
505   gchar buf[7];
506
507   context_simple->tentative_match = 0;
508   context_simple->tentative_match_len = 0;
509
510   str = g_string_new (NULL);
511   
512   i = 0;
513   while (i < n_compose)
514     {
515       gunichar ch;
516       
517       ch = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
518       
519       if (ch == 0)
520         return FALSE;
521
522       if (!g_unichar_isxdigit (ch))
523         return FALSE;
524
525       buf[g_unichar_to_utf8 (ch, buf)] = '\0';
526
527       g_string_append (str, buf);
528       
529       ++i;
530     }
531
532   n = strtoul (str->str, &nptr, 16);
533
534   /* if strtoul fails it probably means non-latin digits were used;
535    * we should in principle handle that, but we probably don't.
536    */
537   if (nptr - str->str < str->len)
538     {
539       g_string_free (str, TRUE);
540       return FALSE;
541     }
542   else
543     g_string_free (str, TRUE);
544
545   if (g_unichar_validate (n))
546     {
547       context_simple->tentative_match = n;
548       context_simple->tentative_match_len = n_compose;
549     }
550   
551   return TRUE;
552 }
553
554 static void
555 beep_window (GdkWindow *window)
556 {
557   GtkWidget *widget;
558
559   gdk_window_get_user_data (window, (gpointer) &widget);
560
561   if (GTK_IS_WIDGET (widget))
562     {
563       gtk_widget_error_bell (widget);
564     }
565   else
566     {
567       GdkScreen *screen = gdk_drawable_get_screen (GDK_DRAWABLE (window));
568       gboolean   beep;
569
570       g_object_get (gtk_settings_get_for_screen (screen),
571                     "gtk-error-bell", &beep,
572                     NULL);
573
574       if (beep)
575         gdk_window_beep (window);
576     }
577 }
578
579 static gboolean
580 no_sequence_matches (GtkIMContextSimple *context_simple,
581                      gint                n_compose,
582                      GdkEventKey        *event)
583 {
584   GtkIMContext *context;
585   gunichar ch;
586   
587   context = GTK_IM_CONTEXT (context_simple);
588   
589   /* No compose sequences found, check first if we have a partial
590    * match pending.
591    */
592   if (context_simple->tentative_match)
593     {
594       gint len = context_simple->tentative_match_len;
595       int i;
596       
597       gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
598       context_simple->compose_buffer[0] = 0;
599       
600       for (i=0; i < n_compose - len - 1; i++)
601         {
602           GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
603           tmp_event->key.keyval = context_simple->compose_buffer[len + i];
604           
605           gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
606           gdk_event_free (tmp_event);
607         }
608
609       return gtk_im_context_filter_keypress (context, event);
610     }
611   else
612     {
613       context_simple->compose_buffer[0] = 0;
614       if (n_compose > 1)                /* Invalid sequence */
615         {
616           beep_window (event->window);
617           return TRUE;
618         }
619   
620       ch = gdk_keyval_to_unicode (event->keyval);
621       if (ch != 0)
622         {
623           gtk_im_context_simple_commit_char (context, ch);
624           return TRUE;
625         }
626       else
627         return FALSE;
628     }
629 }
630
631 static gboolean
632 is_hex_keyval (guint keyval)
633 {
634   gunichar ch = gdk_keyval_to_unicode (keyval);
635
636   return g_unichar_isxdigit (ch);
637 }
638
639 static guint
640 canonical_hex_keyval (GdkEventKey *event)
641 {
642   GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_drawable_get_display (event->window));
643   guint keyval;
644   guint *keyvals = NULL;
645   gint n_vals = 0;
646   gint i;
647   
648   /* See if the keyval is already a hex digit */
649   if (is_hex_keyval (event->keyval))
650     return event->keyval;
651
652   /* See if this key would have generated a hex keyval in
653    * any other state, and return that hex keyval if so
654    */
655   gdk_keymap_get_entries_for_keycode (keymap,
656                                       event->hardware_keycode,
657                                       NULL,
658                                       &keyvals, &n_vals);
659
660   keyval = 0;
661   i = 0;
662   while (i < n_vals)
663     {
664       if (is_hex_keyval (keyvals[i]))
665         {
666           keyval = keyvals[i];
667           break;
668         }
669
670       ++i;
671     }
672
673   g_free (keyvals);
674   
675   if (keyval)
676     return keyval;
677   else
678     /* No way to make it a hex digit
679      */
680     return 0;
681 }
682
683 static gboolean
684 gtk_im_context_simple_filter_keypress (GtkIMContext *context,
685                                        GdkEventKey  *event)
686 {
687   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
688   GSList *tmp_list;  
689   int n_compose = 0;
690   gboolean have_hex_mods;
691   gboolean is_hex_start;
692   gboolean is_hex_end;
693   gboolean is_backspace;
694   gboolean is_escape;
695   guint hex_keyval;
696   int i;
697
698   while (context_simple->compose_buffer[n_compose] != 0)
699     n_compose++;
700
701   if (event->type == GDK_KEY_RELEASE)
702     {
703       if (context_simple->in_hex_sequence &&
704           (event->keyval == GDK_Control_L || event->keyval == GDK_Control_R ||
705            event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R))
706         {
707           if (context_simple->tentative_match &&
708               g_unichar_validate (context_simple->tentative_match))
709             {
710               gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
711               context_simple->compose_buffer[0] = 0;
712
713             }
714           else if (n_compose == 0)
715             {
716               context_simple->modifiers_dropped = TRUE;
717             }
718           else
719             {
720               /* invalid hex sequence */
721               beep_window (event->window);
722               
723               context_simple->tentative_match = 0;
724               context_simple->in_hex_sequence = FALSE;
725               context_simple->compose_buffer[0] = 0;
726               
727               g_signal_emit_by_name (context_simple, "preedit_changed");
728               g_signal_emit_by_name (context_simple, "preedit_end");
729             }
730
731           return TRUE;
732         }
733       else
734         return FALSE;
735     }
736
737   /* Ignore modifier key presses */
738   for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
739     if (event->keyval == gtk_compose_ignore[i])
740       return FALSE;
741
742   if (context_simple->in_hex_sequence && context_simple->modifiers_dropped)
743     have_hex_mods = TRUE;
744   else
745     have_hex_mods = (event->state & (HEX_MOD_MASK)) == HEX_MOD_MASK;
746   is_hex_start = event->keyval == GDK_U;
747   is_hex_end = (event->keyval == GDK_space || 
748                 event->keyval == GDK_KP_Space ||
749                 event->keyval == GDK_Return || 
750                 event->keyval == GDK_ISO_Enter ||
751                 event->keyval == GDK_KP_Enter);
752   is_backspace = event->keyval == GDK_BackSpace;
753   is_escape = event->keyval == GDK_Escape;
754   hex_keyval = canonical_hex_keyval (event);
755
756   /* If we are already in a non-hex sequence, or
757    * this keystroke is not hex modifiers + hex digit, don't filter 
758    * key events with accelerator modifiers held down.
759    */
760   if (!have_hex_mods ||
761       (n_compose > 0 && !context_simple->in_hex_sequence) || 
762       (n_compose == 0 && !context_simple->in_hex_sequence && !is_hex_start) ||
763       (context_simple->in_hex_sequence && !hex_keyval && 
764        !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
765     {
766       if (event->state & (gtk_accelerator_get_default_mod_mask () & ~GDK_SHIFT_MASK) ||
767           (context_simple->in_hex_sequence && context_simple->modifiers_dropped &&
768            (event->keyval == GDK_Return || 
769             event->keyval == GDK_ISO_Enter ||
770             event->keyval == GDK_KP_Enter)))
771         {
772           return FALSE;
773         }
774     }
775   
776   /* Handle backspace */
777   if (context_simple->in_hex_sequence && have_hex_mods && is_backspace)
778     {
779       if (n_compose > 0)
780         {
781           n_compose--;
782           context_simple->compose_buffer[n_compose] = 0;
783           check_hex (context_simple, n_compose);
784         }
785       else
786         {
787           context_simple->in_hex_sequence = FALSE;
788         }
789
790       g_signal_emit_by_name (context_simple, "preedit_changed");
791
792       if (!context_simple->in_hex_sequence)
793         g_signal_emit_by_name (context_simple, "preedit_end");
794       
795       return TRUE;
796     }
797
798   /* Check for hex sequence restart */
799   if (context_simple->in_hex_sequence && have_hex_mods && is_hex_start)
800     {
801       if (context_simple->tentative_match &&
802           g_unichar_validate (context_simple->tentative_match))
803         {
804           gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
805           context_simple->compose_buffer[0] = 0;
806         }
807       else 
808         {
809           /* invalid hex sequence */
810           if (n_compose > 0)
811             beep_window (event->window);
812           
813           context_simple->tentative_match = 0;
814           context_simple->in_hex_sequence = FALSE;
815           context_simple->compose_buffer[0] = 0;
816         }
817     }
818   
819   /* Check for hex sequence start */
820   if (!context_simple->in_hex_sequence && have_hex_mods && is_hex_start)
821     {
822       context_simple->compose_buffer[0] = 0;
823       context_simple->in_hex_sequence = TRUE;
824       context_simple->modifiers_dropped = FALSE;
825       context_simple->tentative_match = 0;
826
827       g_signal_emit_by_name (context_simple, "preedit_start");
828       g_signal_emit_by_name (context_simple, "preedit_changed");
829   
830       return TRUE;
831     }
832   
833   /* Then, check for compose sequences */
834   if (context_simple->in_hex_sequence)
835     {
836       if (hex_keyval)
837         context_simple->compose_buffer[n_compose++] = hex_keyval;
838       else if (is_escape)
839         {
840           gtk_im_context_simple_reset (context);
841           
842           return TRUE;
843         }
844       else if (!is_hex_end)
845         {
846           /* non-hex character in hex sequence */
847           beep_window (event->window);
848           
849           return TRUE;
850         }
851     }
852   else
853     context_simple->compose_buffer[n_compose++] = event->keyval;
854
855   context_simple->compose_buffer[n_compose] = 0;
856
857   if (context_simple->in_hex_sequence)
858     {
859       /* If the modifiers are still held down, consider the sequence again */
860       if (have_hex_mods)
861         {
862           /* space or return ends the sequence, and we eat the key */
863           if (n_compose > 0 && is_hex_end)
864             {
865               if (context_simple->tentative_match &&
866                   g_unichar_validate (context_simple->tentative_match))
867                 {
868                   gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
869                   context_simple->compose_buffer[0] = 0;
870                 }
871               else
872                 {
873                   /* invalid hex sequence */
874                   beep_window (event->window);
875
876                   context_simple->tentative_match = 0;
877                   context_simple->in_hex_sequence = FALSE;
878                   context_simple->compose_buffer[0] = 0;
879                 }
880             }
881           else if (!check_hex (context_simple, n_compose))
882             beep_window (event->window);
883           
884           g_signal_emit_by_name (context_simple, "preedit_changed");
885
886           if (!context_simple->in_hex_sequence)
887             g_signal_emit_by_name (context_simple, "preedit_end");
888
889           return TRUE;
890         }
891     }
892   else
893     {
894       tmp_list = context_simple->tables;
895       while (tmp_list)
896         {
897           if (check_table (context_simple, tmp_list->data, n_compose))
898             return TRUE;
899           tmp_list = tmp_list->next;
900         }
901   
902       if (check_compact_table (context_simple, &gtk_compose_table_compact, n_compose))
903         return TRUE;
904
905       if (check_algorithmically (context_simple, n_compose))
906         return TRUE;
907     }
908   
909   /* The current compose_buffer doesn't match anything */
910   return no_sequence_matches (context_simple, n_compose, event);
911 }
912
913 static void
914 gtk_im_context_simple_reset (GtkIMContext *context)
915 {
916   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
917
918   context_simple->compose_buffer[0] = 0;
919
920   if (context_simple->tentative_match || context_simple->in_hex_sequence)
921     {
922       context_simple->in_hex_sequence = FALSE;
923       context_simple->tentative_match = 0;
924       context_simple->tentative_match_len = 0;
925       g_signal_emit_by_name (context_simple, "preedit_changed");
926       g_signal_emit_by_name (context_simple, "preedit_end");
927     }
928 }
929
930 static void     
931 gtk_im_context_simple_get_preedit_string (GtkIMContext   *context,
932                                           gchar         **str,
933                                           PangoAttrList **attrs,
934                                           gint           *cursor_pos)
935 {
936   char outbuf[37]; /* up to 6 hex digits */
937   int len = 0;
938   
939   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
940
941   if (context_simple->in_hex_sequence)
942     {
943       int hexchars = 0;
944          
945       outbuf[0] = 'u';
946       len = 1;
947
948       while (context_simple->compose_buffer[hexchars] != 0)
949         {
950           len += g_unichar_to_utf8 (gdk_keyval_to_unicode (context_simple->compose_buffer[hexchars]),
951                                     outbuf + len);
952           ++hexchars;
953         }
954
955       g_assert (len < 25);
956     }
957   else if (context_simple->tentative_match)
958     len = g_unichar_to_utf8 (context_simple->tentative_match, outbuf);
959       
960   outbuf[len] = '\0';      
961
962   if (str)
963     *str = g_strdup (outbuf);
964
965   if (attrs)
966     {
967       *attrs = pango_attr_list_new ();
968       
969       if (len)
970         {
971           PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
972           attr->start_index = 0;
973           attr->end_index = len;
974           pango_attr_list_insert (*attrs, attr);
975         }
976     }
977
978   if (cursor_pos)
979     *cursor_pos = len;
980 }
981
982 /**
983  * gtk_im_context_simple_add_table:
984  * @context_simple: A #GtkIMContextSimple
985  * @data: the table 
986  * @max_seq_len: Maximum length of a sequence in the table
987  *               (cannot be greater than #GTK_MAX_COMPOSE_LEN)
988  * @n_seqs: number of sequences in the table
989  * 
990  * Adds an additional table to search to the input context.
991  * Each row of the table consists of @max_seq_len key symbols
992  * followed by two #guint16 interpreted as the high and low
993  * words of a #gunicode value. Tables are searched starting
994  * from the last added.
995  *
996  * The table must be sorted in dictionary order on the
997  * numeric value of the key symbol fields. (Values beyond
998  * the length of the sequence should be zero.)
999  **/
1000 void
1001 gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
1002                                  guint16            *data,
1003                                  gint                max_seq_len,
1004                                  gint                n_seqs)
1005 {
1006   GtkComposeTable *table;
1007
1008   g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
1009   g_return_if_fail (data != NULL);
1010   g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
1011   
1012   table = g_new (GtkComposeTable, 1);
1013   table->data = data;
1014   table->max_seq_len = max_seq_len;
1015   table->n_seqs = n_seqs;
1016
1017   context_simple->tables = g_slist_prepend (context_simple->tables, table);
1018 }
1019
1020 #define __GTK_IM_CONTEXT_SIMPLE_C__
1021 #include "gtkaliasdef.c"