gtkimmulticontext.c 22.5 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
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"
Tor Lillqvist's avatar
Tor Lillqvist committed
30
#include "gtkprivate.h" /* To get redefinition of GTK_LOCALE_DIR on Win32 */
31

32 33
#define NONE_ID "gtk-im-context-none"

34 35
struct _GtkIMMulticontextPrivate
{
36
  GtkIMContext          *slave;
37

38 39 40 41 42 43 44 45 46
  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;
47 48
};

49 50 51
static void     gtk_im_multicontext_finalize           (GObject                 *object);

static void     gtk_im_multicontext_set_slave          (GtkIMMulticontext       *multicontext,
52 53
							GtkIMContext            *slave,
							gboolean                 finalizing);
54 55 56 57 58

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
59 60
							PangoAttrList          **attrs,
							gint                   *cursor_pos);
61 62 63 64
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
65
static void     gtk_im_multicontext_reset              (GtkIMContext            *context);
66
static void     gtk_im_multicontext_set_cursor_location (GtkIMContext            *context,
67
							GdkRectangle		*area);
68 69
static void     gtk_im_multicontext_set_use_preedit    (GtkIMContext            *context,
							gboolean                 use_preedit);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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);
93

Owen Taylor's avatar
Owen Taylor committed
94 95
static const gchar *global_context_id = NULL;

Matthias Clasen's avatar
Matthias Clasen committed
96
G_DEFINE_TYPE (GtkIMMulticontext, gtk_im_multicontext, GTK_TYPE_IM_CONTEXT)
97 98 99 100 101 102 103 104 105 106 107 108

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);
  
  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
109
  im_context_class->reset = gtk_im_multicontext_reset;
110
  im_context_class->set_cursor_location = gtk_im_multicontext_set_cursor_location;
111
  im_context_class->set_use_preedit = gtk_im_multicontext_set_use_preedit;
112 113
  im_context_class->set_surrounding = gtk_im_multicontext_set_surrounding;
  im_context_class->get_surrounding = gtk_im_multicontext_get_surrounding;
114 115

  gobject_class->finalize = gtk_im_multicontext_finalize;
116 117

  g_type_class_add_private (gobject_class, sizeof (GtkIMMulticontextPrivate));   
118 119 120 121 122
}

static void
gtk_im_multicontext_init (GtkIMMulticontext *multicontext)
{
123
  GtkIMMulticontextPrivate *priv;
124
  
125
  multicontext->priv = G_TYPE_INSTANCE_GET_PRIVATE (multicontext, GTK_TYPE_IM_MULTICONTEXT, GtkIMMulticontextPrivate);
126 127 128 129 130 131
  priv = multicontext->priv;

  priv->slave = NULL;
  priv->use_preedit = TRUE;
  priv->have_cursor_location = FALSE;
  priv->focus_in = FALSE;
132 133
}

Matthias Clasen's avatar
Matthias Clasen committed
134 135 136 137 138 139 140
/**
 * gtk_im_multicontext_new:
 *
 * Creates a new #GtkIMMulticontext.
 *
 * Returns: a new #GtkIMMulticontext.
 **/
141 142 143
GtkIMContext *
gtk_im_multicontext_new (void)
{
Manish Singh's avatar
Manish Singh committed
144
  return g_object_new (GTK_TYPE_IM_MULTICONTEXT, NULL);
145 146 147 148 149
}

static void
gtk_im_multicontext_finalize (GObject *object)
{
150
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (object);
151 152
  GtkIMMulticontextPrivate *priv = multicontext->priv;

153
  gtk_im_multicontext_set_slave (multicontext, NULL, TRUE);
154 155
  g_free (priv->context_id);
  g_free (priv->context_id_aux);
156

Matthias Clasen's avatar
Matthias Clasen committed
157
  G_OBJECT_CLASS (gtk_im_multicontext_parent_class)->finalize (object);
158 159 160 161
}

static void
gtk_im_multicontext_set_slave (GtkIMMulticontext *multicontext,
162 163
			       GtkIMContext      *slave,
			       gboolean           finalizing)
