gtkimcontextxim.c 48.4 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14
 * Copyright (C) 2000 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17
 */

18
#include "config.h"
19 20
#include "locale.h"
#include <string.h>
21
#include <stdlib.h>
22 23 24

#include "gtkimcontextxim.h"

25 26
#include "gtk/gtkintl.h"

27
typedef struct _StatusWindow StatusWindow;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
typedef struct _GtkXIMInfo GtkXIMInfo;

struct _GtkIMContextXIM
{
  GtkIMContext object;

  GtkXIMInfo *im_info;

  gchar *locale;
  gchar *mb_charset;

  GdkWindow *client_window;
  GtkWidget *client_widget;

  /* The status window for this input context; we claim the
   * status window when we are focused and have created an XIC
   */
  StatusWindow *status_window;

  gint preedit_size;
  gint preedit_length;
  gunichar *preedit_chars;
  XIMFeedback *feedbacks;

  gint preedit_cursor;
  
  XIMCallback preedit_start_callback;
  XIMCallback preedit_done_callback;
  XIMCallback preedit_draw_callback;
  XIMCallback preedit_caret_callback;

  XIMCallback status_start_callback;
  XIMCallback status_done_callback;
  XIMCallback status_draw_callback;

63
  XIMCallback string_conversion_callback;
64

65 66 67 68 69 70 71 72
  XIC ic;

  guint filter_key_release : 1;
  guint use_preedit : 1;
  guint finalizing : 1;
  guint in_toplevel : 1;
  guint has_focus : 1;
};
73

74 75
struct _GtkXIMInfo
{
76
  GdkScreen *screen;
77 78
  XIM im;
  char *locale;
79 80
  XIMStyle preedit_style_setting;
  XIMStyle status_style_setting;
81
  XIMStyle style;
82 83 84
  GtkSettings *settings;
  gulong status_set;
  gulong preedit_set;
85
  gulong display_closed_cb;
86
  XIMStyles *xim_styles;
87
  GSList *ics;
88 89

  guint reconnecting :1;
90
  guint supports_string_conversion;
91 92
};

93 94 95 96 97 98 99
/* A context status window; these are kept in the status_windows list. */
struct _StatusWindow
{
  GtkWidget *window;
  
  /* Toplevel window to which the status window corresponds */
  GtkWidget *toplevel;
100 101 102

  /* Currently focused GtkIMContextXIM for the toplevel, if any */
  GtkIMContextXIM *context;
103 104
};

105 106 107 108 109 110 111 112
static void     gtk_im_context_xim_class_init         (GtkIMContextXIMClass  *class);
static void     gtk_im_context_xim_init               (GtkIMContextXIM       *im_context_xim);
static void     gtk_im_context_xim_finalize           (GObject               *obj);
static void     gtk_im_context_xim_set_client_window  (GtkIMContext          *context,
						       GdkWindow             *client_window);
static gboolean gtk_im_context_xim_filter_keypress    (GtkIMContext          *context,
						       GdkEventKey           *key);
static void     gtk_im_context_xim_reset              (GtkIMContext          *context);
113 114
static void     gtk_im_context_xim_focus_in           (GtkIMContext          *context);
static void     gtk_im_context_xim_focus_out          (GtkIMContext          *context);
115
static void     gtk_im_context_xim_set_cursor_location (GtkIMContext          *context,
116
						       GdkRectangle		*area);
117 118
static void     gtk_im_context_xim_set_use_preedit    (GtkIMContext          *context,
						       gboolean		      use_preedit);
119 120 121 122 123
static void     gtk_im_context_xim_get_preedit_string (GtkIMContext          *context,
						       gchar                **str,
						       PangoAttrList        **attrs,
						       gint                  *cursor_pos);

124
static void reinitialize_ic      (GtkIMContextXIM *context_xim);
125
static void set_ic_client_window (GtkIMContextXIM *context_xim,
126
				  GdkWindow       *client_window);
127

128 129
static void setup_styles (GtkXIMInfo *info);

130 131 132 133 134 135 136
static void update_client_widget   (GtkIMContextXIM *context_xim);
static void update_status_window   (GtkIMContextXIM *context_xim);

static StatusWindow *status_window_get      (GtkWidget    *toplevel);
static void          status_window_free     (StatusWindow *status_window);
static void          status_window_set_text (StatusWindow *status_window,
					     const gchar  *text);
137

138 139 140 141
static void xim_destroy_callback   (XIM      xim,
				    XPointer client_data,
				    XPointer call_data);

142
static XIC       gtk_im_context_xim_get_ic            (GtkIMContextXIM *context_xim);
143 144 145 146
static void           xim_info_display_closed (GdkDisplay *display,
			                       gboolean    is_error,
			                       GtkXIMInfo *info);

147 148 149 150
static GObjectClass *parent_class;

GType gtk_type_im_context_xim = 0;

Matthias Clasen's avatar
Matthias Clasen committed
151
static GSList *open_ims = NULL;
152

153 154 155
/* List of status windows for different toplevels */
static GSList *status_windows = NULL;

156 157 158
void
gtk_im_context_xim_register_type (GTypeModule *type_module)
{
159
  const GTypeInfo im_context_xim_info =
160 161 162 163 164 165 166 167 168
  {
    sizeof (GtkIMContextXIMClass),
    (GBaseInitFunc) NULL,
    (GBaseFinalizeFunc) NULL,
    (GClassInitFunc) gtk_im_context_xim_class_init,
    NULL,           /* class_finalize */    
    NULL,           /* class_data */
    sizeof (GtkIMContextXIM),
    0,
Manish Singh's avatar
Manish Singh committed
169
    (GInstanceInitFunc) gtk_im_context_xim_init,
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  };

  gtk_type_im_context_xim = 
    g_type_module_register_type (type_module,
				 GTK_TYPE_IM_CONTEXT,
				 "GtkIMContextXIM",
				 &im_context_xim_info, 0);
}

#define PREEDIT_MASK (XIMPreeditCallbacks | XIMPreeditPosition | \
		      XIMPreeditArea | XIMPreeditNothing | XIMPreeditNone)
#define STATUS_MASK (XIMStatusCallbacks | XIMStatusArea | \
		      XIMStatusNothing | XIMStatusNone)
#define ALLOWED_MASK (XIMPreeditCallbacks | XIMPreeditNothing | XIMPreeditNone | \
		      XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone)

