gtkimcontextsimple.c 32.2 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4
 * Copyright (C) 2000 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
13
 *
14
 * 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 <stdlib.h>
21
#include <string.h>
22

23
#include "gtkprivate.h"
24
#include "gtkaccelgroup.h"
25
#include "gtkimcontextsimple.h"
26 27
#include "gtksettings.h"
#include "gtkwidget.h"
28
#include "gtkdebug.h"
29
#include "gtkintl.h"
30

31

32 33 34 35 36 37 38
/**
 * SECTION:gtkimcontextsimple
 * @Short_description: An input method context supporting table-based input methods
 * @Title: GtkIMContextSimple
 */


Owen Taylor's avatar
Owen Taylor committed
39
typedef struct _GtkComposeTable GtkComposeTable;
40
typedef struct _GtkComposeTableCompact GtkComposeTableCompact;
41

42
struct _GtkIMContextSimplePrivate
43 44 45 46 47 48 49 50 51 52 53
{
  GSList        *tables;

  guint          compose_buffer[GTK_MAX_COMPOSE_LEN + 1];
  gunichar       tentative_match;
  gint           tentative_match_len;

  guint          in_hex_sequence : 1;
  guint          modifiers_dropped : 1;
};

Owen Taylor's avatar
Owen Taylor committed
54
struct _GtkComposeTable 
55
{
Matthias Clasen's avatar
Matthias Clasen committed
56
  const guint16 *data;
Owen Taylor's avatar
Owen Taylor committed
57 58
  gint max_seq_len;
  gint n_seqs;
59 60
};

61 62 63 64 65 66
struct _GtkComposeTableCompact
{
  const guint16 *data;
  gint max_seq_len;
  gint n_index_size;
  gint n_index_stride;
Owen Taylor's avatar
Owen Taylor committed
67 68
};

69
/* This file contains the table of the compose sequences,
70
 * static const guint16 gtk_compose_seqs_compact[] = {}
71
 * It is generated from the compose-parse.py script.
72 73 74
 */
#include "gtkimcontextsimpleseqs.h"

75 76
/* From the values below, the value 24 means the number of different first keysyms
 * that exist in the Compose file (from Xorg). When running compose-parse.py without
77 78 79 80 81 82
 * parameters, you get the count that you can put here. Needed when updating the
 * gtkimcontextsimpleseqs.h header file (contains the compose sequences).
 */
static const GtkComposeTableCompact gtk_compose_table_compact = {
  gtk_compose_seqs_compact,
  5,
83
  24,
84
  6
85 86
};

Matthias Clasen's avatar
Matthias Clasen committed
87
static const guint16 gtk_compose_ignore[] = {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  GDK_KEY_Shift_L,
  GDK_KEY_Shift_R,
  GDK_KEY_Control_L,
  GDK_KEY_Control_R,
  GDK_KEY_Caps_Lock,
  GDK_KEY_Shift_Lock,
  GDK_KEY_Meta_L,
  GDK_KEY_Meta_R,
  GDK_KEY_Alt_L,
  GDK_KEY_Alt_R,
  GDK_KEY_Super_L,
  GDK_KEY_Super_R,
  GDK_KEY_Hyper_L,
  GDK_KEY_Hyper_R,
  GDK_KEY_Mode_switch,
  GDK_KEY_ISO_Level3_Shift
104 105
};

Owen Taylor's avatar
Owen Taylor committed
106 107 108 109 110 111 112 113
static void     gtk_im_context_simple_finalize           (GObject                  *obj);
static gboolean gtk_im_context_simple_filter_keypress    (GtkIMContext             *context,
							  GdkEventKey              *key);
static void     gtk_im_context_simple_reset              (GtkIMContext             *context);
static void     gtk_im_context_simple_get_preedit_string (GtkIMContext             *context,
							  gchar                   **str,
							  PangoAttrList           **attrs,
							  gint                     *cursor_pos);
114

Matthias Clasen's avatar
Matthias Clasen committed
115
G_DEFINE_TYPE (GtkIMContextSimple, gtk_im_context_simple, GTK_TYPE_IM_CONTEXT)
116 117 118 119 120

static void
gtk_im_context_simple_class_init (GtkIMContextSimpleClass *class)
{
  GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
Owen Taylor's avatar
Owen Taylor committed
121 122
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

123
  im_context_class->filter_keypress = gtk_im_context_simple_filter_keypress;
Owen Taylor's avatar
Owen Taylor committed
124 125 126
  im_context_class->reset = gtk_im_context_simple_reset;
  im_context_class->get_preedit_string = gtk_im_context_simple_get_preedit_string;
  gobject_class->finalize = gtk_im_context_simple_finalize;
127

128
  g_type_class_add_private (class, sizeof (GtkIMContextSimplePrivate));
129 130 131 132
}

static void
gtk_im_context_simple_init (GtkIMContextSimple *im_context_simple)
133 134 135
{
  im_context_simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (im_context_simple,
                                                         GTK_TYPE_IM_CONTEXT_SIMPLE,
136
                                                         GtkIMContextSimplePrivate);
137 138
}

Owen Taylor's avatar
Owen Taylor committed
139 140 141 142
static void
gtk_im_context_simple_finalize (GObject *obj)
{
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (obj);
143
  GtkIMContextSimplePrivate *priv = context_simple->priv;
Owen Taylor's avatar
Owen Taylor committed
144

145
  if (priv->tables)
146
    {
147 148
      g_slist_foreach (priv->tables, (GFunc)g_free, NULL);
      g_slist_free (priv->tables);
149

150
      priv->tables = NULL;
151 152
    }

Matthias Clasen's avatar
Matthias Clasen committed
153
  G_OBJECT_CLASS (gtk_im_context_simple_parent_class)->finalize (obj);
Owen Taylor's avatar
Owen Taylor committed
154 155
}