164
{
165
  GtkIMMulticontextPrivate *priv = multicontext->priv;
166 167
  gboolean need_preedit_changed = FALSE;
  
168
  if (priv->slave)
169
    {
170
      if (!finalizing)
171
	gtk_im_context_reset (priv->slave);
172
      
173
      g_signal_handlers_disconnect_by_func (priv->slave,
Manish Singh's avatar
Manish Singh committed
174
					    gtk_im_multicontext_preedit_start_cb,
175
					    multicontext);
176
      g_signal_handlers_disconnect_by_func (priv->slave,
Manish Singh's avatar
Manish Singh committed
177
					    gtk_im_multicontext_preedit_end_cb,
178
					    multicontext);
179
      g_signal_handlers_disconnect_by_func (priv->slave,
Manish Singh's avatar
Manish Singh committed
180
					    gtk_im_multicontext_preedit_changed_cb,
181
					    multicontext);
182
      g_signal_handlers_disconnect_by_func (priv->slave,
Manish Singh's avatar
Manish Singh committed
183
					    gtk_im_multicontext_commit_cb,
184
					    multicontext);
185

186 187
      g_object_unref (priv->slave);
      priv->slave = NULL;
188 189

      if (!finalizing)
190
	need_preedit_changed = TRUE;
191 192
    }
  
193
  priv->slave = slave;
194

195
  if (priv->slave)
196
    {
197
      g_object_ref (priv->slave);
198

199
      g_signal_connect (priv->slave, "preedit-start",
200 201
			G_CALLBACK (gtk_im_multicontext_preedit_start_cb),
			multicontext);
202
      g_signal_connect (priv->slave, "preedit-end",
203 204
			G_CALLBACK (gtk_im_multicontext_preedit_end_cb),
			multicontext);
205
      g_signal_connect (priv->slave, "preedit-changed",
206 207
			G_CALLBACK (gtk_im_multicontext_preedit_changed_cb),
			multicontext);
208
      g_signal_connect (priv->slave, "commit",
209 210
			G_CALLBACK (gtk_im_multicontext_commit_cb),
			multicontext);
211
      g_signal_connect (priv->slave, "retrieve-surrounding",
212 213
			G_CALLBACK (gtk_im_multicontext_retrieve_surrounding_cb),
			multicontext);
214
      g_signal_connect (priv->slave, "delete-surrounding",
215 216
			G_CALLBACK (gtk_im_multicontext_delete_surrounding_cb),
			multicontext);
217
      
218 219 220 221 222 223 224 225
      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);
226
    }
227 228

  if (need_preedit_changed)
229
    g_signal_emit_by_name (multicontext, "preedit-changed");
230 231
}

232 233 234
static const gchar *
get_effective_context_id (GtkIMMulticontext *multicontext)
{
235 236 237 238
  GtkIMMulticontextPrivate *priv = multicontext->priv;

  if (priv->context_id_aux)
    return priv->context_id_aux;
239 240

  if (!global_context_id)
241
    global_context_id = _gtk_im_module_get_default_context_id (priv->client_window);
242 243 244 245

  return global_context_id;
}

246 247 248
static GtkIMContext *
gtk_im_multicontext_get_slave (GtkIMMulticontext *multicontext)
{
249 250 251
  GtkIMMulticontextPrivate *priv = multicontext->priv;

  if (!priv->slave)
Owen Taylor's avatar
Owen Taylor committed
252
    {
253
      GtkIMContext *slave;
254

255
      g_free (priv->context_id);
256

257
      priv->context_id = g_strdup (get_effective_context_id (multicontext));
258

259
      if (g_strcmp0 (priv->context_id, NONE_ID) == 0)
260 261
        return NULL;

262
      slave = _gtk_im_module_create (priv->context_id);
263 264
      gtk_im_multicontext_set_slave (multicontext, slave, FALSE);
      g_object_unref (slave);
Owen Taylor's avatar
Owen Taylor committed
265
    }
266

267
  return priv->slave;
268 269
}

