gtkimcontext.c 16.1 KB
Newer Older
1 2 3 4
/* GTK - The GIMP Toolkit
 * Copyright (C) 2000 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17 18 19
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
#include <config.h>
Manish Singh's avatar
Manish Singh committed
21
#include <string.h>
22
#include "gtkimcontext.h"
23
#include "gtkmain.h"		/* For _gtk_boolean_handled_accumulator */
24
#include "gtkmarshalers.h"
25
#include "gtkintl.h"
26
#include "gtkalias.h"
27 28 29 30 31 32

enum {
  PREEDIT_START,
  PREEDIT_END,
  PREEDIT_CHANGED,
  COMMIT,
33 34
  RETRIEVE_SURROUNDING,
  DELETE_SURROUNDING,
35 36 37 38 39 40 41 42
  LAST_SIGNAL
};

static guint im_context_signals[LAST_SIGNAL] = { 0 };

static void gtk_im_context_class_init (GtkIMContextClass *class);
static void gtk_im_context_init (GtkIMContext *im_context);

43 44 45 46 47 48 49 50 51 52 53 54 55
static void     gtk_im_context_real_get_preedit_string (GtkIMContext   *context,
							gchar         **str,
							PangoAttrList **attrs,
							gint           *cursor_pos);
static gboolean gtk_im_context_real_filter_keypress    (GtkIMContext   *context,
							GdkEventKey    *event);
static gboolean gtk_im_context_real_get_surrounding    (GtkIMContext   *context,
							gchar         **text,
							gint           *cursor_index);
static void     gtk_im_context_real_set_surrounding    (GtkIMContext   *context,
							const char     *text,
							gint            len,
							gint            cursor_index);
56

Manish Singh's avatar
Manish Singh committed
57
GType
58 59
gtk_im_context_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
60
  static GType im_context_type = 0;
61 62 63

  if (!im_context_type)
    {
64
      static const GTypeInfo im_context_info =
65
      {
66 67 68 69
        sizeof (GtkIMContextClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gtk_im_context_class_init,
Manish Singh's avatar
Manish Singh committed
70 71
        NULL,		/* class_finalize */
        NULL,		/* class_data */
72
        sizeof (GtkIMContext),
Manish Singh's avatar
Manish Singh committed
73
        0,		/* n_preallocs */
74
        (GInstanceInitFunc) gtk_im_context_init,
Manish Singh's avatar
Manish Singh committed
75
        NULL,		/* value_table */
76
      };
77
      
Manish Singh's avatar
Manish Singh committed
78
      im_context_type =
79
	g_type_register_static (G_TYPE_OBJECT, I_("GtkIMContext"),
Manish Singh's avatar
Manish Singh committed
80
				&im_context_info, G_TYPE_FLAG_ABSTRACT);
81 82 83 84 85 86 87 88
    }

  return im_context_type;
}

static void
gtk_im_context_class_init (GtkIMContextClass *klass)
{
89 90
  klass->get_preedit_string = gtk_im_context_real_get_preedit_string;
  klass->filter_keypress = gtk_im_context_real_filter_keypress;
91 92
  klass->get_surrounding = gtk_im_context_real_get_surrounding;
  klass->set_surrounding = gtk_im_context_real_set_surrounding;
93

94
  im_context_signals[PREEDIT_START] =
95
    g_signal_new (I_("preedit_start"),
Manish Singh's avatar
Manish Singh committed
96
		  G_TYPE_FROM_CLASS (klass),
97 98 99 100 101
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkIMContextClass, preedit_start),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
102 103
  
  im_context_signals[PREEDIT_END] =
104
    g_signal_new (I_("preedit_end"),
Manish Singh's avatar
Manish Singh committed
105
		  G_TYPE_FROM_CLASS (klass),
106 107 108 109 110
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkIMContextClass, preedit_end),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
111 112
  
  im_context_signals[PREEDIT_CHANGED] =
113
    g_signal_new (I_("preedit_changed"),
Manish Singh's avatar
Manish Singh committed
114
		  G_TYPE_FROM_CLASS (klass),
115 116 117 118 119
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkIMContextClass, preedit_changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
120 121
  
  im_context_signals[COMMIT] =
122
    g_signal_new (I_("commit"),
Manish Singh's avatar
Manish Singh committed
123
		  G_TYPE_FROM_CLASS (klass),
124 125 126 127 128 129
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkIMContextClass, commit),
		  NULL, NULL,
		  _gtk_marshal_VOID__STRING,
		  G_TYPE_NONE, 1,
		  G_TYPE_STRING);
130 131

  im_context_signals[RETRIEVE_SURROUNDING] =
132
    g_signal_new (I_("retrieve_surrounding"),
Manish Singh's avatar
Manish Singh committed
133
                  G_TYPE_FROM_CLASS (klass),
134 135
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkIMContextClass, retrieve_surrounding),
136
                  _gtk_boolean_handled_accumulator, NULL,
137
                  _gtk_marshal_BOOLEAN__VOID,
138
                  G_TYPE_BOOLEAN, 0);
