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/>.
20 #include "gtkimcontext.h"
21 #include "gtkprivate.h"
22 #include "gtkmarshalers.h"
26 * SECTION:gtkimcontext
27 * @title: GtkIMContext
28 * @short_description: Base class for input method contexts
29 * @include: gtk/gtk.h,gtk/gtkimmodule.h
31 * #GtkIMContext defines the interface for GTK+ input methods. An input method
32 * is used by GTK+ text input widgets like #GtkEntry to map from key events to
33 * Unicode character strings.
35 * The user may change the current input method via a context menu, unless the
36 * #GtkSettings:gtk-show-input-method-menu GtkSettings property is set to FALSE.
37 * The default input method can be set programmatically via the
38 * #GtkSettings:gtk-im-module GtkSettings property. Alternatively, you may set
39 * the GTK_IM_MODULE environment variable as documented in #gtk-running.
41 * The #GtkEntry #GtkEntry:im-module and #GtkTextView #GtkTextView:im-module
42 * properties may also be used to set input methods for specific widget
43 * instances. For instance, a certain entry widget might be expected to contain
44 * certain characters which would be easier to input with a certain input
47 * An input method may consume multiple key events in sequence and finally
48 * output the composed result. This is called preediting, and an input method
49 * may provide feedback about this process by displaying the intermediate
50 * composition states as preedit text. For instance, the default GTK+ input
51 * method implements the input of arbitrary Unicode code points by holding down
52 * the Control and Shift keys and then typing "U" followed by the hexadecimal
53 * digits of the code point. When releasing the Control and Shift keys,
54 * preediting ends and the character is inserted as text. Ctrl+Shift+u20AC for
55 * example results in the € sign.
57 * Additional input methods can be made available for use by GTK+ widgets as
58 * loadable modules. An input method module is a small shared library which
59 * implements a subclass of #GtkIMContext or #GtkIMContextSimple and exports
60 * these four functions:
62 * <informalexample><programlisting>
63 * void im_module_init(#GTypeModule *module);
64 * </programlisting></informalexample>
65 * This function should register the #GType of the #GtkIMContext subclass which
66 * implements the input method by means of g_type_module_register_type(). Note
67 * that g_type_register_static() cannot be used as the type needs to be
68 * registered dynamically.
70 * <informalexample><programlisting>
71 * void im_module_exit(void);
72 * </programlisting></informalexample>
73 * Here goes any cleanup code your input method might require on module unload.
75 * <informalexample><programlisting>
76 * void im_module_list(const #GtkIMContextInfo ***contexts, int *n_contexts)
78 * *contexts = info_list;
79 * *n_contexts = G_N_ELEMENTS (info_list);
81 * </programlisting></informalexample>
82 * This function returns the list of input methods provided by the module. The
83 * example implementation above shows a common solution and simply returns a
84 * pointer to statically defined array of #GtkIMContextInfo items for each
85 * provided input method.
87 * <informalexample><programlisting>
88 * #GtkIMContext * im_module_create(const #gchar *context_id);
89 * </programlisting></informalexample>
90 * This function should return a pointer to a newly created instance of the
91 * #GtkIMContext subclass identified by @context_id. The context ID is the same
92 * as specified in the #GtkIMContextInfo array returned by im_module_list().
94 * After a new loadable input method module has been installed on the system,
95 * the configuration file <filename>gtk.immodules</filename> needs to be
96 * regenerated by <link linkend="gtk-query-immodules-3.0">gtk-query-immodules-3.0</link>,
97 * in order for the new input method to become available to GTK+ applications.
105 RETRIEVE_SURROUNDING,
110 static guint im_context_signals[LAST_SIGNAL] = { 0 };
112 static void gtk_im_context_real_get_preedit_string (GtkIMContext *context,
114 PangoAttrList **attrs,
116 static gboolean gtk_im_context_real_filter_keypress (GtkIMContext *context,
118 static gboolean gtk_im_context_real_get_surrounding (GtkIMContext *context,
121 static void gtk_im_context_real_set_surrounding (GtkIMContext *context,
126 G_DEFINE_ABSTRACT_TYPE (GtkIMContext, gtk_im_context, G_TYPE_OBJECT)
130 * @preedit_start: Default handler of the #GtkIMContext::preedit-start signal.
131 * @preedit_end: Default handler of the #GtkIMContext::preedit-end signal.
132 * @preedit_changed: Default handler of the #GtkIMContext::preedit-changed
134 * @commit: Default handler of the #GtkIMContext::commit signal.
135 * @retrieve_surrounding: Default handler of the
136 * #GtkIMContext::retrieve-surrounding signal.
137 * @delete_surrounding: Default handler of the
138 * #GtkIMContext::delete-surrounding signal.
139 * @set_client_window: Called via gtk_im_context_set_client_window() when the
140 * input window where the entered text will appear changes. Override this to
141 * keep track of the current input window, for instance for the purpose of
142 * positioning a status display of your input method.
143 * @get_preedit_string: Called via gtk_im_context_get_preedit_string() to
144 * retrieve the text currently being preedited for display at the cursor
145 * position. Any input method which composes complex characters or any
146 * other compositions from multiple sequential key presses should override
147 * this method to provide feedback.
148 * @filter_keypress: Called via gtk_im_context_filter_keypress() on every
149 * key press or release event. Every non-trivial input method needs to
150 * override this in order to implement the mapping from key events to text.
151 * A return value of %TRUE indicates to the caller that the event was
152 * consumed by the input method. In that case, the #GtkIMContext::commit
153 * signal should be emitted upon completion of a key sequence to pass the
154 * resulting text back to the input widget. Alternatively, %FALSE may be
155 * returned to indicate that the event wasn't handled by the input method.
156 * If a builtin mapping exists for the key, it is used to produce a
158 * @focus_in: Called via gtk_im_context_focus_in() when the input widget
159 * has gained focus. May be overridden to keep track of the current focus.
160 * @focus_out: Called via gtk_im_context_focus_out() when the input widget
161 * has lost focus. May be overridden to keep track of the current focus.
162 * @reset: Called via gtk_im_context_reset() to signal a change such as a
163 * change in cursor position. An input method that implements preediting
164 * should override this method to clear the preedit state on reset.
165 * @set_cursor_location: Called via gtk_im_context_set_cursor_location()
166 * to inform the input method of the current cursor location relative to
167 * the client window. May be overridden to implement the display of popup
168 * windows at the cursor position.
169 * @set_use_preedit: Called via gtk_im_context_set_use_preedit() to control
170 * the use of the preedit string. Override this to display feedback by some
171 * other means if turned off.
172 * @set_surrounding: Called via gtk_im_context_set_surrounding() in response
173 * to signal #GtkIMContext::retrieve-surrounding to update the input
174 * method's idea of the context around the cursor. It is not necessary to
175 * override this method even with input methods which implement
176 * context-dependent behavior. The base implementation is sufficient for
177 * gtk_im_context_get_surrounding() to work.
178 * @get_surrounding: Called via gtk_im_context_get_surrounding() to update
179 * the context around the cursor location. It is not necessary to override
180 * this method even with input methods which implement context-dependent
181 * behavior. The base implementation emits
182 * #GtkIMContext::retrieve-surrounding and records the context received
183 * by the subsequent invocation of @get_surrounding.
186 gtk_im_context_class_init (GtkIMContextClass *klass)
188 klass->get_preedit_string = gtk_im_context_real_get_preedit_string;
189 klass->filter_keypress = gtk_im_context_real_filter_keypress;
190 klass->get_surrounding = gtk_im_context_real_get_surrounding;
191 klass->set_surrounding = gtk_im_context_real_set_surrounding;
194 * GtkIMContext::preedit-start:
195 * @context: the object on which the signal is emitted
197 * The ::preedit-start signal is emitted when a new preediting sequence
200 im_context_signals[PREEDIT_START] =
201 g_signal_new (I_("preedit-start"),
202 G_TYPE_FROM_CLASS (klass),
204 G_STRUCT_OFFSET (GtkIMContextClass, preedit_start),
206 _gtk_marshal_VOID__VOID,
209 * GtkIMContext::preedit-end:
210 * @context: the object on which the signal is emitted
212 * The ::preedit-end signal is emitted when a preediting sequence
213 * has been completed or canceled.
215 im_context_signals[PREEDIT_END] =
216 g_signal_new (I_("preedit-end"),
217 G_TYPE_FROM_CLASS (klass),
219 G_STRUCT_OFFSET (GtkIMContextClass, preedit_end),
221 _gtk_marshal_VOID__VOID,
224 * GtkIMContext::preedit-changed:
225 * @context: the object on which the signal is emitted
227 * The ::preedit-changed signal is emitted whenever the preedit sequence
228 * currently being entered has changed. It is also emitted at the end of
229 * a preedit sequence, in which case
230 * gtk_im_context_get_preedit_string() returns the empty string.
232 im_context_signals[PREEDIT_CHANGED] =
233 g_signal_new (I_("preedit-changed"),
234 G_TYPE_FROM_CLASS (klass),
236 G_STRUCT_OFFSET (GtkIMContextClass, preedit_changed),
238 _gtk_marshal_VOID__VOID,
241 * GtkIMContext::commit:
242 * @context: the object on which the signal is emitted
243 * @str: the completed character(s) entered by the user
245 * The ::commit signal is emitted when a complete input sequence
246 * has been entered by the user. This can be a single character
247 * immediately after a key press or the final result of preediting.
249 im_context_signals[COMMIT] =
250 g_signal_new (I_("commit"),
251 G_TYPE_FROM_CLASS (klass),
253 G_STRUCT_OFFSET (GtkIMContextClass, commit),
255 _gtk_marshal_VOID__STRING,
259 * GtkIMContext::retrieve-surrounding:
260 * @context: the object on which the signal is emitted
262 * The ::retrieve-surrounding signal is emitted when the input method
263 * requires the context surrounding the cursor. The callback should set
264 * the input method surrounding context by calling the
265 * gtk_im_context_set_surrounding() method.
267 * Return value: %TRUE if the signal was handled.
269 im_context_signals[RETRIEVE_SURROUNDING] =
270 g_signal_new (I_("retrieve-surrounding"),
271 G_TYPE_FROM_CLASS (klass),
273 G_STRUCT_OFFSET (GtkIMContextClass, retrieve_surrounding),
274 _gtk_boolean_handled_accumulator, NULL,
275 _gtk_marshal_BOOLEAN__VOID,
278 * GtkIMContext::delete-surrounding:
279 * @context: the object on which the signal is emitted
280 * @offset: the character offset from the cursor position of the text
281 * to be deleted. A negative value indicates a position before
283 * @n_chars: the number of characters to be deleted
285 * The ::delete-surrounding signal is emitted when the input method
286 * needs to delete all or part of the context surrounding the cursor.
288 * Return value: %TRUE if the signal was handled.
290 im_context_signals[DELETE_SURROUNDING] =
291 g_signal_new (I_("delete-surrounding"),
292 G_TYPE_FROM_CLASS (klass),
294 G_STRUCT_OFFSET (GtkIMContextClass, delete_surrounding),
295 _gtk_boolean_handled_accumulator, NULL,
296 _gtk_marshal_BOOLEAN__INT_INT,
303 gtk_im_context_init (GtkIMContext *im_context)
308 gtk_im_context_real_get_preedit_string (GtkIMContext *context,
310 PangoAttrList **attrs,
314 *str = g_strdup ("");
316 *attrs = pango_attr_list_new ();
322 gtk_im_context_real_filter_keypress (GtkIMContext *context,
335 gtk_im_context_real_set_surrounding (GtkIMContext *context,
340 SurroundingInfo *info = g_object_get_data (G_OBJECT (context),
341 "gtk-im-surrounding-info");
346 info->text = g_strndup (text, len);
347 info->cursor_index = cursor_index;
352 gtk_im_context_real_get_surrounding (GtkIMContext *context,
357 gboolean info_is_local = FALSE;
358 SurroundingInfo local_info = { NULL, 0 };
359 SurroundingInfo *info;
361 info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");
365 g_object_set_data (G_OBJECT (context), I_("gtk-im-surrounding-info"), info);
366 info_is_local = TRUE;
369 g_signal_emit (context,
370 im_context_signals[RETRIEVE_SURROUNDING], 0,
375 *text = g_strdup (info->text ? info->text : "");
376 *cursor_index = info->cursor_index;
387 g_object_set_data (G_OBJECT (context), I_("gtk-im-surrounding-info"), NULL);
394 * gtk_im_context_set_client_window:
395 * @context: a #GtkIMContext
396 * @window: (allow-none): the client window. This may be %NULL to indicate
397 * that the previous client window no longer exists.
399 * Set the client window for the input context; this is the
400 * #GdkWindow in which the input appears. This window is
401 * used in order to correctly position status windows, and may
402 * also be used for purposes internal to the input method.
405 gtk_im_context_set_client_window (GtkIMContext *context,
408 GtkIMContextClass *klass;
410 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
412 klass = GTK_IM_CONTEXT_GET_CLASS (context);
413 if (klass->set_client_window)
414 klass->set_client_window (context, window);
418 * gtk_im_context_get_preedit_string:
419 * @context: a #GtkIMContext
420 * @str: (out) (transfer full): location to store the retrieved
421 * string. The string retrieved must be freed with g_free().
422 * @attrs: (out) (transfer full): location to store the retrieved
423 * attribute list. When you are done with this list, you
424 * must unreference it with pango_attr_list_unref().
425 * @cursor_pos: (out): location to store position of cursor (in characters)
426 * within the preedit string.
428 * Retrieve the current preedit string for the input context,
429 * and a list of attributes to apply to the string.
430 * This string should be displayed inserted at the insertion
434 gtk_im_context_get_preedit_string (GtkIMContext *context,
436 PangoAttrList **attrs,
439 GtkIMContextClass *klass;
441 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
443 klass = GTK_IM_CONTEXT_GET_CLASS (context);
444 klass->get_preedit_string (context, str, attrs, cursor_pos);
445 g_return_if_fail (str == NULL || g_utf8_validate (*str, -1, NULL));
449 * gtk_im_context_filter_keypress:
450 * @context: a #GtkIMContext
451 * @event: the key event
453 * Allow an input method to internally handle key press and release
454 * events. If this function returns %TRUE, then no further processing
455 * should be done for this key event.
457 * Return value: %TRUE if the input method handled the key event.
461 gtk_im_context_filter_keypress (GtkIMContext *context,
464 GtkIMContextClass *klass;
466 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
467 g_return_val_if_fail (key != NULL, FALSE);
469 klass = GTK_IM_CONTEXT_GET_CLASS (context);
470 return klass->filter_keypress (context, key);
474 * gtk_im_context_focus_in:
475 * @context: a #GtkIMContext
477 * Notify the input method that the widget to which this
478 * input context corresponds has gained focus. The input method
479 * may, for example, change the displayed feedback to reflect
483 gtk_im_context_focus_in (GtkIMContext *context)
485 GtkIMContextClass *klass;
487 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
489 klass = GTK_IM_CONTEXT_GET_CLASS (context);
491 klass->focus_in (context);
495 * gtk_im_context_focus_out:
496 * @context: a #GtkIMContext
498 * Notify the input method that the widget to which this
499 * input context corresponds has lost focus. The input method
500 * may, for example, change the displayed feedback or reset the contexts
501 * state to reflect this change.
504 gtk_im_context_focus_out (GtkIMContext *context)
506 GtkIMContextClass *klass;
508 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
510 klass = GTK_IM_CONTEXT_GET_CLASS (context);
511 if (klass->focus_out)
512 klass->focus_out (context);
516 * gtk_im_context_reset:
517 * @context: a #GtkIMContext
519 * Notify the input method that a change such as a change in cursor
520 * position has been made. This will typically cause the input
521 * method to clear the preedit state.
524 gtk_im_context_reset (GtkIMContext *context)
526 GtkIMContextClass *klass;
528 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
530 klass = GTK_IM_CONTEXT_GET_CLASS (context);
532 klass->reset (context);
537 * gtk_im_context_set_cursor_location:
538 * @context: a #GtkIMContext
539 * @area: new location
541 * Notify the input method that a change in cursor
542 * position has been made. The location is relative to the client
546 gtk_im_context_set_cursor_location (GtkIMContext *context,
547 const GdkRectangle *area)
549 GtkIMContextClass *klass;
551 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
553 klass = GTK_IM_CONTEXT_GET_CLASS (context);
554 if (klass->set_cursor_location)
555 klass->set_cursor_location (context, (GdkRectangle *) area);
559 * gtk_im_context_set_use_preedit:
560 * @context: a #GtkIMContext
561 * @use_preedit: whether the IM context should use the preedit string.
563 * Sets whether the IM context should use the preedit string
564 * to display feedback. If @use_preedit is FALSE (default
565 * is TRUE), then the IM context may use some other method to display
566 * feedback, such as displaying it in a child of the root window.
569 gtk_im_context_set_use_preedit (GtkIMContext *context,
570 gboolean use_preedit)
572 GtkIMContextClass *klass;
574 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
576 klass = GTK_IM_CONTEXT_GET_CLASS (context);
577 if (klass->set_use_preedit)
578 klass->set_use_preedit (context, use_preedit);
582 * gtk_im_context_set_surrounding:
583 * @context: a #GtkIMContext
584 * @text: text surrounding the insertion point, as UTF-8.
585 * the preedit string should not be included within
587 * @len: the length of @text, or -1 if @text is nul-terminated
588 * @cursor_index: the byte index of the insertion cursor within @text.
590 * Sets surrounding context around the insertion point and preedit
591 * string. This function is expected to be called in response to the
592 * GtkIMContext::retrieve_surrounding signal, and will likely have no
593 * effect if called at other times.
596 gtk_im_context_set_surrounding (GtkIMContext *context,
601 GtkIMContextClass *klass;
603 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
604 g_return_if_fail (text != NULL || len == 0);
606 if (text == NULL && len == 0)
611 g_return_if_fail (cursor_index >= 0 && cursor_index <= len);
613 klass = GTK_IM_CONTEXT_GET_CLASS (context);
614 if (klass->set_surrounding)
615 klass->set_surrounding (context, text, len, cursor_index);
619 * gtk_im_context_get_surrounding:
620 * @context: a #GtkIMContext
621 * @text: (out) (transfer full): location to store a UTF-8 encoded
622 * string of text holding context around the insertion point.
623 * If the function returns %TRUE, then you must free the result
624 * stored in this location with g_free().
625 * @cursor_index: (out) location to store byte index of the insertion
626 * cursor within @text.
628 * Retrieves context around the insertion point. Input methods
629 * typically want context in order to constrain input text based on
630 * existing text; this is important for languages such as Thai where
631 * only some sequences of characters are allowed.
633 * This function is implemented by emitting the
634 * GtkIMContext::retrieve_surrounding signal on the input method; in
635 * response to this signal, a widget should provide as much context as
636 * is available, up to an entire paragraph, by calling
637 * gtk_im_context_set_surrounding(). Note that there is no obligation
638 * for a widget to respond to the ::retrieve_surrounding signal, so input
639 * methods must be prepared to function without context.
641 * Return value: %TRUE if surrounding text was provided; in this case
642 * you must free the result stored in *text.
645 gtk_im_context_get_surrounding (GtkIMContext *context,
649 GtkIMContextClass *klass;
650 gchar *local_text = NULL;
652 gboolean result = FALSE;
654 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
656 klass = GTK_IM_CONTEXT_GET_CLASS (context);
657 if (klass->get_surrounding)
658 result = klass->get_surrounding (context,
659 text ? text : &local_text,
660 cursor_index ? cursor_index : &local_index);
669 * gtk_im_context_delete_surrounding:
670 * @context: a #GtkIMContext
671 * @offset: offset from cursor position in chars;
672 * a negative value means start before the cursor.
673 * @n_chars: number of characters to delete.
675 * Asks the widget that the input context is attached to to delete
676 * characters around the cursor position by emitting the
677 * GtkIMContext::delete_surrounding signal. Note that @offset and @n_chars
678 * are in characters not in bytes which differs from the usage other
679 * places in #GtkIMContext.
681 * In order to use this function, you should first call
682 * gtk_im_context_get_surrounding() to get the current context, and
683 * call this function immediately afterwards to make sure that you
684 * know what you are deleting. You should also account for the fact
685 * that even if the signal was handled, the input context might not
686 * have deleted all the characters that were requested to be deleted.
688 * This function is used by an input method that wants to make
689 * subsitutions in the existing text in response to new input. It is
690 * not useful for applications.
692 * Return value: %TRUE if the signal was handled.
695 gtk_im_context_delete_surrounding (GtkIMContext *context,
701 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
703 g_signal_emit (context,
704 im_context_signals[DELETE_SURROUNDING], 0,
705 offset, n_chars, &result);