gtkimcontextxim.c 35.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GTK - The GIMP Toolkit
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "locale.h"
#include <string.h>

23
#include "gtk/gtkintl.h"
24
#include "gtk/gtklabel.h"
25
#include "gtk/gtksignal.h"
26
#include "gtk/gtkwindow.h"
27 28
#include "gtkimcontextxim.h"

29 30
typedef struct _StatusWindow StatusWindow;

31 32
struct _GtkXIMInfo
{
33
  GdkScreen *screen;
34 35
  XIM im;
  char *locale;
36 37
  XIMStyle preedit_style_setting;
  XIMStyle status_style_setting;
38
  XIMStyle style;
39 40 41 42
  GtkSettings *settings;
  gulong status_set;
  gulong preedit_set;
  XIMStyles *xim_styles;
43
  GSList *ics;
44 45
};

46 47 48 49 50 51 52 53 54
/* 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;
  
  /* Signal connection ids; we connect to the toplevel */
55 56
  gulong destroy_handler_id;
  gulong configure_handler_id;
57 58
};

59 60 61 62 63 64 65 66
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);
67 68
static void     gtk_im_context_xim_focus_in           (GtkIMContext          *context);
static void     gtk_im_context_xim_focus_out          (GtkIMContext          *context);
69
static void     gtk_im_context_xim_set_cursor_location (GtkIMContext          *context,
70
						       GdkRectangle		*area);
71 72
static void     gtk_im_context_xim_set_use_preedit    (GtkIMContext          *context,
						       gboolean		      use_preedit);
73 74 75 76 77
static void     gtk_im_context_xim_get_preedit_string (GtkIMContext          *context,
						       gchar                **str,
						       PangoAttrList        **attrs,
						       gint                  *cursor_pos);

78 79 80 81 82 83
static void reinitialize_ic      (GtkIMContextXIM *context_xim,
				  gboolean         send_signal);
static void set_ic_client_window (GtkIMContextXIM *context_xim,
				  GdkWindow       *client_window,
				  gboolean         send_signal);

84 85
static void setup_styles (GtkXIMInfo *info);

86 87 88 89 90
static void status_window_show     (GtkIMContextXIM *context_xim);
static void status_window_hide     (GtkIMContextXIM *context_xim);
static void status_window_set_text (GtkIMContextXIM *context_xim,
				    const gchar     *text);

91 92 93 94 95
static XIC       gtk_im_context_xim_get_ic            (GtkIMContextXIM *context_xim);
static GObjectClass *parent_class;

GType gtk_type_im_context_xim = 0;

96
GSList *open_ims = NULL;
97

98 99 100
/* List of status windows for different toplevels */
static GSList *status_windows = NULL;

101 102 103 104 105 106 107 108 109 110 111 112 113
void
gtk_im_context_xim_register_type (GTypeModule *type_module)
{
  static const GTypeInfo im_context_xim_info =
  {
    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
114
    (GInstanceInitFunc) gtk_im_context_xim_init,
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  };

  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;
  } 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 */
}

170 171 172 173 174 175 176 177 178
static void
reinitialize_all_ics (GtkXIMInfo *info)
{
  GSList *tmp_list;

  for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
    reinitialize_ic (tmp_list->data, TRUE);
}

179
static void
180
status_style_change (GtkXIMInfo *info)
181
{
182
  GtkIMStatusStyle status_style;
183
  
184 185 186 187 188 189 190 191 192
  g_object_get (info->settings,
		"gtk-im-status-style", &status_style,
		NULL);
  if (status_style == GTK_IM_STATUS_CALLBACK)
    info->status_style_setting = XIMStatusCallbacks;
  else if (status_style == GTK_IM_STATUS_NOTHING)
    info->status_style_setting = XIMStatusNothing;
  else
    return;
193

194
  setup_styles (info);
195 196
  
  reinitialize_all_ics (info);
197
}
198

199 200 201 202 203 204
static void
preedit_style_change (GtkXIMInfo *info)
{
  GtkIMPreeditStyle preedit_style;
  g_object_get (info->settings,
		"gtk-im-preedit-style", &preedit_style,
205
		NULL);
206 207 208 209 210 211 212 213
  if (preedit_style == GTK_IM_PREEDIT_CALLBACK)
    info->preedit_style_setting = XIMPreeditCallbacks;
  else if (preedit_style == GTK_IM_PREEDIT_NOTHING)
    info->preedit_style_setting = XIMPreeditNothing;
  else
    return;

  setup_styles (info);
214 215
  
  reinitialize_all_ics (info);
216
}
217

