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.
25 #include "gtkimcontextxim.h"
27 #include "gtk/gtkintl.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;
65 XIMCallback string_conversion_callback;
69 guint filter_key_release : 1;
70 guint use_preedit : 1;
72 guint in_toplevel : 1;
81 XIMStyle preedit_style_setting;
82 XIMStyle status_style_setting;
84 GtkSettings *settings;
87 gulong display_closed_cb;
88 XIMStyles *xim_styles;
91 guint reconnecting :1;
92 guint supports_string_conversion;
95 /* A context status window; these are kept in the status_windows list. */
100 /* Toplevel window to which the status window corresponds */
103 /* Currently focused GtkIMContextXIM for the toplevel, if any */
104 GtkIMContextXIM *context;
107 static void gtk_im_context_xim_class_init (GtkIMContextXIMClass *class);
108 static void gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim);
109 static void gtk_im_context_xim_finalize (GObject *obj);
110 static void gtk_im_context_xim_set_client_window (GtkIMContext *context,
111 GdkWindow *client_window);
112 static gboolean gtk_im_context_xim_filter_keypress (GtkIMContext *context,
114 static void gtk_im_context_xim_reset (GtkIMContext *context);
115 static void gtk_im_context_xim_focus_in (GtkIMContext *context);
116 static void gtk_im_context_xim_focus_out (GtkIMContext *context);
117 static void gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
119 static void gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
120 gboolean use_preedit);
121 static void gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
123 PangoAttrList **attrs,
126 static void reinitialize_ic (GtkIMContextXIM *context_xim);
127 static void set_ic_client_window (GtkIMContextXIM *context_xim,
128 GdkWindow *client_window);
130 static void setup_styles (GtkXIMInfo *info);
132 static void update_client_widget (GtkIMContextXIM *context_xim);
133 static void update_status_window (GtkIMContextXIM *context_xim);
135 static StatusWindow *status_window_get (GtkWidget *toplevel);
136 static void status_window_free (StatusWindow *status_window);
137 static void status_window_set_text (StatusWindow *status_window,
140 static void xim_destroy_callback (XIM xim,
141 XPointer client_data,
144 static XIC gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim);
145 static void xim_info_display_closed (GdkDisplay *display,
149 static GObjectClass *parent_class;
151 GType gtk_type_im_context_xim = 0;
153 static GSList *open_ims = NULL;
155 /* List of status windows for different toplevels */
156 static GSList *status_windows = NULL;
159 gtk_im_context_xim_register_type (GTypeModule *type_module)
161 const GTypeInfo im_context_xim_info =
163 sizeof (GtkIMContextXIMClass),
164 (GBaseInitFunc) NULL,
165 (GBaseFinalizeFunc) NULL,
166 (GClassInitFunc) gtk_im_context_xim_class_init,
167 NULL, /* class_finalize */
168 NULL, /* class_data */
169 sizeof (GtkIMContextXIM),
171 (GInstanceInitFunc) gtk_im_context_xim_init,
174 gtk_type_im_context_xim =
175 g_type_module_register_type (type_module,
178 &im_context_xim_info, 0);
181 #define PREEDIT_MASK (XIMPreeditCallbacks | XIMPreeditPosition | \
182 XIMPreeditArea | XIMPreeditNothing | XIMPreeditNone)
183 #define STATUS_MASK (XIMStatusCallbacks | XIMStatusArea | \
184 XIMStatusNothing | XIMStatusNone)
185 #define ALLOWED_MASK (XIMPreeditCallbacks | XIMPreeditNothing | XIMPreeditNone | \
186 XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone)
189 choose_better_style (XIMStyle style1, XIMStyle style2)
193 if (style1 == 0) return style2;
194 if (style2 == 0) return style1;
195 if ((style1 & (PREEDIT_MASK | STATUS_MASK))
196 == (style2 & (PREEDIT_MASK | STATUS_MASK)))
199 s1 = style1 & PREEDIT_MASK;
200 s2 = style2 & PREEDIT_MASK;
203 if (u & XIMPreeditCallbacks)
204 return (s1 == XIMPreeditCallbacks) ? style1 : style2;
205 else if (u & XIMPreeditPosition)
206 return (s1 == XIMPreeditPosition) ? style1 :style2;
207 else if (u & XIMPreeditArea)
208 return (s1 == XIMPreeditArea) ? style1 : style2;
209 else if (u & XIMPreeditNothing)
210 return (s1 == XIMPreeditNothing) ? style1 : style2;
211 else if (u & XIMPreeditNone)
212 return (s1 == XIMPreeditNone) ? style1 : style2;
214 s1 = style1 & STATUS_MASK;
215 s2 = style2 & STATUS_MASK;
217 if (u & XIMStatusCallbacks)
218 return (s1 == XIMStatusCallbacks) ? style1 : style2;
219 else if (u & XIMStatusArea)
220 return (s1 == XIMStatusArea) ? style1 : style2;
221 else if (u & XIMStatusNothing)
222 return (s1 == XIMStatusNothing) ? style1 : style2;
223 else if (u & XIMStatusNone)
224 return (s1 == XIMStatusNone) ? style1 : style2;
226 return 0; /* Get rid of stupid warning */
230 reinitialize_all_ics (GtkXIMInfo *info)
234 for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
235 reinitialize_ic (tmp_list->data);
239 status_style_change (GtkXIMInfo *info)
241 GtkIMStatusStyle status_style;
243 g_object_get (info->settings,
244 "gtk-im-status-style", &status_style,
246 if (status_style == GTK_IM_STATUS_CALLBACK)
247 info->status_style_setting = XIMStatusCallbacks;
248 else if (status_style == GTK_IM_STATUS_NOTHING)
249 info->status_style_setting = XIMStatusNothing;
250 else if (status_style == GTK_IM_STATUS_NONE)
251 info->status_style_setting = XIMStatusNone;
257 reinitialize_all_ics (info);
261 preedit_style_change (GtkXIMInfo *info)
263 GtkIMPreeditStyle preedit_style;
264 g_object_get (info->settings,
265 "gtk-im-preedit-style", &preedit_style,
267 if (preedit_style == GTK_IM_PREEDIT_CALLBACK)
268 info->preedit_style_setting = XIMPreeditCallbacks;
269 else if (preedit_style == GTK_IM_PREEDIT_NOTHING)
270 info->preedit_style_setting = XIMPreeditNothing;
271 else if (preedit_style == GTK_IM_PREEDIT_NONE)
272 info->preedit_style_setting = XIMPreeditNone;
278 reinitialize_all_ics (info);
282 setup_styles (GtkXIMInfo *info)
285 unsigned long settings_preference;
286 XIMStyles *xim_styles = info->xim_styles;
288 settings_preference = info->status_style_setting|info->preedit_style_setting;
292 for (i = 0; i < xim_styles->count_styles; i++)
293 if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
295 if (settings_preference == xim_styles->supported_styles[i])
297 info->style = settings_preference;
300 info->style = choose_better_style (info->style,
301 xim_styles->supported_styles[i]);
304 if (info->style == 0)
305 info->style = XIMPreeditNothing | XIMStatusNothing;
309 setup_im (GtkXIMInfo *info)
311 XIMValuesList *ic_values = NULL;
312 XIMCallback im_destroy_callback;
315 if (info->im == NULL)
318 im_destroy_callback.client_data = (XPointer)info;
319 im_destroy_callback.callback = (XIMProc)xim_destroy_callback;
320 XSetIMValues (info->im,
321 XNDestroyCallback, &im_destroy_callback,
324 XGetIMValues (info->im,
325 XNQueryInputStyle, &info->xim_styles,
326 XNQueryICValuesList, &ic_values,
329 info->settings = gtk_settings_get_for_screen (info->screen);
330 info->status_set = g_signal_connect_swapped (info->settings,
331 "notify::gtk-im-status-style",
332 G_CALLBACK (status_style_change),
334 info->preedit_set = g_signal_connect_swapped (info->settings,
335 "notify::gtk-im-preedit-style",
336 G_CALLBACK (preedit_style_change),
339 info->supports_string_conversion = FALSE;
344 for (i = 0; i < ic_values->count_values; i++)
345 if (strcmp (ic_values->supported_values[i],
346 XNStringConversionCallback) == 0)
348 info->supports_string_conversion = TRUE;
353 for (i = 0; i < ic_values->count_values; i++)
354 g_print ("%s\n", ic_values->supported_values[i]);
355 for (i = 0; i < xim_styles->count_styles; i++)
356 g_print ("%#x\n", xim_styles->supported_styles[i]);
362 status_style_change (info);
363 preedit_style_change (info);
365 display = gdk_screen_get_display (info->screen);
366 info->display_closed_cb = g_signal_connect (display, "closed",
367 G_CALLBACK (xim_info_display_closed), info);
371 xim_info_display_closed (GdkDisplay *display,
375 GSList *ics, *tmp_list;
377 open_ims = g_slist_remove (open_ims, info);
382 for (tmp_list = ics; tmp_list; tmp_list = tmp_list->next)
383 set_ic_client_window (tmp_list->data, NULL);
387 if (info->status_set)
388 g_signal_handler_disconnect (info->settings, info->status_set);
389 if (info->preedit_set)
390 g_signal_handler_disconnect (info->settings, info->preedit_set);
391 if (info->display_closed_cb)
392 g_signal_handler_disconnect (display, info->display_closed_cb);
394 if (info->xim_styles)
395 XFree (info->xim_styles);
396 g_free (info->locale);
405 xim_instantiate_callback (Display *display, XPointer client_data,
408 GtkXIMInfo *info = (GtkXIMInfo*)client_data;
411 im = XOpenIM (display, NULL, NULL, NULL);
419 XUnregisterIMInstantiateCallback (display, NULL, NULL, NULL,
420 xim_instantiate_callback,
422 info->reconnecting = FALSE;
425 /* initialize info->im */
427 xim_info_try_im (GtkXIMInfo *info)
429 GdkScreen *screen = info->screen;
430 GdkDisplay *display = gdk_screen_get_display (screen);
432 g_assert (info->im == NULL);
433 if (info->reconnecting)
436 if (XSupportsLocale ())
438 if (!XSetLocaleModifiers (""))
439 g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
440 info->im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
443 XRegisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY(display),
445 xim_instantiate_callback,
447 info->reconnecting = TRUE;
455 xim_destroy_callback (XIM xim,
456 XPointer client_data,
459 GtkXIMInfo *info = (GtkXIMInfo*)client_data;
463 g_signal_handler_disconnect (info->settings, info->status_set);
464 info->status_set = 0;
465 g_signal_handler_disconnect (info->settings, info->preedit_set);
466 info->preedit_set = 0;
468 reinitialize_all_ics (info);
469 xim_info_try_im (info);
474 get_im (GdkWindow *client_window,
479 GdkScreen *screen = gdk_window_get_screen (client_window);
485 GtkXIMInfo *tmp_info = tmp_list->data;
486 if (tmp_info->screen == screen &&
487 strcmp (tmp_info->locale, locale) == 0)
499 tmp_list = tmp_list->next;
504 info = g_new (GtkXIMInfo, 1);
505 open_ims = g_slist_prepend (open_ims, info);
507 info->screen = screen;
508 info->locale = g_strdup (locale);
509 info->xim_styles = NULL;
510 info->preedit_style_setting = 0;
511 info->status_style_setting = 0;
512 info->settings = NULL;
513 info->preedit_set = 0;
514 info->status_set = 0;
515 info->display_closed_cb = 0;
517 info->reconnecting = FALSE;
521 xim_info_try_im (info);
526 gtk_im_context_xim_class_init (GtkIMContextXIMClass *class)
528 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
529 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
531 parent_class = g_type_class_peek_parent (class);
533 im_context_class->set_client_window = gtk_im_context_xim_set_client_window;
534 im_context_class->filter_keypress = gtk_im_context_xim_filter_keypress;
535 im_context_class->reset = gtk_im_context_xim_reset;
536 im_context_class->get_preedit_string = gtk_im_context_xim_get_preedit_string;
537 im_context_class->focus_in = gtk_im_context_xim_focus_in;
538 im_context_class->focus_out = gtk_im_context_xim_focus_out;
539 im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
540 im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
541 gobject_class->finalize = gtk_im_context_xim_finalize;
545 gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
547 im_context_xim->use_preedit = TRUE;
548 im_context_xim->filter_key_release = FALSE;
549 im_context_xim->finalizing = FALSE;
550 im_context_xim->has_focus = FALSE;
551 im_context_xim->in_toplevel = FALSE;
555 gtk_im_context_xim_finalize (GObject *obj)
557 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);
559 context_xim->finalizing = TRUE;
561 if (context_xim->im_info && !context_xim->im_info->ics->next)
563 if (context_xim->im_info->reconnecting)
567 display = gdk_screen_get_display (context_xim->im_info->screen);
568 XUnregisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY (display),
570 xim_instantiate_callback,
571 (XPointer)context_xim->im_info);
573 else if (context_xim->im_info->im)
575 XIMCallback im_destroy_callback;
577 im_destroy_callback.client_data = NULL;
578 im_destroy_callback.callback = NULL;
579 XSetIMValues (context_xim->im_info->im,
580 XNDestroyCallback, &im_destroy_callback,
585 set_ic_client_window (context_xim, NULL);
587 g_free (context_xim->locale);
588 g_free (context_xim->mb_charset);
590 G_OBJECT_CLASS (parent_class)->finalize (obj);
594 reinitialize_ic (GtkIMContextXIM *context_xim)
598 XDestroyIC (context_xim->ic);
599 context_xim->ic = NULL;
600 update_status_window (context_xim);
602 if (context_xim->preedit_length)
604 context_xim->preedit_length = 0;
605 if (!context_xim->finalizing)
606 g_signal_emit_by_name (context_xim, "preedit-changed");
610 reset filter_key_release flag, otherwise keystrokes will be doubled
611 until reconnecting to XIM.
613 context_xim->filter_key_release = FALSE;
617 set_ic_client_window (GtkIMContextXIM *context_xim,
618 GdkWindow *client_window)
620 reinitialize_ic (context_xim);
621 if (context_xim->client_window)
623 context_xim->im_info->ics = g_slist_remove (context_xim->im_info->ics, context_xim);
624 context_xim->im_info = NULL;
627 context_xim->client_window = client_window;
629 if (context_xim->client_window)
631 context_xim->im_info = get_im (context_xim->client_window, context_xim->locale);
632 context_xim->im_info->ics = g_slist_prepend (context_xim->im_info->ics, context_xim);
635 update_client_widget (context_xim);
639 gtk_im_context_xim_set_client_window (GtkIMContext *context,
640 GdkWindow *client_window)
642 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
644 set_ic_client_window (context_xim, client_window);
648 gtk_im_context_xim_new (void)
650 GtkIMContextXIM *result;
651 const gchar *charset;
653 if (!GDK_IS_X11_DISPLAY(gdk_display_get_default()))
655 result = g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL);
657 result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
659 g_get_charset (&charset);
660 result->mb_charset = g_strdup (charset);
662 return GTK_IM_CONTEXT (result);
666 mb_to_utf8 (GtkIMContextXIM *context_xim,
669 GError *error = NULL;
672 if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
673 result = g_strdup (str);
676 result = g_convert (str, -1,
677 "UTF-8", context_xim->mb_charset,
681 g_warning ("Error converting text from IM to UTF-8: %s\n", error->message);
682 g_error_free (error);
690 gtk_im_context_xim_filter_keypress (GtkIMContext *context,
693 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
694 XIC ic = gtk_im_context_xim_get_ic (context_xim);
695 gchar static_buffer[256];
696 gchar *buffer = static_buffer;
697 gint buffer_size = sizeof(static_buffer) - 1;
701 gboolean result = FALSE;
702 GdkWindow *root_window = gdk_screen_get_root_window (gdk_window_get_screen (event->window));
704 XKeyPressedEvent xevent;
706 if (event->type == GDK_KEY_RELEASE && !context_xim->filter_key_release)
709 xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
710 xevent.serial = 0; /* hope it doesn't matter */
711 xevent.send_event = event->send_event;
712 xevent.display = GDK_WINDOW_XDISPLAY (event->window);
713 xevent.window = GDK_WINDOW_XID (event->window);
714 xevent.root = GDK_WINDOW_XID (root_window);
715 xevent.subwindow = xevent.window;
716 xevent.time = event->time;
717 xevent.x = xevent.x_root = 0;
718 xevent.y = xevent.y_root = 0;
719 xevent.state = event->state;
720 xevent.keycode = event->hardware_keycode;
721 xevent.same_screen = True;
723 if (XFilterEvent ((XEvent *)&xevent, GDK_WINDOW_XID (context_xim->client_window)))
727 (gtk_accelerator_get_default_mod_mask () & ~(GDK_SHIFT_MASK | GDK_CONTROL_MASK)))
732 num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
735 num_bytes = XLookupString (&xevent, buffer, buffer_size, &keysym, NULL);
736 status = XLookupBoth;
739 if (status == XBufferOverflow)
741 buffer_size = num_bytes;
742 if (buffer != static_buffer)
744 buffer = g_malloc (num_bytes + 1);
748 /* I don't know how we should properly handle XLookupKeysym or XLookupBoth
749 * here ... do input methods actually change the keysym? we can't really
750 * feed it back to accelerator processing at this point...
752 if (status == XLookupChars || status == XLookupBoth)
756 buffer[num_bytes] = '\0';
758 result_utf8 = mb_to_utf8 (context_xim, buffer);
761 if ((guchar)result_utf8[0] >= 0x20 &&
762 result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
763 * control characters into strings
766 g_signal_emit_by_name (context, "commit", result_utf8);
770 g_free (result_utf8);
774 if (buffer != static_buffer)
781 gtk_im_context_xim_focus_in (GtkIMContext *context)
783 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
785 if (!context_xim->has_focus)
787 XIC ic = gtk_im_context_xim_get_ic (context_xim);
789 context_xim->has_focus = TRUE;
790 update_status_window (context_xim);
800 gtk_im_context_xim_focus_out (GtkIMContext *context)
802 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
804 if (context_xim->has_focus)
806 XIC ic = gtk_im_context_xim_get_ic (context_xim);
808 context_xim->has_focus = FALSE;
809 update_status_window (context_xim);
819 gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
822 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
823 XIC ic = gtk_im_context_xim_get_ic (context_xim);
825 XVaNestedList preedit_attr;
832 spot.y = area->y + area->height;
834 preedit_attr = XVaCreateNestedList (0,
835 XNSpotLocation, &spot,
838 XNPreeditAttributes, preedit_attr,
846 gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
847 gboolean use_preedit)
849 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
851 use_preedit = use_preedit != FALSE;
853 if (context_xim->use_preedit != use_preedit)
855 context_xim->use_preedit = use_preedit;
856 reinitialize_ic (context_xim);
863 gtk_im_context_xim_reset (GtkIMContext *context)
865 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
866 XIC ic = gtk_im_context_xim_get_ic (context_xim);
869 /* restore conversion state after resetting ic later */
870 XIMPreeditState preedit_state = XIMPreeditUnKnown;
871 XVaNestedList preedit_attr;
872 gboolean have_preedit_state = FALSE;
878 if (context_xim->preedit_length == 0)
881 preedit_attr = XVaCreateNestedList(0,
882 XNPreeditState, &preedit_state,
884 if (!XGetICValues(ic,
885 XNPreeditAttributes, preedit_attr,
887 have_preedit_state = TRUE;
891 result = XmbResetIC (ic);
893 preedit_attr = XVaCreateNestedList(0,
894 XNPreeditState, preedit_state,
896 if (have_preedit_state)
898 XNPreeditAttributes, preedit_attr,
905 char *result_utf8 = mb_to_utf8 (context_xim, result);
908 g_signal_emit_by_name (context, "commit", result_utf8);
909 g_free (result_utf8);
913 if (context_xim->preedit_length)
915 context_xim->preedit_length = 0;
916 g_signal_emit_by_name (context, "preedit-changed");
922 /* Mask of feedback bits that we render
924 #define FEEDBACK_MASK (XIMReverse | XIMUnderline)
927 add_feedback_attr (PangoAttrList *attrs,
929 XIMFeedback feedback,
933 PangoAttribute *attr;
935 gint start_index = g_utf8_offset_to_pointer (str, start_pos) - str;
936 gint end_index = g_utf8_offset_to_pointer (str, end_pos) - str;
938 if (feedback & XIMUnderline)
940 attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
941 attr->start_index = start_index;
942 attr->end_index = end_index;
944 pango_attr_list_change (attrs, attr);
947 if (feedback & XIMReverse)
949 attr = pango_attr_foreground_new (0xffff, 0xffff, 0xffff);
950 attr->start_index = start_index;
951 attr->end_index = end_index;
953 pango_attr_list_change (attrs, attr);
955 attr = pango_attr_background_new (0, 0, 0);
956 attr->start_index = start_index;
957 attr->end_index = end_index;
959 pango_attr_list_change (attrs, attr);
962 if (feedback & ~FEEDBACK_MASK)
963 g_warning ("Unrendered feedback style: %#lx", feedback & ~FEEDBACK_MASK);
967 gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
969 PangoAttrList **attrs,
972 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
973 gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
978 XIMFeedback last_feedback = 0;
981 *attrs = pango_attr_list_new ();
983 for (i = 0; i < context_xim->preedit_length; i++)
985 XIMFeedback new_feedback = context_xim->feedbacks[i] & FEEDBACK_MASK;
986 if (new_feedback != last_feedback)
989 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
991 last_feedback = new_feedback;
997 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
1006 *cursor_pos = context_xim->preedit_cursor;
1010 preedit_start_callback (XIC xic,
1011 XPointer client_data,
1014 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
1015 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
1017 if (!context_xim->finalizing)
1018 g_signal_emit_by_name (context, "preedit-start");
1020 return -1; /* No length limit */
1024 preedit_done_callback (XIC xic,
1025 XPointer client_data,
1028 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
1029 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
1031 if (context_xim->preedit_length)
1033 context_xim->preedit_length = 0;
1034 if (!context_xim->finalizing)
1035 g_signal_emit_by_name (context_xim, "preedit-changed");
1038 if (!context_xim->finalizing)
1039 g_signal_emit_by_name (context, "preedit-end");
1043 xim_text_to_utf8 (GtkIMContextXIM *context, XIMText *xim_text, gchar **text)
1045 gint text_length = 0;
1046 GError *error = NULL;
1047 gchar *result = NULL;
1049 if (xim_text && xim_text->string.multi_byte)
1051 if (xim_text->encoding_is_wchar)
1053 g_warning ("Wide character return from Xlib not currently supported");
1058 if (strcmp (context->mb_charset, "UTF-8") == 0)
1059 result = g_strdup (xim_text->string.multi_byte);
1061 result = g_convert (xim_text->string.multi_byte,
1064 context->mb_charset,
1065 NULL, NULL, &error);
1069 text_length = g_utf8_strlen (result, -1);
1071 if (text_length != xim_text->length)
1073 g_warning ("Size mismatch when converting text from input method: supplied length = %d\n, result length = %d", xim_text->length, text_length);
1078 g_warning ("Error converting text from IM to UCS-4: %s", error->message);
1079 g_error_free (error);
1096 preedit_draw_callback (XIC xic,
1097 XPointer client_data,
1098 XIMPreeditDrawCallbackStruct *call_data)
1100 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1102 XIMText *new_xim_text = call_data->text;
1103 gint new_text_length;
1104 gunichar *new_text = NULL;
1110 gint chg_first = CLAMP (call_data->chg_first, 0, context->preedit_length);
1111 gint chg_length = CLAMP (call_data->chg_length, 0, context->preedit_length - chg_first);
1113 context->preedit_cursor = call_data->caret;
1115 if (chg_first != call_data->chg_first || chg_length != call_data->chg_length)
1116 g_warning ("Invalid change to preedit string, first=%d length=%d (orig length == %d)",
1117 call_data->chg_first, call_data->chg_length, context->preedit_length);
1119 new_text_length = xim_text_to_utf8 (context, new_xim_text, &tmp);
1122 new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
1126 diff = new_text_length - chg_length;
1127 new_length = context->preedit_length + diff;
1129 if (new_length > context->preedit_size)
1131 context->preedit_size = new_length;
1132 context->preedit_chars = g_renew (gunichar, context->preedit_chars, new_length);
1133 context->feedbacks = g_renew (XIMFeedback, context->feedbacks, new_length);
1138 for (i = chg_first + chg_length ; i < context->preedit_length; i++)
1140 context->preedit_chars[i + diff] = context->preedit_chars[i];
1141 context->feedbacks[i + diff] = context->feedbacks[i];
1146 for (i = context->preedit_length - 1; i >= chg_first + chg_length ; i--)
1148 context->preedit_chars[i + diff] = context->preedit_chars[i];
1149 context->feedbacks[i + diff] = context->feedbacks[i];
1153 for (i = 0; i < new_text_length; i++)
1155 context->preedit_chars[chg_first + i] = new_text[i];
1156 context->feedbacks[chg_first + i] = new_xim_text->feedback[i];
1159 context->preedit_length += diff;
1163 if (!context->finalizing)
1164 g_signal_emit_by_name (context, "preedit-changed");
1169 preedit_caret_callback (XIC xic,
1170 XPointer client_data,
1171 XIMPreeditCaretCallbackStruct *call_data)
1173 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1175 if (call_data->direction == XIMAbsolutePosition)
1177 context->preedit_cursor = call_data->position;
1178 if (!context->finalizing)
1179 g_signal_emit_by_name (context, "preedit-changed");
1183 g_warning ("Caret movement command: %d %d %d not supported",
1184 call_data->position, call_data->direction, call_data->style);
1189 status_start_callback (XIC xic,
1190 XPointer client_data,
1197 status_done_callback (XIC xic,
1198 XPointer client_data,
1205 status_draw_callback (XIC xic,
1206 XPointer client_data,
1207 XIMStatusDrawCallbackStruct *call_data)
1209 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1211 if (call_data->type == XIMTextType)
1214 xim_text_to_utf8 (context, call_data->data.text, &text);
1216 if (context->status_window)
1217 status_window_set_text (context->status_window, text ? text : "");
1221 g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
1226 string_conversion_callback (XIC xic, XPointer client_data, XPointer call_data)
1228 GtkIMContextXIM *context_xim;
1229 XIMStringConversionCallbackStruct *conv_data;
1233 context_xim = (GtkIMContextXIM *)client_data;
1234 conv_data = (XIMStringConversionCallbackStruct *)call_data;
1236 if (gtk_im_context_get_surrounding ((GtkIMContext *)context_xim,
1237 &surrounding, &cursor_index))
1241 gint subst_offset = 0, subst_nchars = 0;
1243 gchar *p = surrounding + cursor_index, *q;
1244 gshort position = (gshort)conv_data->position;
1248 for (i = position; i > 0 && *p; --i)
1249 p = g_utf8_next_char (p);
1253 /* According to X11R6.4 Xlib - C Library Reference Manual
1254 * section 13.5.7.3 String Conversion Callback,
1255 * XIMStringConversionPosition is starting position _relative_
1256 * to current client's cursor position. So it should be able
1257 * to be negative, or referring to a position before the cursor
1258 * would be impossible. But current X protocol defines this as
1259 * unsigned short. So, compiler may warn about the value range
1260 * here. We hope the X protocol is fixed soon.
1262 else if (position < 0)
1264 for (i = position; i < 0 && p > surrounding; ++i)
1265 p = g_utf8_prev_char (p);
1270 switch (conv_data->direction)
1272 case XIMForwardChar:
1273 for (i = conv_data->factor, q = p; i > 0 && *q; --i)
1274 q = g_utf8_next_char (q);
1277 text = g_locale_from_utf8 (p, q - p, NULL, &text_len, NULL);
1278 subst_offset = position;
1279 subst_nchars = conv_data->factor;
1282 case XIMBackwardChar:
1283 for (i = conv_data->factor, q = p; i > 0 && q > surrounding; --i)
1284 q = g_utf8_prev_char (q);
1287 text = g_locale_from_utf8 (q, p - q, NULL, &text_len, NULL);
1288 subst_offset = position - conv_data->factor;
1289 subst_nchars = conv_data->factor;
1292 case XIMForwardWord:
1293 case XIMBackwardWord:
1297 case XIMPreviousLine:
1300 case XIMAbsolutePosition:
1305 /* block out any failure happenning to "text", including conversion */
1308 conv_data->text = (XIMStringConversionText *)
1309 malloc (sizeof (XIMStringConversionText));
1310 if (conv_data->text)
1312 conv_data->text->length = text_len;
1313 conv_data->text->feedback = NULL;
1314 conv_data->text->encoding_is_wchar = False;
1315 conv_data->text->string.mbs = (char *)malloc (text_len);
1316 if (conv_data->text->string.mbs)
1317 memcpy (conv_data->text->string.mbs, text, text_len);
1320 free (conv_data->text);
1321 conv_data->text = NULL;
1327 if (conv_data->operation == XIMStringConversionSubstitution
1328 && subst_nchars > 0)
1330 gtk_im_context_delete_surrounding ((GtkIMContext *)context_xim,
1331 subst_offset, subst_nchars);
1334 g_free (surrounding);
1339 static XVaNestedList
1340 set_preedit_callback (GtkIMContextXIM *context_xim)
1342 context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
1343 context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
1344 context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
1345 context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
1346 context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
1347 context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
1348 context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
1349 context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
1350 return XVaCreateNestedList (0,
1351 XNPreeditStartCallback, &context_xim->preedit_start_callback,
1352 XNPreeditDoneCallback, &context_xim->preedit_done_callback,
1353 XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
1354 XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
1358 static XVaNestedList
1359 set_status_callback (GtkIMContextXIM *context_xim)
1361 context_xim->status_start_callback.client_data = (XPointer)context_xim;
1362 context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
1363 context_xim->status_done_callback.client_data = (XPointer)context_xim;
1364 context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
1365 context_xim->status_draw_callback.client_data = (XPointer)context_xim;
1366 context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
1368 return XVaCreateNestedList (0,
1369 XNStatusStartCallback, &context_xim->status_start_callback,
1370 XNStatusDoneCallback, &context_xim->status_done_callback,
1371 XNStatusDrawCallback, &context_xim->status_draw_callback,
1377 set_string_conversion_callback (GtkIMContextXIM *context_xim, XIC xic)
1379 if (!context_xim->im_info->supports_string_conversion)
1382 context_xim->string_conversion_callback.client_data = (XPointer)context_xim;
1383 context_xim->string_conversion_callback.callback = (XIMProc)string_conversion_callback;
1386 XNStringConversionCallback,
1387 (XPointer)&context_xim->string_conversion_callback,
1392 gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
1394 if (context_xim->im_info == NULL || context_xim->im_info->im == NULL)
1397 if (!context_xim->ic)
1399 const char *name1 = NULL;
1400 XVaNestedList list1 = NULL;
1401 const char *name2 = NULL;
1402 XVaNestedList list2 = NULL;
1403 XIMStyle im_style = 0;
1406 if (context_xim->use_preedit &&
1407 (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
1409 im_style |= XIMPreeditCallbacks;
1410 name1 = XNPreeditAttributes;
1411 list1 = set_preedit_callback (context_xim);
1413 else if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditNone)
1414 im_style |= XIMPreeditNone;
1416 im_style |= XIMPreeditNothing;
1418 if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
1420 im_style |= XIMStatusCallbacks;
1423 name1 = XNStatusAttributes;
1424 list1 = set_status_callback (context_xim);
1428 name2 = XNStatusAttributes;
1429 list2 = set_status_callback (context_xim);
1432 else if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusNone)
1433 im_style |= XIMStatusNone;
1435 im_style |= XIMStatusNothing;
1437 xic = XCreateIC (context_xim->im_info->im,
1438 XNInputStyle, im_style,
1439 XNClientWindow, GDK_WINDOW_XID (context_xim->client_window),
1450 /* Don't filter key released events with XFilterEvents unless
1451 * input methods ask for. This is a workaround for Solaris input
1452 * method bug in C and European locales. It doubles each key
1453 * stroke if both key pressed and released events are filtered.
1456 gulong mask = 0xaaaaaaaa;
1458 XNFilterEvents, &mask,
1460 context_xim->filter_key_release = (mask & KeyReleaseMask) != 0;
1461 set_string_conversion_callback (context_xim, xic);
1464 context_xim->ic = xic;
1466 update_status_window (context_xim);
1468 if (xic && context_xim->has_focus)
1471 return context_xim->ic;
1474 /*****************************************************************
1475 * Status Window handling
1477 * A status window is a small window attached to the toplevel
1478 * that is used to display information to the user about the
1479 * current input operation.
1481 * We claim the toplevel's status window for an input context if:
1483 * A) The input context has a toplevel
1484 * B) The input context has the focus
1485 * C) The input context has an XIC associated with it
1487 * Tracking A) and C) is pretty reliable since we
1488 * compute A) and create the XIC for C) ourselves.
1489 * For B) we basically have to depend on our callers
1490 * calling ::focus-in and ::focus-out at the right time.
1492 * The toplevel is computed by walking up the GdkWindow
1493 * hierarchy from context->client_window until we find a
1494 * window that is owned by some widget, and then calling
1495 * gtk_widget_get_toplevel() on that widget. This should
1496 * handle both cases where we might have GdkWindows without widgets,
1497 * and cases where GtkWidgets have strange window hierarchies
1498 * (like a torn off GtkHandleBox.)
1500 * The status window is visible if and only if there is text
1501 * for it; whenever a new GtkIMContextXIM claims the status
1502 * window, we blank out any existing text. We actually only
1503 * create a GtkWindow for the status window the first time
1504 * it is shown; this is an important optimization when we are
1505 * using XIM with something like a simple compose-key input
1506 * method that never needs a status window.
1507 *****************************************************************/
1509 /* Called when we no longer need a status window
1512 disclaim_status_window (GtkIMContextXIM *context_xim)
1514 if (context_xim->status_window)
1516 g_assert (context_xim->status_window->context == context_xim);
1518 status_window_set_text (context_xim->status_window, "");
1520 context_xim->status_window->context = NULL;
1521 context_xim->status_window = NULL;
1525 /* Called when we need a status window
1528 claim_status_window (GtkIMContextXIM *context_xim)
1530 if (!context_xim->status_window && context_xim->client_widget)
1532 GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
1533 if (toplevel && gtk_widget_is_toplevel (toplevel))
1535 StatusWindow *status_window = status_window_get (toplevel);
1537 if (status_window->context)
1538 disclaim_status_window (status_window->context);
1540 status_window->context = context_xim;
1541 context_xim->status_window = status_window;
1546 /* Basic call made whenever something changed that might cause
1547 * us to need, or not to need a status window.
1550 update_status_window (GtkIMContextXIM *context_xim)
1552 if (context_xim->ic && context_xim->in_toplevel && context_xim->has_focus)
1553 claim_status_window (context_xim);
1555 disclaim_status_window (context_xim);
1558 /* Updates the in_toplevel flag for @context_xim
1561 update_in_toplevel (GtkIMContextXIM *context_xim)
1563 if (context_xim->client_widget)
1565 GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
1567 context_xim->in_toplevel = (toplevel && gtk_widget_is_toplevel (toplevel));
1570 context_xim->in_toplevel = FALSE;
1572 /* Some paranoia, in case we don't get a focus out */
1573 if (!context_xim->in_toplevel)
1574 context_xim->has_focus = FALSE;
1576 update_status_window (context_xim);
1579 /* Callback when @widget's toplevel changes. It will always
1580 * change from NULL to a window, or a window to NULL;
1581 * we use that intermediate NULL state to make sure
1582 * that we disclaim the toplevel status window for the old
1586 on_client_widget_hierarchy_changed (GtkWidget *widget,
1587 GtkWidget *old_toplevel,
1588 GtkIMContextXIM *context_xim)
1590 update_in_toplevel (context_xim);
1593 /* Finds the GtkWidget that owns the window, or if none, the
1594 * widget owning the nearest parent that has a widget.
1597 widget_for_window (GdkWindow *window)
1602 gdk_window_get_user_data (window, &user_data);
1606 window = gdk_window_get_parent (window);
1612 /* Called when context_xim->client_window changes; takes care of
1613 * removing and/or setting up our watches for the toplevel
1616 update_client_widget (GtkIMContextXIM *context_xim)
1618 GtkWidget *new_client_widget = widget_for_window (context_xim->client_window);
1620 if (new_client_widget != context_xim->client_widget)
1622 if (context_xim->client_widget)
1624 g_signal_handlers_disconnect_by_func (context_xim->client_widget,
1625 G_CALLBACK (on_client_widget_hierarchy_changed),
1628 context_xim->client_widget = new_client_widget;
1629 if (context_xim->client_widget)
1631 g_signal_connect (context_xim->client_widget, "hierarchy-changed",
1632 G_CALLBACK (on_client_widget_hierarchy_changed),
1636 update_in_toplevel (context_xim);
1640 /* Called when the toplevel is destroyed; frees the status window
1643 on_status_toplevel_destroy (GtkWidget *toplevel,
1644 StatusWindow *status_window)
1646 status_window_free (status_window);
1649 /* Called when the screen for the toplevel changes; updates the
1650 * screen for the status window to match.
1653 on_status_toplevel_notify_screen (GtkWindow *toplevel,
1655 StatusWindow *status_window)
1657 if (status_window->window)
1658 gtk_window_set_screen (GTK_WINDOW (status_window->window),
1659 gtk_widget_get_screen (GTK_WIDGET (toplevel)));
1662 /* Called when the toplevel window is moved; updates the position of
1663 * the status window to follow it.
1666 on_status_toplevel_configure (GtkWidget *toplevel,
1667 GdkEventConfigure *event,
1668 StatusWindow *status_window)
1671 GtkRequisition requisition;
1675 if (status_window->window)
1677 height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
1679 gdk_window_get_frame_extents (gtk_widget_get_window (toplevel),
1681 gtk_widget_get_preferred_size ( (status_window->window),
1682 &requisition, NULL);
1684 if (rect.y + rect.height + requisition.height < height)
1685 y = rect.y + rect.height;
1687 y = height - requisition.height;
1689 gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
1695 /* Frees a status window and removes its link from the status_windows list
1698 status_window_free (StatusWindow *status_window)
1700 status_windows = g_slist_remove (status_windows, status_window);
1702 if (status_window->context)
1703 status_window->context->status_window = NULL;
1705 g_signal_handlers_disconnect_by_func (status_window->toplevel,
1706 G_CALLBACK (on_status_toplevel_destroy),
1708 g_signal_handlers_disconnect_by_func (status_window->toplevel,
1709 G_CALLBACK (on_status_toplevel_notify_screen),
1711 g_signal_handlers_disconnect_by_func (status_window->toplevel,
1712 G_CALLBACK (on_status_toplevel_configure),
1715 if (status_window->window)
1716 gtk_widget_destroy (status_window->window);
1718 g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
1720 g_free (status_window);
1723 /* Finds the status window object for a toplevel, creating it if necessary.
1725 static StatusWindow *
1726 status_window_get (GtkWidget *toplevel)
1728 StatusWindow *status_window;
1730 status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
1732 return status_window;
1734 status_window = g_new0 (StatusWindow, 1);
1735 status_window->toplevel = toplevel;
1737 status_windows = g_slist_prepend (status_windows, status_window);
1739 g_signal_connect (toplevel, "destroy",
1740 G_CALLBACK (on_status_toplevel_destroy),
1742 g_signal_connect (toplevel, "configure-event",
1743 G_CALLBACK (on_status_toplevel_configure),
1745 g_signal_connect (toplevel, "notify::screen",
1746 G_CALLBACK (on_status_toplevel_notify_screen),
1749 g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);
1751 return status_window;
1754 /* Draw the background (normally white) and border for the status window
1757 on_status_window_draw (GtkWidget *widget,
1760 GtkStyleContext *style;
1763 style = gtk_widget_get_style_context (widget);
1765 gtk_style_context_get_background_color (style, 0, &color);
1766 gdk_cairo_set_source_rgba (cr, &color);
1769 gtk_style_context_get_color (style, 0, &color);
1770 gdk_cairo_set_source_rgba (cr, &color);
1773 cairo_rectangle (cr,
1775 gtk_widget_get_allocated_width (widget) - 1,
1776 gtk_widget_get_allocated_height (widget) - 1);
1782 /* Creates the widgets for the status window; called when we
1783 * first need to show text for the status window.
1786 status_window_make_window (StatusWindow *status_window)
1789 GtkWidget *status_label;
1791 status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
1792 window = status_window->window;
1794 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
1795 gtk_widget_set_app_paintable (window, TRUE);
1797 status_label = gtk_label_new ("");
1798 g_object_set (status_label, "margin", 1, NULL);
1799 gtk_widget_show (status_label);
1801 gtk_container_add (GTK_CONTAINER (window), status_label);
1803 g_signal_connect (window, "draw",
1804 G_CALLBACK (on_status_window_draw), NULL);
1806 gtk_window_set_screen (GTK_WINDOW (status_window->window),
1807 gtk_widget_get_screen (status_window->toplevel));
1809 on_status_toplevel_configure (status_window->toplevel, NULL, status_window);
1812 /* Updates the text in the status window, hiding or
1813 * showing the window as necessary.
1816 status_window_set_text (StatusWindow *status_window,
1823 if (!status_window->window)
1824 status_window_make_window (status_window);
1826 label = gtk_bin_get_child (GTK_BIN (status_window->window));
1827 gtk_label_set_text (GTK_LABEL (label), text);
1829 gtk_widget_show (status_window->window);
1833 if (status_window->window)
1834 gtk_widget_hide (status_window->window);
1839 * gtk_im_context_xim_shutdown:
1841 * Destroys all the status windows that are kept by the XIM contexts. This
1842 * function should only be called by the XIM module exit routine.
1845 gtk_im_context_xim_shutdown (void)
1847 while (status_windows)
1848 status_window_free (status_windows->data);
1852 GtkXIMInfo *info = open_ims->data;
1853 GdkDisplay *display = gdk_screen_get_display (info->screen);
1855 xim_info_display_closed (display, FALSE, info);
1856 open_ims = g_slist_remove_link (open_ims, open_ims);