static XIMStyle 
choose_better_style (XIMStyle style1, XIMStyle style2) 
{
  XIMStyle s1, s2, u; 
  
  if (style1 == 0) return style2;
  if (style2 == 0) return style1;
  if ((style1 & (PREEDIT_MASK | STATUS_MASK))
    	== (style2 & (PREEDIT_MASK | STATUS_MASK)))
    return style1;

  s1 = style1 & PREEDIT_MASK;
  s2 = style2 & PREEDIT_MASK;
  u = s1 | s2;
  if (s1 != s2) {
    if (u & XIMPreeditCallbacks)
      return (s1 == XIMPreeditCallbacks) ? style1 : style2;
    else if (u & XIMPreeditPosition)
      return (s1 == XIMPreeditPosition) ? style1 :style2;
    else if (u & XIMPreeditArea)
      return (s1 == XIMPreeditArea) ? style1 : style2;
    else if (u & XIMPreeditNothing)
      return (s1 == XIMPreeditNothing) ? style1 : style2;
209 210
    else if (u & XIMPreeditNone)
      return (s1 == XIMPreeditNone) ? style1 : style2;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  } else {
    s1 = style1 & STATUS_MASK;
    s2 = style2 & STATUS_MASK;
    u = s1 | s2;
    if (u & XIMStatusCallbacks)
      return (s1 == XIMStatusCallbacks) ? style1 : style2;
    else if (u & XIMStatusArea)
      return (s1 == XIMStatusArea) ? style1 : style2;
    else if (u & XIMStatusNothing)
      return (s1 == XIMStatusNothing) ? style1 : style2;
    else if (u & XIMStatusNone)
      return (s1 == XIMStatusNone) ? style1 : style2;
  }
  return 0; /* Get rid of stupid warning */
}

227 228 229 230 231 232
static void
reinitialize_all_ics (GtkXIMInfo *info)
{
  GSList *tmp_list;

  for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
233
    reinitialize_ic (tmp_list->data);
234 235
}

236 237 238 239 240 241 242 243
static void
setup_styles (GtkXIMInfo *info)
{
  int i;
  unsigned long settings_preference;
  XIMStyles *xim_styles = info->xim_styles;

  settings_preference = info->status_style_setting|info->preedit_style_setting;
244
  info->style = 0;
245 246 247 248
  if (xim_styles)
    {
      for (i = 0; i < xim_styles->count_styles; i++)
	if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
249
	  {
250
	    if (settings_preference == xim_styles->supported_styles[i])
251
	      {
252
		info->style = settings_preference;
253 254 255 256 257
		break;
	      }
	    info->style = choose_better_style (info->style,
					       xim_styles->supported_styles[i]);
	  }
258
    }
259 260
  if (info->style == 0)
    info->style = XIMPreeditNothing | XIMStatusNothing;
261 262 263
}

static void
264
setup_im (GtkXIMInfo *info)
265 266
{
  XIMValuesList *ic_values = NULL;
267
  XIMCallback im_destroy_callback;
268
  GdkDisplay *display;
269

270 271 272
  if (info->im == NULL)
    return;

273 274 275 276 277 278
  im_destroy_callback.client_data = (XPointer)info;
  im_destroy_callback.callback = (XIMProc)xim_destroy_callback;
  XSetIMValues (info->im,
		XNDestroyCallback, &im_destroy_callback,
		NULL);

279 280 281 282
  XGetIMValues (info->im,
		XNQueryInputStyle, &info->xim_styles,
		XNQueryICValuesList, &ic_values,
		NULL);
283

284
  info->supports_string_conversion = FALSE;
285 286
  if (ic_values)
    {
287 288 289 290 291 292 293 294 295 296 297
      int i;
      
      for (i = 0; i < ic_values->count_values; i++)
	if (strcmp (ic_values->supported_values[i],
		    XNStringConversionCallback) == 0)
	  {
	    info->supports_string_conversion = TRUE;
	    break;
	  }

#if 0
298 299 300 301
      for (i = 0; i < ic_values->count_values; i++)
	g_print ("%s\n", ic_values->supported_values[i]);
      for (i = 0; i < xim_styles->count_styles; i++)
	g_print ("%#x\n", xim_styles->supported_styles[i]);
302
#endif
303 304 305
      
      XFree (ic_values);
    }
306

307 308 309 310
  info->status_style_setting = XIMStatusCallbacks;
  info->preedit_style_setting = XIMPreeditCallbacks;
  setup_styles (info);
  reinitialize_all_ics (info);
311 312 313 314

  display = gdk_screen_get_display (info->screen);
  info->display_closed_cb = g_signal_connect (display, "closed",
	                                      G_CALLBACK (xim_info_display_closed), info);
315 316
}

317 318 319 320 321 322 323 324 325 326 327 328 329
static void
xim_info_display_closed (GdkDisplay *display,
			 gboolean    is_error,
			 GtkXIMInfo *info)
{
  GSList *ics, *tmp_list;

  open_ims = g_slist_remove (open_ims, info);

  ics = info->ics;
  info->ics = NULL;

  for (tmp_list = ics; tmp_list; tmp_list = tmp_list->next)
330
    set_ic_client_window (tmp_list->data, NULL);
331

332
  g_slist_free (ics);
333 334 335 336 337 338 339 340 341 342

  if (info->status_set)
    g_signal_handler_disconnect (info->settings, info->status_set);
  if (info->preedit_set)
    g_signal_handler_disconnect (info->settings, info->preedit_set);
  if (info->display_closed_cb)
    g_signal_handler_disconnect (display, info->display_closed_cb);

  if (info->xim_styles)
    XFree (info->xim_styles);
343
  g_free (info->locale);
344 345 346

  if (info->im)
    XCloseIM (info->im);
347 348 349 350

  g_free (info);
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
static void
xim_instantiate_callback (Display *display, XPointer client_data,
			  XPointer call_data)
{
  GtkXIMInfo *info = (GtkXIMInfo*)client_data;
  XIM im = NULL;

  im = XOpenIM (display, NULL, NULL, NULL);

  if (!im)
    return;

  info->im = im;
  setup_im (info);

  XUnregisterIMInstantiateCallback (display, NULL, NULL, NULL,
				    xim_instantiate_callback,
				    (XPointer)info);
  info->reconnecting = FALSE;
}

/* initialize info->im */
static void
xim_info_try_im (GtkXIMInfo *info)
{
  GdkScreen *screen = info->screen;
  GdkDisplay *display = gdk_screen_get_display (screen);

  g_assert (info->im == NULL);
  if (info->reconnecting)
    return;

  if (XSupportsLocale ())
    {
      if (!XSetLocaleModifiers (""))
	g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
      info->im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
      if (!info->im)
	{
	  XRegisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY(display),
					  NULL, NULL, NULL,
					  xim_instantiate_callback,
					  (XPointer)info);
	  info->reconnecting = TRUE;
	  return;
	}
      setup_im (info);
    }
}

