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;
36 XIMStyle preedit_style_setting;
37 XIMStyle status_style_setting;
39 GtkSettings *settings;
42 XIMStyles *xim_styles;
45 guint reconnecting :1;
48 /* A context status window; these are kept in the status_windows list. */
53 /* Toplevel window to which the status window corresponds */
56 /* Signal connection ids; we connect to the toplevel */
57 gulong destroy_handler_id;
58 gulong configure_handler_id;
61 static void gtk_im_context_xim_class_init (GtkIMContextXIMClass *class);
62 static void gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim);
63 static void gtk_im_context_xim_finalize (GObject *obj);
64 static void gtk_im_context_xim_set_client_window (GtkIMContext *context,
65 GdkWindow *client_window);
66 static gboolean gtk_im_context_xim_filter_keypress (GtkIMContext *context,
68 static void gtk_im_context_xim_reset (GtkIMContext *context);
69 static void gtk_im_context_xim_focus_in (GtkIMContext *context);
70 static void gtk_im_context_xim_focus_out (GtkIMContext *context);
71 static void gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
73 static void gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
74 gboolean use_preedit);
75 static void gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
77 PangoAttrList **attrs,
80 static void reinitialize_ic (GtkIMContextXIM *context_xim);
81 static void set_ic_client_window (GtkIMContextXIM *context_xim,
82 GdkWindow *client_window);
84 static void setup_styles (GtkXIMInfo *info);
86 static void status_window_show (GtkIMContextXIM *context_xim);
87 static void status_window_hide (GtkIMContextXIM *context_xim);
88 static void status_window_set_text (GtkIMContextXIM *context_xim,
91 static void xim_destroy_callback (XIM xim,
95 static XIC gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim);
96 static GObjectClass *parent_class;
98 GType gtk_type_im_context_xim = 0;
100 GSList *open_ims = NULL;
102 /* List of status windows for different toplevels */
103 static GSList *status_windows = NULL;
106 gtk_im_context_xim_register_type (GTypeModule *type_module)
108 static const GTypeInfo im_context_xim_info =
110 sizeof (GtkIMContextXIMClass),
111 (GBaseInitFunc) NULL,
112 (GBaseFinalizeFunc) NULL,
113 (GClassInitFunc) gtk_im_context_xim_class_init,
114 NULL, /* class_finalize */
115 NULL, /* class_data */
116 sizeof (GtkIMContextXIM),
118 (GInstanceInitFunc) gtk_im_context_xim_init,
121 gtk_type_im_context_xim =
122 g_type_module_register_type (type_module,
125 &im_context_xim_info, 0);
128 #define PREEDIT_MASK (XIMPreeditCallbacks | XIMPreeditPosition | \
129 XIMPreeditArea | XIMPreeditNothing | XIMPreeditNone)
130 #define STATUS_MASK (XIMStatusCallbacks | XIMStatusArea | \
131 XIMStatusNothing | XIMStatusNone)
132 #define ALLOWED_MASK (XIMPreeditCallbacks | XIMPreeditNothing | XIMPreeditNone | \
133 XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone)
136 choose_better_style (XIMStyle style1, XIMStyle style2)
140 if (style1 == 0) return style2;
141 if (style2 == 0) return style1;
142 if ((style1 & (PREEDIT_MASK | STATUS_MASK))
143 == (style2 & (PREEDIT_MASK | STATUS_MASK)))
146 s1 = style1 & PREEDIT_MASK;
147 s2 = style2 & PREEDIT_MASK;
150 if (u & XIMPreeditCallbacks)
151 return (s1 == XIMPreeditCallbacks) ? style1 : style2;
152 else if (u & XIMPreeditPosition)
153 return (s1 == XIMPreeditPosition) ? style1 :style2;
154 else if (u & XIMPreeditArea)
155 return (s1 == XIMPreeditArea) ? style1 : style2;
156 else if (u & XIMPreeditNothing)
157 return (s1 == XIMPreeditNothing) ? style1 : style2;
158 else if (u & XIMPreeditNone)
159 return (s1 == XIMPreeditNone) ? style1 : style2;
161 s1 = style1 & STATUS_MASK;
162 s2 = style2 & STATUS_MASK;
164 if (u & XIMStatusCallbacks)
165 return (s1 == XIMStatusCallbacks) ? style1 : style2;
166 else if (u & XIMStatusArea)
167 return (s1 == XIMStatusArea) ? style1 : style2;
168 else if (u & XIMStatusNothing)
169 return (s1 == XIMStatusNothing) ? style1 : style2;
170 else if (u & XIMStatusNone)
171 return (s1 == XIMStatusNone) ? style1 : style2;
173 return 0; /* Get rid of stupid warning */
177 reinitialize_all_ics (GtkXIMInfo *info)
181 for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
182 reinitialize_ic (tmp_list->data);
186 status_style_change (GtkXIMInfo *info)
188 GtkIMStatusStyle status_style;
190 g_object_get (info->settings,
191 "gtk-im-status-style", &status_style,
193 if (status_style == GTK_IM_STATUS_CALLBACK)
194 info->status_style_setting = XIMStatusCallbacks;
195 else if (status_style == GTK_IM_STATUS_NOTHING)
196 info->status_style_setting = XIMStatusNothing;
197 else if (status_style == GTK_IM_STATUS_NONE)
198 info->status_style_setting = XIMStatusNone;
204 reinitialize_all_ics (info);
208 preedit_style_change (GtkXIMInfo *info)
210 GtkIMPreeditStyle preedit_style;
211 g_object_get (info->settings,
212 "gtk-im-preedit-style", &preedit_style,
214 if (preedit_style == GTK_IM_PREEDIT_CALLBACK)
215 info->preedit_style_setting = XIMPreeditCallbacks;
216 else if (preedit_style == GTK_IM_PREEDIT_NOTHING)
217 info->preedit_style_setting = XIMPreeditNothing;
218 else if (preedit_style == GTK_IM_PREEDIT_NONE)
219 info->preedit_style_setting = XIMPreeditNone;
225 reinitialize_all_ics (info);
229 setup_styles (GtkXIMInfo *info)
232 unsigned long settings_preference;
233 XIMStyles *xim_styles = info->xim_styles;
235 settings_preference = info->status_style_setting|info->preedit_style_setting;
239 for (i = 0; i < xim_styles->count_styles; i++)
240 if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
242 if (settings_preference == xim_styles->supported_styles[i])
244 info->style = settings_preference;
247 info->style = choose_better_style (info->style,
248 xim_styles->supported_styles[i]);
251 if (info->style == 0)
252 info->style = XIMPreeditNothing | XIMStatusNothing;
256 setup_im (GtkXIMInfo *info)
258 XIMValuesList *ic_values = NULL;
259 XIMCallback im_destroy_callback;
261 if (info->im == NULL)
264 im_destroy_callback.client_data = (XPointer)info;
265 im_destroy_callback.callback = (XIMProc)xim_destroy_callback;
266 XSetIMValues (info->im,
267 XNDestroyCallback, &im_destroy_callback,
270 XGetIMValues (info->im,
271 XNQueryInputStyle, &info->xim_styles,
272 XNQueryICValuesList, &ic_values,
275 info->settings = gtk_settings_get_for_screen (info->screen);
277 if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
278 "gtk-im-preedit-style"))
279 gtk_settings_install_property (g_param_spec_enum ("gtk-im-preedit-style",
280 _("IM Preedit style"),
281 _("How to draw the input method preedit string"),
282 GTK_TYPE_IM_PREEDIT_STYLE,
283 GTK_IM_PREEDIT_CALLBACK,
286 if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
287 "gtk-im-status-style"))
288 gtk_settings_install_property (g_param_spec_enum ("gtk-im-status-style",
289 _("IM Status style"),
290 _("How to draw the input method statusbar"),
291 GTK_TYPE_IM_STATUS_STYLE,
292 GTK_IM_STATUS_CALLBACK,
295 info->status_set = g_signal_connect_swapped (info->settings,
296 "notify::gtk-im-status-style",
297 G_CALLBACK (status_style_change),
299 info->preedit_set = g_signal_connect_swapped (info->settings,
300 "notify::gtk-im-preedit-style",
301 G_CALLBACK (preedit_style_change),
304 status_style_change (info);
305 preedit_style_change (info);
310 for (i = 0; i < ic_values->count_values; i++)
311 g_print ("%s\n", ic_values->supported_values[i]);
312 for (i = 0; i < xim_styles->count_styles; i++)
313 g_print ("%#x\n", xim_styles->supported_styles[i]);
322 xim_info_display_closed (GdkDisplay *display,
326 GSList *ics, *tmp_list;
328 open_ims = g_slist_remove (open_ims, info);
333 for (tmp_list = ics; tmp_list; tmp_list = tmp_list->next)
334 set_ic_client_window (tmp_list->data, NULL);
338 g_signal_handler_disconnect (info->settings, info->status_set);
339 g_signal_handler_disconnect (info->settings, info->preedit_set);
341 XFree (info->xim_styles->supported_styles);
342 XFree (info->xim_styles);
343 g_free (info->locale);
352 xim_instantiate_callback (Display *display, XPointer client_data,
355 GtkXIMInfo *info = (GtkXIMInfo*)client_data;
358 im = XOpenIM (display, NULL, NULL, NULL);
366 XUnregisterIMInstantiateCallback (display, NULL, NULL, NULL,
367 xim_instantiate_callback,
369 info->reconnecting = FALSE;
372 /* initialize info->im */
374 xim_info_try_im (GtkXIMInfo *info)
376 GdkScreen *screen = info->screen;
377 GdkDisplay *display = gdk_screen_get_display (screen);
379 g_assert (info->im == NULL);
380 if (info->reconnecting)
383 if (XSupportsLocale ())
385 if (!XSetLocaleModifiers (""))
386 g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
387 info->im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
390 XRegisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY(display),
392 xim_instantiate_callback,
394 info->reconnecting = TRUE;
399 g_signal_connect (display, "closed",
400 G_CALLBACK (xim_info_display_closed), info);
405 xim_destroy_callback (XIM xim,
406 XPointer client_data,
409 GtkXIMInfo *info = (GtkXIMInfo*)client_data;
413 g_signal_handler_disconnect (info->settings, info->status_set);
414 g_signal_handler_disconnect (info->settings, info->preedit_set);
416 reinitialize_all_ics (info);
417 xim_info_try_im (info);
422 get_im (GdkWindow *client_window,
427 GdkScreen *screen = gdk_drawable_get_screen (client_window);
433 info = tmp_list->data;
434 if (info->screen == screen &&
435 strcmp (info->locale, locale) == 0)
442 tmp_list = tmp_list->next;
447 info = g_new (GtkXIMInfo, 1);
448 open_ims = g_slist_prepend (open_ims, info);
450 info->screen = screen;
451 info->locale = g_strdup (locale);
452 info->xim_styles = NULL;
453 info->preedit_style_setting = 0;
454 info->status_style_setting = 0;
455 info->settings = NULL;
456 info->preedit_set = 0;
457 info->status_set = 0;
459 info->reconnecting = FALSE;
463 xim_info_try_im (info);
468 gtk_im_context_xim_class_init (GtkIMContextXIMClass *class)
470 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
471 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
473 parent_class = g_type_class_peek_parent (class);
475 im_context_class->set_client_window = gtk_im_context_xim_set_client_window;
476 im_context_class->filter_keypress = gtk_im_context_xim_filter_keypress;
477 im_context_class->reset = gtk_im_context_xim_reset;
478 im_context_class->get_preedit_string = gtk_im_context_xim_get_preedit_string;
479 im_context_class->focus_in = gtk_im_context_xim_focus_in;
480 im_context_class->focus_out = gtk_im_context_xim_focus_out;
481 im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
482 im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
483 gobject_class->finalize = gtk_im_context_xim_finalize;
487 gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
489 im_context_xim->use_preedit = TRUE;
490 im_context_xim->filter_key_release = FALSE;
491 im_context_xim->status_visible = FALSE;
492 im_context_xim->finalizing = FALSE;
496 gtk_im_context_xim_finalize (GObject *obj)
498 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);
500 context_xim->finalizing = TRUE;
502 set_ic_client_window (context_xim, NULL);
504 g_free (context_xim->locale);
505 g_free (context_xim->mb_charset);
509 reinitialize_ic (GtkIMContextXIM *context_xim)
513 XDestroyIC (context_xim->ic);
514 status_window_hide (context_xim);
515 context_xim->ic = NULL;
517 if (context_xim->preedit_length)
519 context_xim->preedit_length = 0;
520 if (!context_xim->finalizing)
521 g_signal_emit_by_name (context_xim, "preedit_changed");
525 reset filter_key_release flag, otherwise keystrokes will be doubled
526 until reconnecting to XIM.
528 context_xim->filter_key_release = FALSE;
532 set_ic_client_window (GtkIMContextXIM *context_xim,
533 GdkWindow *client_window)
535 reinitialize_ic (context_xim);
536 if (context_xim->client_window)
538 context_xim->im_info->ics = g_slist_remove (context_xim->im_info->ics, context_xim);
539 context_xim->im_info = NULL;
542 context_xim->client_window = client_window;
544 if (context_xim->client_window)
546 context_xim->im_info = get_im (context_xim->client_window, context_xim->locale);
547 context_xim->im_info->ics = g_slist_prepend (context_xim->im_info->ics, context_xim);
552 gtk_im_context_xim_set_client_window (GtkIMContext *context,
553 GdkWindow *client_window)
555 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
557 set_ic_client_window (context_xim, client_window);
561 gtk_im_context_xim_new (void)
563 GtkIMContextXIM *result;
564 const gchar *charset;
566 result = GTK_IM_CONTEXT_XIM (g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL));
568 result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
570 g_get_charset (&charset);
571 result->mb_charset = g_strdup (charset);
573 return GTK_IM_CONTEXT (result);
577 mb_to_utf8 (GtkIMContextXIM *context_xim,
580 GError *error = NULL;
583 if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
584 result = g_strdup (str);
587 result = g_convert (str, -1,
588 "UTF-8", context_xim->mb_charset,
592 g_warning ("Error converting text from IM to UTF-8: %s\n", error->message);
593 g_error_free (error);
601 gtk_im_context_xim_filter_keypress (GtkIMContext *context,
604 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
605 XIC ic = gtk_im_context_xim_get_ic (context_xim);
606 gchar static_buffer[256];
607 gchar *buffer = static_buffer;
608 gint buffer_size = sizeof(static_buffer) - 1;
612 gboolean result = FALSE;
613 GdkWindow *root_window = gdk_screen_get_root_window (gdk_drawable_get_screen (event->window));
615 XKeyPressedEvent xevent;
617 if (event->type == GDK_KEY_RELEASE && !context_xim->filter_key_release)
620 xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
621 xevent.serial = 0; /* hope it doesn't matter */
622 xevent.send_event = event->send_event;
623 xevent.display = GDK_DRAWABLE_XDISPLAY (event->window);
624 xevent.window = GDK_DRAWABLE_XID (event->window);
625 xevent.root = GDK_DRAWABLE_XID (root_window);
626 xevent.subwindow = xevent.window;
627 xevent.time = event->time;
628 xevent.x = xevent.x_root = 0;
629 xevent.y = xevent.y_root = 0;
630 xevent.state = event->state;
631 xevent.keycode = event->hardware_keycode;
632 xevent.same_screen = True;
634 if (XFilterEvent ((XEvent *)&xevent, GDK_DRAWABLE_XID (context_xim->client_window)))
639 num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
642 num_bytes = XLookupString (&xevent, buffer, buffer_size, &keysym, NULL);
643 status = XLookupBoth;
646 if (status == XBufferOverflow)
648 buffer_size = num_bytes;
649 if (buffer != static_buffer)
651 buffer = g_malloc (num_bytes + 1);
655 /* I don't know how we should properly handle XLookupKeysym or XLookupBoth
656 * here ... do input methods actually change the keysym? we can't really
657 * feed it back to accelerator processing at this point...
659 if (status == XLookupChars || status == XLookupBoth)
663 buffer[num_bytes] = '\0';
665 result_utf8 = mb_to_utf8 (context_xim, buffer);
668 if ((guchar)result_utf8[0] >= 0x20 &&
669 result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
670 * control characters into strings
673 g_signal_emit_by_name (context, "commit", result_utf8);
677 g_free (result_utf8);
681 if (buffer != static_buffer)
688 gtk_im_context_xim_focus_in (GtkIMContext *context)
690 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
691 XIC ic = gtk_im_context_xim_get_ic (context_xim);
698 status_window_show (context_xim);
704 gtk_im_context_xim_focus_out (GtkIMContext *context)
706 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
707 XIC ic = gtk_im_context_xim_get_ic (context_xim);
714 status_window_hide (context_xim);
720 gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
723 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
724 XIC ic = gtk_im_context_xim_get_ic (context_xim);
726 XVaNestedList preedit_attr;
735 preedit_attr = XVaCreateNestedList (0,
736 XNSpotLocation, &spot,
739 XNPreeditAttributes, preedit_attr,
747 gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
748 gboolean use_preedit)
750 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
752 use_preedit = use_preedit != FALSE;
754 if (context_xim->use_preedit != use_preedit)
756 context_xim->use_preedit = use_preedit;
757 reinitialize_ic (context_xim);
764 gtk_im_context_xim_reset (GtkIMContext *context)
766 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
767 XIC ic = gtk_im_context_xim_get_ic (context_xim);
770 /* restore conversion state after resetting ic later */
771 XIMPreeditState preedit_state = XIMPreeditUnKnown;
772 XVaNestedList preedit_attr;
773 gboolean have_preedit_state = FALSE;
779 if (context_xim->preedit_length == 0)
782 preedit_attr = XVaCreateNestedList(0,
783 XNPreeditState, &preedit_state,
785 if (!XGetICValues(ic,
786 XNPreeditAttributes, preedit_attr,
788 have_preedit_state = TRUE;
792 result = XmbResetIC (ic);
794 preedit_attr = XVaCreateNestedList(0,
795 XNPreeditState, preedit_state,
797 if (have_preedit_state)
799 XNPreeditAttributes, preedit_attr,
806 char *result_utf8 = mb_to_utf8 (context_xim, result);
809 g_signal_emit_by_name (context, "commit", result_utf8);
810 g_free (result_utf8);
814 if (context_xim->preedit_length)
816 context_xim->preedit_length = 0;
817 g_signal_emit_by_name (context, "preedit_changed");
823 /* Mask of feedback bits that we render
825 #define FEEDBACK_MASK (XIMReverse | XIMUnderline)
828 add_feedback_attr (PangoAttrList *attrs,
830 XIMFeedback feedback,
834 PangoAttribute *attr;
836 gint start_index = g_utf8_offset_to_pointer (str, start_pos) - str;
837 gint end_index = g_utf8_offset_to_pointer (str, end_pos) - str;
839 if (feedback & XIMUnderline)
841 attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
842 attr->start_index = start_index;
843 attr->end_index = end_index;
845 pango_attr_list_change (attrs, attr);
848 if (feedback & XIMReverse)
850 attr = pango_attr_foreground_new (0xffff, 0xffff, 0xffff);
851 attr->start_index = start_index;
852 attr->end_index = end_index;
854 pango_attr_list_change (attrs, attr);
856 attr = pango_attr_background_new (0, 0, 0);
857 attr->start_index = start_index;
858 attr->end_index = end_index;
860 pango_attr_list_change (attrs, attr);
863 if (feedback & ~FEEDBACK_MASK)
864 g_warning ("Unrendered feedback style: %#lx", feedback & ~FEEDBACK_MASK);
868 gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
870 PangoAttrList **attrs,
873 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
874 gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
879 XIMFeedback last_feedback = 0;
882 *attrs = pango_attr_list_new ();
884 for (i = 0; i < context_xim->preedit_length; i++)
886 XIMFeedback new_feedback = context_xim->feedbacks[i] & FEEDBACK_MASK;
887 if (new_feedback != last_feedback)
890 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
892 last_feedback = new_feedback;
898 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
907 *cursor_pos = context_xim->preedit_cursor;
911 preedit_start_callback (XIC xic,
912 XPointer client_data,
915 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
916 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
918 if (!context_xim->finalizing)
919 g_signal_emit_by_name (context, "preedit_start");
923 preedit_done_callback (XIC xic,
924 XPointer client_data,
927 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
928 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
930 if (!context_xim->finalizing)
931 g_signal_emit_by_name (context, "preedit_end");
935 xim_text_to_utf8 (GtkIMContextXIM *context, XIMText *xim_text, gchar **text)
937 gint text_length = 0;
938 GError *error = NULL;
939 gchar *result = NULL;
941 if (xim_text && xim_text->string.multi_byte)
943 if (xim_text->encoding_is_wchar)
945 g_warning ("Wide character return from Xlib not currently supported");
950 if (strcmp (context->mb_charset, "UTF-8") == 0)
951 result = g_strdup (xim_text->string.multi_byte);
953 result = g_convert (xim_text->string.multi_byte,
961 text_length = g_utf8_strlen (result, -1);
963 if (text_length != xim_text->length)
965 g_warning ("Size mismatch when converting text from input method: supplied length = %d\n, result length = %d", xim_text->length, text_length);
970 g_warning ("Error converting text from IM to UCS-4: %s", error->message);
971 g_error_free (error);
988 preedit_draw_callback (XIC xic,
989 XPointer client_data,
990 XIMPreeditDrawCallbackStruct *call_data)
992 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
994 XIMText *new_xim_text = call_data->text;
995 gint new_text_length;
996 gunichar *new_text = NULL;
1002 gint chg_first = CLAMP (call_data->chg_first, 0, context->preedit_length);
1003 gint chg_length = CLAMP (call_data->chg_length, 0, context->preedit_length - chg_first);
1005 context->preedit_cursor = call_data->caret;
1007 if (chg_first != call_data->chg_first || chg_length != call_data->chg_length)
1008 g_warning ("Invalid change to preedit string, first=%d length=%d (orig length == %d)",
1009 call_data->chg_first, call_data->chg_length, context->preedit_length);
1011 new_text_length = xim_text_to_utf8 (context, new_xim_text, &tmp);
1014 new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
1018 diff = new_text_length - chg_length;
1019 new_length = context->preedit_length + diff;
1021 if (new_length > context->preedit_size)
1023 context->preedit_size = new_length;
1024 context->preedit_chars = g_renew (gunichar, context->preedit_chars, new_length);
1025 context->feedbacks = g_renew (XIMFeedback, context->feedbacks, new_length);
1030 for (i = chg_first + chg_length ; i < context->preedit_length; i++)
1032 context->preedit_chars[i + diff] = context->preedit_chars[i];
1033 context->feedbacks[i + diff] = context->feedbacks[i];
1038 for (i = context->preedit_length - 1; i >= chg_first + chg_length ; i--)
1040 context->preedit_chars[i + diff] = context->preedit_chars[i];
1041 context->feedbacks[i + diff] = context->feedbacks[i];
1045 for (i = 0; i < new_text_length; i++)
1047 context->preedit_chars[chg_first + i] = new_text[i];
1048 context->feedbacks[chg_first + i] = new_xim_text->feedback[i];
1051 context->preedit_length += diff;
1056 if (!context->finalizing)
1057 g_signal_emit_by_name (context, "preedit_changed");
1062 preedit_caret_callback (XIC xic,
1063 XPointer client_data,
1064 XIMPreeditCaretCallbackStruct *call_data)
1066 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1068 if (call_data->direction == XIMAbsolutePosition)
1070 context->preedit_cursor = call_data->position;
1071 if (!context->finalizing)
1072 g_signal_emit_by_name (context, "preedit_changed");
1076 g_warning ("Caret movement command: %d %d %d not supported",
1077 call_data->position, call_data->direction, call_data->style);
1082 status_start_callback (XIC xic,
1083 XPointer client_data,
1090 status_done_callback (XIC xic,
1091 XPointer client_data,
1098 status_draw_callback (XIC xic,
1099 XPointer client_data,
1100 XIMStatusDrawCallbackStruct *call_data)
1102 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1104 if (!context->status_visible)
1107 if (call_data->type == XIMTextType)
1110 xim_text_to_utf8 (context, call_data->data.text, &text);
1113 status_window_set_text (context, text);
1115 status_window_set_text (context, "");
1119 g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
1123 static XVaNestedList
1124 set_preedit_callback (GtkIMContextXIM *context_xim)
1126 context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
1127 context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
1128 context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
1129 context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
1130 context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
1131 context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
1132 context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
1133 context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
1134 return XVaCreateNestedList (0,
1135 XNPreeditStartCallback, &context_xim->preedit_start_callback,
1136 XNPreeditDoneCallback, &context_xim->preedit_done_callback,
1137 XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
1138 XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
1142 static XVaNestedList
1143 set_status_callback (GtkIMContextXIM *context_xim)
1145 context_xim->status_start_callback.client_data = (XPointer)context_xim;
1146 context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
1147 context_xim->status_done_callback.client_data = (XPointer)context_xim;
1148 context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
1149 context_xim->status_draw_callback.client_data = (XPointer)context_xim;
1150 context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
1152 return XVaCreateNestedList (0,
1153 XNStatusStartCallback, &context_xim->status_start_callback,
1154 XNStatusDoneCallback, &context_xim->status_done_callback,
1155 XNStatusDrawCallback, &context_xim->status_draw_callback,
1161 gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
1163 if (context_xim->im_info == NULL || context_xim->im_info->im == NULL)
1166 if (!context_xim->ic)
1168 const char *name1 = NULL;
1169 XVaNestedList list1 = NULL;
1170 const char *name2 = NULL;
1171 XVaNestedList list2 = NULL;
1172 XIMStyle im_style = 0;
1175 if (context_xim->use_preedit &&
1176 (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
1178 im_style |= XIMPreeditCallbacks;
1179 name1 = XNPreeditAttributes;
1180 list1 = set_preedit_callback (context_xim);
1182 else if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditNone)
1183 im_style |= XIMPreeditNone;
1185 im_style |= XIMPreeditNothing;
1187 if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
1189 im_style |= XIMStatusCallbacks;
1192 name1 = XNStatusAttributes;
1193 list1 = set_status_callback (context_xim);
1197 name2 = XNStatusAttributes;
1198 list2 = set_status_callback (context_xim);
1201 else if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusNone)
1202 im_style |= XIMStatusNone;
1204 im_style |= XIMStatusNothing;
1206 xic = XCreateIC (context_xim->im_info->im,
1207 XNInputStyle, im_style,
1208 XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
1219 /* Don't filter key released events with XFilterEvents unless
1220 * input methods ask for. This is a workaround for Solaris input
1221 * method bug in C and European locales. It doubles each key
1222 * stroke if both key pressed and released events are filtered.
1227 XNFilterEvents, &mask,
1229 context_xim->filter_key_release = (mask & KeyReleaseMask);
1231 context_xim->ic = xic;
1233 return context_xim->ic;
1236 /**************************
1238 * Status Window handling *
1240 **************************/
1243 status_window_expose_event (GtkWidget *widget,
1244 GdkEventExpose *event)
1246 gdk_draw_rectangle (widget->window,
1247 widget->style->base_gc [GTK_STATE_NORMAL],
1250 widget->allocation.width, widget->allocation.height);
1251 gdk_draw_rectangle (widget->window,
1252 widget->style->text_gc [GTK_STATE_NORMAL],
1255 widget->allocation.width - 1, widget->allocation.height - 1);
1261 status_window_style_set (GtkWidget *toplevel,
1262 GtkStyle *previous_style,
1267 for (i = 0; i < 5; i++)
1268 gtk_widget_modify_fg (label, i, &toplevel->style->text[i]);
1271 /* Frees a status window and removes its link from the status_windows list */
1273 status_window_free (StatusWindow *status_window)
1275 status_windows = g_slist_remove (status_windows, status_window);
1277 g_signal_handler_disconnect (status_window->toplevel, status_window->destroy_handler_id);
1278 g_signal_handler_disconnect (status_window->toplevel, status_window->configure_handler_id);
1279 gtk_widget_destroy (status_window->window);
1280 g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
1282 g_free (status_window);
1286 status_window_configure (GtkWidget *toplevel,
1287 GdkEventConfigure *event,
1288 StatusWindow *status_window)
1291 GtkRequisition requisition;
1293 gint height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
1295 gdk_window_get_frame_extents (toplevel->window, &rect);
1296 gtk_widget_size_request (status_window->window, &requisition);
1298 if (rect.y + rect.height + requisition.height < height)
1299 y = rect.y + rect.height;
1301 y = height - requisition.height;
1303 gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
1309 status_window_get (GtkIMContextXIM *context_xim,
1312 GdkWindow *toplevel_gdk;
1313 GtkWidget *toplevel;
1315 StatusWindow *status_window;
1316 GtkWidget *status_label;
1318 GdkWindow *root_window;
1320 if (!context_xim->client_window)
1323 toplevel_gdk = context_xim->client_window;
1324 screen = gdk_drawable_get_screen (toplevel_gdk);
1325 root_window = gdk_screen_get_root_window (screen);
1329 GdkWindow *parent = gdk_window_get_parent (toplevel_gdk);
1330 if (parent == root_window)
1333 toplevel_gdk = parent;
1336 gdk_window_get_user_data (toplevel_gdk, (gpointer *)&toplevel);
1340 status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
1342 return status_window->window;
1346 status_window = g_new (StatusWindow, 1);
1347 status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
1348 status_window->toplevel = toplevel;
1350 status_windows = g_slist_prepend (status_windows, status_window);
1352 window = status_window->window;
1354 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
1355 gtk_widget_set_app_paintable (window, TRUE);
1357 status_label = gtk_label_new ("");
1358 gtk_misc_set_padding (GTK_MISC (status_label), 1, 1);
1359 gtk_widget_show (status_label);
1361 gtk_container_add (GTK_CONTAINER (window), status_label);
1363 status_window->destroy_handler_id = g_signal_connect_swapped (toplevel, "destroy",
1364 G_CALLBACK (status_window_free),
1366 status_window->configure_handler_id = g_signal_connect (toplevel, "configure_event",
1367 G_CALLBACK (status_window_configure),
1370 status_window_configure (toplevel, NULL, status_window);
1372 g_signal_connect (window, "style_set",
1373 G_CALLBACK (status_window_style_set), status_label);
1374 g_signal_connect (window, "expose_event",
1375 G_CALLBACK (status_window_expose_event), NULL);
1377 g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);
1383 status_window_has_text (GtkWidget *status_window)
1385 GtkWidget *label = GTK_BIN (status_window)->child;
1386 const gchar *text = gtk_label_get_text (GTK_LABEL (label));
1388 return text[0] != '\0';
1392 status_window_show (GtkIMContextXIM *context_xim)
1394 context_xim->status_visible = TRUE;
1398 status_window_hide (GtkIMContextXIM *context_xim)
1400 GtkWidget *status_window = status_window_get (context_xim, FALSE);
1402 context_xim->status_visible = FALSE;
1405 status_window_set_text (context_xim, "");
1409 status_window_set_text (GtkIMContextXIM *context_xim,
1412 GtkWidget *status_window = status_window_get (context_xim, TRUE);
1416 GtkWidget *label = GTK_BIN (status_window)->child;
1417 gtk_label_set_text (GTK_LABEL (label), text);
1419 if (context_xim->status_visible && status_window_has_text (status_window))
1420 gtk_widget_show (status_window);
1422 gtk_widget_hide (status_window);
1427 * gtk_im_context_xim_shutdown:
1429 * Destroys all the status windows that are kept by the XIM contexts. This
1430 * function should only be called by the XIM module exit routine.
1433 gtk_im_context_xim_shutdown (void)
1435 while (status_windows)
1436 status_window_free (status_windows->data);