156
/**
Matthias Clasen's avatar
Matthias Clasen committed
157 158 159 160 161 162
 * gtk_im_context_simple_new:
 * 
 * Creates a new #GtkIMContextSimple.
 *
 * Returns: a new #GtkIMContextSimple.
 **/
163 164 165
GtkIMContext *
gtk_im_context_simple_new (void)
{
Manish Singh's avatar
Manish Singh committed
166
  return g_object_new (GTK_TYPE_IM_CONTEXT_SIMPLE, NULL);
167 168 169 170
}

static void
gtk_im_context_simple_commit_char (GtkIMContext *context,
171
				   gunichar ch)
172
{
173
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
174
  GtkIMContextSimplePrivate *priv = context_simple->priv;
175
  gchar buf[10];
176
  gint len;
Owen Taylor's avatar
Owen Taylor committed
177

178
  g_return_if_fail (g_unichar_validate (ch));
179

180
  len = g_unichar_to_utf8 (ch, buf);
181 182
  buf[len] = '\0';

183
  if (priv->tentative_match || priv->in_hex_sequence)
184
    {
185 186 187
      priv->in_hex_sequence = FALSE;
      priv->tentative_match = 0;
      priv->tentative_match_len = 0;
188 189
      g_signal_emit_by_name (context_simple, "preedit-changed");
      g_signal_emit_by_name (context_simple, "preedit-end");
190
    }
Owen Taylor's avatar
Owen Taylor committed
191

192
  g_signal_emit_by_name (context, "commit", &buf);
193 194
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208
static int
compare_seq_index (const void *key, const void *value)
{
  const guint *keysyms = key;
  const guint16 *seq = value;

  if (keysyms[0] < seq[0])
    return -1;
  else if (keysyms[0] > seq[0])
    return 1;

  return 0;
}

209 210 211 212 213
static int
compare_seq (const void *key, const void *value)
{
  int i = 0;
  const guint *keysyms = key;
Owen Taylor's avatar
Owen Taylor committed
214
  const guint16 *seq = value;
215 216 217

  while (keysyms[i])
    {
Owen Taylor's avatar
Owen Taylor committed
218
      if (keysyms[i] < seq[i])
219
	return -1;
Owen Taylor's avatar
Owen Taylor committed
220
      else if (keysyms[i] > seq[i])
221
	return 1;
222

223 224 225 226 227 228
      i++;
    }

  return 0;
}

Owen Taylor's avatar
Owen Taylor committed
229 230 231 232 233
static gboolean
check_table (GtkIMContextSimple    *context_simple,
	     const GtkComposeTable *table,
	     gint                   n_compose)
{
234
  GtkIMContextSimplePrivate *priv = context_simple->priv;
Owen Taylor's avatar
Owen Taylor committed
235
  gint row_stride = table->max_seq_len + 2; 
Matthias Clasen's avatar
Matthias Clasen committed
236 237 238 239 240 241 242 243
  guint16 *seq; 
  
  /* Will never match, if the sequence in the compose buffer is longer
   * than the sequences in the table.  Further, compare_seq (key, val)
   * will overrun val if key is longer than val. */
  if (n_compose > table->max_seq_len)
    return FALSE;
  
244
  seq = bsearch (priv->compose_buffer,
Matthias Clasen's avatar
Matthias Clasen committed
245 246 247
		 table->data, table->n_seqs,
		 sizeof (guint16) *  row_stride, 
		 compare_seq);
Owen Taylor's avatar
Owen Taylor committed
248 249 250 251 252 253

  if (seq)
    {
      guint16 *prev_seq;

      /* Back up to the first sequence that matches to make sure
254
       * we find the exact match if there is one.
Owen Taylor's avatar
Owen Taylor committed
255 256 257 258
       */
      while (seq > table->data)
	{
	  prev_seq = seq - row_stride;
259
	  if (compare_seq (priv->compose_buffer, prev_seq) != 0)
Owen Taylor's avatar
Owen Taylor committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	    break;
	  seq = prev_seq;
	}
      
      if (n_compose == table->max_seq_len ||
	  seq[n_compose] == 0) /* complete sequence */
	{
	  guint16 *next_seq;
	  gunichar value = 
	    0x10000 * seq[table->max_seq_len] + seq[table->max_seq_len + 1];

	  /* We found a tentative match. See if there are any longer
	   * sequences containing this subsequence
	   */
	  next_seq = seq + row_stride;
	  if (next_seq < table->data + row_stride * table->n_seqs)
	    {
277
	      if (compare_seq (priv->compose_buffer, next_seq) == 0)
Owen Taylor's avatar
Owen Taylor committed
278
		{
279 280
		  priv->tentative_match = value;
		  priv->tentative_match_len = n_compose;
281

282
		  g_signal_emit_by_name (context_simple, "preedit-changed");
Owen Taylor's avatar
Owen Taylor committed
283 284 285 286 287 288

		  return TRUE;
		}
	    }

	  gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
289
	  priv->compose_buffer[0] = 0;
Owen Taylor's avatar
Owen Taylor committed
290 291 292 293 294 295 296 297
	}
      
      return TRUE;
    }

  return FALSE;
}

298
/* Checks if a keysym is a dead key. Dead key keysym values are defined in
299
 * ../gdk/gdkkeysyms.h and the first is GDK_KEY_dead_grave. As X.Org is updated,
300
 * more dead keys are added and we need to update the upper limit.
301 302
 * Currently, the upper limit is GDK_KEY_dead_dasia+1. The +1 has to do with
 * a temporary issue in the X.Org header files.
303 304 305
 * In future versions it will be just the keysym (no +1).
 */
#define IS_DEAD_KEY(k) \
306
    ((k) >= GDK_KEY_dead_grave && (k) <= (GDK_KEY_dead_dasia+1))
307 308 309 310 311 312 313 314 315 316 317 318 319 320

#ifdef GDK_WINDOWING_WIN32

/* On Windows, user expectation is that typing a dead accent followed
 * by space will input the corresponding spacing character. The X
 * compose tables are different for dead acute and diaeresis, which
 * when followed by space produce a plain ASCII apostrophe and double
 * quote respectively. So special-case those.
 */

static gboolean
check_win32_special_cases (GtkIMContextSimple    *context_simple,
			   gint                   n_compose)
{
321
  GtkIMContextSimplePrivate *priv = context_simple->priv;
322
  if (n_compose == 2 &&
323
      priv->compose_buffer[1] == GDK_KEY_space)
324 325 326
    {
      gunichar value = 0;

327
      switch (priv->compose_buffer[0])
328
	{
329
	case GDK_KEY_dead_acute:
330
	  value = 0x00B4; break;
331
	case GDK_KEY_dead_diaeresis:
332 333 334 335 336
	  value = 0x00A8; break;
	}
      if (value > 0)
	{
	  gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
337
	  priv->compose_buffer[0] = 0;
338 339 340 341 342 343 344 345 346 347 348 349 350

	  GTK_NOTE (MISC, g_print ("win32: U+%04X\n", value));
	  return TRUE;
	}
    }
  return FALSE;
}

static void
check_win32_special_case_after_compact_match (GtkIMContextSimple    *context_simple,
					      gint                   n_compose,
					      guint                  value)
{
351
  GtkIMContextSimplePrivate *priv = context_simple->priv;
352

353 354 355 356
  /* On Windows user expectation is that typing two dead accents will input
   * two corresponding spacing accents.
   */
  if (n_compose == 2 &&
357 358
      priv->compose_buffer[0] == priv->compose_buffer[1] &&
      IS_DEAD_KEY (priv->compose_buffer[0]))
359 360 361 362 363 364 365 366
    {
      gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
      GTK_NOTE (MISC, g_print ("win32: U+%04X ", value));
    }
}

#endif

367
static gboolean
368 369 370
check_compact_table (GtkIMContextSimple           *context_simple,
                     const GtkComposeTableCompact *table,
                     gint                          n_compose)
371
{
372
  GtkIMContextSimplePrivate *priv = context_simple->priv;
373 374
  gint row_stride;
  guint16 *seq_index;
375
  guint16 *seq;
376
  gint i;
377 378
  gboolean match;
  gunichar value;
379 380 381

  /* Will never match, if the sequence in the compose buffer is longer
   * than the sequences in the table.  Further, compare_seq (key, val)
382 383
   * will overrun val if key is longer than val.
   */
384 385
  if (n_compose > table->max_seq_len)
    return FALSE;
386 387

  seq_index = bsearch (priv->compose_buffer,
388 389 390 391
                       table->data,
                       table->n_index_size,
                       sizeof (guint16) * table->n_index_stride,
                       compare_seq_index);
392 393

  if (!seq_index)
394 395 396 397
    {
      GTK_NOTE (MISC, g_print ("compact: no\n"));
      return FALSE;
    }
398 399

  if (seq_index && n_compose == 1)
400 401 402 403
    {
      GTK_NOTE (MISC, g_print ("compact: yes\n"));
      return TRUE;
    }
404

405
  GTK_NOTE (MISC, g_print ("compact: %d ", *seq_index));
406
  seq = NULL;
407
  match = FALSE;
408

409
  for (i = n_compose - 1; i < table->max_seq_len; i++)
410 411 412
    {
      row_stride = i + 1;

413
      if (seq_index[i + 1] - seq_index[i] > 0)
414
        {
415 416 417 418 419
          seq = bsearch (priv->compose_buffer + 1,
                         table->data + seq_index[i],
                         (seq_index[i + 1] - seq_index[i]) / row_stride,
                         sizeof (guint16) *  row_stride,
                         compare_seq);
420

421
          if (seq)
422 423
            {
              if (i == n_compose - 1)
424 425 426 427
                {
                  value = seq[row_stride - 1];
                  match = TRUE;
                }
428 429
              else
                {
430 431 432 433 434 435 436
                  if (match)
                    {
                      GTK_NOTE (MISC, g_print ("tentative match U+%04X ", value));
                      priv->tentative_match = value;
                      priv->tentative_match_len = n_compose;
                    }

437
                  g_signal_emit_by_name (context_simple, "preedit-changed");
438

439 440
                  GTK_NOTE (MISC, g_print ("yes\n"));
                  return TRUE;
441 442 443 444 445
                }
             }
        }
    }

446
  if (match)
447 448
    {
      gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
449 450 451
#ifdef G_OS_WIN32
      check_win32_special_case_after_compact_match (context_simple, n_compose, value);
#endif
452
      priv->compose_buffer[0] = 0;
453

454
      GTK_NOTE (MISC, g_print ("U+%04X\n", value));
455 456 457
      return TRUE;
    }

458
  GTK_NOTE (MISC, g_print ("no\n"));
459 460 461
  return FALSE;
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
/* This function receives a sequence of Unicode characters and tries to
 * normalize it (NFC). We check for the case the the resulting string
 * has length 1 (single character).
 * NFC normalisation normally rearranges diacritic marks, unless these
 * belong to the same Canonical Combining Class.
 * If they belong to the same canonical combining class, we produce all
 * permutations of the diacritic marks, then attempt to normalize.
 */
static gboolean
check_normalize_nfc (gunichar* combination_buffer, gint n_compose)
{
  gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
  gchar *combination_utf8_temp = NULL;
  gchar *nfc_temp = NULL;
  gint n_combinations;
  gunichar temp_swap;
  gint i;

  n_combinations = 1;

  for (i = 1; i < n_compose; i++ )
     n_combinations *= i;

  /* Xorg reuses dead_tilde for the perispomeni diacritic mark.
   * We check if base character belongs to Greek Unicode block,
487 488
   * and if so, we replace tilde with perispomeni.
   */
489 490 491 492 493 494 495 496 497 498 499 500 501
  if (combination_buffer[0] >= 0x390 && combination_buffer[0] <= 0x3FF)
    {
      for (i = 1; i < n_compose; i++ )
        if (combination_buffer[i] == 0x303)
          combination_buffer[i] = 0x342;
    }

  memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );

  for (i = 0; i < n_combinations; i++ )
    {
      g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
      combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
502
      nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

      if (g_utf8_strlen (nfc_temp, -1) == 1)
        {
          memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );

          g_free (combination_utf8_temp);
          g_free (nfc_temp);

          return TRUE;
        }

      g_free (combination_utf8_temp);
      g_free (nfc_temp);

      if (n_compose > 2)
        {
          temp_swap = combination_buffer_temp[i % (n_compose - 1) + 1];
          combination_buffer_temp[i % (n_compose - 1) + 1] = combination_buffer_temp[(i+1) % (n_compose - 1) + 1];
          combination_buffer_temp[(i+1) % (n_compose - 1) + 1] = temp_swap;
        }
      else
        break;
    }

  return FALSE;
}