218 219 220 221 222 223 224 225
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;
226 227 228 229
  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])
230
	  {
231
	    if (settings_preference == xim_styles->supported_styles[i])
232
	      {
233
		info->style = settings_preference;
234 235 236 237 238
		break;
	      }
	    info->style = choose_better_style (info->style,
					       xim_styles->supported_styles[i]);
	  }
239
    }
240 241 242
}

static void
243
setup_im (GtkXIMInfo *info)
244 245 246 247 248 249 250
{
  XIMValuesList *ic_values = NULL;

  XGetIMValues (info->im,
		XNQueryInputStyle, &info->xim_styles,
		XNQueryICValuesList, &ic_values,
		NULL);
251

252
  info->settings = gtk_settings_get_for_screen (info->screen);
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

  if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
				     "gtk-im-preedit-style"))
    gtk_settings_install_property (g_param_spec_enum ("gtk-im-preedit-style",
						      _("IM Preedit style"),
						      _("How to draw the input method preedit string"),
						      GTK_TYPE_IM_PREEDIT_STYLE,
						      GTK_IM_PREEDIT_CALLBACK,
						      G_PARAM_READWRITE));

  if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
				     "gtk-im-status-style"))
    gtk_settings_install_property (g_param_spec_enum ("gtk-im-status-style",
						      _("IM Status style"),
						      _("How to draw the input method statusbar"),
						      GTK_TYPE_IM_STATUS_STYLE,
						      GTK_IM_STATUS_CALLBACK,
						      G_PARAM_READWRITE));
271 272 273 274 275 276 277 278 279 280

  info->status_set = g_signal_connect_swapped (info->settings,
					       "notify::gtk-im-status-style",
					       G_CALLBACK (status_style_change),
					       info);
  info->preedit_set = g_signal_connect_swapped (info->settings,
						"notify::gtk-im-preedit-style",
						G_CALLBACK (preedit_style_change),
						info);

281 282
  status_style_change (info);
  preedit_style_change (info);
283 284

#if 0
285 286 287 288 289 290 291
  if (ic_values)
    {
      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]);
    }
292 293
#endif

294 295
  if (ic_values)
    XFree (ic_values);
296 297
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
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)
    set_ic_client_window (tmp_list->data, NULL, TRUE);

  g_slist_free (tmp_list);
  
  g_signal_handler_disconnect (info->settings, info->status_set);
  g_signal_handler_disconnect (info->settings, info->preedit_set);
  
  XFree (info->xim_styles->supported_styles);
  XFree (info->xim_styles);
  g_free (info->locale);
  XCloseIM (info->im);

  g_free (info);
}

326
static GtkXIMInfo *
327
get_im (GdkWindow *client_window,
328
	const char *locale)
329
{
330
  GSList *tmp_list;
331 332
  GtkXIMInfo *info;
  XIM im = NULL;
333 334
  GdkScreen *screen = gdk_drawable_get_screen (client_window);
  GdkDisplay *display = gdk_screen_get_display (screen);
335

336
  tmp_list = open_ims;
337 338 339
  while (tmp_list)
    {
      info = tmp_list->data;
340
      if (info->screen == screen &&
341
	  strcmp (info->locale, locale) == 0)
342 343 344 345 346 347 348 349 350 351
	return info;

      tmp_list = tmp_list->next;
    }

  info = NULL;

  if (XSupportsLocale ())
    {
      if (!XSetLocaleModifiers (""))
352
	g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
353
      
354
      im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
355
      
356 357 358
      if (!im)
	g_warning ("Unable to open XIM input method, falling back to XLookupString()");

359 360 361
      if (im)
	{
	  info = g_new (GtkXIMInfo, 1);
362
	  open_ims = g_slist_prepend (open_ims, info);
363

364
	  info->screen = screen;
365 366
	  info->locale = g_strdup (locale);
	  info->im = im;
367 368 369
	  info->xim_styles = NULL;
	  info->preedit_style_setting = 0;
	  info->status_style_setting = 0;
370
	  info->settings = NULL;
371 372
	  info->preedit_set = 0;
	  info->status_set = 0;
373
	  info->ics = NULL;
374

375 376
	  setup_im (info);

377 378
	  g_signal_connect (display, "closed",
			    G_CALLBACK (xim_info_display_closed), info);
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	}
    }

  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;
397 398
  im_context_class->focus_in = gtk_im_context_xim_focus_in;
  im_context_class->focus_out = gtk_im_context_xim_focus_out;
399
  im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
400
  im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
401 402 403 404 405 406
  gobject_class->finalize = gtk_im_context_xim_finalize;
}

