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 #define GTK_DISABLE_DEPRECATED
22 #include "gtkimcontext.h"
23 #include "gtkmain.h" /* For _gtk_boolean_handled_accumulator */
24 #include "gtkmarshalers.h"
25 #include "gtksignal.h"
38 static guint im_context_signals[LAST_SIGNAL] = { 0 };
40 static void gtk_im_context_class_init (GtkIMContextClass *class);
41 static void gtk_im_context_init (GtkIMContext *im_context);
43 static void gtk_im_context_real_get_preedit_string (GtkIMContext *context,
45 PangoAttrList **attrs,
47 static gboolean gtk_im_context_real_filter_keypress (GtkIMContext *context,
49 static gboolean gtk_im_context_real_get_surrounding (GtkIMContext *context,
52 static void gtk_im_context_real_set_surrounding (GtkIMContext *context,
58 gtk_im_context_get_type (void)
60 static GtkType im_context_type = 0;
64 static const GTypeInfo im_context_info =
66 sizeof (GtkIMContextClass),
68 (GBaseFinalizeFunc) NULL,
69 (GClassInitFunc) gtk_im_context_class_init,
70 NULL, /* class_finalize */
71 NULL, /* class_data */
72 sizeof (GtkIMContext),
74 (GInstanceInitFunc) gtk_im_context_init,
75 NULL /* value_table */
78 im_context_type = 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 GtkObjectClass *object_class;
90 object_class = (GtkObjectClass*) klass;
92 klass->get_preedit_string = gtk_im_context_real_get_preedit_string;
93 klass->filter_keypress = gtk_im_context_real_filter_keypress;
94 klass->get_surrounding = gtk_im_context_real_get_surrounding;
95 klass->set_surrounding = gtk_im_context_real_set_surrounding;
97 im_context_signals[PREEDIT_START] =
98 g_signal_new ("preedit_start",
99 G_TYPE_FROM_CLASS (object_class),
101 G_STRUCT_OFFSET (GtkIMContextClass, preedit_start),
103 _gtk_marshal_VOID__VOID,
106 im_context_signals[PREEDIT_END] =
107 g_signal_new ("preedit_end",
108 G_TYPE_FROM_CLASS (object_class),
110 G_STRUCT_OFFSET (GtkIMContextClass, preedit_end),
112 _gtk_marshal_VOID__VOID,
115 im_context_signals[PREEDIT_CHANGED] =
116 g_signal_new ("preedit_changed",
117 G_TYPE_FROM_CLASS (object_class),
119 G_STRUCT_OFFSET (GtkIMContextClass, preedit_changed),
121 _gtk_marshal_VOID__VOID,
124 im_context_signals[COMMIT] =
125 g_signal_new ("commit",
126 G_TYPE_FROM_CLASS (object_class),
128 G_STRUCT_OFFSET (GtkIMContextClass, commit),
130 _gtk_marshal_VOID__STRING,
134 im_context_signals[RETRIEVE_SURROUNDING] =
135 g_signal_new ("retrieve_surrounding",
136 G_TYPE_FROM_CLASS (object_class),
138 G_STRUCT_OFFSET (GtkIMContextClass, retrieve_surrounding),
139 _gtk_boolean_handled_accumulator, NULL,
140 _gtk_marshal_BOOLEAN__VOID,
142 im_context_signals[DELETE_SURROUNDING] =
143 g_signal_new ("delete_surrounding",
144 G_TYPE_FROM_CLASS (object_class),
146 G_STRUCT_OFFSET (GtkIMContextClass, delete_surrounding),
147 _gtk_boolean_handled_accumulator, NULL,
148 _gtk_marshal_BOOLEAN__INT_INT,
155 gtk_im_context_init (GtkIMContext *im_context)
160 gtk_im_context_real_get_preedit_string (GtkIMContext *context,
162 PangoAttrList **attrs,
166 *str = g_strdup ("");
168 *attrs = pango_attr_list_new ();
174 gtk_im_context_real_filter_keypress (GtkIMContext *context,
187 gtk_im_context_real_set_surrounding (GtkIMContext *context,
192 SurroundingInfo *info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");
197 info->text = g_strndup (text, len);
198 info->cursor_index = cursor_index;
203 gtk_im_context_real_get_surrounding (GtkIMContext *context,
208 gboolean info_is_local = FALSE;
209 SurroundingInfo local_info = { NULL, 0 };
210 SurroundingInfo *info;
212 info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");
216 g_object_set_data (G_OBJECT (context), "gtk-im-surrounding-info", info);
217 info_is_local = TRUE;
220 g_signal_emit (context,
221 im_context_signals[RETRIEVE_SURROUNDING], 0,
226 *text = g_strdup (info->text ? info->text : "");
227 *cursor_index = info->cursor_index;
238 g_object_set_data (G_OBJECT (context), "gtk-im-surrounding-info", NULL);
245 * gtk_im_context_set_client_window:
246 * @context: a #GtkIMContext
247 * @window: the client window. This may be %NULL to indicate
248 * that the previous client window no longer exists.
250 * Set the client window for the input context; this is the
251 * #GdkWindow in which the input appears. This window is
252 * used in order to correctly position status windows, and may
253 * also be used for purposes internal to the input method.
256 gtk_im_context_set_client_window (GtkIMContext *context,
259 GtkIMContextClass *klass;
261 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
263 klass = GTK_IM_CONTEXT_GET_CLASS (context);
264 if (klass->set_client_window)
265 klass->set_client_window (context, window);
269 * gtk_im_context_get_preedit_string:
270 * @context: a #GtkIMContext
271 * @str: location to store the retrieved string. The
272 * string retrieved must be freed with g_free ().
273 * @attrs: location to store the retrieved attribute list.
274 * When you are done with this list, you must
275 * unreference it with pango_attr_list_unref().
276 * @cursor_pos: location to store position of cursor (in bytes)
277 * within the preedit string.
279 * Retrieve the current preedit string for the input context,
280 * and a list of attributes to apply to the string.
281 * This string should be displayed inserted at the insertion
285 gtk_im_context_get_preedit_string (GtkIMContext *context,
287 PangoAttrList **attrs,
290 GtkIMContextClass *klass;
292 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
294 klass = GTK_IM_CONTEXT_GET_CLASS (context);
295 klass->get_preedit_string (context, str, attrs, cursor_pos);
296 g_return_if_fail (str == NULL || g_utf8_validate (*str, -1, NULL));
300 * gtk_im_context_filter_keypress:
301 * @context: a #GtkIMContext
302 * @event: the key event
304 * Allow an input method to internally handle a key press event.
305 * If this function returns %TRUE, then no further processing
306 * should be done for this keystroke.
308 * Return value: %TRUE if the input method handled the keystroke.
312 gtk_im_context_filter_keypress (GtkIMContext *context,
315 GtkIMContextClass *klass;
317 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
318 g_return_val_if_fail (key != NULL, FALSE);
320 klass = GTK_IM_CONTEXT_GET_CLASS (context);
321 return klass->filter_keypress (context, key);
325 * gtk_im_context_focus_in:
326 * @context: a #GtkIMContext
328 * Notify the input method that the widget to which this
329 * input context corresponds has lost gained. The input method
330 * may, for example, change the displayed feedback to reflect
334 gtk_im_context_focus_in (GtkIMContext *context)
336 GtkIMContextClass *klass;
338 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
340 klass = GTK_IM_CONTEXT_GET_CLASS (context);
342 klass->focus_in (context);
346 * gtk_im_context_focus_out:
347 * @context: a #GtkIMContext
349 * Notify the input method that the widget to which this
350 * input context corresponds has lost focus. The input method
351 * may, for example, change the displayed feedback or reset the contexts
352 * state to reflect this change.
355 gtk_im_context_focus_out (GtkIMContext *context)
357 GtkIMContextClass *klass;
359 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
361 klass = GTK_IM_CONTEXT_GET_CLASS (context);
362 if (klass->focus_out)
363 klass->focus_out (context);
367 * gtk_im_context_reset:
368 * @context: a #GtkIMContext
370 * Notify the input method that a change such as a change in cursor
371 * position has been made. This will typically cause the input
372 * method to clear the preedit state.
375 gtk_im_context_reset (GtkIMContext *context)
377 GtkIMContextClass *klass;
379 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
381 klass = GTK_IM_CONTEXT_GET_CLASS (context);
383 klass->reset (context);
388 * gtk_im_context_set_cursor_location:
389 * @context: a #GtkIMContext
390 * @area: new location
392 * Notify the input method that a change in cursor
393 * position has been made.
396 gtk_im_context_set_cursor_location (GtkIMContext *context,
399 GtkIMContextClass *klass;
401 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
403 klass = GTK_IM_CONTEXT_GET_CLASS (context);
404 if (klass->set_cursor_location)
405 klass->set_cursor_location (context, area);
409 * gtk_im_context_set_use_preedit:
410 * @context: a #GtkIMContext
411 * @use_preedit: whether the IM context should use the preedit string.
413 * Sets whether the IM context should use the preedit string
414 * to display feedback. If @use_preedit is FALSE (default
415 * is TRUE), then the IM context may use some other method to display
416 * feedback, such as displaying it in a child of the root window.
419 gtk_im_context_set_use_preedit (GtkIMContext *context,
420 gboolean use_preedit)
422 GtkIMContextClass *klass;
424 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
426 klass = GTK_IM_CONTEXT_GET_CLASS (context);
427 if (klass->set_use_preedit)
428 klass->set_use_preedit (context, use_preedit);
432 * gtk_im_context_set_surrounding:
433 * @context: a #GtkIMContext
434 * @text: text surrounding the insertion point, as UTF-8.
435 * the preedit string should not be included within
437 * @len: the length of @text, or -1 if @text is nul-terminated
438 * @cursor_index: the byte index of the insertion cursor within @text.
440 * Sets surrounding context around the insertion point and preedit
441 * string. This function is expected to be called in response to the
442 * GtkIMContext::retrieve_context signal, and will likely have no
443 * effect if called at other times.
446 gtk_im_context_set_surrounding (GtkIMContext *context,
451 GtkIMContextClass *klass;
453 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
454 g_return_if_fail (text != NULL || len == 0);
456 if (text == NULL && len == 0)
461 g_return_if_fail (cursor_index >= 0 && cursor_index <= len);
463 klass = GTK_IM_CONTEXT_GET_CLASS (context);
464 if (klass->set_surrounding)
465 klass->set_surrounding (context, text, len, cursor_index);
469 * gtk_im_context_get_surrounding:
470 * @context: a #GtkIMContext
471 * @text: location to store a UTF-8 encoded string of text
472 * holding context around the insertion point.
473 * If the function returns %TRUE, then you must free
474 * the result stored in this location with g_free().
475 * @cursor_index: location to store byte index of the insertion cursor
478 * Retrieves context around the insertion point. Input methods
479 * typically want context in order to constrain input text based on
480 * existing text; this is important for languages such as Thai where
481 * only some sequences of characters are allowed.
483 * This function is implemented by emitting the
484 * GtkIMContext::retrieve_context signal on the input method; in
485 * response to this signal, a widget should provide as much context as
486 * is available, up to an entire paragraph, by calling
487 * gtk_im_context_set_surrounding(). Note that there is no obligation
488 * for a widget to respond to the ::retrieve_context signal, so input
489 * methods must be prepared to function without context.
491 * Return value: %TRUE if surrounding text was provided; in this case
492 * you must free the result stored in *text.
495 gtk_im_context_get_surrounding (GtkIMContext *context,
499 GtkIMContextClass *klass;
500 gchar *local_text = NULL;
502 gboolean result = FALSE;
504 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
506 klass = GTK_IM_CONTEXT_GET_CLASS (context);
507 if (klass->get_surrounding)
508 result = klass->get_surrounding (context,
509 text ? text : &local_text,
510 cursor_index ? cursor_index : &local_index);
519 * gtk_im_context_delete_surrounding:
520 * @context: a #GtkIMContext
521 * @offset: offset from cursor position in chars;
522 * a negative value means start before the cursor.
523 * @n_chars: number of characters to delete.
525 * Asks the widget that the input context is attached to to delete
526 * characters around the cursor position by emitting the
527 * GtkIMContext::delete_context signal. Note that @offset and @n_chars
528 * are in characters not in bytes, which differs from the usage other
529 * places in #GtkIMContext.
531 * In order to use this function, you should first call
532 * gtk_im_context_get_surrounding() to get the current context, and
533 * call this function immediately afterwards to make sure that you
534 * know what you are deleting. You should also account for the fact
535 * that even if the signal was handled, the input context might not
536 * have deleted all the characters that were requested to be deleted.
538 * This function is used by an input method that wants to make
539 * subsitutions in the existing text in response to new input. It is
540 * not useful for applications.
542 * Return value: %TRUE if the signal was handled.
545 gtk_im_context_delete_surrounding (GtkIMContext *context,
551 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
553 g_signal_emit (context,
554 im_context_signals[DELETE_SURROUNDING], 0,
555 offset, n_chars, &result);