530
static gboolean
531 532
check_algorithmically (GtkIMContextSimple *context_simple,
                       gint                n_compose)
533 534

{
535
  GtkIMContextSimplePrivate *priv = context_simple->priv;
536
  gint i;
537 538 539 540 541 542
  gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
  gchar *combination_utf8, *nfc;

  if (n_compose >= GTK_MAX_COMPOSE_LEN)
    return FALSE;

543
  for (i = 0; i < n_compose && IS_DEAD_KEY (priv->compose_buffer[i]); i++)
544 545 546 547 548 549
    ;
  if (i == n_compose)
    return TRUE;

  if (i > 0 && i == n_compose - 1)
    {
550
      combination_buffer[0] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
551 552 553 554
      combination_buffer[n_compose] = 0;
      i--;
      while (i >= 0)
	{
555
	  switch (priv->compose_buffer[i])
556 557
	    {
#define CASE(keysym, unicode) \
558
	    case GDK_KEY_dead_##keysym: combination_buffer[i+1] = unicode; break
559 560 561 562

	    CASE (grave, 0x0300);
	    CASE (acute, 0x0301);
	    CASE (circumflex, 0x0302);
563
	    CASE (tilde, 0x0303);	/* Also used with perispomeni, 0x342. */
564 565 566 567
	    CASE (macron, 0x0304);
	    CASE (breve, 0x0306);
	    CASE (abovedot, 0x0307);
	    CASE (diaeresis, 0x0308);
568
	    CASE (hook, 0x0309);
569 570 571
	    CASE (abovering, 0x030A);
	    CASE (doubleacute, 0x030B);
	    CASE (caron, 0x030C);
572 573 574 575
	    CASE (abovecomma, 0x0313);         /* Equivalent to psili */
	    CASE (abovereversedcomma, 0x0314); /* Equivalent to dasia */
	    CASE (horn, 0x031B);	/* Legacy use for psili, 0x313 (or 0x343). */
	    CASE (belowdot, 0x0323);
576
	    CASE (cedilla, 0x0327);
577 578
	    CASE (ogonek, 0x0328);	/* Legacy use for dasia, 0x314.*/
	    CASE (iota, 0x0345);
579
	    CASE (voiced_sound, 0x3099);	/* Per Markus Kuhn keysyms.txt file. */
580 581 582 583 584 585 586 587 588 589
	    CASE (semivoiced_sound, 0x309A);	/* Per Markus Kuhn keysyms.txt file. */

	    /* The following cases are to be removed once xkeyboard-config,
 	     * xorg are fully updated.
 	     */
            /* Workaround for typo in 1.4.x xserver-xorg */
	    case 0xfe66: combination_buffer[i+1] = 0x314; break;
	    /* CASE (dasia, 0x314); */
	    /* CASE (perispomeni, 0x342); */
	    /* CASE (psili, 0x343); */
590 591
#undef CASE
	    default:
592
	      combination_buffer[i+1] = gdk_keyval_to_unicode (priv->compose_buffer[i]);
593 594 595 596
	    }
	  i--;
	}
      
597 598
      /* If the buffer normalizes to a single character, then modify the order
       * of combination_buffer accordingly, if necessary, and return TRUE.
599 600 601
       */
      if (check_normalize_nfc (combination_buffer, n_compose))
        {
Matthias Clasen's avatar
Matthias Clasen committed
602
          gunichar value;
603 604
      	  combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
          nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
605

Matthias Clasen's avatar
Matthias Clasen committed
606
          value = g_utf8_get_char (nfc);
607
          gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
608
          priv->compose_buffer[0] = 0;
609

610 611 612 613 614
          g_free (combination_utf8);
          g_free (nfc);

          return TRUE;
        }
615 616 617 618 619
    }

  return FALSE;
}