static void
xim_destroy_callback (XIM      xim,
		      XPointer client_data,
		      XPointer call_data)
{
  GtkXIMInfo *info = (GtkXIMInfo*)client_data;

  info->im = NULL;

  g_signal_handler_disconnect (info->settings, info->status_set);
411
  info->status_set = 0;
412
  g_signal_handler_disconnect (info->settings, info->preedit_set);
413
  info->preedit_set = 0;
414 415 416 417 418 419

  reinitialize_all_ics (info);
  xim_info_try_im (info);
  return;
} 

420
static GtkXIMInfo *
421
get_im (GdkWindow *client_window,
422
	const char *locale)
423
{
424
  GSList *tmp_list;
425
  GtkXIMInfo *info;
426
  GdkScreen *screen = gdk_window_get_screen (client_window);
427

428
  info = NULL;
429
  tmp_list = open_ims;
430 431
  while (tmp_list)
    {
432 433 434
      GtkXIMInfo *tmp_info = tmp_list->data;
      if (tmp_info->screen == screen &&
	  strcmp (tmp_info->locale, locale) == 0)
435
	{
436 437 438 439
	  if (tmp_info->im)
	    {
	      return tmp_info;
	    }
440
	  else
441 442 443 444
	    {
	      tmp_info = tmp_info;
	      break;
	    }
445
	}
446 447 448
      tmp_list = tmp_list->next;
    }

449
  if (info == NULL)
450
    {
451 452 453 454 455 456 457 458 459 460 461
      info = g_new (GtkXIMInfo, 1);
      open_ims = g_slist_prepend (open_ims, info);

      info->screen = screen;
      info->locale = g_strdup (locale);
      info->xim_styles = NULL;
      info->preedit_style_setting = 0;
      info->status_style_setting = 0;
      info->settings = NULL;
      info->preedit_set = 0;
      info->status_set = 0;
462
      info->display_closed_cb = 0;
463
      info->ics = NULL;
464 465
      info->reconnecting = FALSE;
      info->im = NULL;
466 467
    }

468
  xim_info_try_im (info);
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
  return info;
}

static void
gtk_im_context_xim_class_init (GtkIMContextXIMClass *class)
{
  GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

  parent_class = g_type_class_peek_parent (class);

  im_context_class->set_client_window = gtk_im_context_xim_set_client_window;
  im_context_class->filter_keypress = gtk_im_context_xim_filter_keypress;
  im_context_class->reset = gtk_im_context_xim_reset;
  im_context_class->get_preedit_string = gtk_im_context_xim_get_preedit_string;
484 485
  im_context_class->focus_in = gtk_im_context_xim_focus_in;
  im_context_class->focus_out = gtk_im_context_xim_focus_out;
486
  im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
487
  im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
488 489 490 491 492 493
  gobject_class->finalize = gtk_im_context_xim_finalize;
}

static void
gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
{
494
  im_context_xim->use_preedit = TRUE;
495
  im_context_xim->filter_key_release = FALSE;
496
  im_context_xim->finalizing = FALSE;
497 498
  im_context_xim->has_focus = FALSE;
  im_context_xim->in_toplevel = FALSE;
499 500 501 502 503 504 505
}

static void
gtk_im_context_xim_finalize (GObject *obj)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);

506 507
  context_xim->finalizing = TRUE;

508
  if (context_xim->im_info && !context_xim->im_info->ics->next) 
509
    {
510 511 512 513 514 515 516 517 518 519
      if (context_xim->im_info->reconnecting)
	{
	  GdkDisplay *display;

	  display = gdk_screen_get_display (context_xim->im_info->screen);
	  XUnregisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY (display),
					    NULL, NULL, NULL,
					    xim_instantiate_callback,
					    (XPointer)context_xim->im_info);
	}
520
      else if (context_xim->im_info->im)
521 522 523 524 525 526 527 528 529
	{
	  XIMCallback im_destroy_callback;

	  im_destroy_callback.client_data = NULL;
	  im_destroy_callback.callback = NULL;
	  XSetIMValues (context_xim->im_info->im,
			XNDestroyCallback, &im_destroy_callback,
			NULL);
	}
530 531
    }

532
  set_ic_client_window (context_xim, NULL);
533

534
  g_free (context_xim->locale);
535
  g_free (context_xim->mb_charset);
Matthias Clasen's avatar
Matthias Clasen committed
536 537

  G_OBJECT_CLASS (parent_class)->finalize (obj);
538 539 540
}

static void
541
reinitialize_ic (GtkIMContextXIM *context_xim)
542 543 544 545 546
{
  if (context_xim->ic)
    {
      XDestroyIC (context_xim->ic);
      context_xim->ic = NULL;
547
      update_status_window (context_xim);
548 549 550 551

      if (context_xim->preedit_length)
	{
	  context_xim->preedit_length = 0;
552
	  if (!context_xim->finalizing)
553
	    g_signal_emit_by_name (context_xim, "preedit-changed");
554
	}
555
    }
556 557 558 559 560
  /* 
     reset filter_key_release flag, otherwise keystrokes will be doubled
     until reconnecting to XIM.
  */
  context_xim->filter_key_release = FALSE;
561 562
}

563 564
static void
set_ic_client_window (GtkIMContextXIM *context_xim,
565
		      GdkWindow       *client_window)
566
{
567
  reinitialize_ic (context_xim);
568 569 570 571 572 573 574 575 576 577 578 579 580
  if (context_xim->client_window)
    {
      context_xim->im_info->ics = g_slist_remove (context_xim->im_info->ics, context_xim);
      context_xim->im_info = NULL;
    }
  
  context_xim->client_window = client_window;

  if (context_xim->client_window)
    {
      context_xim->im_info = get_im (context_xim->client_window, context_xim->locale);
      context_xim->im_info->ics = g_slist_prepend (context_xim->im_info->ics, context_xim);
    }
581 582
  
  update_client_widget (context_xim);
583 584
}

585 586 587 588 589
static void
gtk_im_context_xim_set_client_window (GtkIMContext          *context,
				      GdkWindow             *client_window)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
590

591
  set_ic_client_window (context_xim, client_window);
592 593 594 595 596 597
}

GtkIMContext *
gtk_im_context_xim_new (void)
{
  GtkIMContextXIM *result;
598
  const gchar *charset;
599

600 601
  if (!GDK_IS_X11_DISPLAY(gdk_display_get_default()))
    return NULL;
602
  result = g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL);
603

604
  result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
605 606 607 608 609 610 611 612 613 614 615 616 617 618
  
  g_get_charset (&charset);
  result->mb_charset = g_strdup (charset);

  return GTK_IM_CONTEXT (result);
}

