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