]> Pileus Git - ~andy/gtk/blob - gtk/gtkimcontextsimple.c
Updated Norwegian bokmål translation.
[~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 23 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   23,
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 /* Checks if a keysym is a dead key. Dead key keysym values are defined in
274  * ../gdk/gdkkeysyms.h and the first is GDK_dead_grave. As X.Org is updated,
275  * more dead keys are added and we need to update the upper limit.
276  * Currently, the upper limit is GDK_dead_dasia+1. The +1 has to do with 
277  * a temporary issue in the X.Org header files. 
278  * In future versions it will be just the keysym (no +1).
279  */
280 #define IS_DEAD_KEY(k) \
281     ((k) >= GDK_dead_grave && (k) <= (GDK_dead_dasia+1))
282
283 #ifdef GDK_WINDOWING_WIN32
284
285 /* On Windows, user expectation is that typing a dead accent followed
286  * by space will input the corresponding spacing character. The X
287  * compose tables are different for dead acute and diaeresis, which
288  * when followed by space produce a plain ASCII apostrophe and double
289  * quote respectively. So special-case those.
290  */
291
292 static gboolean
293 check_win32_special_cases (GtkIMContextSimple    *context_simple,
294                            gint                   n_compose)
295 {
296   if (n_compose == 2 &&
297       context_simple->compose_buffer[1] == GDK_space)
298     {
299       gunichar value = 0;
300
301       switch (context_simple->compose_buffer[0])
302         {
303         case GDK_dead_acute:
304           value = 0x00B4; break;
305         case GDK_dead_diaeresis:
306           value = 0x00A8; break;
307         }
308       if (value > 0)
309         {
310           gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
311           context_simple->compose_buffer[0] = 0;
312
313           GTK_NOTE (MISC, g_print ("win32: U+%04X\n", value));
314           return TRUE;
315         }
316     }
317   return FALSE;
318 }
319
320 static void
321 check_win32_special_case_after_compact_match (GtkIMContextSimple    *context_simple,
322                                               gint                   n_compose,
323                                               guint                  value)
324 {
325   /* On Windows user expectation is that typing two dead accents will input
326    * two corresponding spacing accents.
327    */
328   if (n_compose == 2 &&
329       context_simple->compose_buffer[0] == context_simple->compose_buffer[1] &&
330       IS_DEAD_KEY (context_simple->compose_buffer[0]))
331     {
332       gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
333       GTK_NOTE (MISC, g_print ("win32: U+%04X ", value));
334     }
335 }
336
337 #endif
338
339 static gboolean
340 check_compact_table (GtkIMContextSimple    *context_simple,
341              const GtkComposeTableCompact *table,
342              gint                   n_compose)
343 {
344   gint row_stride;
345   guint16 *seq_index;
346   guint16 *seq; 
347   gint i;
348
349   /* Will never match, if the sequence in the compose buffer is longer
350    * than the sequences in the table.  Further, compare_seq (key, val)
351    * will overrun val if key is longer than val. */
352   if (n_compose > table->max_seq_len)
353     return FALSE;
354   
355   seq_index = bsearch (context_simple->compose_buffer,
356                  table->data, table->n_index_size,
357                  sizeof (guint16) *  table->n_index_stride, 
358                  compare_seq_index);
359
360   if (!seq_index)
361     {
362       GTK_NOTE (MISC, g_print ("compact: no\n"));
363       return FALSE;
364     }
365
366   if (seq_index && n_compose == 1)
367     {
368       GTK_NOTE (MISC, g_print ("compact: yes\n"));
369       return TRUE;
370     }
371
372   GTK_NOTE (MISC, g_print ("compact: %d ", *seq_index));
373   seq = NULL;
374
375   for (i = n_compose-1; i < table->max_seq_len; i++)
376     {
377       row_stride = i + 1;
378
379       if (seq_index[i+1] - seq_index[i] > 0)
380         {
381           seq = bsearch (context_simple->compose_buffer + 1,
382                  table->data + seq_index[i], (seq_index[i+1] - seq_index[i]) / row_stride,
383                  sizeof (guint16) *  row_stride, 
384                  compare_seq);
385
386           if (seq)
387             {
388               if (i == n_compose - 1)
389                 break;
390               else
391                 {
392                   g_signal_emit_by_name (context_simple, "preedit-changed");
393
394                   GTK_NOTE (MISC, g_print ("yes\n"));
395                   return TRUE;
396                 }
397              }
398         }
399     }
400
401   if (!seq)
402     {
403       GTK_NOTE (MISC, g_print ("no\n"));
404       return FALSE;
405     }
406   else
407     {
408       gunichar value;
409
410       value = seq[row_stride - 1];
411
412       gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
413 #ifdef G_OS_WIN32
414       check_win32_special_case_after_compact_match (context_simple, n_compose, value);
415 #endif
416       context_simple->compose_buffer[0] = 0;
417
418       GTK_NOTE (MISC, g_print ("U+%04X\n", value));
419       return TRUE;
420     }
421
422   GTK_NOTE (MISC, g_print ("no\n"));
423   return FALSE;
424 }
425
426 /* This function receives a sequence of Unicode characters and tries to
427  * normalize it (NFC). We check for the case the the resulting string
428  * has length 1 (single character).
429  * NFC normalisation normally rearranges diacritic marks, unless these
430  * belong to the same Canonical Combining Class.
431  * If they belong to the same canonical combining class, we produce all
432  * permutations of the diacritic marks, then attempt to normalize.
433  */
434 static gboolean
435 check_normalize_nfc (gunichar* combination_buffer, gint n_compose)
436 {
437   gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
438   gchar *combination_utf8_temp = NULL;
439   gchar *nfc_temp = NULL;
440   gint n_combinations;
441   gunichar temp_swap;
442   gint i;
443
444   n_combinations = 1;
445
446   for (i = 1; i < n_compose; i++ )
447      n_combinations *= i;
448
449   /* Xorg reuses dead_tilde for the perispomeni diacritic mark.
450    * We check if base character belongs to Greek Unicode block,
451    * and if so, we replace tilde with perispomeni. */
452   if (combination_buffer[0] >= 0x390 && combination_buffer[0] <= 0x3FF)
453     {
454       for (i = 1; i < n_compose; i++ )
455         if (combination_buffer[i] == 0x303)
456           combination_buffer[i] = 0x342;
457     }
458
459   memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
460
461   for (i = 0; i < n_combinations; i++ )
462     {
463       g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
464       combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
465       nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);         
466
467       if (g_utf8_strlen (nfc_temp, -1) == 1)
468         {
469           memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
470
471           g_free (combination_utf8_temp);
472           g_free (nfc_temp);
473
474           return TRUE;
475         }
476
477       g_free (combination_utf8_temp);
478       g_free (nfc_temp);
479
480       if (n_compose > 2)
481         {
482           temp_swap = combination_buffer_temp[i % (n_compose - 1) + 1];
483           combination_buffer_temp[i % (n_compose - 1) + 1] = combination_buffer_temp[(i+1) % (n_compose - 1) + 1];
484           combination_buffer_temp[(i+1) % (n_compose - 1) + 1] = temp_swap;
485         }
486       else
487         break;
488     }
489
490   return FALSE;
491 }
492
493 static gboolean
494 check_algorithmically (GtkIMContextSimple    *context_simple,
495                        gint                   n_compose)
496
497 {
498   gint i;
499   gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
500   gchar *combination_utf8, *nfc;
501
502   if (n_compose >= GTK_MAX_COMPOSE_LEN)
503     return FALSE;
504
505   for (i = 0; i < n_compose && IS_DEAD_KEY (context_simple->compose_buffer[i]); i++)
506     ;
507   if (i == n_compose)
508     return TRUE;
509
510   if (i > 0 && i == n_compose - 1)
511     {
512       combination_buffer[0] = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
513       combination_buffer[n_compose] = 0;
514       i--;
515       while (i >= 0)
516         {
517           switch (context_simple->compose_buffer[i])
518             {
519 #define CASE(keysym, unicode) \
520             case GDK_dead_##keysym: combination_buffer[i+1] = unicode; break
521
522             CASE (grave, 0x0300);
523             CASE (acute, 0x0301);
524             CASE (circumflex, 0x0302);
525             CASE (tilde, 0x0303);       /* Also used with perispomeni, 0x342. */
526             CASE (macron, 0x0304);
527             CASE (breve, 0x0306);
528             CASE (abovedot, 0x0307);
529             CASE (diaeresis, 0x0308);
530             CASE (hook, 0x0309);
531             CASE (abovering, 0x030A);
532             CASE (doubleacute, 0x030B);
533             CASE (caron, 0x030C);
534             CASE (abovecomma, 0x0313);         /* Equivalent to psili */
535             CASE (abovereversedcomma, 0x0314); /* Equivalent to dasia */
536             CASE (horn, 0x031B);        /* Legacy use for psili, 0x313 (or 0x343). */
537             CASE (belowdot, 0x0323);
538             CASE (cedilla, 0x0327);
539             CASE (ogonek, 0x0328);      /* Legacy use for dasia, 0x314.*/
540             CASE (iota, 0x0345);
541             CASE (voiced_sound, 0x3099);        /* Per Markus Kuhn keysyms.txt file. */
542             CASE (semivoiced_sound, 0x309A);    /* Per Markus Kuhn keysyms.txt file. */
543
544             /* The following cases are to be removed once xkeyboard-config,
545              * xorg are fully updated.
546              */
547             /* Workaround for typo in 1.4.x xserver-xorg */
548             case 0xfe66: combination_buffer[i+1] = 0x314; break;
549             /* CASE (dasia, 0x314); */
550             /* CASE (perispomeni, 0x342); */
551             /* CASE (psili, 0x343); */
552 #undef CASE
553             default:
554               combination_buffer[i+1] = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
555             }
556           i--;
557         }
558       
559       /* If the buffer normalizes to a single character, 
560        * then modify the order of combination_buffer accordingly, if necessary,
561        * and return TRUE. 
562        */
563       if (check_normalize_nfc (combination_buffer, n_compose))
564         {
565           gunichar value;
566           combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
567           nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
568
569           value = g_utf8_get_char (nfc);
570           gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
571           context_simple->compose_buffer[0] = 0;
572
573           g_free (combination_utf8);
574           g_free (nfc);
575
576           return TRUE;
577         }
578     }
579
580   return FALSE;
581 }
582
583 /* In addition to the table-driven sequences, we allow Unicode hex
584  * codes to be entered. The method chosen here is similar to the
585  * one recommended in ISO 14755, but not exactly the same, since we
586  * don't want to steal 16 valuable key combinations. 
587  * 
588  * A hex Unicode sequence must be started with Ctrl-Shift-U, followed
589  * by a sequence of hex digits entered with Ctrl-Shift still held.
590  * Releasing one of the modifiers or pressing space while the modifiers
591  * are still held commits the character. It is possible to erase
592  * digits using backspace.
593  *
594  * As an extension to the above, we also allow to start the sequence
595  * with Ctrl-Shift-U, then release the modifiers before typing any
596  * digits, and enter the digits without modifiers.
597  */
598 #define HEX_MOD_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK)
599
600 static gboolean
601 check_hex (GtkIMContextSimple *context_simple,
602            gint                n_compose)
603 {
604   /* See if this is a hex sequence, return TRUE if so */
605   gint i;
606   GString *str;
607   gulong n;
608   gchar *nptr = NULL;
609   gchar buf[7];
610
611   context_simple->tentative_match = 0;
612   context_simple->tentative_match_len = 0;
613
614   str = g_string_new (NULL);
615   
616   i = 0;
617   while (i < n_compose)
618     {
619       gunichar ch;
620       
621       ch = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
622       
623       if (ch == 0)
624         return FALSE;
625
626       if (!g_unichar_isxdigit (ch))
627         return FALSE;
628
629       buf[g_unichar_to_utf8 (ch, buf)] = '\0';
630
631       g_string_append (str, buf);
632       
633       ++i;
634     }
635
636   n = strtoul (str->str, &nptr, 16);
637
638   /* if strtoul fails it probably means non-latin digits were used;
639    * we should in principle handle that, but we probably don't.
640    */
641   if (nptr - str->str < str->len)
642     {
643       g_string_free (str, TRUE);
644       return FALSE;
645     }
646   else
647     g_string_free (str, TRUE);
648
649   if (g_unichar_validate (n))
650     {
651       context_simple->tentative_match = n;
652       context_simple->tentative_match_len = n_compose;
653     }
654   
655   return TRUE;
656 }
657
658 static void
659 beep_window (GdkWindow *window)
660 {
661   GtkWidget *widget;
662
663   gdk_window_get_user_data (window, (gpointer) &widget);
664
665   if (GTK_IS_WIDGET (widget))
666     {
667       gtk_widget_error_bell (widget);
668     }
669   else
670     {
671       GdkScreen *screen = gdk_drawable_get_screen (GDK_DRAWABLE (window));
672       gboolean   beep;
673
674       g_object_get (gtk_settings_get_for_screen (screen),
675                     "gtk-error-bell", &beep,
676                     NULL);
677
678       if (beep)
679         gdk_window_beep (window);
680     }
681 }
682
683 static gboolean
684 no_sequence_matches (GtkIMContextSimple *context_simple,
685                      gint                n_compose,
686                      GdkEventKey        *event)
687 {
688   GtkIMContext *context;
689   gunichar ch;
690   
691   context = GTK_IM_CONTEXT (context_simple);
692   
693   /* No compose sequences found, check first if we have a partial
694    * match pending.
695    */
696   if (context_simple->tentative_match)
697     {
698       gint len = context_simple->tentative_match_len;
699       int i;
700       
701       gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
702       context_simple->compose_buffer[0] = 0;
703       
704       for (i=0; i < n_compose - len - 1; i++)
705         {
706           GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
707           tmp_event->key.keyval = context_simple->compose_buffer[len + i];
708           
709           gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
710           gdk_event_free (tmp_event);
711         }
712
713       return gtk_im_context_filter_keypress (context, event);
714     }
715   else
716     {
717       context_simple->compose_buffer[0] = 0;
718       if (n_compose > 1)                /* Invalid sequence */
719         {
720           beep_window (event->window);
721           return TRUE;
722         }
723   
724       ch = gdk_keyval_to_unicode (event->keyval);
725       if (ch != 0)
726         {
727           gtk_im_context_simple_commit_char (context, ch);
728           return TRUE;
729         }
730       else
731         return FALSE;
732     }
733 }
734
735 static gboolean
736 is_hex_keyval (guint keyval)
737 {
738   gunichar ch = gdk_keyval_to_unicode (keyval);
739
740   return g_unichar_isxdigit (ch);
741 }
742
743 static guint
744 canonical_hex_keyval (GdkEventKey *event)
745 {
746   GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_drawable_get_display (event->window));
747   guint keyval;
748   guint *keyvals = NULL;
749   gint n_vals = 0;
750   gint i;
751   
752   /* See if the keyval is already a hex digit */
753   if (is_hex_keyval (event->keyval))
754     return event->keyval;
755
756   /* See if this key would have generated a hex keyval in
757    * any other state, and return that hex keyval if so
758    */
759   gdk_keymap_get_entries_for_keycode (keymap,
760                                       event->hardware_keycode,
761                                       NULL,
762                                       &keyvals, &n_vals);
763
764   keyval = 0;
765   i = 0;
766   while (i < n_vals)
767     {
768       if (is_hex_keyval (keyvals[i]))
769         {
770           keyval = keyvals[i];
771           break;
772         }
773
774       ++i;
775     }
776
777   g_free (keyvals);
778   
779   if (keyval)
780     return keyval;
781   else
782     /* No way to make it a hex digit
783      */
784     return 0;
785 }
786
787 static gboolean
788 gtk_im_context_simple_filter_keypress (GtkIMContext *context,
789                                        GdkEventKey  *event)
790 {
791   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
792   GSList *tmp_list;  
793   int n_compose = 0;
794   gboolean have_hex_mods;
795   gboolean is_hex_start;
796   gboolean is_hex_end;
797   gboolean is_backspace;
798   gboolean is_escape;
799   guint hex_keyval;
800   int i;
801
802   while (context_simple->compose_buffer[n_compose] != 0)
803     n_compose++;
804
805   if (event->type == GDK_KEY_RELEASE)
806     {
807       if (context_simple->in_hex_sequence &&
808           (event->keyval == GDK_Control_L || event->keyval == GDK_Control_R ||
809            event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R))
810         {
811           if (context_simple->tentative_match &&
812               g_unichar_validate (context_simple->tentative_match))
813             {
814               gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
815               context_simple->compose_buffer[0] = 0;
816
817             }
818           else if (n_compose == 0)
819             {
820               context_simple->modifiers_dropped = TRUE;
821             }
822           else
823             {
824               /* invalid hex sequence */
825               beep_window (event->window);
826               
827               context_simple->tentative_match = 0;
828               context_simple->in_hex_sequence = FALSE;
829               context_simple->compose_buffer[0] = 0;
830               
831               g_signal_emit_by_name (context_simple, "preedit-changed");
832               g_signal_emit_by_name (context_simple, "preedit-end");
833             }
834
835           return TRUE;
836         }
837       else
838         return FALSE;
839     }
840
841   /* Ignore modifier key presses */
842   for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
843     if (event->keyval == gtk_compose_ignore[i])
844       return FALSE;
845
846   if (context_simple->in_hex_sequence && context_simple->modifiers_dropped)
847     have_hex_mods = TRUE;
848   else
849     have_hex_mods = (event->state & (HEX_MOD_MASK)) == HEX_MOD_MASK;
850   is_hex_start = event->keyval == GDK_U;
851   is_hex_end = (event->keyval == GDK_space || 
852                 event->keyval == GDK_KP_Space ||
853                 event->keyval == GDK_Return || 
854                 event->keyval == GDK_ISO_Enter ||
855                 event->keyval == GDK_KP_Enter);
856   is_backspace = event->keyval == GDK_BackSpace;
857   is_escape = event->keyval == GDK_Escape;
858   hex_keyval = canonical_hex_keyval (event);
859
860   /* If we are already in a non-hex sequence, or
861    * this keystroke is not hex modifiers + hex digit, don't filter 
862    * key events with accelerator modifiers held down.
863    */
864   if (!have_hex_mods ||
865       (n_compose > 0 && !context_simple->in_hex_sequence) || 
866       (n_compose == 0 && !context_simple->in_hex_sequence && !is_hex_start) ||
867       (context_simple->in_hex_sequence && !hex_keyval && 
868        !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
869     {
870       if (event->state & (gtk_accelerator_get_default_mod_mask () & ~GDK_SHIFT_MASK) ||
871           (context_simple->in_hex_sequence && context_simple->modifiers_dropped &&
872            (event->keyval == GDK_Return || 
873             event->keyval == GDK_ISO_Enter ||
874             event->keyval == GDK_KP_Enter)))
875         {
876           return FALSE;
877         }
878     }
879   
880   /* Handle backspace */
881   if (context_simple->in_hex_sequence && have_hex_mods && is_backspace)
882     {
883       if (n_compose > 0)
884         {
885           n_compose--;
886           context_simple->compose_buffer[n_compose] = 0;
887           check_hex (context_simple, n_compose);
888         }
889       else
890         {
891           context_simple->in_hex_sequence = FALSE;
892         }
893
894       g_signal_emit_by_name (context_simple, "preedit-changed");
895
896       if (!context_simple->in_hex_sequence)
897         g_signal_emit_by_name (context_simple, "preedit-end");
898       
899       return TRUE;
900     }
901
902   /* Check for hex sequence restart */
903   if (context_simple->in_hex_sequence && have_hex_mods && is_hex_start)
904     {
905       if (context_simple->tentative_match &&
906           g_unichar_validate (context_simple->tentative_match))
907         {
908           gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
909           context_simple->compose_buffer[0] = 0;
910         }
911       else 
912         {
913           /* invalid hex sequence */
914           if (n_compose > 0)
915             beep_window (event->window);
916           
917           context_simple->tentative_match = 0;
918           context_simple->in_hex_sequence = FALSE;
919           context_simple->compose_buffer[0] = 0;
920         }
921     }
922   
923   /* Check for hex sequence start */
924   if (!context_simple->in_hex_sequence && have_hex_mods && is_hex_start)
925     {
926       context_simple->compose_buffer[0] = 0;
927       context_simple->in_hex_sequence = TRUE;
928       context_simple->modifiers_dropped = FALSE;
929       context_simple->tentative_match = 0;
930
931       g_signal_emit_by_name (context_simple, "preedit-start");
932       g_signal_emit_by_name (context_simple, "preedit-changed");
933   
934       return TRUE;
935     }
936   
937   /* Then, check for compose sequences */
938   if (context_simple->in_hex_sequence)
939     {
940       if (hex_keyval)
941         context_simple->compose_buffer[n_compose++] = hex_keyval;
942       else if (is_escape)
943         {
944           gtk_im_context_simple_reset (context);
945           
946           return TRUE;
947         }
948       else if (!is_hex_end)
949         {
950           /* non-hex character in hex sequence */
951           beep_window (event->window);
952           
953           return TRUE;
954         }
955     }
956   else
957     context_simple->compose_buffer[n_compose++] = event->keyval;
958
959   context_simple->compose_buffer[n_compose] = 0;
960
961   if (context_simple->in_hex_sequence)
962     {
963       /* If the modifiers are still held down, consider the sequence again */
964       if (have_hex_mods)
965         {
966           /* space or return ends the sequence, and we eat the key */
967           if (n_compose > 0 && is_hex_end)
968             {
969               if (context_simple->tentative_match &&
970                   g_unichar_validate (context_simple->tentative_match))
971                 {
972                   gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
973                   context_simple->compose_buffer[0] = 0;
974                 }
975               else
976                 {
977                   /* invalid hex sequence */
978                   beep_window (event->window);
979
980                   context_simple->tentative_match = 0;
981                   context_simple->in_hex_sequence = FALSE;
982                   context_simple->compose_buffer[0] = 0;
983                 }
984             }
985           else if (!check_hex (context_simple, n_compose))
986             beep_window (event->window);
987           
988           g_signal_emit_by_name (context_simple, "preedit-changed");
989
990           if (!context_simple->in_hex_sequence)
991             g_signal_emit_by_name (context_simple, "preedit-end");
992
993           return TRUE;
994         }
995     }
996   else
997     {
998       tmp_list = context_simple->tables;
999       while (tmp_list)
1000         {
1001           if (check_table (context_simple, tmp_list->data, n_compose))
1002             return TRUE;
1003           tmp_list = tmp_list->next;
1004         }
1005
1006       GTK_NOTE (MISC, {
1007           g_print ("[ ");
1008           for (i = 0; i < n_compose; i++)
1009             {
1010               const gchar *keyval_name = gdk_keyval_name (context_simple->compose_buffer[i]);
1011               
1012               if (keyval_name != NULL)
1013                 g_print ("%s ", keyval_name);
1014               else
1015                 g_print ("%04x ", context_simple->compose_buffer[i]);
1016             }
1017           g_print ("] ");
1018         });
1019
1020 #ifdef GDK_WINDOWING_WIN32
1021       if (check_win32_special_cases (context_simple, n_compose))
1022         return TRUE;
1023 #endif
1024
1025       if (check_compact_table (context_simple, &gtk_compose_table_compact, n_compose))
1026         return TRUE;
1027   
1028       if (check_algorithmically (context_simple, n_compose))
1029         return TRUE;
1030     }
1031   
1032   /* The current compose_buffer doesn't match anything */
1033   return no_sequence_matches (context_simple, n_compose, event);
1034 }
1035
1036 static void
1037 gtk_im_context_simple_reset (GtkIMContext *context)
1038 {
1039   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1040
1041   context_simple->compose_buffer[0] = 0;
1042
1043   if (context_simple->tentative_match || context_simple->in_hex_sequence)
1044     {
1045       context_simple->in_hex_sequence = FALSE;
1046       context_simple->tentative_match = 0;
1047       context_simple->tentative_match_len = 0;
1048       g_signal_emit_by_name (context_simple, "preedit-changed");
1049       g_signal_emit_by_name (context_simple, "preedit-end");
1050     }
1051 }
1052
1053 static void     
1054 gtk_im_context_simple_get_preedit_string (GtkIMContext   *context,
1055                                           gchar         **str,
1056                                           PangoAttrList **attrs,
1057                                           gint           *cursor_pos)
1058 {
1059   char outbuf[37]; /* up to 6 hex digits */
1060   int len = 0;
1061   
1062   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1063
1064   if (context_simple->in_hex_sequence)
1065     {
1066       int hexchars = 0;
1067          
1068       outbuf[0] = 'u';
1069       len = 1;
1070
1071       while (context_simple->compose_buffer[hexchars] != 0)
1072         {
1073           len += g_unichar_to_utf8 (gdk_keyval_to_unicode (context_simple->compose_buffer[hexchars]),
1074                                     outbuf + len);
1075           ++hexchars;
1076         }
1077
1078       g_assert (len < 25);
1079     }
1080   else if (context_simple->tentative_match)
1081     len = g_unichar_to_utf8 (context_simple->tentative_match, outbuf);
1082       
1083   outbuf[len] = '\0';      
1084
1085   if (str)
1086     *str = g_strdup (outbuf);
1087
1088   if (attrs)
1089     {
1090       *attrs = pango_attr_list_new ();
1091       
1092       if (len)
1093         {
1094           PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
1095           attr->start_index = 0;
1096           attr->end_index = len;
1097           pango_attr_list_insert (*attrs, attr);
1098         }
1099     }
1100
1101   if (cursor_pos)
1102     *cursor_pos = len;
1103 }
1104
1105 /**
1106  * gtk_im_context_simple_add_table:
1107  * @context_simple: A #GtkIMContextSimple
1108  * @data: the table 
1109  * @max_seq_len: Maximum length of a sequence in the table
1110  *               (cannot be greater than #GTK_MAX_COMPOSE_LEN)
1111  * @n_seqs: number of sequences in the table
1112  * 
1113  * Adds an additional table to search to the input context.
1114  * Each row of the table consists of @max_seq_len key symbols
1115  * followed by two #guint16 interpreted as the high and low
1116  * words of a #gunicode value. Tables are searched starting
1117  * from the last added.
1118  *
1119  * The table must be sorted in dictionary order on the
1120  * numeric value of the key symbol fields. (Values beyond
1121  * the length of the sequence should be zero.)
1122  **/
1123 void
1124 gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
1125                                  guint16            *data,
1126                                  gint                max_seq_len,
1127                                  gint                n_seqs)
1128 {
1129   GtkComposeTable *table;
1130
1131   g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
1132   g_return_if_fail (data != NULL);
1133   g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
1134   
1135   table = g_new (GtkComposeTable, 1);
1136   table->data = data;
1137   table->max_seq_len = max_seq_len;
1138   table->n_seqs = n_seqs;
1139
1140   context_simple->tables = g_slist_prepend (context_simple->tables, table);
1141 }
1142
1143 #define __GTK_IM_CONTEXT_SIMPLE_C__
1144 #include "gtkaliasdef.c"