Havoc Pennington's avatar
Havoc Pennington committed
620
/* In addition to the table-driven sequences, we allow Unicode hex
621 622
 * codes to be entered. The method chosen here is similar to the
 * one recommended in ISO 14755, but not exactly the same, since we
623 624
 * don't want to steal 16 valuable key combinations.
 *
625 626 627 628 629 630 631 632 633
 * A hex Unicode sequence must be started with Ctrl-Shift-U, followed
 * by a sequence of hex digits entered with Ctrl-Shift still held.
 * Releasing one of the modifiers or pressing space while the modifiers
 * are still held commits the character. It is possible to erase
 * digits using backspace.
 *
 * As an extension to the above, we also allow to start the sequence
 * with Ctrl-Shift-U, then release the modifiers before typing any
 * digits, and enter the digits without modifiers.
Havoc Pennington's avatar
Havoc Pennington committed
634 635
 */

636
static gboolean
Havoc Pennington's avatar
Havoc Pennington committed
637 638
check_hex (GtkIMContextSimple *context_simple,
           gint                n_compose)
639
{
640
  GtkIMContextSimplePrivate *priv = context_simple->priv;
Havoc Pennington's avatar
Havoc Pennington committed
641 642 643 644 645
  /* See if this is a hex sequence, return TRUE if so */
  gint i;
  GString *str;
  gulong n;
  gchar *nptr = NULL;
646
  gchar buf[7];
647

648 649
  priv->tentative_match = 0;
  priv->tentative_match_len = 0;
650

Havoc Pennington's avatar
Havoc Pennington committed
651 652 653 654 655 656 657
  str = g_string_new (NULL);
  
  i = 0;
  while (i < n_compose)
    {
      gunichar ch;
      
658
      ch = gdk_keyval_to_unicode (priv->compose_buffer[i]);
Havoc Pennington's avatar
Havoc Pennington committed
659 660 661
      
      if (ch == 0)
        return FALSE;
662

Havoc Pennington's avatar
Havoc Pennington committed
663 664
      if (!g_unichar_isxdigit (ch))
        return FALSE;
665

Havoc Pennington's avatar
Havoc Pennington committed
666
      buf[g_unichar_to_utf8 (ch, buf)] = '\0';
667

Havoc Pennington's avatar
Havoc Pennington committed
668 669 670 671
      g_string_append (str, buf);
      
      ++i;
    }
672

Havoc Pennington's avatar
Havoc Pennington committed
673 674
  n = strtoul (str->str, &nptr, 16);

675
  /* If strtoul fails it probably means non-latin digits were used;
Havoc Pennington's avatar
Havoc Pennington committed
676 677
   * we should in principle handle that, but we probably don't.
   */
678
  if (nptr - str->str < str->len)
Owen Taylor's avatar
Owen Taylor committed
679
    {
Havoc Pennington's avatar
Havoc Pennington committed
680 681
      g_string_free (str, TRUE);
      return FALSE;
Owen Taylor's avatar
Owen Taylor committed
682
    }
Havoc Pennington's avatar
Havoc Pennington committed
683 684 685
  else
    g_string_free (str, TRUE);

686 687
  if (g_unichar_validate (n))
    {
688 689
      priv->tentative_match = n;
      priv->tentative_match_len = n_compose;
690
    }
Havoc Pennington's avatar
Havoc Pennington committed
691 692 693 694
  
  return TRUE;
}

