Blame modules/input/gtkimcontextxim.c

Packit Service fb6fa5
/* GTK - The GIMP Toolkit
Packit Service fb6fa5
 * Copyright (C) 2000 Red Hat, Inc.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * This library is free software; you can redistribute it and/or
Packit Service fb6fa5
 * modify it under the terms of the GNU Lesser General Public
Packit Service fb6fa5
 * License as published by the Free Software Foundation; either
Packit Service fb6fa5
 * version 2 of the License, or (at your option) any later version.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * This library is distributed in the hope that it will be useful,
Packit Service fb6fa5
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fb6fa5
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service fb6fa5
 * Lesser General Public License for more details.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * You should have received a copy of the GNU Lesser General Public
Packit Service fb6fa5
 * License along with this library; if not, write to the
Packit Service fb6fa5
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Service fb6fa5
 * Boston, MA 02111-1307, USA.
Packit Service fb6fa5
 */
Packit Service fb6fa5
Packit Service fb6fa5
#include "config.h"
Packit Service fb6fa5
#include "locale.h"
Packit Service fb6fa5
#include <string.h>
Packit Service fb6fa5
#include <stdlib.h>
Packit Service fb6fa5
Packit Service fb6fa5
#include "gtkimcontextxim.h"
Packit Service fb6fa5
Packit Service fb6fa5
#include "gtk/gtkintl.h"
Packit Service fb6fa5
Packit Service fb6fa5
typedef struct _StatusWindow StatusWindow;
Packit Service fb6fa5
typedef struct _GtkXIMInfo GtkXIMInfo;
Packit Service fb6fa5
Packit Service fb6fa5
struct _GtkIMContextXIM
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContext object;
Packit Service fb6fa5
Packit Service fb6fa5
  GtkXIMInfo *im_info;
Packit Service fb6fa5
Packit Service fb6fa5
  gchar *locale;
Packit Service fb6fa5
  gchar *mb_charset;
Packit Service fb6fa5
Packit Service fb6fa5
  GdkWindow *client_window;
Packit Service fb6fa5
  GtkWidget *client_widget;
Packit Service fb6fa5
Packit Service fb6fa5
  /* The status window for this input context; we claim the
Packit Service fb6fa5
   * status window when we are focused and have created an XIC
Packit Service fb6fa5
   */
Packit Service fb6fa5
  StatusWindow *status_window;
Packit Service fb6fa5
Packit Service fb6fa5
  gint preedit_size;
Packit Service fb6fa5
  gint preedit_length;
Packit Service fb6fa5
  gunichar *preedit_chars;
Packit Service fb6fa5
  XIMFeedback *feedbacks;
Packit Service fb6fa5
Packit Service fb6fa5
  gint preedit_cursor;
Packit Service fb6fa5
  
Packit Service fb6fa5
  XIMCallback preedit_start_callback;
Packit Service fb6fa5
  XIMCallback preedit_done_callback;
Packit Service fb6fa5
  XIMCallback preedit_draw_callback;
Packit Service fb6fa5
  XIMCallback preedit_caret_callback;
Packit Service fb6fa5
Packit Service fb6fa5
  XIMCallback status_start_callback;
Packit Service fb6fa5
  XIMCallback status_done_callback;
Packit Service fb6fa5
  XIMCallback status_draw_callback;
Packit Service fb6fa5
Packit Service fb6fa5
  XIMCallback string_conversion_callback;
Packit Service fb6fa5
Packit Service fb6fa5
  XIC ic;
Packit Service fb6fa5
Packit Service fb6fa5
  guint filter_key_release : 1;
Packit Service fb6fa5
  guint use_preedit : 1;
Packit Service fb6fa5
  guint finalizing : 1;
Packit Service fb6fa5
  guint in_toplevel : 1;
Packit Service fb6fa5
  guint has_focus : 1;
Packit Service fb6fa5
};
Packit Service fb6fa5
Packit Service fb6fa5
struct _GtkXIMInfo
Packit Service fb6fa5
{
Packit Service fb6fa5
  GdkScreen *screen;
Packit Service fb6fa5
  XIM im;
Packit Service fb6fa5
  char *locale;
Packit Service fb6fa5
  XIMStyle preedit_style_setting;
Packit Service fb6fa5
  XIMStyle status_style_setting;
Packit Service fb6fa5
  XIMStyle style;
Packit Service fb6fa5
  GtkSettings *settings;
Packit Service fb6fa5
  gulong status_set;
Packit Service fb6fa5
  gulong preedit_set;
Packit Service fb6fa5
  gulong display_closed_cb;
Packit Service fb6fa5
  XIMStyles *xim_styles;
Packit Service fb6fa5
  GSList *ics;
Packit Service fb6fa5
Packit Service fb6fa5
  guint reconnecting :1;
Packit Service fb6fa5
  guint supports_string_conversion;
Packit Service fb6fa5
};
Packit Service fb6fa5
Packit Service fb6fa5
/* A context status window; these are kept in the status_windows list. */
Packit Service fb6fa5
struct _StatusWindow
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkWidget *window;
Packit Service fb6fa5
  
Packit Service fb6fa5
  /* Toplevel window to which the status window corresponds */
Packit Service fb6fa5
  GtkWidget *toplevel;
Packit Service fb6fa5
Packit Service fb6fa5
  /* Currently focused GtkIMContextXIM for the toplevel, if any */
Packit Service fb6fa5
  GtkIMContextXIM *context;
Packit Service fb6fa5
};
Packit Service fb6fa5
Packit Service fb6fa5
static void     gtk_im_context_xim_class_init         (GtkIMContextXIMClass  *class);
Packit Service fb6fa5
static void     gtk_im_context_xim_init               (GtkIMContextXIM       *im_context_xim);
Packit Service fb6fa5
static void     gtk_im_context_xim_finalize           (GObject               *obj);
Packit Service fb6fa5
static void     gtk_im_context_xim_set_client_window  (GtkIMContext          *context,
Packit Service fb6fa5
						       GdkWindow             *client_window);
Packit Service fb6fa5
static gboolean gtk_im_context_xim_filter_keypress    (GtkIMContext          *context,
Packit Service fb6fa5
						       GdkEventKey           *key);
Packit Service fb6fa5
static void     gtk_im_context_xim_reset              (GtkIMContext          *context);
Packit Service fb6fa5
static void     gtk_im_context_xim_focus_in           (GtkIMContext          *context);
Packit Service fb6fa5
static void     gtk_im_context_xim_focus_out          (GtkIMContext          *context);
Packit Service fb6fa5
static void     gtk_im_context_xim_set_cursor_location (GtkIMContext          *context,
Packit Service fb6fa5
						       GdkRectangle		*area);
Packit Service fb6fa5
static void     gtk_im_context_xim_set_use_preedit    (GtkIMContext          *context,
Packit Service fb6fa5
						       gboolean		      use_preedit);
Packit Service fb6fa5
static void     gtk_im_context_xim_get_preedit_string (GtkIMContext          *context,
Packit Service fb6fa5
						       gchar                **str,
Packit Service fb6fa5
						       PangoAttrList        **attrs,
Packit Service fb6fa5
						       gint                  *cursor_pos);
Packit Service fb6fa5
Packit Service fb6fa5
static void reinitialize_ic      (GtkIMContextXIM *context_xim);
Packit Service fb6fa5
static void set_ic_client_window (GtkIMContextXIM *context_xim,
Packit Service fb6fa5
				  GdkWindow       *client_window);
Packit Service fb6fa5
Packit Service fb6fa5
static void setup_styles (GtkXIMInfo *info);
Packit Service fb6fa5
Packit Service fb6fa5
static void update_client_widget   (GtkIMContextXIM *context_xim);
Packit Service fb6fa5
static void update_status_window   (GtkIMContextXIM *context_xim);
Packit Service fb6fa5
Packit Service fb6fa5
static StatusWindow *status_window_get      (GtkWidget    *toplevel);
Packit Service fb6fa5
static void          status_window_free     (StatusWindow *status_window);
Packit Service fb6fa5
static void          status_window_set_text (StatusWindow *status_window,
Packit Service fb6fa5
					     const gchar  *text);
Packit Service fb6fa5
Packit Service fb6fa5
static void xim_destroy_callback   (XIM      xim,
Packit Service fb6fa5
				    XPointer client_data,
Packit Service fb6fa5
				    XPointer call_data);
Packit Service fb6fa5
Packit Service fb6fa5
static XIC       gtk_im_context_xim_get_ic            (GtkIMContextXIM *context_xim);
Packit Service fb6fa5
static void           xim_info_display_closed (GdkDisplay *display,
Packit Service fb6fa5
			                       gboolean    is_error,
Packit Service fb6fa5
			                       GtkXIMInfo *info);
Packit Service fb6fa5
Packit Service fb6fa5
static GObjectClass *parent_class;
Packit Service fb6fa5
Packit Service fb6fa5
GType gtk_type_im_context_xim = 0;
Packit Service fb6fa5
Packit Service fb6fa5
static GSList *open_ims = NULL;
Packit Service fb6fa5
Packit Service fb6fa5
/* List of status windows for different toplevels */
Packit Service fb6fa5
static GSList *status_windows = NULL;
Packit Service fb6fa5
Packit Service fb6fa5
void
Packit Service fb6fa5
gtk_im_context_xim_register_type (GTypeModule *type_module)
Packit Service fb6fa5
{
Packit Service fb6fa5
  const GTypeInfo im_context_xim_info =
Packit Service fb6fa5
  {
Packit Service fb6fa5
    sizeof (GtkIMContextXIMClass),
Packit Service fb6fa5
    (GBaseInitFunc) NULL,
Packit Service fb6fa5
    (GBaseFinalizeFunc) NULL,
Packit Service fb6fa5
    (GClassInitFunc) gtk_im_context_xim_class_init,
Packit Service fb6fa5
    NULL,           /* class_finalize */    
Packit Service fb6fa5
    NULL,           /* class_data */
Packit Service fb6fa5
    sizeof (GtkIMContextXIM),
Packit Service fb6fa5
    0,
Packit Service fb6fa5
    (GInstanceInitFunc) gtk_im_context_xim_init,
Packit Service fb6fa5
  };
Packit Service fb6fa5
Packit Service fb6fa5
  gtk_type_im_context_xim = 
Packit Service fb6fa5
    g_type_module_register_type (type_module,
Packit Service fb6fa5
				 GTK_TYPE_IM_CONTEXT,
Packit Service fb6fa5
				 "GtkIMContextXIM",
Packit Service fb6fa5
				 &im_context_xim_info, 0);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
#define PREEDIT_MASK (XIMPreeditCallbacks | XIMPreeditPosition | \
Packit Service fb6fa5
		      XIMPreeditArea | XIMPreeditNothing | XIMPreeditNone)