static void
gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
{
407
  im_context_xim->use_preedit = TRUE;
408
  im_context_xim->filter_key_release = FALSE;
409 410 411 412 413 414 415
}

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

416
  set_ic_client_window (context_xim, NULL, FALSE);
417

418
  g_free (context_xim->locale);
419 420 421 422
  g_free (context_xim->mb_charset);
}

static void
423 424
reinitialize_ic (GtkIMContextXIM *context_xim,
		 gboolean         send_signal)
425 426 427 428 429
{
  if (context_xim->ic)
    {
      XDestroyIC (context_xim->ic);
      context_xim->ic = NULL;
430 431 432 433

      if (context_xim->preedit_length)
	{
	  context_xim->preedit_length = 0;
434 435
	  if (send_signal)
	    g_signal_emit_by_name (context_xim, "preedit_changed");
436
	}
437
    }
438 439
}

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
static void
set_ic_client_window (GtkIMContextXIM *context_xim,
		      GdkWindow       *client_window,
		      gboolean         send_signal)
{
  reinitialize_ic (context_xim, send_signal);
  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);
    }
}

461 462 463 464 465
static void
gtk_im_context_xim_set_client_window (GtkIMContext          *context,
				      GdkWindow             *client_window)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
466

467
  set_ic_client_window (context_xim, client_window, TRUE);
468 469 470 471 472 473
}

GtkIMContext *
gtk_im_context_xim_new (void)
{
  GtkIMContextXIM *result;
474
  const gchar *charset;
475

476
  result = GTK_IM_CONTEXT_XIM (g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL));
477

478
  result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
479 480 481 482 483 484 485 486 487 488 489 490 491 492
  
  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;

493 494 495
  if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
    result = g_strdup (str);
  else
496
    {
497 498 499 500 501 502 503 504
      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);
	}
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    }
  
  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;
523
  GdkWindow *root_window = gdk_screen_get_root_window (gdk_drawable_get_screen (event->window));
524 525 526

  XKeyPressedEvent xevent;

527
  if (event->type == GDK_KEY_RELEASE && !context_xim->filter_key_release)
528 529
    return FALSE;

Owen Taylor's avatar
Owen Taylor committed
530
  xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
531 532 533 534
  xevent.serial = 0;		/* hope it doesn't matter */
  xevent.send_event = event->send_event;
  xevent.display = GDK_DRAWABLE_XDISPLAY (event->window);
  xevent.window = GDK_DRAWABLE_XID (event->window);
535
  xevent.root = GDK_DRAWABLE_XID (root_window);
536 537 538 539 540
  xevent.subwindow = xevent.window;
  xevent.time = event->time;
  xevent.x = xevent.x_root = 0;
  xevent.y = xevent.y_root = 0;
  xevent.state = event->state;
541
  xevent.keycode = event->hardware_keycode;
542 543 544 545 546 547
  xevent.same_screen = True;
  
  if (XFilterEvent ((XEvent *)&xevent, GDK_DRAWABLE_XID (context_xim->client_window)))
    return TRUE;
  
 again:
548 549 550 551 552 553 554
  if (ic)
    num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
  else
    {
      num_bytes = XLookupString (&xevent, buffer, buffer_size, &keysym, NULL);
      status = XLookupBoth;
    }
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

  if (status == XBufferOverflow)
    {
      buffer_size = num_bytes;
      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)
	{
576 577 578 579
	  if ((guchar)result_utf8[0] >= 0x20 &&
	      result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
				       * control characters into strings
				       */
580
	    {
581
	      g_signal_emit_by_name (context, "commit", result_utf8);
582 583 584 585 586 587 588
	      result = TRUE;
	    }
	  
	  g_free (result_utf8);
	}
    }

589
  return result;
590 591
}

592 593 594 595 596 597 598 599 600 601
static void
gtk_im_context_xim_focus_in (GtkIMContext *context)
{
  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
  XIC ic = gtk_im_context_xim_get_ic (context_xim);

  if (!ic)
    return;

  XSetICFocus (ic);
602 603 604

  status_window_show (context_xim);

605 606 607 608 609 610 611 612 613 614 615 616 617
  return;
}

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

  if (!ic)
    return;

  XUnsetICFocus (ic);
618 619 620

  status_window_hide (context_xim);

621 622 623
  return;
}

624
static void
625 626
gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
					GdkRectangle *area)
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
{
  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;
  spot.y = area->y;

  preedit_attr = XVaCreateNestedList (0,
				      XNSpotLocation, &spot,
				      0);
  XSetICValues (ic,
		XNPreeditAttributes, preedit_attr,
		NULL);
  XFree(preedit_attr);

  return;
}

