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. The location is relative to the client
397 gtk_im_context_set_cursor_location (GtkIMContext *context,
400 GtkIMContextClass *klass;
402 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
404 klass = GTK_IM_CONTEXT_GET_CLASS (context);
405 if (klass->set_cursor_location)
406 klass->set_cursor_location (context, area);
410 * gtk_im_context_set_use_preedit:
411 * @context: a #GtkIMContext
412 * @use_preedit: whether the IM context should use the preedit string.
414 * Sets whether the IM context should use the preedit string
415 * to display feedback. If @use_preedit is FALSE (default
416 * is TRUE), then the IM context may use some other method to display
417 * feedback, such as displaying it in a child of the root window.
420 gtk_im_context_set_use_preedit (GtkIMContext *context,
421 gboolean use_preedit)
423 GtkIMContextClass *klass;
425 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
427 klass = GTK_IM_CONTEXT_GET_CLASS (context);
428 if (klass->set_use_preedit)
429 klass->set_use_preedit (context, use_preedit);
433 * gtk_im_context_set_surrounding:
434 * @context: a #GtkIMContext
435 * @text: text surrounding the insertion point, as UTF-8.
436 * the preedit string should not be included within
438 * @len: the length of @text, or -1 if @text is nul-terminated
439 * @cursor_index: the byte index of the insertion cursor within @text.
441 * Sets surrounding context around the insertion point and preedit
442 * string. This function is expected to be called in response to the
443 * GtkIMContext::retrieve_context signal, and will likely have no
444 * effect if called at other times.
447 gtk_im_context_set_surrounding (GtkIMContext *context,
452 GtkIMContextClass *klass;
454 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
455 g_return_if_fail (text != NULL || len == 0);
457 if (text == NULL && len == 0)
462 g_return_if_fail (cursor_index >= 0 && cursor_index <= len);
464 klass = GTK_IM_CONTEXT_GET_CLASS (context);
465 if (klass->set_surrounding)
466 klass->set_surrounding (context, text, len, cursor_index);
470 * gtk_im_context_get_surrounding:
471 * @context: a #GtkIMContext
472 * @text: location to store a UTF-8 encoded string of text
473 * holding context around the insertion point.
474 * If the function returns %TRUE, then you must free
475 * the result stored in this location with g_free().
476 * @cursor_index: location to store byte index of the insertion cursor
479 * Retrieves context around the insertion point. Input methods
480 * typically want context in order to constrain input text based on
481 * existing text; this is important for languages such as Thai where
482 * only some sequences of characters are allowed.
484 * This function is implemented by emitting the
485 * GtkIMContext::retrieve_context signal on the input method; in
486 * response to this signal, a widget should provide as much context as
487 * is available, up to an entire paragraph, by calling
488 * gtk_im_context_set_surrounding(). Note that there is no obligation
489 * for a widget to respond to the ::retrieve_context signal, so input
490 * methods must be prepared to function without context.
492 * Return value: %TRUE if surrounding text was provided; in this case
493 * you must free the result stored in *text.
496 gtk_im_context_get_surrounding (GtkIMContext *context,
500 GtkIMContextClass *klass;
501 gchar *local_text = NULL;
503 gboolean result = FALSE;
505 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
507 klass = GTK_IM_CONTEXT_GET_CLASS (context);
508 if (klass->get_surrounding)
509 result = klass->get_surrounding (context,
510 text ? text : &local_text,
511 cursor_index ? cursor_index : &local_index);
520 * gtk_im_context_delete_surrounding:
521 * @context: a #GtkIMContext
522 * @offset: offset from cursor position in chars;
523 * a negative value means start before the cursor.
524 * @n_chars: number of characters to delete.
526 * Asks the widget that the input context is attached to to delete
527 * characters around the cursor position by emitting the
528 * GtkIMContext::delete_context signal. Note that @offset and @n_chars
529 * are in characters not in bytes, which differs from the usage other
530 * places in #GtkIMContext.
532 * In order to use this function, you should first call
533 * gtk_im_context_get_surrounding() to get the current context, and
534 * call this function immediately afterwards to make sure that you
535 * know what you are deleting. You should also account for the fact
536 * that even if the signal was handled, the input context might not
537 * have deleted all the characters that were requested to be deleted.
539 * This function is used by an input method that wants to make
540 * subsitutions in the existing text in response to new input. It is
541 * not useful for applications.
543 * Return value: %TRUE if the signal was handled.
546 gtk_im_context_delete_surrounding (GtkIMContext *context,
552 g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
554 g_signal_emit (context,
555 im_context_signals[DELETE_SURROUNDING], 0,
556 offset, n_chars, &result);