gtkimmulticontext.c 23.2 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4
 * 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17
 */

18
#include "config.h"
19

Owen Taylor's avatar
Owen Taylor committed
20 21 22
#include <string.h>
#include <locale.h>

23
#include "gtkimmulticontext.h"
24
#include "gtkimmoduleprivate.h"
25
#include "gtkintl.h"
Benjamin Otte's avatar
Benjamin Otte committed
26
#include "gtklabel.h"
27
#include "gtkmain.h"
28
#include "gtkprivate.h"
29
#include "gtkradiomenuitem.h"
Benjamin Otte's avatar
Benjamin Otte committed
30
#include "gtkseparatormenuitem.h"
31
#include "gtksettings.h"
32

33 34 35 36 37 38 39 40

/**
 * SECTION:gtkimmulticontext
 * @Short_description: An input method context supporting multiple, loadable input methods
 * @Title: GtkIMMulticontext
 */


41 42
struct _GtkIMMulticontextPrivate
{
43
  GtkIMContext          *slave;
44

45 46 47 48 49 50 51 52 53
  GdkWindow             *client_window;
  GdkRectangle           cursor_location;

  gchar                 *context_id;
  gchar                 *context_id_aux;

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

56 57
static void     gtk_im_multicontext_notify             (GObject                 *object,
                                                        GParamSpec              *pspec);
58 59 60
static void     gtk_im_multicontext_finalize           (GObject                 *object);

static void     gtk_im_multicontext_set_slave          (GtkIMMulticontext       *multicontext,
61 62
							GtkIMContext            *slave,
							gboolean                 finalizing);
63 64 65 66 67

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
68 69
							PangoAttrList          **attrs,
							gint                   *cursor_pos);
70 71 72 73
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
74
static void     gtk_im_multicontext_reset              (GtkIMContext            *context);
75
static void     gtk_im_multicontext_set_cursor_location (GtkIMContext            *context,
76
							GdkRectangle		*area);
77 78
static void     gtk_im_multicontext_set_use_preedit    (GtkIMContext            *context,
							gboolean                 use_preedit);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
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);
102

103 104
static void propagate_purpose (GtkIMMulticontext *context);

Owen Taylor's avatar
Owen Taylor committed
105 106
static const gchar *global_context_id = NULL;

107
G_DEFINE_TYPE_WITH_PRIVATE (GtkIMMulticontext, gtk_im_multicontext, GTK_TYPE_IM_CONTEXT)
108 109 110 111 112 113

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);
114 115 116

  gobject_class->notify = gtk_im_multicontext_notify;

117 118 119 120 121
  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
122
  im_context_class->reset = gtk_im_multicontext_reset;
123
  im_context_class->set_cursor_location = gtk_im_multicontext_set_cursor_location;
124
  im_context_class->set_use_preedit = gtk_im_multicontext_set_use_preedit;
125 126
  im_context_class->set_surrounding = gtk_im_multicontext_set_surrounding;
  im_context_class->get_surrounding = gtk_im_multicontext_get_surrounding;
127 128 129 130 131 132 133

  gobject_class->finalize = gtk_im_multicontext_finalize;
}

static void
gtk_im_multicontext_init (GtkIMMulticontext *multicontext)
{
134
  GtkIMMulticontextPrivate *priv;
135
  
136
  multicontext->priv = gtk_im_multicontext_get_instance_private (multicontext);
137 138 139 140 141 142
  priv = multicontext->priv;

  priv->slave = NULL;
  priv->use_preedit = TRUE;
  priv->have_cursor_location = FALSE;
  priv->focus_in = FALSE;
143 144
}

Matthias Clasen's avatar
Matthias Clasen committed
145 146 147 148 149 150 151
/**
 * gtk_im_multicontext_new:
 *
 * Creates a new #GtkIMMulticontext.
 *
 * Returns: a new #GtkIMMulticontext.
 **/
152 153 154
GtkIMContext *
gtk_im_multicontext_new (void)
{
Manish Singh's avatar
Manish Singh committed
155
  return g_object_new (GTK_TYPE_IM_MULTICONTEXT, NULL);
156 157 158 159 160
}