651 652 653 654 655 656 657 658 659 660 661
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;
662
      reinitialize_ic (context_xim, TRUE);
663 664 665 666 667
    }

  return;
}

668 669 670 671 672 673 674
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;

675 676 677 678 679
  /* restore conversion state after resetting ic later */
  XIMPreeditState preedit_state = XIMPreeditUnKnown;
  XVaNestedList preedit_attr;
  gboolean have_preedit_state = FALSE;

680 681 682
  if (!ic)
    return;
  
683

684 685 686
  if (context_xim->preedit_length == 0)
    return;

687 688 689 690 691 692 693 694 695 696
  preedit_attr = XVaCreateNestedList(0,
                                     XNPreeditState, &preedit_state,
                                     0);
  if (!XGetICValues(ic,
                    XNPreeditAttributes, preedit_attr,
                    NULL))
    have_preedit_state = TRUE;

  XFree(preedit_attr);

697 698
  result = XmbResetIC (ic);

699 700 701 702 703 704 705 706 707 708
  preedit_attr = XVaCreateNestedList(0,
                                     XNPreeditState, preedit_state,
                                     0);
  if (have_preedit_state)
    XSetICValues(ic,
		 XNPreeditAttributes, preedit_attr,
		 NULL);

  XFree(preedit_attr);

709 710 711 712 713
  if (result)
    {
      char *result_utf8 = mb_to_utf8 (context_xim, result);
      if (result_utf8)
	{
714
	  g_signal_emit_by_name (context, "commit", result_utf8);
715 716 717 718 719 720 721
	  g_free (result_utf8);
	}
    }

  if (context_xim->preedit_length)
    {
      context_xim->preedit_length = 0;
722
      g_signal_emit_by_name (context, "preedit_changed");
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    }

  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);
779
  gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

  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;
}

static void
preedit_start_callback (XIC      xic,
			XPointer client_data,
			XPointer call_data)
{
  GtkIMContext *context = GTK_IM_CONTEXT (client_data);
  
822
  g_signal_emit_by_name (context, "preedit_start");
823 824 825 826 827 828 829 830 831
}		     

static void
preedit_done_callback (XIC      xic,
		     XPointer client_data,
		     XPointer call_data)
{
  GtkIMContext *context = GTK_IM_CONTEXT (client_data);
  
832
  g_signal_emit_by_name (context, "preedit_end");  
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
}		     

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;
	}

851 852 853 854 855 856 857 858
      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);
859 860 861 862 863 864 865 866 867 868 869 870 871 872
      
      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);
873 874 875

	  *text = NULL;
	  return 0;
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
	}

      *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)
    {
915
      new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
916 917 918 919 920 921 922 923 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
      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;

  if (new_text)
    g_free (new_text);

957
  g_signal_emit_by_name (context, "preedit_changed");
958 959 960 961 962 963 964 965 966 967 968 969 970
}
    

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;
971
      g_signal_emit_by_name (context, "preedit_changed");
972 973 974 975 976 977 978 979 980 981 982 983 984
    }
  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)
{
985
  return;
986 987 988 989 990 991 992
} 

static void
status_done_callback (XIC      xic,
		      XPointer client_data,
		      XPointer call_data)
{
993
  return;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
}

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);

      if (text)
1009 1010 1011
	status_window_set_text (context, text);
      else
	status_window_set_text (context, "");
1012 1013 1014
    }
  else				/* bitmap */
    {
1015
      g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
1016 1017 1018
    }
}

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
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);
}

