gtkimmulticontext.c 17.6 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>
21

Owen Taylor's avatar
Owen Taylor committed
22 23 24
#include <string.h>
#include <locale.h>

25
#include "gtkimmulticontext.h"
Owen Taylor's avatar
Owen Taylor committed
26
#include "gtkimmodule.h"
27
#include "gtkmain.h"
28
#include "gtkradiomenuitem.h"
29
#include "gtkintl.h"
30
#include "gtkprivate.h"
31
#include "gtkalias.h"
32

33 34 35 36 37 38 39 40 41 42
struct _GtkIMMulticontextPrivate
{
  GdkWindow *client_window;
  GdkRectangle cursor_location;

  guint use_preedit : 1;
  guint have_cursor_location : 1;
  guint focus_in : 1;
};

43 44 45 46 47
static void     gtk_im_multicontext_class_init         (GtkIMMulticontextClass  *class);
static void     gtk_im_multicontext_init               (GtkIMMulticontext       *im_multicontext);
static void     gtk_im_multicontext_finalize           (GObject                 *object);

static void     gtk_im_multicontext_set_slave          (GtkIMMulticontext       *multicontext,
48 49
							GtkIMContext            *slave,
							gboolean                 finalizing);
50 51 52 53 54

static void     gtk_im_multicontext_set_client_window  (GtkIMContext            *context,
							GdkWindow               *window);
static void     gtk_im_multicontext_get_preedit_string (GtkIMContext            *context,
							gchar                  **str,
Owen Taylor's avatar
Owen Taylor committed
55 56
							PangoAttrList          **attrs,
							gint                   *cursor_pos);
57 58 59 60
static gboolean gtk_im_multicontext_filter_keypress    (GtkIMContext            *context,
							GdkEventKey             *event);
static void     gtk_im_multicontext_focus_in           (GtkIMContext            *context);
static void     gtk_im_multicontext_focus_out          (GtkIMContext            *context);
Owen Taylor's avatar
Owen Taylor committed
61
static void     gtk_im_multicontext_reset              (GtkIMContext            *context);
62
static void     gtk_im_multicontext_set_cursor_location (GtkIMContext            *context,
63
							GdkRectangle		*area);
64 65
static void     gtk_im_multicontext_set_use_preedit    (GtkIMContext            *context,
							gboolean                 use_preedit);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
static gboolean gtk_im_multicontext_get_surrounding    (GtkIMContext            *context,
							gchar                  **text,
							gint                    *cursor_index);
static void     gtk_im_multicontext_set_surrounding    (GtkIMContext            *context,
							const char              *text,
							gint                     len,
							gint                     cursor_index);

static void     gtk_im_multicontext_preedit_start_cb        (GtkIMContext      *slave,
							     GtkIMMulticontext *multicontext);
static void     gtk_im_multicontext_preedit_end_cb          (GtkIMContext      *slave,
							     GtkIMMulticontext *multicontext);
static void     gtk_im_multicontext_preedit_changed_cb      (GtkIMContext      *slave,
							     GtkIMMulticontext *multicontext);
static void     gtk_im_multicontext_commit_cb               (GtkIMContext      *slave,
							     const gchar       *str,
							     GtkIMMulticontext *multicontext);
static gboolean gtk_im_multicontext_retrieve_surrounding_cb (GtkIMContext      *slave,
							     GtkIMMulticontext *multicontext);
static gboolean gtk_im_multicontext_delete_surrounding_cb   (GtkIMContext      *slave,
							     gint               offset,
							     gint               n_chars,
							     GtkIMMulticontext *multicontext);
89 90
static GtkIMContextClass *parent_class;

Owen Taylor's avatar
Owen Taylor committed
91 92
static const gchar *global_context_id = NULL;

Manish Singh's avatar
Manish Singh committed
93
GType
94 95
gtk_im_multicontext_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
96
  static GType im_multicontext_type = 0;
97
 
