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.h>
25 #include "gtkprivate.h"
26 #include "gtkaccelgroup.h"
27 #include "gtkimcontextsimple.h"
28 #include "gtksettings.h"
29 #include "gtkwidget.h"
33 typedef struct _GtkComposeTable GtkComposeTable;
34 typedef struct _GtkComposeTableCompact GtkComposeTableCompact;
36 struct _GtkIMContextSimplePriv
40 guint compose_buffer[GTK_MAX_COMPOSE_LEN + 1];
41 gunichar tentative_match;
42 gint tentative_match_len;
44 guint in_hex_sequence : 1;
45 guint modifiers_dropped : 1;
48 struct _GtkComposeTable
55 struct _GtkComposeTableCompact
63 /* This file contains the table of the compose sequences,
64 * static const guint16 gtk_compose_seqs_compact[] = {}
65 * IT is generated from the compose-parse.py script.
67 #include "gtkimcontextsimpleseqs.h"
69 /* From the values below, the value 23 means the number of different first keysyms
70 * that exist in the Compose file (from Xorg). When running compose-parse.py without
71 * parameters, you get the count that you can put here. Needed when updating the
72 * gtkimcontextsimpleseqs.h header file (contains the compose sequences).
74 static const GtkComposeTableCompact gtk_compose_table_compact = {
75 gtk_compose_seqs_compact,
81 static const guint16 gtk_compose_ignore[] = {
100 static void gtk_im_context_simple_finalize (GObject *obj);
101 static gboolean gtk_im_context_simple_filter_keypress (GtkIMContext *context,
103 static void gtk_im_context_simple_reset (GtkIMContext *context);
104 static void gtk_im_context_simple_get_preedit_string (GtkIMContext *context,
106 PangoAttrList **attrs,
109 G_DEFINE_TYPE (GtkIMContextSimple, gtk_im_context_simple, GTK_TYPE_IM_CONTEXT)
112 gtk_im_context_simple_class_init (GtkIMContextSimpleClass *class)
114 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
115 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
117 im_context_class->filter_keypress = gtk_im_context_simple_filter_keypress;
118 im_context_class->reset = gtk_im_context_simple_reset;
119 im_context_class->get_preedit_string = gtk_im_context_simple_get_preedit_string;
120 gobject_class->finalize = gtk_im_context_simple_finalize;
122 g_type_class_add_private (class, sizeof (GtkIMContextSimplePriv));
126 gtk_im_context_simple_init (GtkIMContextSimple *im_context_simple)
128 im_context_simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (im_context_simple,
129 GTK_TYPE_IM_CONTEXT_SIMPLE,
130 GtkIMContextSimplePriv);
134 gtk_im_context_simple_finalize (GObject *obj)
136 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (obj);
137 GtkIMContextSimplePriv *priv = context_simple->priv;
141 g_slist_foreach (priv->tables, (GFunc)g_free, NULL);
142 g_slist_free (priv->tables);
147 G_OBJECT_CLASS (gtk_im_context_simple_parent_class)->finalize (obj);
151 * gtk_im_context_simple_new:
153 * Creates a new #GtkIMContextSimple.
155 * Returns: a new #GtkIMContextSimple.
158 gtk_im_context_simple_new (void)
160 return g_object_new (GTK_TYPE_IM_CONTEXT_SIMPLE, NULL);
164 gtk_im_context_simple_commit_char (GtkIMContext *context,
167 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
168 GtkIMContextSimplePriv *priv = context_simple->priv;
172 g_return_if_fail (g_unichar_validate (ch));
174 len = g_unichar_to_utf8 (ch, buf);
177 if (priv->tentative_match || priv->in_hex_sequence)
179 priv->in_hex_sequence = FALSE;
180 priv->tentative_match = 0;
181 priv->tentative_match_len = 0;
182 g_signal_emit_by_name (context_simple, "preedit-changed");
183 g_signal_emit_by_name (context_simple, "preedit-end");
186 g_signal_emit_by_name (context, "commit", &buf);
190 compare_seq_index (const void *key, const void *value)
192 const guint *keysyms = key;
193 const guint16 *seq = value;
195 if (keysyms[0] < seq[0])
197 else if (keysyms[0] > seq[0])
204 compare_seq (const void *key, const void *value)
207 const guint *keysyms = key;
208 const guint16 *seq = value;
212 if (keysyms[i] < seq[i])
214 else if (keysyms[i] > seq[i])
224 check_table (GtkIMContextSimple *context_simple,
225 const GtkComposeTable *table,
228 GtkIMContextSimplePriv *priv = context_simple->priv;
229 gint row_stride = table->max_seq_len + 2;
232 /* Will never match, if the sequence in the compose buffer is longer
233 * than the sequences in the table. Further, compare_seq (key, val)
234 * will overrun val if key is longer than val. */
235 if (n_compose > table->max_seq_len)
238 seq = bsearch (priv->compose_buffer,
239 table->data, table->n_seqs,
240 sizeof (guint16) * row_stride,
247 /* Back up to the first sequence that matches to make sure
248 * we find the exact match if their is one.
250 while (seq > table->data)
252 prev_seq = seq - row_stride;
253 if (compare_seq (priv->compose_buffer, prev_seq) != 0)
258 if (n_compose == table->max_seq_len ||
259 seq[n_compose] == 0) /* complete sequence */
263 0x10000 * seq[table->max_seq_len] + seq[table->max_seq_len + 1];
266 /* We found a tentative match. See if there are any longer
267 * sequences containing this subsequence
269 next_seq = seq + row_stride;
270 if (next_seq < table->data + row_stride * table->n_seqs)
272 if (compare_seq (priv->compose_buffer, next_seq) == 0)
274 priv->tentative_match = value;
275 priv->tentative_match_len = n_compose;
277 g_signal_emit_by_name (context_simple, "preedit-changed");
283 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
284 priv->compose_buffer[0] = 0;
293 /* Checks if a keysym is a dead key. Dead key keysym values are defined in
294 * ../gdk/gdkkeysyms.h and the first is GDK_dead_grave. As X.Org is updated,
295 * more dead keys are added and we need to update the upper limit.
296 * Currently, the upper limit is GDK_dead_dasia+1. The +1 has to do with
297 * a temporary issue in the X.Org header files.
298 * In future versions it will be just the keysym (no +1).
300 #define IS_DEAD_KEY(k) \
301 ((k) >= GDK_dead_grave && (k) <= (GDK_dead_dasia+1))
303 #ifdef GDK_WINDOWING_WIN32
305 /* On Windows, user expectation is that typing a dead accent followed
306 * by space will input the corresponding spacing character. The X
307 * compose tables are different for dead acute and diaeresis, which
308 * when followed by space produce a plain ASCII apostrophe and double
309 * quote respectively. So special-case those.
313 check_win32_special_cases (GtkIMContextSimple *context_simple,
316 GtkIMContextSimplePriv *priv = context_simple->priv;
317 if (n_compose == 2 &&
318 priv->compose_buffer[1] == GDK_space)
322 switch (priv->compose_buffer[0])
325 value = 0x00B4; break;
326 case GDK_dead_diaeresis:
327 value = 0x00A8; break;
331 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
332 priv->compose_buffer[0] = 0;
334 GTK_NOTE (MISC, g_print ("win32: U+%04X\n", value));
342 check_win32_special_case_after_compact_match (GtkIMContextSimple *context_simple,
346 GtkIMContextSimplePriv *priv = context_simple->priv;
348 /* On Windows user expectation is that typing two dead accents will input
349 * two corresponding spacing accents.
351 if (n_compose == 2 &&
352 priv->compose_buffer[0] == priv->compose_buffer[1] &&
353 IS_DEAD_KEY (priv->compose_buffer[0]))
355 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
356 GTK_NOTE (MISC, g_print ("win32: U+%04X ", value));
363 check_compact_table (GtkIMContextSimple *context_simple,
364 const GtkComposeTableCompact *table,
367 GtkIMContextSimplePriv *priv = context_simple->priv;
373 /* Will never match, if the sequence in the compose buffer is longer
374 * than the sequences in the table. Further, compare_seq (key, val)
375 * will overrun val if key is longer than val. */
376 if (n_compose > table->max_seq_len)
379 seq_index = bsearch (priv->compose_buffer,
380 table->data, table->n_index_size,
381 sizeof (guint16) * table->n_index_stride,
386 GTK_NOTE (MISC, g_print ("compact: no\n"));
390 if (seq_index && n_compose == 1)
392 GTK_NOTE (MISC, g_print ("compact: yes\n"));
396 GTK_NOTE (MISC, g_print ("compact: %d ", *seq_index));
399 for (i = n_compose-1; i < table->max_seq_len; i++)
403 if (seq_index[i+1] - seq_index[i] > 0)
405 seq = bsearch (priv->compose_buffer + 1,
406 table->data + seq_index[i], (seq_index[i+1] - seq_index[i]) / row_stride,
407 sizeof (guint16) * row_stride,
412 if (i == n_compose - 1)
416 g_signal_emit_by_name (context_simple, "preedit-changed");
418 GTK_NOTE (MISC, g_print ("yes\n"));
427 GTK_NOTE (MISC, g_print ("no\n"));
434 value = seq[row_stride - 1];
436 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
438 check_win32_special_case_after_compact_match (context_simple, n_compose, value);
440 priv->compose_buffer[0] = 0;
442 GTK_NOTE (MISC, g_print ("U+%04X\n", value));
446 GTK_NOTE (MISC, g_print ("no\n"));
450 /* This function receives a sequence of Unicode characters and tries to
451 * normalize it (NFC). We check for the case the the resulting string
452 * has length 1 (single character).
453 * NFC normalisation normally rearranges diacritic marks, unless these
454 * belong to the same Canonical Combining Class.
455 * If they belong to the same canonical combining class, we produce all
456 * permutations of the diacritic marks, then attempt to normalize.
459 check_normalize_nfc (gunichar* combination_buffer, gint n_compose)
461 gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
462 gchar *combination_utf8_temp = NULL;
463 gchar *nfc_temp = NULL;
470 for (i = 1; i < n_compose; i++ )
473 /* Xorg reuses dead_tilde for the perispomeni diacritic mark.
474 * We check if base character belongs to Greek Unicode block,
475 * and if so, we replace tilde with perispomeni. */
476 if (combination_buffer[0] >= 0x390 && combination_buffer[0] <= 0x3FF)
478 for (i = 1; i < n_compose; i++ )
479 if (combination_buffer[i] == 0x303)
480 combination_buffer[i] = 0x342;
483 memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
485 for (i = 0; i < n_combinations; i++ )
487 g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
488 combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
489 nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);
491 if (g_utf8_strlen (nfc_temp, -1) == 1)
493 memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
495 g_free (combination_utf8_temp);
501 g_free (combination_utf8_temp);
506 temp_swap = combination_buffer_temp[i % (n_compose - 1) + 1];
507 combination_buffer_temp[i % (n_compose - 1) + 1] = combination_buffer_temp[(i+1) % (n_compose - 1) + 1];
508 combination_buffer_temp[(i+1) % (n_compose - 1) + 1] = temp_swap;
518 check_algorithmically (GtkIMContextSimple *context_simple,
522 GtkIMContextSimplePriv *priv = context_simple->priv;
524 gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
525 gchar *combination_utf8, *nfc;
527 if (n_compose >= GTK_MAX_COMPOSE_LEN)
530 for (i = 0; i < n_compose && IS_DEAD_KEY (priv->compose_buffer[i]); i++)
535 if (i > 0 && i == n_compose - 1)
537 combination_buffer[0] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
538 combination_buffer[n_compose] = 0;
542 switch (priv->compose_buffer[i])
544 #define CASE(keysym, unicode) \
545 case GDK_dead_##keysym: combination_buffer[i+1] = unicode; break
547 CASE (grave, 0x0300);
548 CASE (acute, 0x0301);
549 CASE (circumflex, 0x0302);
550 CASE (tilde, 0x0303); /* Also used with perispomeni, 0x342. */
551 CASE (macron, 0x0304);
552 CASE (breve, 0x0306);
553 CASE (abovedot, 0x0307);
554 CASE (diaeresis, 0x0308);
556 CASE (abovering, 0x030A);
557 CASE (doubleacute, 0x030B);
558 CASE (caron, 0x030C);
559 CASE (abovecomma, 0x0313); /* Equivalent to psili */
560 CASE (abovereversedcomma, 0x0314); /* Equivalent to dasia */
561 CASE (horn, 0x031B); /* Legacy use for psili, 0x313 (or 0x343). */
562 CASE (belowdot, 0x0323);
563 CASE (cedilla, 0x0327);
564 CASE (ogonek, 0x0328); /* Legacy use for dasia, 0x314.*/
566 CASE (voiced_sound, 0x3099); /* Per Markus Kuhn keysyms.txt file. */
567 CASE (semivoiced_sound, 0x309A); /* Per Markus Kuhn keysyms.txt file. */
569 /* The following cases are to be removed once xkeyboard-config,
570 * xorg are fully updated.
572 /* Workaround for typo in 1.4.x xserver-xorg */
573 case 0xfe66: combination_buffer[i+1] = 0x314; break;
574 /* CASE (dasia, 0x314); */
575 /* CASE (perispomeni, 0x342); */
576 /* CASE (psili, 0x343); */
579 combination_buffer[i+1] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
584 /* If the buffer normalizes to a single character,
585 * then modify the order of combination_buffer accordingly, if necessary,
588 if (check_normalize_nfc (combination_buffer, n_compose))
591 combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
592 nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
594 value = g_utf8_get_char (nfc);
595 gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
596 priv->compose_buffer[0] = 0;
598 g_free (combination_utf8);
608 /* In addition to the table-driven sequences, we allow Unicode hex
609 * codes to be entered. The method chosen here is similar to the
610 * one recommended in ISO 14755, but not exactly the same, since we
611 * don't want to steal 16 valuable key combinations.
613 * A hex Unicode sequence must be started with Ctrl-Shift-U, followed
614 * by a sequence of hex digits entered with Ctrl-Shift still held.
615 * Releasing one of the modifiers or pressing space while the modifiers
616 * are still held commits the character. It is possible to erase
617 * digits using backspace.
619 * As an extension to the above, we also allow to start the sequence
620 * with Ctrl-Shift-U, then release the modifiers before typing any
621 * digits, and enter the digits without modifiers.
623 #define HEX_MOD_MASK (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)
626 check_hex (GtkIMContextSimple *context_simple,
629 GtkIMContextSimplePriv *priv = context_simple->priv;
630 /* See if this is a hex sequence, return TRUE if so */
637 priv->tentative_match = 0;
638 priv->tentative_match_len = 0;
640 str = g_string_new (NULL);
643 while (i < n_compose)
647 ch = gdk_keyval_to_unicode (priv->compose_buffer[i]);
652 if (!g_unichar_isxdigit (ch))
655 buf[g_unichar_to_utf8 (ch, buf)] = '\0';
657 g_string_append (str, buf);
662 n = strtoul (str->str, &nptr, 16);
664 /* if strtoul fails it probably means non-latin digits were used;
665 * we should in principle handle that, but we probably don't.
667 if (nptr - str->str < str->len)
669 g_string_free (str, TRUE);
673 g_string_free (str, TRUE);
675 if (g_unichar_validate (n))
677 priv->tentative_match = n;
678 priv->tentative_match_len = n_compose;
685 beep_window (GdkWindow *window)
689 gdk_window_get_user_data (window, (gpointer) &widget);
691 if (GTK_IS_WIDGET (widget))
693 gtk_widget_error_bell (widget);
697 GdkScreen *screen = gdk_drawable_get_screen (GDK_DRAWABLE (window));
700 g_object_get (gtk_settings_get_for_screen (screen),
701 "gtk-error-bell", &beep,
705 gdk_window_beep (window);
710 no_sequence_matches (GtkIMContextSimple *context_simple,
714 GtkIMContextSimplePriv *priv = context_simple->priv;
715 GtkIMContext *context;
718 context = GTK_IM_CONTEXT (context_simple);
720 /* No compose sequences found, check first if we have a partial
723 if (priv->tentative_match)
725 gint len = priv->tentative_match_len;
728 gtk_im_context_simple_commit_char (context, priv->tentative_match);
729 priv->compose_buffer[0] = 0;
731 for (i=0; i < n_compose - len - 1; i++)
733 GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
734 tmp_event->key.keyval = priv->compose_buffer[len + i];
736 gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
737 gdk_event_free (tmp_event);
740 return gtk_im_context_filter_keypress (context, event);
744 priv->compose_buffer[0] = 0;
745 if (n_compose > 1) /* Invalid sequence */
747 beep_window (event->window);
751 ch = gdk_keyval_to_unicode (event->keyval);
754 gtk_im_context_simple_commit_char (context, ch);
763 is_hex_keyval (guint keyval)
765 gunichar ch = gdk_keyval_to_unicode (keyval);
767 return g_unichar_isxdigit (ch);
771 canonical_hex_keyval (GdkEventKey *event)
773 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_drawable_get_display (event->window));
775 guint *keyvals = NULL;
779 /* See if the keyval is already a hex digit */
780 if (is_hex_keyval (event->keyval))
781 return event->keyval;
783 /* See if this key would have generated a hex keyval in
784 * any other state, and return that hex keyval if so
786 gdk_keymap_get_entries_for_keycode (keymap,
787 event->hardware_keycode,
795 if (is_hex_keyval (keyvals[i]))
809 /* No way to make it a hex digit
815 gtk_im_context_simple_filter_keypress (GtkIMContext *context,
818 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
819 GtkIMContextSimplePriv *priv = context_simple->priv;
822 gboolean have_hex_mods;
823 gboolean is_hex_start;
825 gboolean is_backspace;
830 while (priv->compose_buffer[n_compose] != 0)
833 if (event->type == GDK_KEY_RELEASE)
835 if (priv->in_hex_sequence &&
836 (event->keyval == GDK_Control_L || event->keyval == GDK_Control_R ||
837 event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R))
839 if (priv->tentative_match &&
840 g_unichar_validate (priv->tentative_match))
842 gtk_im_context_simple_commit_char (context, priv->tentative_match);
843 priv->compose_buffer[0] = 0;
846 else if (n_compose == 0)
848 priv->modifiers_dropped = TRUE;
852 /* invalid hex sequence */
853 beep_window (event->window);
855 priv->tentative_match = 0;
856 priv->in_hex_sequence = FALSE;
857 priv->compose_buffer[0] = 0;
859 g_signal_emit_by_name (context_simple, "preedit-changed");
860 g_signal_emit_by_name (context_simple, "preedit-end");
869 /* Ignore modifier key presses */
870 for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
871 if (event->keyval == gtk_compose_ignore[i])
874 if (priv->in_hex_sequence && priv->modifiers_dropped)
875 have_hex_mods = TRUE;
877 have_hex_mods = (event->state & (HEX_MOD_MASK)) == HEX_MOD_MASK;
878 is_hex_start = event->keyval == GDK_U;
879 is_hex_end = (event->keyval == GDK_space ||
880 event->keyval == GDK_KP_Space ||
881 event->keyval == GDK_Return ||
882 event->keyval == GDK_ISO_Enter ||
883 event->keyval == GDK_KP_Enter);
884 is_backspace = event->keyval == GDK_BackSpace;
885 is_escape = event->keyval == GDK_Escape;
886 hex_keyval = canonical_hex_keyval (event);
888 /* If we are already in a non-hex sequence, or
889 * this keystroke is not hex modifiers + hex digit, don't filter
890 * key events with accelerator modifiers held down. We only treat
891 * Control and Alt as accel modifiers here, since Super, Hyper and
892 * Meta are often co-located with Mode_Switch, Multi_Key or
895 if (!have_hex_mods ||
896 (n_compose > 0 && !priv->in_hex_sequence) ||
897 (n_compose == 0 && !priv->in_hex_sequence && !is_hex_start) ||
898 (priv->in_hex_sequence && !hex_keyval &&
899 !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
901 if (event->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK) ||
902 (priv->in_hex_sequence && priv->modifiers_dropped &&
903 (event->keyval == GDK_Return ||
904 event->keyval == GDK_ISO_Enter ||
905 event->keyval == GDK_KP_Enter)))
911 /* Handle backspace */
912 if (priv->in_hex_sequence && have_hex_mods && is_backspace)
917 priv->compose_buffer[n_compose] = 0;
918 check_hex (context_simple, n_compose);
922 priv->in_hex_sequence = FALSE;
925 g_signal_emit_by_name (context_simple, "preedit-changed");
927 if (!priv->in_hex_sequence)
928 g_signal_emit_by_name (context_simple, "preedit-end");
933 /* Check for hex sequence restart */
934 if (priv->in_hex_sequence && have_hex_mods && is_hex_start)
936 if (priv->tentative_match &&
937 g_unichar_validate (priv->tentative_match))
939 gtk_im_context_simple_commit_char (context, priv->tentative_match);
940 priv->compose_buffer[0] = 0;
944 /* invalid hex sequence */
946 beep_window (event->window);
948 priv->tentative_match = 0;
949 priv->in_hex_sequence = FALSE;
950 priv->compose_buffer[0] = 0;
954 /* Check for hex sequence start */
955 if (!priv->in_hex_sequence && have_hex_mods && is_hex_start)
957 priv->compose_buffer[0] = 0;
958 priv->in_hex_sequence = TRUE;
959 priv->modifiers_dropped = FALSE;
960 priv->tentative_match = 0;
962 g_signal_emit_by_name (context_simple, "preedit-start");
963 g_signal_emit_by_name (context_simple, "preedit-changed");
968 /* Then, check for compose sequences */
969 if (priv->in_hex_sequence)
972 priv->compose_buffer[n_compose++] = hex_keyval;
975 gtk_im_context_simple_reset (context);
979 else if (!is_hex_end)
981 /* non-hex character in hex sequence */
982 beep_window (event->window);
988 priv->compose_buffer[n_compose++] = event->keyval;
990 priv->compose_buffer[n_compose] = 0;
992 if (priv->in_hex_sequence)
994 /* If the modifiers are still held down, consider the sequence again */
997 /* space or return ends the sequence, and we eat the key */
998 if (n_compose > 0 && is_hex_end)
1000 if (priv->tentative_match &&
1001 g_unichar_validate (priv->tentative_match))
1003 gtk_im_context_simple_commit_char (context, priv->tentative_match);
1004 priv->compose_buffer[0] = 0;
1008 /* invalid hex sequence */
1009 beep_window (event->window);
1011 priv->tentative_match = 0;
1012 priv->in_hex_sequence = FALSE;
1013 priv->compose_buffer[0] = 0;
1016 else if (!check_hex (context_simple, n_compose))
1017 beep_window (event->window);
1019 g_signal_emit_by_name (context_simple, "preedit-changed");
1021 if (!priv->in_hex_sequence)
1022 g_signal_emit_by_name (context_simple, "preedit-end");
1029 tmp_list = priv->tables;
1032 if (check_table (context_simple, tmp_list->data, n_compose))
1034 tmp_list = tmp_list->next;
1039 for (i = 0; i < n_compose; i++)
1041 const gchar *keyval_name = gdk_keyval_name (priv->compose_buffer[i]);
1043 if (keyval_name != NULL)
1044 g_print ("%s ", keyval_name);
1046 g_print ("%04x ", priv->compose_buffer[i]);
1051 #ifdef GDK_WINDOWING_WIN32
1052 if (check_win32_special_cases (context_simple, n_compose))
1056 if (check_compact_table (context_simple, >k_compose_table_compact, n_compose))
1059 if (check_algorithmically (context_simple, n_compose))
1063 /* The current compose_buffer doesn't match anything */
1064 return no_sequence_matches (context_simple, n_compose, event);
1068 gtk_im_context_simple_reset (GtkIMContext *context)
1070 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1071 GtkIMContextSimplePriv *priv = context_simple->priv;
1073 priv->compose_buffer[0] = 0;
1075 if (priv->tentative_match || priv->in_hex_sequence)
1077 priv->in_hex_sequence = FALSE;
1078 priv->tentative_match = 0;
1079 priv->tentative_match_len = 0;
1080 g_signal_emit_by_name (context_simple, "preedit-changed");
1081 g_signal_emit_by_name (context_simple, "preedit-end");
1086 gtk_im_context_simple_get_preedit_string (GtkIMContext *context,
1088 PangoAttrList **attrs,
1091 GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1092 GtkIMContextSimplePriv *priv = context_simple->priv;
1093 char outbuf[37]; /* up to 6 hex digits */
1096 if (priv->in_hex_sequence)
1103 while (priv->compose_buffer[hexchars] != 0)
1105 len += g_unichar_to_utf8 (gdk_keyval_to_unicode (priv->compose_buffer[hexchars]),
1110 g_assert (len < 25);
1112 else if (priv->tentative_match)
1113 len = g_unichar_to_utf8 (priv->tentative_match, outbuf);
1118 *str = g_strdup (outbuf);
1122 *attrs = pango_attr_list_new ();
1126 PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
1127 attr->start_index = 0;
1128 attr->end_index = len;
1129 pango_attr_list_insert (*attrs, attr);
1138 * gtk_im_context_simple_add_table:
1139 * @context_simple: A #GtkIMContextSimple
1141 * @max_seq_len: Maximum length of a sequence in the table
1142 * (cannot be greater than #GTK_MAX_COMPOSE_LEN)
1143 * @n_seqs: number of sequences in the table
1145 * Adds an additional table to search to the input context.
1146 * Each row of the table consists of @max_seq_len key symbols
1147 * followed by two #guint16 interpreted as the high and low
1148 * words of a #gunicode value. Tables are searched starting
1149 * from the last added.
1151 * The table must be sorted in dictionary order on the
1152 * numeric value of the key symbol fields. (Values beyond
1153 * the length of the sequence should be zero.)
1156 gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
1161 GtkIMContextSimplePriv *priv = context_simple->priv;
1162 GtkComposeTable *table;
1164 g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
1165 g_return_if_fail (data != NULL);
1166 g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
1168 table = g_new (GtkComposeTable, 1);
1170 table->max_seq_len = max_seq_len;
1171 table->n_seqs = n_seqs;
1173 priv->tables = g_slist_prepend (priv->tables, table);