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.
20 #include "gtkimcontext.h"
21 #include "gtkmain.h" /* For _gtk_boolean_handled_accumulator */
22 #include "gtkmarshalers.h"
23 #include "gtksignal.h"
36 static guint im_context_signals[LAST_SIGNAL] = { 0 };
38 static void gtk_im_context_class_init (GtkIMContextClass *class);
39 static void gtk_im_context_init (GtkIMContext *im_context);
41 static void gtk_im_context_real_get_preedit_string (GtkIMContext *context,
43 PangoAttrList **attrs,
45 static gboolean gtk_im_context_real_filter_keypress (GtkIMContext *context,
47 static gboolean gtk_im_context_real_get_surrounding (GtkIMContext *context,
50 static void gtk_im_context_real_set_surrounding (GtkIMContext *context,
56 gtk_im_context_get_type (void)
58 static GtkType im_context_type = 0;
62 static const GTypeInfo im_context_info =
64 sizeof (GtkIMContextClass),
66 (GBaseFinalizeFunc) NULL,
67 (GClassInitFunc) gtk_im_context_class_init,
68 NULL, /* class_finalize */
69 NULL, /* class_data */
70 sizeof (GtkIMContext),
72 (GInstanceInitFunc) gtk_im_context_init,
75 im_context_type = g_type_register_static (G_TYPE_OBJECT,
80 return im_context_type;
84 gtk_im_context_class_init (GtkIMContextClass *klass)
86 GtkObjectClass *object_class;
88 object_class = (GtkObjectClass*) klass;
90 klass->get_preedit_string = gtk_im_context_real_get_preedit_string;
91 klass->filter_keypress = gtk_im_context_real_filter_keypress;
92 klass->get_surrounding = gtk_im_context_real_get_surrounding;
93 klass->set_surrounding = gtk_im_context_real_set_surrounding;
95 im_context_signals[PREEDIT_START] =
96 gtk_signal_new ("preedit_start",
98 GTK_CLASS_TYPE (object_class),
99 GTK_SIGNAL_OFFSET (GtkIMContextClass, preedit_start),
100 _gtk_marshal_VOID__VOID,
103 im_context_signals[PREEDIT_END] =
104 gtk_signal_new ("preedit_end",
106 GTK_CLASS_TYPE (object_class),
107 GTK_SIGNAL_OFFSET (GtkIMContextClass, preedit_end),
108 _gtk_marshal_VOID__VOID,
111 im_context_signals[PREEDIT_CHANGED] =
112 gtk_signal_new ("preedit_changed",
114 GTK_CLASS_TYPE (object_class),
115 GTK_SIGNAL_OFFSET (GtkIMContextClass, preedit_changed),
116 _gtk_marshal_VOID__VOID,
119 im_context_signals[COMMIT] =
120 gtk_signal_new ("commit",
122 GTK_CLASS_TYPE (object_class),
123 GTK_SIGNAL_OFFSET (GtkIMContextClass, commit),
124 _gtk_marshal_VOID__STRING,
128 im_context_signals[RETRIEVE_SURROUNDING] =
129 g_signal_new ("retrieve_surrounding",
130 GTK_CLASS_TYPE (object_class),
132 GTK_SIGNAL_OFFSET (GtkIMContextClass, retrieve_surrounding),
133 _gtk_boolean_handled_accumulator, NULL,
134 _gtk_marshal_BOOLEAN__VOID,
136 im_context_signals[DELETE_SURROUNDING] =
137 g_signal_new ("delete_surrounding",
138 GTK_CLASS_TYPE (object_class),
140 GTK_SIGNAL_OFFSET (GtkIMContextClass, delete_surrounding),
141 _gtk_boolean_handled_accumulator, NULL,
142 _gtk_marshal_BOOLEAN__INT_INT,
149 gtk_im_context_init (GtkIMContext *im_context)
154 gtk_im_context_real_get_preedit_string (GtkIMContext *context,
156 PangoAttrList **attrs,
160 *str = g_strdup ("");
162 *attrs = pango_attr_list_new ();
168 gtk_im_context_real_filter_keypress (GtkIMContext *context,
181 gtk_im_context_real_set_surrounding (GtkIMContext *context,
186 SurroundingInfo *info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");
191 info->text = g_strndup (text, len);
192 info->cursor_index = cursor_index;
197 gtk_im_context_real_get_surrounding (GtkIMContext *context,
202 gboolean info_is_local = FALSE;
203 SurroundingInfo local_info = { NULL, 0 };
204 SurroundingInfo *info;
206 info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");
210 g_object_set_data (G_OBJECT (context), "gtk-im-surrounding-info", info);
211 info_is_local = TRUE;
214 g_signal_emit (context,
215 im_context_signals[RETRIEVE_SURROUNDING], 0,
220 *text = g_strdup (info->text ? info->text : "");
221 *cursor_index = info->cursor_index;
232 g_object_set_data (G_OBJECT (context), "gtk-im-surrounding-info", NULL);
239 * gtk_im_context_set_client_window:
240 * @context: a #GtkIMContext
241 * @window: the client window. This may be %NULL to indicate
242 * that the previous client window no longer exists.
244 * Set the client window for the input context; this is the
245 * #GdkWindow in which the input appears. This window is
246 * used in order to correctly position status windows, and may
247 * also be used for purposes internal to the input method.
250 gtk_im_context_set_client_window (GtkIMContext *context,
253 GtkIMContextClass *klass;
255 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
257 klass = GTK_IM_CONTEXT_GET_CLASS (context);
258 if (klass->set_client_window)
259 klass->set_client_window (context, window);
263 * gtk_im_context_get_preedit_string:
264 * @context: a #GtkIMContext
265 * @str: location to store the retrieved string. The
266 * string retrieved must be freed with g_free ().
267 * @attrs: location to store the retrieved attribute list.
268 * When you are done with this list, you must
269 * unreference it with pango_attr_list_unref().
270 * @cursor_pos: location to store position of cursor (in bytes)
271 * within the preedit string.
273 * Retrieve the current preedit string for the input context,
274 * and a list of attributes to apply to the string.
275 * This string should be displayed inserted at the insertion
279 gtk_im_context_get_preedit_string (GtkIMContext *context,
281 PangoAttrList **attrs,
284 GtkIMContextClass *klass;
286 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
288 klass = GTK_IM_CONTEXT_GET_CLASS (context);
289 klass->get_preedit_string (context, str, attrs, cursor_pos);
290 g_return_if_fail (str == NULL || g_utf8_validate (*str, -1, NULL));
294 * gtk_im_context_filter_keypress:
295 * @context: a #GtkIMContext
296 * @event: the key event
298 * Allow an input method to internally handle a key press event.
299 * If this function returns %TRUE, then no further processing
300 * should be done for this keystroke.
302 * Return value: %TRUE if the input method handled the keystroke.
306 gtk_im_context_filter_keypress (GtkIMContext *context,
309 GtkIMContextClass *klass;
311 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
312 g_return_val_if_fail (key != NULL, FALSE);
314 klass = GTK_IM_CONTEXT_GET_CLASS (context);
315 return klass->filter_keypress (context, key);
319 * gtk_im_context_focus_in:
320 * @context: a #GtkIMContext
322 * Notify the input method that the widget to which this
323 * input context corresponds has lost gained. The input method
324 * may, for example, change the displayed feedback to reflect
328 gtk_im_context_focus_in (GtkIMContext *context)
330 GtkIMContextClass *klass;
332 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
334 klass = GTK_IM_CONTEXT_GET_CLASS (context);
336 klass->focus_in (context);
340 * gtk_im_context_focus_out:
341 * @context: a #GtkIMContext
343 * Notify the input method that the widget to which this
344 * input context corresponds has lost focus. The input method
345 * may, for example, change the displayed feedback or reset the contexts
346 * state to reflect this change.
349 gtk_im_context_focus_out (GtkIMContext *context)
351 GtkIMContextClass *klass;
353 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
355 klass = GTK_IM_CONTEXT_GET_CLASS (context);
356 if (klass->focus_out)
357 klass->focus_out (context);
361 * gtk_im_context_reset:
362 * @context: a #GtkIMContext
364 * Notify the input method that a change such as a change in cursor
365 * position has been made. This will typically cause the input
366 * method to clear the preedit state.
369 gtk_im_context_reset (GtkIMContext *context)
371 GtkIMContextClass *klass;
373 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
375 klass = GTK_IM_CONTEXT_GET_CLASS (context);
377 klass->reset (context);
382 * gtk_im_context_set_cursor_location:
383 * @context: a #GtkIMContext
384 * @area: new location
386 * Notify the input method that a change in cursor
387 * position has been made.
390 gtk_im_context_set_cursor_location (GtkIMContext *context,
393 GtkIMContextClass *klass;
395 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
397 klass = GTK_IM_CONTEXT_GET_CLASS (context);
398 if (klass->set_cursor_location)
399 klass->set_cursor_location (context, area);
403 * gtk_im_context_set_use_preedit:
404 * @context: a #GtkIMContext
405 * @use_preedit: whether the IM context should use the preedit string.
407 * Sets whether the IM context should use the preedit string
408 * to display feedback. If @use_preedit is FALSE (default
409 * is TRUE), then the IM context may use some other method to display
410 * feedback, such as displaying it in a child of the root window.
413 gtk_im_context_set_use_preedit (GtkIMContext *context,
414 gboolean use_preedit)
416 GtkIMContextClass *klass;
418 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
420 klass = GTK_IM_CONTEXT_GET_CLASS (context);
421 if (klass->set_use_preedit)
422 klass->set_use_preedit (context, use_preedit);
426 * gtk_im_context_set_surrounding:
427 * @context: a #GtkIMContext
428 * @text: text surrounding the insertion point, as UTF-8.
429 * the preedit string should not be included within
431 * @len: the length of @text, or -1 if @text is nul-terminated
432 * @cursor_index: the byte index of the insertion cursor within @text.
434 * Sets surrounding context around the insertion point and preedit
435 * string. This function is expected to be called in response to the
436 * GtkIMContext::retrieve_context signal, and will likely have no
437 * effect if called at other times.
440 gtk_im_context_set_surrounding (GtkIMContext *context,
445 GtkIMContextClass *klass;
447 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
448 g_return_if_fail (text != NULL || len == 0);
450 if (text == NULL && len == 0)
455 g_return_if_fail (cursor_index >= 0 && cursor_index <= len);
457 klass = GTK_IM_CONTEXT_GET_CLASS (context);
458 if (klass->set_surrounding)
459 klass->set_surrounding (context, text, len, cursor_index);
463 * gtk_im_context_get_surrounding:
464 * @context: a #GtkIMContext
465 * @text: location to store a UTF-8 encoded string of text
466 * holding context around the insertion point.
467 * If the function returns %TRUE, then you must free
468 * the result stored in this location with g_free().
469 * @cursor_index: location to store byte index of the insertion cursor
472 * Retrieves context around the insertion point. Input methods
473 * typically want context in order to constrain input text based on
474 * existing text; this is important for languages such as Thai where
475 * only some sequences of characters are allowed.
477 * This function is implemented by emitting the
478 * GtkIMContext::retrieve_context signal on the input method; in
479 * response to this signal, a widget should provide as much context as
480 * is available, up to an entire paragraph, by calling
481 * gtk_im_context_set_surrounding(). Note that there is no obligation
482 * for a widget to respond to the ::retrieve_context signal, so input
483 * methods must be prepared to function without context.
485 * Return value: %TRUE if surrounding text was provided; in this case
486 * you must free the result stored in *text.
489 gtk_im_context_get_surrounding (GtkIMContext *context,
493 GtkIMContextClass *klass;
494 gchar *local_text = NULL;
496 gboolean result = FALSE;
498 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
500 klass = GTK_IM_CONTEXT_GET_CLASS (context);
501 if (klass->get_surrounding)
502 result = klass->get_surrounding (context,
503 text ? text : &local_text,
504 cursor_index ? cursor_index : &local_index);
513 * gtk_im_context_delete_surrounding:
514 * @context: a #GtkIMContext
515 * @offset: offset from cursor position in chars;
516 * a negative value means start before the cursor.
517 * @n_chars: number of characters to delete.
519 * Asks the widget that the input context is attached to to delete
520 * characters around the cursor position by emitting the
521 * GtkIMContext::delete_context signal. Note that @offset and @n_chars
522 * are in characters not in bytes, which differs from the usage other
523 * places in #GtkIMContext.
525 * In order to use this function, you should first call
526 * gtk_im_context_get_surrounding() to get the current context, and
527 * call this function immediately afterwards to make sure that you
528 * know what you are deleting. You should also account for the fact
529 * that even if the signal was handled, the input context might not
530 * have deleted all the characters that were requested to be deleted.
532 * This function is used by an input method that wants to make
533 * subsitutions in the existing text in response to new input. It is
534 * not useful for applications.
536 * Return value: %TRUE if the signal was handled.
539 gtk_im_context_delete_surrounding (GtkIMContext *context,
545 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
547 g_signal_emit (context,
548 im_context_signals[DELETE_SURROUNDING], 0,
549 offset, n_chars, &result);