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