static void
gtk_im_multicontext_finalize (GObject *object)
{
161
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (object);
162 163
  GtkIMMulticontextPrivate *priv = multicontext->priv;

164
  gtk_im_multicontext_set_slave (multicontext, NULL, TRUE);
165 166
  g_free (priv->context_id);
  g_free (priv->context_id_aux);
167

Matthias Clasen's avatar
Matthias Clasen committed
168
  G_OBJECT_CLASS (gtk_im_multicontext_parent_class)->finalize (object);
169 170 171 172
}

static void
gtk_im_multicontext_set_slave (GtkIMMulticontext *multicontext,
173 174
			       GtkIMContext      *slave,
			       gboolean           finalizing)
175
{
176
  GtkIMMulticontextPrivate *priv = multicontext->priv;
177 178
  gboolean need_preedit_changed = FALSE;
  
179
  if (priv->slave)
180
    {
181
      if (!finalizing)
182
	gtk_im_context_reset (priv->slave);
183
      
184
      g_signal_handlers_disconnect_by_func (priv->slave,
Manish Singh's avatar
Manish Singh committed
185
					    gtk_im_multicontext_preedit_start_cb,
186
					    multicontext);
187
      g_signal_handlers_disconnect_by_func (priv->slave,
Manish Singh's avatar
Manish Singh committed
188
					    gtk_im_multicontext_preedit_end_cb,
189
					    multicontext);
190
      g_signal_handlers_disconnect_by_func (priv->slave,
Manish Singh's avatar
Manish Singh committed
191
					    gtk_im_multicontext_preedit_changed_cb,
192
					    multicontext);
193
      g_signal_handlers_disconnect_by_func (priv->slave,
Manish Singh's avatar
Manish Singh committed
194
					    gtk_im_multicontext_commit_cb,
195
					    multicontext);
196

197 198
      g_object_unref (priv->slave);
      priv->slave = NULL;
199 200

      if (!finalizing)
201
	need_preedit_changed = TRUE;
202
    }
203

204
  priv->slave = slave;
205

206
  if (priv->slave)
207
    {
208
      g_object_ref (priv->slave);
209

210 211
      propagate_purpose (multicontext);

212
      g_signal_connect (priv->slave, "preedit-start",
213 214
			G_CALLBACK (gtk_im_multicontext_preedit_start_cb),
			multicontext);
215
      g_signal_connect (priv->slave, "preedit-end",
216 217
			G_CALLBACK (gtk_im_multicontext_preedit_end_cb),
			multicontext);
218
      g_signal_connect (priv->slave, "preedit-changed",
219 220
			G_CALLBACK (gtk_im_multicontext_preedit_changed_cb),
			multicontext);
221
      g_signal_connect (priv->slave, "commit",
222 223
			G_CALLBACK (gtk_im_multicontext_commit_cb),
			multicontext);
224
      g_signal_connect (priv->slave, "retrieve-surrounding",
225 226
			G_CALLBACK (gtk_im_multicontext_retrieve_surrounding_cb),
			multicontext);
227
      g_signal_connect (priv->slave, "delete-surrounding",
228 229
			G_CALLBACK (gtk_im_multicontext_delete_surrounding_cb),
			multicontext);
230

231 232 233 234 235 236 237 238
      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);
239
    }
240 241

  if (need_preedit_changed)
242
    g_signal_emit_by_name (multicontext, "preedit-changed");
243 244
}

245 246 247
static const gchar *
get_effective_context_id (GtkIMMulticontext *multicontext)
{
248 249 250 251
  GtkIMMulticontextPrivate *priv = multicontext->priv;

  if (priv->context_id_aux)
    return priv->context_id_aux;
252 253

  if (!global_context_id)
254
    global_context_id = _gtk_im_module_get_default_context_id ();
255 256 257 258

  return global_context_id;
}