static char *
mb_to_utf8 (GtkIMContextXIM *context_xim,
	    const char      *str)
{
  GError *error = NULL;
  gchar *result;

619 620 621
  if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
    result = g_strdup (str);
  else
622
    {
623 624 625 626 627 628 629 630
      result = g_convert (str, -1,
			  "UTF-8", context_xim->mb_charset,
			  NULL, NULL, &error);
      if (!result)
	{
	  g_warning ("Error converting text from IM to UTF-8: %s\n", error->message);
	  g_error_free (error);
	}
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    }
  
  return result;
}

static gboolean
gtk_im_context_xim_filter_keypress (GtkIMContext *context,
				    GdkEventKey  *event)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
  XIC ic = gtk_im_context_xim_get_ic (context_xim);
  gchar static_buffer[256];
  gchar *buffer = static_buffer;
  gint buffer_size = sizeof(static_buffer) - 1;
  gint num_bytes = 0;
  KeySym keysym;
  Status status;
  gboolean result = FALSE;
649 650
  GdkWindow *root_window;
  GdkWindow *window;
651 652
  XKeyPressedEvent xevent;

653
  if (event->type == GDK_KEY_RELEASE && !context_xim->filter_key_release)
654 655
    return FALSE;

656 657 658
  root_window = gdk_screen_get_root_window (gdk_window_get_screen (event->window));
  window = gdk_window_get_toplevel (event->window);

659
  xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
660 661
  xevent.serial = 0;		/* hope it doesn't matter */
  xevent.send_event = event->send_event;
662 663
  xevent.display = GDK_WINDOW_XDISPLAY (window);
  xevent.window = GDK_WINDOW_XID (window);
664
  xevent.root = GDK_WINDOW_XID (root_window);
665 666 667 668 669
  xevent.subwindow = xevent.window;
  xevent.time = event->time;
  xevent.x = xevent.x_root = 0;
  xevent.y = xevent.y_root = 0;
  xevent.state = event->state;
670
  xevent.keycode = event->hardware_keycode;
671 672
  xevent.same_screen = True;
  
673
  if (XFilterEvent ((XEvent *)&xevent, GDK_WINDOW_XID (context_xim->client_window)))
674 675
    return TRUE;
  
676 677 678 679
  if (event->state &
      (gtk_accelerator_get_default_mod_mask () & ~(GDK_SHIFT_MASK | GDK_CONTROL_MASK))) 
    return FALSE;

680
 again:
681 682 683 684 685 686 687
  if (ic)
    num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
  else
    {
      num_bytes = XLookupString (&xevent, buffer, buffer_size, &keysym, NULL);
      status = XLookupBoth;
    }
688 689 690 691

  if (status == XBufferOverflow)
    {
      buffer_size = num_bytes;
692 693
      if (buffer != static_buffer) 
	g_free (buffer);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
      buffer = g_malloc (num_bytes + 1);
      goto again;
    }

  /* I don't know how we should properly handle XLookupKeysym or XLookupBoth
   * here ... do input methods actually change the keysym? we can't really
   * feed it back to accelerator processing at this point...
   */
  if (status == XLookupChars || status == XLookupBoth)
    {
      char *result_utf8;

      buffer[num_bytes] = '\0';

      result_utf8 = mb_to_utf8 (context_xim, buffer);
      if (result_utf8)
	{
711 712 713 714
	  if ((guchar)result_utf8[0] >= 0x20 &&
	      result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
				       * control characters into strings
				       */
715
	    {
716
	      g_signal_emit_by_name (context, "commit", result_utf8);
717 718 719 720 721 722 723
	      result = TRUE;
	    }
	  
	  g_free (result_utf8);
	}
    }

724 725 726
  if (buffer != static_buffer) 
    g_free (buffer);

727
  return result;
728 729
}

730 731 732 733 734
static void
gtk_im_context_xim_focus_in (GtkIMContext *context)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);

735 736 737
  if (!context_xim->has_focus)
    {
      XIC ic = gtk_im_context_xim_get_ic (context_xim);
738

739 740 741 742 743 744
      context_xim->has_focus = TRUE;
      update_status_window (context_xim);
      
      if (ic)
	XSetICFocus (ic);
    }
745

746 747 748 749 750 751 752 753
  return;
}

static void
gtk_im_context_xim_focus_out (GtkIMContext *context)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);

754 755 756 757 758 759 760 761 762 763
  if (context_xim->has_focus)
    {
      XIC ic = gtk_im_context_xim_get_ic (context_xim);
      
      context_xim->has_focus = FALSE;
      update_status_window (context_xim);
  
      if (ic)
	XUnsetICFocus (ic);
    }
764

765 766 767
  return;
}

768
static void
769 770
gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
					GdkRectangle *area)
771 772 773 774 775 776 777 778 779 780 781
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
  XIC ic = gtk_im_context_xim_get_ic (context_xim);

  XVaNestedList preedit_attr;
  XPoint          spot;

  if (!ic)
    return;

  spot.x = area->x;
782
  spot.y = area->y + area->height;
783 784 785

  preedit_attr = XVaCreateNestedList (0,
				      XNSpotLocation, &spot,
786
				      NULL);
787 788 789 790 791 792 793 794
  XSetICValues (ic,
		XNPreeditAttributes, preedit_attr,
		NULL);
  XFree(preedit_attr);

  return;
}

795 796 797 798 799 800 801 802 803 804 805
static void
gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
				    gboolean      use_preedit)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);

  use_preedit = use_preedit != FALSE;

  if (context_xim->use_preedit != use_preedit)
    {
      context_xim->use_preedit = use_preedit;
806
      reinitialize_ic (context_xim);
807 808 809 810 811
    }

  return;
}

812 813 814 815 816 817 818
static void
gtk_im_context_xim_reset (GtkIMContext *context)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
  XIC ic = gtk_im_context_xim_get_ic (context_xim);
  gchar *result;

819 820 821 822 823
  /* restore conversion state after resetting ic later */
  XIMPreeditState preedit_state = XIMPreeditUnKnown;
  XVaNestedList preedit_attr;
  gboolean have_preedit_state = FALSE;

824 825 826
  if (!ic)
    return;
  
827

828 829 830
  if (context_xim->preedit_length == 0)
    return;

831 832
  preedit_attr = XVaCreateNestedList(0,
                                     XNPreeditState, &preedit_state,
833
                                     NULL);
834 835 836 837 838 839 840
  if (!XGetICValues(ic,
                    XNPreeditAttributes, preedit_attr,
                    NULL))
    have_preedit_state = TRUE;

  XFree(preedit_attr);

841 842
  result = XmbResetIC (ic);

843 844
  preedit_attr = XVaCreateNestedList(0,
                                     XNPreeditState, preedit_state,
845
                                     NULL);