270 271 272 273 274 275 276 277
static void
im_module_setting_changed (GtkSettings *settings, 
                           gpointer     data)
{
  global_context_id = NULL;
}


278 279 280 281 282
static void
gtk_im_multicontext_set_client_window (GtkIMContext *context,
				       GdkWindow    *window)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
283
  GtkIMMulticontextPrivate *priv = multicontext->priv;
284 285 286
  GdkScreen *screen; 
  GtkSettings *settings;
  gboolean connected;
287

288
  priv->client_window = window;
289

290
  if (window)
291
    {
292 293
      screen = gdk_drawable_get_screen (GDK_DRAWABLE (window));
      settings = gtk_settings_get_for_screen (screen);
294

295 296 297 298 299 300 301 302 303 304 305
      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;
        }
306
    }
307

308
  if (g_strcmp0 (priv->context_id, get_effective_context_id (multicontext)) != 0)
309
    gtk_im_multicontext_set_slave (multicontext, NULL, FALSE);
310

311 312
  if (priv->slave)
    gtk_im_context_set_client_window (priv->slave, window);
313 314 315 316 317
}

static void
gtk_im_multicontext_get_preedit_string (GtkIMContext   *context,
					gchar         **str,
Owen Taylor's avatar
Owen Taylor committed
318 319
					PangoAttrList **attrs,
					gint           *cursor_pos)
320 321 322 323 324
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

  if (slave)
Owen Taylor's avatar
Owen Taylor committed
325
    gtk_im_context_get_preedit_string (slave, str, attrs, cursor_pos);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  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);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  else if (event->type == GDK_KEY_PRESS &&
           (event->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK)) == 0)
    {
      gunichar ch;

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

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

          g_signal_emit_by_name (multicontext, "commit", buf);

          return TRUE;
        }
    }

  return FALSE;
365 366 367 368 369 370
}

static void
gtk_im_multicontext_focus_in (GtkIMContext   *context)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
371
  GtkIMMulticontextPrivate *priv = multicontext->priv;
Owen Taylor's avatar
Owen Taylor committed
372 373
  GtkIMContext *slave;

374
  if (g_strcmp0 (priv->context_id, get_effective_context_id (multicontext)) != 0)
375
    gtk_im_multicontext_set_slave (multicontext, NULL, FALSE);
Owen Taylor's avatar
Owen Taylor committed
376 377

  slave = gtk_im_multicontext_get_slave (multicontext);
378 379 380

  priv->focus_in = TRUE;

381
  if (slave)
Tor Lillqvist's avatar
Tor Lillqvist committed
382
    gtk_im_context_focus_in (slave);
383 384 385 386 387 388
}

static void
gtk_im_multicontext_focus_out (GtkIMContext   *context)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
389
  GtkIMMulticontextPrivate *priv = multicontext->priv;
390 391
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

392 393
  priv->focus_in = FALSE;

394
  if (slave)
Tor Lillqvist's avatar
Tor Lillqvist committed
395
    gtk_im_context_focus_out (slave);
396 397
}

Owen Taylor's avatar
Owen Taylor committed
398 399 400 401 402 403 404 405 406 407
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);
}

408
static void
409 410
gtk_im_multicontext_set_cursor_location (GtkIMContext   *context,
					 GdkRectangle   *area)
411 412
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
413
  GtkIMMulticontextPrivate *priv = multicontext->priv;
414 415
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

416 417
  priv->have_cursor_location = TRUE;
  priv->cursor_location = *area;
418

419
  if (slave)
420
    gtk_im_context_set_cursor_location (slave, area);
421 422
}

423 424 425 426 427
static void
gtk_im_multicontext_set_use_preedit (GtkIMContext   *context,
				     gboolean	    use_preedit)
{
  GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
428
  GtkIMMulticontextPrivate *priv = multicontext->priv;
429 430
  GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);

431 432
  use_preedit = use_preedit != FALSE;

433
  priv->use_preedit = use_preedit;
434

