]> Pileus Git - ~andy/gtk/blob - gtk/gtkimmulticontext.c
Merge branch 'master' into broadway
[~andy/gtk] / gtk / gtkimmulticontext.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include <string.h>
23 #include <locale.h>
24
25 #include "gtkimmulticontext.h"
26 #include "gtkimmodule.h"
27 #include "gtkmain.h"
28 #include "gtkradiomenuitem.h"
29 #include "gtkintl.h"
30 #include "gtkprivate.h" /* To get redefinition of GTK_LOCALE_DIR on Win32 */
31
32
33 /**
34  * SECTION:gtkimmulticontext
35  * @Short_description: An input method context supporting multiple, loadable input methods
36  * @Title: GtkIMMulticontext
37  */
38
39
40 #define NONE_ID "gtk-im-context-none"
41
42 struct _GtkIMMulticontextPrivate
43 {
44   GtkIMContext          *slave;
45
46   GdkWindow             *client_window;
47   GdkRectangle           cursor_location;
48
49   gchar                 *context_id;
50   gchar                 *context_id_aux;
51
52   guint                  use_preedit          : 1;
53   guint                  have_cursor_location : 1;
54   guint                  focus_in             : 1;
55 };
56
57 static void     gtk_im_multicontext_finalize           (GObject                 *object);
58
59 static void     gtk_im_multicontext_set_slave          (GtkIMMulticontext       *multicontext,
60                                                         GtkIMContext            *slave,
61                                                         gboolean                 finalizing);
62
63 static void     gtk_im_multicontext_set_client_window  (GtkIMContext            *context,
64                                                         GdkWindow               *window);
65 static void     gtk_im_multicontext_get_preedit_string (GtkIMContext            *context,
66                                                         gchar                  **str,
67                                                         PangoAttrList          **attrs,
68                                                         gint                   *cursor_pos);
69 static gboolean gtk_im_multicontext_filter_keypress    (GtkIMContext            *context,
70                                                         GdkEventKey             *event);
71 static void     gtk_im_multicontext_focus_in           (GtkIMContext            *context);
72 static void     gtk_im_multicontext_focus_out          (GtkIMContext            *context);
73 static void     gtk_im_multicontext_reset              (GtkIMContext            *context);
74 static void     gtk_im_multicontext_set_cursor_location (GtkIMContext            *context,
75                                                         GdkRectangle            *area);
76 static void     gtk_im_multicontext_set_use_preedit    (GtkIMContext            *context,
77                                                         gboolean                 use_preedit);
78 static gboolean gtk_im_multicontext_get_surrounding    (GtkIMContext            *context,
79                                                         gchar                  **text,
80                                                         gint                    *cursor_index);
81 static void     gtk_im_multicontext_set_surrounding    (GtkIMContext            *context,
82                                                         const char              *text,
83                                                         gint                     len,
84                                                         gint                     cursor_index);
85
86 static void     gtk_im_multicontext_preedit_start_cb        (GtkIMContext      *slave,
87                                                              GtkIMMulticontext *multicontext);
88 static void     gtk_im_multicontext_preedit_end_cb          (GtkIMContext      *slave,
89                                                              GtkIMMulticontext *multicontext);
90 static void     gtk_im_multicontext_preedit_changed_cb      (GtkIMContext      *slave,
91                                                              GtkIMMulticontext *multicontext);
92 static void     gtk_im_multicontext_commit_cb               (GtkIMContext      *slave,
93                                                              const gchar       *str,
94                                                              GtkIMMulticontext *multicontext);
95 static gboolean gtk_im_multicontext_retrieve_surrounding_cb (GtkIMContext      *slave,
96                                                              GtkIMMulticontext *multicontext);
97 static gboolean gtk_im_multicontext_delete_surrounding_cb   (GtkIMContext      *slave,
98                                                              gint               offset,
99                                                              gint               n_chars,
100                                                              GtkIMMulticontext *multicontext);
101
102 static const gchar *global_context_id = NULL;
103
104 G_DEFINE_TYPE (GtkIMMulticontext, gtk_im_multicontext, GTK_TYPE_IM_CONTEXT)
105
106 static void
107 gtk_im_multicontext_class_init (GtkIMMulticontextClass *class)
108 {
109   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
110   GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
111   
112   im_context_class->set_client_window = gtk_im_multicontext_set_client_window;
113   im_context_class->get_preedit_string = gtk_im_multicontext_get_preedit_string;
114   im_context_class->filter_keypress = gtk_im_multicontext_filter_keypress;
115   im_context_class->focus_in = gtk_im_multicontext_focus_in;
116   im_context_class->focus_out = gtk_im_multicontext_focus_out;
117   im_context_class->reset = gtk_im_multicontext_reset;
118   im_context_class->set_cursor_location = gtk_im_multicontext_set_cursor_location;
119   im_context_class->set_use_preedit = gtk_im_multicontext_set_use_preedit;
120   im_context_class->set_surrounding = gtk_im_multicontext_set_surrounding;
121   im_context_class->get_surrounding = gtk_im_multicontext_get_surrounding;
122
123   gobject_class->finalize = gtk_im_multicontext_finalize;
124
125   g_type_class_add_private (gobject_class, sizeof (GtkIMMulticontextPrivate));   
126 }
127
128 static void
129 gtk_im_multicontext_init (GtkIMMulticontext *multicontext)
130 {
131   GtkIMMulticontextPrivate *priv;
132   
133   multicontext->priv = G_TYPE_INSTANCE_GET_PRIVATE (multicontext, GTK_TYPE_IM_MULTICONTEXT, GtkIMMulticontextPrivate);
134   priv = multicontext->priv;
135
136   priv->slave = NULL;
137   priv->use_preedit = TRUE;
138   priv->have_cursor_location = FALSE;
139   priv->focus_in = FALSE;
140 }
141
142 /**
143  * gtk_im_multicontext_new:
144  *
145  * Creates a new #GtkIMMulticontext.
146  *
147  * Returns: a new #GtkIMMulticontext.
148  **/
149 GtkIMContext *
150 gtk_im_multicontext_new (void)
151 {
152   return g_object_new (GTK_TYPE_IM_MULTICONTEXT, NULL);
153 }
154
155 static void
156 gtk_im_multicontext_finalize (GObject *object)
157 {
158   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (object);
159   GtkIMMulticontextPrivate *priv = multicontext->priv;
160
161   gtk_im_multicontext_set_slave (multicontext, NULL, TRUE);
162   g_free (priv->context_id);
163   g_free (priv->context_id_aux);
164
165   G_OBJECT_CLASS (gtk_im_multicontext_parent_class)->finalize (object);
166 }
167
168 static void
169 gtk_im_multicontext_set_slave (GtkIMMulticontext *multicontext,
170                                GtkIMContext      *slave,
171                                gboolean           finalizing)
172 {
173   GtkIMMulticontextPrivate *priv = multicontext->priv;
174   gboolean need_preedit_changed = FALSE;
175   
176   if (priv->slave)
177     {
178       if (!finalizing)
179         gtk_im_context_reset (priv->slave);
180       
181       g_signal_handlers_disconnect_by_func (priv->slave,
182                                             gtk_im_multicontext_preedit_start_cb,
183                                             multicontext);
184       g_signal_handlers_disconnect_by_func (priv->slave,
185                                             gtk_im_multicontext_preedit_end_cb,
186                                             multicontext);
187       g_signal_handlers_disconnect_by_func (priv->slave,
188                                             gtk_im_multicontext_preedit_changed_cb,
189                                             multicontext);
190       g_signal_handlers_disconnect_by_func (priv->slave,
191                                             gtk_im_multicontext_commit_cb,
192                                             multicontext);
193
194       g_object_unref (priv->slave);
195       priv->slave = NULL;
196
197       if (!finalizing)
198         need_preedit_changed = TRUE;
199     }
200   
201   priv->slave = slave;
202
203   if (priv->slave)
204     {
205       g_object_ref (priv->slave);
206
207       g_signal_connect (priv->slave, "preedit-start",
208                         G_CALLBACK (gtk_im_multicontext_preedit_start_cb),
209                         multicontext);
210       g_signal_connect (priv->slave, "preedit-end",
211                         G_CALLBACK (gtk_im_multicontext_preedit_end_cb),
212                         multicontext);
213       g_signal_connect (priv->slave, "preedit-changed",
214                         G_CALLBACK (gtk_im_multicontext_preedit_changed_cb),
215                         multicontext);
216       g_signal_connect (priv->slave, "commit",
217                         G_CALLBACK (gtk_im_multicontext_commit_cb),
218                         multicontext);
219       g_signal_connect (priv->slave, "retrieve-surrounding",
220                         G_CALLBACK (gtk_im_multicontext_retrieve_surrounding_cb),
221                         multicontext);
222       g_signal_connect (priv->slave, "delete-surrounding",
223                         G_CALLBACK (gtk_im_multicontext_delete_surrounding_cb),
224                         multicontext);
225       
226       if (!priv->use_preedit)   /* Default is TRUE */
227         gtk_im_context_set_use_preedit (slave, FALSE);
228       if (priv->client_window)
229         gtk_im_context_set_client_window (slave, priv->client_window);
230       if (priv->have_cursor_location)
231         gtk_im_context_set_cursor_location (slave, &priv->cursor_location);
232       if (priv->focus_in)
233         gtk_im_context_focus_in (slave);
234     }
235
236   if (need_preedit_changed)
237     g_signal_emit_by_name (multicontext, "preedit-changed");
238 }
239
240 static const gchar *
241 get_effective_context_id (GtkIMMulticontext *multicontext)
242 {
243   GtkIMMulticontextPrivate *priv = multicontext->priv;
244
245   if (priv->context_id_aux)
246     return priv->context_id_aux;
247
248   if (!global_context_id)
249     global_context_id = _gtk_im_module_get_default_context_id (priv->client_window);
250
251   return global_context_id;
252 }
253
254 static GtkIMContext *
255 gtk_im_multicontext_get_slave (GtkIMMulticontext *multicontext)
256 {
257   GtkIMMulticontextPrivate *priv = multicontext->priv;
258
259   if (!priv->slave)
260     {
261       GtkIMContext *slave;
262
263       g_free (priv->context_id);
264
265       priv->context_id = g_strdup (get_effective_context_id (multicontext));
266
267       if (g_strcmp0 (priv->context_id, NONE_ID) == 0)
268         return NULL;
269
270       slave = _gtk_im_module_create (priv->context_id);
271       gtk_im_multicontext_set_slave (multicontext, slave, FALSE);
272       g_object_unref (slave);
273     }
274
275   return priv->slave;
276 }
277
278 static void
279 im_module_setting_changed (GtkSettings *settings, 
280                            gpointer     data)
281 {
282   global_context_id = NULL;
283 }
284
285
286 static void
287 gtk_im_multicontext_set_client_window (GtkIMContext *context,
288                                        GdkWindow    *window)
289 {
290   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
291   GtkIMMulticontextPrivate *priv = multicontext->priv;
292   GdkScreen *screen; 
293   GtkSettings *settings;
294   gboolean connected;
295
296   priv->client_window = window;
297
298   if (window)
299     {
300       screen = gdk_window_get_screen (window);
301       settings = gtk_settings_get_for_screen (screen);
302
303       connected = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
304                                                       "gtk-im-module-connected"));
305       if (!connected)
306         {
307           g_signal_connect (settings, "notify::gtk-im-module",
308                             G_CALLBACK (im_module_setting_changed), NULL);
309           g_object_set_data (G_OBJECT (settings), "gtk-im-module-connected",
310                              GINT_TO_POINTER (TRUE));
311
312           global_context_id = NULL;
313         }
314     }
315
316   if (g_strcmp0 (priv->context_id, get_effective_context_id (multicontext)) != 0)
317     gtk_im_multicontext_set_slave (multicontext, NULL, FALSE);
318
319   if (priv->slave)
320     gtk_im_context_set_client_window (priv->slave, window);
321 }
322
323 static void
324 gtk_im_multicontext_get_preedit_string (GtkIMContext   *context,
325                                         gchar         **str,
326                                         PangoAttrList **attrs,
327                                         gint           *cursor_pos)
328 {
329   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
330   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
331
332   if (slave)
333     gtk_im_context_get_preedit_string (slave, str, attrs, cursor_pos);
334   else
335     {
336       if (str)
337         *str = g_strdup ("");
338       if (attrs)
339         *attrs = pango_attr_list_new ();
340     }
341 }
342
343 static gboolean
344 gtk_im_multicontext_filter_keypress (GtkIMContext *context,
345                                      GdkEventKey  *event)
346 {
347   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
348   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
349
350   if (slave)
351     return gtk_im_context_filter_keypress (slave, event);
352   else if (event->type == GDK_KEY_PRESS &&
353            (event->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK)) == 0)
354     {
355       gunichar ch;
356
357       ch = gdk_keyval_to_unicode (event->keyval);
358       if (ch != 0)
359         {
360           gint len;
361           gchar buf[10];
362
363           len = g_unichar_to_utf8 (ch, buf);
364           buf[len] = '\0';
365
366           g_signal_emit_by_name (multicontext, "commit", buf);
367
368           return TRUE;
369         }
370     }
371
372   return FALSE;
373 }
374
375 static void
376 gtk_im_multicontext_focus_in (GtkIMContext   *context)
377 {
378   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
379   GtkIMMulticontextPrivate *priv = multicontext->priv;
380   GtkIMContext *slave;
381
382   if (g_strcmp0 (priv->context_id, get_effective_context_id (multicontext)) != 0)
383     gtk_im_multicontext_set_slave (multicontext, NULL, FALSE);
384
385   slave = gtk_im_multicontext_get_slave (multicontext);
386
387   priv->focus_in = TRUE;
388
389   if (slave)
390     gtk_im_context_focus_in (slave);
391 }
392
393 static void
394 gtk_im_multicontext_focus_out (GtkIMContext   *context)
395 {
396   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
397   GtkIMMulticontextPrivate *priv = multicontext->priv;
398   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
399
400   priv->focus_in = FALSE;
401
402   if (slave)
403     gtk_im_context_focus_out (slave);
404 }
405
406 static void
407 gtk_im_multicontext_reset (GtkIMContext   *context)
408 {
409   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
410   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
411
412   if (slave)
413     gtk_im_context_reset (slave);
414 }
415
416 static void
417 gtk_im_multicontext_set_cursor_location (GtkIMContext   *context,
418                                          GdkRectangle   *area)
419 {
420   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
421   GtkIMMulticontextPrivate *priv = multicontext->priv;
422   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
423
424   priv->have_cursor_location = TRUE;
425   priv->cursor_location = *area;
426
427   if (slave)
428     gtk_im_context_set_cursor_location (slave, area);
429 }
430
431 static void
432 gtk_im_multicontext_set_use_preedit (GtkIMContext   *context,
433                                      gboolean       use_preedit)
434 {
435   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
436   GtkIMMulticontextPrivate *priv = multicontext->priv;
437   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
438
439   use_preedit = use_preedit != FALSE;
440
441   priv->use_preedit = use_preedit;
442
443   if (slave)
444     gtk_im_context_set_use_preedit (slave, use_preedit);
445 }
446
447 static gboolean
448 gtk_im_multicontext_get_surrounding (GtkIMContext  *context,
449                                      gchar        **text,
450                                      gint          *cursor_index)
451 {
452   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
453   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
454
455   if (slave)
456     return gtk_im_context_get_surrounding (slave, text, cursor_index);
457   else
458     {
459       if (text)
460         *text = NULL;
461       if (cursor_index)
462         *cursor_index = 0;
463
464       return FALSE;
465     }
466 }
467
468 static void
469 gtk_im_multicontext_set_surrounding (GtkIMContext *context,
470                                      const char   *text,
471                                      gint          len,
472                                      gint          cursor_index)
473 {
474   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
475   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
476
477   if (slave)
478     gtk_im_context_set_surrounding (slave, text, len, cursor_index);
479 }
480
481 static void
482 gtk_im_multicontext_preedit_start_cb   (GtkIMContext      *slave,
483                                         GtkIMMulticontext *multicontext)
484 {
485   g_signal_emit_by_name (multicontext, "preedit-start");
486 }
487
488 static void
489 gtk_im_multicontext_preedit_end_cb (GtkIMContext      *slave,
490                                     GtkIMMulticontext *multicontext)
491 {
492   g_signal_emit_by_name (multicontext, "preedit-end");
493 }
494
495 static void
496 gtk_im_multicontext_preedit_changed_cb (GtkIMContext      *slave,
497                                         GtkIMMulticontext *multicontext)
498 {
499   g_signal_emit_by_name (multicontext, "preedit-changed");
500 }
501
502 static void
503 gtk_im_multicontext_commit_cb (GtkIMContext      *slave,
504                                const gchar       *str,
505                                GtkIMMulticontext *multicontext)
506 {
507   g_signal_emit_by_name (multicontext, "commit", str);
508 }
509
510 static gboolean
511 gtk_im_multicontext_retrieve_surrounding_cb (GtkIMContext      *slave,
512                                              GtkIMMulticontext *multicontext)
513 {
514   gboolean result;
515   
516   g_signal_emit_by_name (multicontext, "retrieve-surrounding", &result);
517
518   return result;
519 }
520
521 static gboolean
522 gtk_im_multicontext_delete_surrounding_cb (GtkIMContext      *slave,
523                                            gint               offset,
524                                            gint               n_chars,
525                                            GtkIMMulticontext *multicontext)
526 {
527   gboolean result;
528   
529   g_signal_emit_by_name (multicontext, "delete-surrounding",
530                          offset, n_chars, &result);
531
532   return result;
533 }
534
535 static void
536 activate_cb (GtkWidget         *menuitem,
537              GtkIMMulticontext *context)
538 {
539   if (gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menuitem)))
540     {
541       const gchar *id = g_object_get_data (G_OBJECT (menuitem), "gtk-context-id");
542
543       gtk_im_multicontext_set_context_id (context, id);
544     }
545 }
546
547 static int
548 pathnamecmp (const char *a,
549              const char *b)
550 {
551 #ifndef G_OS_WIN32
552   return strcmp (a, b);
553 #else
554   /* Ignore case insensitivity, probably not that relevant here. Just
555    * make sure slash and backslash compare equal.
556    */
557   while (*a && *b)
558     if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) ||
559         *a == *b)
560       a++, b++;
561     else
562       return (*a - *b);
563   return (*a - *b);
564 #endif
565 }
566
567 /**
568  * gtk_im_multicontext_append_menuitems:
569  * @context: a #GtkIMMulticontext
570  * @menushell: a #GtkMenuShell
571  * 
572  * Add menuitems for various available input methods to a menu;
573  * the menuitems, when selected, will switch the input method
574  * for the context and the global default input method.
575  **/
576 void
577 gtk_im_multicontext_append_menuitems (GtkIMMulticontext *context,
578                                       GtkMenuShell      *menushell)
579 {
580   GtkIMMulticontextPrivate *priv = context->priv;
581   const GtkIMContextInfo **contexts;
582   guint n_contexts, i;
583   GSList *group = NULL;
584   GtkWidget *menuitem, *system_menuitem;
585   const char *system_context_id; 
586
587   system_context_id = _gtk_im_module_get_default_context_id (priv->client_window);
588   system_menuitem = menuitem = gtk_radio_menu_item_new_with_label (group, C_("input method menu", "System"));
589   if (!priv->context_id_aux)
590     gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
591   group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
592   g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"), NULL);
593   g_signal_connect (menuitem, "activate", G_CALLBACK (activate_cb), context);
594
595   gtk_widget_show (menuitem);
596   gtk_menu_shell_append (menushell, menuitem);
597
598   menuitem = gtk_radio_menu_item_new_with_label (group, C_("input method menu", "None"));
599   if (g_strcmp0 (priv->context_id_aux, NONE_ID) == 0)
600     gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
601   g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"), NONE_ID);
602   g_signal_connect (menuitem, "activate", G_CALLBACK (activate_cb), context);
603   gtk_widget_show (menuitem);
604   gtk_menu_shell_append (menushell, menuitem);
605   group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
606   
607   menuitem = gtk_separator_menu_item_new ();
608   gtk_widget_show (menuitem);
609   gtk_menu_shell_append (menushell, menuitem);
610
611   _gtk_im_module_list (&contexts, &n_contexts);
612
613   for (i = 0; i < n_contexts; i++)
614     {
615       const gchar *translated_name;
616 #ifdef ENABLE_NLS
617       if (contexts[i]->domain && contexts[i]->domain[0])
618         {
619           if (strcmp (contexts[i]->domain, GETTEXT_PACKAGE) == 0)
620             {
621               /* Same translation domain as GTK+ */
622               if (!(contexts[i]->domain_dirname && contexts[i]->domain_dirname[0]) ||
623                   pathnamecmp (contexts[i]->domain_dirname, GTK_LOCALEDIR) == 0)
624                 {
625                   /* Empty or NULL, domain directory, or same as
626                    * GTK+. Input method may have a name in the GTK+
627                    * message catalog.
628                    */
629                   translated_name = _(contexts[i]->context_name);
630                 }
631               else
632                 {
633                   /* Separate domain directory but the same
634                    * translation domain as GTK+. We can't call
635                    * bindtextdomain() as that would make GTK+ forget
636                    * its own messages.
637                    */
638                   g_warning ("Input method %s should not use GTK's translation domain %s",
639                              contexts[i]->context_id, GETTEXT_PACKAGE);
640                   /* Try translating the name in GTK+'s domain */
641                   translated_name = _(contexts[i]->context_name);
642                 }
643             }
644           else if (contexts[i]->domain_dirname && contexts[i]->domain_dirname[0])
645             /* Input method has own translation domain and message catalog */
646             {
647               bindtextdomain (contexts[i]->domain,
648                               contexts[i]->domain_dirname);
649 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
650               bind_textdomain_codeset (contexts[i]->domain, "UTF-8");
651 #endif
652               translated_name = g_dgettext (contexts[i]->domain, contexts[i]->context_name);
653             }
654           else
655             {
656               /* Different translation domain, but no domain directory */
657               translated_name = contexts[i]->context_name;
658             }
659         }
660       else
661         /* Empty or NULL domain. We assume that input method does not
662          * want a translated name in this case.
663          */
664         translated_name = contexts[i]->context_name;
665 #else
666       translated_name = contexts[i]->context_name;
667 #endif
668       menuitem = gtk_radio_menu_item_new_with_label (group,
669                                                      translated_name);
670       
671       if ((priv->context_id_aux &&
672            strcmp (contexts[i]->context_id, priv->context_id_aux) == 0))
673         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
674
675       if (strcmp (contexts[i]->context_id, system_context_id) == 0)
676         {
677           GtkWidget *label;
678           char *text;
679
680           label = gtk_bin_get_child (GTK_BIN (system_menuitem));
681           text = g_strdup_printf (C_("input method menu", "System (%s)"), translated_name);
682           gtk_label_set_text (GTK_LABEL (label), text);
683           g_free (text);
684         }     
685  
686       group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
687       
688       g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"),
689                          (char *)contexts[i]->context_id);
690       g_signal_connect (menuitem, "activate",
691                         G_CALLBACK (activate_cb), context);
692
693       gtk_widget_show (menuitem);
694       gtk_menu_shell_append (menushell, menuitem);
695     }
696
697   g_free (contexts);
698 }
699
700 /**
701  * gtk_im_multicontext_get_context_id:
702  * @context: a #GtkIMMulticontext
703  *
704  * Gets the id of the currently active slave of the @context.
705  *
706  * Returns: the id of the currently active slave
707  *
708  * Since: 2.16
709  */
710 const char *
711 gtk_im_multicontext_get_context_id (GtkIMMulticontext *context)
712 {
713   g_return_val_if_fail (GTK_IS_IM_MULTICONTEXT (context), NULL);
714
715   return context->priv->context_id;
716 }
717
718 /**
719  * gtk_im_multicontext_set_context_id:
720  * @context: a #GtkIMMulticontext
721  * @context_id: the id to use 
722  *
723  * Sets the context id for @context.
724  *
725  * This causes the currently active slave of @context to be
726  * replaced by the slave corresponding to the new context id.
727  *
728  * Since: 2.16
729  */
730 void
731 gtk_im_multicontext_set_context_id (GtkIMMulticontext *context,
732                                     const char        *context_id)
733 {
734   GtkIMMulticontextPrivate *priv;
735
736   g_return_if_fail (GTK_IS_IM_MULTICONTEXT (context));
737
738   priv = context->priv;
739
740   gtk_im_context_reset (GTK_IM_CONTEXT (context));
741   g_free (priv->context_id_aux);
742   priv->context_id_aux = g_strdup (context_id);
743   gtk_im_multicontext_set_slave (context, NULL, FALSE);
744 }