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;
46 /* A context status window; these are kept in the status_windows list. */
51 /* Toplevel window to which the status window corresponds */
54 /* Signal connection ids; we connect to the toplevel */
55 gulong destroy_handler_id;
56 gulong configure_handler_id;
59 static void gtk_im_context_xim_class_init (GtkIMContextXIMClass *class);
60 static void gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim);
61 static void gtk_im_context_xim_finalize (GObject *obj);
62 static void gtk_im_context_xim_set_client_window (GtkIMContext *context,
63 GdkWindow *client_window);
64 static gboolean gtk_im_context_xim_filter_keypress (GtkIMContext *context,
66 static void gtk_im_context_xim_reset (GtkIMContext *context);
67 static void gtk_im_context_xim_focus_in (GtkIMContext *context);
68 static void gtk_im_context_xim_focus_out (GtkIMContext *context);
69 static void gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
71 static void gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
72 gboolean use_preedit);
73 static void gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
75 PangoAttrList **attrs,
78 static void reinitialize_ic (GtkIMContextXIM *context_xim,
79 gboolean send_signal);
80 static void set_ic_client_window (GtkIMContextXIM *context_xim,
81 GdkWindow *client_window,
82 gboolean send_signal);
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 XIC gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim);
92 static GObjectClass *parent_class;
94 GType gtk_type_im_context_xim = 0;
96 GSList *open_ims = NULL;
98 /* List of status windows for different toplevels */
99 static GSList *status_windows = NULL;
102 gtk_im_context_xim_register_type (GTypeModule *type_module)
104 static const GTypeInfo im_context_xim_info =
106 sizeof (GtkIMContextXIMClass),
107 (GBaseInitFunc) NULL,
108 (GBaseFinalizeFunc) NULL,
109 (GClassInitFunc) gtk_im_context_xim_class_init,
110 NULL, /* class_finalize */
111 NULL, /* class_data */
112 sizeof (GtkIMContextXIM),
114 (GInstanceInitFunc) gtk_im_context_xim_init,
117 gtk_type_im_context_xim =
118 g_type_module_register_type (type_module,
121 &im_context_xim_info, 0);
124 #define PREEDIT_MASK (XIMPreeditCallbacks | XIMPreeditPosition | \
125 XIMPreeditArea | XIMPreeditNothing | XIMPreeditNone)
126 #define STATUS_MASK (XIMStatusCallbacks | XIMStatusArea | \
127 XIMStatusNothing | XIMStatusNone)
128 #define ALLOWED_MASK (XIMPreeditCallbacks | XIMPreeditNothing | XIMPreeditNone | \
129 XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone)
132 choose_better_style (XIMStyle style1, XIMStyle style2)
136 if (style1 == 0) return style2;
137 if (style2 == 0) return style1;
138 if ((style1 & (PREEDIT_MASK | STATUS_MASK))
139 == (style2 & (PREEDIT_MASK | STATUS_MASK)))
142 s1 = style1 & PREEDIT_MASK;
143 s2 = style2 & PREEDIT_MASK;
146 if (u & XIMPreeditCallbacks)
147 return (s1 == XIMPreeditCallbacks) ? style1 : style2;
148 else if (u & XIMPreeditPosition)
149 return (s1 == XIMPreeditPosition) ? style1 :style2;
150 else if (u & XIMPreeditArea)
151 return (s1 == XIMPreeditArea) ? style1 : style2;
152 else if (u & XIMPreeditNothing)
153 return (s1 == XIMPreeditNothing) ? style1 : style2;
155 s1 = style1 & STATUS_MASK;
156 s2 = style2 & STATUS_MASK;
158 if (u & XIMStatusCallbacks)
159 return (s1 == XIMStatusCallbacks) ? style1 : style2;
160 else if (u & XIMStatusArea)
161 return (s1 == XIMStatusArea) ? style1 : style2;
162 else if (u & XIMStatusNothing)
163 return (s1 == XIMStatusNothing) ? style1 : style2;
164 else if (u & XIMStatusNone)
165 return (s1 == XIMStatusNone) ? style1 : style2;
167 return 0; /* Get rid of stupid warning */
171 reinitialize_all_ics (GtkXIMInfo *info)
175 for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
176 reinitialize_ic (tmp_list->data, TRUE);
180 status_style_change (GtkXIMInfo *info)
182 GtkIMStatusStyle status_style;
184 g_object_get (info->settings,
185 "gtk-im-status-style", &status_style,
187 if (status_style == GTK_IM_STATUS_CALLBACK)
188 info->status_style_setting = XIMStatusCallbacks;
189 else if (status_style == GTK_IM_STATUS_NOTHING)
190 info->status_style_setting = XIMStatusNothing;
196 reinitialize_all_ics (info);
200 preedit_style_change (GtkXIMInfo *info)
202 GtkIMPreeditStyle preedit_style;
203 g_object_get (info->settings,
204 "gtk-im-preedit-style", &preedit_style,
206 if (preedit_style == GTK_IM_PREEDIT_CALLBACK)
207 info->preedit_style_setting = XIMPreeditCallbacks;
208 else if (preedit_style == GTK_IM_PREEDIT_NOTHING)
209 info->preedit_style_setting = XIMPreeditNothing;
215 reinitialize_all_ics (info);
219 setup_styles (GtkXIMInfo *info)
222 unsigned long settings_preference;
223 XIMStyles *xim_styles = info->xim_styles;
225 settings_preference = info->status_style_setting|info->preedit_style_setting;
228 for (i = 0; i < xim_styles->count_styles; i++)
229 if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
231 if (settings_preference == xim_styles->supported_styles[i])
233 info->style = settings_preference;
236 info->style = choose_better_style (info->style,
237 xim_styles->supported_styles[i]);
243 setup_im (GtkXIMInfo *info)
245 XIMValuesList *ic_values = NULL;
247 if (info->im == NULL)
250 XGetIMValues (info->im,
251 XNQueryInputStyle, &info->xim_styles,
252 XNQueryICValuesList, &ic_values,
255 info->settings = gtk_settings_get_for_screen (info->screen);
257 if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
258 "gtk-im-preedit-style"))
259 gtk_settings_install_property (g_param_spec_enum ("gtk-im-preedit-style",
260 _("IM Preedit style"),
261 _("How to draw the input method preedit string"),
262 GTK_TYPE_IM_PREEDIT_STYLE,
263 GTK_IM_PREEDIT_CALLBACK,
266 if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
267 "gtk-im-status-style"))
268 gtk_settings_install_property (g_param_spec_enum ("gtk-im-status-style",
269 _("IM Status style"),
270 _("How to draw the input method statusbar"),
271 GTK_TYPE_IM_STATUS_STYLE,
272 GTK_IM_STATUS_CALLBACK,
275 info->status_set = g_signal_connect_swapped (info->settings,
276 "notify::gtk-im-status-style",
277 G_CALLBACK (status_style_change),
279 info->preedit_set = g_signal_connect_swapped (info->settings,
280 "notify::gtk-im-preedit-style",
281 G_CALLBACK (preedit_style_change),
284 status_style_change (info);
285 preedit_style_change (info);
290 for (i = 0; i < ic_values->count_values; i++)
291 g_print ("%s\n", ic_values->supported_values[i]);
292 for (i = 0; i < xim_styles->count_styles; i++)
293 g_print ("%#x\n", xim_styles->supported_styles[i]);
302 xim_info_display_closed (GdkDisplay *display,
306 GSList *ics, *tmp_list;
308 open_ims = g_slist_remove (open_ims, info);
313 for (tmp_list = ics; tmp_list; tmp_list = tmp_list->next)
314 set_ic_client_window (tmp_list->data, NULL, TRUE);
316 g_slist_free (tmp_list);
318 g_signal_handler_disconnect (info->settings, info->status_set);
319 g_signal_handler_disconnect (info->settings, info->preedit_set);
321 XFree (info->xim_styles->supported_styles);
322 XFree (info->xim_styles);
323 g_free (info->locale);
332 get_im (GdkWindow *client_window,
338 GdkScreen *screen = gdk_drawable_get_screen (client_window);
339 GdkDisplay *display = gdk_screen_get_display (screen);
344 info = tmp_list->data;
345 if (info->screen == screen &&
346 strcmp (info->locale, locale) == 0)
349 tmp_list = tmp_list->next;
354 if (XSupportsLocale ())
356 if (!XSetLocaleModifiers (""))
357 g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
359 im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
362 g_warning ("Unable to open XIM input method, falling back to XLookupString()");
364 info = g_new (GtkXIMInfo, 1);
365 open_ims = g_slist_prepend (open_ims, info);
367 info->screen = screen;
368 info->locale = g_strdup (locale);
370 info->xim_styles = NULL;
371 info->preedit_style_setting = 0;
372 info->status_style_setting = 0;
373 info->settings = NULL;
374 info->preedit_set = 0;
375 info->status_set = 0;
380 g_signal_connect (display, "closed",
381 G_CALLBACK (xim_info_display_closed), info);
388 gtk_im_context_xim_class_init (GtkIMContextXIMClass *class)
390 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
391 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
393 parent_class = g_type_class_peek_parent (class);
395 im_context_class->set_client_window = gtk_im_context_xim_set_client_window;
396 im_context_class->filter_keypress = gtk_im_context_xim_filter_keypress;
397 im_context_class->reset = gtk_im_context_xim_reset;
398 im_context_class->get_preedit_string = gtk_im_context_xim_get_preedit_string;
399 im_context_class->focus_in = gtk_im_context_xim_focus_in;
400 im_context_class->focus_out = gtk_im_context_xim_focus_out;
401 im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
402 im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
403 gobject_class->finalize = gtk_im_context_xim_finalize;
407 gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
409 im_context_xim->use_preedit = TRUE;
410 im_context_xim->filter_key_release = FALSE;
411 im_context_xim->status_visible = FALSE;
415 gtk_im_context_xim_finalize (GObject *obj)
417 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);
419 set_ic_client_window (context_xim, NULL, FALSE);
421 g_free (context_xim->locale);
422 g_free (context_xim->mb_charset);
426 reinitialize_ic (GtkIMContextXIM *context_xim,
427 gboolean send_signal)
431 XDestroyIC (context_xim->ic);
432 status_window_hide (context_xim);
433 context_xim->ic = NULL;
435 if (context_xim->preedit_length)
437 context_xim->preedit_length = 0;
439 g_signal_emit_by_name (context_xim, "preedit_changed");
445 set_ic_client_window (GtkIMContextXIM *context_xim,
446 GdkWindow *client_window,
447 gboolean send_signal)
449 reinitialize_ic (context_xim, send_signal);
450 if (context_xim->client_window)
452 context_xim->im_info->ics = g_slist_remove (context_xim->im_info->ics, context_xim);
453 context_xim->im_info = NULL;
456 context_xim->client_window = client_window;
458 if (context_xim->client_window)
460 context_xim->im_info = get_im (context_xim->client_window, context_xim->locale);
461 context_xim->im_info->ics = g_slist_prepend (context_xim->im_info->ics, context_xim);
466 gtk_im_context_xim_set_client_window (GtkIMContext *context,
467 GdkWindow *client_window)
469 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
471 set_ic_client_window (context_xim, client_window, TRUE);
475 gtk_im_context_xim_new (void)
477 GtkIMContextXIM *result;
478 const gchar *charset;
480 result = GTK_IM_CONTEXT_XIM (g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL));
482 result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
484 g_get_charset (&charset);
485 result->mb_charset = g_strdup (charset);
487 return GTK_IM_CONTEXT (result);
491 mb_to_utf8 (GtkIMContextXIM *context_xim,
494 GError *error = NULL;
497 if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
498 result = g_strdup (str);
501 result = g_convert (str, -1,
502 "UTF-8", context_xim->mb_charset,
506 g_warning ("Error converting text from IM to UTF-8: %s\n", error->message);
507 g_error_free (error);
515 gtk_im_context_xim_filter_keypress (GtkIMContext *context,
518 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
519 XIC ic = gtk_im_context_xim_get_ic (context_xim);
520 gchar static_buffer[256];
521 gchar *buffer = static_buffer;
522 gint buffer_size = sizeof(static_buffer) - 1;
526 gboolean result = FALSE;
527 GdkWindow *root_window = gdk_screen_get_root_window (gdk_drawable_get_screen (event->window));
529 XKeyPressedEvent xevent;
531 if (event->type == GDK_KEY_RELEASE && !context_xim->filter_key_release)
534 xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
535 xevent.serial = 0; /* hope it doesn't matter */
536 xevent.send_event = event->send_event;
537 xevent.display = GDK_DRAWABLE_XDISPLAY (event->window);
538 xevent.window = GDK_DRAWABLE_XID (event->window);
539 xevent.root = GDK_DRAWABLE_XID (root_window);
540 xevent.subwindow = xevent.window;
541 xevent.time = event->time;
542 xevent.x = xevent.x_root = 0;
543 xevent.y = xevent.y_root = 0;
544 xevent.state = event->state;
545 xevent.keycode = event->hardware_keycode;
546 xevent.same_screen = True;
548 if (XFilterEvent ((XEvent *)&xevent, GDK_DRAWABLE_XID (context_xim->client_window)))
553 num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
556 num_bytes = XLookupString (&xevent, buffer, buffer_size, &keysym, NULL);
557 status = XLookupBoth;
560 if (status == XBufferOverflow)
562 buffer_size = num_bytes;
563 buffer = g_malloc (num_bytes + 1);
567 /* I don't know how we should properly handle XLookupKeysym or XLookupBoth
568 * here ... do input methods actually change the keysym? we can't really
569 * feed it back to accelerator processing at this point...
571 if (status == XLookupChars || status == XLookupBoth)
575 buffer[num_bytes] = '\0';
577 result_utf8 = mb_to_utf8 (context_xim, buffer);
580 if ((guchar)result_utf8[0] >= 0x20 &&
581 result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
582 * control characters into strings
585 g_signal_emit_by_name (context, "commit", result_utf8);
589 g_free (result_utf8);
597 gtk_im_context_xim_focus_in (GtkIMContext *context)
599 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
600 XIC ic = gtk_im_context_xim_get_ic (context_xim);
607 status_window_show (context_xim);
613 gtk_im_context_xim_focus_out (GtkIMContext *context)
615 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
616 XIC ic = gtk_im_context_xim_get_ic (context_xim);
623 status_window_hide (context_xim);
629 gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
632 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
633 XIC ic = gtk_im_context_xim_get_ic (context_xim);
635 XVaNestedList preedit_attr;
644 preedit_attr = XVaCreateNestedList (0,
645 XNSpotLocation, &spot,
648 XNPreeditAttributes, preedit_attr,
656 gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
657 gboolean use_preedit)
659 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
661 use_preedit = use_preedit != FALSE;
663 if (context_xim->use_preedit != use_preedit)
665 context_xim->use_preedit = use_preedit;
666 reinitialize_ic (context_xim, TRUE);
673 gtk_im_context_xim_reset (GtkIMContext *context)
675 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
676 XIC ic = gtk_im_context_xim_get_ic (context_xim);
679 /* restore conversion state after resetting ic later */
680 XIMPreeditState preedit_state = XIMPreeditUnKnown;
681 XVaNestedList preedit_attr;
682 gboolean have_preedit_state = FALSE;
688 if (context_xim->preedit_length == 0)
691 preedit_attr = XVaCreateNestedList(0,
692 XNPreeditState, &preedit_state,
694 if (!XGetICValues(ic,
695 XNPreeditAttributes, preedit_attr,
697 have_preedit_state = TRUE;
701 result = XmbResetIC (ic);
703 preedit_attr = XVaCreateNestedList(0,
704 XNPreeditState, preedit_state,
706 if (have_preedit_state)
708 XNPreeditAttributes, preedit_attr,
715 char *result_utf8 = mb_to_utf8 (context_xim, result);
718 g_signal_emit_by_name (context, "commit", result_utf8);
719 g_free (result_utf8);
723 if (context_xim->preedit_length)
725 context_xim->preedit_length = 0;
726 g_signal_emit_by_name (context, "preedit_changed");
732 /* Mask of feedback bits that we render
734 #define FEEDBACK_MASK (XIMReverse | XIMUnderline)
737 add_feedback_attr (PangoAttrList *attrs,
739 XIMFeedback feedback,
743 PangoAttribute *attr;
745 gint start_index = g_utf8_offset_to_pointer (str, start_pos) - str;
746 gint end_index = g_utf8_offset_to_pointer (str, end_pos) - str;
748 if (feedback & XIMUnderline)
750 attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
751 attr->start_index = start_index;
752 attr->end_index = end_index;
754 pango_attr_list_change (attrs, attr);
757 if (feedback & XIMReverse)
759 attr = pango_attr_foreground_new (0xffff, 0xffff, 0xffff);
760 attr->start_index = start_index;
761 attr->end_index = end_index;
763 pango_attr_list_change (attrs, attr);
765 attr = pango_attr_background_new (0, 0, 0);
766 attr->start_index = start_index;
767 attr->end_index = end_index;
769 pango_attr_list_change (attrs, attr);
772 if (feedback & ~FEEDBACK_MASK)
773 g_warning ("Unrendered feedback style: %#lx", feedback & ~FEEDBACK_MASK);
777 gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
779 PangoAttrList **attrs,
782 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
783 gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
788 XIMFeedback last_feedback = 0;
791 *attrs = pango_attr_list_new ();
793 for (i = 0; i < context_xim->preedit_length; i++)
795 XIMFeedback new_feedback = context_xim->feedbacks[i] & FEEDBACK_MASK;
796 if (new_feedback != last_feedback)
799 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
801 last_feedback = new_feedback;
807 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
816 *cursor_pos = context_xim->preedit_cursor;
820 preedit_start_callback (XIC xic,
821 XPointer client_data,
824 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
826 g_signal_emit_by_name (context, "preedit_start");
830 preedit_done_callback (XIC xic,
831 XPointer client_data,
834 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
836 g_signal_emit_by_name (context, "preedit_end");
840 xim_text_to_utf8 (GtkIMContextXIM *context, XIMText *xim_text, gchar **text)
842 gint text_length = 0;
843 GError *error = NULL;
844 gchar *result = NULL;
846 if (xim_text && xim_text->string.multi_byte)
848 if (xim_text->encoding_is_wchar)
850 g_warning ("Wide character return from Xlib not currently supported");
855 if (strcmp (context->mb_charset, "UTF-8") == 0)
856 result = g_strdup (xim_text->string.multi_byte);
858 result = g_convert (xim_text->string.multi_byte,
866 text_length = g_utf8_strlen (result, -1);
868 if (text_length != xim_text->length)
870 g_warning ("Size mismatch when converting text from input method: supplied length = %d\n, result length = %d", xim_text->length, text_length);
875 g_warning ("Error converting text from IM to UCS-4: %s", error->message);
876 g_error_free (error);
893 preedit_draw_callback (XIC xic,
894 XPointer client_data,
895 XIMPreeditDrawCallbackStruct *call_data)
897 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
899 XIMText *new_xim_text = call_data->text;
900 gint new_text_length;
901 gunichar *new_text = NULL;
907 gint chg_first = CLAMP (call_data->chg_first, 0, context->preedit_length);
908 gint chg_length = CLAMP (call_data->chg_length, 0, context->preedit_length - chg_first);
910 context->preedit_cursor = call_data->caret;
912 if (chg_first != call_data->chg_first || chg_length != call_data->chg_length)
913 g_warning ("Invalid change to preedit string, first=%d length=%d (orig length == %d)",
914 call_data->chg_first, call_data->chg_length, context->preedit_length);
916 new_text_length = xim_text_to_utf8 (context, new_xim_text, &tmp);
919 new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
923 diff = new_text_length - chg_length;
924 new_length = context->preedit_length + diff;
926 if (new_length > context->preedit_size)
928 context->preedit_size = new_length;
929 context->preedit_chars = g_renew (gunichar, context->preedit_chars, new_length);
930 context->feedbacks = g_renew (XIMFeedback, context->feedbacks, new_length);
935 for (i = chg_first + chg_length ; i < context->preedit_length; i++)
937 context->preedit_chars[i + diff] = context->preedit_chars[i];
938 context->feedbacks[i + diff] = context->feedbacks[i];
943 for (i = context->preedit_length - 1; i >= chg_first + chg_length ; i--)
945 context->preedit_chars[i + diff] = context->preedit_chars[i];
946 context->feedbacks[i + diff] = context->feedbacks[i];
950 for (i = 0; i < new_text_length; i++)
952 context->preedit_chars[chg_first + i] = new_text[i];
953 context->feedbacks[chg_first + i] = new_xim_text->feedback[i];
956 context->preedit_length += diff;
961 g_signal_emit_by_name (context, "preedit_changed");
966 preedit_caret_callback (XIC xic,
967 XPointer client_data,
968 XIMPreeditCaretCallbackStruct *call_data)
970 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
972 if (call_data->direction == XIMAbsolutePosition)
974 context->preedit_cursor = call_data->position;
975 g_signal_emit_by_name (context, "preedit_changed");
979 g_warning ("Caret movement command: %d %d %d not supported",
980 call_data->position, call_data->direction, call_data->style);
985 status_start_callback (XIC xic,
986 XPointer client_data,
993 status_done_callback (XIC xic,
994 XPointer client_data,
1001 status_draw_callback (XIC xic,
1002 XPointer client_data,
1003 XIMStatusDrawCallbackStruct *call_data)
1005 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1007 if (context->status_visible == FALSE)
1010 if (call_data->type == XIMTextType)
1013 xim_text_to_utf8 (context, call_data->data.text, &text);
1016 status_window_set_text (context, text);
1018 status_window_set_text (context, "");
1022 g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
1026 static XVaNestedList
1027 set_preedit_callback (GtkIMContextXIM *context_xim)
1029 context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
1030 context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
1031 context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
1032 context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
1033 context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
1034 context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
1035 context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
1036 context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
1037 return XVaCreateNestedList (0,
1038 XNPreeditStartCallback, &context_xim->preedit_start_callback,
1039 XNPreeditDoneCallback, &context_xim->preedit_done_callback,
1040 XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
1041 XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
1045 static XVaNestedList
1046 set_status_callback (GtkIMContextXIM *context_xim)
1048 context_xim->status_start_callback.client_data = (XPointer)context_xim;
1049 context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
1050 context_xim->status_done_callback.client_data = (XPointer)context_xim;
1051 context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
1052 context_xim->status_draw_callback.client_data = (XPointer)context_xim;
1053 context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
1055 return XVaCreateNestedList (0,
1056 XNStatusStartCallback, &context_xim->status_start_callback,
1057 XNStatusDoneCallback, &context_xim->status_done_callback,
1058 XNStatusDrawCallback, &context_xim->status_draw_callback,
1063 get_ic_real (GtkIMContextXIM *context_xim)
1066 const char *name1 = NULL;
1067 XVaNestedList list1 = NULL;
1068 const char *name2 = NULL;
1069 XVaNestedList list2 = NULL;
1070 XIMStyle im_style = 0;
1072 if (context_xim->im_info->im == NULL)
1075 if (context_xim->use_preedit &&
1076 (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
1078 im_style |= XIMPreeditCallbacks;
1079 name1 = XNPreeditAttributes;
1080 list1 = set_preedit_callback (context_xim);
1083 im_style |= XIMPreeditNothing;
1085 if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
1087 im_style |= XIMStatusCallbacks;
1090 name1 = XNStatusAttributes;
1091 list1 = set_status_callback (context_xim);
1095 name2 = XNStatusAttributes;
1096 list2 = set_status_callback (context_xim);
1100 im_style |= XIMStatusNothing;
1102 xic = XCreateIC (context_xim->im_info->im,
1103 XNInputStyle, im_style,
1104 XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
1115 /* Don't filter key released events with XFilterEvents unless
1116 * input methods ask for. This is a workaround for Solaris input
1117 * method bug in C and European locales. It doubles each key
1118 * stroke if both key pressed and released events are filtered.
1123 XNFilterEvents, &mask,
1125 context_xim->filter_key_release = (mask & KeyReleaseMask);
1132 gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
1134 if (!context_xim->ic && context_xim->im_info)
1135 context_xim->ic = get_ic_real (context_xim);
1137 return context_xim->ic;
1140 /**************************
1142 * Status Window handling *
1144 **************************/
1147 status_window_expose_event (GtkWidget *widget,
1148 GdkEventExpose *event)
1150 gdk_draw_rectangle (widget->window,
1151 widget->style->base_gc [GTK_STATE_NORMAL],
1154 widget->allocation.width, widget->allocation.height);
1155 gdk_draw_rectangle (widget->window,
1156 widget->style->text_gc [GTK_STATE_NORMAL],
1159 widget->allocation.width - 1, widget->allocation.height - 1);
1165 status_window_style_set (GtkWidget *toplevel,
1166 GtkStyle *previous_style,
1171 for (i = 0; i < 5; i++)
1172 gtk_widget_modify_fg (label, i, &toplevel->style->text[i]);
1175 /* Frees a status window and removes its link from the status_windows list */
1177 status_window_free (StatusWindow *status_window)
1179 status_windows = g_slist_remove (status_windows, status_window);
1181 g_signal_handler_disconnect (status_window->toplevel, status_window->destroy_handler_id);
1182 g_signal_handler_disconnect (status_window->toplevel, status_window->configure_handler_id);
1183 gtk_widget_destroy (status_window->window);
1184 g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
1186 g_free (status_window);
1190 status_window_configure (GtkWidget *toplevel,
1191 GdkEventConfigure *event,
1192 StatusWindow *status_window)
1195 GtkRequisition requisition;
1197 gint height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
1199 gdk_window_get_frame_extents (toplevel->window, &rect);
1200 gtk_widget_size_request (status_window->window, &requisition);
1202 if (rect.y + rect.height + requisition.height < height)
1203 y = rect.y + rect.height;
1205 y = height - requisition.height;
1207 gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
1213 status_window_get (GtkIMContextXIM *context_xim,
1216 GdkWindow *toplevel_gdk;
1217 GtkWidget *toplevel;
1219 StatusWindow *status_window;
1220 GtkWidget *status_label;
1222 GdkWindow *root_window;
1224 if (!context_xim->client_window)
1227 toplevel_gdk = context_xim->client_window;
1228 screen = gdk_drawable_get_screen (toplevel_gdk);
1229 root_window = gdk_screen_get_root_window (screen);
1233 GdkWindow *parent = gdk_window_get_parent (toplevel_gdk);
1234 if (parent == root_window)
1237 toplevel_gdk = parent;
1240 gdk_window_get_user_data (toplevel_gdk, (gpointer *)&toplevel);
1244 status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
1246 return status_window->window;
1250 status_window = g_new (StatusWindow, 1);
1251 status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
1252 status_window->toplevel = toplevel;
1254 status_windows = g_slist_prepend (status_windows, status_window);
1256 window = status_window->window;
1258 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
1259 gtk_widget_set_app_paintable (window, TRUE);
1261 status_label = gtk_label_new ("");
1262 gtk_misc_set_padding (GTK_MISC (status_label), 1, 1);
1263 gtk_widget_show (status_label);
1265 gtk_container_add (GTK_CONTAINER (window), status_label);
1267 status_window->destroy_handler_id = g_signal_connect_swapped (toplevel, "destroy",
1268 G_CALLBACK (status_window_free),
1270 status_window->configure_handler_id = g_signal_connect (toplevel, "configure_event",
1271 G_CALLBACK (status_window_configure),
1274 status_window_configure (toplevel, NULL, status_window);
1276 g_signal_connect (window, "style_set",
1277 G_CALLBACK (status_window_style_set), status_label);
1278 g_signal_connect (window, "expose_event",
1279 G_CALLBACK (status_window_expose_event), NULL);
1281 g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);
1287 status_window_has_text (GtkWidget *status_window)
1289 GtkWidget *label = GTK_BIN (status_window)->child;
1290 const gchar *text = gtk_label_get_text (GTK_LABEL (label));
1292 return text[0] != '\0';
1296 status_window_show (GtkIMContextXIM *context_xim)
1298 GtkWidget *status_window = status_window_get (context_xim, TRUE);
1300 context_xim->status_visible = TRUE;
1304 status_window_hide (GtkIMContextXIM *context_xim)
1306 GtkWidget *status_window = status_window_get (context_xim, FALSE);
1308 context_xim->status_visible = FALSE;
1310 status_window_set_text (context_xim, "");
1314 status_window_set_text (GtkIMContextXIM *context_xim,
1317 GtkWidget *status_window = status_window_get (context_xim, TRUE);
1321 GtkWidget *label = GTK_BIN (status_window)->child;
1322 gtk_label_set_text (GTK_LABEL (label), text);
1324 if (context_xim->status_visible && status_window_has_text (status_window))
1325 gtk_widget_show (status_window);
1327 gtk_widget_hide (status_window);
1332 * gtk_im_context_xim_shutdown:
1334 * Destroys all the status windows that are kept by the XIM contexts. This
1335 * function should only be called by the XIM module exit routine.
1338 gtk_im_context_xim_shutdown (void)
1340 while (status_windows)
1341 status_window_free (status_windows->data);