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