Packit Service fb6fa5
#define STATUS_MASK (XIMStatusCallbacks | XIMStatusArea | \
Packit Service fb6fa5
		      XIMStatusNothing | XIMStatusNone)
Packit Service fb6fa5
#define ALLOWED_MASK (XIMPreeditCallbacks | XIMPreeditNothing | XIMPreeditNone | \
Packit Service fb6fa5
		      XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone)
Packit Service fb6fa5
Packit Service fb6fa5
static XIMStyle 
Packit Service fb6fa5
choose_better_style (XIMStyle style1, XIMStyle style2) 
Packit Service fb6fa5
{
Packit Service fb6fa5
  XIMStyle s1, s2, u; 
Packit Service fb6fa5
  
Packit Service fb6fa5
  if (style1 == 0) return style2;
Packit Service fb6fa5
  if (style2 == 0) return style1;
Packit Service fb6fa5
  if ((style1 & (PREEDIT_MASK | STATUS_MASK))
Packit Service fb6fa5
    	== (style2 & (PREEDIT_MASK | STATUS_MASK)))
Packit Service fb6fa5
    return style1;
Packit Service fb6fa5
Packit Service fb6fa5
  s1 = style1 & PREEDIT_MASK;
Packit Service fb6fa5
  s2 = style2 & PREEDIT_MASK;
Packit Service fb6fa5
  u = s1 | s2;
Packit Service fb6fa5
  if (s1 != s2) {
Packit Service fb6fa5
    if (u & XIMPreeditCallbacks)
Packit Service fb6fa5
      return (s1 == XIMPreeditCallbacks) ? style1 : style2;
Packit Service fb6fa5
    else if (u & XIMPreeditPosition)
Packit Service fb6fa5
      return (s1 == XIMPreeditPosition) ? style1 :style2;
Packit Service fb6fa5
    else if (u & XIMPreeditArea)
Packit Service fb6fa5
      return (s1 == XIMPreeditArea) ? style1 : style2;
Packit Service fb6fa5
    else if (u & XIMPreeditNothing)
Packit Service fb6fa5
      return (s1 == XIMPreeditNothing) ? style1 : style2;
Packit Service fb6fa5
    else if (u & XIMPreeditNone)
Packit Service fb6fa5
      return (s1 == XIMPreeditNone) ? style1 : style2;
Packit Service fb6fa5
  } else {
Packit Service fb6fa5
    s1 = style1 & STATUS_MASK;
Packit Service fb6fa5
    s2 = style2 & STATUS_MASK;
Packit Service fb6fa5
    u = s1 | s2;
Packit Service fb6fa5
    if (u & XIMStatusCallbacks)
Packit Service fb6fa5
      return (s1 == XIMStatusCallbacks) ? style1 : style2;
Packit Service fb6fa5
    else if (u & XIMStatusArea)
Packit Service fb6fa5
      return (s1 == XIMStatusArea) ? style1 : style2;
Packit Service fb6fa5
    else if (u & XIMStatusNothing)
Packit Service fb6fa5
      return (s1 == XIMStatusNothing) ? style1 : style2;
Packit Service fb6fa5
    else if (u & XIMStatusNone)
Packit Service fb6fa5
      return (s1 == XIMStatusNone) ? style1 : style2;
Packit Service fb6fa5
  }
Packit Service fb6fa5
  return 0; /* Get rid of stupid warning */
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
reinitialize_all_ics (GtkXIMInfo *info)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GSList *tmp_list;
Packit Service fb6fa5
Packit Service fb6fa5
  for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
Packit Service fb6fa5
    reinitialize_ic (tmp_list->data);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
status_style_change (GtkXIMInfo *info)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMStatusStyle status_style;
Packit Service fb6fa5
  
Packit Service fb6fa5
  g_object_get (info->settings,
Packit Service fb6fa5
		"gtk-im-status-style", &status_style,
Packit Service fb6fa5
		NULL);
Packit Service fb6fa5
  if (status_style == GTK_IM_STATUS_CALLBACK)
Packit Service fb6fa5
    info->status_style_setting = XIMStatusCallbacks;
Packit Service fb6fa5
  else if (status_style == GTK_IM_STATUS_NOTHING)
Packit Service fb6fa5
    info->status_style_setting = XIMStatusNothing;
Packit Service fb6fa5
  else if (status_style == GTK_IM_STATUS_NONE)
Packit Service fb6fa5
    info->status_style_setting = XIMStatusNone;
Packit Service fb6fa5
  else
Packit Service fb6fa5
    return;
Packit Service fb6fa5
Packit Service fb6fa5
  setup_styles (info);
Packit Service fb6fa5
  
Packit Service fb6fa5
  reinitialize_all_ics (info);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
preedit_style_change (GtkXIMInfo *info)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMPreeditStyle preedit_style;
Packit Service fb6fa5
  g_object_get (info->settings,
Packit Service fb6fa5
		"gtk-im-preedit-style", &preedit_style,
Packit Service fb6fa5
		NULL);
Packit Service fb6fa5
  if (preedit_style == GTK_IM_PREEDIT_CALLBACK)
Packit Service fb6fa5
    info->preedit_style_setting = XIMPreeditCallbacks;
Packit Service fb6fa5
  else if (preedit_style == GTK_IM_PREEDIT_NOTHING)
Packit Service fb6fa5
    info->preedit_style_setting = XIMPreeditNothing;
Packit Service fb6fa5
  else if (preedit_style == GTK_IM_PREEDIT_NONE)
Packit Service fb6fa5
    info->preedit_style_setting = XIMPreeditNone;
Packit Service fb6fa5
  else
Packit Service fb6fa5
    return;
Packit Service fb6fa5
Packit Service fb6fa5
  setup_styles (info);
Packit Service fb6fa5
  
Packit Service fb6fa5
  reinitialize_all_ics (info);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
setup_styles (GtkXIMInfo *info)
Packit Service fb6fa5
{
Packit Service fb6fa5
  int i;
Packit Service fb6fa5
  unsigned long settings_preference;
Packit Service fb6fa5
  XIMStyles *xim_styles = info->xim_styles;
Packit Service fb6fa5
Packit Service fb6fa5
  settings_preference = info->status_style_setting|info->preedit_style_setting;
Packit Service fb6fa5
  info->style = 0;
Packit Service fb6fa5
  if (xim_styles)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      for (i = 0; i < xim_styles->count_styles; i++)
Packit Service fb6fa5
	if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
Packit Service fb6fa5
	  {
Packit Service fb6fa5
	    if (settings_preference == xim_styles->supported_styles[i])
Packit Service fb6fa5
	      {
Packit Service fb6fa5
		info->style = settings_preference;
Packit Service fb6fa5
		break;
Packit Service fb6fa5
	      }
Packit Service fb6fa5
	    info->style = choose_better_style (info->style,
Packit Service fb6fa5
					       xim_styles->supported_styles[i]);
Packit Service fb6fa5
	  }
Packit Service fb6fa5
    }
Packit Service fb6fa5
  if (info->style == 0)
Packit Service fb6fa5
    info->style = XIMPreeditNothing | XIMStatusNothing;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
setup_im (GtkXIMInfo *info)
Packit Service fb6fa5
{
Packit Service fb6fa5
  XIMValuesList *ic_values = NULL;
Packit Service fb6fa5
  XIMCallback im_destroy_callback;
Packit Service fb6fa5
  GdkDisplay *display;
Packit Service fb6fa5
Packit Service fb6fa5
  if (info->im == NULL)
Packit Service fb6fa5
    return;
Packit Service fb6fa5
Packit Service fb6fa5
  im_destroy_callback.client_data = (XPointer)info;
Packit Service fb6fa5
  im_destroy_callback.callback = (XIMProc)xim_destroy_callback;
Packit Service fb6fa5
  XSetIMValues (info->im,
Packit Service fb6fa5
		XNDestroyCallback, &im_destroy_callback,
Packit Service fb6fa5
		NULL);
Packit Service fb6fa5
Packit Service fb6fa5
  XGetIMValues (info->im,
Packit Service fb6fa5
		XNQueryInputStyle, &info->xim_styles,
Packit Service fb6fa5
		XNQueryICValuesList, &ic_values,
Packit Service fb6fa5
		NULL);
Packit Service fb6fa5
Packit Service fb6fa5
  info->settings = gtk_settings_get_for_screen (info->screen);
Packit Service fb6fa5
  info->status_set = g_signal_connect_swapped (info->settings,
Packit Service fb6fa5
					       "notify::gtk-im-status-style",
Packit Service fb6fa5
					       G_CALLBACK (status_style_change),
Packit Service fb6fa5
					       info);
Packit Service fb6fa5
  info->preedit_set = g_signal_connect_swapped (info->settings,
Packit Service fb6fa5
						"notify::gtk-im-preedit-style",
Packit Service fb6fa5
						G_CALLBACK (preedit_style_change),
Packit Service fb6fa5
						info);
Packit Service fb6fa5
Packit Service fb6fa5
  info->supports_string_conversion = FALSE;
Packit Service fb6fa5
  if (ic_values)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      int i;
Packit Service fb6fa5
      
Packit Service fb6fa5
      for (i = 0; i < ic_values->count_values; i++)
Packit Service fb6fa5
	if (strcmp (ic_values->supported_values[i],
Packit Service fb6fa5
		    XNStringConversionCallback) == 0)
Packit Service fb6fa5
	  {
Packit Service fb6fa5
	    info->supports_string_conversion = TRUE;
Packit Service fb6fa5
	    break;
Packit Service fb6fa5
	  }
Packit Service fb6fa5
Packit Service fb6fa5
#if 0
Packit Service fb6fa5
      for (i = 0; i < ic_values->count_values; i++)
Packit Service fb6fa5
	g_print ("%s\n", ic_values->supported_values[i]);
Packit Service fb6fa5
      for (i = 0; i < xim_styles->count_styles; i++)
Packit Service fb6fa5
	g_print ("%#x\n", xim_styles->supported_styles[i]);
Packit Service fb6fa5
#endif
Packit Service fb6fa5
      
Packit Service fb6fa5
      XFree (ic_values);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  status_style_change (info);
Packit Service fb6fa5
  preedit_style_change (info);
Packit Service fb6fa5
Packit Service fb6fa5
  display = gdk_screen_get_display (info->screen);
Packit Service fb6fa5
  info->display_closed_cb = g_signal_connect (display, "closed",
Packit Service fb6fa5
	                                      G_CALLBACK (xim_info_display_closed), info);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
xim_info_display_closed (GdkDisplay *display,
Packit Service fb6fa5
			 gboolean    is_error,
Packit Service fb6fa5
			 GtkXIMInfo *info)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GSList *ics, *tmp_list;
Packit Service fb6fa5
Packit Service fb6fa5
  open_ims = g_slist_remove (open_ims, info);
Packit Service fb6fa5
Packit Service fb6fa5
  ics = info->ics;
Packit Service fb6fa5
  info->ics = NULL;
Packit Service fb6fa5
Packit Service fb6fa5
  for (tmp_list = ics; tmp_list; tmp_list = tmp_list->next)
Packit Service fb6fa5
    set_ic_client_window (tmp_list->data, NULL);
Packit Service fb6fa5
Packit Service fb6fa5
  g_slist_free (ics);
Packit Service fb6fa5
Packit Service fb6fa5
  if (info->status_set)
Packit Service fb6fa5
    g_signal_handler_disconnect (info->settings, info->status_set);
Packit Service fb6fa5
  if (info->preedit_set)
Packit Service fb6fa5
    g_signal_handler_disconnect (info->settings, info->preedit_set);
Packit Service fb6fa5
  if (info->display_closed_cb)
Packit Service fb6fa5
    g_signal_handler_disconnect (display, info->display_closed_cb);
Packit Service fb6fa5
Packit Service fb6fa5
  if (info->xim_styles)
Packit Service fb6fa5
    XFree (info->xim_styles);
Packit Service fb6fa5
  g_free (info->locale);
Packit Service fb6fa5
Packit Service fb6fa5
  if (info->im)
Packit Service fb6fa5
    XCloseIM (info->im);
Packit Service fb6fa5
Packit Service fb6fa5
  g_free (info);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
xim_instantiate_callback (Display *display, XPointer client_data,
Packit Service fb6fa5
			  XPointer call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkXIMInfo *info = (GtkXIMInfo*)client_data;
Packit Service fb6fa5
  XIM im = NULL;
Packit Service fb6fa5
Packit Service fb6fa5
  im = XOpenIM (display, NULL, NULL, NULL);
Packit Service fb6fa5
Packit Service fb6fa5
  if (!im)
Packit Service fb6fa5
    return;
Packit Service fb6fa5
Packit Service fb6fa5
  info->im = im;
Packit Service fb6fa5
  setup_im (info);
Packit Service fb6fa5
Packit Service fb6fa5
  XUnregisterIMInstantiateCallback (display, NULL, NULL, NULL,
Packit Service fb6fa5
				    xim_instantiate_callback,
Packit Service fb6fa5
				    (XPointer)info);
Packit Service fb6fa5
  info->reconnecting = FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* initialize info->im */
Packit Service fb6fa5
static void
Packit Service fb6fa5
xim_info_try_im (GtkXIMInfo *info)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GdkScreen *screen = info->screen;
Packit Service fb6fa5
  GdkDisplay *display = gdk_screen_get_display (screen);
Packit Service fb6fa5
Packit Service fb6fa5
  g_assert (info->im == NULL);
Packit Service fb6fa5
  if (info->reconnecting)
Packit Service fb6fa5
    return;
Packit Service fb6fa5
Packit Service fb6fa5
  if (XSupportsLocale ())
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (!XSetLocaleModifiers (""))
Packit Service fb6fa5
	g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
Packit Service fb6fa5
      info->im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
Packit Service fb6fa5
      if (!info->im)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  XRegisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY(display),
Packit Service fb6fa5
					  NULL, NULL, NULL,
Packit Service fb6fa5
					  xim_instantiate_callback,
Packit Service fb6fa5
					  (XPointer)info);
Packit Service fb6fa5
	  info->reconnecting = TRUE;
Packit Service fb6fa5
	  return;
Packit Service fb6fa5
	}
Packit Service fb6fa5
      setup_im (info);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
xim_destroy_callback (XIM      xim,
Packit Service fb6fa5
		      XPointer client_data,
Packit Service fb6fa5
		      XPointer call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkXIMInfo *info = (GtkXIMInfo*)client_data;
Packit Service fb6fa5
Packit Service fb6fa5
  info->im = NULL;
Packit Service fb6fa5
Packit Service fb6fa5
  g_signal_handler_disconnect (info->settings, info->status_set);
Packit Service fb6fa5
  info->status_set = 0;
Packit Service fb6fa5
  g_signal_handler_disconnect (info->settings, info->preedit_set);
Packit Service fb6fa5
  info->preedit_set = 0;
Packit Service fb6fa5
Packit Service fb6fa5
  reinitialize_all_ics (info);
Packit Service fb6fa5
  xim_info_try_im (info);
Packit Service fb6fa5
  return;
Packit Service fb6fa5
} 
Packit Service fb6fa5
Packit Service fb6fa5
static GtkXIMInfo *
Packit Service fb6fa5
get_im (GdkWindow *client_window,
Packit Service fb6fa5
	const char *locale)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GSList *tmp_list;
Packit Service fb6fa5
  GtkXIMInfo *info;
Packit Service fb6fa5
  GdkScreen *screen = gdk_window_get_screen (client_window);
Packit Service fb6fa5
Packit Service fb6fa5
  info = NULL;
Packit Service fb6fa5
  tmp_list = open_ims;
Packit Service fb6fa5
  while (tmp_list)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GtkXIMInfo *tmp_info = tmp_list->data;
Packit Service fb6fa5
      if (tmp_info->screen == screen &&
Packit Service fb6fa5
	  strcmp (tmp_info->locale, locale) == 0)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  if (tmp_info->im)
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      return tmp_info;
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	  else
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      tmp_info = tmp_info;
Packit Service fb6fa5
	      break;
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	}
Packit Service fb6fa5
      tmp_list = tmp_list->next;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (info == NULL)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      info = g_new (GtkXIMInfo, 1);