98 99
  if (!im_multicontext_type)
    {
100
      static const GTypeInfo im_multicontext_info =
101
      {
102 103 104 105 106 107 108 109 110
        sizeof (GtkIMMulticontextClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gtk_im_multicontext_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GtkIMMulticontext),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_im_multicontext_init,
111
      };
112
      
Manish Singh's avatar
Manish Singh committed
113
      im_multicontext_type =
114
	g_type_register_static (GTK_TYPE_IM_CONTEXT, I_("GtkIMMulticontext"),
Manish Singh's avatar
Manish Singh committed
115
				&im_multicontext_info, 0);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    }

  return im_multicontext_type;
}

static void
gtk_im_multicontext_class_init (GtkIMMulticontextClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
  
  parent_class = g_type_class_peek_parent (class);

  im_context_class->set_client_window = gtk_im_multicontext_set_client_window;
  im_context_class->get_preedit_string = gtk_im_multicontext_get_preedit_string;
  im_context_class->filter_keypress = gtk_im_multicontext_filter_keypress;
  im_context_class->focus_in = gtk_im_multicontext_focus_in;
  im_context_class->focus_out = gtk_im_multicontext_focus_out;
Owen Taylor's avatar
Owen Taylor committed
134
  im_context_class->reset = gtk_im_multicontext_reset;
135
  im_context_class->set_cursor_location = gtk_im_multicontext_set_cursor_location;
136
  im_context_class->set_use_preedit = gtk_im_multicontext_set_use_preedit;
137 138
  im_context_class->set_surrounding = gtk_im_multicontext_set_surrounding;
  im_context_class->get_surrounding = gtk_im_multicontext_get_surrounding;
139 140 141 142 143 144 145 146

  gobject_class->finalize = gtk_im_multicontext_finalize;
}

static void
gtk_im_multicontext_init (GtkIMMulticontext *multicontext)
{
  multicontext->slave = NULL;
147 148 149 150 151
  
  multicontext->priv = g_new0 (GtkIMMulticontextPrivate, 1);
  multicontext->priv->use_preedit = TRUE;
  multicontext->priv->have_cursor_location = FALSE;
  multicontext->priv->focus_in = FALSE;
152 153
}

Matthias Clasen's avatar
Matthias Clasen committed
154 155 156 157 158 159 160
/**
 * gtk_im_multicontext_new:
 *
 * Creates a new #GtkIMMulticontext.
 *
 * Returns: a new #GtkIMMulticontext.
 **/
161 162 163
GtkIMContext *
gtk_im_multicontext_new (void)
{
Manish Singh's avatar
Manish Singh committed
164
  return g_object_new (GTK_TYPE_IM_MULTICONTEXT, NULL);
165 166 167 168 169
}