259 260 261
static GtkIMContext *
gtk_im_multicontext_get_slave (GtkIMMulticontext *multicontext)
{
262 263
  GtkIMMulticontextPrivate *priv = multicontext->priv;

264 265 266
  if (g_strcmp0 (priv->context_id, get_effective_context_id (multicontext)) != 0)
    gtk_im_multicontext_set_slave (multicontext, NULL, FALSE);

267
  if (!priv->slave)
Owen Taylor's avatar
Owen Taylor committed
268
    {
269
      GtkIMContext *slave;
270

271
      g_free (priv->context_id);
272

273
      priv->context_id = g_strdup (get_effective_context_id (multicontext));
274

275
      slave = _gtk_im_module_create (priv->context_id);
276 277 278 279 280
      if (slave)
        {
          gtk_im_multicontext_set_slave (multicontext, slave, FALSE);
          g_object_unref (slave);
        }
Owen Taylor's avatar
Owen Taylor committed
281
    }
282

283
  return priv->slave;
284 285
}

286 287 288 289 290 291 292 293
static void
im_module_setting_changed (GtkSettings *settings, 
                           gpointer     data)
{
  global_context_id = NULL;
}


294 295 296 297 298
static void
gtk_im_multicontext_set_client_window (GtkIMContext *context,
				       GdkWindow    *window)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
299
  GtkIMMulticontextPrivate *priv = multicontext->priv;
300 301
  GtkIMContext *slave;
  GdkScreen *screen;
302 303
  GtkSettings *settings;
  gboolean connected;
304

305
  priv->client_window = window;
306

307
  if (window)
308
    {
309
      screen = gdk_window_get_screen (window);
310
      settings = gtk_settings_get_for_screen (screen);
311

312 313 314 315 316 317 318 319 320 321 322
      connected = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
                                                      "gtk-im-module-connected"));
      if (!connected)
        {
          g_signal_connect (settings, "notify::gtk-im-module",
                            G_CALLBACK (im_module_setting_changed), NULL);
          g_object_set_data (G_OBJECT (settings), "gtk-im-module-connected",
                             GINT_TO_POINTER (TRUE));

          global_context_id = NULL;
        }
323
    }
324

325 326 327
  slave = gtk_im_multicontext_get_slave (multicontext);
  if (slave)
    gtk_im_context_set_client_window (slave, window);
328 329 330 331 332
}

static void
gtk_im_multicontext_get_preedit_string (GtkIMContext   *context,
					gchar         **str,
Owen Taylor's avatar
Owen Taylor committed
333 334
					PangoAttrList **attrs,
					gint           *cursor_pos)
335 336 337 338 339
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

  if (slave)
Owen Taylor's avatar
Owen Taylor committed
340
    gtk_im_context_get_preedit_string (slave, str, attrs, cursor_pos);
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  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)
358
    {
359 360 361 362 363 364
      return gtk_im_context_filter_keypress (slave, event);
    }
  else
    {
      GdkDisplay *display;
      GdkModifierType no_text_input_mask;
365

366 367 368 369 370 371 372 373
      display = gdk_window_get_display (event->window);

      no_text_input_mask =
        gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
                                      GDK_MODIFIER_INTENT_NO_TEXT_INPUT);

      if (event->type == GDK_KEY_PRESS &&
          (event->state & no_text_input_mask) == 0)
374
        {
375 376 377 378 379 380 381
          gunichar ch;

          ch = gdk_keyval_to_unicode (event->keyval);
          if (ch != 0 && !g_unichar_iscntrl (ch))
            {
              gint len;
              gchar buf[10];
382

383 384
              len = g_unichar_to_utf8 (ch, buf);
              buf[len] = '\0';
385

386
              g_signal_emit_by_name (multicontext, "commit", buf);
387

388 389
              return TRUE;
            }
390 391 392 393
        }
    }

  return FALSE;
394 395 396 397 398 399
}

static void
gtk_im_multicontext_focus_in (GtkIMContext   *context)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
400
  GtkIMMulticontextPrivate *priv = multicontext->priv;
401
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
402 403 404

  priv->focus_in = TRUE;

405
  if (slave)