435 436 437 438
  if (slave)
    gtk_im_context_set_use_preedit (slave, use_preedit);
}

439 440 441 442 443 444 445 446 447
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)
448
    return gtk_im_context_get_surrounding (slave, text, cursor_index);
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  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);
}

473
static void
474 475 476
gtk_im_multicontext_preedit_start_cb   (GtkIMContext      *slave,
					GtkIMMulticontext *multicontext)
{
477
  g_signal_emit_by_name (multicontext, "preedit-start");
478 479
}

480
static void
481 482 483
gtk_im_multicontext_preedit_end_cb (GtkIMContext      *slave,
				    GtkIMMulticontext *multicontext)
{
484
  g_signal_emit_by_name (multicontext, "preedit-end");
485 486
}

487
static void
488 489 490
gtk_im_multicontext_preedit_changed_cb (GtkIMContext      *slave,
					GtkIMMulticontext *multicontext)
{
491
  g_signal_emit_by_name (multicontext, "preedit-changed");
492 493
}

494
static void
495 496 497 498
gtk_im_multicontext_commit_cb (GtkIMContext      *slave,
			       const gchar       *str,
			       GtkIMMulticontext *multicontext)
{
499
  g_signal_emit_by_name (multicontext, "commit", str);
500 501
}

502 503 504 505 506 507
static gboolean
gtk_im_multicontext_retrieve_surrounding_cb (GtkIMContext      *slave,
					     GtkIMMulticontext *multicontext)
{
  gboolean result;
  
508
  g_signal_emit_by_name (multicontext, "retrieve-surrounding", &result);
509 510 511 512 513 514 515 516 517 518 519 520

  return result;
}

static gboolean
gtk_im_multicontext_delete_surrounding_cb (GtkIMContext      *slave,
					   gint               offset,
					   gint               n_chars,
					   GtkIMMulticontext *multicontext)
{
  gboolean result;
  
521
  g_signal_emit_by_name (multicontext, "delete-surrounding",
522 523 524 525 526
			 offset, n_chars, &result);

  return result;
}

Owen Taylor's avatar
Owen Taylor committed
527 528 529 530
static void
activate_cb (GtkWidget         *menuitem,
	     GtkIMMulticontext *context)
{
531
  if (gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menuitem)))
532
    {
Manish Singh's avatar
Manish Singh committed
533
      const gchar *id = g_object_get_data (G_OBJECT (menuitem), "gtk-context-id");
Owen Taylor's avatar
Owen Taylor committed
534

535
      gtk_im_multicontext_set_context_id (context, id);
536
    }
Owen Taylor's avatar
Owen Taylor committed
537 538
}

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
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
559 560
/**
 * gtk_im_multicontext_append_menuitems:
561
 * @context: a #GtkIMMulticontext
Owen Taylor's avatar
Owen Taylor committed
562 563 564 565 566 567 568 569 570 571
 * @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)
{
572
  GtkIMMulticontextPrivate *priv = context->priv;
Owen Taylor's avatar
Owen Taylor committed
573
  const GtkIMContextInfo **contexts;
574
  guint n_contexts, i;
575
  GSList *group = NULL;
576 577
  GtkWidget *menuitem, *system_menuitem;
  const char *system_context_id; 
578 579

  system_context_id = _gtk_im_module_get_default_context_id (priv->client_window);
580
  system_menuitem = menuitem = gtk_radio_menu_item_new_with_label (group, C_("input method menu", "System"));
581
  if (!priv->context_id_aux)
582 583 584 585 586 587 588 589
    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);

590
  menuitem = gtk_radio_menu_item_new_with_label (group, C_("input method menu", "None"));
591
  if (g_strcmp0 (priv->context_id_aux, NONE_ID) == 0)
592 593 594 595 596 597 598
    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
  g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"), NONE_ID);
  g_signal_connect (menuitem, "activate", G_CALLBACK (activate_cb), context);
  gtk_widget_show (menuitem);
  gtk_menu_shell_append (menushell, menuitem);
  group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
  
599 600 601 602
  menuitem = gtk_separator_menu_item_new ();
  gtk_widget_show (menuitem);
  gtk_menu_shell_append (menushell, menuitem);

Owen Taylor's avatar
Owen Taylor committed
603 604
  _gtk_im_module_list (&contexts, &n_contexts);

605
  for (i = 0; i < n_contexts; i++)
Owen Taylor's avatar
Owen Taylor committed
606
    {
607 608
      const gchar *translated_name;
#ifdef ENABLE_NLS
609
      if (contexts[i]->domain && contexts[i]->domain[0])
610
	{
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	  if (strcmp (contexts[i]->domain, GETTEXT_PACKAGE) == 0)
	    {
	      /* Same translation domain as GTK+ */
	      if (!(contexts[i]->domain_dirname && contexts[i]->domain_dirname[0]) ||
		  pathnamecmp (contexts[i]->domain_dirname, GTK_LOCALEDIR) == 0)
		{
		  /* Empty or NULL, domain directory, or same as
		   * GTK+. Input method may have a name in the GTK+
		   * message catalog.
		   */
		  translated_name = _(contexts[i]->context_name);
		}
	      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 */
		  translated_name = _(contexts[i]->context_name);
		}
	    }
	  else if (contexts[i]->domain_dirname && contexts[i]->domain_dirname[0])
	    /* Input method has own translation domain and message catalog */
