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