846 847 848 849 850 851 852
  if (have_preedit_state)
    XSetICValues(ic,
		 XNPreeditAttributes, preedit_attr,
		 NULL);

  XFree(preedit_attr);

853 854 855 856 857
  if (result)
    {
      char *result_utf8 = mb_to_utf8 (context_xim, result);
      if (result_utf8)
	{
858
	  g_signal_emit_by_name (context, "commit", result_utf8);
859 860 861 862 863 864 865
	  g_free (result_utf8);
	}
    }

  if (context_xim->preedit_length)
    {
      context_xim->preedit_length = 0;
866
      g_signal_emit_by_name (context, "preedit-changed");
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
    }

  XFree (result);
}

/* Mask of feedback bits that we render
 */
#define FEEDBACK_MASK (XIMReverse | XIMUnderline)

static void
add_feedback_attr (PangoAttrList *attrs,
		   const gchar   *str,
		   XIMFeedback    feedback,
		   gint           start_pos,
		   gint           end_pos)
{
  PangoAttribute *attr;
  
  gint start_index = g_utf8_offset_to_pointer (str, start_pos) - str;
  gint end_index = g_utf8_offset_to_pointer (str, end_pos) - str;

  if (feedback & XIMUnderline)
    {
      attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
      attr->start_index = start_index;
      attr->end_index = end_index;

      pango_attr_list_change (attrs, attr);
    }

  if (feedback & XIMReverse)
    {
      attr = pango_attr_foreground_new (0xffff, 0xffff, 0xffff);
      attr->start_index = start_index;
      attr->end_index = end_index;

      pango_attr_list_change (attrs, attr);

      attr = pango_attr_background_new (0, 0, 0);
      attr->start_index = start_index;
      attr->end_index = end_index;

      pango_attr_list_change (attrs, attr);
    }

  if (feedback & ~FEEDBACK_MASK)
    g_warning ("Unrendered feedback style: %#lx", feedback & ~FEEDBACK_MASK);
}

static void     
gtk_im_context_xim_get_preedit_string (GtkIMContext   *context,
				       gchar         **str,
				       PangoAttrList **attrs,
				       gint           *cursor_pos)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
923
  gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

  if (attrs)
    {
      int i;
      XIMFeedback last_feedback = 0;
      gint start = -1;
      
      *attrs = pango_attr_list_new ();

      for (i = 0; i < context_xim->preedit_length; i++)
	{
	  XIMFeedback new_feedback = context_xim->feedbacks[i] & FEEDBACK_MASK;
	  if (new_feedback != last_feedback)
	    {
	      if (start >= 0)
		add_feedback_attr (*attrs, utf8, last_feedback, start, i);
	      
	      last_feedback = new_feedback;
	      start = i;
	    }
	}

      if (start >= 0)
	add_feedback_attr (*attrs, utf8, last_feedback, start, i);
    }

  if (str)
    *str = utf8;
  else
    g_free (utf8);

  if (cursor_pos)
    *cursor_pos = context_xim->preedit_cursor;
}

959
static int
960 961 962 963 964
preedit_start_callback (XIC      xic,
			XPointer client_data,
			XPointer call_data)
{
  GtkIMContext *context = GTK_IM_CONTEXT (client_data);
965
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
966
  
967
  if (!context_xim->finalizing)
968
    g_signal_emit_by_name (context, "preedit-start");
969 970

  return -1;			/* No length limit */
971 972 973 974 975 976 977 978
}		     

static void
preedit_done_callback (XIC      xic,
		     XPointer client_data,
		     XPointer call_data)
{
  GtkIMContext *context = GTK_IM_CONTEXT (client_data);
979 980
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);

981 982 983 984
  if (context_xim->preedit_length)
    {
      context_xim->preedit_length = 0;
      if (!context_xim->finalizing)
985
	g_signal_emit_by_name (context_xim, "preedit-changed");
986 987
    }

988
  if (!context_xim->finalizing)
989
    g_signal_emit_by_name (context, "preedit-end");
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
}		     

static gint
xim_text_to_utf8 (GtkIMContextXIM *context, XIMText *xim_text, gchar **text)
{
  gint text_length = 0;
  GError *error = NULL;
  gchar *result = NULL;

  if (xim_text && xim_text->string.multi_byte)
    {
      if (xim_text->encoding_is_wchar)
	{
	  g_warning ("Wide character return from Xlib not currently supported");
	  *text = NULL;
	  return 0;
	}

1008 1009 1010 1011 1012 1013 1014 1015
      if (strcmp (context->mb_charset, "UTF-8") == 0)
	result = g_strdup (xim_text->string.multi_byte);
      else
	result = g_convert (xim_text->string.multi_byte,
			    -1,
			    "UTF-8",
			    context->mb_charset,
			    NULL, NULL, &error);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
      
      if (result)
	{
	  text_length = g_utf8_strlen (result, -1);
	  
	  if (text_length != xim_text->length)
	    {
	      g_warning ("Size mismatch when converting text from input method: supplied length = %d\n, result length = %d", xim_text->length, text_length);
	    }
	}
      else
	{
	  g_warning ("Error converting text from IM to UCS-4: %s", error->message);
	  g_error_free (error);
1030 1031 1032

	  *text = NULL;
	  return 0;
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	}

      *text = result;
      return text_length;
    }
  else
    {
      *text = NULL;
      return 0;
    }
}

static void
preedit_draw_callback (XIC                           xic, 
		       XPointer                      client_data,
		       XIMPreeditDrawCallbackStruct *call_data)
{
  GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);

  XIMText *new_xim_text = call_data->text;
  gint new_text_length;
  gunichar *new_text = NULL;
  gint i;
  gint diff;
  gint new_length;
  gchar *tmp;
  
  gint chg_first = CLAMP (call_data->chg_first, 0, context->preedit_length);
  gint chg_length = CLAMP (call_data->chg_length, 0, context->preedit_length - chg_first);

  context->preedit_cursor = call_data->caret;
  
  if (chg_first != call_data->chg_first || chg_length != call_data->chg_length)
    g_warning ("Invalid change to preedit string, first=%d length=%d (orig length == %d)",
	       call_data->chg_first, call_data->chg_length, context->preedit_length);

  new_text_length = xim_text_to_utf8 (context, new_xim_text, &tmp);
  if (tmp)
    {
1072
      new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
      g_free (tmp);
    }
  
  diff = new_text_length - chg_length;
  new_length = context->preedit_length + diff;

  if (new_length > context->preedit_size)
    {
      context->preedit_size = new_length;
      context->preedit_chars = g_renew (gunichar, context->preedit_chars, new_length);
      context->feedbacks = g_renew (XIMFeedback, context->feedbacks, new_length);
    }

  if (diff < 0)
    {
      for (i = chg_first + chg_length ; i < context->preedit_length; i++)
	{
	  context->preedit_chars[i + diff] = context->preedit_chars[i];
	  context->feedbacks[i + diff] = context->feedbacks[i];
	}
    }
  else
    {
      for (i = context->preedit_length - 1; i >= chg_first + chg_length ; i--)
	{
	  context->preedit_chars[i + diff] = context->preedit_chars[i];
	  context->feedbacks[i + diff] = context->feedbacks[i];
	}
    }

  for (i = 0; i < new_text_length; i++)
    {
      context->preedit_chars[chg_first + i] = new_text[i];
      context->feedbacks[chg_first + i] = new_xim_text->feedback[i];
    }

  context->preedit_length += diff;

