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/gtklabel.h"
24 #include "gtk/gtksignal.h"
25 #include "gtk/gtkwindow.h"
26 #include "gtkimcontextxim.h"
28 typedef struct _StatusWindow StatusWindow;
38 /* A context status window; these are kept in the status_windows list. */
43 /* Toplevel window to which the status window corresponds */
46 /* Signal connection ids; we connect to the toplevel */
47 gulong destroy_handler_id;
48 gulong configure_handler_id;
51 static void gtk_im_context_xim_class_init (GtkIMContextXIMClass *class);
52 static void gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim);
53 static void gtk_im_context_xim_finalize (GObject *obj);
54 static void gtk_im_context_xim_set_client_window (GtkIMContext *context,
55 GdkWindow *client_window);
56 static gboolean gtk_im_context_xim_filter_keypress (GtkIMContext *context,
58 static void gtk_im_context_xim_reset (GtkIMContext *context);
59 static void gtk_im_context_xim_focus_in (GtkIMContext *context);
60 static void gtk_im_context_xim_focus_out (GtkIMContext *context);
61 static void gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
63 static void gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
64 gboolean use_preedit);
65 static void gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
67 PangoAttrList **attrs,
70 static void status_window_show (GtkIMContextXIM *context_xim);
71 static void status_window_hide (GtkIMContextXIM *context_xim);
72 static void status_window_set_text (GtkIMContextXIM *context_xim,
75 static XIC gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim);
76 static GObjectClass *parent_class;
78 GType gtk_type_im_context_xim = 0;
80 GSList *open_ims = NULL;
82 /* List of status windows for different toplevels */
83 static GSList *status_windows = NULL;
86 gtk_im_context_xim_register_type (GTypeModule *type_module)
88 static const GTypeInfo im_context_xim_info =
90 sizeof (GtkIMContextXIMClass),
92 (GBaseFinalizeFunc) NULL,
93 (GClassInitFunc) gtk_im_context_xim_class_init,
94 NULL, /* class_finalize */
95 NULL, /* class_data */
96 sizeof (GtkIMContextXIM),
98 (GtkObjectInitFunc) gtk_im_context_xim_init,
101 gtk_type_im_context_xim =
102 g_type_module_register_type (type_module,
105 &im_context_xim_info, 0);
108 #define PREEDIT_MASK (XIMPreeditCallbacks | XIMPreeditPosition | \
109 XIMPreeditArea | XIMPreeditNothing | XIMPreeditNone)
110 #define STATUS_MASK (XIMStatusCallbacks | XIMStatusArea | \
111 XIMStatusNothing | XIMStatusNone)
112 #define ALLOWED_MASK (XIMPreeditCallbacks | XIMPreeditNothing | XIMPreeditNone | \
113 XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone)
116 choose_better_style (XIMStyle style1, XIMStyle style2)
120 if (style1 == 0) return style2;
121 if (style2 == 0) return style1;
122 if ((style1 & (PREEDIT_MASK | STATUS_MASK))
123 == (style2 & (PREEDIT_MASK | STATUS_MASK)))
126 s1 = style1 & PREEDIT_MASK;
127 s2 = style2 & PREEDIT_MASK;
130 if (u & XIMPreeditCallbacks)
131 return (s1 == XIMPreeditCallbacks) ? style1 : style2;
132 else if (u & XIMPreeditPosition)
133 return (s1 == XIMPreeditPosition) ? style1 :style2;
134 else if (u & XIMPreeditArea)
135 return (s1 == XIMPreeditArea) ? style1 : style2;
136 else if (u & XIMPreeditNothing)
137 return (s1 == XIMPreeditNothing) ? style1 : style2;
139 s1 = style1 & STATUS_MASK;
140 s2 = style2 & STATUS_MASK;
142 if (u & XIMStatusCallbacks)
143 return (s1 == XIMStatusCallbacks) ? style1 : style2;
144 else if (u & XIMStatusArea)
145 return (s1 == XIMStatusArea) ? style1 : style2;
146 else if (u & XIMStatusNothing)
147 return (s1 == XIMStatusNothing) ? style1 : style2;
148 else if (u & XIMStatusNone)
149 return (s1 == XIMStatusNone) ? style1 : style2;
151 return 0; /* Get rid of stupid warning */
155 setup_im (GtkXIMInfo *info)
157 XIMStyles *xim_styles = NULL;
158 XIMValuesList *ic_values = NULL;
161 XGetIMValues (info->im,
162 XNQueryInputStyle, &xim_styles,
163 XNQueryICValuesList, &ic_values,
169 for (i = 0; i < xim_styles->count_styles; i++)
170 if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
171 info->style = choose_better_style (info->style,
172 xim_styles->supported_styles[i]);
179 for (i = 0; i < ic_values->count_values; i++)
180 g_print ("%s\n", ic_values->supported_values[i]);
181 for (i = 0; i < xim_styles->count_styles; i++)
182 g_print ("%#x\n", xim_styles->supported_styles[i]);
193 get_im (GdkDisplay *display,
203 info = tmp_list->data;
204 if (info->display == display &&
205 strcmp (info->locale, locale) == 0)
208 tmp_list = tmp_list->next;
213 if (XSupportsLocale ())
215 if (!XSetLocaleModifiers (""))
216 g_warning ("can not set locale modifiers");
218 im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
222 info = g_new (GtkXIMInfo, 1);
223 open_ims = g_slist_prepend (open_ims, im);
225 info->display = display;
226 info->locale = g_strdup (locale);
237 gtk_im_context_xim_class_init (GtkIMContextXIMClass *class)
239 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
240 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
242 parent_class = g_type_class_peek_parent (class);
244 im_context_class->set_client_window = gtk_im_context_xim_set_client_window;
245 im_context_class->filter_keypress = gtk_im_context_xim_filter_keypress;
246 im_context_class->reset = gtk_im_context_xim_reset;
247 im_context_class->get_preedit_string = gtk_im_context_xim_get_preedit_string;
248 im_context_class->focus_in = gtk_im_context_xim_focus_in;
249 im_context_class->focus_out = gtk_im_context_xim_focus_out;
250 im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
251 im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
252 gobject_class->finalize = gtk_im_context_xim_finalize;
256 gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
258 im_context_xim->use_preedit = TRUE;
262 gtk_im_context_xim_finalize (GObject *obj)
264 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);
268 XDestroyIC (context_xim->ic);
269 context_xim->ic = NULL;
272 g_free (context_xim->locale);
273 g_free (context_xim->mb_charset);
277 reinitialize_ic (GtkIMContextXIM *context_xim)
281 XDestroyIC (context_xim->ic);
282 context_xim->ic = NULL;
284 if (context_xim->preedit_length)
286 context_xim->preedit_length = 0;
287 g_signal_emit_by_name (context_xim, "preedit_changed");
293 gtk_im_context_xim_set_client_window (GtkIMContext *context,
294 GdkWindow *client_window)
296 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
298 reinitialize_ic (context_xim);
299 context_xim->client_window = client_window;
301 if (context_xim->client_window)
302 context_xim->im_info = get_im (gdk_drawable_get_display (context_xim->client_window), context_xim->locale);
304 context_xim->im_info = NULL;
308 gtk_im_context_xim_new (void)
310 GtkIMContextXIM *result;
311 const gchar *charset;
313 result = GTK_IM_CONTEXT_XIM (g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL));
315 result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
317 g_get_charset (&charset);
318 result->mb_charset = g_strdup (charset);
320 return GTK_IM_CONTEXT (result);
324 mb_to_utf8 (GtkIMContextXIM *context_xim,
327 GError *error = NULL;
330 if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
331 result = g_strdup (str);
334 result = g_convert (str, -1,
335 "UTF-8", context_xim->mb_charset,
339 g_warning ("Error converting text from IM to UTF-8: %s\n", error->message);
340 g_error_free (error);
348 gtk_im_context_xim_filter_keypress (GtkIMContext *context,
351 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
352 XIC ic = gtk_im_context_xim_get_ic (context_xim);
353 gchar static_buffer[256];
354 gchar *buffer = static_buffer;
355 gint buffer_size = sizeof(static_buffer) - 1;
359 gboolean result = FALSE;
360 GdkWindow *root_window = gdk_screen_get_root_window (gdk_drawable_get_screen (event->window));
362 XKeyPressedEvent xevent;
367 xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
368 xevent.serial = 0; /* hope it doesn't matter */
369 xevent.send_event = event->send_event;
370 xevent.display = GDK_DRAWABLE_XDISPLAY (event->window);
371 xevent.window = GDK_DRAWABLE_XID (event->window);
372 xevent.root = GDK_DRAWABLE_XID (root_window);
373 xevent.subwindow = xevent.window;
374 xevent.time = event->time;
375 xevent.x = xevent.x_root = 0;
376 xevent.y = xevent.y_root = 0;
377 xevent.state = event->state;
378 xevent.keycode = event->keyval ? XKeysymToKeycode (xevent.display, event->keyval) : 0;
379 xevent.same_screen = True;
381 if (XFilterEvent ((XEvent *)&xevent, GDK_DRAWABLE_XID (context_xim->client_window)))
385 num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
387 if (status == XBufferOverflow)
389 buffer_size = num_bytes;
390 buffer = g_malloc (num_bytes + 1);
394 /* I don't know how we should properly handle XLookupKeysym or XLookupBoth
395 * here ... do input methods actually change the keysym? we can't really
396 * feed it back to accelerator processing at this point...
398 if (status == XLookupChars || status == XLookupBoth)
402 buffer[num_bytes] = '\0';
404 result_utf8 = mb_to_utf8 (context_xim, buffer);
407 if ((guchar)result_utf8[0] >= 0x20 &&
408 result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
409 * control characters into strings
412 g_signal_emit_by_name (context, "commit", result_utf8);
416 g_free (result_utf8);
424 gtk_im_context_xim_focus_in (GtkIMContext *context)
426 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
427 XIC ic = gtk_im_context_xim_get_ic (context_xim);
434 status_window_show (context_xim);
440 gtk_im_context_xim_focus_out (GtkIMContext *context)
442 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
443 XIC ic = gtk_im_context_xim_get_ic (context_xim);
450 status_window_hide (context_xim);
456 gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
459 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
460 XIC ic = gtk_im_context_xim_get_ic (context_xim);
462 XVaNestedList preedit_attr;
471 preedit_attr = XVaCreateNestedList (0,
472 XNSpotLocation, &spot,
475 XNPreeditAttributes, preedit_attr,
483 gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
484 gboolean use_preedit)
486 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
488 use_preedit = use_preedit != FALSE;
490 if (context_xim->use_preedit != use_preedit)
492 context_xim->use_preedit = use_preedit;
493 reinitialize_ic (context_xim);
500 gtk_im_context_xim_reset (GtkIMContext *context)
502 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
503 XIC ic = gtk_im_context_xim_get_ic (context_xim);
506 /* restore conversion state after resetting ic later */
507 XIMPreeditState preedit_state = XIMPreeditUnKnown;
508 XVaNestedList preedit_attr;
509 gboolean have_preedit_state = FALSE;
515 if (context_xim->preedit_length == 0)
518 preedit_attr = XVaCreateNestedList(0,
519 XNPreeditState, &preedit_state,
521 if (!XGetICValues(ic,
522 XNPreeditAttributes, preedit_attr,
524 have_preedit_state = TRUE;
528 result = XmbResetIC (ic);
530 preedit_attr = XVaCreateNestedList(0,
531 XNPreeditState, preedit_state,
533 if (have_preedit_state)
535 XNPreeditAttributes, preedit_attr,
542 char *result_utf8 = mb_to_utf8 (context_xim, result);
545 g_signal_emit_by_name (context, "commit", result_utf8);
546 g_free (result_utf8);
550 if (context_xim->preedit_length)
552 context_xim->preedit_length = 0;
553 g_signal_emit_by_name (context, "preedit_changed");
559 /* Mask of feedback bits that we render
561 #define FEEDBACK_MASK (XIMReverse | XIMUnderline)
564 add_feedback_attr (PangoAttrList *attrs,
566 XIMFeedback feedback,
570 PangoAttribute *attr;
572 gint start_index = g_utf8_offset_to_pointer (str, start_pos) - str;
573 gint end_index = g_utf8_offset_to_pointer (str, end_pos) - str;
575 if (feedback & XIMUnderline)
577 attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
578 attr->start_index = start_index;
579 attr->end_index = end_index;
581 pango_attr_list_change (attrs, attr);
584 if (feedback & XIMReverse)
586 attr = pango_attr_foreground_new (0xffff, 0xffff, 0xffff);
587 attr->start_index = start_index;
588 attr->end_index = end_index;
590 pango_attr_list_change (attrs, attr);
592 attr = pango_attr_background_new (0, 0, 0);
593 attr->start_index = start_index;
594 attr->end_index = end_index;
596 pango_attr_list_change (attrs, attr);
599 if (feedback & ~FEEDBACK_MASK)
600 g_warning ("Unrendered feedback style: %#lx", feedback & ~FEEDBACK_MASK);
604 gtk_im_context_xim_get_preedit_string (GtkIMContext *context,
606 PangoAttrList **attrs,
609 GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
610 gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
615 XIMFeedback last_feedback = 0;
618 *attrs = pango_attr_list_new ();
620 for (i = 0; i < context_xim->preedit_length; i++)
622 XIMFeedback new_feedback = context_xim->feedbacks[i] & FEEDBACK_MASK;
623 if (new_feedback != last_feedback)
626 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
628 last_feedback = new_feedback;
634 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
643 *cursor_pos = context_xim->preedit_cursor;
647 preedit_start_callback (XIC xic,
648 XPointer client_data,
651 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
653 g_signal_emit_by_name (context, "preedit_start");
657 preedit_done_callback (XIC xic,
658 XPointer client_data,
661 GtkIMContext *context = GTK_IM_CONTEXT (client_data);
663 g_signal_emit_by_name (context, "preedit_end");
667 xim_text_to_utf8 (GtkIMContextXIM *context, XIMText *xim_text, gchar **text)
669 gint text_length = 0;
670 GError *error = NULL;
671 gchar *result = NULL;
673 if (xim_text && xim_text->string.multi_byte)
675 if (xim_text->encoding_is_wchar)
677 g_warning ("Wide character return from Xlib not currently supported");
682 if (strcmp (context->mb_charset, "UTF-8") == 0)
683 result = g_strdup (xim_text->string.multi_byte);
685 result = g_convert (xim_text->string.multi_byte,
693 text_length = g_utf8_strlen (result, -1);
695 if (text_length != xim_text->length)
697 g_warning ("Size mismatch when converting text from input method: supplied length = %d\n, result length = %d", xim_text->length, text_length);
702 g_warning ("Error converting text from IM to UCS-4: %s", error->message);
703 g_error_free (error);
720 preedit_draw_callback (XIC xic,
721 XPointer client_data,
722 XIMPreeditDrawCallbackStruct *call_data)
724 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
726 XIMText *new_xim_text = call_data->text;
727 gint new_text_length;
728 gunichar *new_text = NULL;
734 gint chg_first = CLAMP (call_data->chg_first, 0, context->preedit_length);
735 gint chg_length = CLAMP (call_data->chg_length, 0, context->preedit_length - chg_first);
737 context->preedit_cursor = call_data->caret;
739 if (chg_first != call_data->chg_first || chg_length != call_data->chg_length)
740 g_warning ("Invalid change to preedit string, first=%d length=%d (orig length == %d)",
741 call_data->chg_first, call_data->chg_length, context->preedit_length);
743 new_text_length = xim_text_to_utf8 (context, new_xim_text, &tmp);
746 new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
750 diff = new_text_length - chg_length;
751 new_length = context->preedit_length + diff;
753 if (new_length > context->preedit_size)
755 context->preedit_size = new_length;
756 context->preedit_chars = g_renew (gunichar, context->preedit_chars, new_length);
757 context->feedbacks = g_renew (XIMFeedback, context->feedbacks, new_length);
762 for (i = chg_first + chg_length ; i < context->preedit_length; i++)
764 context->preedit_chars[i + diff] = context->preedit_chars[i];
765 context->feedbacks[i + diff] = context->feedbacks[i];
770 for (i = context->preedit_length - 1; i >= chg_first + chg_length ; i--)
772 context->preedit_chars[i + diff] = context->preedit_chars[i];
773 context->feedbacks[i + diff] = context->feedbacks[i];
777 for (i = 0; i < new_text_length; i++)
779 context->preedit_chars[chg_first + i] = new_text[i];
780 context->feedbacks[chg_first + i] = new_xim_text->feedback[i];
783 context->preedit_length += diff;
788 g_signal_emit_by_name (context, "preedit_changed");
793 preedit_caret_callback (XIC xic,
794 XPointer client_data,
795 XIMPreeditCaretCallbackStruct *call_data)
797 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
799 if (call_data->direction == XIMAbsolutePosition)
801 context->preedit_cursor = call_data->position;
802 g_signal_emit_by_name (context, "preedit_changed");
806 g_warning ("Caret movement command: %d %d %d not supported",
807 call_data->position, call_data->direction, call_data->style);
812 status_start_callback (XIC xic,
813 XPointer client_data,
820 status_done_callback (XIC xic,
821 XPointer client_data,
828 status_draw_callback (XIC xic,
829 XPointer client_data,
830 XIMStatusDrawCallbackStruct *call_data)
832 GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
834 if (call_data->type == XIMTextType)
837 xim_text_to_utf8 (context, call_data->data.text, &text);
840 status_window_set_text (context, text);
842 status_window_set_text (context, "");
846 g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
851 gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
853 const char *name1 = NULL;
854 XVaNestedList list1 = NULL;
855 const char *name2 = NULL;
856 XVaNestedList list2 = NULL;
858 if (!context_xim->ic && context_xim->client_window)
860 if (!context_xim->use_preedit)
862 context_xim->ic = XCreateIC (context_xim->im_info->im,
863 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
864 XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
866 return context_xim->ic;
869 if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
871 context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
872 context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
873 context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
874 context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
875 context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
876 context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
877 context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
878 context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
880 name1 = XNPreeditAttributes;
881 list1 = XVaCreateNestedList (0,
882 XNPreeditStartCallback, &context_xim->preedit_start_callback,
883 XNPreeditDoneCallback, &context_xim->preedit_done_callback,
884 XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
885 XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
889 if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
891 XVaNestedList status_attrs;
893 context_xim->status_start_callback.client_data = (XPointer)context_xim;
894 context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
895 context_xim->status_done_callback.client_data = (XPointer)context_xim;
896 context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
897 context_xim->status_draw_callback.client_data = (XPointer)context_xim;
898 context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
900 status_attrs = XVaCreateNestedList (0,
901 XNStatusStartCallback, &context_xim->status_start_callback,
902 XNStatusDoneCallback, &context_xim->status_done_callback,
903 XNStatusDrawCallback, &context_xim->status_draw_callback,
908 name1 = XNStatusAttributes;
909 list1 = status_attrs;
913 name2 = XNStatusAttributes;
914 list2 = status_attrs;
918 context_xim->ic = XCreateIC (context_xim->im_info->im,
919 XNInputStyle, context_xim->im_info->style,
920 XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
931 return context_xim->ic;
934 /**************************
936 * Status Window handling *
938 **************************/
941 status_window_expose_event (GtkWidget *widget,
942 GdkEventExpose *event)
944 gdk_draw_rectangle (widget->window,
945 widget->style->base_gc [GTK_STATE_NORMAL],
948 widget->allocation.width, widget->allocation.height);
949 gdk_draw_rectangle (widget->window,
950 widget->style->text_gc [GTK_STATE_NORMAL],
953 widget->allocation.width - 1, widget->allocation.height - 1);
959 status_window_style_set (GtkWidget *toplevel,
960 GtkStyle *previous_style,
965 for (i = 0; i < 5; i++)
966 gtk_widget_modify_fg (label, i, &toplevel->style->text[i]);
969 /* Frees a status window and removes its link from the status_windows list */
971 status_window_free (StatusWindow *status_window)
973 status_windows = g_slist_remove (status_windows, status_window);
975 g_signal_handler_disconnect (status_window->toplevel, status_window->destroy_handler_id);
976 g_signal_handler_disconnect (status_window->toplevel, status_window->configure_handler_id);
977 gtk_widget_destroy (status_window->window);
978 g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
980 g_free (status_window);
984 status_window_configure (GtkWidget *toplevel,
985 GdkEventConfigure *event,
986 StatusWindow *status_window)
989 GtkRequisition requisition;
991 gint height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
993 gdk_window_get_frame_extents (toplevel->window, &rect);
994 gtk_widget_size_request (status_window->window, &requisition);
996 if (rect.y + rect.height + requisition.height < height)
997 y = rect.y + rect.height;
999 y = height - requisition.height;
1001 gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
1007 status_window_get (GtkIMContextXIM *context_xim,
1010 GdkWindow *toplevel_gdk;
1011 GtkWidget *toplevel;
1013 StatusWindow *status_window;
1014 GtkWidget *status_label;
1016 if (!context_xim->client_window)
1019 toplevel_gdk = context_xim->client_window;
1022 GdkWindow *parent = gdk_window_get_parent (toplevel_gdk);
1023 if (parent == gdk_get_default_root_window ())
1026 toplevel_gdk = parent;
1029 gdk_window_get_user_data (toplevel_gdk, (gpointer *)&toplevel);
1033 status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
1035 return status_window->window;
1039 status_window = g_new (StatusWindow, 1);
1040 status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
1041 status_window->toplevel = toplevel;
1043 status_windows = g_slist_prepend (status_windows, status_window);
1045 window = status_window->window;
1047 gtk_window_set_policy (GTK_WINDOW (window), FALSE, FALSE, FALSE);
1048 gtk_widget_set_app_paintable (window, TRUE);
1050 status_label = gtk_label_new ("");
1051 gtk_misc_set_padding (GTK_MISC (status_label), 1, 1);
1052 gtk_widget_show (status_label);
1054 gtk_container_add (GTK_CONTAINER (window), status_label);
1056 status_window->destroy_handler_id = g_signal_connect_swapped (toplevel, "destroy",
1057 G_CALLBACK (status_window_free),
1059 status_window->configure_handler_id = g_signal_connect (toplevel, "configure_event",
1060 G_CALLBACK (status_window_configure),
1063 status_window_configure (toplevel, NULL, status_window);
1065 g_signal_connect (window, "style_set",
1066 G_CALLBACK (status_window_style_set), status_label);
1067 g_signal_connect (window, "expose_event",
1068 G_CALLBACK (status_window_expose_event), NULL);
1070 g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);
1076 status_window_has_text (GtkWidget *status_window)
1078 GtkWidget *label = GTK_BIN (status_window)->child;
1079 const gchar *text = gtk_label_get_text (GTK_LABEL (label));
1081 return text[0] != '\0';
1085 status_window_show (GtkIMContextXIM *context_xim)
1087 GtkWidget *status_window = status_window_get (context_xim, TRUE);
1089 context_xim->status_visible = TRUE;
1091 if (status_window && status_window_has_text (status_window))
1092 gtk_widget_show (status_window);
1096 status_window_hide (GtkIMContextXIM *context_xim)
1098 GtkWidget *status_window = status_window_get (context_xim, FALSE);
1100 context_xim->status_visible = FALSE;
1103 gtk_widget_hide (status_window);
1107 status_window_set_text (GtkIMContextXIM *context_xim,
1110 GtkWidget *status_window = status_window_get (context_xim, TRUE);
1114 GtkWidget *label = GTK_BIN (status_window)->child;
1115 gtk_label_set_text (GTK_LABEL (label), text);
1117 if (context_xim->status_visible && status_window_has_text (status_window))
1118 gtk_widget_show (status_window);
1120 gtk_widget_hide (status_window);
1125 * gtk_im_context_xim_shutdown:
1127 * Destroys all the status windows that are kept by the XIM contexts. This
1128 * function should only be called by the XIM module exit routine.
1131 gtk_im_context_xim_shutdown (void)
1133 while (status_windows)
1134 status_window_free (status_windows->data);