static void
gtk_im_multicontext_finalize (GObject *object)
{
170 171 172 173
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (object);
  
  gtk_im_multicontext_set_slave (multicontext, NULL, TRUE);
  g_free (multicontext->priv);
174 175 176 177 178 179

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gtk_im_multicontext_set_slave (GtkIMMulticontext *multicontext,
180 181
			       GtkIMContext      *slave,
			       gboolean           finalizing)
182
{
183
  GtkIMMulticontextPrivate *priv = multicontext->priv;
184 185
  gboolean need_preedit_changed = FALSE;
  
186 187
  if (multicontext->slave)
    {
188 189 190
      if (!finalizing)
	gtk_im_context_reset (multicontext->slave);
      
191
      g_signal_handlers_disconnect_by_func (multicontext->slave,
Manish Singh's avatar
Manish Singh committed
192
					    gtk_im_multicontext_preedit_start_cb,
193 194
					    multicontext);
      g_signal_handlers_disconnect_by_func (multicontext->slave,
Manish Singh's avatar
Manish Singh committed
195
					    gtk_im_multicontext_preedit_end_cb,
196 197
					    multicontext);
      g_signal_handlers_disconnect_by_func (multicontext->slave,
Manish Singh's avatar
Manish Singh committed
198
					    gtk_im_multicontext_preedit_changed_cb,
199 200
					    multicontext);
      g_signal_handlers_disconnect_by_func (multicontext->slave,
Manish Singh's avatar
Manish Singh committed
201
					    gtk_im_multicontext_commit_cb,
202
					    multicontext);
203

204
      g_object_unref (multicontext->slave);
205 206 207
      multicontext->slave = NULL;

      if (!finalizing)
208
	need_preedit_changed = TRUE;
209 210 211 212 213 214
    }
  
  multicontext->slave = slave;

  if (multicontext->slave)
    {
215 216 217 218 219 220 221 222 223 224 225 226 227 228
      g_object_ref (multicontext->slave);

      g_signal_connect (multicontext->slave, "preedit_start",
			G_CALLBACK (gtk_im_multicontext_preedit_start_cb),
			multicontext);
      g_signal_connect (multicontext->slave, "preedit_end",
			G_CALLBACK (gtk_im_multicontext_preedit_end_cb),
			multicontext);
      g_signal_connect (multicontext->slave, "preedit_changed",
			G_CALLBACK (gtk_im_multicontext_preedit_changed_cb),
			multicontext);
      g_signal_connect (multicontext->slave, "commit",
			G_CALLBACK (gtk_im_multicontext_commit_cb),
			multicontext);
229 230 231 232 233 234
      g_signal_connect (multicontext->slave, "retrieve_surrounding",
			G_CALLBACK (gtk_im_multicontext_retrieve_surrounding_cb),
			multicontext);
      g_signal_connect (multicontext->slave, "delete_surrounding",
			G_CALLBACK (gtk_im_multicontext_delete_surrounding_cb),
			multicontext);
235
      
236 237 238 239 240 241 242 243
      if (!priv->use_preedit)	/* Default is TRUE */
	gtk_im_context_set_use_preedit (slave, FALSE);
      if (priv->client_window)
	gtk_im_context_set_client_window (slave, priv->client_window);
      if (priv->have_cursor_location)
	gtk_im_context_set_cursor_location (slave, &priv->cursor_location);
      if (priv->focus_in)
	gtk_im_context_focus_in (slave);
244
    }
245 246 247

  if (need_preedit_changed)
    g_signal_emit_by_name (multicontext, "preedit_changed");
248 249 250 251 252 253
}

static GtkIMContext *
gtk_im_multicontext_get_slave (GtkIMMulticontext *multicontext)
{
  if (!multicontext->slave)
Owen Taylor's avatar
Owen Taylor committed
254
    {
255 256
      GtkIMContext *slave;

Owen Taylor's avatar
Owen Taylor committed
257 258
      if (!global_context_id)
	{
259
	  gchar *locale = _gtk_get_lc_ctype ();
Owen Taylor's avatar
Owen Taylor committed
260
	  global_context_id = _gtk_im_module_get_default_context_id (locale);
261
	  g_free (locale);
Owen Taylor's avatar
Owen Taylor committed
262 263
	}
	
264 265 266 267
      slave = _gtk_im_module_create (global_context_id);
      gtk_im_multicontext_set_slave (multicontext, slave, FALSE);
      g_object_unref (slave);

Owen Taylor's avatar
Owen Taylor committed
268 269
      multicontext->context_id = global_context_id;
    }
270 271 272 273 274 275 276 277 278

  return multicontext->slave;
}

static void
gtk_im_multicontext_set_client_window (GtkIMContext *context,
				       GdkWindow    *window)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
279
  
280 281
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

282
  multicontext->priv->client_window = window;
Owen Taylor's avatar
Owen Taylor committed
283
  
284 285 286 287 288 289 290
  if (slave)
    gtk_im_context_set_client_window (slave, window);
}

static void
gtk_im_multicontext_get_preedit_string (GtkIMContext   *context,
					gchar         **str,
Owen Taylor's avatar
Owen Taylor committed
291 292
					PangoAttrList **attrs,
					gint           *cursor_pos)
293 294 295 296 297
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

  if (slave)
Owen Taylor's avatar
Owen Taylor committed
298
    gtk_im_context_get_preedit_string (slave, str, attrs, cursor_pos);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  else
    {
      if (str)
	*str = g_strdup ("");
      if (attrs)
	*attrs = pango_attr_list_new ();
    }
}

static gboolean
gtk_im_multicontext_filter_keypress (GtkIMContext *context,
				     GdkEventKey  *event)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

  if (slave)
    return gtk_im_context_filter_keypress (slave, event);
  else
    return FALSE;
}