Tor Lillqvist's avatar
Tor Lillqvist committed
406
    gtk_im_context_focus_in (slave);
407 408 409 410 411 412
}

static void
gtk_im_multicontext_focus_out (GtkIMContext   *context)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
413
  GtkIMMulticontextPrivate *priv = multicontext->priv;
414 415
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

416 417
  priv->focus_in = FALSE;

418
  if (slave)
Tor Lillqvist's avatar
Tor Lillqvist committed
419
    gtk_im_context_focus_out (slave);
420 421
}

Owen Taylor's avatar
Owen Taylor committed
422 423 424 425 426 427 428 429 430 431
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);
}

432
static void
433 434
gtk_im_multicontext_set_cursor_location (GtkIMContext   *context,
					 GdkRectangle   *area)
435 436
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
437
  GtkIMMulticontextPrivate *priv = multicontext->priv;
438 439
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

440 441
  priv->have_cursor_location = TRUE;
  priv->cursor_location = *area;
442

443
  if (slave)
444
    gtk_im_context_set_cursor_location (slave, area);
445 446
}

447 448 449 450 451
static void
gtk_im_multicontext_set_use_preedit (GtkIMContext   *context,
				     gboolean	    use_preedit)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
452
  GtkIMMulticontextPrivate *priv = multicontext->priv;
453 454
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

455 456
  use_preedit = use_preedit != FALSE;

457
  priv->use_preedit = use_preedit;
458

459 460 461 462
  if (slave)
    gtk_im_context_set_use_preedit (slave, use_preedit);
}

463 464 465 466 467 468 469 470 471
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)
472
    return gtk_im_context_get_surrounding (slave, text, cursor_index);
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
  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);
}

497
static void
498 499 500
gtk_im_multicontext_preedit_start_cb   (GtkIMContext      *slave,
					GtkIMMulticontext *multicontext)
{
501
  g_signal_emit_by_name (multicontext, "preedit-start");
502 503
}

504
static void
505 506 507
gtk_im_multicontext_preedit_end_cb (GtkIMContext      *slave,
				    GtkIMMulticontext *multicontext)
{
508
  g_signal_emit_by_name (multicontext, "preedit-end");
509 510
}

511
static void
512 513 514
gtk_im_multicontext_preedit_changed_cb (GtkIMContext      *slave,
					GtkIMMulticontext *multicontext)
{
515
  g_signal_emit_by_name (multicontext, "preedit-changed");
516 517
}

518
static void
519 520 521 522
gtk_im_multicontext_commit_cb (GtkIMContext      *slave,
			       const gchar       *str,
			       GtkIMMulticontext *multicontext)
{
523
  g_signal_emit_by_name (multicontext, "commit", str);
524 525
}

526 527 528 529 530 531
static gboolean
gtk_im_multicontext_retrieve_surrounding_cb (GtkIMContext      *slave,
					     GtkIMMulticontext *multicontext)
{
  gboolean result;
  
532
  g_signal_emit_by_name (multicontext, "retrieve-surrounding", &result);
533 534 535 536 537 538 539 540 541 542 543 544

  return result;
}

static gboolean
gtk_im_multicontext_delete_surrounding_cb (GtkIMContext      *slave,
					   gint               offset,
					   gint               n_chars,
					   GtkIMMulticontext *multicontext)
{
  gboolean result;
  
545
  g_signal_emit_by_name (multicontext, "delete-surrounding",
546 547 548 549 550
			 offset, n_chars, &result);

  return result;
}

Owen Taylor's avatar
Owen Taylor committed
551 552 553 554
static void
activate_cb (GtkWidget         *menuitem,
	     GtkIMMulticontext *context)
{
555
  if (gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menuitem)))
556
    {
Manish Singh's avatar
Manish Singh committed
557
      const gchar *id = g_object_get_data (G_OBJECT (menuitem), "gtk-context-id");
Owen Taylor's avatar
Owen Taylor committed
558

559
      gtk_im_multicontext_set_context_id (context, id);
560
    }