Packit Service fb6fa5
      open_ims = g_slist_prepend (open_ims, info);
Packit Service fb6fa5
Packit Service fb6fa5
      info->screen = screen;
Packit Service fb6fa5
      info->locale = g_strdup (locale);
Packit Service fb6fa5
      info->xim_styles = NULL;
Packit Service fb6fa5
      info->preedit_style_setting = 0;
Packit Service fb6fa5
      info->status_style_setting = 0;
Packit Service fb6fa5
      info->settings = NULL;
Packit Service fb6fa5
      info->preedit_set = 0;
Packit Service fb6fa5
      info->status_set = 0;
Packit Service fb6fa5
      info->display_closed_cb = 0;
Packit Service fb6fa5
      info->ics = NULL;
Packit Service fb6fa5
      info->reconnecting = FALSE;
Packit Service fb6fa5
      info->im = NULL;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  xim_info_try_im (info);
Packit Service fb6fa5
  return info;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_xim_class_init (GtkIMContextXIMClass *class)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
Packit Service fb6fa5
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Packit Service fb6fa5
Packit Service fb6fa5
  parent_class = g_type_class_peek_parent (class);
Packit Service fb6fa5
Packit Service fb6fa5
  im_context_class->set_client_window = gtk_im_context_xim_set_client_window;
Packit Service fb6fa5
  im_context_class->filter_keypress = gtk_im_context_xim_filter_keypress;
Packit Service fb6fa5
  im_context_class->reset = gtk_im_context_xim_reset;
Packit Service fb6fa5
  im_context_class->get_preedit_string = gtk_im_context_xim_get_preedit_string;
Packit Service fb6fa5
  im_context_class->focus_in = gtk_im_context_xim_focus_in;
Packit Service fb6fa5
  im_context_class->focus_out = gtk_im_context_xim_focus_out;
Packit Service fb6fa5
  im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
Packit Service fb6fa5
  im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
Packit Service fb6fa5
  gobject_class->finalize = gtk_im_context_xim_finalize;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  im_context_xim->use_preedit = TRUE;
Packit Service fb6fa5
  im_context_xim->filter_key_release = FALSE;
Packit Service fb6fa5
  im_context_xim->finalizing = FALSE;
Packit Service fb6fa5
  im_context_xim->has_focus = FALSE;
Packit Service fb6fa5
  im_context_xim->in_toplevel = FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_xim_finalize (GObject *obj)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);
Packit Service fb6fa5
Packit Service fb6fa5
  context_xim->finalizing = TRUE;
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_xim->im_info && !context_xim->im_info->ics->next) 
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (context_xim->im_info->reconnecting)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  GdkDisplay *display;
Packit Service fb6fa5
Packit Service fb6fa5
	  display = gdk_screen_get_display (context_xim->im_info->screen);
Packit Service fb6fa5
	  XUnregisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY (display),
Packit Service fb6fa5
					    NULL, NULL, NULL,
Packit Service fb6fa5
					    xim_instantiate_callback,
Packit Service fb6fa5
					    (XPointer)context_xim->im_info);
Packit Service fb6fa5
	}
Packit Service fb6fa5
      else if (context_xim->im_info->im)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  XIMCallback im_destroy_callback;
Packit Service fb6fa5
Packit Service fb6fa5
	  im_destroy_callback.client_data = NULL;
Packit Service fb6fa5
	  im_destroy_callback.callback = NULL;
Packit Service fb6fa5
	  XSetIMValues (context_xim->im_info->im,
Packit Service fb6fa5
			XNDestroyCallback, &im_destroy_callback,
Packit Service fb6fa5
			NULL);
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  set_ic_client_window (context_xim, NULL);
Packit Service fb6fa5
Packit Service fb6fa5
  g_free (context_xim->locale);
Packit Service fb6fa5
  g_free (context_xim->mb_charset);
Packit Service fb6fa5
Packit Service fb6fa5
  G_OBJECT_CLASS (parent_class)->finalize (obj);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