static void
gtk_im_multicontext_focus_in (GtkIMContext   *context)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
Owen Taylor's avatar
Owen Taylor committed
325 326 327 328 329 330 331 332
  GtkIMContext *slave;

  /* If the global context type is different from the context we were
   * using before, get rid of the old slave and create a new one
   * for the new global context type.
   */
  if (!multicontext->context_id ||
      strcmp (global_context_id, multicontext->context_id) != 0)
333
    gtk_im_multicontext_set_slave (multicontext, NULL, FALSE);
Owen Taylor's avatar
Owen Taylor committed
334 335

  slave = gtk_im_multicontext_get_slave (multicontext);
336 337 338
  
  multicontext->priv->focus_in = TRUE;
  
339
  if (slave)
Tor Lillqvist's avatar
Tor Lillqvist committed
340
    gtk_im_context_focus_in (slave);
341 342 343 344 345 346 347 348
}

static void
gtk_im_multicontext_focus_out (GtkIMContext   *context)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

349 350
  multicontext->priv->focus_in = FALSE;
  
351
  if (slave)
Tor Lillqvist's avatar
Tor Lillqvist committed
352
    gtk_im_context_focus_out (slave);
353 354
}

Owen Taylor's avatar
Owen Taylor committed
355 356 357 358 359 360 361 362 363 364
static void
gtk_im_multicontext_reset (GtkIMContext   *context)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

  if (slave)
    gtk_im_context_reset (slave);
}

365
static void
366 367
gtk_im_multicontext_set_cursor_location (GtkIMContext   *context,
					 GdkRectangle   *area)
368 369 370 371
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

372 373 374
  multicontext->priv->have_cursor_location = TRUE;
  multicontext->priv->cursor_location = *area;

375
  if (slave)
376
    gtk_im_context_set_cursor_location (slave, area);
377 378
}

379 380 381 382 383 384 385
static void
gtk_im_multicontext_set_use_preedit (GtkIMContext   *context,
				     gboolean	    use_preedit)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

386 387 388 389
  use_preedit = use_preedit != FALSE;

  multicontext->priv->use_preedit = use_preedit;

390 391 392 393
  if (slave)
    gtk_im_context_set_use_preedit (slave, use_preedit);
}

394 395 396 397 398 399 400 401 402
static gboolean
gtk_im_multicontext_get_surrounding (GtkIMContext  *context,
				     gchar        **text,
				     gint          *cursor_index)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

  if (slave)
403
    return gtk_im_context_get_surrounding (slave, text, cursor_index);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  else
    {
      if (text)
	*text = NULL;
      if (cursor_index)
	*cursor_index = 0;

      return FALSE;
    }
}

static void
gtk_im_multicontext_set_surrounding (GtkIMContext *context,
				     const char   *text,
				     gint          len,
				     gint          cursor_index)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

  if (slave)
    gtk_im_context_set_surrounding (slave, text, len, cursor_index);
}

428
static void
429 430 431
gtk_im_multicontext_preedit_start_cb   (GtkIMContext      *slave,
					GtkIMMulticontext *multicontext)
{
432
  g_signal_emit_by_name (multicontext, "preedit_start");
433 434
}

435
static void
436 437 438
gtk_im_multicontext_preedit_end_cb (GtkIMContext      *slave,
				    GtkIMMulticontext *multicontext)
{
439
  g_signal_emit_by_name (multicontext, "preedit_end");
440 441
}

442
static void
443 444 445
gtk_im_multicontext_preedit_changed_cb (GtkIMContext      *slave,
					GtkIMMulticontext *multicontext)
{
446
  g_signal_emit_by_name (multicontext, "preedit_changed");
447 448
}