1111
  g_free (new_text);
1112

1113
  if (!context->finalizing)
1114
    g_signal_emit_by_name (context, "preedit-changed");
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
}
    

static void
preedit_caret_callback (XIC                            xic,
			XPointer                       client_data,
			XIMPreeditCaretCallbackStruct *call_data)
{
  GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
  
  if (call_data->direction == XIMAbsolutePosition)
    {
      context->preedit_cursor = call_data->position;
1128
      if (!context->finalizing)
1129
	g_signal_emit_by_name (context, "preedit-changed");
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
    }
  else
    {
      g_warning ("Caret movement command: %d %d %d not supported",
		 call_data->position, call_data->direction, call_data->style);
    }
}	     

static void
status_start_callback (XIC      xic,
		       XPointer client_data,
		       XPointer call_data)
{
1143
  return;
1144 1145 1146 1147 1148 1149 1150
} 

static void
status_done_callback (XIC      xic,
		      XPointer client_data,
		      XPointer call_data)
{
1151
  return;
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
}

static void
status_draw_callback (XIC      xic,
		      XPointer client_data,
		      XIMStatusDrawCallbackStruct *call_data)
{
  GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);

  if (call_data->type == XIMTextType)
    {
      gchar *text;
      xim_text_to_utf8 (context, call_data->data.text, &text);

1166 1167
      if (context->status_window)
	status_window_set_text (context->status_window, text ? text : "");
1168 1169 1170
    }
  else				/* bitmap */
    {
1171
      g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
1172 1173 1174
    }
}

1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
static void
string_conversion_callback (XIC xic, XPointer client_data, XPointer call_data)
{
  GtkIMContextXIM *context_xim;
  XIMStringConversionCallbackStruct *conv_data;
  gchar *surrounding;
  gint  cursor_index;

  context_xim = (GtkIMContextXIM *)client_data;
  conv_data = (XIMStringConversionCallbackStruct *)call_data;

  if (gtk_im_context_get_surrounding ((GtkIMContext *)context_xim,
                                      &surrounding, &cursor_index))
    {
      gchar *text = NULL;
      gsize text_len = 0;
      gint  subst_offset = 0, subst_nchars = 0;
      gint  i;
      gchar *p = surrounding + cursor_index, *q;
      gshort position = (gshort)conv_data->position;

      if (position > 0)
        {
          for (i = position; i > 0 && *p; --i)
            p = g_utf8_next_char (p);
          if (i > 0)
            return;
        }
      /* According to X11R6.4 Xlib - C Library Reference Manual
       * section 13.5.7.3 String Conversion Callback,
       * XIMStringConversionPosition is starting position _relative_
       * to current client's cursor position. So it should be able
       * to be negative, or referring to a position before the cursor
       * would be impossible. But current X protocol defines this as
       * unsigned short. So, compiler may warn about the value range
       * here. We hope the X protocol is fixed soon.
       */
      else if (position < 0)
        {
          for (i = position; i < 0 && p > surrounding; ++i)
            p = g_utf8_prev_char (p);
          if (i < 0)
            return;
        }

      switch (conv_data->direction)
        {
        case XIMForwardChar:
          for (i = conv_data->factor, q = p; i > 0 && *q; --i)
            q = g_utf8_next_char (q);
          if (i > 0)
            break;
          text = g_locale_from_utf8 (p, q - p, NULL, &text_len, NULL);
          subst_offset = position;
          subst_nchars = conv_data->factor;
          break;

        case XIMBackwardChar:
          for (i = conv_data->factor, q = p; i > 0 && q > surrounding; --i)
            q = g_utf8_prev_char (q);
          if (i > 0)
            break;
          text = g_locale_from_utf8 (q, p - q, NULL, &text_len, NULL);
          subst_offset = position - conv_data->factor;
          subst_nchars = conv_data->factor;
          break;

        case XIMForwardWord:
        case XIMBackwardWord:
        case XIMCaretUp:
        case XIMCaretDown:
        case XIMNextLine:
        case XIMPreviousLine:
        case XIMLineStart:
        case XIMLineEnd:
        case XIMAbsolutePosition:
        case XIMDontChange:
        default:
          break;
        }
      /* block out any failure happenning to "text", including conversion */
      if (text)
        {
          conv_data->text = (XIMStringConversionText *)
                              malloc (sizeof (XIMStringConversionText));
          if (conv_data->text)
            {
              conv_data->text->length = text_len;
              conv_data->text->feedback = NULL;
              conv_data->text->encoding_is_wchar = False;
              conv_data->text->string.mbs = (char *)malloc (text_len);
              if (conv_data->text->string.mbs)
                memcpy (conv_data->text->string.mbs, text, text_len);
              else
                {
                  free (conv_data->text);
                  conv_data->text = NULL;
                }
            }

          g_free (text);
        }
      if (conv_data->operation == XIMStringConversionSubstitution
          && subst_nchars > 0)
        {
          gtk_im_context_delete_surrounding ((GtkIMContext *)context_xim,
                                            subst_offset, subst_nchars);
        }

      g_free (surrounding);
    }
}


1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
static XVaNestedList
set_preedit_callback (GtkIMContextXIM *context_xim)
{
  context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
  context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
  context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
  context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
  context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
  context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
  context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
  context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
  return XVaCreateNestedList (0,
			      XNPreeditStartCallback, &context_xim->preedit_start_callback,
			      XNPreeditDoneCallback, &context_xim->preedit_done_callback,
			      XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
			      XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
			      NULL);
}

static XVaNestedList
set_status_callback (GtkIMContextXIM *context_xim)
{
  context_xim->status_start_callback.client_data = (XPointer)context_xim;
  context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
  context_xim->status_done_callback.client_data = (XPointer)context_xim;
  context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
  context_xim->status_draw_callback.client_data = (XPointer)context_xim;
  context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
	  
  return XVaCreateNestedList (0,
			      XNStatusStartCallback, &context_xim->status_start_callback,
			      XNStatusDoneCallback, &context_xim->status_done_callback,
			      XNStatusDrawCallback, &context_xim->status_draw_callback,
			      NULL);
}