reinitialize_ic (GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (context_xim->ic)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      XDestroyIC (context_xim->ic);
Packit Service fb6fa5
      context_xim->ic = NULL;
Packit Service fb6fa5
      update_status_window (context_xim);
Packit Service fb6fa5
Packit Service fb6fa5
      if (context_xim->preedit_length)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  context_xim->preedit_length = 0;
Packit Service fb6fa5
	  if (!context_xim->finalizing)
Packit Service fb6fa5
	    g_signal_emit_by_name (context_xim, "preedit-changed");
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
  /* 
Packit Service fb6fa5
     reset filter_key_release flag, otherwise keystrokes will be doubled
Packit Service fb6fa5
     until reconnecting to XIM.
Packit Service fb6fa5
  */
Packit Service fb6fa5
  context_xim->filter_key_release = FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
set_ic_client_window (GtkIMContextXIM *context_xim,
Packit Service fb6fa5
		      GdkWindow       *client_window)
Packit Service fb6fa5
{
Packit Service fb6fa5
  reinitialize_ic (context_xim);
Packit Service fb6fa5
  if (context_xim->client_window)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_xim->im_info->ics = g_slist_remove (context_xim->im_info->ics, context_xim);
Packit Service fb6fa5
      context_xim->im_info = NULL;
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  context_xim->client_window = client_window;
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_xim->client_window)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_xim->im_info = get_im (context_xim->client_window, context_xim->locale);
Packit Service fb6fa5
      context_xim->im_info->ics = g_slist_prepend (context_xim->im_info->ics, context_xim);
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  update_client_widget (context_xim);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_xim_set_client_window (GtkIMContext          *context,
Packit Service fb6fa5
				      GdkWindow             *client_window)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
Packit Service fb6fa5
  set_ic_client_window (context_xim, client_window);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
GtkIMContext *
Packit Service fb6fa5
gtk_im_context_xim_new (void)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *result;
Packit Service fb6fa5
  const gchar *charset;
Packit Service fb6fa5
Packit Service fb6fa5
  result = g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL);
Packit Service fb6fa5
Packit Service fb6fa5
  result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
Packit Service fb6fa5
  
Packit Service fb6fa5
  g_get_charset (&charset);
Packit Service fb6fa5
  result->mb_charset = g_strdup (charset);
Packit Service fb6fa5
Packit Service fb6fa5
  return GTK_IM_CONTEXT (result);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static char *
Packit Service fb6fa5
mb_to_utf8 (GtkIMContextXIM *context_xim,
Packit Service fb6fa5
	    const char      *str)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GError *error = NULL;
Packit Service fb6fa5
  gchar *result;
Packit Service fb6fa5
Packit Service fb6fa5
  if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
Packit Service fb6fa5
    result = g_strdup (str);
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      result = g_convert (str, -1,
Packit Service fb6fa5
			  "UTF-8", context_xim->mb_charset,
Packit Service fb6fa5
			  NULL, NULL, &error);
Packit Service fb6fa5
      if (!result)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  g_warning ("Error converting text from IM to UTF-8: %s\n", error->message);
Packit Service fb6fa5
	  g_error_free (error);
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  return result;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
gtk_im_context_xim_filter_keypress (GtkIMContext *context,
Packit Service fb6fa5
				    GdkEventKey  *event)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
  XIC ic = gtk_im_context_xim_get_ic (context_xim);
Packit Service fb6fa5
  gchar static_buffer[256];
Packit Service fb6fa5
  gchar *buffer = static_buffer;
Packit Service fb6fa5
  gint buffer_size = sizeof(static_buffer) - 1;
Packit Service fb6fa5
  gint num_bytes = 0;
Packit Service fb6fa5
  KeySym keysym;
Packit Service fb6fa5
  Status status;
Packit Service fb6fa5
  gboolean result = FALSE;
Packit Service fb6fa5
  GdkWindow *root_window = gdk_screen_get_root_window (gdk_window_get_screen (event->window));
Packit Service fb6fa5
Packit Service fb6fa5
  XKeyPressedEvent xevent;
Packit Service fb6fa5
Packit Service fb6fa5
  if (event->type == GDK_KEY_RELEASE && !context_xim->filter_key_release)
Packit Service fb6fa5
    return FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
  xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
Packit Service fb6fa5
  xevent.serial = 0;		/* hope it doesn't matter */
Packit Service fb6fa5
  xevent.send_event = event->send_event;
Packit Service fb6fa5
  xevent.display = GDK_DRAWABLE_XDISPLAY (event->window);
Packit Service fb6fa5
  xevent.window = GDK_DRAWABLE_XID (event->window);
Packit Service fb6fa5
  xevent.root = GDK_DRAWABLE_XID (root_window);
Packit Service fb6fa5
  xevent.subwindow = xevent.window;
Packit Service fb6fa5
  xevent.time = event->time;
Packit Service fb6fa5
  xevent.x = xevent.x_root = 0;
Packit Service fb6fa5
  xevent.y = xevent.y_root = 0;
Packit Service fb6fa5
  xevent.state = event->state;
Packit Service fb6fa5
  xevent.keycode = event->hardware_keycode;
Packit Service fb6fa5
  xevent.same_screen = True;
Packit Service fb6fa5
  
Packit Service fb6fa5
  if (XFilterEvent ((XEvent *)&xevent, GDK_DRAWABLE_XID (context_xim->client_window)))
Packit Service fb6fa5
    return TRUE;
Packit Service fb6fa5
  
Packit Service fb6fa5
  if (event->state &
Packit Service fb6fa5
      (gtk_accelerator_get_default_mod_mask () & ~(GDK_SHIFT_MASK | GDK_CONTROL_MASK))) 
Packit Service fb6fa5
    return FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
 again:
Packit Service fb6fa5
  if (ic)
Packit Service fb6fa5
    num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      num_bytes = XLookupString (&xevent, buffer, buffer_size, &keysym, NULL);
Packit Service fb6fa5
      status = XLookupBoth;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (status == XBufferOverflow)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      buffer_size = num_bytes;
Packit Service fb6fa5
      if (buffer != static_buffer) 
Packit Service fb6fa5
	g_free (buffer);
Packit Service fb6fa5
      buffer = g_malloc (num_bytes + 1);
Packit Service fb6fa5
      goto again;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  /* I don't know how we should properly handle XLookupKeysym or XLookupBoth
Packit Service fb6fa5
   * here ... do input methods actually change the keysym? we can't really
Packit Service fb6fa5
   * feed it back to accelerator processing at this point...
Packit Service fb6fa5
   */
Packit Service fb6fa5
  if (status == XLookupChars || status == XLookupBoth)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      char *result_utf8;
Packit Service fb6fa5
Packit Service fb6fa5
      buffer[num_bytes] = '\0';
Packit Service fb6fa5
Packit Service fb6fa5
      result_utf8 = mb_to_utf8 (context_xim, buffer);
Packit Service fb6fa5
      if (result_utf8)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  if ((guchar)result_utf8[0] >= 0x20 &&
Packit Service fb6fa5
	      result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
Packit Service fb6fa5
				       * control characters into strings
Packit Service fb6fa5
				       */
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      g_signal_emit_by_name (context, "commit", result_utf8);
Packit Service fb6fa5
	      result = TRUE;
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	  
Packit Service fb6fa5
	  g_free (result_utf8);
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (buffer != static_buffer) 
Packit Service fb6fa5
    g_free (buffer);
Packit Service fb6fa5
Packit Service fb6fa5
  return result;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_xim_focus_in (GtkIMContext *context)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
Packit Service fb6fa5
  if (!context_xim->has_focus)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      XIC ic = gtk_im_context_xim_get_ic (context_xim);
Packit Service fb6fa5
Packit Service fb6fa5
      context_xim->has_focus = TRUE;
Packit Service fb6fa5
      update_status_window (context_xim);
Packit Service fb6fa5
      
Packit Service fb6fa5
      if (ic)
Packit Service fb6fa5
	XSetICFocus (ic);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_xim_focus_out (GtkIMContext *context)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_xim->has_focus)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      XIC ic = gtk_im_context_xim_get_ic (context_xim);
Packit Service fb6fa5
      
Packit Service fb6fa5
      context_xim->has_focus = FALSE;
Packit Service fb6fa5
      update_status_window (context_xim);
Packit Service fb6fa5
  
Packit Service fb6fa5
      if (ic)
Packit Service fb6fa5
	XUnsetICFocus (ic);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
Packit Service fb6fa5
					GdkRectangle *area)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
  XIC ic = gtk_im_context_xim_get_ic (context_xim);
Packit Service fb6fa5
Packit Service fb6fa5
  XVaNestedList preedit_attr;
Packit Service fb6fa5
  XPoint          spot;
Packit Service fb6fa5
Packit Service fb6fa5
  if (!ic)
Packit Service fb6fa5
    return;
Packit Service fb6fa5
Packit Service fb6fa5
  spot.x = area->x;
Packit Service fb6fa5
  spot.y = area->y + area->height;
Packit Service fb6fa5
Packit Service fb6fa5
  preedit_attr = XVaCreateNestedList (0,
Packit Service fb6fa5
				      XNSpotLocation, &spot,
Packit Service fb6fa5
				      NULL);
Packit Service fb6fa5
  XSetICValues (ic,
Packit Service fb6fa5
		XNPreeditAttributes, preedit_attr,
Packit Service fb6fa5
		NULL);
Packit Service fb6fa5
  XFree(preedit_attr);
Packit Service fb6fa5
Packit Service fb6fa5
  return;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
Packit Service fb6fa5
				    gboolean      use_preedit)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
Packit Service fb6fa5
  use_preedit = use_preedit != FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_xim->use_preedit != use_preedit)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_xim->use_preedit = use_preedit;
Packit Service fb6fa5
      reinitialize_ic (context_xim);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_xim_reset (GtkIMContext *context)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
  XIC ic = gtk_im_context_xim_get_ic (context_xim);
Packit Service fb6fa5
  gchar *result;
Packit Service fb6fa5
Packit Service fb6fa5
  /* restore conversion state after resetting ic later */
Packit Service fb6fa5
  XIMPreeditState preedit_state = XIMPreeditUnKnown;
Packit Service fb6fa5
  XVaNestedList preedit_attr;