695 696 697
static void
beep_window (GdkWindow *window)
{
698
  GdkScreen *screen = gdk_window_get_screen (window);
699
  gboolean   beep;
700

701 702 703
  g_object_get (gtk_settings_get_for_screen (screen),
                "gtk-error-bell", &beep,
                NULL);
704

705 706
  if (beep)
    gdk_window_beep (window);
707 708
}

Havoc Pennington's avatar
Havoc Pennington committed
709 710 711 712 713
static gboolean
no_sequence_matches (GtkIMContextSimple *context_simple,
                     gint                n_compose,
                     GdkEventKey        *event)
{
714
  GtkIMContextSimplePrivate *priv = context_simple->priv;
Havoc Pennington's avatar
Havoc Pennington committed
715 716
  GtkIMContext *context;
  gunichar ch;
Owen Taylor's avatar
Owen Taylor committed
717
  
Havoc Pennington's avatar
Havoc Pennington committed
718
  context = GTK_IM_CONTEXT (context_simple);
Owen Taylor's avatar
Owen Taylor committed
719 720 721 722
  
  /* No compose sequences found, check first if we have a partial
   * match pending.
   */
723
  if (priv->tentative_match)
Owen Taylor's avatar
Owen Taylor committed
724
    {
725
      gint len = priv->tentative_match_len;
Owen Taylor's avatar
Owen Taylor committed
726 727
      int i;
      
728 729
      gtk_im_context_simple_commit_char (context, priv->tentative_match);
      priv->compose_buffer[0] = 0;
Owen Taylor's avatar
Owen Taylor committed
730 731 732
      
      for (i=0; i < n_compose - len - 1; i++)
	{
733
	  GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
734
	  tmp_event->key.keyval = priv->compose_buffer[len + i];
Owen Taylor's avatar
Owen Taylor committed
735
	  
736
	  gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
737
	  gdk_event_free (tmp_event);
Owen Taylor's avatar
Owen Taylor committed
738
	}
739

Owen Taylor's avatar
Owen Taylor committed
740 741 742
      return gtk_im_context_filter_keypress (context, event);
    }
  else
743
    {
744
      priv->compose_buffer[0] = 0;
Owen Taylor's avatar
Owen Taylor committed
745
      if (n_compose > 1)		/* Invalid sequence */
746
	{
747
	  beep_window (event->window);
Owen Taylor's avatar
Owen Taylor committed
748
	  return TRUE;
749
	}
Owen Taylor's avatar
Owen Taylor committed
750 751
  
      ch = gdk_keyval_to_unicode (event->keyval);
752
      if (ch != 0 && !g_unichar_iscntrl (ch))
Owen Taylor's avatar
Owen Taylor committed
753 754 755 756 757 758
	{
	  gtk_im_context_simple_commit_char (context, ch);
	  return TRUE;
	}
      else
	return FALSE;
759
    }
Owen Taylor's avatar
Owen Taylor committed
760
}
761

Havoc Pennington's avatar
Havoc Pennington committed
762 763 764 765 766 767 768 769 770 771 772
static gboolean
is_hex_keyval (guint keyval)
{
  gunichar ch = gdk_keyval_to_unicode (keyval);

  return g_unichar_isxdigit (ch);
}

