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.
23 #include "gtkimcontext.h"
24 #include "gtkmain.h" /* For _gtk_boolean_handled_accumulator */
25 #include "gtkmarshalers.h"
37 static guint im_context_signals[LAST_SIGNAL] = { 0 };
39 static void gtk_im_context_class_init (GtkIMContextClass *class);
40 static void gtk_im_context_init (GtkIMContext *im_context);
42 static void gtk_im_context_real_get_preedit_string (GtkIMContext *context,
44 PangoAttrList **attrs,
46 static gboolean gtk_im_context_real_filter_keypress (GtkIMContext *context,
48 static gboolean gtk_im_context_real_get_surrounding (GtkIMContext *context,
51 static void gtk_im_context_real_set_surrounding (GtkIMContext *context,
57 gtk_im_context_get_type (void)
59 static GType im_context_type = 0;
63 static const GTypeInfo im_context_info =
65 sizeof (GtkIMContextClass),
67 (GBaseFinalizeFunc) NULL,
68 (GClassInitFunc) gtk_im_context_class_init,
69 NULL, /* class_finalize */
70 NULL, /* class_data */
71 sizeof (GtkIMContext),
73 (GInstanceInitFunc) gtk_im_context_init,
74 NULL, /* value_table */
78 g_type_register_static (G_TYPE_OBJECT, "GtkIMContext",
79 &im_context_info, G_TYPE_FLAG_ABSTRACT);
82 return im_context_type;
86 gtk_im_context_class_init (GtkIMContextClass *klass)
88 klass->get_preedit_string = gtk_im_context_real_get_preedit_string;
89 klass->filter_keypress = gtk_im_context_real_filter_keypress;
90 klass->get_surrounding = gtk_im_context_real_get_surrounding;
91 klass->set_surrounding = gtk_im_context_real_set_surrounding;
93 im_context_signals[PREEDIT_START] =
94 g_signal_new ("preedit_start",
95 G_TYPE_FROM_CLASS (klass),
97 G_STRUCT_OFFSET (GtkIMContextClass, preedit_start),
99 _gtk_marshal_VOID__VOID,
102 im_context_signals[PREEDIT_END] =
103 g_signal_new ("preedit_end",
104 G_TYPE_FROM_CLASS (klass),
106 G_STRUCT_OFFSET (GtkIMContextClass, preedit_end),
108 _gtk_marshal_VOID__VOID,
111 im_context_signals[PREEDIT_CHANGED] =
112 g_signal_new ("preedit_changed",
113 G_TYPE_FROM_CLASS (klass),
115 G_STRUCT_OFFSET (GtkIMContextClass, preedit_changed),
117 _gtk_marshal_VOID__VOID,
120 im_context_signals[COMMIT] =
121 g_signal_new ("commit",
122 G_TYPE_FROM_CLASS (klass),
124 G_STRUCT_OFFSET (GtkIMContextClass, commit),
126 _gtk_marshal_VOID__STRING,
130 im_context_signals[RETRIEVE_SURROUNDING] =
131 g_signal_new ("retrieve_surrounding",
132 G_TYPE_FROM_CLASS (klass),
134 G_STRUCT_OFFSET (GtkIMContextClass, retrieve_surrounding),
135 _gtk_boolean_handled_accumulator, NULL,
136 _gtk_marshal_BOOLEAN__VOID,
138 im_context_signals[DELETE_SURROUNDING] =
139 g_signal_new ("delete_surrounding",
140 G_TYPE_FROM_CLASS (klass),
142 G_STRUCT_OFFSET (GtkIMContextClass, delete_surrounding),
143 _gtk_boolean_handled_accumulator, NULL,
144 _gtk_marshal_BOOLEAN__INT_INT,
151 gtk_im_context_init (GtkIMContext *im_context)
156 gtk_im_context_real_get_preedit_string (GtkIMContext *context,
158 PangoAttrList **attrs,
162 *str = g_strdup ("");
164 *attrs = pango_attr_list_new ();
170 gtk_im_context_real_filter_keypress (GtkIMContext *context,
183 gtk_im_context_real_set_surrounding (GtkIMContext *context,
188 SurroundingInfo *info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");
193 info->text = g_strndup (text, len);
194 info->cursor_index = cursor_index;
199 gtk_im_context_real_get_surrounding (GtkIMContext *context,
204 gboolean info_is_local = FALSE;
205 SurroundingInfo local_info = { NULL, 0 };
206 SurroundingInfo *info;
208 info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");
212 g_object_set_data (G_OBJECT (context), "gtk-im-surrounding-info", info);
213 info_is_local = TRUE;
216 g_signal_emit (context,
217 im_context_signals[RETRIEVE_SURROUNDING], 0,
222 *text = g_strdup (info->text ? info->text : "");
223 *cursor_index = info->cursor_index;
234 g_object_set_data (G_OBJECT (context), "gtk-im-surrounding-info", NULL);
241 * gtk_im_context_set_client_window:
242 * @context: a #GtkIMContext
243 * @window: the client window. This may be %NULL to indicate
244 * that the previous client window no longer exists.
246 * Set the client window for the input context; this is the
247 * #GdkWindow in which the input appears. This window is
248 * used in order to correctly position status windows, and may
249 * also be used for purposes internal to the input method.
252 gtk_im_context_set_client_window (GtkIMContext *context,
255 GtkIMContextClass *klass;
257 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
259 klass = GTK_IM_CONTEXT_GET_CLASS (context);
260 if (klass->set_client_window)
261 klass->set_client_window (context, window);
265 * gtk_im_context_get_preedit_string:
266 * @context: a #GtkIMContext
267 * @str: location to store the retrieved string. The
268 * string retrieved must be freed with g_free ().
269 * @attrs: location to store the retrieved attribute list.
270 * When you are done with this list, you must
271 * unreference it with pango_attr_list_unref().
272 * @cursor_pos: location to store position of cursor (in bytes)
273 * within the preedit string.
275 * Retrieve the current preedit string for the input context,
276 * and a list of attributes to apply to the string.
277 * This string should be displayed inserted at the insertion
281 gtk_im_context_get_preedit_string (GtkIMContext *context,
283 PangoAttrList **attrs,
286 GtkIMContextClass *klass;
288 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
290 klass = GTK_IM_CONTEXT_GET_CLASS (context);
291 klass->get_preedit_string (context, str, attrs, cursor_pos);
292 g_return_if_fail (str == NULL || g_utf8_validate (*str, -1, NULL));
296 * gtk_im_context_filter_keypress:
297 * @context: a #GtkIMContext
298 * @event: the key event
300 * Allow an input method to internally handle a key press event.
301 * If this function returns %TRUE, then no further processing
302 * should be done for this keystroke.
304 * Return value: %TRUE if the input method handled the keystroke.
308 gtk_im_context_filter_keypress (GtkIMContext *context,
311 GtkIMContextClass *klass;
313 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
314 g_return_val_if_fail (key != NULL, FALSE);
316 klass = GTK_IM_CONTEXT_GET_CLASS (context);
317 return klass->filter_keypress (context, key);
321 * gtk_im_context_focus_in:
322 * @context: a #GtkIMContext
324 * Notify the input method that the widget to which this
325 * input context corresponds has gained focus. The input method
326 * may, for example, change the displayed feedback to reflect
330 gtk_im_context_focus_in (GtkIMContext *context)
332 GtkIMContextClass *klass;
334 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
336 klass = GTK_IM_CONTEXT_GET_CLASS (context);
338 klass->focus_in (context);
342 * gtk_im_context_focus_out:
343 * @context: a #GtkIMContext
345 * Notify the input method that the widget to which this
346 * input context corresponds has lost focus. The input method
347 * may, for example, change the displayed feedback or reset the contexts
348 * state to reflect this change.
351 gtk_im_context_focus_out (GtkIMContext *context)
353 GtkIMContextClass *klass;
355 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
357 klass = GTK_IM_CONTEXT_GET_CLASS (context);
358 if (klass->focus_out)
359 klass->focus_out (context);
363 * gtk_im_context_reset:
364 * @context: a #GtkIMContext
366 * Notify the input method that a change such as a change in cursor
367 * position has been made. This will typically cause the input
368 * method to clear the preedit state.
371 gtk_im_context_reset (GtkIMContext *context)
373 GtkIMContextClass *klass;
375 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
377 klass = GTK_IM_CONTEXT_GET_CLASS (context);
379 klass->reset (context);
384 * gtk_im_context_set_cursor_location:
385 * @context: a #GtkIMContext
386 * @area: new location
388 * Notify the input method that a change in cursor
389 * position has been made. The location is relative to the client
393 gtk_im_context_set_cursor_location (GtkIMContext *context,
396 GtkIMContextClass *klass;
398 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
400 klass = GTK_IM_CONTEXT_GET_CLASS (context);
401 if (klass->set_cursor_location)
402 klass->set_cursor_location (context, area);
406 * gtk_im_context_set_use_preedit:
407 * @context: a #GtkIMContext
408 * @use_preedit: whether the IM context should use the preedit string.
410 * Sets whether the IM context should use the preedit string
411 * to display feedback. If @use_preedit is FALSE (default
412 * is TRUE), then the IM context may use some other method to display
413 * feedback, such as displaying it in a child of the root window.
416 gtk_im_context_set_use_preedit (GtkIMContext *context,
417 gboolean use_preedit)
419 GtkIMContextClass *klass;
421 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
423 klass = GTK_IM_CONTEXT_GET_CLASS (context);
424 if (klass->set_use_preedit)
425 klass->set_use_preedit (context, use_preedit);
429 * gtk_im_context_set_surrounding:
430 * @context: a #GtkIMContext
431 * @text: text surrounding the insertion point, as UTF-8.
432 * the preedit string should not be included within
434 * @len: the length of @text, or -1 if @text is nul-terminated
435 * @cursor_index: the byte index of the insertion cursor within @text.
437 * Sets surrounding context around the insertion point and preedit
438 * string. This function is expected to be called in response to the
439 * GtkIMContext::retrieve_surrounding signal, and will likely have no
440 * effect if called at other times.
443 gtk_im_context_set_surrounding (GtkIMContext *context,
448 GtkIMContextClass *klass;
450 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
451 g_return_if_fail (text != NULL || len == 0);
453 if (text == NULL && len == 0)
458 g_return_if_fail (cursor_index >= 0 && cursor_index <= len);
460 klass = GTK_IM_CONTEXT_GET_CLASS (context);
461 if (klass->set_surrounding)
462 klass->set_surrounding (context, text, len, cursor_index);
466 * gtk_im_context_get_surrounding:
467 * @context: a #GtkIMContext
468 * @text: location to store a UTF-8 encoded string of text
469 * holding context around the insertion point.
470 * If the function returns %TRUE, then you must free
471 * the result stored in this location with g_free().
472 * @cursor_index: location to store byte index of the insertion cursor
475 * Retrieves context around the insertion point. Input methods
476 * typically want context in order to constrain input text based on
477 * existing text; this is important for languages such as Thai where
478 * only some sequences of characters are allowed.
480 * This function is implemented by emitting the
481 * GtkIMContext::retrieve_surrounding signal on the input method; in
482 * response to this signal, a widget should provide as much context as
483 * is available, up to an entire paragraph, by calling
484 * gtk_im_context_set_surrounding(). Note that there is no obligation
485 * for a widget to respond to the ::retrieve_surrounding signal, so input
486 * methods must be prepared to function without context.
488 * Return value: %TRUE if surrounding text was provided; in this case
489 * you must free the result stored in *text.
492 gtk_im_context_get_surrounding (GtkIMContext *context,
496 GtkIMContextClass *klass;
497 gchar *local_text = NULL;
499 gboolean result = FALSE;
501 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
503 klass = GTK_IM_CONTEXT_GET_CLASS (context);
504 if (klass->get_surrounding)
505 result = klass->get_surrounding (context,
506 text ? text : &local_text,
507 cursor_index ? cursor_index : &local_index);
516 * gtk_im_context_delete_surrounding:
517 * @context: a #GtkIMContext
518 * @offset: offset from cursor position in chars;
519 * a negative value means start before the cursor.
520 * @n_chars: number of characters to delete.
522 * Asks the widget that the input context is attached to to delete
523 * characters around the cursor position by emitting the
524 * GtkIMContext::delete_surrounding signal. Note that @offset and @n_chars
525 * are in characters not in bytes, which differs from the usage other
526 * places in #GtkIMContext.
528 * In order to use this function, you should first call
529 * gtk_im_context_get_surrounding() to get the current context, and
530 * call this function immediately afterwards to make sure that you
531 * know what you are deleting. You should also account for the fact
532 * that even if the signal was handled, the input context might not
533 * have deleted all the characters that were requested to be deleted.
535 * This function is used by an input method that wants to make
536 * subsitutions in the existing text in response to new input. It is
537 * not useful for applications.
539 * Return value: %TRUE if the signal was handled.
542 gtk_im_context_delete_surrounding (GtkIMContext *context,
548 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
550 g_signal_emit (context,
551 im_context_signals[DELETE_SURROUNDING], 0,
552 offset, n_chars, &result);