638 639 640 641 642 643
	    {
	      bindtextdomain (contexts[i]->domain,
			      contexts[i]->domain_dirname);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
	      bind_textdomain_codeset (contexts[i]->domain, "UTF-8");
#endif
644
	      translated_name = g_dgettext (contexts[i]->domain, contexts[i]->context_name);
645
	    }
646 647 648 649 650
	  else
	    {
	      /* Different translation domain, but no domain directory */
	      translated_name = contexts[i]->context_name;
	    }
651 652
	}
      else
653 654
	/* Empty or NULL domain. We assume that input method does not
	 * want a translated name in this case.
655 656 657 658 659
	 */
	translated_name = contexts[i]->context_name;
#else
      translated_name = contexts[i]->context_name;
#endif
660
      menuitem = gtk_radio_menu_item_new_with_label (group,
661
						     translated_name);
662
      
663 664
      if ((priv->context_id_aux &&
           strcmp (contexts[i]->context_id, priv->context_id_aux) == 0))
665
        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
666 667 668 669 670 671 672

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

          label = gtk_bin_get_child (GTK_BIN (system_menuitem));
673
          text = g_strdup_printf (C_("input method menu", "System (%s)"), translated_name);
674 675 676 677
          gtk_label_set_text (GTK_LABEL (label), text);
          g_free (text);
        }     
 
Manish Singh's avatar
Manish Singh committed
678
      group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
679
      
680
      g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"),
Manish Singh's avatar
Manish Singh committed
681 682 683
			 (char *)contexts[i]->context_id);
      g_signal_connect (menuitem, "activate",
			G_CALLBACK (activate_cb), context);
Owen Taylor's avatar
Owen Taylor committed
684 685 686 687

      gtk_widget_show (menuitem);
      gtk_menu_shell_append (menushell, menuitem);
    }
688 689

  g_free (contexts);
Owen Taylor's avatar
Owen Taylor committed
690 691
}

692 693 694 695 696 697 698 699 700 701 702 703 704
/**
 * 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)
{
705 706 707
  g_return_val_if_fail (GTK_IS_IM_MULTICONTEXT (context), NULL);

  return context->priv->context_id;
708 709
}

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
/**
 * 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)
{
726 727 728 729 730 731
  GtkIMMulticontextPrivate *priv;

  g_return_if_fail (GTK_IS_IM_MULTICONTEXT (context));

  priv = context->priv;

732
  gtk_im_context_reset (GTK_IM_CONTEXT (context));
733 734
  g_free (priv->context_id_aux);
  priv->context_id_aux = g_strdup (context_id);
735 736
  gtk_im_multicontext_set_slave (context, NULL, FALSE);
}