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