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, see <http://www.gnu.org/licenses/>.
23 #include "gtkprivate.h"
24 #include "gtkaccelgroup.h"
25 #include "gtkimcontextsimple.h"
26 #include "gtksettings.h"
27 #include "gtkwidget.h"
33 * SECTION:gtkimcontextsimple
34 * @Short_description: An input method context supporting table-based input methods
35 * @Title: GtkIMContextSimple
39 typedef struct _GtkComposeTable GtkComposeTable;
40 typedef struct _GtkComposeTableCompact GtkComposeTableCompact;
42 struct _GtkIMContextSimplePrivate
46 guint compose_buffer[GTK_MAX_COMPOSE_LEN + 1];
47 gunichar tentative_match;
48 gint tentative_match_len;
50 guint in_hex_sequence : 1;
51 guint modifiers_dropped : 1;
54 struct _GtkComposeTable
61 struct _GtkComposeTableCompact
69 /* This file contains the table of the compose sequences,
70 * static const guint16 gtk_compose_seqs_compact[] = {}
71 * It is generated from the compose-parse.py script.
73 #include "gtkimcontextsimpleseqs.h"
75 /* From the values below, the value 24 means the number of different first keysyms
76 * that exist in the Compose file (from Xorg). When running compose-parse.py without
77 * parameters, you get the count that you can put here. Needed when updating the
78 * gtkimcontextsimpleseqs.h header file (contains the compose sequences).
80 static const GtkComposeTableCompact gtk_compose_table_compact = {
81 gtk_compose_seqs_compact,
87 static const guint16 gtk_compose_ignore[] = {
103 GDK_KEY_ISO_Level3_Shift
106 static void gtk_im_context_simple_finalize (GObject *obj);
107 static gboolean gtk_im_context_simple_filter_keypress (GtkIMContext *context,
109 static void gtk_im_context_simple_reset (GtkIMContext *context);
110 static void gtk_im_context_simple_get_preedit_string (GtkIMContext *context,
112 PangoAttrList **attrs,
115 G_DEFINE_TYPE (GtkIMContextSimple, gtk_im_context_simple, GTK_TYPE_IM_CONTEXT)
118 gtk_im_context_simple_class_init (GtkIMContextSimpleClass *class)
120 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
121 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
123 im_context_class->filter_keypress = gtk_im_context_simple_filter_keypress;
124 im_context_class->reset = gtk_im_context_simple_reset;
125 im_context_class->get_preedit_string = gtk_im_context_simple_get_preedit_string;
126 gobject_class->finalize = gtk_im_context_simple_finalize;
128 g_type_class_add_private (class, sizeof (GtkIMContextSimplePrivate));
132 gtk_im_context_simple_init (GtkIMContextSimple *im_context_simple)
134 im_context_simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (im_context_simple,
135 GTK_TYPE_IM_CONTEXT_SIMPLE,
136 GtkIMContextSimplePrivate);
140 gtk_im_context_simple_finalize (GObject *obj)
142 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (obj);
143 GtkIMContextSimplePrivate *priv = context_simple->priv;
147 g_slist_foreach (priv->tables, (GFunc)g_free, NULL);
148 g_slist_free (priv->tables);
153 G_OBJECT_CLASS (gtk_im_context_simple_parent_class)->finalize (obj);
157 * gtk_im_context_simple_new:
159 * Creates a new #GtkIMContextSimple.
161 * Returns: a new #GtkIMContextSimple.
164 gtk_im_context_simple_new (void)
166 return g_object_new (GTK_TYPE_IM_CONTEXT_SIMPLE, NULL);
170 gtk_im_context_simple_commit_char (GtkIMContext *context,
173 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
174 GtkIMContextSimplePrivate *priv = context_simple->priv;
178 g_return_if_fail (g_unichar_validate (ch));
180 len = g_unichar_to_utf8 (ch, buf);
183 if (priv->tentative_match || priv->in_hex_sequence)
185 priv->in_hex_sequence = FALSE;
186 priv->tentative_match = 0;
187 priv->tentative_match_len = 0;
188 g_signal_emit_by_name (context_simple, "preedit-changed");
189 g_signal_emit_by_name (context_simple, "preedit-end");
192 g_signal_emit_by_name (context, "commit", &buf);
196 compare_seq_index (const void *key, const void *value)
198 const guint *keysyms = key;
199 const guint16 *seq = value;
201 if (keysyms[0] < seq[0])
203 else if (keysyms[0] > seq[0])
210 compare_seq (const void *key, const void *value)
213 const guint *keysyms = key;
214 const guint16 *seq = value;
218 if (keysyms[i] < seq[i])
220 else if (keysyms[i] > seq[i])
230 check_table (GtkIMContextSimple *context_simple,
231 const GtkComposeTable *table,
234 GtkIMContextSimplePrivate *priv = context_simple->priv;
235 gint row_stride = table->max_seq_len + 2;
238 /* Will never match, if the sequence in the compose buffer is longer
239 * than the sequences in the table. Further, compare_seq (key, val)
240 * will overrun val if key is longer than val. */
241 if (n_compose > table->max_seq_len)
244 seq = bsearch (priv->compose_buffer,
245 table->data, table->n_seqs,
246 sizeof (guint16) * row_stride,
253 /* Back up to the first sequence that matches to make sure
254 * we find the exact match if there is one.
256 while (seq > table->data)
258 prev_seq = seq - row_stride;
259 if (compare_seq (priv->compose_buffer, prev_seq) != 0)
264 if (n_compose == table->max_seq_len ||
265 seq[n_compose] == 0) /* complete sequence */
269 0x10000 * seq[table->max_seq_len] + seq[table->max_seq_len + 1];
271 /* We found a tentative match. See if there are any longer
272 * sequences containing this subsequence
274 next_seq = seq + row_stride;
275 if (next_seq < table->data + row_stride * table->n_seqs)
277 if (compare_seq (priv->compose_buffer, next_seq) == 0)
279 priv->tentative_match = value;
280 priv->tentative_match_len = n_compose;
282 g_signal_emit_by_name (context_simple, "preedit-changed");
288 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
289 priv->compose_buffer[0] = 0;
298 /* Checks if a keysym is a dead key. Dead key keysym values are defined in
299 * ../gdk/gdkkeysyms.h and the first is GDK_KEY_dead_grave. As X.Org is updated,
300 * more dead keys are added and we need to update the upper limit.
301 * Currently, the upper limit is GDK_KEY_dead_dasia+1. The +1 has to do with
302 * a temporary issue in the X.Org header files.
303 * In future versions it will be just the keysym (no +1).
305 #define IS_DEAD_KEY(k) \
306 ((k) >= GDK_KEY_dead_grave && (k) <= (GDK_KEY_dead_dasia+1))
308 #ifdef GDK_WINDOWING_WIN32
310 /* On Windows, user expectation is that typing a dead accent followed
311 * by space will input the corresponding spacing character. The X
312 * compose tables are different for dead acute and diaeresis, which
313 * when followed by space produce a plain ASCII apostrophe and double
314 * quote respectively. So special-case those.
318 check_win32_special_cases (GtkIMContextSimple *context_simple,
321 GtkIMContextSimplePrivate *priv = context_simple->priv;
322 if (n_compose == 2 &&
323 priv->compose_buffer[1] == GDK_KEY_space)
327 switch (priv->compose_buffer[0])
329 case GDK_KEY_dead_acute:
330 value = 0x00B4; break;
331 case GDK_KEY_dead_diaeresis:
332 value = 0x00A8; break;
336 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
337 priv->compose_buffer[0] = 0;
339 GTK_NOTE (MISC, g_print ("win32: U+%04X\n", value));
347 check_win32_special_case_after_compact_match (GtkIMContextSimple *context_simple,
351 GtkIMContextSimplePrivate *priv = context_simple->priv;
353 /* On Windows user expectation is that typing two dead accents will input
354 * two corresponding spacing accents.
356 if (n_compose == 2 &&
357 priv->compose_buffer[0] == priv->compose_buffer[1] &&
358 IS_DEAD_KEY (priv->compose_buffer[0]))
360 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
361 GTK_NOTE (MISC, g_print ("win32: U+%04X ", value));
367 #ifdef GDK_WINDOWING_QUARTZ
370 check_quartz_special_cases (GtkIMContextSimple *context_simple,
373 GtkIMContextSimplePrivate *priv = context_simple->priv;
375 if (n_compose == 2 &&
376 priv->compose_buffer[1] == GDK_KEY_space)
380 switch (priv->compose_buffer[0])
382 case GDK_KEY_dead_doubleacute:
388 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
389 priv->compose_buffer[0] = 0;
391 GTK_NOTE (MISC, g_print ("quartz: U+%04X\n", value));
402 check_compact_table (GtkIMContextSimple *context_simple,
403 const GtkComposeTableCompact *table,
406 GtkIMContextSimplePrivate *priv = context_simple->priv;
414 /* Will never match, if the sequence in the compose buffer is longer
415 * than the sequences in the table. Further, compare_seq (key, val)
416 * will overrun val if key is longer than val.
418 if (n_compose > table->max_seq_len)
421 seq_index = bsearch (priv->compose_buffer,
424 sizeof (guint16) * table->n_index_stride,
429 GTK_NOTE (MISC, g_print ("compact: no\n"));
433 if (seq_index && n_compose == 1)
435 GTK_NOTE (MISC, g_print ("compact: yes\n"));
439 GTK_NOTE (MISC, g_print ("compact: %d ", *seq_index));
443 for (i = n_compose - 1; i < table->max_seq_len; i++)
447 if (seq_index[i + 1] - seq_index[i] > 0)
449 seq = bsearch (priv->compose_buffer + 1,
450 table->data + seq_index[i],
451 (seq_index[i + 1] - seq_index[i]) / row_stride,
452 sizeof (guint16) * row_stride,
457 if (i == n_compose - 1)
459 value = seq[row_stride - 1];
466 GTK_NOTE (MISC, g_print ("tentative match U+%04X ", value));
467 priv->tentative_match = value;
468 priv->tentative_match_len = n_compose;
471 g_signal_emit_by_name (context_simple, "preedit-changed");
473 GTK_NOTE (MISC, g_print ("yes\n"));
482 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
484 check_win32_special_case_after_compact_match (context_simple, n_compose, value);
486 priv->compose_buffer[0] = 0;
488 GTK_NOTE (MISC, g_print ("U+%04X\n", value));
492 GTK_NOTE (MISC, g_print ("no\n"));
496 /* This function receives a sequence of Unicode characters and tries to
497 * normalize it (NFC). We check for the case where the resulting string
498 * has length 1 (single character).
499 * NFC normalisation normally rearranges diacritic marks, unless these
500 * belong to the same Canonical Combining Class.
501 * If they belong to the same canonical combining class, we produce all
502 * permutations of the diacritic marks, then attempt to normalize.
505 check_normalize_nfc (gunichar* combination_buffer, gint n_compose)
507 gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
508 gchar *combination_utf8_temp = NULL;
509 gchar *nfc_temp = NULL;
516 for (i = 1; i < n_compose; i++ )
519 /* Xorg reuses dead_tilde for the perispomeni diacritic mark.
520 * We check if base character belongs to Greek Unicode block,
521 * and if so, we replace tilde with perispomeni.
523 if (combination_buffer[0] >= 0x390 && combination_buffer[0] <= 0x3FF)
525 for (i = 1; i < n_compose; i++ )
526 if (combination_buffer[i] == 0x303)
527 combination_buffer[i] = 0x342;
530 memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
532 for (i = 0; i < n_combinations; i++ )
534 g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
535 combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
536 nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);
538 if (g_utf8_strlen (nfc_temp, -1) == 1)
540 memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
542 g_free (combination_utf8_temp);
548 g_free (combination_utf8_temp);
553 temp_swap = combination_buffer_temp[i % (n_compose - 1) + 1];
554 combination_buffer_temp[i % (n_compose - 1) + 1] = combination_buffer_temp[(i+1) % (n_compose - 1) + 1];
555 combination_buffer_temp[(i+1) % (n_compose - 1) + 1] = temp_swap;
565 check_algorithmically (GtkIMContextSimple *context_simple,
569 GtkIMContextSimplePrivate *priv = context_simple->priv;
571 gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
572 gchar *combination_utf8, *nfc;
574 if (n_compose >= GTK_MAX_COMPOSE_LEN)
577 for (i = 0; i < n_compose && IS_DEAD_KEY (priv->compose_buffer[i]); i++)
582 if (i > 0 && i == n_compose - 1)
584 combination_buffer[0] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
585 combination_buffer[n_compose] = 0;
589 switch (priv->compose_buffer[i])
591 #define CASE(keysym, unicode) \
592 case GDK_KEY_dead_##keysym: combination_buffer[i+1] = unicode; break
594 CASE (grave, 0x0300);
595 CASE (acute, 0x0301);
596 CASE (circumflex, 0x0302);
597 CASE (tilde, 0x0303); /* Also used with perispomeni, 0x342. */
598 CASE (macron, 0x0304);
599 CASE (breve, 0x0306);
600 CASE (abovedot, 0x0307);
601 CASE (diaeresis, 0x0308);
603 CASE (abovering, 0x030A);
604 CASE (doubleacute, 0x030B);
605 CASE (caron, 0x030C);
606 CASE (abovecomma, 0x0313); /* Equivalent to psili */
607 CASE (abovereversedcomma, 0x0314); /* Equivalent to dasia */
608 CASE (horn, 0x031B); /* Legacy use for psili, 0x313 (or 0x343). */
609 CASE (belowdot, 0x0323);
610 CASE (cedilla, 0x0327);
611 CASE (ogonek, 0x0328); /* Legacy use for dasia, 0x314.*/
613 CASE (voiced_sound, 0x3099); /* Per Markus Kuhn keysyms.txt file. */
614 CASE (semivoiced_sound, 0x309A); /* Per Markus Kuhn keysyms.txt file. */
616 /* The following cases are to be removed once xkeyboard-config,
617 * xorg are fully updated.
619 /* Workaround for typo in 1.4.x xserver-xorg */
620 case 0xfe66: combination_buffer[i+1] = 0x314; break;
621 /* CASE (dasia, 0x314); */
622 /* CASE (perispomeni, 0x342); */
623 /* CASE (psili, 0x343); */
626 combination_buffer[i+1] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
631 /* If the buffer normalizes to a single character, then modify the order
632 * of combination_buffer accordingly, if necessary, and return TRUE.
634 if (check_normalize_nfc (combination_buffer, n_compose))
637 combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
638 nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
640 value = g_utf8_get_char (nfc);
641 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
642 priv->compose_buffer[0] = 0;
644 g_free (combination_utf8);
654 /* In addition to the table-driven sequences, we allow Unicode hex
655 * codes to be entered. The method chosen here is similar to the
656 * one recommended in ISO 14755, but not exactly the same, since we
657 * don't want to steal 16 valuable key combinations.
659 * A hex Unicode sequence must be started with Ctrl-Shift-U, followed
660 * by a sequence of hex digits entered with Ctrl-Shift still held.
661 * Releasing one of the modifiers or pressing space while the modifiers
662 * are still held commits the character. It is possible to erase
663 * digits using backspace.
665 * As an extension to the above, we also allow to start the sequence
666 * with Ctrl-Shift-U, then release the modifiers before typing any
667 * digits, and enter the digits without modifiers.
671 check_hex (GtkIMContextSimple *context_simple,
674 GtkIMContextSimplePrivate *priv = context_simple->priv;
675 /* See if this is a hex sequence, return TRUE if so */
682 priv->tentative_match = 0;
683 priv->tentative_match_len = 0;
685 str = g_string_new (NULL);
688 while (i < n_compose)
692 ch = gdk_keyval_to_unicode (priv->compose_buffer[i]);
697 if (!g_unichar_isxdigit (ch))
700 buf[g_unichar_to_utf8 (ch, buf)] = '\0';
702 g_string_append (str, buf);
707 n = strtoul (str->str, &nptr, 16);
709 /* If strtoul fails it probably means non-latin digits were used;
710 * we should in principle handle that, but we probably don't.
712 if (nptr - str->str < str->len)
714 g_string_free (str, TRUE);
718 g_string_free (str, TRUE);
720 if (g_unichar_validate (n))
722 priv->tentative_match = n;
723 priv->tentative_match_len = n_compose;
730 beep_window (GdkWindow *window)
732 GdkScreen *screen = gdk_window_get_screen (window);
735 g_object_get (gtk_settings_get_for_screen (screen),
736 "gtk-error-bell", &beep,
740 gdk_window_beep (window);
744 no_sequence_matches (GtkIMContextSimple *context_simple,
748 GtkIMContextSimplePrivate *priv = context_simple->priv;
749 GtkIMContext *context;
752 context = GTK_IM_CONTEXT (context_simple);
754 /* No compose sequences found, check first if we have a partial
757 if (priv->tentative_match)
759 gint len = priv->tentative_match_len;
762 gtk_im_context_simple_commit_char (context, priv->tentative_match);
763 priv->compose_buffer[0] = 0;
765 for (i=0; i < n_compose - len - 1; i++)
767 GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
768 tmp_event->key.keyval = priv->compose_buffer[len + i];
770 gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
771 gdk_event_free (tmp_event);
774 return gtk_im_context_filter_keypress (context, event);
778 priv->compose_buffer[0] = 0;
779 if (n_compose > 1) /* Invalid sequence */
781 beep_window (event->window);
785 ch = gdk_keyval_to_unicode (event->keyval);
786 if (ch != 0 && !g_unichar_iscntrl (ch))
788 gtk_im_context_simple_commit_char (context, ch);
797 is_hex_keyval (guint keyval)
799 gunichar ch = gdk_keyval_to_unicode (keyval);
801 return g_unichar_isxdigit (ch);
805 canonical_hex_keyval (GdkEventKey *event)
807 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_window_get_display (event->window));
809 guint *keyvals = NULL;
813 /* See if the keyval is already a hex digit */
814 if (is_hex_keyval (event->keyval))
815 return event->keyval;
817 /* See if this key would have generated a hex keyval in
818 * any other state, and return that hex keyval if so
820 gdk_keymap_get_entries_for_keycode (keymap,
821 event->hardware_keycode,
829 if (is_hex_keyval (keyvals[i]))
843 /* No way to make it a hex digit
849 gtk_im_context_simple_filter_keypress (GtkIMContext *context,
852 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
853 GtkIMContextSimplePrivate *priv = context_simple->priv;
854 GdkDisplay *display = gdk_window_get_display (event->window);
857 GdkModifierType hex_mod_mask;
858 gboolean have_hex_mods;
859 gboolean is_hex_start;
861 gboolean is_backspace;
866 while (priv->compose_buffer[n_compose] != 0)
869 if (event->type == GDK_KEY_RELEASE)
871 if (priv->in_hex_sequence &&
872 (event->keyval == GDK_KEY_Control_L || event->keyval == GDK_KEY_Control_R ||
873 event->keyval == GDK_KEY_Shift_L || event->keyval == GDK_KEY_Shift_R))
875 if (priv->tentative_match &&
876 g_unichar_validate (priv->tentative_match))
878 gtk_im_context_simple_commit_char (context, priv->tentative_match);
879 priv->compose_buffer[0] = 0;
882 else if (n_compose == 0)
884 priv->modifiers_dropped = TRUE;
888 /* invalid hex sequence */
889 beep_window (event->window);
891 priv->tentative_match = 0;
892 priv->in_hex_sequence = FALSE;
893 priv->compose_buffer[0] = 0;
895 g_signal_emit_by_name (context_simple, "preedit-changed");
896 g_signal_emit_by_name (context_simple, "preedit-end");
905 /* Ignore modifier key presses */
906 for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
907 if (event->keyval == gtk_compose_ignore[i])
910 hex_mod_mask = gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
911 GDK_MODIFIER_INTENT_PRIMARY_ACCELERATOR);
912 hex_mod_mask |= GDK_SHIFT_MASK;
914 if (priv->in_hex_sequence && priv->modifiers_dropped)
915 have_hex_mods = TRUE;
917 have_hex_mods = (event->state & (hex_mod_mask)) == hex_mod_mask;
918 is_hex_start = event->keyval == GDK_KEY_U;
919 is_hex_end = (event->keyval == GDK_KEY_space ||
920 event->keyval == GDK_KEY_KP_Space ||
921 event->keyval == GDK_KEY_Return ||
922 event->keyval == GDK_KEY_ISO_Enter ||
923 event->keyval == GDK_KEY_KP_Enter);
924 is_backspace = event->keyval == GDK_KEY_BackSpace;
925 is_escape = event->keyval == GDK_KEY_Escape;
926 hex_keyval = canonical_hex_keyval (event);
928 /* If we are already in a non-hex sequence, or
929 * this keystroke is not hex modifiers + hex digit, don't filter
930 * key events with accelerator modifiers held down. We only treat
931 * Control and Alt as accel modifiers here, since Super, Hyper and
932 * Meta are often co-located with Mode_Switch, Multi_Key or
935 if (!have_hex_mods ||
936 (n_compose > 0 && !priv->in_hex_sequence) ||
937 (n_compose == 0 && !priv->in_hex_sequence && !is_hex_start) ||
938 (priv->in_hex_sequence && !hex_keyval &&
939 !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
942 GdkModifierType no_text_input_mask;
944 display = gdk_window_get_display (event->window);
947 gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
948 GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
950 if (event->state & no_text_input_mask ||
951 (priv->in_hex_sequence && priv->modifiers_dropped &&
952 (event->keyval == GDK_KEY_Return ||
953 event->keyval == GDK_KEY_ISO_Enter ||
954 event->keyval == GDK_KEY_KP_Enter)))
960 /* Handle backspace */
961 if (priv->in_hex_sequence && have_hex_mods && is_backspace)
966 priv->compose_buffer[n_compose] = 0;
967 check_hex (context_simple, n_compose);
971 priv->in_hex_sequence = FALSE;
974 g_signal_emit_by_name (context_simple, "preedit-changed");
976 if (!priv->in_hex_sequence)
977 g_signal_emit_by_name (context_simple, "preedit-end");
982 /* Check for hex sequence restart */
983 if (priv->in_hex_sequence && have_hex_mods && is_hex_start)
985 if (priv->tentative_match &&
986 g_unichar_validate (priv->tentative_match))
988 gtk_im_context_simple_commit_char (context, priv->tentative_match);
989 priv->compose_buffer[0] = 0;
993 /* invalid hex sequence */
995 beep_window (event->window);
997 priv->tentative_match = 0;
998 priv->in_hex_sequence = FALSE;
999 priv->compose_buffer[0] = 0;
1003 /* Check for hex sequence start */
1004 if (!priv->in_hex_sequence && have_hex_mods && is_hex_start)
1006 priv->compose_buffer[0] = 0;
1007 priv->in_hex_sequence = TRUE;
1008 priv->modifiers_dropped = FALSE;
1009 priv->tentative_match = 0;
1011 g_signal_emit_by_name (context_simple, "preedit-start");
1012 g_signal_emit_by_name (context_simple, "preedit-changed");
1017 /* Then, check for compose sequences */
1018 if (priv->in_hex_sequence)
1021 priv->compose_buffer[n_compose++] = hex_keyval;
1024 gtk_im_context_simple_reset (context);
1028 else if (!is_hex_end)
1030 /* non-hex character in hex sequence */
1031 beep_window (event->window);
1037 priv->compose_buffer[n_compose++] = event->keyval;
1039 priv->compose_buffer[n_compose] = 0;
1041 if (priv->in_hex_sequence)
1043 /* If the modifiers are still held down, consider the sequence again */
1046 /* space or return ends the sequence, and we eat the key */
1047 if (n_compose > 0 && is_hex_end)
1049 if (priv->tentative_match &&
1050 g_unichar_validate (priv->tentative_match))
1052 gtk_im_context_simple_commit_char (context, priv->tentative_match);
1053 priv->compose_buffer[0] = 0;
1057 /* invalid hex sequence */
1058 beep_window (event->window);
1060 priv->tentative_match = 0;
1061 priv->in_hex_sequence = FALSE;
1062 priv->compose_buffer[0] = 0;
1065 else if (!check_hex (context_simple, n_compose))
1066 beep_window (event->window);
1068 g_signal_emit_by_name (context_simple, "preedit-changed");
1070 if (!priv->in_hex_sequence)
1071 g_signal_emit_by_name (context_simple, "preedit-end");
1078 tmp_list = priv->tables;
1081 if (check_table (context_simple, tmp_list->data, n_compose))
1083 tmp_list = tmp_list->next;
1088 for (i = 0; i < n_compose; i++)
1090 const gchar *keyval_name = gdk_keyval_name (priv->compose_buffer[i]);
1092 if (keyval_name != NULL)
1093 g_print ("%s ", keyval_name);
1095 g_print ("%04x ", priv->compose_buffer[i]);
1100 #ifdef GDK_WINDOWING_WIN32
1101 if (check_win32_special_cases (context_simple, n_compose))
1105 #ifdef GDK_WINDOWING_QUARTZ
1106 if (check_quartz_special_cases (context_simple, n_compose))
1110 if (check_compact_table (context_simple, >k_compose_table_compact, n_compose))
1113 if (check_algorithmically (context_simple, n_compose))
1117 /* The current compose_buffer doesn't match anything */
1118 return no_sequence_matches (context_simple, n_compose, event);
1122 gtk_im_context_simple_reset (GtkIMContext *context)
1124 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1125 GtkIMContextSimplePrivate *priv = context_simple->priv;
1127 priv->compose_buffer[0] = 0;
1129 if (priv->tentative_match || priv->in_hex_sequence)
1131 priv->in_hex_sequence = FALSE;
1132 priv->tentative_match = 0;
1133 priv->tentative_match_len = 0;
1134 g_signal_emit_by_name (context_simple, "preedit-changed");
1135 g_signal_emit_by_name (context_simple, "preedit-end");
1140 gtk_im_context_simple_get_preedit_string (GtkIMContext *context,
1142 PangoAttrList **attrs,
1145 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1146 GtkIMContextSimplePrivate *priv = context_simple->priv;
1147 char outbuf[37]; /* up to 6 hex digits */
1150 if (priv->in_hex_sequence)
1157 while (priv->compose_buffer[hexchars] != 0)
1159 len += g_unichar_to_utf8 (gdk_keyval_to_unicode (priv->compose_buffer[hexchars]),
1164 g_assert (len < 25);
1166 else if (priv->tentative_match)
1167 len = g_unichar_to_utf8 (priv->tentative_match, outbuf);
1172 *str = g_strdup (outbuf);
1176 *attrs = pango_attr_list_new ();
1180 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
1181 attr->start_index = 0;
1182 attr->end_index = len;
1183 pango_attr_list_insert (*attrs, attr);
1192 * gtk_im_context_simple_add_table: (skip)
1193 * @context_simple: A #GtkIMContextSimple
1195 * @max_seq_len: Maximum length of a sequence in the table
1196 * (cannot be greater than #GTK_MAX_COMPOSE_LEN)
1197 * @n_seqs: number of sequences in the table
1199 * Adds an additional table to search to the input context.
1200 * Each row of the table consists of @max_seq_len key symbols
1201 * followed by two #guint16 interpreted as the high and low
1202 * words of a #gunicode value. Tables are searched starting
1203 * from the last added.
1205 * The table must be sorted in dictionary order on the
1206 * numeric value of the key symbol fields. (Values beyond
1207 * the length of the sequence should be zero.)
1210 gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
1215 GtkIMContextSimplePrivate *priv = context_simple->priv;
1216 GtkComposeTable *table;
1218 g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
1219 g_return_if_fail (data != NULL);
1220 g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
1222 table = g_new (GtkComposeTable, 1);
1224 table->max_seq_len = max_seq_len;
1225 table->n_seqs = n_seqs;
1227 priv->tables = g_slist_prepend (priv->tables, table);