449
static void
450 451 452 453
gtk_im_multicontext_commit_cb (GtkIMContext      *slave,
			       const gchar       *str,
			       GtkIMMulticontext *multicontext)
{
454
  g_signal_emit_by_name (multicontext, "commit", str);;
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
static gboolean
gtk_im_multicontext_retrieve_surrounding_cb (GtkIMContext      *slave,
					     GtkIMMulticontext *multicontext)
{
  gboolean result;
  
  g_signal_emit_by_name (multicontext, "retrieve_surrounding", &result);

  return result;
}

static gboolean
gtk_im_multicontext_delete_surrounding_cb (GtkIMContext      *slave,
					   gint               offset,
					   gint               n_chars,
					   GtkIMMulticontext *multicontext)
{
  gboolean result;
  
  g_signal_emit_by_name (multicontext, "delete_surrounding",
			 offset, n_chars, &result);

  return result;
}

Owen Taylor's avatar
Owen Taylor committed
482 483 484 485
static void
activate_cb (GtkWidget         *menuitem,
	     GtkIMMulticontext *context)
{
486 487
  if (GTK_CHECK_MENU_ITEM (menuitem)->active)
    {
Manish Singh's avatar
Manish Singh committed
488
      const gchar *id = g_object_get_data (G_OBJECT (menuitem), "gtk-context-id");
Owen Taylor's avatar
Owen Taylor committed
489

490 491 492
      gtk_im_context_reset (GTK_IM_CONTEXT (context));
      
      global_context_id = id;
493
      gtk_im_multicontext_set_slave (context, NULL, FALSE);
494
    }
Owen Taylor's avatar
Owen Taylor committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
}

/**
 * gtk_im_multicontext_append_menuitems:
 * @context: a #GtkIMMultiContext
 * @menushell: a #GtkMenuShell
 * 
 * Add menuitems for various available input methods to a menu;
 * the menuitems, when selected, will switch the input method
 * for the context and the global default input method.
 **/
void
gtk_im_multicontext_append_menuitems (GtkIMMulticontext *context,
				      GtkMenuShell      *menushell)
{
  const GtkIMContextInfo **contexts;
511
  guint n_contexts, i;
512
  GSList *group = NULL;
Owen Taylor's avatar
Owen Taylor committed
513 514 515 516 517 518
  
  _gtk_im_module_list (&contexts, &n_contexts);

  for (i=0; i < n_contexts; i++)
    {
      GtkWidget *menuitem;
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
      const gchar *translated_name;
#ifdef ENABLE_NLS
      if (contexts[i]->domain && contexts[i]->domain_dirname &&
	  contexts[i]->domain[0] && contexts[i]->domain_dirname[0])
	{
	  if (strcmp (contexts[i]->domain, GETTEXT_PACKAGE) == 0 &&
	      strcmp (contexts[i]->domain_dirname, GTK_LOCALEDIR) == 0)
	    /* Input method may have a name in the GTK+ message catalog */
	    translated_name = _(contexts[i]->context_name);
	  else
	    /* Input method has own message catalog */
	    {
	      bindtextdomain (contexts[i]->domain,
			      contexts[i]->domain_dirname);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
	      bind_textdomain_codeset (contexts[i]->domain, "UTF-8");
#endif
	      translated_name = dgettext (contexts[i]->domain, contexts[i]->context_name);
	    }
	}
      else
	/* Either domain or domain_dirname is NULL or "". We assume that
	 * input method does not want a translated name in this case
	 */
	translated_name = contexts[i]->context_name;
#else
      translated_name = contexts[i]->context_name;
#endif
547
      menuitem = gtk_radio_menu_item_new_with_label (group,
548
						     translated_name);
549 550 551 552 553 554 555
      
      if ((global_context_id == NULL && group == NULL) ||
          (global_context_id &&
           strcmp (contexts[i]->context_id, global_context_id) == 0))
        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem),
                                        TRUE);
      
Manish Singh's avatar
Manish Singh committed
556
      group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
557
      
558
      g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"),
Manish Singh's avatar
Manish Singh committed
559 560 561
			 (char *)contexts[i]->context_id);
      g_signal_connect (menuitem, "activate",
			G_CALLBACK (activate_cb), context);
Owen Taylor's avatar
Owen Taylor committed
562 563 564 565

      gtk_widget_show (menuitem);
      gtk_menu_shell_append (menushell, menuitem);
    }
566 567

  g_free (contexts);
Owen Taylor's avatar
Owen Taylor committed
568 569
}

570 571
#define __GTK_IM_MULTICONTEXT_C__
#include "gtkaliasdef.c"