static guint
canonical_hex_keyval (GdkEventKey *event)
{
773
  GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_window_get_display (event->window));
Havoc Pennington's avatar
Havoc Pennington committed
774 775 776 777 778 779 780 781 782 783 784 785
  guint keyval;
  guint *keyvals = NULL;
  gint n_vals = 0;
  gint i;
  
  /* See if the keyval is already a hex digit */
  if (is_hex_keyval (event->keyval))
    return event->keyval;

  /* See if this key would have generated a hex keyval in
   * any other state, and return that hex keyval if so
   */
786 787 788 789
  gdk_keymap_get_entries_for_keycode (keymap,
				      event->hardware_keycode,
				      NULL,
				      &keyvals, &n_vals);
Havoc Pennington's avatar
Havoc Pennington committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

  keyval = 0;
  i = 0;
  while (i < n_vals)
    {
      if (is_hex_keyval (keyvals[i]))
        {
          keyval = keyvals[i];
          break;
        }

      ++i;
    }

  g_free (keyvals);
  
  if (keyval)
    return keyval;
  else
809
    /* No way to make it a hex digit
Havoc Pennington's avatar
Havoc Pennington committed
810
     */
811
    return 0;
Havoc Pennington's avatar
Havoc Pennington committed
812 813 814 815 816 817 818
}

static gboolean
gtk_im_context_simple_filter_keypress (GtkIMContext *context,
				       GdkEventKey  *event)
{
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
819
  GtkIMContextSimplePrivate *priv = context_simple->priv;
820
  GdkDisplay *display = gdk_window_get_display (event->window);
Havoc Pennington's avatar
Havoc Pennington committed
821 822
  GSList *tmp_list;  
  int n_compose = 0;
823
  GdkModifierType hex_mod_mask;
824
  gboolean have_hex_mods;
825
  gboolean is_hex_start;
826
  gboolean is_hex_end;
827
  gboolean is_backspace;
828
  gboolean is_escape;
829
  guint hex_keyval;
Havoc Pennington's avatar
Havoc Pennington committed
830 831
  int i;

832
  while (priv->compose_buffer[n_compose] != 0)
833 834
    n_compose++;

835 836
  if (event->type == GDK_KEY_RELEASE)
    {
837
      if (priv->in_hex_sequence &&
838 839
	  (event->keyval == GDK_KEY_Control_L || event->keyval == GDK_KEY_Control_R ||
	   event->keyval == GDK_KEY_Shift_L || event->keyval == GDK_KEY_Shift_R))
840
	{
841 842
	  if (priv->tentative_match &&
	      g_unichar_validate (priv->tentative_match))
843
	    {
844 845
	      gtk_im_context_simple_commit_char (context, priv->tentative_match);
	      priv->compose_buffer[0] = 0;
846 847 848 849

	    }
	  else if (n_compose == 0)
	    {
850
	      priv->modifiers_dropped = TRUE;
851 852
	    }
	  else
853 854
	    {
	      /* invalid hex sequence */
855
	      beep_window (event->window);
856
	      
857 858 859
	      priv->tentative_match = 0;
	      priv->in_hex_sequence = FALSE;
	      priv->compose_buffer[0] = 0;
860
	      
861 862
	      g_signal_emit_by_name (context_simple, "preedit-changed");
	      g_signal_emit_by_name (context_simple, "preedit-end");
863
	    }
864 865 866 867 868 869

	  return TRUE;
	}
      else
	return FALSE;
    }
870

871
  /* Ignore modifier key presses */
872
  for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
Havoc Pennington's avatar
Havoc Pennington committed
873 874 875
    if (event->keyval == gtk_compose_ignore[i])
      return FALSE;

876 877 878 879
  hex_mod_mask = gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
                                               GDK_MODIFIER_INTENT_PRIMARY_ACCELERATOR);
  hex_mod_mask |= GDK_SHIFT_MASK;

880
  if (priv->in_hex_sequence && priv->modifiers_dropped)
881 882
    have_hex_mods = TRUE;
  else
883
    have_hex_mods = (event->state & (hex_mod_mask)) == hex_mod_mask;
884 885 886 887 888 889 890 891
  is_hex_start = event->keyval == GDK_KEY_U;
  is_hex_end = (event->keyval == GDK_KEY_space ||
		event->keyval == GDK_KEY_KP_Space ||
		event->keyval == GDK_KEY_Return ||
		event->keyval == GDK_KEY_ISO_Enter ||
		event->keyval == GDK_KEY_KP_Enter);
  is_backspace = event->keyval == GDK_KEY_BackSpace;
  is_escape = event->keyval == GDK_KEY_Escape;
892 893
  hex_keyval = canonical_hex_keyval (event);

Havoc Pennington's avatar
Havoc Pennington committed
894
  /* If we are already in a non-hex sequence, or
895 896 897 898 899
   * this keystroke is not hex modifiers + hex digit, don't filter
   * key events with accelerator modifiers held down. We only treat
   * Control and Alt as accel modifiers here, since Super, Hyper and
   * Meta are often co-located with Mode_Switch, Multi_Key or
   * ISO_Level3_Switch.
Havoc Pennington's avatar
Havoc Pennington committed
900
   */