1055
static XIC
1056
get_ic_real (GtkIMContextXIM *context_xim)
1057
{
1058
  XIC xic = 0;
1059
  const char *name1 = NULL;
1060
  XVaNestedList list1 = NULL;
1061
  const char *name2 = NULL;
1062
  XVaNestedList list2 = NULL;
1063
  XIMStyle im_style = 0;
1064

1065 1066
  if (context_xim->use_preedit &&
      (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
1067
    {
1068
      im_style |= XIMPreeditCallbacks;
1069
      name1 = XNPreeditAttributes;
1070
      list1 = set_preedit_callback (context_xim);
1071
    }
1072 1073
  else
    im_style |= XIMPreeditNothing;
1074 1075 1076

  if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
    {
1077
      im_style |= XIMStatusCallbacks;
1078
      if (name1 == NULL)
1079
	{
1080
	  name1 = XNStatusAttributes;
1081
	  list1 = set_status_callback (context_xim);
1082
	}
1083
      else
1084
	{
1085
	  name2 = XNStatusAttributes;
1086
	  list2 = set_status_callback (context_xim);
1087
	}
1088
    }
1089 1090
  else
    im_style |= XIMStatusNothing;
1091

1092
  xic = XCreateIC (context_xim->im_info->im,
1093
		   XNInputStyle, im_style,
1094 1095 1096 1097 1098 1099 1100 1101 1102
		   XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
		   name1, list1,
		   name2, list2,
		   NULL);
  if (list1)
    XFree (list1);
  if (list2)
    XFree (list2);

1103 1104 1105 1106 1107 1108 1109 1110 1111
  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)
       */
      gulong mask = 0;
1112
      XGetICValues (xic,
1113
		    XNFilterEvents, &mask,
1114
		    NULL);
1115 1116 1117 1118
      context_xim->filter_key_release = (mask & KeyReleaseMask);
    }

  return xic;
1119 1120 1121 1122 1123 1124
}

static XIC
gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
{
  if (!context_xim->ic && context_xim->im_info)
1125 1126
    context_xim->ic = get_ic_real (context_xim);
  
1127 1128
  return context_xim->ic;
}
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

/**************************
 *                        *
 * Status Window handling *
 *                        *
 **************************/

static gboolean
status_window_expose_event (GtkWidget      *widget,
			    GdkEventExpose *event)
{
  gdk_draw_rectangle (widget->window,
		      widget->style->base_gc [GTK_STATE_NORMAL],
		      TRUE,
		      0, 0,
		      widget->allocation.width, widget->allocation.height);
  gdk_draw_rectangle (widget->window,
		      widget->style->text_gc [GTK_STATE_NORMAL],
		      FALSE,
		      0, 0,
		      widget->allocation.width - 1, widget->allocation.height - 1);

  return FALSE;
}

static void
status_window_style_set (GtkWidget *toplevel,
			 GtkStyle  *previous_style,
			 GtkWidget *label)
{
  gint i;
  
  for (i = 0; i < 5; i++)
    gtk_widget_modify_fg (label, i, &toplevel->style->text[i]);
}

1165
/* Frees a status window and removes its link from the status_windows list */
1166
static void
1167
status_window_free (StatusWindow *status_window)
1168
{
1169 1170 1171 1172 1173 1174 1175 1176
  status_windows = g_slist_remove (status_windows, status_window);
 
  g_signal_handler_disconnect (status_window->toplevel, status_window->destroy_handler_id);
  g_signal_handler_disconnect (status_window->toplevel, status_window->configure_handler_id);
  gtk_widget_destroy (status_window->window);
  g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
 
  g_free (status_window);
1177 1178 1179 1180 1181
}

static gboolean
status_window_configure (GtkWidget         *toplevel,
			 GdkEventConfigure *event,
1182
  			 StatusWindow      *status_window)
1183 1184 1185 1186
{
  GdkRectangle rect;
  GtkRequisition requisition;
  gint y;
1187 1188
  gint height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
  
1189
  gdk_window_get_frame_extents (toplevel->window, &rect);
1190
  gtk_widget_size_request (status_window->window, &requisition);
1191

1192
  if (rect.y + rect.height + requisition.height < height)
1193 1194
    y = rect.y + rect.height;
  else
1195
    y = height - requisition.height;
1196
  
1197
  gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207

  return FALSE;
}

static GtkWidget *
status_window_get (GtkIMContextXIM *context_xim,
		   gboolean         create)
{
  GdkWindow *toplevel_gdk;
  GtkWidget *toplevel;
1208 1209
  GtkWidget *window;
  StatusWindow *status_window;
1210
  GtkWidget *status_label;
1211 1212
  GdkScreen *screen;
  GdkWindow *root_window;
1213 1214 1215 1216 1217
  
  if (!context_xim->client_window)
    return NULL;

  toplevel_gdk = context_xim->client_window;
1218 1219 1220
  screen = gdk_drawable_get_screen (toplevel_gdk);
  root_window = gdk_screen_get_root_window (screen);
  
1221 1222 1223
  while (TRUE)
    {
      GdkWindow *parent = gdk_window_get_parent (toplevel_gdk);
1224
      if (parent == root_window)
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	break;
      else
	toplevel_gdk = parent;
    }

  gdk_window_get_user_data (toplevel_gdk, (gpointer *)&toplevel);
  if (!toplevel)
    return NULL;

  status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
1235 1236 1237 1238 1239 1240 1241 1242