Packit Service fb6fa5
  gboolean have_preedit_state = FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
  if (!ic)
Packit Service fb6fa5
    return;
Packit Service fb6fa5
  
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_xim->preedit_length == 0)
Packit Service fb6fa5
    return;
Packit Service fb6fa5
Packit Service fb6fa5
  preedit_attr = XVaCreateNestedList(0,
Packit Service fb6fa5
                                     XNPreeditState, &preedit_state,
Packit Service fb6fa5
                                     NULL);
Packit Service fb6fa5
  if (!XGetICValues(ic,
Packit Service fb6fa5
                    XNPreeditAttributes, preedit_attr,
Packit Service fb6fa5
                    NULL))
Packit Service fb6fa5
    have_preedit_state = TRUE;
Packit Service fb6fa5
Packit Service fb6fa5
  XFree(preedit_attr);
Packit Service fb6fa5
Packit Service fb6fa5
  result = XmbResetIC (ic);
Packit Service fb6fa5
Packit Service fb6fa5
  preedit_attr = XVaCreateNestedList(0,
Packit Service fb6fa5
                                     XNPreeditState, preedit_state,
Packit Service fb6fa5
                                     NULL);
Packit Service fb6fa5
  if (have_preedit_state)
Packit Service fb6fa5
    XSetICValues(ic,
Packit Service fb6fa5
		 XNPreeditAttributes, preedit_attr,
Packit Service fb6fa5
		 NULL);
Packit Service fb6fa5
Packit Service fb6fa5
  XFree(preedit_attr);
Packit Service fb6fa5
Packit Service fb6fa5
  if (result)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      char *result_utf8 = mb_to_utf8 (context_xim, result);
Packit Service fb6fa5
      if (result_utf8)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  g_signal_emit_by_name (context, "commit", result_utf8);
Packit Service fb6fa5
	  g_free (result_utf8);
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_xim->preedit_length)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_xim->preedit_length = 0;
Packit Service fb6fa5
      g_signal_emit_by_name (context, "preedit-changed");
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  XFree (result);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Mask of feedback bits that we render
Packit Service fb6fa5
 */
Packit Service fb6fa5
#define FEEDBACK_MASK (XIMReverse | XIMUnderline)
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
add_feedback_attr (PangoAttrList *attrs,
Packit Service fb6fa5
		   const gchar   *str,
Packit Service fb6fa5
		   XIMFeedback    feedback,
Packit Service fb6fa5
		   gint           start_pos,
Packit Service fb6fa5
		   gint           end_pos)
Packit Service fb6fa5
{
Packit Service fb6fa5
  PangoAttribute *attr;
Packit Service fb6fa5
  
Packit Service fb6fa5
  gint start_index = g_utf8_offset_to_pointer (str, start_pos) - str;
Packit Service fb6fa5
  gint end_index = g_utf8_offset_to_pointer (str, end_pos) - str;
Packit Service fb6fa5
Packit Service fb6fa5
  if (feedback & XIMUnderline)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
Packit Service fb6fa5
      attr->start_index = start_index;
Packit Service fb6fa5
      attr->end_index = end_index;
Packit Service fb6fa5
Packit Service fb6fa5
      pango_attr_list_change (attrs, attr);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (feedback & XIMReverse)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      attr = pango_attr_foreground_new (0xffff, 0xffff, 0xffff);
Packit Service fb6fa5
      attr->start_index = start_index;
Packit Service fb6fa5
      attr->end_index = end_index;
Packit Service fb6fa5
Packit Service fb6fa5
      pango_attr_list_change (attrs, attr);
Packit Service fb6fa5
Packit Service fb6fa5
      attr = pango_attr_background_new (0, 0, 0);
Packit Service fb6fa5
      attr->start_index = start_index;
Packit Service fb6fa5
      attr->end_index = end_index;
Packit Service fb6fa5
Packit Service fb6fa5
      pango_attr_list_change (attrs, attr);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (feedback & ~FEEDBACK_MASK)
Packit Service fb6fa5
    g_warning ("Unrendered feedback style: %#lx", feedback & ~FEEDBACK_MASK);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void     
Packit Service fb6fa5
gtk_im_context_xim_get_preedit_string (GtkIMContext   *context,
Packit Service fb6fa5
				       gchar         **str,
Packit Service fb6fa5
				       PangoAttrList **attrs,
Packit Service fb6fa5
				       gint           *cursor_pos)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
  gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
Packit Service fb6fa5
Packit Service fb6fa5
  if (attrs)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      int i;
Packit Service fb6fa5
      XIMFeedback last_feedback = 0;
Packit Service fb6fa5
      gint start = -1;
Packit Service fb6fa5
      
Packit Service fb6fa5
      *attrs = pango_attr_list_new ();
Packit Service fb6fa5
Packit Service fb6fa5
      for (i = 0; i < context_xim->preedit_length; i++)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  XIMFeedback new_feedback = context_xim->feedbacks[i] & FEEDBACK_MASK;
Packit Service fb6fa5
	  if (new_feedback != last_feedback)
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      if (start >= 0)
Packit Service fb6fa5
		add_feedback_attr (*attrs, utf8, last_feedback, start, i);
Packit Service fb6fa5
	      
Packit Service fb6fa5
	      last_feedback = new_feedback;
Packit Service fb6fa5
	      start = i;
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	}
Packit Service fb6fa5
Packit Service fb6fa5
      if (start >= 0)
Packit Service fb6fa5
	add_feedback_attr (*attrs, utf8, last_feedback, start, i);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (str)
Packit Service fb6fa5
    *str = utf8;
Packit Service fb6fa5
  else
Packit Service fb6fa5
    g_free (utf8);
Packit Service fb6fa5
Packit Service fb6fa5
  if (cursor_pos)
Packit Service fb6fa5
    *cursor_pos = context_xim->preedit_cursor;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static int
Packit Service fb6fa5
preedit_start_callback (XIC      xic,
Packit Service fb6fa5
			XPointer client_data,
Packit Service fb6fa5
			XPointer call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContext *context = GTK_IM_CONTEXT (client_data);
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
  
Packit Service fb6fa5
  if (!context_xim->finalizing)
Packit Service fb6fa5
    g_signal_emit_by_name (context, "preedit-start");
Packit Service fb6fa5
Packit Service fb6fa5
  return -1;			/* No length limit */
Packit Service fb6fa5
}		     
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
preedit_done_callback (XIC      xic,
Packit Service fb6fa5
		     XPointer client_data,
Packit Service fb6fa5
		     XPointer call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContext *context = GTK_IM_CONTEXT (client_data);
Packit Service fb6fa5
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_xim->preedit_length)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_xim->preedit_length = 0;
Packit Service fb6fa5
      if (!context_xim->finalizing)
Packit Service fb6fa5
	g_signal_emit_by_name (context_xim, "preedit-changed");
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (!context_xim->finalizing)
Packit Service fb6fa5
    g_signal_emit_by_name (context, "preedit-end");
Packit Service fb6fa5
}		     
Packit Service fb6fa5
Packit Service fb6fa5
static gint
Packit Service fb6fa5
xim_text_to_utf8 (GtkIMContextXIM *context, XIMText *xim_text, gchar **text)
Packit Service fb6fa5
{
Packit Service fb6fa5
  gint text_length = 0;
Packit Service fb6fa5
  GError *error = NULL;
Packit Service fb6fa5
  gchar *result = NULL;
Packit Service fb6fa5
Packit Service fb6fa5
  if (xim_text && xim_text->string.multi_byte)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (xim_text->encoding_is_wchar)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  g_warning ("Wide character return from Xlib not currently supported");
Packit Service fb6fa5
	  *text = NULL;
Packit Service fb6fa5
	  return 0;
Packit Service fb6fa5
	}
Packit Service fb6fa5
Packit Service fb6fa5
      if (strcmp (context->mb_charset, "UTF-8") == 0)
Packit Service fb6fa5
	result = g_strdup (xim_text->string.multi_byte);
Packit Service fb6fa5
      else
Packit Service fb6fa5
	result = g_convert (xim_text->string.multi_byte,
Packit Service fb6fa5
			    -1,
Packit Service fb6fa5
			    "UTF-8",
Packit Service fb6fa5
			    context->mb_charset,
Packit Service fb6fa5
			    NULL, NULL, &error);
Packit Service fb6fa5
      
Packit Service fb6fa5
      if (result)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  text_length = g_utf8_strlen (result, -1);
Packit Service fb6fa5
	  
Packit Service fb6fa5
	  if (text_length != xim_text->length)
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      g_warning ("Size mismatch when converting text from input method: supplied length = %d\n, result length = %d", xim_text->length, text_length);
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	}
Packit Service fb6fa5
      else
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  g_warning ("Error converting text from IM to UCS-4: %s", error->message);
Packit Service fb6fa5
	  g_error_free (error);
Packit Service fb6fa5
Packit Service fb6fa5
	  *text = NULL;
Packit Service fb6fa5
	  return 0;
Packit Service fb6fa5
	}
Packit Service fb6fa5
Packit Service fb6fa5
      *text = result;
