]> Pileus Git - ~andy/gtk/blob - gtk/gtkimmulticontext.c
gtk: remove the private GTK_NO_TEXT_INPUT_MOD_MASK
[~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     {
352       return gtk_im_context_filter_keypress (slave, event);
353     }
354   else
355     {
356       GdkDisplay *display;
357       GdkModifierType no_text_input_mask;
358
359       display = gdk_window_get_display (event->window);
360
361       no_text_input_mask =
362         gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
363                                       GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
364
365       if (event->type == GDK_KEY_PRESS &&
366           (event->state & no_text_input_mask) == 0)
367         {
368           gunichar ch;
369
370           ch = gdk_keyval_to_unicode (event->keyval);
371           if (ch != 0 && !g_unichar_iscntrl (ch))
372             {
373               gint len;
374               gchar buf[10];
375
376               len = g_unichar_to_utf8 (ch, buf);
377               buf[len] = '\0';
378
379               g_signal_emit_by_name (multicontext, "commit", buf);
380
381               return TRUE;
382             }
383         }
384     }
385
386   return FALSE;
387 }
388
389 static void
390 gtk_im_multicontext_focus_in (GtkIMContext   *context)
391 {
392   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
393   GtkIMMulticontextPrivate *priv = multicontext->priv;
394   GtkIMContext *slave;
395
396   if (g_strcmp0 (priv->context_id, get_effective_context_id (multicontext)) != 0)
397     gtk_im_multicontext_set_slave (multicontext, NULL, FALSE);
398
399   slave = gtk_im_multicontext_get_slave (multicontext);
400
401   priv->focus_in = TRUE;
402
403   if (slave)
404     gtk_im_context_focus_in (slave);
405 }
406
407 static void
408 gtk_im_multicontext_focus_out (GtkIMContext   *context)
409 {
410   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
411   GtkIMMulticontextPrivate *priv = multicontext->priv;
412   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
413
414   priv->focus_in = FALSE;
415
416   if (slave)
417     gtk_im_context_focus_out (slave);
418 }
419
420 static void
421 gtk_im_multicontext_reset (GtkIMContext   *context)
422 {
423   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
424   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
425
426   if (slave)
427     gtk_im_context_reset (slave);
428 }
429
430 static void
431 gtk_im_multicontext_set_cursor_location (GtkIMContext   *context,
432                                          GdkRectangle   *area)
433 {
434   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
435   GtkIMMulticontextPrivate *priv = multicontext->priv;
436   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
437
438   priv->have_cursor_location = TRUE;
439   priv->cursor_location = *area;
440
441   if (slave)
442     gtk_im_context_set_cursor_location (slave, area);
443 }
444
445 static void
446 gtk_im_multicontext_set_use_preedit (GtkIMContext   *context,
447                                      gboolean       use_preedit)
448 {
449   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
450   GtkIMMulticontextPrivate *priv = multicontext->priv;
451   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
452
453   use_preedit = use_preedit != FALSE;
454
455   priv->use_preedit = use_preedit;
456
457   if (slave)
458     gtk_im_context_set_use_preedit (slave, use_preedit);
459 }
460
461 static gboolean
462 gtk_im_multicontext_get_surrounding (GtkIMContext  *context,
463                                      gchar        **text,
464                                      gint          *cursor_index)
465 {
466   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
467   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
468
469   if (slave)
470     return gtk_im_context_get_surrounding (slave, text, cursor_index);
471   else
472     {
473       if (text)
474         *text = NULL;
475       if (cursor_index)
476         *cursor_index = 0;
477
478       return FALSE;
479     }
480 }
481
482 static void
483 gtk_im_multicontext_set_surrounding (GtkIMContext *context,
484                                      const char   *text,
485                                      gint          len,
486                                      gint          cursor_index)
487 {
488   GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
489   GtkIMContext *slave = gtk_im_multicontext_get_slave (multicontext);
490
491   if (slave)
492     gtk_im_context_set_surrounding (slave, text, len, cursor_index);
493 }
494
495 static void
496 gtk_im_multicontext_preedit_start_cb   (GtkIMContext      *slave,
497                                         GtkIMMulticontext *multicontext)
498 {
499   g_signal_emit_by_name (multicontext, "preedit-start");
500 }
501
502 static void
503 gtk_im_multicontext_preedit_end_cb (GtkIMContext      *slave,
504                                     GtkIMMulticontext *multicontext)
505 {
506   g_signal_emit_by_name (multicontext, "preedit-end");
507 }
508
509 static void
510 gtk_im_multicontext_preedit_changed_cb (GtkIMContext      *slave,
511                                         GtkIMMulticontext *multicontext)
512 {
513   g_signal_emit_by_name (multicontext, "preedit-changed");
514 }
515
516 static void
517 gtk_im_multicontext_commit_cb (GtkIMContext      *slave,
518                                const gchar       *str,
519                                GtkIMMulticontext *multicontext)
520 {
521   g_signal_emit_by_name (multicontext, "commit", str);
522 }
523
524 static gboolean
525 gtk_im_multicontext_retrieve_surrounding_cb (GtkIMContext      *slave,
526                                              GtkIMMulticontext *multicontext)
527 {
528   gboolean result;
529   
530   g_signal_emit_by_name (multicontext, "retrieve-surrounding", &result);
531
532   return result;
533 }
534
535 static gboolean
536 gtk_im_multicontext_delete_surrounding_cb (GtkIMContext      *slave,
537                                            gint               offset,
538                                            gint               n_chars,
539                                            GtkIMMulticontext *multicontext)
540 {
541   gboolean result;
542   
543   g_signal_emit_by_name (multicontext, "delete-surrounding",
544                          offset, n_chars, &result);
545
546   return result;
547 }
548
549 static void
550 activate_cb (GtkWidget         *menuitem,
551              GtkIMMulticontext *context)
552 {
553   if (gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menuitem)))
554     {
555       const gchar *id = g_object_get_data (G_OBJECT (menuitem), "gtk-context-id");
556
557       gtk_im_multicontext_set_context_id (context, id);
558     }
559 }
560
561 static int
562 pathnamecmp (const char *a,
563              const char *b)
564 {
565 #ifndef G_OS_WIN32
566   return strcmp (a, b);
567 #else
568   /* Ignore case insensitivity, probably not that relevant here. Just
569    * make sure slash and backslash compare equal.
570    */
571   while (*a && *b)
572     if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) ||
573         *a == *b)
574       a++, b++;
575     else
576       return (*a - *b);
577   return (*a - *b);
578 #endif
579 }
580
581 /**
582  * gtk_im_multicontext_append_menuitems:
583  * @context: a #GtkIMMulticontext
584  * @menushell: a #GtkMenuShell
585  * 
586  * Add menuitems for various available input methods to a menu;
587  * the menuitems, when selected, will switch the input method
588  * for the context and the global default input method.
589  **/
590 void
591 gtk_im_multicontext_append_menuitems (GtkIMMulticontext *context,
592                                       GtkMenuShell      *menushell)
593 {
594   GtkIMMulticontextPrivate *priv = context->priv;
595   const GtkIMContextInfo **contexts;
596   guint n_contexts, i;
597   GSList *group = NULL;
598   GtkWidget *menuitem, *system_menuitem;
599   const char *system_context_id; 
600
601   system_context_id = _gtk_im_module_get_default_context_id (priv->client_window);
602   system_menuitem = menuitem = gtk_radio_menu_item_new_with_label (group, C_("input method menu", "System"));
603   if (!priv->context_id_aux)
604     gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
605   group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
606   g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"), NULL);
607   g_signal_connect (menuitem, "activate", G_CALLBACK (activate_cb), context);
608
609   gtk_widget_show (menuitem);
610   gtk_menu_shell_append (menushell, menuitem);
611
612   menuitem = gtk_radio_menu_item_new_with_label (group, C_("input method menu", "None"));
613   if (g_strcmp0 (priv->context_id_aux, NONE_ID) == 0)
614     gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
615   g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"), NONE_ID);
616   g_signal_connect (menuitem, "activate", G_CALLBACK (activate_cb), context);
617   gtk_widget_show (menuitem);
618   gtk_menu_shell_append (menushell, menuitem);
619   group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
620   
621   menuitem = gtk_separator_menu_item_new ();
622   gtk_widget_show (menuitem);
623   gtk_menu_shell_append (menushell, menuitem);
624
625   _gtk_im_module_list (&contexts, &n_contexts);
626
627   for (i = 0; i < n_contexts; i++)
628     {
629       const gchar *translated_name;
630 #ifdef ENABLE_NLS
631       if (contexts[i]->domain && contexts[i]->domain[0])
632         {
633           if (strcmp (contexts[i]->domain, GETTEXT_PACKAGE) == 0)
634             {
635               /* Same translation domain as GTK+ */
636               if (!(contexts[i]->domain_dirname && contexts[i]->domain_dirname[0]) ||
637                   pathnamecmp (contexts[i]->domain_dirname, GTK_LOCALEDIR) == 0)
638                 {
639                   /* Empty or NULL, domain directory, or same as
640                    * GTK+. Input method may have a name in the GTK+
641                    * message catalog.
642                    */
643                   translated_name = _(contexts[i]->context_name);
644                 }
645               else
646                 {
647                   /* Separate domain directory but the same
648                    * translation domain as GTK+. We can't call
649                    * bindtextdomain() as that would make GTK+ forget
650                    * its own messages.
651                    */
652                   g_warning ("Input method %s should not use GTK's translation domain %s",
653                              contexts[i]->context_id, GETTEXT_PACKAGE);
654                   /* Try translating the name in GTK+'s domain */
655                   translated_name = _(contexts[i]->context_name);
656                 }
657             }
658           else if (contexts[i]->domain_dirname && contexts[i]->domain_dirname[0])
659             /* Input method has own translation domain and message catalog */
660             {
661               bindtextdomain (contexts[i]->domain,
662                               contexts[i]->domain_dirname);
663 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
664               bind_textdomain_codeset (contexts[i]->domain, "UTF-8");
665 #endif
666               translated_name = g_dgettext (contexts[i]->domain, contexts[i]->context_name);
667             }
668           else
669             {
670               /* Different translation domain, but no domain directory */
671               translated_name = contexts[i]->context_name;
672             }
673         }
674       else
675         /* Empty or NULL domain. We assume that input method does not
676          * want a translated name in this case.
677          */
678         translated_name = contexts[i]->context_name;
679 #else
680       translated_name = contexts[i]->context_name;
681 #endif
682       menuitem = gtk_radio_menu_item_new_with_label (group,
683                                                      translated_name);
684       
685       if ((priv->context_id_aux &&
686            strcmp (contexts[i]->context_id, priv->context_id_aux) == 0))
687         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), TRUE);
688
689       if (strcmp (contexts[i]->context_id, system_context_id) == 0)
690         {
691           GtkWidget *label;
692           char *text;
693
694           label = gtk_bin_get_child (GTK_BIN (system_menuitem));
695           text = g_strdup_printf (C_("input method menu", "System (%s)"), translated_name);
696           gtk_label_set_text (GTK_LABEL (label), text);
697           g_free (text);
698         }     
699  
700       group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
701       
702       g_object_set_data (G_OBJECT (menuitem), I_("gtk-context-id"),
703                          (char *)contexts[i]->context_id);
704       g_signal_connect (menuitem, "activate",
705                         G_CALLBACK (activate_cb), context);
706
707       gtk_widget_show (menuitem);
708       gtk_menu_shell_append (menushell, menuitem);
709     }
710
711   g_free (contexts);
712 }
713
714 /**
715  * gtk_im_multicontext_get_context_id:
716  * @context: a #GtkIMMulticontext
717  *
718  * Gets the id of the currently active slave of the @context.
719  *
720  * Returns: the id of the currently active slave
721  *
722  * Since: 2.16
723  */
724 const char *
725 gtk_im_multicontext_get_context_id (GtkIMMulticontext *context)
726 {
727   g_return_val_if_fail (GTK_IS_IM_MULTICONTEXT (context), NULL);
728
729   return context->priv->context_id;
730 }
731
732 /**
733  * gtk_im_multicontext_set_context_id:
734  * @context: a #GtkIMMulticontext
735  * @context_id: the id to use 
736  *
737  * Sets the context id for @context.
738  *
739  * This causes the currently active slave of @context to be
740  * replaced by the slave corresponding to the new context id.
741  *
742  * Since: 2.16
743  */
744 void
745 gtk_im_multicontext_set_context_id (GtkIMMulticontext *context,
746                                     const char        *context_id)
747 {
748   GtkIMMulticontextPrivate *priv;
749
750   g_return_if_fail (GTK_IS_IM_MULTICONTEXT (context));
751
752   priv = context->priv;
753
754   gtk_im_context_reset (GTK_IM_CONTEXT (context));
755   g_free (priv->context_id_aux);
756   priv->context_id_aux = g_strdup (context_id);
757   gtk_im_multicontext_set_slave (context, NULL, FALSE);
758 }