139
  im_context_signals[DELETE_SURROUNDING] =
140
    g_signal_new (I_("delete_surrounding"),
Manish Singh's avatar
Manish Singh committed
141
                  G_TYPE_FROM_CLASS (klass),
142 143
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkIMContextClass, delete_surrounding),
144
                  _gtk_boolean_handled_accumulator, NULL,
145
                  _gtk_marshal_BOOLEAN__INT_INT,
146 147 148
                  G_TYPE_BOOLEAN, 2,
                  G_TYPE_INT,
		  G_TYPE_INT);
149 150 151 152 153 154 155 156 157 158
}

static void
gtk_im_context_init (GtkIMContext *im_context)
{
}

static void
gtk_im_context_real_get_preedit_string (GtkIMContext       *context,
					gchar             **str,
Owen Taylor's avatar
Owen Taylor committed
159 160
					PangoAttrList     **attrs,
					gint               *cursor_pos)
161 162 163 164 165
{
  if (str)
    *str = g_strdup ("");
  if (attrs)
    *attrs = pango_attr_list_new ();
Owen Taylor's avatar
Owen Taylor committed
166 167
  if (cursor_pos)
    *cursor_pos = 0;
168 169 170 171 172 173 174 175 176
}

static gboolean
gtk_im_context_real_filter_keypress (GtkIMContext       *context,
				     GdkEventKey        *event)
{
  return FALSE;
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
typedef struct
{
  gchar *text;
  gint cursor_index;
} SurroundingInfo;

static void
gtk_im_context_real_set_surrounding (GtkIMContext  *context,
				     const gchar   *text,
				     gint           len,
				     gint           cursor_index)
{
  SurroundingInfo *info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");

  if (info)
    {
      g_free (info->text);
      info->text = g_strndup (text, len);
      info->cursor_index = cursor_index;
    }
}

static gboolean
gtk_im_context_real_get_surrounding (GtkIMContext *context,
				     gchar       **text,
				     gint         *cursor_index)
{
  gboolean result;
  gboolean info_is_local = FALSE;
  SurroundingInfo local_info = { NULL, 0 };
  SurroundingInfo *info;
  
  info = g_object_get_data (G_OBJECT (context), "gtk-im-surrounding-info");
  if (!info)
    {
      info = &local_info;
213
      g_object_set_data (G_OBJECT (context), I_("gtk-im-surrounding-info"), info);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
      info_is_local = TRUE;
    }
  
  g_signal_emit (context,
		 im_context_signals[RETRIEVE_SURROUNDING], 0,
		 &result);

  if (result)
    {
      *text = g_strdup (info->text ? info->text : "");
      *cursor_index = info->cursor_index;
    }
  else
    {
      *text = NULL;
      *cursor_index = 0;
    }

  if (info_is_local)
233 234
    {
      g_free (info->text);
235
      g_object_set_data (G_OBJECT (context), I_("gtk-im-surrounding-info"), NULL);
236
    }
237 238 239 240
  
  return result;
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
/**
 * gtk_im_context_set_client_window:
 * @context: a #GtkIMContext
 * @window:  the client window. This may be %NULL to indicate
 *           that the previous client window no longer exists.
 * 
 * Set the client window for the input context; this is the
 * #GdkWindow in which the input appears. This window is
 * used in order to correctly position status windows, and may
 * also be used for purposes internal to the input method.
 **/
void
gtk_im_context_set_client_window (GtkIMContext *context,
				  GdkWindow    *window)
{
  GtkIMContextClass *klass;
  
  g_return_if_fail (GTK_IS_IM_CONTEXT (context));

  klass = GTK_IM_CONTEXT_GET_CLASS (context);
  if (klass->set_client_window)
    klass->set_client_window (context, window);
}

/**
 * gtk_im_context_get_preedit_string:
Owen Taylor's avatar
Owen Taylor committed
267 268 269 270 271 272
 * @context:    a #GtkIMContext
 * @str:        location to store the retrieved string. The
 *              string retrieved must be freed with g_free ().
 * @attrs:      location to store the retrieved attribute list.
 *              When you are done with this list, you must
 *              unreference it with pango_attr_list_unref().
273
 * @cursor_pos: location to store position of cursor (in characters)
Owen Taylor's avatar
Owen Taylor committed
274
 *              within the preedit string.  
275 276 277 278 279 280 281 282 283
 * 
 * Retrieve the current preedit string for the input context,
 * and a list of attributes to apply to the string.
 * This string should be displayed inserted at the insertion
 * point.
 **/
void
gtk_im_context_get_preedit_string (GtkIMContext   *context,
				   gchar         **str,
Owen Taylor's avatar
Owen Taylor committed
284 285
				   PangoAttrList **attrs,
				   gint           *cursor_pos)
286 287 288 289 290 291
{
  GtkIMContextClass *klass;
  
  g_return_if_fail (GTK_IS_IM_CONTEXT (context));
  
  klass = GTK_IM_CONTEXT_GET_CLASS (context);
Owen Taylor's avatar
Owen Taylor committed
292
  klass->get_preedit_string (context, str, attrs, cursor_pos);
293
  g_return_if_fail (str == NULL || g_utf8_validate (*str, -1, NULL));
294 295 296 297 298
}

/**
 * gtk_im_context_filter_keypress:
 * @context: a #GtkIMContext
299
 * @event: the key event
300 301
 * 
 * Allow an input method to internally handle a key press event.
302
 * If this function returns %TRUE, then no further processing
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
 * should be done for this keystroke.
 * 
 * Return value: %TRUE if the input method handled the keystroke.
 *
 **/
gboolean
gtk_im_context_filter_keypress (GtkIMContext *context,
				GdkEventKey  *key)
{
  GtkIMContextClass *klass;
  
  g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
  g_return_val_if_fail (key != NULL, FALSE);

  klass = GTK_IM_CONTEXT_GET_CLASS (context);
  return klass->filter_keypress (context, key);
}

/**
 * gtk_im_context_focus_in:
 * @context: a #GtkIMContext
 *
 * Notify the input method that the widget to which this
326
 * input context corresponds has gained focus. The input method
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
 * may, for example, change the displayed feedback to reflect
 * this change.
 **/
void
gtk_im_context_focus_in (GtkIMContext   *context)
{
  GtkIMContextClass *klass;
  
  g_return_if_fail (GTK_IS_IM_CONTEXT (context));
  
  klass = GTK_IM_CONTEXT_GET_CLASS (context);
  if (klass->focus_in)
    klass->focus_in (context);
}

/**
Owen Taylor's avatar
Owen Taylor committed
343
 * gtk_im_context_focus_out:
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
 * @context: a #GtkIMContext
 *
 * Notify the input method that the widget to which this
 * input context corresponds has lost focus. The input method
 * may, for example, change the displayed feedback or reset the contexts
 * state to reflect this change.
 **/
void
gtk_im_context_focus_out (GtkIMContext   *context)
{
  GtkIMContextClass *klass;
  
  g_return_if_fail (GTK_IS_IM_CONTEXT (context));

  klass = GTK_IM_CONTEXT_GET_CLASS (context);
  if (klass->focus_out)
    klass->focus_out (context);
}

Owen Taylor's avatar
Owen Taylor committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
/**
 * gtk_im_context_reset:
 * @context: a #GtkIMContext
 *
 * Notify the input method that a change such as a change in cursor
 * position has been made. This will typically cause the input
 * method to clear the preedit state.
 **/
void
gtk_im_context_reset (GtkIMContext   *context)
{
  GtkIMContextClass *klass;
  
  g_return_if_fail (GTK_IS_IM_CONTEXT (context));

  klass = GTK_IM_CONTEXT_GET_CLASS (context);
  if (klass->reset)
    klass->reset (context);
}

383

384
/**
385
 * gtk_im_context_set_cursor_location:
386
 * @context: a #GtkIMContext
Havoc Pennington's avatar
Havoc Pennington committed
387
 * @area: new location
388 389
 *
 * Notify the input method that a change in cursor 
390 391
 * position has been made. The location is relative to the client
 * window.
392 393
 **/
void
Havoc Pennington's avatar
Havoc Pennington committed
394 395
gtk_im_context_set_cursor_location (GtkIMContext *context,
				    GdkRectangle *area)
396 397 398 399 400 401
{
  GtkIMContextClass *klass;
  
  g_return_if_fail (GTK_IS_IM_CONTEXT (context));

  klass = GTK_IM_CONTEXT_GET_CLASS (context);
402 403
  if (klass->set_cursor_location)
    klass->set_cursor_location (context, area);
404 405
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
/**
 * gtk_im_context_set_use_preedit:
 * @context: a #GtkIMContext
 * @use_preedit: whether the IM context should use the preedit string.
 * 
 * Sets whether the IM context should use the preedit string
 * to display feedback. If @use_preedit is FALSE (default
 * is TRUE), then the IM context may use some other method to display
 * feedback, such as displaying it in a child of the root window.
 **/
void
gtk_im_context_set_use_preedit (GtkIMContext *context,
				gboolean      use_preedit)
{
  GtkIMContextClass *klass;
  
  g_return_if_fail (GTK_IS_IM_CONTEXT (context));

  klass = GTK_IM_CONTEXT_GET_CLASS (context);
  if (klass->set_use_preedit)
    klass->set_use_preedit (context, use_preedit);
}
428 429 430 431 432 433 434 435 436 437 438 439

/**
 * gtk_im_context_set_surrounding:
 * @context: a #GtkIMContext 
 * @text: text surrounding the insertion point, as UTF-8.
 *        the preedit string should not be included within
 *        @text.
 * @len: the length of @text, or -1 if @text is nul-terminated
 * @cursor_index: the byte index of the insertion cursor within @text.
 * 
 * Sets surrounding context around the insertion point and preedit
 * string. This function is expected to be called in response to the
440
 * GtkIMContext::retrieve_surrounding signal, and will likely have no
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
 * effect if called at other times.
 **/
void
gtk_im_context_set_surrounding (GtkIMContext  *context,
				const gchar   *text,
				gint           len,
				gint           cursor_index)
{
  GtkIMContextClass *klass;
  
  g_return_if_fail (GTK_IS_IM_CONTEXT (context));
  g_return_if_fail (text != NULL || len == 0);

  if (text == NULL && len == 0)
    text = "";
  if (len < 0)
    len = strlen (text);

  g_return_if_fail (cursor_index >= 0 && cursor_index <= len);

  klass = GTK_IM_CONTEXT_GET_CLASS (context);
  if (klass->set_surrounding)
    klass->set_surrounding (context, text, len, cursor_index);
}

/**
 * gtk_im_context_get_surrounding:
 * @context: a #GtkIMContext
 * @text: location to store a UTF-8 encoded string of text
 *        holding context around the insertion point.
 *        If the function returns %TRUE, then you must free
 *        the result stored in this location with g_free().
 * @cursor_index: location to store byte index of the insertion cursor
 *        within @text.
 * 
 * Retrieves context around the insertion point. Input methods
 * typically want context in order to constrain input text based on
 * existing text; this is important for languages such as Thai where
 * only some sequences of characters are allowed.
 *
 * This function is implemented by emitting the
482
 * GtkIMContext::retrieve_surrounding signal on the input method; in
483 484
 * response to this signal, a widget should provide as much context as
 * is available, up to an entire paragraph, by calling
485
 * gtk_im_context_set_surrounding(). Note that there is no obligation
486
 * for a widget to respond to the ::retrieve_surrounding signal, so input
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
 * methods must be prepared to function without context.
 *
 * Return value: %TRUE if surrounding text was provided; in this case
 *    you must free the result stored in *text.
 **/
gboolean
gtk_im_context_get_surrounding (GtkIMContext *context,
				gchar       **text,
				gint         *cursor_index)
{
  GtkIMContextClass *klass;
  gchar *local_text = NULL;
  gint local_index;
  gboolean result = FALSE;
  
  g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);

  klass = GTK_IM_CONTEXT_GET_CLASS (context);
  if (klass->get_surrounding)
    result = klass->get_surrounding (context,
				     text ? text : &local_text,
				     cursor_index ? cursor_index : &local_index);

  if (result)
    g_free (local_text);

  return result;
}

/**
 * gtk_im_context_delete_surrounding:
 * @context: a #GtkIMContext
 * @offset: offset from cursor position in chars;
 *    a negative value means start before the cursor.
 * @n_chars: number of characters to delete.
 * 
 * Asks the widget that the input context is attached to to delete
 * characters around the cursor position by emitting the
525
 * GtkIMContext::delete_surrounding signal. Note that @offset and @n_chars
526
 * are in characters not in bytes which differs from the usage other
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
 * places in #GtkIMContext.
 *
 * In order to use this function, you should first call
 * gtk_im_context_get_surrounding() to get the current context, and
 * call this function immediately afterwards to make sure that you
 * know what you are deleting. You should also account for the fact
 * that even if the signal was handled, the input context might not
 * have deleted all the characters that were requested to be deleted.
 *
 * This function is used by an input method that wants to make
 * subsitutions in the existing text in response to new input. It is
 * not useful for applications.
 * 
 * Return value: %TRUE if the signal was handled.
 **/
gboolean
gtk_im_context_delete_surrounding (GtkIMContext *context,
				   gint          offset,
				   gint          n_chars)
{
  gboolean result;
  
  g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);

  g_signal_emit (context,
		 im_context_signals[DELETE_SURROUNDING], 0,
		 offset, n_chars, &result);

  return result;
}

558 559
#define __GTK_IM_CONTEXT_C__
#include "gtkaliasdef.c"