Owen Taylor's avatar
Owen Taylor committed
561 562
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
static int
pathnamecmp (const char *a,
	     const char *b)
{
#ifndef G_OS_WIN32
  return strcmp (a, b);
#else
  /* Ignore case insensitivity, probably not that relevant here. Just
   * make sure slash and backslash compare equal.
   */
  while (*a && *b)
    if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) ||
	*a == *b)
      a++, b++;
    else
      return (*a - *b);
  return (*a - *b);
#endif
}

Owen Taylor's avatar
Owen Taylor committed
583 584
/**
 * gtk_im_multicontext_append_menuitems:
585
 * @context: a #GtkIMMulticontext
Owen Taylor's avatar
Owen Taylor committed
586 587 588 589 590
 * @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.
591 592 593 594 595
 *
 * Deprecated: 3.10: It is better to use the system-wide input
 *     method framework for changing input methods. Modern
 *     desktop shells offer on-screen displays for this that
 *     can triggered with a keyboard shortcut, e.g. Super-Space.
Owen Taylor's avatar
Owen Taylor committed
596 597 598 599 600
 **/
void
gtk_im_multicontext_append_menuitems (GtkIMMulticontext *context,
				      GtkMenuShell      *menushell)
{
601
  GtkIMMulticontextPrivate *priv = context->priv;
Owen Taylor's avatar
Owen Taylor committed
602
  const GtkIMContextInfo **contexts;
603
  guint n_contexts, i;
604
  GSList *group = NULL;
605 606
  GtkWidget *menuitem, *system_menuitem;
  const char *system_context_id; 
607

608
  system_context_id = _gtk_im_module_get_default_context_id ();
609
  system_menuitem = menuitem = gtk_radio_menu_item_new_with_label (group, C_("input method menu", "System"));
610
  if (!priv->context_id_aux)
611 612 613 614 615 616 617 618
    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
  group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
  g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"), NULL);
  g_signal_connect (menuitem, "activate", G_CALLBACK (activate_cb), context);

  gtk_widget_show (menuitem);
  gtk_menu_shell_append (menushell, menuitem);

Owen Taylor's avatar
Owen Taylor committed
619 620
  _gtk_im_module_list (&contexts, &n_contexts);

621
  for (i = 0; i < n_contexts; i++)
Owen Taylor's avatar
Owen Taylor committed
622
    {
623 624
      const gchar *translated_name;
#ifdef ENABLE_NLS
625
      if (contexts[i]->domain && contexts[i]->domain[0])
626
	{
627 628 629 630
	  if (strcmp (contexts[i]->domain, GETTEXT_PACKAGE) == 0)
	    {
	      /* Same translation domain as GTK+ */
	      if (!(contexts[i]->domain_dirname && contexts[i]->domain_dirname[0]) ||
631
		  pathnamecmp (contexts[i]->domain_dirname, _gtk_get_localedir ()) == 0)
632 633 634 635 636
		{
		  /* Empty or NULL, domain directory, or same as
		   * GTK+. Input method may have a name in the GTK+
		   * message catalog.
		   */
637
		  translated_name = g_dpgettext2 (GETTEXT_PACKAGE, "input method menu", contexts[i]->context_name);
638 639 640 641 642 643 644 645 646 647 648
		}
	      else
		{
		  /* Separate domain directory but the same
		   * translation domain as GTK+. We can't call
		   * bindtextdomain() as that would make GTK+ forget
		   * its own messages.
		   */
		  g_warning ("Input method %s should not use GTK's translation domain %s",
			     contexts[i]->context_id, GETTEXT_PACKAGE);
		  /* Try translating the name in GTK+'s domain */
649
		  translated_name = g_dpgettext2 (GETTEXT_PACKAGE, "input method menu", contexts[i]->context_name);
650 651 652 653
		}
	    }
	  else if (contexts[i]->domain_dirname && contexts[i]->domain_dirname[0])
	    /* Input method has own translation domain and message catalog */
654 655 656 657 658 659
	    {
	      bindtextdomain (contexts[i]->domain,
			      contexts[i]->domain_dirname);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
	      bind_textdomain_codeset (contexts[i]->domain, "UTF-8");
#endif
660
	      translated_name = g_dgettext (contexts[i]->domain, contexts[i]->context_name);
661
	    }
662 663 664 665 666
	  else
	    {
	      /* Different translation domain, but no domain directory */
	      translated_name = contexts[i]->context_name;
	    }
667 668
	}
      else
669 670
	/* Empty or NULL domain. We assume that input method does not
	 * want a translated name in this case.
671 672 673 674 675
	 */
	translated_name = contexts[i]->context_name;
#else
      translated_name = contexts[i]->context_name;
#endif
676 677
      menuitem = gtk_radio_menu_item_new_with_label (group, translated_name);

678 679
      if ((priv->context_id_aux &&
           strcmp (contexts[i]->context_id, priv->context_id_aux) == 0))
680
        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
681 682 683 684 685 686 687

      if (strcmp (contexts[i]->context_id, system_context_id) == 0)
        {
          GtkWidget *label;
          char *text;

          label = gtk_bin_get_child (GTK_BIN (system_menuitem));
688
          text = g_strdup_printf (C_("input method menu", "System (%s)"), translated_name);
689 690
          gtk_label_set_text (GTK_LABEL (label), text);
          g_free (text);
691 692
        }

Manish Singh's avatar
Manish Singh committed
693
      group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
694

695
      g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"),
Manish Singh's avatar
Manish Singh committed
696 697 698
			 (char *)contexts[i]->context_id);
      g_signal_connect (menuitem, "activate",
			G_CALLBACK (activate_cb), context);
Owen Taylor's avatar
Owen Taylor committed
699 700 701 702

      gtk_widget_show (menuitem);
      gtk_menu_shell_append (menushell, menuitem);
    }