1325

1326 1327 1328 1329 1330 1331 1332
static void
set_string_conversion_callback (GtkIMContextXIM *context_xim, XIC xic)
{
  if (!context_xim->im_info->supports_string_conversion)
    return;
  
  context_xim->string_conversion_callback.client_data = (XPointer)context_xim;
1333
  context_xim->string_conversion_callback.callback = (XIMProc)string_conversion_callback;
1334 1335 1336 1337 1338 1339 1340
  
  XSetICValues (xic,
		XNStringConversionCallback,
		(XPointer)&context_xim->string_conversion_callback,
		NULL);
}

1341
static XIC
1342
gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
1343
{
1344 1345
  if (context_xim->im_info == NULL || context_xim->im_info->im == NULL)
    return NULL;
1346

1347
  if (!context_xim->ic)
1348
    {
1349 1350 1351 1352 1353
      const char *name1 = NULL;
      XVaNestedList list1 = NULL;
      const char *name2 = NULL;
      XVaNestedList list2 = NULL;
      XIMStyle im_style = 0;
1354
      XIC xic = NULL;
1355 1356 1357

      if (context_xim->use_preedit &&
	  (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
1358
	{
1359 1360 1361
	  im_style |= XIMPreeditCallbacks;
	  name1 = XNPreeditAttributes;
	  list1 = set_preedit_callback (context_xim);
1362
	}
1363 1364
      else if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditNone)
	im_style |= XIMPreeditNone;
1365
      else
1366 1367 1368
	im_style |= XIMPreeditNothing;

      if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
1369
	{
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	  im_style |= XIMStatusCallbacks;
	  if (name1 == NULL)
	    {
	      name1 = XNStatusAttributes;
	      list1 = set_status_callback (context_xim);
	    }
	  else
	    {
	      name2 = XNStatusAttributes;
	      list2 = set_status_callback (context_xim);
	    }
1381
	}
1382 1383 1384 1385 1386 1387 1388
      else if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusNone)
	im_style |= XIMStatusNone;
      else
	im_style |= XIMStatusNothing;

      xic = XCreateIC (context_xim->im_info->im,
		       XNInputStyle, im_style,
1389
		       XNClientWindow, GDK_WINDOW_XID (context_xim->client_window),
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
		       name1, list1,
		       name2, list2,
		       NULL);
      if (list1)
	XFree (list1);
      if (list2)
	XFree (list2);

      if (xic)
	{
	  /* Don't filter key released events with XFilterEvents unless
	   * input methods ask for. This is a workaround for Solaris input
	   * method bug in C and European locales. It doubles each key
	   * stroke if both key pressed and released events are filtered.
	   * (bugzilla #81759)
	   */
1406
	  gulong mask = 0xaaaaaaaa;
1407 1408 1409
	  XGetICValues (xic,
			XNFilterEvents, &mask,
			NULL);
1410
	  context_xim->filter_key_release = (mask & KeyReleaseMask) != 0;
1411
	  set_string_conversion_callback (context_xim, xic);
1412
	}
1413
      
1414
      context_xim->ic = xic;
1415 1416 1417 1418 1419

      update_status_window (context_xim);
      
      if (xic && context_xim->has_focus)
	XSetICFocus (xic);
1420
    }
1421 1422
  return context_xim->ic;
}
1423

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
/*****************************************************************
 * Status Window handling
 *
 * A status window is a small window attached to the toplevel
 * that is used to display information to the user about the
 * current input operation.
 *
 * We claim the toplevel's status window for an input context if:
 *
 * A) The input context has a toplevel
 * B) The input context has the focus
 * C) The input context has an XIC associated with it
 *
 * Tracking A) and C) is pretty reliable since we
 * compute A) and create the XIC for C) ourselves.
 * For B) we basically have to depend on our callers
 * calling ::focus-in and ::focus-out at the right time.
 *
 * The toplevel is computed by walking up the GdkWindow
 * hierarchy from context->client_window until we find a
 * window that is owned by some widget, and then calling
 * gtk_widget_get_toplevel() on that widget. This should
 * handle both cases where we might have GdkWindows without widgets,
 * and cases where GtkWidgets have strange window hierarchies
 * (like a torn off GtkHandleBox.)
 *
 * The status window is visible if and only if there is text
 * for it; whenever a new GtkIMContextXIM claims the status
 * window, we blank out any existing text. We actually only
 * create a GtkWindow for the status window the first time
 * it is shown; this is an important optimization when we are
 * using XIM with something like a simple compose-key input
 * method that never needs a status window.
 *****************************************************************/

/* Called when we no longer need a status window
*/
static void
disclaim_status_window (GtkIMContextXIM *context_xim)
{
  if (context_xim->status_window)
    {
      g_assert (context_xim->status_window->context == context_xim);

      status_window_set_text (context_xim->status_window, "");
      
      context_xim->status_window->context = NULL;
      context_xim->status_window = NULL;
    }
}
1474

1475 1476 1477 1478
/* Called when we need a status window
 */
static void
claim_status_window (GtkIMContextXIM *context_xim)
1479
{
1480 1481 1482
  if (!context_xim->status_window && context_xim->client_widget)
    {
      GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
1483
      if (toplevel && gtk_widget_is_toplevel (toplevel))
1484 1485
	{
	  StatusWindow *status_window = status_window_get (toplevel);
1486

1487 1488 1489 1490 1491 1492 1493
	  if (status_window->context)
	    disclaim_status_window (status_window->context);

	  status_window->context = context_xim;
	  context_xim->status_window = status_window;
	}
    }
1494 1495
}

1496 1497 1498
/* Basic call made whenever something changed that might cause
 * us to need, or not to need a status window.
 */
1499
static void
1500
update_status_window (GtkIMContextXIM *context_xim)
1501
{
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
  if (context_xim->ic && context_xim->in_toplevel && context_xim->has_focus)
    claim_status_window (context_xim);
  else
    disclaim_status_window (context_xim);
}

/* Updates the in_toplevel flag for @context_xim
 */
static void
update_in_toplevel (GtkIMContextXIM *context_xim)
{
  if (context_xim->client_widget)
    {
      GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
      
1517
      context_xim->in_toplevel = (toplevel && gtk_widget_is_toplevel (toplevel));
1518 1519 1520 1521 1522 1523 1524
    }
  else
    context_xim->in_toplevel = FALSE;

  /* Some paranoia, in case we don't get a focus out */
  if (!context_xim->in_toplevel)
    context_xim->has_focus = FALSE;
1525
  
1526
  update_status_window (context_xim);
1527 1528
}