901
  if (!have_hex_mods ||
902 903 904
      (n_compose > 0 && !priv->in_hex_sequence) ||
      (n_compose == 0 && !priv->in_hex_sequence && !is_hex_start) ||
      (priv->in_hex_sequence && !hex_keyval &&
905
       !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
Havoc Pennington's avatar
Havoc Pennington committed
906
    {
907 908 909 910 911 912 913 914 915 916
      GdkDisplay *display;
      GdkModifierType no_text_input_mask;

      display = gdk_window_get_display (event->window);

      no_text_input_mask =
        gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
                                      GDK_MODIFIER_INTENT_NO_TEXT_INPUT);

      if (event->state & no_text_input_mask ||
917
	  (priv->in_hex_sequence && priv->modifiers_dropped &&
918 919 920
	   (event->keyval == GDK_KEY_Return ||
	    event->keyval == GDK_KEY_ISO_Enter ||
	    event->keyval == GDK_KEY_KP_Enter)))
921 922 923
	{
	  return FALSE;
	}
924 925 926
    }
  
  /* Handle backspace */
927
  if (priv->in_hex_sequence && have_hex_mods && is_backspace)
928 929 930 931
    {
      if (n_compose > 0)
	{
	  n_compose--;
932
	  priv->compose_buffer[n_compose] = 0;
933 934 935 936
          check_hex (context_simple, n_compose);
	}
      else
	{
937
	  priv->in_hex_sequence = FALSE;
938 939
	}

940
      g_signal_emit_by_name (context_simple, "preedit-changed");
941

942
      if (!priv->in_hex_sequence)
943
        g_signal_emit_by_name (context_simple, "preedit-end");
944 945
      
      return TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
946
    }
947

948
  /* Check for hex sequence restart */
949
  if (priv->in_hex_sequence && have_hex_mods && is_hex_start)
950
    {
951 952
      if (priv->tentative_match &&
	  g_unichar_validate (priv->tentative_match))
953
	{
954 955
	  gtk_im_context_simple_commit_char (context, priv->tentative_match);
	  priv->compose_buffer[0] = 0;
956 957 958 959 960
	}
      else 
	{
	  /* invalid hex sequence */
	  if (n_compose > 0)
961
	    beep_window (event->window);
962
	  
963 964 965
	  priv->tentative_match = 0;
	  priv->in_hex_sequence = FALSE;
	  priv->compose_buffer[0] = 0;
966 967
	}
    }
968
  
969
  /* Check for hex sequence start */
970
  if (!priv->in_hex_sequence && have_hex_mods && is_hex_start)
971
    {
972 973 974 975
      priv->compose_buffer[0] = 0;
      priv->in_hex_sequence = TRUE;
      priv->modifiers_dropped = FALSE;
      priv->tentative_match = 0;
976

977 978
      g_signal_emit_by_name (context_simple, "preedit-start");
      g_signal_emit_by_name (context_simple, "preedit-changed");
Havoc Pennington's avatar
Havoc Pennington committed
979
  
980 981
      return TRUE;
    }
982
  
983
  /* Then, check for compose sequences */
984
  if (priv->in_hex_sequence)
985 986
    {
      if (hex_keyval)
987
	priv->compose_buffer[n_compose++] = hex_keyval;
988 989 990 991 992 993 994
      else if (is_escape)
	{
	  gtk_im_context_simple_reset (context);
	  
	  return TRUE;
	}
      else if (!is_hex_end)
995 996
	{
	  /* non-hex character in hex sequence */
997
	  beep_window (event->window);
998 999 1000 1001
	  
	  return TRUE;
	}
    }
Havoc Pennington's avatar
Havoc Pennington committed
1002
  else
1003
    priv->compose_buffer[n_compose++] = event->keyval;
Havoc Pennington's avatar
Havoc Pennington committed
1004

1005
  priv->compose_buffer[n_compose] = 0;
Havoc Pennington's avatar
Havoc Pennington committed
1006

1007
  if (priv->in_hex_sequence)
Havoc Pennington's avatar
Havoc Pennington committed
1008 1009
    {
      /* If the modifiers are still held down, consider the sequence again */
1010
      if (have_hex_mods)
Havoc Pennington's avatar
Havoc Pennington committed
1011
        {
1012 1013
          /* space or return ends the sequence, and we eat the key */
          if (n_compose > 0 && is_hex_end)
Havoc Pennington's avatar
Havoc Pennington committed
1014
            {
1015 1016
	      if (priv->tentative_match &&
		  g_unichar_validate (priv->tentative_match))
1017
		{
1018 1019
		  gtk_im_context_simple_commit_char (context, priv->tentative_match);
		  priv->compose_buffer[0] = 0;
1020 1021 1022 1023
		}
	      else
		{
		  /* invalid hex sequence */
1024
		  beep_window (event->window);
1025

1026 1027 1028
		  priv->tentative_match = 0;
		  priv->in_hex_sequence = FALSE;
		  priv->compose_buffer[0] = 0;
1029
		}
Havoc Pennington's avatar
Havoc Pennington committed
1030
            }
1031
          else if (!check_hex (context_simple, n_compose))
1032
	    beep_window (event->window);
1033
	  
1034
	  g_signal_emit_by_name (context_simple, "preedit-changed");
1035

1036
	  if (!priv->in_hex_sequence)
1037
	    g_signal_emit_by_name (context_simple, "preedit-end");
1038

1039
	  return TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
1040 1041 1042 1043
        }
    }
  else
    {
1044
      tmp_list = priv->tables;
Havoc Pennington's avatar
Havoc Pennington committed
1045 1046 1047 1048 1049 1050
      while (tmp_list)
        {
          if (check_table (context_simple, tmp_list->data, n_compose))
            return TRUE;
          tmp_list = tmp_list->next;
        }
1051

1052 1053 1054 1055
      GTK_NOTE (MISC, {
	  g_print ("[ ");
	  for (i = 0; i < n_compose; i++)
	    {
1056
	      const gchar *keyval_name = gdk_keyval_name (priv->compose_buffer[i]);
1057 1058 1059 1060
	      
	      if (keyval_name != NULL)
		g_print ("%s ", keyval_name);
	      else
1061
		g_print ("%04x ", priv->compose_buffer[i]);
1062 1063 1064 1065 1066 1067 1068 1069 1070
	    }
	  g_print ("] ");
	});

#ifdef GDK_WINDOWING_WIN32
      if (check_win32_special_cases (context_simple, n_compose))
	return TRUE;
#endif

1071 1072
      if (check_compact_table (context_simple, &gtk_compose_table_compact, n_compose))
        return TRUE;
1073 1074 1075
  
      if (check_algorithmically (context_simple, n_compose))
	return TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
1076 1077 1078 1079 1080 1081
    }
  
  /* The current compose_buffer doesn't match anything */
  return no_sequence_matches (context_simple, n_compose, event);
}

