1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
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.
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.
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.
23 #include "gtk/gtkintl.h"
24 #include "gtk/gtklabel.h"
25 #include "gtk/gtksignal.h"
26 #include "gtk/gtkwindow.h"
27 #include "gtkimcontextxim.h"
29 typedef struct _StatusWindow StatusWindow;
30 typedef struct _GtkXIMInfo GtkXIMInfo;
32 struct _GtkIMContextXIM
41 GdkWindow *client_window;
42 GtkWidget *client_widget;
44 /* The status window for this input context; we claim the
45 * status window when we are focused and have created an XIC
47 StatusWindow *status_window;
51 gunichar *preedit_chars;
52 XIMFeedback *feedbacks;
56 XIMCallback preedit_start_callback;
57 XIMCallback preedit_done_callback;
58 XIMCallback preedit_draw_callback;
59 XIMCallback preedit_caret_callback;
61 XIMCallback status_start_callback;
62 XIMCallback status_done_callback;
63 XIMCallback status_draw_callback;
67 guint filter_key_release : 1;
68 guint use_preedit : 1;
70 guint in_toplevel : 1;
79 XIMStyle preedit_style_setting;
80 XIMStyle status_style_setting;
82 GtkSettings *settings;
85 XIMStyles *xim_styles;
88 guint reconnecting :1;
91 /* A context status window; these are kept in the status_windows list. */
96 /* Toplevel window to which the status window corresponds */
99 /* Currently focused GtkIMContextXIM for the toplevel, if any */
100 GtkIMContextXIM *context;
103 static void gtk_im_context_xim_class_init (GtkIMContextXIMClass *class);
104 static void gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim);
105 static void gtk_im_context_xim_finalize (GObject *obj);
106 static void gtk_im_context_xim_set_client_window (GtkIMContext *context,
107 GdkWindow *client_window);
108 static gboolean gtk_im_context_xim_filter_keypress (GtkIMContext *context,
110 static void gtk_im_context_xim_reset (GtkIMContext *context);
111 static void gtk_im_context_xim_focus_in (GtkIMContext *context);
112 static void gtk_im_context_xim_focus_out (GtkIMContext *context);
113 static void gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
115 static void gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
116 gboolean use_preedit);
117 static void gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
119 PangoAttrList **attrs,
122 static void reinitialize_ic (GtkIMContextXIM *context_xim);
123 static void set_ic_client_window (GtkIMContextXIM *context_xim,
124 GdkWindow *client_window);
126 static void setup_styles (GtkXIMInfo *info);
128 static void update_client_widget (GtkIMContextXIM *context_xim);
129 static void update_status_window (GtkIMContextXIM *context_xim);
131 static StatusWindow *status_window_get (GtkWidget *toplevel);
132 static void status_window_free (StatusWindow *status_window);
133 static void status_window_set_text (StatusWindow *status_window,
136 static void xim_destroy_callback (XIM xim,
137 XPointer client_data,
140 static XIC gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim);
141 static GObjectClass *parent_class;
143 GType gtk_type_im_context_xim = 0;
145 GSList *open_ims = NULL;
147 /* List of status windows for different toplevels */
148 static GSList *status_windows = NULL;
151 gtk_im_context_xim_register_type (GTypeModule *type_module)
153 static const GTypeInfo im_context_xim_info =
155 sizeof (GtkIMContextXIMClass),
156 (GBaseInitFunc) NULL,
157 (GBaseFinalizeFunc) NULL,
158 (GClassInitFunc) gtk_im_context_xim_class_init,
159 NULL, /* class_finalize */
160 NULL, /* class_data */
161 sizeof (GtkIMContextXIM),
163 (GInstanceInitFunc) gtk_im_context_xim_init,
166 gtk_type_im_context_xim =
167 g_type_module_register_type (type_module,
170 &im_context_xim_info, 0);
173 #define PREEDIT_MASK (XIMPreeditCallbacks | XIMPreeditPosition | \
174 XIMPreeditArea | XIMPreeditNothing | XIMPreeditNone)
175 #define STATUS_MASK (XIMStatusCallbacks | XIMStatusArea | \
176 XIMStatusNothing | XIMStatusNone)
177 #define ALLOWED_MASK (XIMPreeditCallbacks | XIMPreeditNothing | XIMPreeditNone | \
178 XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone)
181 choose_better_style (XIMStyle style1, XIMStyle style2)
185 if (style1 == 0) return style2;
186 if (style2 == 0) return style1;
187 if ((style1 & (PREEDIT_MASK | STATUS_MASK))
188 == (style2 & (PREEDIT_MASK | STATUS_MASK)))
191 s1 = style1 & PREEDIT_MASK;
192 s2 = style2 & PREEDIT_MASK;
195 if (u & XIMPreeditCallbacks)
196 return (s1 == XIMPreeditCallbacks) ? style1 : style2;
197 else if (u & XIMPreeditPosition)
198 return (s1 == XIMPreeditPosition) ? style1 :style2;
199 else if (u & XIMPreeditArea)
200 return (s1 == XIMPreeditArea) ? style1 : style2;
201 else if (u & XIMPreeditNothing)
202 return (s1 == XIMPreeditNothing) ? style1 : style2;
203 else if (u & XIMPreeditNone)
204 return (s1 == XIMPreeditNone) ? style1 : style2;
206 s1 = style1 & STATUS_MASK;
207 s2 = style2 & STATUS_MASK;
209 if (u & XIMStatusCallbacks)
210 return (s1 == XIMStatusCallbacks) ? style1 : style2;
211 else if (u & XIMStatusArea)
212 return (s1 == XIMStatusArea) ? style1 : style2;
213 else if (u & XIMStatusNothing)
214 return (s1 == XIMStatusNothing) ? style1 : style2;
215 else if (u & XIMStatusNone)
216 return (s1 == XIMStatusNone) ? style1 : style2;
218 return 0; /* Get rid of stupid warning */
222 reinitialize_all_ics (GtkXIMInfo *info)
226 for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
227 reinitialize_ic (tmp_list->data);
231 status_style_change (GtkXIMInfo *info)
233 GtkIMStatusStyle status_style;
235 g_object_get (info->settings,
236 "gtk-im-status-style", &status_style,
238 if (status_style == GTK_IM_STATUS_CALLBACK)
239 info->status_style_setting = XIMStatusCallbacks;
240 else if (status_style == GTK_IM_STATUS_NOTHING)
241 info->status_style_setting = XIMStatusNothing;
242 else if (status_style == GTK_IM_STATUS_NONE)
243 info->status_style_setting = XIMStatusNone;
249 reinitialize_all_ics (info);
253 preedit_style_change (GtkXIMInfo *info)
255 GtkIMPreeditStyle preedit_style;
256 g_object_get (info->settings,
257 "gtk-im-preedit-style", &preedit_style,
259 if (preedit_style == GTK_IM_PREEDIT_CALLBACK)
260 info->preedit_style_setting = XIMPreeditCallbacks;
261 else if (preedit_style == GTK_IM_PREEDIT_NOTHING)
262 info->preedit_style_setting = XIMPreeditNothing;
263 else if (preedit_style == GTK_IM_PREEDIT_NONE)
264 info->preedit_style_setting = XIMPreeditNone;
270 reinitialize_all_ics (info);
274 setup_styles (GtkXIMInfo *info)
277 unsigned long settings_preference;
278 XIMStyles *xim_styles = info->xim_styles;
280 settings_preference = info->status_style_setting|info->preedit_style_setting;
284 for (i = 0; i < xim_styles->count_styles; i++)
285 if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
287 if (settings_preference == xim_styles->supported_styles[i])
289 info->style = settings_preference;
292 info->style = choose_better_style (info->style,
293 xim_styles->supported_styles[i]);
296 if (info->style == 0)
297 info->style = XIMPreeditNothing | XIMStatusNothing;
301 setup_im (GtkXIMInfo *info)
303 XIMValuesList *ic_values = NULL;
304 XIMCallback im_destroy_callback;
306 if (info->im == NULL)
309 im_destroy_callback.client_data = (XPointer)info;
310 im_destroy_callback.callback = (XIMProc)xim_destroy_callback;
311 XSetIMValues (info->im,
312 XNDestroyCallback, &im_destroy_callback,
315 XGetIMValues (info->im,
316 XNQueryInputStyle, &info->xim_styles,
317 XNQueryICValuesList, &ic_values,
320 info->settings = gtk_settings_get_for_screen (info->screen);
322 if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
323 "gtk-im-preedit-style"))
324 gtk_settings_install_property (g_param_spec_enum ("gtk-im-preedit-style",
325 _("IM Preedit style"),
326 _("How to draw the input method preedit string"),
327 GTK_TYPE_IM_PREEDIT_STYLE,
328 GTK_IM_PREEDIT_CALLBACK,
331 if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
332 "gtk-im-status-style"))
333 gtk_settings_install_property (g_param_spec_enum ("gtk-im-status-style",
334 _("IM Status style"),
335 _("How to draw the input method statusbar"),
336 GTK_TYPE_IM_STATUS_STYLE,
337 GTK_IM_STATUS_CALLBACK,
340 info->status_set = g_signal_connect_swapped (info->settings,
341 "notify::gtk-im-status-style",
342 G_CALLBACK (status_style_change),
344 info->preedit_set = g_signal_connect_swapped (info->settings,
345 "notify::gtk-im-preedit-style",
346 G_CALLBACK (preedit_style_change),
349 status_style_change (info);
350 preedit_style_change (info);
355 for (i = 0; i < ic_values->count_values; i++)
356 g_print ("%s\n", ic_values->supported_values[i]);
357 for (i = 0; i < xim_styles->count_styles; i++)
358 g_print ("%#x\n", xim_styles->supported_styles[i]);
367 xim_info_display_closed (GdkDisplay *display,
371 GSList *ics, *tmp_list;
373 open_ims = g_slist_remove (open_ims, info);
378 for (tmp_list = ics; tmp_list; tmp_list = tmp_list->next)
379 set_ic_client_window (tmp_list->data, NULL);
383 g_signal_handler_disconnect (info->settings, info->status_set);
384 g_signal_handler_disconnect (info->settings, info->preedit_set);
386 XFree (info->xim_styles->supported_styles);
387 XFree (info->xim_styles);
388 g_free (info->locale);
397 xim_instantiate_callback (Display *display, XPointer client_data,
400 GtkXIMInfo *info = (GtkXIMInfo*)client_data;
403 im = XOpenIM (display, NULL, NULL, NULL);
411 XUnregisterIMInstantiateCallback (display, NULL, NULL, NULL,
412 xim_instantiate_callback,
414 info->reconnecting = FALSE;
417 /* initialize info->im */
419 xim_info_try_im (GtkXIMInfo *info)
421 GdkScreen *screen = info->screen;
422 GdkDisplay *display = gdk_screen_get_display (screen);
424 g_assert (info->im == NULL);
425 if (info->reconnecting)
428 if (XSupportsLocale ())
430 if (!XSetLocaleModifiers (""))
431 g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
432 info->im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
435 XRegisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY(display),
437 xim_instantiate_callback,
439 info->reconnecting = TRUE;
444 g_signal_connect (display, "closed",
445 G_CALLBACK (xim_info_display_closed), info);
450 xim_destroy_callback (XIM xim,
451 XPointer client_data,
454 GtkXIMInfo *info = (GtkXIMInfo*)client_data;
458 g_signal_handler_disconnect (info->settings, info->status_set);
459 g_signal_handler_disconnect (info->settings, info->preedit_set);
461 reinitialize_all_ics (info);
462 xim_info_try_im (info);
467 get_im (GdkWindow *client_window,
472 GdkScreen *screen = gdk_drawable_get_screen (client_window);
478 GtkXIMInfo *tmp_info = tmp_list->data;
479 if (tmp_info->screen == screen &&
480 strcmp (tmp_info->locale, locale) == 0)
492 tmp_list = tmp_list->next;
497 info = g_new (GtkXIMInfo, 1);
498 open_ims = g_slist_prepend (open_ims, info);
500 info->screen = screen;
501 info->locale = g_strdup (locale);
502 info->xim_styles = NULL;
503 info->preedit_style_setting = 0;
504 info->status_style_setting = 0;
505 info->settings = NULL;
506 info->preedit_set = 0;
507 info->status_set = 0;
509 info->reconnecting = FALSE;
513 xim_info_try_im (info);
518 gtk_im_context_xim_class_init (GtkIMContextXIMClass *class)
520 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
521 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
523 parent_class = g_type_class_peek_parent (class);
525 im_context_class->set_client_window = gtk_im_context_xim_set_client_window;
526 im_context_class->filter_keypress = gtk_im_context_xim_filter_keypress;
527 im_context_class->reset = gtk_im_context_xim_reset;
528 im_context_class->get_preedit_string = gtk_im_context_xim_get_preedit_string;
529 im_context_class->focus_in = gtk_im_context_xim_focus_in;
530 im_context_class->focus_out = gtk_im_context_xim_focus_out;
531 im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
532 im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
533 gobject_class->finalize = gtk_im_context_xim_finalize;
537 gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
539 im_context_xim->use_preedit = TRUE;
540 im_context_xim->filter_key_release = FALSE;
541 im_context_xim->finalizing = FALSE;
542 im_context_xim->has_focus = FALSE;
543 im_context_xim->in_toplevel = FALSE;
547 gtk_im_context_xim_finalize (GObject *obj)
549 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);
551 context_xim->finalizing = TRUE;
553 set_ic_client_window (context_xim, NULL);
555 g_free (context_xim->locale);
556 g_free (context_xim->mb_charset);
560 reinitialize_ic (GtkIMContextXIM *context_xim)
564 XDestroyIC (context_xim->ic);
565 context_xim->ic = NULL;
566 update_status_window (context_xim);
568 if (context_xim->preedit_length)
570 context_xim->preedit_length = 0;
571 if (!context_xim->finalizing)
572 g_signal_emit_by_name (context_xim, "preedit_changed");
576 reset filter_key_release flag, otherwise keystrokes will be doubled
577 until reconnecting to XIM.
579 context_xim->filter_key_release = FALSE;
583 set_ic_client_window (GtkIMContextXIM *context_xim,
584 GdkWindow *client_window)
586 reinitialize_ic (context_xim);
587 if (context_xim->client_window)
589 context_xim->im_info->ics = g_slist_remove (context_xim->im_info->ics, context_xim);
590 context_xim->im_info = NULL;
593 context_xim->client_window = client_window;
595 if (context_xim->client_window)
597 context_xim->im_info = get_im (context_xim->client_window, context_xim->locale);
598 context_xim->im_info->ics = g_slist_prepend (context_xim->im_info->ics, context_xim);
601 update_client_widget (context_xim);
605 gtk_im_context_xim_set_client_window (GtkIMContext *context,
606 GdkWindow *client_window)
608 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
610 set_ic_client_window (context_xim, client_window);
614 gtk_im_context_xim_new (void)
616 GtkIMContextXIM *result;
617 const gchar *charset;
619 result = GTK_IM_CONTEXT_XIM (g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL));
621 result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
623 g_get_charset (&charset);
624 result->mb_charset = g_strdup (charset);
626 return GTK_IM_CONTEXT (result);
630 mb_to_utf8 (GtkIMContextXIM *context_xim,
633 GError *error = NULL;
636 if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
637 result = g_strdup (str);
640 result = g_convert (str, -1,
641 "UTF-8", context_xim->mb_charset,
645 g_warning ("Error converting text from IM to UTF-8: %s\n", error->message);
646 g_error_free (error);
654 gtk_im_context_xim_filter_keypress (GtkIMContext *context,
657 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
658 XIC ic = gtk_im_context_xim_get_ic (context_xim);
659 gchar static_buffer[256];
660 gchar *buffer = static_buffer;
661 gint buffer_size = sizeof(static_buffer) - 1;
665 gboolean result = FALSE;
666 GdkWindow *root_window = gdk_screen_get_root_window (gdk_drawable_get_screen (event->window));
668 XKeyPressedEvent xevent;
670 if (event->type == GDK_KEY_RELEASE && !context_xim->filter_key_release)
673 xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
674 xevent.serial = 0; /* hope it doesn't matter */
675 xevent.send_event = event->send_event;
676 xevent.display = GDK_DRAWABLE_XDISPLAY (event->window);
677 xevent.window = GDK_DRAWABLE_XID (event->window);
678 xevent.root = GDK_DRAWABLE_XID (root_window);
679 xevent.subwindow = xevent.window;
680 xevent.time = event->time;
681 xevent.x = xevent.x_root = 0;
682 xevent.y = xevent.y_root = 0;
683 xevent.state = event->state;
684 xevent.keycode = event->hardware_keycode;
685 xevent.same_screen = True;
687 if (XFilterEvent ((XEvent *)&xevent, GDK_DRAWABLE_XID (context_xim->client_window)))
692 num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
695 num_bytes = XLookupString (&xevent, buffer, buffer_size, &keysym, NULL);
696 status = XLookupBoth;
699 if (status == XBufferOverflow)
701 buffer_size = num_bytes;
702 if (buffer != static_buffer)
704 buffer = g_malloc (num_bytes + 1);
708 /* I don't know how we should properly handle XLookupKeysym or XLookupBoth
709 * here ... do input methods actually change the keysym? we can't really
710 * feed it back to accelerator processing at this point...
712 if (status == XLookupChars || status == XLookupBoth)
716 buffer[num_bytes] = '\0';
718 result_utf8 = mb_to_utf8 (context_xim, buffer);
721 if ((guchar)result_utf8[0] >= 0x20 &&
722 result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
723 * control characters into strings
726 g_signal_emit_by_name (context, "commit", result_utf8);
730 g_free (result_utf8);
734 if (buffer != static_buffer)
741 gtk_im_context_xim_focus_in (GtkIMContext *context)
743 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
745 if (!context_xim->has_focus)
747 XIC ic = gtk_im_context_xim_get_ic (context_xim);
749 context_xim->has_focus = TRUE;
750 update_status_window (context_xim);
760 gtk_im_context_xim_focus_out (GtkIMContext *context)
762 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
764 if (context_xim->has_focus)
766 XIC ic = gtk_im_context_xim_get_ic (context_xim);
768 context_xim->has_focus = FALSE;
769 update_status_window (context_xim);
779 gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
782 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
783 XIC ic = gtk_im_context_xim_get_ic (context_xim);
785 XVaNestedList preedit_attr;
794 preedit_attr = XVaCreateNestedList (0,
795 XNSpotLocation, &spot,
798 XNPreeditAttributes, preedit_attr,
806 gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
807 gboolean use_preedit)
809 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
811 use_preedit = use_preedit != FALSE;
813 if (context_xim->use_preedit != use_preedit)
815 context_xim->use_preedit = use_preedit;
816 reinitialize_ic (context_xim);
823 gtk_im_context_xim_reset (GtkIMContext *context)
825 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
826 XIC ic = gtk_im_context_xim_get_ic (context_xim);
829 /* restore conversion state after resetting ic later */
830 XIMPreeditState preedit_state = XIMPreeditUnKnown;
831 XVaNestedList preedit_attr;
832 gboolean have_preedit_state = FALSE;
838 if (context_xim->preedit_length == 0)
841 preedit_attr = XVaCreateNestedList(0,
842 XNPreeditState, &preedit_state,
844 if (!XGetICValues(ic,
845 XNPreeditAttributes, preedit_attr,
847 have_preedit_state = TRUE;
851 result = XmbResetIC (ic);
853 preedit_attr = XVaCreateNestedList(0,
854 XNPreeditState, preedit_state,
856 if (have_preedit_state)
858 XNPreeditAttributes, preedit_attr,
865 char *result_utf8 = mb_to_utf8 (context_xim, result);
868 g_signal_emit_by_name (context, "commit", result_utf8);
869 g_free (result_utf8);
873 if (context_xim->preedit_length)
875 context_xim->preedit_length = 0;
876 g_signal_emit_by_name (context, "preedit_changed");
882 /* Mask of feedback bits that we render
884 #define FEEDBACK_MASK (XIMReverse | XIMUnderline)
887 add_feedback_attr (PangoAttrList *attrs,
889 XIMFeedback feedback,
893 PangoAttribute *attr;
895 gint start_index = g_utf8_offset_to_pointer (str, start_pos) - str;
896 gint end_index = g_utf8_offset_to_pointer (str, end_pos) - str;
898 if (feedback & XIMUnderline)
900 attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
901 attr->start_index = start_index;
902 attr->end_index = end_index;
904 pango_attr_list_change (attrs, attr);
907 if (feedback & XIMReverse)
909 attr = pango_attr_foreground_new (0xffff, 0xffff, 0xffff);
910 attr->start_index = start_index;
911 attr->end_index = end_index;
913 pango_attr_list_change (attrs, attr);
915 attr = pango_attr_background_new (0, 0, 0);
916 attr->start_index = start_index;
917 attr->end_index = end_index;
919 pango_attr_list_change (attrs, attr);
922 if (feedback & ~FEEDBACK_MASK)
923 g_warning ("Unrendered feedback style: %#lx", feedback & ~FEEDBACK_MASK);
927 gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
929 PangoAttrList **attrs,
932 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
933 gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
938 XIMFeedback last_feedback = 0;
941 *attrs = pango_attr_list_new ();
943 for (i = 0; i < context_xim->preedit_length; i++)
945 XIMFeedback new_feedback = context_xim->feedbacks[i] & FEEDBACK_MASK;
946 if (new_feedback != last_feedback)
949 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
951 last_feedback = new_feedback;
957 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
966 *cursor_pos = context_xim->preedit_cursor;
970 preedit_start_callback (XIC xic,
971 XPointer client_data,
974 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
975 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
977 if (!context_xim->finalizing)
978 g_signal_emit_by_name (context, "preedit_start");
982 preedit_done_callback (XIC xic,
983 XPointer client_data,
986 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
987 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
989 if (!context_xim->finalizing)
990 g_signal_emit_by_name (context, "preedit_end");
994 xim_text_to_utf8 (GtkIMContextXIM *context, XIMText *xim_text, gchar **text)
996 gint text_length = 0;
997 GError *error = NULL;
998 gchar *result = NULL;
1000 if (xim_text && xim_text->string.multi_byte)
1002 if (xim_text->encoding_is_wchar)
1004 g_warning ("Wide character return from Xlib not currently supported");
1009 if (strcmp (context->mb_charset, "UTF-8") == 0)
1010 result = g_strdup (xim_text->string.multi_byte);
1012 result = g_convert (xim_text->string.multi_byte,
1015 context->mb_charset,
1016 NULL, NULL, &error);
1020 text_length = g_utf8_strlen (result, -1);
1022 if (text_length != xim_text->length)
1024 g_warning ("Size mismatch when converting text from input method: supplied length = %d\n, result length = %d", xim_text->length, text_length);
1029 g_warning ("Error converting text from IM to UCS-4: %s", error->message);
1030 g_error_free (error);
1047 preedit_draw_callback (XIC xic,
1048 XPointer client_data,
1049 XIMPreeditDrawCallbackStruct *call_data)
1051 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1053 XIMText *new_xim_text = call_data->text;
1054 gint new_text_length;
1055 gunichar *new_text = NULL;
1061 gint chg_first = CLAMP (call_data->chg_first, 0, context->preedit_length);
1062 gint chg_length = CLAMP (call_data->chg_length, 0, context->preedit_length - chg_first);
1064 context->preedit_cursor = call_data->caret;
1066 if (chg_first != call_data->chg_first || chg_length != call_data->chg_length)
1067 g_warning ("Invalid change to preedit string, first=%d length=%d (orig length == %d)",
1068 call_data->chg_first, call_data->chg_length, context->preedit_length);
1070 new_text_length = xim_text_to_utf8 (context, new_xim_text, &tmp);
1073 new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
1077 diff = new_text_length - chg_length;
1078 new_length = context->preedit_length + diff;
1080 if (new_length > context->preedit_size)
1082 context->preedit_size = new_length;
1083 context->preedit_chars = g_renew (gunichar, context->preedit_chars, new_length);
1084 context->feedbacks = g_renew (XIMFeedback, context->feedbacks, new_length);
1089 for (i = chg_first + chg_length ; i < context->preedit_length; i++)
1091 context->preedit_chars[i + diff] = context->preedit_chars[i];
1092 context->feedbacks[i + diff] = context->feedbacks[i];
1097 for (i = context->preedit_length - 1; i >= chg_first + chg_length ; i--)
1099 context->preedit_chars[i + diff] = context->preedit_chars[i];
1100 context->feedbacks[i + diff] = context->feedbacks[i];
1104 for (i = 0; i < new_text_length; i++)
1106 context->preedit_chars[chg_first + i] = new_text[i];
1107 context->feedbacks[chg_first + i] = new_xim_text->feedback[i];
1110 context->preedit_length += diff;
1115 if (!context->finalizing)
1116 g_signal_emit_by_name (context, "preedit_changed");
1121 preedit_caret_callback (XIC xic,
1122 XPointer client_data,
1123 XIMPreeditCaretCallbackStruct *call_data)
1125 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1127 if (call_data->direction == XIMAbsolutePosition)
1129 context->preedit_cursor = call_data->position;
1130 if (!context->finalizing)
1131 g_signal_emit_by_name (context, "preedit_changed");
1135 g_warning ("Caret movement command: %d %d %d not supported",
1136 call_data->position, call_data->direction, call_data->style);
1141 status_start_callback (XIC xic,
1142 XPointer client_data,
1149 status_done_callback (XIC xic,
1150 XPointer client_data,
1157 status_draw_callback (XIC xic,
1158 XPointer client_data,
1159 XIMStatusDrawCallbackStruct *call_data)
1161 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1163 if (call_data->type == XIMTextType)
1166 xim_text_to_utf8 (context, call_data->data.text, &text);
1168 if (context->status_window)
1169 status_window_set_text (context->status_window, text ? text : "");
1173 g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
1177 static XVaNestedList
1178 set_preedit_callback (GtkIMContextXIM *context_xim)
1180 context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
1181 context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
1182 context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
1183 context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
1184 context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
1185 context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
1186 context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
1187 context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
1188 return XVaCreateNestedList (0,
1189 XNPreeditStartCallback, &context_xim->preedit_start_callback,
1190 XNPreeditDoneCallback, &context_xim->preedit_done_callback,
1191 XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
1192 XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
1196 static XVaNestedList
1197 set_status_callback (GtkIMContextXIM *context_xim)
1199 context_xim->status_start_callback.client_data = (XPointer)context_xim;
1200 context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
1201 context_xim->status_done_callback.client_data = (XPointer)context_xim;
1202 context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
1203 context_xim->status_draw_callback.client_data = (XPointer)context_xim;
1204 context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
1206 return XVaCreateNestedList (0,
1207 XNStatusStartCallback, &context_xim->status_start_callback,
1208 XNStatusDoneCallback, &context_xim->status_done_callback,
1209 XNStatusDrawCallback, &context_xim->status_draw_callback,
1215 gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
1217 if (context_xim->im_info == NULL || context_xim->im_info->im == NULL)
1220 if (!context_xim->ic)
1222 const char *name1 = NULL;
1223 XVaNestedList list1 = NULL;
1224 const char *name2 = NULL;
1225 XVaNestedList list2 = NULL;
1226 XIMStyle im_style = 0;
1229 if (context_xim->use_preedit &&
1230 (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
1232 im_style |= XIMPreeditCallbacks;
1233 name1 = XNPreeditAttributes;
1234 list1 = set_preedit_callback (context_xim);
1236 else if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditNone)
1237 im_style |= XIMPreeditNone;
1239 im_style |= XIMPreeditNothing;
1241 if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
1243 im_style |= XIMStatusCallbacks;
1246 name1 = XNStatusAttributes;
1247 list1 = set_status_callback (context_xim);
1251 name2 = XNStatusAttributes;
1252 list2 = set_status_callback (context_xim);
1255 else if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusNone)
1256 im_style |= XIMStatusNone;
1258 im_style |= XIMStatusNothing;
1260 xic = XCreateIC (context_xim->im_info->im,
1261 XNInputStyle, im_style,
1262 XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
1273 /* Don't filter key released events with XFilterEvents unless
1274 * input methods ask for. This is a workaround for Solaris input
1275 * method bug in C and European locales. It doubles each key
1276 * stroke if both key pressed and released events are filtered.
1281 XNFilterEvents, &mask,
1283 context_xim->filter_key_release = (mask & KeyReleaseMask) != 0;
1286 context_xim->ic = xic;
1288 update_status_window (context_xim);
1290 if (xic && context_xim->has_focus)
1293 return context_xim->ic;
1296 /*****************************************************************
1297 * Status Window handling
1299 * A status window is a small window attached to the toplevel
1300 * that is used to display information to the user about the
1301 * current input operation.
1303 * We claim the toplevel's status window for an input context if:
1305 * A) The input context has a toplevel
1306 * B) The input context has the focus
1307 * C) The input context has an XIC associated with it
1309 * Tracking A) and C) is pretty reliable since we
1310 * compute A) and create the XIC for C) ourselves.
1311 * For B) we basically have to depend on our callers
1312 * calling ::focus-in and ::focus-out at the right time.
1314 * The toplevel is computed by walking up the GdkWindow
1315 * hierarchy from context->client_window until we find a
1316 * window that is owned by some widget, and then calling
1317 * gtk_widget_get_toplevel() on that widget. This should
1318 * handle both cases where we might have GdkWindows without widgets,
1319 * and cases where GtkWidgets have strange window hierarchies
1320 * (like a torn off GtkHandleBox.)
1322 * The status window is visible if and only if there is text
1323 * for it; whenever a new GtkIMContextXIM claims the status
1324 * window, we blank out any existing text. We actually only
1325 * create a GtkWindow for the status window the first time
1326 * it is shown; this is an important optimization when we are
1327 * using XIM with something like a simple compose-key input
1328 * method that never needs a status window.
1329 *****************************************************************/
1331 /* Called when we no longer need a status window
1334 disclaim_status_window (GtkIMContextXIM *context_xim)
1336 if (context_xim->status_window)
1338 g_assert (context_xim->status_window->context == context_xim);
1340 status_window_set_text (context_xim->status_window, "");
1342 context_xim->status_window->context = NULL;
1343 context_xim->status_window = NULL;
1347 /* Called when we need a status window
1350 claim_status_window (GtkIMContextXIM *context_xim)
1352 if (!context_xim->status_window && context_xim->client_widget)
1354 GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
1355 if (toplevel && GTK_WIDGET_TOPLEVEL (toplevel))
1357 StatusWindow *status_window = status_window_get (toplevel);
1359 if (status_window->context)
1360 disclaim_status_window (status_window->context);
1362 status_window->context = context_xim;
1363 context_xim->status_window = status_window;
1368 /* Basic call made whenever something changed that might cause
1369 * us to need, or not to need a status window.
1372 update_status_window (GtkIMContextXIM *context_xim)
1374 if (context_xim->ic && context_xim->in_toplevel && context_xim->has_focus)
1375 claim_status_window (context_xim);
1377 disclaim_status_window (context_xim);
1380 /* Updates the in_toplevel flag for @context_xim
1383 update_in_toplevel (GtkIMContextXIM *context_xim)
1385 if (context_xim->client_widget)
1387 GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
1389 context_xim->in_toplevel = (toplevel && GTK_WIDGET_TOPLEVEL (toplevel));
1392 context_xim->in_toplevel = FALSE;
1394 /* Some paranoia, in case we don't get a focus out */
1395 if (!context_xim->in_toplevel)
1396 context_xim->has_focus = FALSE;
1398 update_status_window (context_xim);
1401 /* Callback when @widget's toplevel changes. It will always
1402 * change from NULL to a window, or a window to NULL;
1403 * we use that intermediate NULL state to make sure
1404 * that we disclaim the toplevel status window for the old
1408 on_client_widget_hierarchy_changed (GtkWidget *widget,
1409 GtkWidget *old_toplevel,
1410 GtkIMContextXIM *context_xim)
1412 update_in_toplevel (context_xim);
1415 /* Finds the GtkWidget that owns the window, or if none, the
1416 * widget owning the nearest parent that has a widget.
1419 widget_for_window (GdkWindow *window)
1424 gdk_window_get_user_data (window, &user_data);
1428 window = gdk_window_get_parent (window);
1434 /* Called when context_xim->client_window changes; takes care of
1435 * removing and/or setting up our watches for the toplevel
1438 update_client_widget (GtkIMContextXIM *context_xim)
1440 GtkWidget *new_client_widget = widget_for_window (context_xim->client_window);
1442 if (new_client_widget != context_xim->client_widget)
1444 if (context_xim->client_widget)
1446 g_signal_handlers_disconnect_by_func (context_xim->client_widget,
1447 G_CALLBACK (on_client_widget_hierarchy_changed),
1450 context_xim->client_widget = new_client_widget;
1451 if (context_xim->client_widget)
1453 g_signal_connect (context_xim->client_widget, "hierarchy-changed",
1454 G_CALLBACK (on_client_widget_hierarchy_changed),
1458 update_in_toplevel (context_xim);
1462 /* Called when the toplevel is destroyed; frees the status window
1465 on_status_toplevel_destroy (GtkWidget *toplevel,
1466 StatusWindow *status_window)
1468 status_window_free (status_window);
1471 /* Called when the screen for the toplevel changes; updates the
1472 * screen for the status window to match.
1475 on_status_toplevel_notify_screen (GtkWindow *toplevel,
1477 StatusWindow *status_window)
1479 if (status_window->window)
1480 gtk_window_set_screen (GTK_WINDOW (status_window->window),
1481 gtk_widget_get_screen (GTK_WIDGET (toplevel)));
1484 /* Called when the toplevel window is moved; updates the position of
1485 * the status window to follow it.
1488 on_status_toplevel_configure (GtkWidget *toplevel,
1489 GdkEventConfigure *event,
1490 StatusWindow *status_window)
1493 GtkRequisition requisition;
1497 if (status_window->window)
1499 height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
1501 gdk_window_get_frame_extents (toplevel->window, &rect);
1502 gtk_widget_size_request (status_window->window, &requisition);
1504 if (rect.y + rect.height + requisition.height < height)
1505 y = rect.y + rect.height;
1507 y = height - requisition.height;
1509 gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
1515 /* Frees a status window and removes its link from the status_windows list
1518 status_window_free (StatusWindow *status_window)
1520 status_windows = g_slist_remove (status_windows, status_window);
1522 if (status_window->context)
1523 status_window->context->status_window = NULL;
1525 g_signal_handlers_disconnect_by_func (status_window->toplevel,
1526 G_CALLBACK (on_status_toplevel_destroy),
1528 g_signal_handlers_disconnect_by_func (status_window->toplevel,
1529 G_CALLBACK (on_status_toplevel_notify_screen),
1531 g_signal_handlers_disconnect_by_func (status_window->toplevel,
1532 G_CALLBACK (on_status_toplevel_configure),
1535 if (status_window->window)
1536 gtk_widget_destroy (status_window->window);
1538 g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
1540 g_free (status_window);
1543 /* Finds the status window object for a toplevel, creating it if necessary.
1545 static StatusWindow *
1546 status_window_get (GtkWidget *toplevel)
1548 StatusWindow *status_window;
1550 status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
1552 return status_window;
1554 status_window = g_new0 (StatusWindow, 1);
1555 status_window->toplevel = toplevel;
1557 status_windows = g_slist_prepend (status_windows, status_window);
1559 g_signal_connect (toplevel, "destroy",
1560 G_CALLBACK (on_status_toplevel_destroy),
1562 g_signal_connect (toplevel, "configure_event",
1563 G_CALLBACK (on_status_toplevel_configure),
1565 g_signal_connect (toplevel, "notify::screen",
1566 G_CALLBACK (on_status_toplevel_notify_screen),
1569 g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);
1571 return status_window;
1574 /* Draw the background (normally white) and border for the status window
1577 on_status_window_expose_event (GtkWidget *widget,
1578 GdkEventExpose *event)
1580 gdk_draw_rectangle (widget->window,
1581 widget->style->base_gc [GTK_STATE_NORMAL],
1584 widget->allocation.width, widget->allocation.height);
1585 gdk_draw_rectangle (widget->window,
1586 widget->style->text_gc [GTK_STATE_NORMAL],
1589 widget->allocation.width - 1, widget->allocation.height - 1);
1594 /* We watch the ::style-set signal for our label widget
1595 * and use that to change it's foreground color to match
1596 * the 'text' color of the toplevel window. The text/base
1597 * pair of colors might be reversed from the fg/bg pair
1598 * that are normally used for labels.
1601 on_status_window_style_set (GtkWidget *toplevel,
1602 GtkStyle *previous_style,
1607 for (i = 0; i < 5; i++)
1608 gtk_widget_modify_fg (label, i, &toplevel->style->text[i]);
1611 /* Creates the widgets for the status window; called when we
1612 * first need to show text for the status window.
1615 status_window_make_window (StatusWindow *status_window)
1618 GtkWidget *status_label;
1620 status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
1621 window = status_window->window;
1623 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
1624 gtk_widget_set_app_paintable (window, TRUE);
1626 status_label = gtk_label_new ("");
1627 gtk_misc_set_padding (GTK_MISC (status_label), 1, 1);
1628 gtk_widget_show (status_label);
1630 g_signal_connect (window, "style_set",
1631 G_CALLBACK (on_status_window_style_set), status_label);
1632 gtk_container_add (GTK_CONTAINER (window), status_label);
1634 g_signal_connect (window, "expose_event",
1635 G_CALLBACK (on_status_window_expose_event), NULL);
1637 gtk_window_set_screen (GTK_WINDOW (status_window->window),
1638 gtk_widget_get_screen (status_window->toplevel));
1640 on_status_toplevel_configure (status_window->toplevel, NULL, status_window);
1643 /* Updates the text in the status window, hiding or
1644 * showing the window as necessary.
1647 status_window_set_text (StatusWindow *status_window,
1654 if (!status_window->window)
1655 status_window_make_window (status_window);
1657 label = GTK_BIN (status_window->window)->child;
1658 gtk_label_set_text (GTK_LABEL (label), text);
1660 gtk_widget_show (status_window->window);
1664 if (status_window->window)
1665 gtk_widget_hide (status_window->window);
1670 * gtk_im_context_xim_shutdown:
1672 * Destroys all the status windows that are kept by the XIM contexts. This
1673 * function should only be called by the XIM module exit routine.
1676 gtk_im_context_xim_shutdown (void)
1678 while (status_windows)
1679 status_window_free (status_windows->data);