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.
24 #include <gdk/gdkkeysyms-compat.h>
25 #include "gtkprivate.h"
26 #include "gtkaccelgroup.h"
27 #include "gtkimcontextsimple.h"
28 #include "gtksettings.h"
29 #include "gtkwidget.h"
34 * SECTION:gtkimcontextsimple
35 * @Short_description: An input method context supporting table-based input methods
36 * @Title: GtkIMContextSimple
40 typedef struct _GtkComposeTable GtkComposeTable;
41 typedef struct _GtkComposeTableCompact GtkComposeTableCompact;
43 struct _GtkIMContextSimplePrivate
47 guint compose_buffer[GTK_MAX_COMPOSE_LEN + 1];
48 gunichar tentative_match;
49 gint tentative_match_len;
51 guint in_hex_sequence : 1;
52 guint modifiers_dropped : 1;
55 struct _GtkComposeTable
62 struct _GtkComposeTableCompact
70 /* This file contains the table of the compose sequences,
71 * static const guint16 gtk_compose_seqs_compact[] = {}
72 * IT is generated from the compose-parse.py script.
74 #include "gtkimcontextsimpleseqs.h"
76 /* From the values below, the value 23 means the number of different first keysyms
77 * that exist in the Compose file (from Xorg). When running compose-parse.py without
78 * parameters, you get the count that you can put here. Needed when updating the
79 * gtkimcontextsimpleseqs.h header file (contains the compose sequences).
81 static const GtkComposeTableCompact gtk_compose_table_compact = {
82 gtk_compose_seqs_compact,
88 static const guint16 gtk_compose_ignore[] = {
107 static void gtk_im_context_simple_finalize (GObject *obj);
108 static gboolean gtk_im_context_simple_filter_keypress (GtkIMContext *context,
110 static void gtk_im_context_simple_reset (GtkIMContext *context);
111 static void gtk_im_context_simple_get_preedit_string (GtkIMContext *context,
113 PangoAttrList **attrs,
116 G_DEFINE_TYPE (GtkIMContextSimple, gtk_im_context_simple, GTK_TYPE_IM_CONTEXT)
119 gtk_im_context_simple_class_init (GtkIMContextSimpleClass *class)
121 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
122 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
124 im_context_class->filter_keypress = gtk_im_context_simple_filter_keypress;
125 im_context_class->reset = gtk_im_context_simple_reset;
126 im_context_class->get_preedit_string = gtk_im_context_simple_get_preedit_string;
127 gobject_class->finalize = gtk_im_context_simple_finalize;
129 g_type_class_add_private (class, sizeof (GtkIMContextSimplePrivate));
133 gtk_im_context_simple_init (GtkIMContextSimple *im_context_simple)
135 im_context_simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (im_context_simple,
136 GTK_TYPE_IM_CONTEXT_SIMPLE,
137 GtkIMContextSimplePrivate);
141 gtk_im_context_simple_finalize (GObject *obj)
143 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (obj);
144 GtkIMContextSimplePrivate *priv = context_simple->priv;
148 g_slist_foreach (priv->tables, (GFunc)g_free, NULL);
149 g_slist_free (priv->tables);
154 G_OBJECT_CLASS (gtk_im_context_simple_parent_class)->finalize (obj);
158 * gtk_im_context_simple_new:
160 * Creates a new #GtkIMContextSimple.
162 * Returns: a new #GtkIMContextSimple.
165 gtk_im_context_simple_new (void)
167 return g_object_new (GTK_TYPE_IM_CONTEXT_SIMPLE, NULL);
171 gtk_im_context_simple_commit_char (GtkIMContext *context,
174 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
175 GtkIMContextSimplePrivate *priv = context_simple->priv;
179 g_return_if_fail (g_unichar_validate (ch));
181 len = g_unichar_to_utf8 (ch, buf);
184 if (priv->tentative_match || priv->in_hex_sequence)
186 priv->in_hex_sequence = FALSE;
187 priv->tentative_match = 0;
188 priv->tentative_match_len = 0;
189 g_signal_emit_by_name (context_simple, "preedit-changed");
190 g_signal_emit_by_name (context_simple, "preedit-end");
193 g_signal_emit_by_name (context, "commit", &buf);
197 compare_seq_index (const void *key, const void *value)
199 const guint *keysyms = key;
200 const guint16 *seq = value;
202 if (keysyms[0] < seq[0])
204 else if (keysyms[0] > seq[0])
211 compare_seq (const void *key, const void *value)
214 const guint *keysyms = key;
215 const guint16 *seq = value;
219 if (keysyms[i] < seq[i])
221 else if (keysyms[i] > seq[i])
231 check_table (GtkIMContextSimple *context_simple,
232 const GtkComposeTable *table,
235 GtkIMContextSimplePrivate *priv = context_simple->priv;
236 gint row_stride = table->max_seq_len + 2;
239 /* Will never match, if the sequence in the compose buffer is longer
240 * than the sequences in the table. Further, compare_seq (key, val)
241 * will overrun val if key is longer than val. */
242 if (n_compose > table->max_seq_len)
245 seq = bsearch (priv->compose_buffer,
246 table->data, table->n_seqs,
247 sizeof (guint16) * row_stride,
254 /* Back up to the first sequence that matches to make sure
255 * we find the exact match if their is one.
257 while (seq > table->data)
259 prev_seq = seq - row_stride;
260 if (compare_seq (priv->compose_buffer, prev_seq) != 0)
265 if (n_compose == table->max_seq_len ||
266 seq[n_compose] == 0) /* complete sequence */
270 0x10000 * seq[table->max_seq_len] + seq[table->max_seq_len + 1];
273 /* We found a tentative match. See if there are any longer
274 * sequences containing this subsequence
276 next_seq = seq + row_stride;
277 if (next_seq < table->data + row_stride * table->n_seqs)
279 if (compare_seq (priv->compose_buffer, next_seq) == 0)
281 priv->tentative_match = value;
282 priv->tentative_match_len = n_compose;
284 g_signal_emit_by_name (context_simple, "preedit-changed");
290 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
291 priv->compose_buffer[0] = 0;
300 /* Checks if a keysym is a dead key. Dead key keysym values are defined in
301 * ../gdk/gdkkeysyms.h and the first is GDK_dead_grave. As X.Org is updated,
302 * more dead keys are added and we need to update the upper limit.
303 * Currently, the upper limit is GDK_dead_dasia+1. The +1 has to do with
304 * a temporary issue in the X.Org header files.
305 * In future versions it will be just the keysym (no +1).
307 #define IS_DEAD_KEY(k) \
308 ((k) >= GDK_dead_grave && (k) <= (GDK_dead_dasia+1))
310 #ifdef GDK_WINDOWING_WIN32
312 /* On Windows, user expectation is that typing a dead accent followed
313 * by space will input the corresponding spacing character. The X
314 * compose tables are different for dead acute and diaeresis, which
315 * when followed by space produce a plain ASCII apostrophe and double
316 * quote respectively. So special-case those.
320 check_win32_special_cases (GtkIMContextSimple *context_simple,
323 GtkIMContextSimplePrivate *priv = context_simple->priv;
324 if (n_compose == 2 &&
325 priv->compose_buffer[1] == GDK_space)
329 switch (priv->compose_buffer[0])
332 value = 0x00B4; break;
333 case GDK_dead_diaeresis:
334 value = 0x00A8; break;
338 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
339 priv->compose_buffer[0] = 0;
341 GTK_NOTE (MISC, g_print ("win32: U+%04X\n", value));
349 check_win32_special_case_after_compact_match (GtkIMContextSimple *context_simple,
353 GtkIMContextSimplePrivate *priv = context_simple->priv;
355 /* On Windows user expectation is that typing two dead accents will input
356 * two corresponding spacing accents.
358 if (n_compose == 2 &&
359 priv->compose_buffer[0] == priv->compose_buffer[1] &&
360 IS_DEAD_KEY (priv->compose_buffer[0]))
362 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
363 GTK_NOTE (MISC, g_print ("win32: U+%04X ", value));
370 check_compact_table (GtkIMContextSimple *context_simple,
371 const GtkComposeTableCompact *table,
374 GtkIMContextSimplePrivate *priv = context_simple->priv;
380 /* Will never match, if the sequence in the compose buffer is longer
381 * than the sequences in the table. Further, compare_seq (key, val)
382 * will overrun val if key is longer than val. */
383 if (n_compose > table->max_seq_len)
386 seq_index = bsearch (priv->compose_buffer,
387 table->data, table->n_index_size,
388 sizeof (guint16) * table->n_index_stride,
393 GTK_NOTE (MISC, g_print ("compact: no\n"));
397 if (seq_index && n_compose == 1)
399 GTK_NOTE (MISC, g_print ("compact: yes\n"));
403 GTK_NOTE (MISC, g_print ("compact: %d ", *seq_index));
406 for (i = n_compose-1; i < table->max_seq_len; i++)
410 if (seq_index[i+1] - seq_index[i] > 0)
412 seq = bsearch (priv->compose_buffer + 1,
413 table->data + seq_index[i], (seq_index[i+1] - seq_index[i]) / row_stride,
414 sizeof (guint16) * row_stride,
419 if (i == n_compose - 1)
423 g_signal_emit_by_name (context_simple, "preedit-changed");
425 GTK_NOTE (MISC, g_print ("yes\n"));
434 GTK_NOTE (MISC, g_print ("no\n"));
441 value = seq[row_stride - 1];
443 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
445 check_win32_special_case_after_compact_match (context_simple, n_compose, value);
447 priv->compose_buffer[0] = 0;
449 GTK_NOTE (MISC, g_print ("U+%04X\n", value));
453 GTK_NOTE (MISC, g_print ("no\n"));
457 /* This function receives a sequence of Unicode characters and tries to
458 * normalize it (NFC). We check for the case the the resulting string
459 * has length 1 (single character).
460 * NFC normalisation normally rearranges diacritic marks, unless these
461 * belong to the same Canonical Combining Class.
462 * If they belong to the same canonical combining class, we produce all
463 * permutations of the diacritic marks, then attempt to normalize.
466 check_normalize_nfc (gunichar* combination_buffer, gint n_compose)
468 gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
469 gchar *combination_utf8_temp = NULL;
470 gchar *nfc_temp = NULL;
477 for (i = 1; i < n_compose; i++ )
480 /* Xorg reuses dead_tilde for the perispomeni diacritic mark.
481 * We check if base character belongs to Greek Unicode block,
482 * and if so, we replace tilde with perispomeni. */
483 if (combination_buffer[0] >= 0x390 && combination_buffer[0] <= 0x3FF)
485 for (i = 1; i < n_compose; i++ )
486 if (combination_buffer[i] == 0x303)
487 combination_buffer[i] = 0x342;
490 memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
492 for (i = 0; i < n_combinations; i++ )
494 g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
495 combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
496 nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);
498 if (g_utf8_strlen (nfc_temp, -1) == 1)
500 memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
502 g_free (combination_utf8_temp);
508 g_free (combination_utf8_temp);
513 temp_swap = combination_buffer_temp[i % (n_compose - 1) + 1];
514 combination_buffer_temp[i % (n_compose - 1) + 1] = combination_buffer_temp[(i+1) % (n_compose - 1) + 1];
515 combination_buffer_temp[(i+1) % (n_compose - 1) + 1] = temp_swap;
525 check_algorithmically (GtkIMContextSimple *context_simple,
529 GtkIMContextSimplePrivate *priv = context_simple->priv;
531 gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
532 gchar *combination_utf8, *nfc;
534 if (n_compose >= GTK_MAX_COMPOSE_LEN)
537 for (i = 0; i < n_compose && IS_DEAD_KEY (priv->compose_buffer[i]); i++)
542 if (i > 0 && i == n_compose - 1)
544 combination_buffer[0] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
545 combination_buffer[n_compose] = 0;
549 switch (priv->compose_buffer[i])
551 #define CASE(keysym, unicode) \
552 case GDK_dead_##keysym: combination_buffer[i+1] = unicode; break
554 CASE (grave, 0x0300);
555 CASE (acute, 0x0301);
556 CASE (circumflex, 0x0302);
557 CASE (tilde, 0x0303); /* Also used with perispomeni, 0x342. */
558 CASE (macron, 0x0304);
559 CASE (breve, 0x0306);
560 CASE (abovedot, 0x0307);
561 CASE (diaeresis, 0x0308);
563 CASE (abovering, 0x030A);
564 CASE (doubleacute, 0x030B);
565 CASE (caron, 0x030C);
566 CASE (abovecomma, 0x0313); /* Equivalent to psili */
567 CASE (abovereversedcomma, 0x0314); /* Equivalent to dasia */
568 CASE (horn, 0x031B); /* Legacy use for psili, 0x313 (or 0x343). */
569 CASE (belowdot, 0x0323);
570 CASE (cedilla, 0x0327);
571 CASE (ogonek, 0x0328); /* Legacy use for dasia, 0x314.*/
573 CASE (voiced_sound, 0x3099); /* Per Markus Kuhn keysyms.txt file. */
574 CASE (semivoiced_sound, 0x309A); /* Per Markus Kuhn keysyms.txt file. */
576 /* The following cases are to be removed once xkeyboard-config,
577 * xorg are fully updated.
579 /* Workaround for typo in 1.4.x xserver-xorg */
580 case 0xfe66: combination_buffer[i+1] = 0x314; break;
581 /* CASE (dasia, 0x314); */
582 /* CASE (perispomeni, 0x342); */
583 /* CASE (psili, 0x343); */
586 combination_buffer[i+1] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
591 /* If the buffer normalizes to a single character,
592 * then modify the order of combination_buffer accordingly, if necessary,
595 if (check_normalize_nfc (combination_buffer, n_compose))
598 combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
599 nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
601 value = g_utf8_get_char (nfc);
602 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
603 priv->compose_buffer[0] = 0;
605 g_free (combination_utf8);
615 /* In addition to the table-driven sequences, we allow Unicode hex
616 * codes to be entered. The method chosen here is similar to the
617 * one recommended in ISO 14755, but not exactly the same, since we
618 * don't want to steal 16 valuable key combinations.
620 * A hex Unicode sequence must be started with Ctrl-Shift-U, followed
621 * by a sequence of hex digits entered with Ctrl-Shift still held.
622 * Releasing one of the modifiers or pressing space while the modifiers
623 * are still held commits the character. It is possible to erase
624 * digits using backspace.
626 * As an extension to the above, we also allow to start the sequence
627 * with Ctrl-Shift-U, then release the modifiers before typing any
628 * digits, and enter the digits without modifiers.
630 #define HEX_MOD_MASK (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)
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)
696 gdk_window_get_user_data (window, (gpointer) &widget);
698 if (GTK_IS_WIDGET (widget))
700 gtk_widget_error_bell (widget);
704 GdkScreen *screen = gdk_window_get_screen (GDK_DRAWABLE (window));
707 g_object_get (gtk_settings_get_for_screen (screen),
708 "gtk-error-bell", &beep,
712 gdk_window_beep (window);
717 no_sequence_matches (GtkIMContextSimple *context_simple,
721 GtkIMContextSimplePrivate *priv = context_simple->priv;
722 GtkIMContext *context;
725 context = GTK_IM_CONTEXT (context_simple);
727 /* No compose sequences found, check first if we have a partial
730 if (priv->tentative_match)
732 gint len = priv->tentative_match_len;
735 gtk_im_context_simple_commit_char (context, priv->tentative_match);
736 priv->compose_buffer[0] = 0;
738 for (i=0; i < n_compose - len - 1; i++)
740 GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
741 tmp_event->key.keyval = priv->compose_buffer[len + i];
743 gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
744 gdk_event_free (tmp_event);
747 return gtk_im_context_filter_keypress (context, event);
751 priv->compose_buffer[0] = 0;
752 if (n_compose > 1) /* Invalid sequence */
754 beep_window (event->window);
758 ch = gdk_keyval_to_unicode (event->keyval);
761 gtk_im_context_simple_commit_char (context, ch);
770 is_hex_keyval (guint keyval)
772 gunichar ch = gdk_keyval_to_unicode (keyval);
774 return g_unichar_isxdigit (ch);
778 canonical_hex_keyval (GdkEventKey *event)
780 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_drawable_get_display (event->window));
782 guint *keyvals = NULL;
786 /* See if the keyval is already a hex digit */
787 if (is_hex_keyval (event->keyval))
788 return event->keyval;
790 /* See if this key would have generated a hex keyval in
791 * any other state, and return that hex keyval if so
793 gdk_keymap_get_entries_for_keycode (keymap,
794 event->hardware_keycode,
802 if (is_hex_keyval (keyvals[i]))
816 /* No way to make it a hex digit
822 gtk_im_context_simple_filter_keypress (GtkIMContext *context,
825 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
826 GtkIMContextSimplePrivate *priv = context_simple->priv;
829 gboolean have_hex_mods;
830 gboolean is_hex_start;
832 gboolean is_backspace;
837 while (priv->compose_buffer[n_compose] != 0)
840 if (event->type == GDK_KEY_RELEASE)
842 if (priv->in_hex_sequence &&
843 (event->keyval == GDK_Control_L || event->keyval == GDK_Control_R ||
844 event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R))
846 if (priv->tentative_match &&
847 g_unichar_validate (priv->tentative_match))
849 gtk_im_context_simple_commit_char (context, priv->tentative_match);
850 priv->compose_buffer[0] = 0;
853 else if (n_compose == 0)
855 priv->modifiers_dropped = TRUE;
859 /* invalid hex sequence */
860 beep_window (event->window);
862 priv->tentative_match = 0;
863 priv->in_hex_sequence = FALSE;
864 priv->compose_buffer[0] = 0;
866 g_signal_emit_by_name (context_simple, "preedit-changed");
867 g_signal_emit_by_name (context_simple, "preedit-end");
876 /* Ignore modifier key presses */
877 for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
878 if (event->keyval == gtk_compose_ignore[i])
881 if (priv->in_hex_sequence && priv->modifiers_dropped)
882 have_hex_mods = TRUE;
884 have_hex_mods = (event->state & (HEX_MOD_MASK)) == HEX_MOD_MASK;
885 is_hex_start = event->keyval == GDK_U;
886 is_hex_end = (event->keyval == GDK_space ||
887 event->keyval == GDK_KP_Space ||
888 event->keyval == GDK_Return ||
889 event->keyval == GDK_ISO_Enter ||
890 event->keyval == GDK_KP_Enter);
891 is_backspace = event->keyval == GDK_BackSpace;
892 is_escape = event->keyval == GDK_Escape;
893 hex_keyval = canonical_hex_keyval (event);
895 /* If we are already in a non-hex sequence, or
896 * this keystroke is not hex modifiers + hex digit, don't filter
897 * key events with accelerator modifiers held down. We only treat
898 * Control and Alt as accel modifiers here, since Super, Hyper and
899 * Meta are often co-located with Mode_Switch, Multi_Key or
902 if (!have_hex_mods ||
903 (n_compose > 0 && !priv->in_hex_sequence) ||
904 (n_compose == 0 && !priv->in_hex_sequence && !is_hex_start) ||
905 (priv->in_hex_sequence && !hex_keyval &&
906 !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
908 if (event->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK) ||
909 (priv->in_hex_sequence && priv->modifiers_dropped &&
910 (event->keyval == GDK_Return ||
911 event->keyval == GDK_ISO_Enter ||
912 event->keyval == GDK_KP_Enter)))
918 /* Handle backspace */
919 if (priv->in_hex_sequence && have_hex_mods && is_backspace)
924 priv->compose_buffer[n_compose] = 0;
925 check_hex (context_simple, n_compose);
929 priv->in_hex_sequence = FALSE;
932 g_signal_emit_by_name (context_simple, "preedit-changed");
934 if (!priv->in_hex_sequence)
935 g_signal_emit_by_name (context_simple, "preedit-end");
940 /* Check for hex sequence restart */
941 if (priv->in_hex_sequence && have_hex_mods && is_hex_start)
943 if (priv->tentative_match &&
944 g_unichar_validate (priv->tentative_match))
946 gtk_im_context_simple_commit_char (context, priv->tentative_match);
947 priv->compose_buffer[0] = 0;
951 /* invalid hex sequence */
953 beep_window (event->window);
955 priv->tentative_match = 0;
956 priv->in_hex_sequence = FALSE;
957 priv->compose_buffer[0] = 0;
961 /* Check for hex sequence start */
962 if (!priv->in_hex_sequence && have_hex_mods && is_hex_start)
964 priv->compose_buffer[0] = 0;
965 priv->in_hex_sequence = TRUE;
966 priv->modifiers_dropped = FALSE;
967 priv->tentative_match = 0;
969 g_signal_emit_by_name (context_simple, "preedit-start");
970 g_signal_emit_by_name (context_simple, "preedit-changed");
975 /* Then, check for compose sequences */
976 if (priv->in_hex_sequence)
979 priv->compose_buffer[n_compose++] = hex_keyval;
982 gtk_im_context_simple_reset (context);
986 else if (!is_hex_end)
988 /* non-hex character in hex sequence */
989 beep_window (event->window);
995 priv->compose_buffer[n_compose++] = event->keyval;
997 priv->compose_buffer[n_compose] = 0;
999 if (priv->in_hex_sequence)
1001 /* If the modifiers are still held down, consider the sequence again */
1004 /* space or return ends the sequence, and we eat the key */
1005 if (n_compose > 0 && is_hex_end)
1007 if (priv->tentative_match &&
1008 g_unichar_validate (priv->tentative_match))
1010 gtk_im_context_simple_commit_char (context, priv->tentative_match);
1011 priv->compose_buffer[0] = 0;
1015 /* invalid hex sequence */
1016 beep_window (event->window);
1018 priv->tentative_match = 0;
1019 priv->in_hex_sequence = FALSE;
1020 priv->compose_buffer[0] = 0;
1023 else if (!check_hex (context_simple, n_compose))
1024 beep_window (event->window);
1026 g_signal_emit_by_name (context_simple, "preedit-changed");
1028 if (!priv->in_hex_sequence)
1029 g_signal_emit_by_name (context_simple, "preedit-end");
1036 tmp_list = priv->tables;
1039 if (check_table (context_simple, tmp_list->data, n_compose))
1041 tmp_list = tmp_list->next;
1046 for (i = 0; i < n_compose; i++)
1048 const gchar *keyval_name = gdk_keyval_name (priv->compose_buffer[i]);
1050 if (keyval_name != NULL)
1051 g_print ("%s ", keyval_name);
1053 g_print ("%04x ", priv->compose_buffer[i]);
1058 #ifdef GDK_WINDOWING_WIN32
1059 if (check_win32_special_cases (context_simple, n_compose))
1063 if (check_compact_table (context_simple, >k_compose_table_compact, n_compose))
1066 if (check_algorithmically (context_simple, n_compose))
1070 /* The current compose_buffer doesn't match anything */
1071 return no_sequence_matches (context_simple, n_compose, event);
1075 gtk_im_context_simple_reset (GtkIMContext *context)
1077 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1078 GtkIMContextSimplePrivate *priv = context_simple->priv;
1080 priv->compose_buffer[0] = 0;
1082 if (priv->tentative_match || priv->in_hex_sequence)
1084 priv->in_hex_sequence = FALSE;
1085 priv->tentative_match = 0;
1086 priv->tentative_match_len = 0;
1087 g_signal_emit_by_name (context_simple, "preedit-changed");
1088 g_signal_emit_by_name (context_simple, "preedit-end");
1093 gtk_im_context_simple_get_preedit_string (GtkIMContext *context,
1095 PangoAttrList **attrs,
1098 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1099 GtkIMContextSimplePrivate *priv = context_simple->priv;
1100 char outbuf[37]; /* up to 6 hex digits */
1103 if (priv->in_hex_sequence)
1110 while (priv->compose_buffer[hexchars] != 0)
1112 len += g_unichar_to_utf8 (gdk_keyval_to_unicode (priv->compose_buffer[hexchars]),
1117 g_assert (len < 25);
1119 else if (priv->tentative_match)
1120 len = g_unichar_to_utf8 (priv->tentative_match, outbuf);
1125 *str = g_strdup (outbuf);
1129 *attrs = pango_attr_list_new ();
1133 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
1134 attr->start_index = 0;
1135 attr->end_index = len;
1136 pango_attr_list_insert (*attrs, attr);
1145 * gtk_im_context_simple_add_table:
1146 * @context_simple: A #GtkIMContextSimple
1148 * @max_seq_len: Maximum length of a sequence in the table
1149 * (cannot be greater than #GTK_MAX_COMPOSE_LEN)
1150 * @n_seqs: number of sequences in the table
1152 * Adds an additional table to search to the input context.
1153 * Each row of the table consists of @max_seq_len key symbols
1154 * followed by two #guint16 interpreted as the high and low
1155 * words of a #gunicode value. Tables are searched starting
1156 * from the last added.
1158 * The table must be sorted in dictionary order on the
1159 * numeric value of the key symbol fields. (Values beyond
1160 * the length of the sequence should be zero.)
1163 gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
1168 GtkIMContextSimplePrivate *priv = context_simple->priv;
1169 GtkComposeTable *table;
1171 g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
1172 g_return_if_fail (data != NULL);
1173 g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
1175 table = g_new (GtkComposeTable, 1);
1177 table->max_seq_len = max_seq_len;
1178 table->n_seqs = n_seqs;
1180 priv->tables = g_slist_prepend (priv->tables, table);