Packit Service fb6fa5
      return text_length;
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      *text = NULL;
Packit Service fb6fa5
      return 0;
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
preedit_draw_callback (XIC                           xic, 
Packit Service fb6fa5
		       XPointer                      client_data,
Packit Service fb6fa5
		       XIMPreeditDrawCallbackStruct *call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
Packit Service fb6fa5
Packit Service fb6fa5
  XIMText *new_xim_text = call_data->text;
Packit Service fb6fa5
  gint new_text_length;
Packit Service fb6fa5
  gunichar *new_text = NULL;
Packit Service fb6fa5
  gint i;
Packit Service fb6fa5
  gint diff;
Packit Service fb6fa5
  gint new_length;
Packit Service fb6fa5
  gchar *tmp;
Packit Service fb6fa5
  
Packit Service fb6fa5
  gint chg_first = CLAMP (call_data->chg_first, 0, context->preedit_length);
Packit Service fb6fa5
  gint chg_length = CLAMP (call_data->chg_length, 0, context->preedit_length - chg_first);
Packit Service fb6fa5
Packit Service fb6fa5
  context->preedit_cursor = call_data->caret;
Packit Service fb6fa5
  
Packit Service fb6fa5
  if (chg_first != call_data->chg_first || chg_length != call_data->chg_length)
Packit Service fb6fa5
    g_warning ("Invalid change to preedit string, first=%d length=%d (orig length == %d)",
Packit Service fb6fa5
	       call_data->chg_first, call_data->chg_length, context->preedit_length);
Packit Service fb6fa5
Packit Service fb6fa5
  new_text_length = xim_text_to_utf8 (context, new_xim_text, &tmp);
Packit Service fb6fa5
  if (tmp)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
Packit Service fb6fa5
      g_free (tmp);
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  diff = new_text_length - chg_length;
Packit Service fb6fa5
  new_length = context->preedit_length + diff;
Packit Service fb6fa5
Packit Service fb6fa5
  if (new_length > context->preedit_size)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context->preedit_size = new_length;
Packit Service fb6fa5
      context->preedit_chars = g_renew (gunichar, context->preedit_chars, new_length);
Packit Service fb6fa5
      context->feedbacks = g_renew (XIMFeedback, context->feedbacks, new_length);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (diff < 0)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      for (i = chg_first + chg_length ; i < context->preedit_length; i++)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  context->preedit_chars[i + diff] = context->preedit_chars[i];
Packit Service fb6fa5
	  context->feedbacks[i + diff] = context->feedbacks[i];
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      for (i = context->preedit_length - 1; i >= chg_first + chg_length ; i--)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  context->preedit_chars[i + diff] = context->preedit_chars[i];
Packit Service fb6fa5
	  context->feedbacks[i + diff] = context->feedbacks[i];
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  for (i = 0; i < new_text_length; i++)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context->preedit_chars[chg_first + i] = new_text[i];
Packit Service fb6fa5
      context->feedbacks[chg_first + i] = new_xim_text->feedback[i];
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  context->preedit_length += diff;
Packit Service fb6fa5
Packit Service fb6fa5
  g_free (new_text);
Packit Service fb6fa5
Packit Service fb6fa5
  if (!context->finalizing)
Packit Service fb6fa5
    g_signal_emit_by_name (context, "preedit-changed");
Packit Service fb6fa5
}
Packit Service fb6fa5
    
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
preedit_caret_callback (XIC                            xic,
Packit Service fb6fa5
			XPointer                       client_data,
Packit Service fb6fa5
			XIMPreeditCaretCallbackStruct *call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
Packit Service fb6fa5
  
Packit Service fb6fa5
  if (call_data->direction == XIMAbsolutePosition)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context->preedit_cursor = call_data->position;
Packit Service fb6fa5
      if (!context->finalizing)
Packit Service fb6fa5
	g_signal_emit_by_name (context, "preedit-changed");
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      g_warning ("Caret movement command: %d %d %d not supported",
Packit Service fb6fa5
		 call_data->position, call_data->direction, call_data->style);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}	     
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
status_start_callback (XIC      xic,
Packit Service fb6fa5
		       XPointer client_data,
Packit Service fb6fa5
		       XPointer call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  return;
Packit Service fb6fa5
} 
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
status_done_callback (XIC      xic,
Packit Service fb6fa5
		      XPointer client_data,
Packit Service fb6fa5
		      XPointer call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  return;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
status_draw_callback (XIC      xic,
Packit Service fb6fa5
		      XPointer client_data,
Packit Service fb6fa5
		      XIMStatusDrawCallbackStruct *call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
Packit Service fb6fa5
Packit Service fb6fa5
  if (call_data->type == XIMTextType)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gchar *text;
Packit Service fb6fa5
      xim_text_to_utf8 (context, call_data->data.text, &text);
Packit Service fb6fa5
Packit Service fb6fa5
      if (context->status_window)
Packit Service fb6fa5
	status_window_set_text (context->status_window, text ? text : "");
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else				/* bitmap */
Packit Service fb6fa5
    {
Packit Service fb6fa5
      g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
string_conversion_callback (XIC xic, XPointer client_data, XPointer call_data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextXIM *context_xim;
Packit Service fb6fa5
  XIMStringConversionCallbackStruct *conv_data;
Packit Service fb6fa5
  gchar *surrounding;
Packit Service fb6fa5
  gint  cursor_index;
Packit Service fb6fa5
Packit Service fb6fa5
  context_xim = (GtkIMContextXIM *)client_data;
Packit Service fb6fa5
  conv_data = (XIMStringConversionCallbackStruct *)call_data;
Packit Service fb6fa5
Packit Service fb6fa5
  if (gtk_im_context_get_surrounding ((GtkIMContext *)context_xim,
Packit Service fb6fa5
                                      &surrounding, &cursor_index))
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gchar *text = NULL;
Packit Service fb6fa5
      gsize text_len = 0;
Packit Service fb6fa5
      gint  subst_offset = 0, subst_nchars = 0;
Packit Service fb6fa5
      gint  i;
Packit Service fb6fa5
      gchar *p = surrounding + cursor_index, *q;
Packit Service fb6fa5
      gshort position = (gshort)conv_data->position;
Packit Service fb6fa5
Packit Service fb6fa5
      if (position > 0)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          for (i = position; i > 0 && *p; --i)
Packit Service fb6fa5
            p = g_utf8_next_char (p);
Packit Service fb6fa5
          if (i > 0)
Packit Service fb6fa5
            return;
Packit Service fb6fa5
        }
Packit Service fb6fa5
      /* According to X11R6.4 Xlib - C Library Reference Manual
Packit Service fb6fa5
       * section 13.5.7.3 String Conversion Callback,
Packit Service fb6fa5
       * XIMStringConversionPosition is starting position _relative_
Packit Service fb6fa5
       * to current client's cursor position. So it should be able
Packit Service fb6fa5
       * to be negative, or referring to a position before the cursor
Packit Service fb6fa5
       * would be impossible. But current X protocol defines this as
Packit Service fb6fa5
       * unsigned short. So, compiler may warn about the value range
Packit Service fb6fa5
       * here. We hope the X protocol is fixed soon.
Packit Service fb6fa5
       */
Packit Service fb6fa5
      else if (position < 0)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          for (i = position; i < 0 && p > surrounding; ++i)
Packit Service fb6fa5
            p = g_utf8_prev_char (p);
Packit Service fb6fa5
          if (i < 0)
Packit Service fb6fa5
            return;
Packit Service fb6fa5
        }
Packit Service fb6fa5
Packit Service fb6fa5
      switch (conv_data->direction)
Packit Service fb6fa5
        {
Packit Service fb6fa5
        case XIMForwardChar:
Packit Service fb6fa5
          for (i = conv_data->factor, q = p; i > 0 && *q; --i)
Packit Service fb6fa5
            q = g_utf8_next_char (q);
Packit Service fb6fa5
          if (i > 0)
Packit Service fb6fa5
            break;
Packit Service fb6fa5
          text = g_locale_from_utf8 (p, q - p, NULL, &text_len, NULL);
Packit Service fb6fa5
          subst_offset = position;
Packit Service fb6fa5
          subst_nchars = conv_data->factor;
Packit Service fb6fa5
          break;
Packit Service fb6fa5
Packit Service fb6fa5
        case XIMBackwardChar:
Packit Service fb6fa5
          for (i = conv_data->factor, q = p; i > 0 && q > surrounding; --i)
Packit Service fb6fa5
            q = g_utf8_prev_char (q);
Packit Service fb6fa5
          if (i > 0)
Packit Service fb6fa5
            break;
Packit Service fb6fa5
          text = g_locale_from_utf8 (q, p - q, NULL, &text_len, NULL);
Packit Service fb6fa5
          subst_offset = position - conv_data->factor;
Packit Service fb6fa5
          subst_nchars = conv_data->factor;
Packit Service fb6fa5
          break;
Packit Service fb6fa5
Packit Service fb6fa5
        case XIMForwardWord:
Packit Service fb6fa5
        case XIMBackwardWord:
Packit Service fb6fa5
        case XIMCaretUp:
Packit Service fb6fa5
        case XIMCaretDown:
Packit Service fb6fa5
        case XIMNextLine:
Packit Service fb6fa5
        case XIMPreviousLine:
Packit Service fb6fa5
        case XIMLineStart:
Packit Service fb6fa5
        case XIMLineEnd:
Packit Service fb6fa5
        case XIMAbsolutePosition:
Packit Service fb6fa5
        case XIMDontChange:
Packit Service fb6fa5
        default:
Packit Service fb6fa5
          break;
Packit Service fb6fa5
        }
Packit Service fb6fa5
      /* block out any failure happenning to "text", including conversion */
Packit Service fb6fa5
      if (text)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          conv_data->text = (XIMStringConversionText *)
Packit Service fb6fa5
                              malloc (sizeof (XIMStringConversionText));
Packit Service fb6fa5
          if (conv_data->text)
Packit Service fb6fa5
            {
Packit Service fb6fa5
              conv_data->text->length = text_len;
Packit Service fb6fa5
              conv_data->text->feedback = NULL;
Packit Service fb6fa5
              conv_data->text->encoding_is_wchar = False;
Packit Service fb6fa5
              conv_data->text->string.mbs = (char *)malloc (text_len);
Packit Service fb6fa5
              if (conv_data->text->string.mbs)
Packit Service fb6fa5
                memcpy (conv_data->text->string.mbs, text, text_len);
Packit Service fb6fa5
              else
Packit Service fb6fa5
                {
Packit Service fb6fa5
                  free (conv_data->text);
Packit Service fb6fa5
                  conv_data->text = NULL;
Packit Service fb6fa5
                }
Packit Service fb6fa5
            }
Packit Service fb6fa5
Packit Service fb6fa5
          g_free (text);
Packit Service fb6fa5
        }
Packit Service fb6fa5
      if (conv_data->operation == XIMStringConversionSubstitution
Packit Service fb6fa5
          && subst_nchars > 0)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          gtk_im_context_delete_surrounding ((GtkIMContext *)context_xim,
Packit Service fb6fa5
                                            subst_offset, subst_nchars);
Packit Service fb6fa5
        }
Packit Service fb6fa5
Packit Service fb6fa5
      g_free (surrounding);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
