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