Owen Taylor's avatar
Owen Taylor committed
1082 1083 1084 1085
static void
gtk_im_context_simple_reset (GtkIMContext *context)
{
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1086
  GtkIMContextSimplePrivate *priv = context_simple->priv;
1087

1088
  priv->compose_buffer[0] = 0;
Owen Taylor's avatar
Owen Taylor committed
1089

1090
  if (priv->tentative_match || priv->in_hex_sequence)
1091
    {
1092 1093 1094
      priv->in_hex_sequence = FALSE;
      priv->tentative_match = 0;
      priv->tentative_match_len = 0;
1095 1096
      g_signal_emit_by_name (context_simple, "preedit-changed");
      g_signal_emit_by_name (context_simple, "preedit-end");
1097
    }
Owen Taylor's avatar
Owen Taylor committed
1098 1099 1100 1101 1102 1103 1104 1105
}

static void     
gtk_im_context_simple_get_preedit_string (GtkIMContext   *context,
					  gchar         **str,
					  PangoAttrList **attrs,
					  gint           *cursor_pos)
{
1106
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
1107
  GtkIMContextSimplePrivate *priv = context_simple->priv;
1108
  char outbuf[37]; /* up to 6 hex digits */
Owen Taylor's avatar
Owen Taylor committed
1109 1110
  int len = 0;

1111
  if (priv->in_hex_sequence)
Havoc Pennington's avatar
Havoc Pennington committed
1112
    {
1113 1114 1115 1116 1117
      int hexchars = 0;
         
      outbuf[0] = 'u';
      len = 1;

1118
      while (priv->compose_buffer[hexchars] != 0)
1119
	{
1120
	  len += g_unichar_to_utf8 (gdk_keyval_to_unicode (priv->compose_buffer[hexchars]),
1121 1122 1123 1124
				    outbuf + len);
	  ++hexchars;
	}

1125
      g_assert (len < 25);
Havoc Pennington's avatar
Havoc Pennington committed
1126
    }
1127 1128
  else if (priv->tentative_match)
    len = g_unichar_to_utf8 (priv->tentative_match, outbuf);
1129
      
1130 1131
  outbuf[len] = '\0';      

Owen Taylor's avatar
Owen Taylor committed
1132
  if (str)
Havoc Pennington's avatar
Havoc Pennington committed
1133
    *str = g_strdup (outbuf);
Owen Taylor's avatar
Owen Taylor committed
1134 1135

  if (attrs)
1136
    {
1137
      *attrs = pango_attr_list_new ();
Havoc Pennington's avatar
Havoc Pennington committed
1138
      
Owen Taylor's avatar
Owen Taylor committed
1139 1140 1141 1142
      if (len)
	{
	  PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
	  attr->start_index = 0;
Havoc Pennington's avatar
Havoc Pennington committed
1143
          attr->end_index = len;
Owen Taylor's avatar
Owen Taylor committed
1144 1145
	  pango_attr_list_insert (*attrs, attr);
	}
1146
    }
Owen Taylor's avatar
Owen Taylor committed
1147 1148

  if (cursor_pos)
1149
    *cursor_pos = len;
Owen Taylor's avatar
Owen Taylor committed
1150 1151 1152
}

/**
1153
 * gtk_im_context_simple_add_table: (skip)
Owen Taylor's avatar
Owen Taylor committed
1154 1155 1156
 * @context_simple: A #GtkIMContextSimple
 * @data: the table 
 * @max_seq_len: Maximum length of a sequence in the table
Matthias Clasen's avatar
Matthias Clasen committed
1157
 *               (cannot be greater than #GTK_MAX_COMPOSE_LEN)
Owen Taylor's avatar
Owen Taylor committed
1158 1159
 * @n_seqs: number of sequences in the table
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1160 1161 1162 1163
 * Adds an additional table to search to the input context.
 * Each row of the table consists of @max_seq_len key symbols
 * followed by two #guint16 interpreted as the high and low
 * words of a #gunicode value. Tables are searched starting
Owen Taylor's avatar
Owen Taylor committed
1164 1165 1166
 * from the last added.
 *
 * The table must be sorted in dictionary order on the
Matthias Clasen's avatar
Matthias Clasen committed
1167
 * numeric value of the key symbol fields. (Values beyond
Owen Taylor's avatar
Owen Taylor committed
1168 1169 1170 1171 1172 1173 1174 1175
 * the length of the sequence should be zero.)
 **/
void
gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
				 guint16            *data,
				 gint                max_seq_len,
				 gint                n_seqs)
{
1176
  GtkIMContextSimplePrivate *priv = context_simple->priv;
Owen Taylor's avatar
Owen Taylor committed
1177 1178 1179 1180 1181
  GtkComposeTable *table;

  g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
  g_return_if_fail (data != NULL);
  g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
1182
  
Owen Taylor's avatar
Owen Taylor committed
1183 1184 1185 1186 1187
  table = g_new (GtkComposeTable, 1);
  table->data = data;
  table->max_seq_len = max_seq_len;
  table->n_seqs = n_seqs;

1188
  priv->tables = g_slist_prepend (priv->tables, table);
1189
}