Packit Service fb6fa5
static XVaNestedList
Packit Service fb6fa5
set_preedit_callback (GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
Packit Service fb6fa5
  context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
Packit Service fb6fa5
  context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
Packit Service fb6fa5
  context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
Packit Service fb6fa5
  context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
Packit Service fb6fa5
  context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
Packit Service fb6fa5
  context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
Packit Service fb6fa5
  context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
Packit Service fb6fa5
  return XVaCreateNestedList (0,
Packit Service fb6fa5
			      XNPreeditStartCallback, &context_xim->preedit_start_callback,
Packit Service fb6fa5
			      XNPreeditDoneCallback, &context_xim->preedit_done_callback,
Packit Service fb6fa5
			      XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
Packit Service fb6fa5
			      XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
Packit Service fb6fa5
			      NULL);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static XVaNestedList
Packit Service fb6fa5
set_status_callback (GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  context_xim->status_start_callback.client_data = (XPointer)context_xim;
Packit Service fb6fa5
  context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
Packit Service fb6fa5
  context_xim->status_done_callback.client_data = (XPointer)context_xim;
Packit Service fb6fa5
  context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
Packit Service fb6fa5
  context_xim->status_draw_callback.client_data = (XPointer)context_xim;
Packit Service fb6fa5
  context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
Packit Service fb6fa5
	  
Packit Service fb6fa5
  return XVaCreateNestedList (0,
Packit Service fb6fa5
			      XNStatusStartCallback, &context_xim->status_start_callback,
Packit Service fb6fa5
			      XNStatusDoneCallback, &context_xim->status_done_callback,
Packit Service fb6fa5
			      XNStatusDrawCallback, &context_xim->status_draw_callback,
Packit Service fb6fa5
			      NULL);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
set_string_conversion_callback (GtkIMContextXIM *context_xim, XIC xic)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (!context_xim->im_info->supports_string_conversion)
Packit Service fb6fa5
    return;
Packit Service fb6fa5
  
Packit Service fb6fa5
  context_xim->string_conversion_callback.client_data = (XPointer)context_xim;
Packit Service fb6fa5
  context_xim->string_conversion_callback.callback = (XIMProc)string_conversion_callback;
Packit Service fb6fa5
  
Packit Service fb6fa5
  XSetICValues (xic,
Packit Service fb6fa5
		XNStringConversionCallback,
Packit Service fb6fa5
		(XPointer)&context_xim->string_conversion_callback,
Packit Service fb6fa5
		NULL);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static XIC
Packit Service fb6fa5
gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (context_xim->im_info == NULL || context_xim->im_info->im == NULL)
Packit Service fb6fa5
    return NULL;
Packit Service fb6fa5
Packit Service fb6fa5
  if (!context_xim->ic)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      const char *name1 = NULL;
Packit Service fb6fa5
      XVaNestedList list1 = NULL;
Packit Service fb6fa5
      const char *name2 = NULL;
Packit Service fb6fa5
      XVaNestedList list2 = NULL;
Packit Service fb6fa5
      XIMStyle im_style = 0;
Packit Service fb6fa5
      XIC xic = NULL;
Packit Service fb6fa5
Packit Service fb6fa5
      if (context_xim->use_preedit &&
Packit Service fb6fa5
	  (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  im_style |= XIMPreeditCallbacks;
Packit Service fb6fa5
	  name1 = XNPreeditAttributes;
Packit Service fb6fa5
	  list1 = set_preedit_callback (context_xim);
Packit Service fb6fa5
	}
Packit Service fb6fa5
      else if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditNone)
Packit Service fb6fa5
	im_style |= XIMPreeditNone;
Packit Service fb6fa5
      else
Packit Service fb6fa5
	im_style |= XIMPreeditNothing;
Packit Service fb6fa5
Packit Service fb6fa5
      if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  im_style |= XIMStatusCallbacks;
Packit Service fb6fa5
	  if (name1 == NULL)
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      name1 = XNStatusAttributes;
Packit Service fb6fa5
	      list1 = set_status_callback (context_xim);
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	  else
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      name2 = XNStatusAttributes;
Packit Service fb6fa5
	      list2 = set_status_callback (context_xim);
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	}
Packit Service fb6fa5
      else if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusNone)
Packit Service fb6fa5
	im_style |= XIMStatusNone;
Packit Service fb6fa5
      else
Packit Service fb6fa5
	im_style |= XIMStatusNothing;
Packit Service fb6fa5
Packit Service fb6fa5
      xic = XCreateIC (context_xim->im_info->im,
Packit Service fb6fa5
		       XNInputStyle, im_style,
Packit Service fb6fa5
		       XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
Packit Service fb6fa5
		       name1, list1,
Packit Service fb6fa5
		       name2, list2,
Packit Service fb6fa5
		       NULL);
Packit Service fb6fa5
      if (list1)
Packit Service fb6fa5
	XFree (list1);
Packit Service fb6fa5
      if (list2)
Packit Service fb6fa5
	XFree (list2);
Packit Service fb6fa5
Packit Service fb6fa5
      if (xic)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  /* Don't filter key released events with XFilterEvents unless
Packit Service fb6fa5
	   * input methods ask for. This is a workaround for Solaris input
Packit Service fb6fa5
	   * method bug in C and European locales. It doubles each key
Packit Service fb6fa5
	   * stroke if both key pressed and released events are filtered.
Packit Service fb6fa5
	   * (bugzilla #81759)
Packit Service fb6fa5
	   */
Packit Service fb6fa5
	  gulong mask = 0xaaaaaaaa;
Packit Service fb6fa5
	  XGetICValues (xic,
Packit Service fb6fa5
			XNFilterEvents, &mask,
Packit Service fb6fa5
			NULL);
Packit Service fb6fa5
	  context_xim->filter_key_release = (mask & KeyReleaseMask) != 0;
Packit Service fb6fa5
	  set_string_conversion_callback (context_xim, xic);
Packit Service fb6fa5
	}
Packit Service fb6fa5
      
Packit Service fb6fa5
      context_xim->ic = xic;
Packit Service fb6fa5
Packit Service fb6fa5
      update_status_window (context_xim);
Packit Service fb6fa5
      
Packit Service fb6fa5
      if (xic && context_xim->has_focus)
Packit Service fb6fa5
	XSetICFocus (xic);
Packit Service fb6fa5
    }
Packit Service fb6fa5
  return context_xim->ic;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/*****************************************************************
Packit Service fb6fa5
 * Status Window handling
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * A status window is a small window attached to the toplevel
Packit Service fb6fa5
 * that is used to display information to the user about the
Packit Service fb6fa5
 * current input operation.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * We claim the toplevel's status window for an input context if:
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * A) The input context has a toplevel
Packit Service fb6fa5
 * B) The input context has the focus
Packit Service fb6fa5
 * C) The input context has an XIC associated with it
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * Tracking A) and C) is pretty reliable since we
Packit Service fb6fa5
 * compute A) and create the XIC for C) ourselves.
Packit Service fb6fa5
 * For B) we basically have to depend on our callers
Packit Service fb6fa5
 * calling ::focus-in and ::focus-out at the right time.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * The toplevel is computed by walking up the GdkWindow
Packit Service fb6fa5
 * hierarchy from context->client_window until we find a
Packit Service fb6fa5
 * window that is owned by some widget, and then calling
Packit Service fb6fa5
 * gtk_widget_get_toplevel() on that widget. This should
Packit Service fb6fa5
 * handle both cases where we might have GdkWindows without widgets,
Packit Service fb6fa5
 * and cases where GtkWidgets have strange window hierarchies
Packit Service fb6fa5
 * (like a torn off GtkHandleBox.)
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * The status window is visible if and only if there is text
Packit Service fb6fa5
 * for it; whenever a new GtkIMContextXIM claims the status
Packit Service fb6fa5
 * window, we blank out any existing text. We actually only
Packit Service fb6fa5
 * create a GtkWindow for the status window the first time
Packit Service fb6fa5
 * it is shown; this is an important optimization when we are
Packit Service fb6fa5
 * using XIM with something like a simple compose-key input
Packit Service fb6fa5
 * method that never needs a status window.
Packit Service fb6fa5
 *****************************************************************/