1529 1530 1531 1532 1533 1534
/* Callback when @widget's toplevel changes. It will always
 * change from NULL to a window, or a window to NULL;
 * we use that intermediate NULL state to make sure
 * that we disclaim the toplevel status window for the old
 * window.
 */
1535
static void
1536 1537 1538
on_client_widget_hierarchy_changed (GtkWidget       *widget,
				    GtkWidget       *old_toplevel,
				    GtkIMContextXIM *context_xim)
1539
{
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
  update_in_toplevel (context_xim);
}

/* Finds the GtkWidget that owns the window, or if none, the
 * widget owning the nearest parent that has a widget.
 */
static GtkWidget *
widget_for_window (GdkWindow *window)
{
  while (window)
    {
      gpointer user_data;
      gdk_window_get_user_data (window, &user_data);
      if (user_data)
	return user_data;

      window = gdk_window_get_parent (window);
    }

  return NULL;
}

/* Called when context_xim->client_window changes; takes care of
 * removing and/or setting up our watches for the toplevel
 */
static void
update_client_widget (GtkIMContextXIM *context_xim)
{
  GtkWidget *new_client_widget = widget_for_window (context_xim->client_window);

  if (new_client_widget != context_xim->client_widget)
    {
      if (context_xim->client_widget)
	{
	  g_signal_handlers_disconnect_by_func (context_xim->client_widget,
						G_CALLBACK (on_client_widget_hierarchy_changed),
						context_xim);
	}
      context_xim->client_widget = new_client_widget;
      if (context_xim->client_widget)
	{
	  g_signal_connect (context_xim->client_widget, "hierarchy-changed",
			    G_CALLBACK (on_client_widget_hierarchy_changed),
			    context_xim);
	}

      update_in_toplevel (context_xim);
    }
}

/* Called when the toplevel is destroyed; frees the status window
 */
static void
on_status_toplevel_destroy (GtkWidget    *toplevel,
			    StatusWindow *status_window)
{
  status_window_free (status_window);
}

/* Called when the screen for the toplevel changes; updates the
 * screen for the status window to match.
 */
static void
on_status_toplevel_notify_screen (GtkWindow    *toplevel,
				  GParamSpec   *pspec,
				  StatusWindow *status_window)
{
  if (status_window->window)
    gtk_window_set_screen (GTK_WINDOW (status_window->window),
			   gtk_widget_get_screen (GTK_WIDGET (toplevel)));
1610 1611
}

1612 1613 1614
/* Called when the toplevel window is moved; updates the position of
 * the status window to follow it.
 */
1615
static gboolean
1616 1617 1618
on_status_toplevel_configure (GtkWidget         *toplevel,
			      GdkEventConfigure *event,
			      StatusWindow      *status_window)
1619 1620 1621 1622
{
  GdkRectangle rect;
  GtkRequisition requisition;
  gint y;
1623
  gint height;
1624

1625 1626 1627
  if (status_window->window)
    {
      height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
1628 1629 1630

      gdk_window_get_frame_extents (gtk_widget_get_window (toplevel),
                                    &rect);
1631
      gtk_widget_get_preferred_size ( (status_window->window),
1632 1633
                                 &requisition, NULL);

1634 1635 1636 1637 1638 1639 1640
      if (rect.y + rect.height + requisition.height < height)
	y = rect.y + rect.height;
      else
	y = height - requisition.height;
      
      gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
    }
1641 1642 1643 1644

  return FALSE;
}

1645 1646 1647 1648
/* Frees a status window and removes its link from the status_windows list
 */
static void
status_window_free (StatusWindow *status_window)
1649
{
1650
  status_windows = g_slist_remove (status_windows, status_window);
1651

1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
  if (status_window->context)
    status_window->context->status_window = NULL;
 
  g_signal_handlers_disconnect_by_func (status_window->toplevel,
					G_CALLBACK (on_status_toplevel_destroy),
					status_window);
  g_signal_handlers_disconnect_by_func (status_window->toplevel,
					G_CALLBACK (on_status_toplevel_notify_screen),
					status_window);
  g_signal_handlers_disconnect_by_func (status_window->toplevel,
					G_CALLBACK (on_status_toplevel_configure),
					status_window);

  if (status_window->window)
    gtk_widget_destroy (status_window->window);
1667
  
1668 1669 1670 1671
  g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
 
  g_free (status_window);
}
1672

1673 1674 1675 1676 1677 1678
/* Finds the status window object for a toplevel, creating it if necessary.
 */
static StatusWindow *
status_window_get (GtkWidget *toplevel)
{
  StatusWindow *status_window;
1679 1680

  status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
1681
  if (status_window)
1682 1683 1684
    return status_window;
  
  status_window = g_new0 (StatusWindow, 1);
1685 1686 1687
  status_window->toplevel = toplevel;

  status_windows = g_slist_prepend (status_windows, status_window);
1688

1689 1690 1691
  g_signal_connect (toplevel, "destroy",
		    G_CALLBACK (on_status_toplevel_destroy),
		    status_window);
1692
  g_signal_connect (toplevel, "configure-event",
1693 1694 1695 1696 1697
		    G_CALLBACK (on_status_toplevel_configure),
		    status_window);
  g_signal_connect (toplevel, "notify::screen",
		    G_CALLBACK (on_status_toplevel_notify_screen),
		    status_window);
1698 1699 1700
  
  g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);

1701
  return status_window;
1702 1703
}

1704 1705 1706
/* Creates the widgets for the status window; called when we
 * first need to show text for the status window.
 */
1707
static void
1708
status_window_make_window (StatusWindow *status_window)
1709
{
1710 1711 1712 1713 1714
  GtkWidget *window;
  GtkWidget *status_label;
  
  status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
  window = status_window->window;
1715

1716
  gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
1717

1718
  status_label = gtk_label_new ("");
1719
  g_object_set (status_label, "margin", 1, NULL);
1720 1721 1722 1723 1724 1725 1726 1727
  gtk_widget_show (status_label);
  
  gtk_container_add (GTK_CONTAINER (window), status_label);
  
  gtk_window_set_screen (GTK_WINDOW (status_window->window),
			 gtk_widget_get_screen (status_window->toplevel));

  on_status_toplevel_configure (status_window->toplevel, NULL, status_window);
1728 1729
}

1730 1731 1732
/* Updates the text in the status window, hiding or
 * showing the window as necessary.
 */
1733
static void
1734 1735
status_window_set_text (StatusWindow *status_window,
			const gchar  *text)
1736
{
1737
  if (text[0])
1738
    {
1739
      GtkWidget *label;
1740
      
1741 1742 1743
      if (!status_window->window)
	status_window_make_window (status_window);