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.
22 #include "gtkimcontext.h"
23 #include "gtkmainprivate.h"
24 #include "gtkmarshalers.h"
28 * SECTION:gtkimcontext
29 * @title: GtkIMContext
30 * @short_description: Base class for input method contexts
31 * @include: gtk/gtk.h,gtk/gtkimmodule.h
33 * #GtkIMContext defines the interface for GTK+ input methods. An input method
34 * is used by GTK+ text input widgets like #GtkEntry to map from key events to
35 * Unicode character strings.
37 * The user may change the current input method via a context menu, unless the
38 * #GtkSettings:gtk-show-input-method-menu GtkSettings property is set to FALSE.
39 * The default input method can be set programmatically via the
40 * #GtkSettings:gtk-im-module GtkSettings property. Alternatively, you may set
41 * the GTK_IM_MODULE environment variable as documented in #gtk-running.
43 * The #GtkEntry #GtkEntry:im-module and #GtkTextView #GtkTextView:im-module
44 * properties may also be used to set input methods for specific widget
45 * instances. For instance, a certain entry widget might be expected to contain
46 * certain characters which would be easier to input with a certain input
49 * An input method may consume multiple key events in sequence and finally
50 * output the composed result. This is called preediting, and an input method
51 * may provide feedback about this process by displaying the intermediate
52 * composition states as preedit text. For instance, the default GTK+ input
53 * method implements the input of arbitrary Unicode code points by holding down
54 * the Control and Shift keys and then typing "U" followed by the hexadecimal
55 * digits of the code point. When releasing the Control and Shift keys,
56 * preediting ends and the character is inserted as text. Ctrl+Shift+u20AC for
57 * example results in the € sign.
59 * Additional input methods can be made available for use by GTK+ widgets as
60 * loadable modules. An input method module is a small shared library which
61 * implements a subclass of #GtkIMContext or #GtkIMContextSimple and exports
62 * these four functions:
64 * <informalexample><programlisting>
65 * void im_module_init(#GTypeModule *module);
66 * </programlisting></informalexample>
67 * This function should register the #GType of the #GtkIMContext subclass which
68 * implements the input method by means of g_type_module_register_type(). Note
69 * that g_type_register_static() cannot be used as the type needs to be
70 * registered dynamically.
72 * <informalexample><programlisting>
73 * void im_module_exit(void);
74 * </programlisting></informalexample>
75 * Here goes any cleanup code your input method might require on module unload.
77 * <informalexample><programlisting>
78 * void im_module_list(const #GtkIMContextInfo ***contexts, int *n_contexts)
80 * *contexts = info_list;
81 * *n_contexts = G_N_ELEMENTS (info_list);
83 * </programlisting></informalexample>
84 * This function returns the list of input methods provided by the module. The
85 * example implementation above shows a common solution and simply returns a
86 * pointer to statically defined array of #GtkIMContextInfo items for each
87 * provided input method.
89 * <informalexample><programlisting>
90 * #GtkIMContext * im_module_create(const #gchar *context_id);
91 * </programlisting></informalexample>
92 * This function should return a pointer to a newly created instance of the
93 * #GtkIMContext subclass identified by @context_id. The context ID is the same
94 * as specified in the #GtkIMContextInfo array returned by im_module_list().
96 * After a new loadable input method module has been installed on the system,
97 * the configuration file <filename>gtk.immodules</filename> needs to be
98 * regenerated by <link linkend="gtk-query-immodules-3.0">gtk-query-immodules-3.0</link>,
99 * in order for the new input method to become available to GTK+ applications.
107 RETRIEVE_SURROUNDING,
112 static guint im_context_signals[LAST_SIGNAL] = { 0 };
114 static void gtk_im_context_real_get_preedit_string (GtkIMContext *context,
116 PangoAttrList **attrs,
118 static gboolean gtk_im_context_real_filter_keypress (GtkIMContext *context,
120 static gboolean gtk_im_context_real_get_surrounding (GtkIMContext *context,
123 static void gtk_im_context_real_set_surrounding (GtkIMContext *context,
128 G_DEFINE_ABSTRACT_TYPE (GtkIMContext, gtk_im_context, G_TYPE_OBJECT)
132 * @preedit_start: Default handler of the #GtkIMContext::preedit-start signal.
133 * @preedit_end: Default handler of the #GtkIMContext::preedit-end signal.
134 * @preedit_changed: Default handler of the #GtkIMContext::preedit-changed
136 * @commit: Default handler of the #GtkIMContext::commit signal.
137 * @retrieve_surrounding: Default handler of the
138 * #GtkIMContext::retrieve-surrounding signal.
139 * @delete_surrounding: Default handler of the
140 * #GtkIMContext::delete-surrounding signal.
141 * @set_client_window: Called via gtk_im_context_set_client_window() when the
142 * input window where the entered text will appear changes. Override this to
143 * keep track of the current input window, for instance for the purpose of
144 * positioning a status display of your input method.
145 * @get_preedit_string: Called via gtk_im_context_get_preedit_string() to
146 * retrieve the text currently being preedited for display at the cursor
147 * position. Any input method which composes complex characters or any
148 * other compositions from multiple sequential key presses should override
149 * this method to provide feedback.
150 * @filter_keypress: Called via gtk_im_context_filter_keypress() on every
151 * key press or release event. Every non-trivial input method needs to
152 * override this in order to implement the mapping from key events to text.
153 * A return value of %TRUE indicates to the caller that the event was
154 * consumed by the input method. In that case, the #GtkIMContext::commit
155 * signal should be emitted upon completion of a key sequence to pass the
156 * resulting text back to the input widget. Alternatively, %FALSE may be
157 * returned to indicate that the event wasn't handled by the input method.
158 * If a builtin mapping exists for the key, it is used to produce a
160 * @focus_in: Called via gtk_im_context_focus_in() when the input widget
161 * has gained focus. May be overridden to keep track of the current focus.
162 * @focus_out: Called via gtk_im_context_focus_out() when the input widget
163 * has lost focus. May be overridden to keep track of the current focus.
164 * @reset: Called via gtk_im_context_reset() to signal a change such as a
165 * change in cursor position. An input method that implements preediting
166 * should override this method to clear the preedit state on reset.
167 * @set_cursor_location: Called via gtk_im_context_set_cursor_location()
168 * to inform the input method of the current cursor location relative to
169 * the client window. May be overridden to implement the display of popup
170 * windows at the cursor position.
171 * @set_use_preedit: Called via gtk_im_context_set_use_preedit() to control
172 * the use of the preedit string. Override this to display feedback by some
173 * other means if turned off.
174 * @set_surrounding: Called via gtk_im_context_set_surrounding() in response
175 * to signal #GtkIMContext::retrieve-surrounding to update the input
176 * method's idea of the context around the cursor. It is not necessary to
177 * override this method even with input methods which implement
178 * context-dependent behavior. The base implementation is sufficient for
179 * gtk_im_context_get_surrounding() to work.
180 * @get_surrounding: Called via gtk_im_context_get_surrounding() to update
181 * the context around the cursor location. It is not necessary to override
182 * this method even with input methods which implement context-dependent
183 * behavior. The base implementation emits
184 * #GtkIMContext::retrieve-surrounding and records the context received
185 * by the subsequent invocation of @get_surrounding.
188 gtk_im_context_class_init (GtkIMContextClass *klass)
190 klass->get_preedit_string = gtk_im_context_real_get_preedit_string;
191 klass->filter_keypress = gtk_im_context_real_filter_keypress;
192 klass->get_surrounding = gtk_im_context_real_get_surrounding;
193 klass->set_surrounding = gtk_im_context_real_set_surrounding;
196 * GtkIMContext::preedit-start:
197 * @context: the object on which the signal is emitted
199 * The ::preedit-start signal is emitted when a new preediting sequence
202 im_context_signals[PREEDIT_START] =
203 g_signal_new (I_("preedit-start"),
204 G_TYPE_FROM_CLASS (klass),
206 G_STRUCT_OFFSET (GtkIMContextClass, preedit_start),
208 _gtk_marshal_VOID__VOID,
211 * GtkIMContext::preedit-end:
212 * @context: the object on which the signal is emitted
214 * The ::preedit-end signal is emitted when a preediting sequence
215 * has been completed or canceled.
217 im_context_signals[PREEDIT_END] =
218 g_signal_new (I_("preedit-end"),
219 G_TYPE_FROM_CLASS (klass),
221 G_STRUCT_OFFSET (GtkIMContextClass, preedit_end),
223 _gtk_marshal_VOID__VOID,
226 * GtkIMContext::preedit-changed:
227 * @context: the object on which the signal is emitted
229 * The ::preedit-changed signal is emitted whenever the preedit sequence
230 * currently being entered has changed. It is also emitted at the end of
231 * a preedit sequence, in which case
232 * gtk_im_context_get_preedit_string() returns the empty string.
234 im_context_signals[PREEDIT_CHANGED] =
235 g_signal_new (I_("preedit-changed"),
236 G_TYPE_FROM_CLASS (klass),
238 G_STRUCT_OFFSET (GtkIMContextClass, preedit_changed),
240 _gtk_marshal_VOID__VOID,
243 * GtkIMContext::commit:
244 * @context: the object on which the signal is emitted
245 * @str: the completed character(s) entered by the user
247 * The ::commit signal is emitted when a complete input sequence
248 * has been entered by the user. This can be a single character
249 * immediately after a key press or the final result of preediting.
251 im_context_signals[COMMIT] =
252 g_signal_new (I_("commit"),
253 G_TYPE_FROM_CLASS (klass),
255 G_STRUCT_OFFSET (GtkIMContextClass, commit),
257 _gtk_marshal_VOID__STRING,
261 * GtkIMContext::retrieve-surrounding:
262 * @context: the object on which the signal is emitted
264 * The ::retrieve-surrounding signal is emitted when the input method
265 * requires the context surrounding the cursor. The callback should set
266 * the input method surrounding context by calling the
267 * gtk_im_context_set_surrounding() method.
269 * Return value: %TRUE if the signal was handled.
271 im_context_signals[RETRIEVE_SURROUNDING] =
272 g_signal_new (I_("retrieve-surrounding"),
273 G_TYPE_FROM_CLASS (klass),
275 G_STRUCT_OFFSET (GtkIMContextClass, retrieve_surrounding),
276 _gtk_boolean_handled_accumulator, NULL,
277 _gtk_marshal_BOOLEAN__VOID,
280 * GtkIMContext::delete-surrounding:
281 * @context: the object on which the signal is emitted
282 * @offset: the character offset from the cursor position of the text
283 * to be deleted. A negative value indicates a position before
285 * @n_chars: the number of characters to be deleted
287 * The ::delete-surrounding signal is emitted when the input method
288 * needs to delete all or part of the context surrounding the cursor.
290 * Return value: %TRUE if the signal was handled.
292 im_context_signals[DELETE_SURROUNDING] =
293 g_signal_new (I_("delete-surrounding"),
294 G_TYPE_FROM_CLASS (klass),
296 G_STRUCT_OFFSET (GtkIMContextClass, delete_surrounding),
297 _gtk_boolean_handled_accumulator, NULL,
298 _gtk_marshal_BOOLEAN__INT_INT,
305 gtk_im_context_init (GtkIMContext *im_context)
310 gtk_im_context_real_get_preedit_string (GtkIMContext *context,
312 PangoAttrList **attrs,
316 *str = g_strdup ("");
318 *attrs = pango_attr_list_new ();
324 gtk_im_context_real_filter_keypress (GtkIMContext *context,
337 gtk_im_context_real_set_surrounding (GtkIMContext *context,
342 SurroundingInfo *info = g_object_get_data (G_OBJECT (context),
343 "gtk-im-surrounding-info");
348 info->text = g_strndup (text, len);
349 info->cursor_index = cursor_index;
354 gtk_im_context_real_get_surrounding (GtkIMContext *context,
359 gboolean info_is_local = FALSE;
360 SurroundingInfo local_info = { NULL, 0 };
361 SurroundingInfo *info;
363 info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");
367 g_object_set_data (G_OBJECT (context), I_("gtk-im-surrounding-info"), info);
368 info_is_local = TRUE;
371 g_signal_emit (context,
372 im_context_signals[RETRIEVE_SURROUNDING], 0,
377 *text = g_strdup (info->text ? info->text : "");
378 *cursor_index = info->cursor_index;
389 g_object_set_data (G_OBJECT (context), I_("gtk-im-surrounding-info"), NULL);
396 * gtk_im_context_set_client_window:
397 * @context: a #GtkIMContext
398 * @window: (allow-none): the client window. This may be %NULL to indicate
399 * that the previous client window no longer exists.
401 * Set the client window for the input context; this is the
402 * #GdkWindow in which the input appears. This window is
403 * used in order to correctly position status windows, and may
404 * also be used for purposes internal to the input method.
407 gtk_im_context_set_client_window (GtkIMContext *context,
410 GtkIMContextClass *klass;
412 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
414 klass = GTK_IM_CONTEXT_GET_CLASS (context);
415 if (klass->set_client_window)
416 klass->set_client_window (context, window);
420 * gtk_im_context_get_preedit_string:
421 * @context: a #GtkIMContext
422 * @str: (out) (transfer full): location to store the retrieved
423 * string. The string retrieved must be freed with g_free().
424 * @attrs: (out) (transfer full): location to store the retrieved
425 * attribute list. When you are done with this list, you
426 * must unreference it with pango_attr_list_unref().
427 * @cursor_pos: (out): location to store position of cursor (in characters)
428 * within the preedit string.
430 * Retrieve the current preedit string for the input context,
431 * and a list of attributes to apply to the string.
432 * This string should be displayed inserted at the insertion
436 gtk_im_context_get_preedit_string (GtkIMContext *context,
438 PangoAttrList **attrs,
441 GtkIMContextClass *klass;
443 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
445 klass = GTK_IM_CONTEXT_GET_CLASS (context);
446 klass->get_preedit_string (context, str, attrs, cursor_pos);
447 g_return_if_fail (str == NULL || g_utf8_validate (*str, -1, NULL));
451 * gtk_im_context_filter_keypress:
452 * @context: a #GtkIMContext
453 * @event: the key event
455 * Allow an input method to internally handle key press and release
456 * events. If this function returns %TRUE, then no further processing
457 * should be done for this key event.
459 * Return value: %TRUE if the input method handled the key event.
463 gtk_im_context_filter_keypress (GtkIMContext *context,
466 GtkIMContextClass *klass;
468 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
469 g_return_val_if_fail (key != NULL, FALSE);
471 klass = GTK_IM_CONTEXT_GET_CLASS (context);
472 return klass->filter_keypress (context, key);
476 * gtk_im_context_focus_in:
477 * @context: a #GtkIMContext
479 * Notify the input method that the widget to which this
480 * input context corresponds has gained focus. The input method
481 * may, for example, change the displayed feedback to reflect
485 gtk_im_context_focus_in (GtkIMContext *context)
487 GtkIMContextClass *klass;
489 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
491 klass = GTK_IM_CONTEXT_GET_CLASS (context);
493 klass->focus_in (context);
497 * gtk_im_context_focus_out:
498 * @context: a #GtkIMContext
500 * Notify the input method that the widget to which this
501 * input context corresponds has lost focus. The input method
502 * may, for example, change the displayed feedback or reset the contexts
503 * state to reflect this change.
506 gtk_im_context_focus_out (GtkIMContext *context)
508 GtkIMContextClass *klass;
510 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
512 klass = GTK_IM_CONTEXT_GET_CLASS (context);
513 if (klass->focus_out)
514 klass->focus_out (context);
518 * gtk_im_context_reset:
519 * @context: a #GtkIMContext
521 * Notify the input method that a change such as a change in cursor
522 * position has been made. This will typically cause the input
523 * method to clear the preedit state.
526 gtk_im_context_reset (GtkIMContext *context)
528 GtkIMContextClass *klass;
530 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
532 klass = GTK_IM_CONTEXT_GET_CLASS (context);
534 klass->reset (context);
539 * gtk_im_context_set_cursor_location:
540 * @context: a #GtkIMContext
541 * @area: new location
543 * Notify the input method that a change in cursor
544 * position has been made. The location is relative to the client
548 gtk_im_context_set_cursor_location (GtkIMContext *context,
549 const GdkRectangle *area)
551 GtkIMContextClass *klass;
553 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
555 klass = GTK_IM_CONTEXT_GET_CLASS (context);
556 if (klass->set_cursor_location)
557 klass->set_cursor_location (context, (GdkRectangle *) area);
561 * gtk_im_context_set_use_preedit:
562 * @context: a #GtkIMContext
563 * @use_preedit: whether the IM context should use the preedit string.
565 * Sets whether the IM context should use the preedit string
566 * to display feedback. If @use_preedit is FALSE (default
567 * is TRUE), then the IM context may use some other method to display
568 * feedback, such as displaying it in a child of the root window.
571 gtk_im_context_set_use_preedit (GtkIMContext *context,
572 gboolean use_preedit)
574 GtkIMContextClass *klass;
576 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
578 klass = GTK_IM_CONTEXT_GET_CLASS (context);
579 if (klass->set_use_preedit)
580 klass->set_use_preedit (context, use_preedit);
584 * gtk_im_context_set_surrounding:
585 * @context: a #GtkIMContext
586 * @text: text surrounding the insertion point, as UTF-8.
587 * the preedit string should not be included within
589 * @len: the length of @text, or -1 if @text is nul-terminated
590 * @cursor_index: the byte index of the insertion cursor within @text.
592 * Sets surrounding context around the insertion point and preedit
593 * string. This function is expected to be called in response to the
594 * GtkIMContext::retrieve_surrounding signal, and will likely have no
595 * effect if called at other times.
598 gtk_im_context_set_surrounding (GtkIMContext *context,
603 GtkIMContextClass *klass;
605 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
606 g_return_if_fail (text != NULL || len == 0);
608 if (text == NULL && len == 0)
613 g_return_if_fail (cursor_index >= 0 && cursor_index <= len);
615 klass = GTK_IM_CONTEXT_GET_CLASS (context);
616 if (klass->set_surrounding)
617 klass->set_surrounding (context, text, len, cursor_index);
621 * gtk_im_context_get_surrounding:
622 * @context: a #GtkIMContext
623 * @text: (out) (transfer full): location to store a UTF-8 encoded
624 * string of text holding context around the insertion point.
625 * If the function returns %TRUE, then you must free the result
626 * stored in this location with g_free().
627 * @cursor_index: (out) location to store byte index of the insertion
628 * cursor within @text.
630 * Retrieves context around the insertion point. Input methods
631 * typically want context in order to constrain input text based on
632 * existing text; this is important for languages such as Thai where
633 * only some sequences of characters are allowed.
635 * This function is implemented by emitting the
636 * GtkIMContext::retrieve_surrounding signal on the input method; in
637 * response to this signal, a widget should provide as much context as
638 * is available, up to an entire paragraph, by calling
639 * gtk_im_context_set_surrounding(). Note that there is no obligation
640 * for a widget to respond to the ::retrieve_surrounding signal, so input
641 * methods must be prepared to function without context.
643 * Return value: %TRUE if surrounding text was provided; in this case
644 * you must free the result stored in *text.
647 gtk_im_context_get_surrounding (GtkIMContext *context,
651 GtkIMContextClass *klass;
652 gchar *local_text = NULL;
654 gboolean result = FALSE;
656 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
658 klass = GTK_IM_CONTEXT_GET_CLASS (context);
659 if (klass->get_surrounding)
660 result = klass->get_surrounding (context,
661 text ? text : &local_text,
662 cursor_index ? cursor_index : &local_index);
671 * gtk_im_context_delete_surrounding:
672 * @context: a #GtkIMContext
673 * @offset: offset from cursor position in chars;
674 * a negative value means start before the cursor.
675 * @n_chars: number of characters to delete.
677 * Asks the widget that the input context is attached to to delete
678 * characters around the cursor position by emitting the
679 * GtkIMContext::delete_surrounding signal. Note that @offset and @n_chars
680 * are in characters not in bytes which differs from the usage other
681 * places in #GtkIMContext.
683 * In order to use this function, you should first call
684 * gtk_im_context_get_surrounding() to get the current context, and
685 * call this function immediately afterwards to make sure that you
686 * know what you are deleting. You should also account for the fact
687 * that even if the signal was handled, the input context might not
688 * have deleted all the characters that were requested to be deleted.
690 * This function is used by an input method that wants to make
691 * subsitutions in the existing text in response to new input. It is
692 * not useful for applications.
694 * Return value: %TRUE if the signal was handled.
697 gtk_im_context_delete_surrounding (GtkIMContext *context,
703 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
705 g_signal_emit (context,
706 im_context_signals[DELETE_SURROUNDING], 0,
707 offset, n_chars, &result);