Packit Service fb6fa5
Packit Service fb6fa5
/* Called when we no longer need a status window
Packit Service fb6fa5
*/
Packit Service fb6fa5
static void
Packit Service fb6fa5
disclaim_status_window (GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (context_xim->status_window)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      g_assert (context_xim->status_window->context == context_xim);
Packit Service fb6fa5
Packit Service fb6fa5
      status_window_set_text (context_xim->status_window, "");
Packit Service fb6fa5
      
Packit Service fb6fa5
      context_xim->status_window->context = NULL;
Packit Service fb6fa5
      context_xim->status_window = NULL;
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Called when we need a status window
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
claim_status_window (GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (!context_xim->status_window && context_xim->client_widget)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
Packit Service fb6fa5
      if (toplevel && gtk_widget_is_toplevel (toplevel))
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  StatusWindow *status_window = status_window_get (toplevel);
Packit Service fb6fa5
Packit Service fb6fa5
	  if (status_window->context)
Packit Service fb6fa5
	    disclaim_status_window (status_window->context);
Packit Service fb6fa5
Packit Service fb6fa5
	  status_window->context = context_xim;
Packit Service fb6fa5
	  context_xim->status_window = status_window;
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Basic call made whenever something changed that might cause
Packit Service fb6fa5
 * us to need, or not to need a status window.
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
update_status_window (GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (context_xim->ic && context_xim->in_toplevel && context_xim->has_focus)
Packit Service fb6fa5
    claim_status_window (context_xim);
Packit Service fb6fa5
  else
Packit Service fb6fa5
    disclaim_status_window (context_xim);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Updates the in_toplevel flag for @context_xim
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
update_in_toplevel (GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (context_xim->client_widget)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
Packit Service fb6fa5
      
Packit Service fb6fa5
      context_xim->in_toplevel = (toplevel && gtk_widget_is_toplevel (toplevel));
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    context_xim->in_toplevel = FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
  /* Some paranoia, in case we don't get a focus out */
Packit Service fb6fa5
  if (!context_xim->in_toplevel)
Packit Service fb6fa5
    context_xim->has_focus = FALSE;
Packit Service fb6fa5
  
Packit Service fb6fa5
  update_status_window (context_xim);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Callback when @widget's toplevel changes. It will always
Packit Service fb6fa5
 * change from NULL to a window, or a window to NULL;
Packit Service fb6fa5
 * we use that intermediate NULL state to make sure
Packit Service fb6fa5
 * that we disclaim the toplevel status window for the old
Packit Service fb6fa5
 * window.
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
on_client_widget_hierarchy_changed (GtkWidget       *widget,
Packit Service fb6fa5
				    GtkWidget       *old_toplevel,
Packit Service fb6fa5
				    GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  update_in_toplevel (context_xim);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Finds the GtkWidget that owns the window, or if none, the
Packit Service fb6fa5
 * widget owning the nearest parent that has a widget.
Packit Service fb6fa5
 */
Packit Service fb6fa5
static GtkWidget *
Packit Service fb6fa5
widget_for_window (GdkWindow *window)
Packit Service fb6fa5
{
Packit Service fb6fa5
  while (window)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gpointer user_data;
Packit Service fb6fa5
      gdk_window_get_user_data (window, &user_data);
Packit Service fb6fa5
      if (user_data)
Packit Service fb6fa5
	return user_data;
Packit Service fb6fa5
Packit Service fb6fa5
      window = gdk_window_get_parent (window);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return NULL;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Called when context_xim->client_window changes; takes care of
Packit Service fb6fa5
 * removing and/or setting up our watches for the toplevel
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
update_client_widget (GtkIMContextXIM *context_xim)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkWidget *new_client_widget = widget_for_window (context_xim->client_window);
Packit Service fb6fa5
Packit Service fb6fa5
  if (new_client_widget != context_xim->client_widget)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (context_xim->client_widget)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  g_signal_handlers_disconnect_by_func (context_xim->client_widget,
Packit Service fb6fa5
						G_CALLBACK (on_client_widget_hierarchy_changed),
Packit Service fb6fa5
						context_xim);
Packit Service fb6fa5
	}
Packit Service fb6fa5
      context_xim->client_widget = new_client_widget;
Packit Service fb6fa5
      if (context_xim->client_widget)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  g_signal_connect (context_xim->client_widget, "hierarchy-changed",
Packit Service fb6fa5
			    G_CALLBACK (on_client_widget_hierarchy_changed),
Packit Service fb6fa5
			    context_xim);
Packit Service fb6fa5
	}
Packit Service fb6fa5
Packit Service fb6fa5
      update_in_toplevel (context_xim);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Called when the toplevel is destroyed; frees the status window
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
on_status_toplevel_destroy (GtkWidget    *toplevel,
Packit Service fb6fa5
			    StatusWindow *status_window)
Packit Service fb6fa5
{
Packit Service fb6fa5
  status_window_free (status_window);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Called when the screen for the toplevel changes; updates the
Packit Service fb6fa5
 * screen for the status window to match.
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
on_status_toplevel_notify_screen (GtkWindow    *toplevel,
Packit Service fb6fa5
				  GParamSpec   *pspec,
Packit Service fb6fa5
				  StatusWindow *status_window)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (status_window->window)
Packit Service fb6fa5
    gtk_window_set_screen (GTK_WINDOW (status_window->window),
Packit Service fb6fa5
			   gtk_widget_get_screen (GTK_WIDGET (toplevel)));
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Called when the toplevel window is moved; updates the position of
Packit Service fb6fa5
 * the status window to follow it.
Packit Service fb6fa5
 */
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
on_status_toplevel_configure (GtkWidget         *toplevel,
Packit Service fb6fa5
			      GdkEventConfigure *event,
Packit Service fb6fa5
			      StatusWindow      *status_window)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GdkRectangle rect;
Packit Service fb6fa5
  GtkRequisition requisition;
Packit Service fb6fa5
  gint y;
Packit Service fb6fa5
  gint height;
Packit Service fb6fa5
Packit Service fb6fa5
  if (status_window->window)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
Packit Service fb6fa5
  
Packit Service fb6fa5
      gdk_window_get_frame_extents (toplevel->window, &rect);
Packit Service fb6fa5
      gtk_widget_size_request (status_window->window, &requisition);
Packit Service fb6fa5
      
Packit Service fb6fa5
      if (rect.y + rect.height + requisition.height < height)
Packit Service fb6fa5
	y = rect.y + rect.height;
Packit Service fb6fa5
      else
Packit Service fb6fa5
	y = height - requisition.height;
Packit Service fb6fa5
      
Packit Service fb6fa5
      gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Frees a status window and removes its link from the status_windows list
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
status_window_free (StatusWindow *status_window)
Packit Service fb6fa5
{
Packit Service fb6fa5
  status_windows = g_slist_remove (status_windows, status_window);
Packit Service fb6fa5
Packit Service fb6fa5
  if (status_window->context)
Packit Service fb6fa5
    status_window->context->status_window = NULL;
Packit Service fb6fa5
 
Packit Service fb6fa5
  g_signal_handlers_disconnect_by_func (status_window->toplevel,
Packit Service fb6fa5
					G_CALLBACK (on_status_toplevel_destroy),
Packit Service fb6fa5
					status_window);
Packit Service fb6fa5
  g_signal_handlers_disconnect_by_func (status_window->toplevel,
Packit Service fb6fa5
					G_CALLBACK (on_status_toplevel_notify_screen),
Packit Service fb6fa5
					status_window);
Packit Service fb6fa5
  g_signal_handlers_disconnect_by_func (status_window->toplevel,
Packit Service fb6fa5
					G_CALLBACK (on_status_toplevel_configure),
Packit Service fb6fa5
					status_window);
Packit Service fb6fa5
Packit Service fb6fa5
  if (status_window->window)
Packit Service fb6fa5
    gtk_widget_destroy (status_window->window);
Packit Service fb6fa5
  
Packit Service fb6fa5
  g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
Packit Service fb6fa5
 
Packit Service fb6fa5
  g_free (status_window);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Finds the status window object for a toplevel, creating it if necessary.
Packit Service fb6fa5
 */
Packit Service fb6fa5
static StatusWindow *
Packit Service fb6fa5
status_window_get (GtkWidget *toplevel)
Packit Service fb6fa5
{
Packit Service fb6fa5
  StatusWindow *status_window;
Packit Service fb6fa5
Packit Service fb6fa5
  status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
Packit Service fb6fa5
  if (status_window)
Packit Service fb6fa5
    return status_window;
Packit Service fb6fa5
  
Packit Service fb6fa5
  status_window = g_new0 (StatusWindow, 1);
Packit Service fb6fa5
  status_window->toplevel = toplevel;
Packit Service fb6fa5
Packit Service fb6fa5
  status_windows = g_slist_prepend (status_windows, status_window);
Packit Service fb6fa5
Packit Service fb6fa5
  g_signal_connect (toplevel, "destroy",
Packit Service fb6fa5
		    G_CALLBACK (on_status_toplevel_destroy),
Packit Service fb6fa5
		    status_window);
Packit Service fb6fa5
  g_signal_connect (toplevel, "configure-event",
Packit Service fb6fa5
		    G_CALLBACK (on_status_toplevel_configure),
Packit Service fb6fa5
		    status_window);
Packit Service fb6fa5
  g_signal_connect (toplevel, "notify::screen",
Packit Service fb6fa5
		    G_CALLBACK (on_status_toplevel_notify_screen),
Packit Service fb6fa5
		    status_window);
Packit Service fb6fa5
  
Packit Service fb6fa5
  g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);
Packit Service fb6fa5
Packit Service fb6fa5
  return status_window;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Creates the widgets for the status window; called when we
Packit Service fb6fa5
 * first need to show text for the status window.
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
status_window_make_window (StatusWindow *status_window)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkWidget *window;
Packit Service fb6fa5
  GtkWidget *status_label;
Packit Service fb6fa5
  
Packit Service fb6fa5
  status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
Packit Service fb6fa5
  window = status_window->window;
Packit Service fb6fa5
Packit Service fb6fa5
  gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
Packit Service fb6fa5
Packit Service fb6fa5
  status_label = gtk_label_new ("");
Packit Service fb6fa5
  gtk_misc_set_padding (GTK_MISC (status_label), 1, 1);
Packit Service fb6fa5
  gtk_widget_show (status_label);
Packit Service fb6fa5
  
Packit Service fb6fa5
  gtk_container_add (GTK_CONTAINER (window), status_label);
Packit Service fb6fa5
  
Packit Service fb6fa5
  gtk_window_set_screen (GTK_WINDOW (status_window->window),
Packit Service fb6fa5
			 gtk_widget_get_screen (status_window->toplevel));
Packit Service fb6fa5
Packit Service fb6fa5
  on_status_toplevel_configure (status_window->toplevel, NULL, status_window);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Updates the text in the status window, hiding or
Packit Service fb6fa5
 * showing the window as necessary.
Packit Service fb6fa5
 */
Packit Service fb6fa5
static void
Packit Service fb6fa5
status_window_set_text (StatusWindow *status_window,
Packit Service fb6fa5
			const gchar  *text)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (text[0])
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GtkWidget *label;
Packit Service fb6fa5
      
Packit Service fb6fa5
      if (!status_window->window)
Packit Service fb6fa5
	status_window_make_window (status_window);
Packit Service fb6fa5
      
Packit Service fb6fa5
      label = GTK_BIN (status_window->window)->child;
Packit Service fb6fa5
      gtk_label_set_text (GTK_LABEL (label), text);
Packit Service fb6fa5
  
Packit Service fb6fa5
      gtk_widget_show (status_window->window);
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (status_window->window)
Packit Service fb6fa5
	gtk_widget_hide (status_window->window);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/**
Packit Service fb6fa5
 * gtk_im_context_xim_shutdown:
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Destroys all the status windows that are kept by the XIM contexts.  This
Packit Service fb6fa5
 * function should only be called by the XIM module exit routine.
Packit Service fb6fa5
 **/
Packit Service fb6fa5
void
Packit Service fb6fa5
gtk_im_context_xim_shutdown (void)
Packit Service fb6fa5
{
Packit Service fb6fa5
  while (status_windows)
Packit Service fb6fa5
    status_window_free (status_windows->data);
Packit Service fb6fa5
Packit Service fb6fa5
  while (open_ims)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GtkXIMInfo *info = open_ims->data;
Packit Service fb6fa5
      GdkDisplay *display = gdk_screen_get_display (info->screen);
Packit Service fb6fa5
Packit Service fb6fa5
      xim_info_display_closed (display, FALSE, info);
Packit Service fb6fa5
      open_ims = g_slist_remove_link (open_ims, open_ims);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}