703 704

  g_free (contexts);
Owen Taylor's avatar
Owen Taylor committed
705 706
}

707 708 709 710 711 712 713 714 715 716 717 718 719
/**
 * gtk_im_multicontext_get_context_id:
 * @context: a #GtkIMMulticontext
 *
 * Gets the id of the currently active slave of the @context.
 *
 * Returns: the id of the currently active slave
 *
 * Since: 2.16
 */
const char *
gtk_im_multicontext_get_context_id (GtkIMMulticontext *context)
{
720 721 722
  g_return_val_if_fail (GTK_IS_IM_MULTICONTEXT (context), NULL);

  return context->priv->context_id;
723 724
}

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
/**
 * gtk_im_multicontext_set_context_id:
 * @context: a #GtkIMMulticontext
 * @context_id: the id to use 
 *
 * Sets the context id for @context.
 *
 * This causes the currently active slave of @context to be
 * replaced by the slave corresponding to the new context id.
 *
 * Since: 2.16
 */
void
gtk_im_multicontext_set_context_id (GtkIMMulticontext *context,
                                    const char        *context_id)
{
741 742 743 744 745 746
  GtkIMMulticontextPrivate *priv;

  g_return_if_fail (GTK_IS_IM_MULTICONTEXT (context));

  priv = context->priv;

747
  gtk_im_context_reset (GTK_IM_CONTEXT (context));
748 749
  g_free (priv->context_id_aux);
  priv->context_id_aux = g_strdup (context_id);
750 751
  gtk_im_multicontext_set_slave (context, NULL, FALSE);
}
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774

static void
propagate_purpose (GtkIMMulticontext *context)
{
  GtkInputPurpose purpose;
  GtkInputHints hints;

  if (context->priv->slave == NULL)
    return;

  g_object_get (context, "input-purpose", &purpose, NULL);
  g_object_set (context->priv->slave, "input-purpose", purpose, NULL);

  g_object_get (context, "input-hints", &hints, NULL);
  g_object_set (context->priv->slave, "input-hints", hints, NULL);
}

static void
gtk_im_multicontext_notify (GObject      *object,
                            GParamSpec   *pspec)
{
  propagate_purpose (GTK_IM_MULTICONTEXT (object));
}