1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
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.
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.
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.
25 #include "gtkprivate.h"
26 #include "gtkaccelgroup.h"
27 #include "gtkimcontextsimple.h"
28 #include "gtksettings.h"
29 #include "gtkwidget.h"
35 * SECTION:gtkimcontextsimple
36 * @Short_description: An input method context supporting table-based input methods
37 * @Title: GtkIMContextSimple
41 typedef struct _GtkComposeTable GtkComposeTable;
42 typedef struct _GtkComposeTableCompact GtkComposeTableCompact;
44 struct _GtkIMContextSimplePrivate
48 guint compose_buffer[GTK_MAX_COMPOSE_LEN + 1];
49 gunichar tentative_match;
50 gint tentative_match_len;
52 guint in_hex_sequence : 1;
53 guint modifiers_dropped : 1;
56 struct _GtkComposeTable
63 struct _GtkComposeTableCompact
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.
75 #include "gtkimcontextsimpleseqs.h"
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).
82 static const GtkComposeTableCompact gtk_compose_table_compact = {
83 gtk_compose_seqs_compact,
89 static const guint16 gtk_compose_ignore[] = {
105 GDK_KEY_ISO_Level3_Shift
108 static void gtk_im_context_simple_finalize (GObject *obj);
109 static gboolean gtk_im_context_simple_filter_keypress (GtkIMContext *context,
111 static void gtk_im_context_simple_reset (GtkIMContext *context);
112 static void gtk_im_context_simple_get_preedit_string (GtkIMContext *context,
114 PangoAttrList **attrs,
117 G_DEFINE_TYPE (GtkIMContextSimple, gtk_im_context_simple, GTK_TYPE_IM_CONTEXT)
120 gtk_im_context_simple_class_init (GtkIMContextSimpleClass *class)
122 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
123 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
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;
130 g_type_class_add_private (class, sizeof (GtkIMContextSimplePrivate));
134 gtk_im_context_simple_init (GtkIMContextSimple *im_context_simple)
136 im_context_simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (im_context_simple,
137 GTK_TYPE_IM_CONTEXT_SIMPLE,
138 GtkIMContextSimplePrivate);
142 gtk_im_context_simple_finalize (GObject *obj)
144 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (obj);
145 GtkIMContextSimplePrivate *priv = context_simple->priv;
149 g_slist_foreach (priv->tables, (GFunc)g_free, NULL);
150 g_slist_free (priv->tables);
155 G_OBJECT_CLASS (gtk_im_context_simple_parent_class)->finalize (obj);
159 * gtk_im_context_simple_new:
161 * Creates a new #GtkIMContextSimple.
163 * Returns: a new #GtkIMContextSimple.
166 gtk_im_context_simple_new (void)
168 return g_object_new (GTK_TYPE_IM_CONTEXT_SIMPLE, NULL);
172 gtk_im_context_simple_commit_char (GtkIMContext *context,
175 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
176 GtkIMContextSimplePrivate *priv = context_simple->priv;
180 g_return_if_fail (g_unichar_validate (ch));
182 len = g_unichar_to_utf8 (ch, buf);
185 if (priv->tentative_match || priv->in_hex_sequence)
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");
194 g_signal_emit_by_name (context, "commit", &buf);
198 compare_seq_index (const void *key, const void *value)
200 const guint *keysyms = key;
201 const guint16 *seq = value;
203 if (keysyms[0] < seq[0])
205 else if (keysyms[0] > seq[0])
212 compare_seq (const void *key, const void *value)
215 const guint *keysyms = key;
216 const guint16 *seq = value;
220 if (keysyms[i] < seq[i])
222 else if (keysyms[i] > seq[i])
232 check_table (GtkIMContextSimple *context_simple,
233 const GtkComposeTable *table,
236 GtkIMContextSimplePrivate *priv = context_simple->priv;
237 gint row_stride = table->max_seq_len + 2;
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)
246 seq = bsearch (priv->compose_buffer,
247 table->data, table->n_seqs,
248 sizeof (guint16) * row_stride,
255 /* Back up to the first sequence that matches to make sure
256 * we find the exact match if their is one.
258 while (seq > table->data)
260 prev_seq = seq - row_stride;
261 if (compare_seq (priv->compose_buffer, prev_seq) != 0)
266 if (n_compose == table->max_seq_len ||
267 seq[n_compose] == 0) /* complete sequence */
271 0x10000 * seq[table->max_seq_len] + seq[table->max_seq_len + 1];
274 /* We found a tentative match. See if there are any longer
275 * sequences containing this subsequence
277 next_seq = seq + row_stride;
278 if (next_seq < table->data + row_stride * table->n_seqs)
280 if (compare_seq (priv->compose_buffer, next_seq) == 0)
282 priv->tentative_match = value;
283 priv->tentative_match_len = n_compose;
285 g_signal_emit_by_name (context_simple, "preedit-changed");
291 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
292 priv->compose_buffer[0] = 0;
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_KEY_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_KEY_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).
308 #define IS_DEAD_KEY(k) \
309 ((k) >= GDK_KEY_dead_grave && (k) <= (GDK_KEY_dead_dasia+1))
311 #ifdef GDK_WINDOWING_WIN32
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.
321 check_win32_special_cases (GtkIMContextSimple *context_simple,
324 GtkIMContextSimplePrivate *priv = context_simple->priv;
325 if (n_compose == 2 &&
326 priv->compose_buffer[1] == GDK_KEY_space)
330 switch (priv->compose_buffer[0])
332 case GDK_KEY_dead_acute:
333 value = 0x00B4; break;
334 case GDK_KEY_dead_diaeresis:
335 value = 0x00A8; break;
339 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
340 priv->compose_buffer[0] = 0;
342 GTK_NOTE (MISC, g_print ("win32: U+%04X\n", value));
350 check_win32_special_case_after_compact_match (GtkIMContextSimple *context_simple,
354 GtkIMContextSimplePrivate *priv = context_simple->priv;
356 /* On Windows user expectation is that typing two dead accents will input
357 * two corresponding spacing accents.
359 if (n_compose == 2 &&
360 priv->compose_buffer[0] == priv->compose_buffer[1] &&
361 IS_DEAD_KEY (priv->compose_buffer[0]))
363 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
364 GTK_NOTE (MISC, g_print ("win32: U+%04X ", value));
371 check_compact_table (GtkIMContextSimple *context_simple,
372 const GtkComposeTableCompact *table,
375 GtkIMContextSimplePrivate *priv = context_simple->priv;
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)
387 seq_index = bsearch (priv->compose_buffer,
388 table->data, table->n_index_size,
389 sizeof (guint16) * table->n_index_stride,
394 GTK_NOTE (MISC, g_print ("compact: no\n"));
398 if (seq_index && n_compose == 1)
400 GTK_NOTE (MISC, g_print ("compact: yes\n"));
404 GTK_NOTE (MISC, g_print ("compact: %d ", *seq_index));
407 for (i = n_compose-1; i < table->max_seq_len; i++)
411 if (seq_index[i+1] - seq_index[i] > 0)
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,
420 if (i == n_compose - 1)
424 g_signal_emit_by_name (context_simple, "preedit-changed");
426 GTK_NOTE (MISC, g_print ("yes\n"));
435 GTK_NOTE (MISC, g_print ("no\n"));
442 value = seq[row_stride - 1];
444 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
446 check_win32_special_case_after_compact_match (context_simple, n_compose, value);
448 priv->compose_buffer[0] = 0;
450 GTK_NOTE (MISC, g_print ("U+%04X\n", value));
454 GTK_NOTE (MISC, g_print ("no\n"));
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.
467 check_normalize_nfc (gunichar* combination_buffer, gint n_compose)
469 gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
470 gchar *combination_utf8_temp = NULL;
471 gchar *nfc_temp = NULL;
478 for (i = 1; i < n_compose; i++ )
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)
486 for (i = 1; i < n_compose; i++ )
487 if (combination_buffer[i] == 0x303)
488 combination_buffer[i] = 0x342;
491 memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
493 for (i = 0; i < n_combinations; i++ )
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);
499 if (g_utf8_strlen (nfc_temp, -1) == 1)
501 memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
503 g_free (combination_utf8_temp);
509 g_free (combination_utf8_temp);
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;
526 check_algorithmically (GtkIMContextSimple *context_simple,
530 GtkIMContextSimplePrivate *priv = context_simple->priv;
532 gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
533 gchar *combination_utf8, *nfc;
535 if (n_compose >= GTK_MAX_COMPOSE_LEN)
538 for (i = 0; i < n_compose && IS_DEAD_KEY (priv->compose_buffer[i]); i++)
543 if (i > 0 && i == n_compose - 1)
545 combination_buffer[0] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
546 combination_buffer[n_compose] = 0;
550 switch (priv->compose_buffer[i])
552 #define CASE(keysym, unicode) \
553 case GDK_KEY_dead_##keysym: combination_buffer[i+1] = unicode; break
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);
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.*/
574 CASE (voiced_sound, 0x3099); /* Per Markus Kuhn keysyms.txt file. */
575 CASE (semivoiced_sound, 0x309A); /* Per Markus Kuhn keysyms.txt file. */
577 /* The following cases are to be removed once xkeyboard-config,
578 * xorg are fully updated.
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); */
587 combination_buffer[i+1] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
592 /* If the buffer normalizes to a single character,
593 * then modify the order of combination_buffer accordingly, if necessary,
596 if (check_normalize_nfc (combination_buffer, n_compose))
599 combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
600 nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
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;
606 g_free (combination_utf8);
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.
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.
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.
633 check_hex (GtkIMContextSimple *context_simple,
636 GtkIMContextSimplePrivate *priv = context_simple->priv;
637 /* See if this is a hex sequence, return TRUE if so */
644 priv->tentative_match = 0;
645 priv->tentative_match_len = 0;
647 str = g_string_new (NULL);
650 while (i < n_compose)
654 ch = gdk_keyval_to_unicode (priv->compose_buffer[i]);
659 if (!g_unichar_isxdigit (ch))
662 buf[g_unichar_to_utf8 (ch, buf)] = '\0';
664 g_string_append (str, buf);
669 n = strtoul (str->str, &nptr, 16);
671 /* if strtoul fails it probably means non-latin digits were used;
672 * we should in principle handle that, but we probably don't.
674 if (nptr - str->str < str->len)
676 g_string_free (str, TRUE);
680 g_string_free (str, TRUE);
682 if (g_unichar_validate (n))
684 priv->tentative_match = n;
685 priv->tentative_match_len = n_compose;
692 beep_window (GdkWindow *window)
694 GdkScreen *screen = gdk_window_get_screen (window);
697 g_object_get (gtk_settings_get_for_screen (screen),
698 "gtk-error-bell", &beep,
702 gdk_window_beep (window);
706 no_sequence_matches (GtkIMContextSimple *context_simple,
710 GtkIMContextSimplePrivate *priv = context_simple->priv;
711 GtkIMContext *context;
714 context = GTK_IM_CONTEXT (context_simple);
716 /* No compose sequences found, check first if we have a partial
719 if (priv->tentative_match)
721 gint len = priv->tentative_match_len;
724 gtk_im_context_simple_commit_char (context, priv->tentative_match);
725 priv->compose_buffer[0] = 0;
727 for (i=0; i < n_compose - len - 1; i++)
729 GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
730 tmp_event->key.keyval = priv->compose_buffer[len + i];
732 gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
733 gdk_event_free (tmp_event);
736 return gtk_im_context_filter_keypress (context, event);
740 priv->compose_buffer[0] = 0;
741 if (n_compose > 1) /* Invalid sequence */
743 beep_window (event->window);
747 ch = gdk_keyval_to_unicode (event->keyval);
748 if (ch != 0 && !g_unichar_iscntrl (ch))
750 gtk_im_context_simple_commit_char (context, ch);
759 is_hex_keyval (guint keyval)
761 gunichar ch = gdk_keyval_to_unicode (keyval);
763 return g_unichar_isxdigit (ch);
767 canonical_hex_keyval (GdkEventKey *event)
769 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_window_get_display (event->window));
771 guint *keyvals = NULL;
775 /* See if the keyval is already a hex digit */
776 if (is_hex_keyval (event->keyval))
777 return event->keyval;
779 /* See if this key would have generated a hex keyval in
780 * any other state, and return that hex keyval if so
782 gdk_keymap_get_entries_for_keycode (keymap,
783 event->hardware_keycode,
791 if (is_hex_keyval (keyvals[i]))
805 /* No way to make it a hex digit
811 gtk_im_context_simple_filter_keypress (GtkIMContext *context,
814 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
815 GtkIMContextSimplePrivate *priv = context_simple->priv;
816 GdkDisplay *display = gdk_window_get_display (event->window);
819 GdkModifierType hex_mod_mask;
820 gboolean have_hex_mods;
821 gboolean is_hex_start;
823 gboolean is_backspace;
828 while (priv->compose_buffer[n_compose] != 0)
831 if (event->type == GDK_KEY_RELEASE)
833 if (priv->in_hex_sequence &&
834 (event->keyval == GDK_KEY_Control_L || event->keyval == GDK_KEY_Control_R ||
835 event->keyval == GDK_KEY_Shift_L || event->keyval == GDK_KEY_Shift_R))
837 if (priv->tentative_match &&
838 g_unichar_validate (priv->tentative_match))
840 gtk_im_context_simple_commit_char (context, priv->tentative_match);
841 priv->compose_buffer[0] = 0;
844 else if (n_compose == 0)
846 priv->modifiers_dropped = TRUE;
850 /* invalid hex sequence */
851 beep_window (event->window);
853 priv->tentative_match = 0;
854 priv->in_hex_sequence = FALSE;
855 priv->compose_buffer[0] = 0;
857 g_signal_emit_by_name (context_simple, "preedit-changed");
858 g_signal_emit_by_name (context_simple, "preedit-end");
867 /* Ignore modifier key presses */
868 for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
869 if (event->keyval == gtk_compose_ignore[i])
872 hex_mod_mask = gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
873 GDK_MODIFIER_INTENT_PRIMARY_ACCELERATOR);
874 hex_mod_mask |= GDK_SHIFT_MASK;
876 if (priv->in_hex_sequence && priv->modifiers_dropped)
877 have_hex_mods = TRUE;
879 have_hex_mods = (event->state & (hex_mod_mask)) == hex_mod_mask;
880 is_hex_start = event->keyval == GDK_KEY_U;
881 is_hex_end = (event->keyval == GDK_KEY_space ||
882 event->keyval == GDK_KEY_KP_Space ||
883 event->keyval == GDK_KEY_Return ||
884 event->keyval == GDK_KEY_ISO_Enter ||
885 event->keyval == GDK_KEY_KP_Enter);
886 is_backspace = event->keyval == GDK_KEY_BackSpace;
887 is_escape = event->keyval == GDK_KEY_Escape;
888 hex_keyval = canonical_hex_keyval (event);
890 /* If we are already in a non-hex sequence, or
891 * this keystroke is not hex modifiers + hex digit, don't filter
892 * key events with accelerator modifiers held down. We only treat
893 * Control and Alt as accel modifiers here, since Super, Hyper and
894 * Meta are often co-located with Mode_Switch, Multi_Key or
897 if (!have_hex_mods ||
898 (n_compose > 0 && !priv->in_hex_sequence) ||
899 (n_compose == 0 && !priv->in_hex_sequence && !is_hex_start) ||
900 (priv->in_hex_sequence && !hex_keyval &&
901 !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
904 GdkModifierType no_text_input_mask;
906 display = gdk_window_get_display (event->window);
909 gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
910 GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
912 if (event->state & no_text_input_mask ||
913 (priv->in_hex_sequence && priv->modifiers_dropped &&
914 (event->keyval == GDK_KEY_Return ||
915 event->keyval == GDK_KEY_ISO_Enter ||
916 event->keyval == GDK_KEY_KP_Enter)))
922 /* Handle backspace */
923 if (priv->in_hex_sequence && have_hex_mods && is_backspace)
928 priv->compose_buffer[n_compose] = 0;
929 check_hex (context_simple, n_compose);
933 priv->in_hex_sequence = FALSE;
936 g_signal_emit_by_name (context_simple, "preedit-changed");
938 if (!priv->in_hex_sequence)
939 g_signal_emit_by_name (context_simple, "preedit-end");
944 /* Check for hex sequence restart */
945 if (priv->in_hex_sequence && have_hex_mods && is_hex_start)
947 if (priv->tentative_match &&
948 g_unichar_validate (priv->tentative_match))
950 gtk_im_context_simple_commit_char (context, priv->tentative_match);
951 priv->compose_buffer[0] = 0;
955 /* invalid hex sequence */
957 beep_window (event->window);
959 priv->tentative_match = 0;
960 priv->in_hex_sequence = FALSE;
961 priv->compose_buffer[0] = 0;
965 /* Check for hex sequence start */
966 if (!priv->in_hex_sequence && have_hex_mods && is_hex_start)
968 priv->compose_buffer[0] = 0;
969 priv->in_hex_sequence = TRUE;
970 priv->modifiers_dropped = FALSE;
971 priv->tentative_match = 0;
973 g_signal_emit_by_name (context_simple, "preedit-start");
974 g_signal_emit_by_name (context_simple, "preedit-changed");
979 /* Then, check for compose sequences */
980 if (priv->in_hex_sequence)
983 priv->compose_buffer[n_compose++] = hex_keyval;
986 gtk_im_context_simple_reset (context);
990 else if (!is_hex_end)
992 /* non-hex character in hex sequence */
993 beep_window (event->window);
999 priv->compose_buffer[n_compose++] = event->keyval;
1001 priv->compose_buffer[n_compose] = 0;
1003 if (priv->in_hex_sequence)
1005 /* If the modifiers are still held down, consider the sequence again */
1008 /* space or return ends the sequence, and we eat the key */
1009 if (n_compose > 0 && is_hex_end)
1011 if (priv->tentative_match &&
1012 g_unichar_validate (priv->tentative_match))
1014 gtk_im_context_simple_commit_char (context, priv->tentative_match);
1015 priv->compose_buffer[0] = 0;
1019 /* invalid hex sequence */
1020 beep_window (event->window);
1022 priv->tentative_match = 0;
1023 priv->in_hex_sequence = FALSE;
1024 priv->compose_buffer[0] = 0;
1027 else if (!check_hex (context_simple, n_compose))
1028 beep_window (event->window);
1030 g_signal_emit_by_name (context_simple, "preedit-changed");
1032 if (!priv->in_hex_sequence)
1033 g_signal_emit_by_name (context_simple, "preedit-end");
1040 tmp_list = priv->tables;
1043 if (check_table (context_simple, tmp_list->data, n_compose))
1045 tmp_list = tmp_list->next;
1050 for (i = 0; i < n_compose; i++)
1052 const gchar *keyval_name = gdk_keyval_name (priv->compose_buffer[i]);
1054 if (keyval_name != NULL)
1055 g_print ("%s ", keyval_name);
1057 g_print ("%04x ", priv->compose_buffer[i]);
1062 #ifdef GDK_WINDOWING_WIN32
1063 if (check_win32_special_cases (context_simple, n_compose))
1067 if (check_compact_table (context_simple, >k_compose_table_compact, n_compose))
1070 if (check_algorithmically (context_simple, n_compose))
1074 /* The current compose_buffer doesn't match anything */
1075 return no_sequence_matches (context_simple, n_compose, event);
1079 gtk_im_context_simple_reset (GtkIMContext *context)
1081 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1082 GtkIMContextSimplePrivate *priv = context_simple->priv;
1084 priv->compose_buffer[0] = 0;
1086 if (priv->tentative_match || priv->in_hex_sequence)
1088 priv->in_hex_sequence = FALSE;
1089 priv->tentative_match = 0;
1090 priv->tentative_match_len = 0;
1091 g_signal_emit_by_name (context_simple, "preedit-changed");
1092 g_signal_emit_by_name (context_simple, "preedit-end");
1097 gtk_im_context_simple_get_preedit_string (GtkIMContext *context,
1099 PangoAttrList **attrs,
1102 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1103 GtkIMContextSimplePrivate *priv = context_simple->priv;
1104 char outbuf[37]; /* up to 6 hex digits */
1107 if (priv->in_hex_sequence)
1114 while (priv->compose_buffer[hexchars] != 0)
1116 len += g_unichar_to_utf8 (gdk_keyval_to_unicode (priv->compose_buffer[hexchars]),
1121 g_assert (len < 25);
1123 else if (priv->tentative_match)
1124 len = g_unichar_to_utf8 (priv->tentative_match, outbuf);
1129 *str = g_strdup (outbuf);
1133 *attrs = pango_attr_list_new ();
1137 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
1138 attr->start_index = 0;
1139 attr->end_index = len;
1140 pango_attr_list_insert (*attrs, attr);
1149 * gtk_im_context_simple_add_table: (skip)
1150 * @context_simple: A #GtkIMContextSimple
1152 * @max_seq_len: Maximum length of a sequence in the table
1153 * (cannot be greater than #GTK_MAX_COMPOSE_LEN)
1154 * @n_seqs: number of sequences in the table
1156 * Adds an additional table to search to the input context.
1157 * Each row of the table consists of @max_seq_len key symbols
1158 * followed by two #guint16 interpreted as the high and low
1159 * words of a #gunicode value. Tables are searched starting
1160 * from the last added.
1162 * The table must be sorted in dictionary order on the
1163 * numeric value of the key symbol fields. (Values beyond
1164 * the length of the sequence should be zero.)
1167 gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
1172 GtkIMContextSimplePrivate *priv = context_simple->priv;
1173 GtkComposeTable *table;
1175 g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
1176 g_return_if_fail (data != NULL);
1177 g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
1179 table = g_new (GtkComposeTable, 1);
1181 table->max_seq_len = max_seq_len;
1182 table->n_seqs = n_seqs;
1184 priv->tables = g_slist_prepend (priv->tables, table);