Blame gtk/gtkimcontextsimple.c

Packit Service fb6fa5
/* GTK - The GIMP Toolkit
Packit Service fb6fa5
 * Copyright (C) 2000 Red Hat, Inc.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * This library is free software; you can redistribute it and/or
Packit Service fb6fa5
 * modify it under the terms of the GNU Lesser General Public
Packit Service fb6fa5
 * License as published by the Free Software Foundation; either
Packit Service fb6fa5
 * version 2 of the License, or (at your option) any later version.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * This library is distributed in the hope that it will be useful,
Packit Service fb6fa5
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fb6fa5
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service fb6fa5
 * Lesser General Public License for more details.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * You should have received a copy of the GNU Lesser General Public
Packit Service fb6fa5
 * License along with this library; if not, write to the
Packit Service fb6fa5
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Service fb6fa5
 * Boston, MA 02111-1307, USA.
Packit Service fb6fa5
 */
Packit Service fb6fa5
Packit Service fb6fa5
#include "config.h"
Packit Service fb6fa5
Packit Service fb6fa5
#include <stdlib.h>
Packit Service fb6fa5
#include <string.h>
Packit Service fb6fa5
Packit Service fb6fa5
#include <gdk/gdkkeysyms.h>
Packit Service fb6fa5
#include "gtkprivate.h"
Packit Service fb6fa5
#include "gtkaccelgroup.h"
Packit Service fb6fa5
#include "gtkimcontextsimple.h"
Packit Service fb6fa5
#include "gtksettings.h"
Packit Service fb6fa5
#include "gtkwidget.h"
Packit Service fb6fa5
#include "gtkintl.h"
Packit Service fb6fa5
#include "gtkalias.h"
Packit Service fb6fa5
Packit Service fb6fa5
#ifdef GDK_WINDOWING_WIN32
Packit Service fb6fa5
#include <win32/gdkwin32keys.h>
Packit Service fb6fa5
#endif
Packit Service fb6fa5
Packit Service fb6fa5
typedef struct _GtkComposeTable GtkComposeTable;
Packit Service fb6fa5
typedef struct _GtkComposeTableCompact GtkComposeTableCompact;
Packit Service fb6fa5
Packit Service fb6fa5
struct _GtkComposeTable 
Packit Service fb6fa5
{
Packit Service fb6fa5
  const guint16 *data;
Packit Service fb6fa5
  gint max_seq_len;
Packit Service fb6fa5
  gint n_seqs;
Packit Service fb6fa5
};
Packit Service fb6fa5
Packit Service fb6fa5
struct _GtkComposeTableCompact
Packit Service fb6fa5
{
Packit Service fb6fa5
  const guint16 *data;
Packit Service fb6fa5
  gint max_seq_len;
Packit Service fb6fa5
  gint n_index_size;
Packit Service fb6fa5
  gint n_index_stride;
Packit Service fb6fa5
};
Packit Service fb6fa5
Packit Service fb6fa5
/* This file contains the table of the compose sequences, 
Packit Service fb6fa5
 * static const guint16 gtk_compose_seqs_compact[] = {}
Packit Service fb6fa5
 * IT is generated from the compose-parse.py script.
Packit Service fb6fa5
 */
Packit Service fb6fa5
#include "gtkimcontextsimpleseqs.h"
Packit Service fb6fa5
Packit Service fb6fa5
/* From the values below, the value 23 means the number of different first keysyms 
Packit Service fb6fa5
 * that exist in the Compose file (from Xorg). When running compose-parse.py without 
Packit Service fb6fa5
 * parameters, you get the count that you can put here. Needed when updating the
Packit Service fb6fa5
 * gtkimcontextsimpleseqs.h header file (contains the compose sequences).
Packit Service fb6fa5
 */
Packit Service fb6fa5
static const GtkComposeTableCompact gtk_compose_table_compact = {
Packit Service fb6fa5
  gtk_compose_seqs_compact,
Packit Service fb6fa5
  5,
Packit Service fb6fa5
  24,
Packit Service fb6fa5
  6
Packit Service fb6fa5
};
Packit Service fb6fa5
Packit Service fb6fa5
static const guint16 gtk_compose_ignore[] = {
Packit Service fb6fa5
  GDK_Shift_L,
Packit Service fb6fa5
  GDK_Shift_R,
Packit Service fb6fa5
  GDK_Control_L,
Packit Service fb6fa5
  GDK_Control_R,
Packit Service fb6fa5
  GDK_Caps_Lock,
Packit Service fb6fa5
  GDK_Shift_Lock,
Packit Service fb6fa5
  GDK_Meta_L,
Packit Service fb6fa5
  GDK_Meta_R,
Packit Service fb6fa5
  GDK_Alt_L,
Packit Service fb6fa5
  GDK_Alt_R,
Packit Service fb6fa5
  GDK_Super_L,
Packit Service fb6fa5
  GDK_Super_R,
Packit Service fb6fa5
  GDK_Hyper_L,
Packit Service fb6fa5
  GDK_Hyper_R,
Packit Service fb6fa5
  GDK_Mode_switch,
Packit Service fb6fa5
  GDK_ISO_Level3_Shift
Packit Service fb6fa5
};
Packit Service fb6fa5
Packit Service fb6fa5
static void     gtk_im_context_simple_finalize           (GObject                  *obj);
Packit Service fb6fa5
static gboolean gtk_im_context_simple_filter_keypress    (GtkIMContext             *context,
Packit Service fb6fa5
							  GdkEventKey              *key);
Packit Service fb6fa5
static void     gtk_im_context_simple_reset              (GtkIMContext             *context);
Packit Service fb6fa5
static void     gtk_im_context_simple_get_preedit_string (GtkIMContext             *context,
Packit Service fb6fa5
							  gchar                   **str,
Packit Service fb6fa5
							  PangoAttrList           **attrs,
Packit Service fb6fa5
							  gint                     *cursor_pos);
Packit Service fb6fa5
Packit Service fb6fa5
G_DEFINE_TYPE (GtkIMContextSimple, gtk_im_context_simple, GTK_TYPE_IM_CONTEXT)
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_simple_class_init (GtkIMContextSimpleClass *class)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
Packit Service fb6fa5
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Packit Service fb6fa5
Packit Service fb6fa5
  im_context_class->filter_keypress = gtk_im_context_simple_filter_keypress;
Packit Service fb6fa5
  im_context_class->reset = gtk_im_context_simple_reset;
Packit Service fb6fa5
  im_context_class->get_preedit_string = gtk_im_context_simple_get_preedit_string;
Packit Service fb6fa5
  gobject_class->finalize = gtk_im_context_simple_finalize;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_simple_init (GtkIMContextSimple *im_context_simple)
Packit Service fb6fa5
{  
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_simple_finalize (GObject *obj)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (obj);
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_simple->tables)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      g_slist_foreach (context_simple->tables, (GFunc)g_free, NULL);
Packit Service fb6fa5
      g_slist_free (context_simple->tables);
Packit Service fb6fa5
Packit Service fb6fa5
      context_simple->tables = NULL;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  G_OBJECT_CLASS (gtk_im_context_simple_parent_class)->finalize (obj);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/** 
Packit Service fb6fa5
 * gtk_im_context_simple_new:
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Creates a new #GtkIMContextSimple.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * Returns: a new #GtkIMContextSimple.
Packit Service fb6fa5
 **/
Packit Service fb6fa5
GtkIMContext *
Packit Service fb6fa5
gtk_im_context_simple_new (void)
Packit Service fb6fa5
{
Packit Service fb6fa5
  return g_object_new (GTK_TYPE_IM_CONTEXT_SIMPLE, NULL);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_simple_commit_char (GtkIMContext *context,
Packit Service fb6fa5
				   gunichar ch)
Packit Service fb6fa5
{
Packit Service fb6fa5
  gchar buf[10];
Packit Service fb6fa5
  gint len;
Packit Service fb6fa5
Packit Service fb6fa5
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
Packit Service fb6fa5
Packit Service fb6fa5
  g_return_if_fail (g_unichar_validate (ch));
Packit Service fb6fa5
  
Packit Service fb6fa5
  len = g_unichar_to_utf8 (ch, buf);
Packit Service fb6fa5
  buf[len] = '\0';
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_simple->tentative_match || context_simple->in_hex_sequence)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_simple->in_hex_sequence = FALSE;  
Packit Service fb6fa5
      context_simple->tentative_match = 0;
Packit Service fb6fa5
      context_simple->tentative_match_len = 0;
Packit Service fb6fa5
      g_signal_emit_by_name (context_simple, "preedit-changed");
Packit Service fb6fa5
      g_signal_emit_by_name (context_simple, "preedit-end");
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  g_signal_emit_by_name (context, "commit", &buf;;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static int
Packit Service fb6fa5
compare_seq_index (const void *key, const void *value)
Packit Service fb6fa5
{
Packit Service fb6fa5
  const guint *keysyms = key;
Packit Service fb6fa5
  const guint16 *seq = value;
Packit Service fb6fa5
Packit Service fb6fa5
  if (keysyms[0] < seq[0])
Packit Service fb6fa5
    return -1;
Packit Service fb6fa5
  else if (keysyms[0] > seq[0])
Packit Service fb6fa5
    return 1;
Packit Service fb6fa5
Packit Service fb6fa5
  return 0;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static int
Packit Service fb6fa5
compare_seq (const void *key, const void *value)
Packit Service fb6fa5
{
Packit Service fb6fa5
  int i = 0;
Packit Service fb6fa5
  const guint *keysyms = key;
Packit Service fb6fa5
  const guint16 *seq = value;
Packit Service fb6fa5
Packit Service fb6fa5
  while (keysyms[i])
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (keysyms[i] < seq[i])
Packit Service fb6fa5
	return -1;
Packit Service fb6fa5
      else if (keysyms[i] > seq[i])
Packit Service fb6fa5
	return 1;
Packit Service fb6fa5
Packit Service fb6fa5
      i++;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return 0;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
check_table (GtkIMContextSimple    *context_simple,
Packit Service fb6fa5
	     const GtkComposeTable *table,
Packit Service fb6fa5
	     gint                   n_compose)
Packit Service fb6fa5
{
Packit Service fb6fa5
  gint row_stride = table->max_seq_len + 2; 
Packit Service fb6fa5
  guint16 *seq; 
Packit Service fb6fa5
  
Packit Service fb6fa5
  /* Will never match, if the sequence in the compose buffer is longer
Packit Service fb6fa5
   * than the sequences in the table.  Further, compare_seq (key, val)
Packit Service fb6fa5
   * will overrun val if key is longer than val. */
Packit Service fb6fa5
  if (n_compose > table->max_seq_len)
Packit Service fb6fa5
    return FALSE;
Packit Service fb6fa5
  
Packit Service fb6fa5
  seq = bsearch (context_simple->compose_buffer,
Packit Service fb6fa5
		 table->data, table->n_seqs,
Packit Service fb6fa5
		 sizeof (guint16) *  row_stride, 
Packit Service fb6fa5
		 compare_seq);
Packit Service fb6fa5
Packit Service fb6fa5
  if (seq)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      guint16 *prev_seq;
Packit Service fb6fa5
Packit Service fb6fa5
      /* Back up to the first sequence that matches to make sure
Packit Service fb6fa5
       * we find the exact match if their is one.
Packit Service fb6fa5
       */
Packit Service fb6fa5
      while (seq > table->data)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  prev_seq = seq - row_stride;
Packit Service fb6fa5
	  if (compare_seq (context_simple->compose_buffer, prev_seq) != 0)
Packit Service fb6fa5
	    break;
Packit Service fb6fa5
	  seq = prev_seq;
Packit Service fb6fa5
	}
Packit Service fb6fa5
      
Packit Service fb6fa5
      if (n_compose == table->max_seq_len ||
Packit Service fb6fa5
	  seq[n_compose] == 0) /* complete sequence */
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  guint16 *next_seq;
Packit Service fb6fa5
	  gunichar value = 
Packit Service fb6fa5
	    0x10000 * seq[table->max_seq_len] + seq[table->max_seq_len + 1];
Packit Service fb6fa5
Packit Service fb6fa5
	  
Packit Service fb6fa5
	  /* We found a tentative match. See if there are any longer
Packit Service fb6fa5
	   * sequences containing this subsequence
Packit Service fb6fa5
	   */
Packit Service fb6fa5
	  next_seq = seq + row_stride;
Packit Service fb6fa5
	  if (next_seq < table->data + row_stride * table->n_seqs)
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      if (compare_seq (context_simple->compose_buffer, next_seq) == 0)
Packit Service fb6fa5
		{
Packit Service fb6fa5
		  context_simple->tentative_match = value;
Packit Service fb6fa5
		  context_simple->tentative_match_len = n_compose;
Packit Service fb6fa5
		
Packit Service fb6fa5
		  g_signal_emit_by_name (context_simple, "preedit-changed");
Packit Service fb6fa5
Packit Service fb6fa5
		  return TRUE;
Packit Service fb6fa5
		}
Packit Service fb6fa5
	    }
Packit Service fb6fa5
Packit Service fb6fa5
	  gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
Packit Service fb6fa5
	  context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
	}
Packit Service fb6fa5
      
Packit Service fb6fa5
      return TRUE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Checks if a keysym is a dead key. Dead key keysym values are defined in
Packit Service fb6fa5
 * ../gdk/gdkkeysyms.h and the first is GDK_dead_grave. As X.Org is updated,
Packit Service fb6fa5
 * more dead keys are added and we need to update the upper limit.
Packit Service fb6fa5
 * Currently, the upper limit is GDK_dead_dasia+1. The +1 has to do with 
Packit Service fb6fa5
 * a temporary issue in the X.Org header files. 
Packit Service fb6fa5
 * In future versions it will be just the keysym (no +1).
Packit Service fb6fa5
 */
Packit Service fb6fa5
#define IS_DEAD_KEY(k) \
Packit Service fb6fa5
    ((k) >= GDK_dead_grave && (k) <= (GDK_dead_dasia+1))
Packit Service fb6fa5
Packit Service fb6fa5
#ifdef GDK_WINDOWING_WIN32
Packit Service fb6fa5
Packit Service fb6fa5
/* On Windows, user expectation is that typing a dead accent followed
Packit Service fb6fa5
 * by space will input the corresponding spacing character. The X
Packit Service fb6fa5
 * compose tables are different for dead acute and diaeresis, which
Packit Service fb6fa5
 * when followed by space produce a plain ASCII apostrophe and double
Packit Service fb6fa5
 * quote respectively. So special-case those.
Packit Service fb6fa5
 */
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
check_win32_special_cases (GtkIMContextSimple    *context_simple,
Packit Service fb6fa5
			   gint                   n_compose)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (n_compose == 2 &&
Packit Service fb6fa5
      context_simple->compose_buffer[1] == GDK_space)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gunichar value = 0;
Packit Service fb6fa5
Packit Service fb6fa5
      switch (context_simple->compose_buffer[0])
Packit Service fb6fa5
	{
Packit Service fb6fa5
	case GDK_dead_acute:
Packit Service fb6fa5
	  value = 0x00B4; break;
Packit Service fb6fa5
	case GDK_dead_diaeresis:
Packit Service fb6fa5
	  value = 0x00A8; break;
Packit Service fb6fa5
	}
Packit Service fb6fa5
      if (value > 0)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
Packit Service fb6fa5
	  context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
Packit Service fb6fa5
	  GTK_NOTE (MISC, g_print ("win32: U+%04X\n", value));
Packit Service fb6fa5
	  return TRUE;
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
  return FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
check_win32_special_case_after_compact_match (GtkIMContextSimple    *context_simple,
Packit Service fb6fa5
					      gint                   n_compose,
Packit Service fb6fa5
					      guint                  value)
Packit Service fb6fa5
{
Packit Service fb6fa5
  /* On Windows user expectation is that typing two dead accents will input
Packit Service fb6fa5
   * two corresponding spacing accents.
Packit Service fb6fa5
   */
Packit Service fb6fa5
  if (n_compose == 2 &&
Packit Service fb6fa5
      context_simple->compose_buffer[0] == context_simple->compose_buffer[1] &&
Packit Service fb6fa5
      IS_DEAD_KEY (context_simple->compose_buffer[0]))
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
Packit Service fb6fa5
      GTK_NOTE (MISC, g_print ("win32: U+%04X ", value));
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
#endif
Packit Service fb6fa5
Packit Service fb6fa5
#ifdef GDK_WINDOWING_QUARTZ
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
check_quartz_special_cases (GtkIMContextSimple *context_simple,
Packit Service fb6fa5
                            gint                n_compose)
Packit Service fb6fa5
{
Packit Service fb6fa5
  guint value = 0;
Packit Service fb6fa5
Packit Service fb6fa5
  if (n_compose == 2)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      switch (context_simple->compose_buffer[0])
Packit Service fb6fa5
        {
Packit Service fb6fa5
        case GDK_KEY_dead_doubleacute:
Packit Service fb6fa5
          switch (context_simple->compose_buffer[1])
Packit Service fb6fa5
            {
Packit Service fb6fa5
            case GDK_KEY_dead_doubleacute:
Packit Service fb6fa5
            case GDK_KEY_space:
Packit Service fb6fa5
              value = GDK_KEY_quotedbl; break;
Packit Service fb6fa5
Packit Service fb6fa5
            case 'a': value = GDK_KEY_adiaeresis; break;
Packit Service fb6fa5
            case 'A': value = GDK_KEY_Adiaeresis; break;
Packit Service fb6fa5
            case 'e': value = GDK_KEY_ediaeresis; break;
Packit Service fb6fa5
            case 'E': value = GDK_KEY_Ediaeresis; break;
Packit Service fb6fa5
            case 'i': value = GDK_KEY_idiaeresis; break;
Packit Service fb6fa5
            case 'I': value = GDK_KEY_Idiaeresis; break;
Packit Service fb6fa5
            case 'o': value = GDK_KEY_odiaeresis; break;
Packit Service fb6fa5
            case 'O': value = GDK_KEY_Odiaeresis; break;
Packit Service fb6fa5
            case 'u': value = GDK_KEY_udiaeresis; break;
Packit Service fb6fa5
            case 'U': value = GDK_KEY_Udiaeresis; break;
Packit Service fb6fa5
            case 'y': value = GDK_KEY_ydiaeresis; break;
Packit Service fb6fa5
            case 'Y': value = GDK_KEY_Ydiaeresis; break;
Packit Service fb6fa5
            }
Packit Service fb6fa5
          break;
Packit Service fb6fa5
Packit Service fb6fa5
        case GDK_KEY_dead_acute:
Packit Service fb6fa5
          switch (context_simple->compose_buffer[1])
Packit Service fb6fa5
            {
Packit Service fb6fa5
            case 'c': value = GDK_KEY_ccedilla; break;
Packit Service fb6fa5
            case 'C': value = GDK_KEY_Ccedilla; break;
Packit Service fb6fa5
            }
Packit Service fb6fa5
          break;
Packit Service fb6fa5
        }
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (value > 0)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple),
Packit Service fb6fa5
                                         gdk_keyval_to_unicode (value));
Packit Service fb6fa5
      context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
Packit Service fb6fa5
      GTK_NOTE (MISC, g_print ("quartz: U+%04X\n", value));
Packit Service fb6fa5
      return TRUE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
#endif
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
check_compact_table (GtkIMContextSimple    *context_simple,
Packit Service fb6fa5
	     const GtkComposeTableCompact *table,
Packit Service fb6fa5
	     gint                   n_compose)
Packit Service fb6fa5
{
Packit Service fb6fa5
  gint row_stride;
Packit Service fb6fa5
  guint16 *seq_index;
Packit Service fb6fa5
  guint16 *seq; 
Packit Service fb6fa5
  gint i;
Packit Service fb6fa5
Packit Service fb6fa5
  /* Will never match, if the sequence in the compose buffer is longer
Packit Service fb6fa5
   * than the sequences in the table.  Further, compare_seq (key, val)
Packit Service fb6fa5
   * will overrun val if key is longer than val. */
Packit Service fb6fa5
  if (n_compose > table->max_seq_len)
Packit Service fb6fa5
    return FALSE;
Packit Service fb6fa5
  
Packit Service fb6fa5
  seq_index = bsearch (context_simple->compose_buffer,
Packit Service fb6fa5
		 table->data, table->n_index_size,
Packit Service fb6fa5
		 sizeof (guint16) *  table->n_index_stride, 
Packit Service fb6fa5
		 compare_seq_index);
Packit Service fb6fa5
Packit Service fb6fa5
  if (!seq_index)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GTK_NOTE (MISC, g_print ("compact: no\n"));
Packit Service fb6fa5
      return FALSE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (seq_index && n_compose == 1)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GTK_NOTE (MISC, g_print ("compact: yes\n"));
Packit Service fb6fa5
      return TRUE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  GTK_NOTE (MISC, g_print ("compact: %d ", *seq_index));
Packit Service fb6fa5
  seq = NULL;
Packit Service fb6fa5
Packit Service fb6fa5
  for (i = n_compose-1; i < table->max_seq_len; i++)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      row_stride = i + 1;
Packit Service fb6fa5
Packit Service fb6fa5
      if (seq_index[i+1] - seq_index[i] > 0)
Packit Service fb6fa5
        {
Packit Service fb6fa5
	  seq = bsearch (context_simple->compose_buffer + 1,
Packit Service fb6fa5
		 table->data + seq_index[i], (seq_index[i+1] - seq_index[i]) / row_stride,
Packit Service fb6fa5
		 sizeof (guint16) *  row_stride, 
Packit Service fb6fa5
		 compare_seq);
Packit Service fb6fa5
Packit Service fb6fa5
	  if (seq)
Packit Service fb6fa5
            {
Packit Service fb6fa5
              if (i == n_compose - 1)
Packit Service fb6fa5
                break;
Packit Service fb6fa5
              else
Packit Service fb6fa5
                {
Packit Service fb6fa5
                  g_signal_emit_by_name (context_simple, "preedit-changed");
Packit Service fb6fa5
Packit Service fb6fa5
		  GTK_NOTE (MISC, g_print ("yes\n"));
Packit Service fb6fa5
      		  return TRUE;
Packit Service fb6fa5
                }
Packit Service fb6fa5
             }
Packit Service fb6fa5
        }
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (!seq)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GTK_NOTE (MISC, g_print ("no\n"));
Packit Service fb6fa5
      return FALSE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gunichar value;
Packit Service fb6fa5
Packit Service fb6fa5
      value = seq[row_stride - 1];
Packit Service fb6fa5
Packit Service fb6fa5
      gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
Packit Service fb6fa5
#ifdef G_OS_WIN32
Packit Service fb6fa5
      check_win32_special_case_after_compact_match (context_simple, n_compose, value);
Packit Service fb6fa5
#endif
Packit Service fb6fa5
      context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
Packit Service fb6fa5
      GTK_NOTE (MISC, g_print ("U+%04X\n", value));
Packit Service fb6fa5
      return TRUE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  GTK_NOTE (MISC, g_print ("no\n"));
Packit Service fb6fa5
  return FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* This function receives a sequence of Unicode characters and tries to
Packit Service fb6fa5
 * normalize it (NFC). We check for the case the the resulting string
Packit Service fb6fa5
 * has length 1 (single character).
Packit Service fb6fa5
 * NFC normalisation normally rearranges diacritic marks, unless these
Packit Service fb6fa5
 * belong to the same Canonical Combining Class.
Packit Service fb6fa5
 * If they belong to the same canonical combining class, we produce all
Packit Service fb6fa5
 * permutations of the diacritic marks, then attempt to normalize.
Packit Service fb6fa5
 */
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
check_normalize_nfc (gunichar* combination_buffer, gint n_compose)
Packit Service fb6fa5
{
Packit Service fb6fa5
  gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
Packit Service fb6fa5
  gchar *combination_utf8_temp = NULL;
Packit Service fb6fa5
  gchar *nfc_temp = NULL;
Packit Service fb6fa5
  gint n_combinations;
Packit Service fb6fa5
  gunichar temp_swap;
Packit Service fb6fa5
  gint i;
Packit Service fb6fa5
Packit Service fb6fa5
  n_combinations = 1;
Packit Service fb6fa5
Packit Service fb6fa5
  for (i = 1; i < n_compose; i++ )
Packit Service fb6fa5
     n_combinations *= i;
Packit Service fb6fa5
Packit Service fb6fa5
  /* Xorg reuses dead_tilde for the perispomeni diacritic mark.
Packit Service fb6fa5
   * We check if base character belongs to Greek Unicode block,
Packit Service fb6fa5
   * and if so, we replace tilde with perispomeni. */
Packit Service fb6fa5
  if (combination_buffer[0] >= 0x390 && combination_buffer[0] <= 0x3FF)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      for (i = 1; i < n_compose; i++ )
Packit Service fb6fa5
        if (combination_buffer[i] == 0x303)
Packit Service fb6fa5
          combination_buffer[i] = 0x342;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
Packit Service fb6fa5
Packit Service fb6fa5
  for (i = 0; i < n_combinations; i++ )
Packit Service fb6fa5
    {
Packit Service fb6fa5
      g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
Packit Service fb6fa5
      combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
Packit Service fb6fa5
      nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);	       	
Packit Service fb6fa5
Packit Service fb6fa5
      if (g_utf8_strlen (nfc_temp, -1) == 1)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
Packit Service fb6fa5
Packit Service fb6fa5
          g_free (combination_utf8_temp);
Packit Service fb6fa5
          g_free (nfc_temp);
Packit Service fb6fa5
Packit Service fb6fa5
          return TRUE;
Packit Service fb6fa5
        }
Packit Service fb6fa5
Packit Service fb6fa5
      g_free (combination_utf8_temp);
Packit Service fb6fa5
      g_free (nfc_temp);
Packit Service fb6fa5
Packit Service fb6fa5
      if (n_compose > 2)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          temp_swap = combination_buffer_temp[i % (n_compose - 1) + 1];
Packit Service fb6fa5
          combination_buffer_temp[i % (n_compose - 1) + 1] = combination_buffer_temp[(i+1) % (n_compose - 1) + 1];
Packit Service fb6fa5
          combination_buffer_temp[(i+1) % (n_compose - 1) + 1] = temp_swap;
Packit Service fb6fa5
        }
Packit Service fb6fa5
      else
Packit Service fb6fa5
        break;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
check_algorithmically (GtkIMContextSimple    *context_simple,
Packit Service fb6fa5
		       gint                   n_compose)
Packit Service fb6fa5
Packit Service fb6fa5
{
Packit Service fb6fa5
  gint i;
Packit Service fb6fa5
  gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
Packit Service fb6fa5
  gchar *combination_utf8, *nfc;
Packit Service fb6fa5
Packit Service fb6fa5
  if (n_compose >= GTK_MAX_COMPOSE_LEN)
Packit Service fb6fa5
    return FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
  for (i = 0; i < n_compose && IS_DEAD_KEY (context_simple->compose_buffer[i]); i++)
Packit Service fb6fa5
    ;
Packit Service fb6fa5
  if (i == n_compose)
Packit Service fb6fa5
    return TRUE;
Packit Service fb6fa5
Packit Service fb6fa5
  if (i > 0 && i == n_compose - 1)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      combination_buffer[0] = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
Packit Service fb6fa5
      combination_buffer[n_compose] = 0;
Packit Service fb6fa5
      i--;
Packit Service fb6fa5
      while (i >= 0)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  switch (context_simple->compose_buffer[i])
Packit Service fb6fa5
	    {
Packit Service fb6fa5
#define CASE(keysym, unicode) \
Packit Service fb6fa5
	    case GDK_dead_##keysym: combination_buffer[i+1] = unicode; break
Packit Service fb6fa5
Packit Service fb6fa5
	    CASE (grave, 0x0300);
Packit Service fb6fa5
	    CASE (acute, 0x0301);
Packit Service fb6fa5
	    CASE (circumflex, 0x0302);
Packit Service fb6fa5
	    CASE (tilde, 0x0303);	/* Also used with perispomeni, 0x342. */
Packit Service fb6fa5
	    CASE (macron, 0x0304);
Packit Service fb6fa5
	    CASE (breve, 0x0306);
Packit Service fb6fa5
	    CASE (abovedot, 0x0307);
Packit Service fb6fa5
	    CASE (diaeresis, 0x0308);
Packit Service fb6fa5
	    CASE (hook, 0x0309);
Packit Service fb6fa5
	    CASE (abovering, 0x030A);
Packit Service fb6fa5
	    CASE (doubleacute, 0x030B);
Packit Service fb6fa5
	    CASE (caron, 0x030C);
Packit Service fb6fa5
	    CASE (abovecomma, 0x0313);         /* Equivalent to psili */
Packit Service fb6fa5
	    CASE (abovereversedcomma, 0x0314); /* Equivalent to dasia */
Packit Service fb6fa5
	    CASE (horn, 0x031B);	/* Legacy use for psili, 0x313 (or 0x343). */
Packit Service fb6fa5
	    CASE (belowdot, 0x0323);
Packit Service fb6fa5
	    CASE (cedilla, 0x0327);
Packit Service fb6fa5
	    CASE (ogonek, 0x0328);	/* Legacy use for dasia, 0x314.*/
Packit Service fb6fa5
	    CASE (iota, 0x0345);
Packit Service fb6fa5
	    CASE (voiced_sound, 0x3099);	/* Per Markus Kuhn keysyms.txt file. */
Packit Service fb6fa5
	    CASE (semivoiced_sound, 0x309A);	/* Per Markus Kuhn keysyms.txt file. */
Packit Service fb6fa5
Packit Service fb6fa5
	    /* The following cases are to be removed once xkeyboard-config,
Packit Service fb6fa5
 	     * xorg are fully updated.
Packit Service fb6fa5
 	     */
Packit Service fb6fa5
            /* Workaround for typo in 1.4.x xserver-xorg */
Packit Service fb6fa5
	    case 0xfe66: combination_buffer[i+1] = 0x314; break;
Packit Service fb6fa5
	    /* CASE (dasia, 0x314); */
Packit Service fb6fa5
	    /* CASE (perispomeni, 0x342); */
Packit Service fb6fa5
	    /* CASE (psili, 0x343); */
Packit Service fb6fa5
#undef CASE
Packit Service fb6fa5
	    default:
Packit Service fb6fa5
	      combination_buffer[i+1] = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	  i--;
Packit Service fb6fa5
	}
Packit Service fb6fa5
      
Packit Service fb6fa5
      /* If the buffer normalizes to a single character, 
Packit Service fb6fa5
       * then modify the order of combination_buffer accordingly, if necessary,
Packit Service fb6fa5
       * and return TRUE. 
Packit Service fb6fa5
       */
Packit Service fb6fa5
      if (check_normalize_nfc (combination_buffer, n_compose))
Packit Service fb6fa5
        {
Packit Service fb6fa5
          gunichar value;
Packit Service fb6fa5
      	  combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
Packit Service fb6fa5
          nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
Packit Service fb6fa5
Packit Service fb6fa5
          value = g_utf8_get_char (nfc);
Packit Service fb6fa5
          gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple), value);
Packit Service fb6fa5
          context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
Packit Service fb6fa5
          g_free (combination_utf8);
Packit Service fb6fa5
          g_free (nfc);
Packit Service fb6fa5
Packit Service fb6fa5
          return TRUE;
Packit Service fb6fa5
        }
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* In addition to the table-driven sequences, we allow Unicode hex
Packit Service fb6fa5
 * codes to be entered. The method chosen here is similar to the
Packit Service fb6fa5
 * one recommended in ISO 14755, but not exactly the same, since we
Packit Service fb6fa5
 * don't want to steal 16 valuable key combinations. 
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * A hex Unicode sequence must be started with Ctrl-Shift-U, followed
Packit Service fb6fa5
 * by a sequence of hex digits entered with Ctrl-Shift still held.
Packit Service fb6fa5
 * Releasing one of the modifiers or pressing space while the modifiers
Packit Service fb6fa5
 * are still held commits the character. It is possible to erase
Packit Service fb6fa5
 * digits using backspace.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * As an extension to the above, we also allow to start the sequence
Packit Service fb6fa5
 * with Ctrl-Shift-U, then release the modifiers before typing any
Packit Service fb6fa5
 * digits, and enter the digits without modifiers.
Packit Service fb6fa5
 */
Packit Service fb6fa5
#define HEX_MOD_MASK (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
check_hex (GtkIMContextSimple *context_simple,
Packit Service fb6fa5
           gint                n_compose)
Packit Service fb6fa5
{
Packit Service fb6fa5
  /* See if this is a hex sequence, return TRUE if so */
Packit Service fb6fa5
  gint i;
Packit Service fb6fa5
  GString *str;
Packit Service fb6fa5
  gulong n;
Packit Service fb6fa5
  gchar *nptr = NULL;
Packit Service fb6fa5
  gchar buf[7];
Packit Service fb6fa5
Packit Service fb6fa5
  context_simple->tentative_match = 0;
Packit Service fb6fa5
  context_simple->tentative_match_len = 0;
Packit Service fb6fa5
Packit Service fb6fa5
  str = g_string_new (NULL);
Packit Service fb6fa5
  
Packit Service fb6fa5
  i = 0;
Packit Service fb6fa5
  while (i < n_compose)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gunichar ch;
Packit Service fb6fa5
      
Packit Service fb6fa5
      ch = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
Packit Service fb6fa5
      
Packit Service fb6fa5
      if (ch == 0)
Packit Service fb6fa5
        return FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
      if (!g_unichar_isxdigit (ch))
Packit Service fb6fa5
        return FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
      buf[g_unichar_to_utf8 (ch, buf)] = '\0';
Packit Service fb6fa5
Packit Service fb6fa5
      g_string_append (str, buf);
Packit Service fb6fa5
      
Packit Service fb6fa5
      ++i;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  n = strtoul (str->str, &nptr, 16);
Packit Service fb6fa5
Packit Service fb6fa5
  /* if strtoul fails it probably means non-latin digits were used;
Packit Service fb6fa5
   * we should in principle handle that, but we probably don't.
Packit Service fb6fa5
   */
Packit Service fb6fa5
  if (nptr - str->str < str->len)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      g_string_free (str, TRUE);
Packit Service fb6fa5
      return FALSE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    g_string_free (str, TRUE);
Packit Service fb6fa5
Packit Service fb6fa5
  if (g_unichar_validate (n))
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_simple->tentative_match = n;
Packit Service fb6fa5
      context_simple->tentative_match_len = n_compose;
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  return TRUE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
beep_window (GdkWindow *window)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkWidget *widget;
Packit Service fb6fa5
Packit Service fb6fa5
  gdk_window_get_user_data (window, (gpointer) &widget);
Packit Service fb6fa5
Packit Service fb6fa5
  if (GTK_IS_WIDGET (widget))
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gtk_widget_error_bell (widget);
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GdkScreen *screen = gdk_window_get_screen (window);
Packit Service fb6fa5
      gboolean   beep;
Packit Service fb6fa5
Packit Service fb6fa5
      g_object_get (gtk_settings_get_for_screen (screen),
Packit Service fb6fa5
                    "gtk-error-bell", &beep,
Packit Service fb6fa5
                    NULL);
Packit Service fb6fa5
Packit Service fb6fa5
      if (beep)
Packit Service fb6fa5
        gdk_window_beep (window);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
no_sequence_matches (GtkIMContextSimple *context_simple,
Packit Service fb6fa5
                     gint                n_compose,
Packit Service fb6fa5
                     GdkEventKey        *event)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContext *context;
Packit Service fb6fa5
  gunichar ch;
Packit Service fb6fa5
  
Packit Service fb6fa5
  context = GTK_IM_CONTEXT (context_simple);
Packit Service fb6fa5
  
Packit Service fb6fa5
  /* No compose sequences found, check first if we have a partial
Packit Service fb6fa5
   * match pending.
Packit Service fb6fa5
   */
Packit Service fb6fa5
  if (context_simple->tentative_match)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      gint len = context_simple->tentative_match_len;
Packit Service fb6fa5
      int i;
Packit Service fb6fa5
      
Packit Service fb6fa5
      gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
Packit Service fb6fa5
      context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
      
Packit Service fb6fa5
      for (i=0; i < n_compose - len - 1; i++)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  GdkEvent *tmp_event = gdk_event_copy ((GdkEvent *)event);
Packit Service fb6fa5
	  tmp_event->key.keyval = context_simple->compose_buffer[len + i];
Packit Service fb6fa5
	  
Packit Service fb6fa5
	  gtk_im_context_filter_keypress (context, (GdkEventKey *)tmp_event);
Packit Service fb6fa5
	  gdk_event_free (tmp_event);
Packit Service fb6fa5
	}
Packit Service fb6fa5
Packit Service fb6fa5
      return gtk_im_context_filter_keypress (context, event);
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
      if (n_compose > 1)		/* Invalid sequence */
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  beep_window (event->window);
Packit Service fb6fa5
	  return TRUE;
Packit Service fb6fa5
	}
Packit Service fb6fa5
  
Packit Service fb6fa5
      ch = gdk_keyval_to_unicode (event->keyval);
Packit Service fb6fa5
      if (ch != 0)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  gtk_im_context_simple_commit_char (context, ch);
Packit Service fb6fa5
	  return TRUE;
Packit Service fb6fa5
	}
Packit Service fb6fa5
      else
Packit Service fb6fa5
	return FALSE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
is_hex_keyval (guint keyval)
Packit Service fb6fa5
{
Packit Service fb6fa5
  gunichar ch = gdk_keyval_to_unicode (keyval);
Packit Service fb6fa5
Packit Service fb6fa5
  return g_unichar_isxdigit (ch);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static guint
Packit Service fb6fa5
canonical_hex_keyval (GdkEventKey *event)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_window_get_display (event->window));
Packit Service fb6fa5
  guint keyval;
Packit Service fb6fa5
  guint *keyvals = NULL;
Packit Service fb6fa5
  gint n_vals = 0;
Packit Service fb6fa5
  gint i;
Packit Service fb6fa5
  
Packit Service fb6fa5
  /* See if the keyval is already a hex digit */
Packit Service fb6fa5
  if (is_hex_keyval (event->keyval))
Packit Service fb6fa5
    return event->keyval;
Packit Service fb6fa5
Packit Service fb6fa5
  /* See if this key would have generated a hex keyval in
Packit Service fb6fa5
   * any other state, and return that hex keyval if so
Packit Service fb6fa5
   */
Packit Service fb6fa5
  gdk_keymap_get_entries_for_keycode (keymap,
Packit Service fb6fa5
				      event->hardware_keycode,
Packit Service fb6fa5
				      NULL,
Packit Service fb6fa5
				      &keyvals, &n_vals);
Packit Service fb6fa5
Packit Service fb6fa5
  keyval = 0;
Packit Service fb6fa5
  i = 0;
Packit Service fb6fa5
  while (i < n_vals)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (is_hex_keyval (keyvals[i]))
Packit Service fb6fa5
        {
Packit Service fb6fa5
          keyval = keyvals[i];
Packit Service fb6fa5
          break;
Packit Service fb6fa5
        }
Packit Service fb6fa5
Packit Service fb6fa5
      ++i;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  g_free (keyvals);
Packit Service fb6fa5
  
Packit Service fb6fa5
  if (keyval)
Packit Service fb6fa5
    return keyval;
Packit Service fb6fa5
  else
Packit Service fb6fa5
    /* No way to make it a hex digit
Packit Service fb6fa5
     */
Packit Service fb6fa5
    return 0;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static gboolean
Packit Service fb6fa5
gtk_im_context_simple_filter_keypress (GtkIMContext *context,
Packit Service fb6fa5
				       GdkEventKey  *event)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
Packit Service fb6fa5
  GSList *tmp_list;  
Packit Service fb6fa5
  int n_compose = 0;
Packit Service fb6fa5
  gboolean have_hex_mods;
Packit Service fb6fa5
  gboolean is_hex_start;
Packit Service fb6fa5
  gboolean is_hex_end;
Packit Service fb6fa5
  gboolean is_backspace;
Packit Service fb6fa5
  gboolean is_escape;
Packit Service fb6fa5
  guint hex_keyval;
Packit Service fb6fa5
  int i;
Packit Service fb6fa5
Packit Service fb6fa5
  while (context_simple->compose_buffer[n_compose] != 0)
Packit Service fb6fa5
    n_compose++;
Packit Service fb6fa5
Packit Service fb6fa5
  if (event->type == GDK_KEY_RELEASE)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (context_simple->in_hex_sequence &&
Packit Service fb6fa5
	  (event->keyval == GDK_Control_L || event->keyval == GDK_Control_R ||
Packit Service fb6fa5
	   event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R))
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  if (context_simple->tentative_match &&
Packit Service fb6fa5
	      g_unichar_validate (context_simple->tentative_match))
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
Packit Service fb6fa5
	      context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	  else if (n_compose == 0)
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      context_simple->modifiers_dropped = TRUE;
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	  else
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      /* invalid hex sequence */
Packit Service fb6fa5
	      beep_window (event->window);
Packit Service fb6fa5
	      
Packit Service fb6fa5
	      context_simple->tentative_match = 0;
Packit Service fb6fa5
	      context_simple->in_hex_sequence = FALSE;
Packit Service fb6fa5
	      context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
	      
Packit Service fb6fa5
	      g_signal_emit_by_name (context_simple, "preedit-changed");
Packit Service fb6fa5
	      g_signal_emit_by_name (context_simple, "preedit-end");
Packit Service fb6fa5
	    }
Packit Service fb6fa5
Packit Service fb6fa5
	  return TRUE;
Packit Service fb6fa5
	}
Packit Service fb6fa5
      else
Packit Service fb6fa5
	return FALSE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  /* Ignore modifier key presses */
Packit Service fb6fa5
  for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
Packit Service fb6fa5
    if (event->keyval == gtk_compose_ignore[i])
Packit Service fb6fa5
      return FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_simple->in_hex_sequence && context_simple->modifiers_dropped)
Packit Service fb6fa5
    have_hex_mods = TRUE;
Packit Service fb6fa5
  else
Packit Service fb6fa5
    have_hex_mods = (event->state & (HEX_MOD_MASK)) == HEX_MOD_MASK;
Packit Service fb6fa5
  is_hex_start = event->keyval == GDK_U;
Packit Service fb6fa5
  is_hex_end = (event->keyval == GDK_space || 
Packit Service fb6fa5
		event->keyval == GDK_KP_Space ||
Packit Service fb6fa5
		event->keyval == GDK_Return || 
Packit Service fb6fa5
		event->keyval == GDK_ISO_Enter ||
Packit Service fb6fa5
		event->keyval == GDK_KP_Enter);
Packit Service fb6fa5
  is_backspace = event->keyval == GDK_BackSpace;
Packit Service fb6fa5
  is_escape = event->keyval == GDK_Escape;
Packit Service fb6fa5
  hex_keyval = canonical_hex_keyval (event);
Packit Service fb6fa5
Packit Service fb6fa5
  /* If we are already in a non-hex sequence, or
Packit Service fb6fa5
   * this keystroke is not hex modifiers + hex digit, don't filter
Packit Service fb6fa5
   * key events with accelerator modifiers held down. We only treat
Packit Service fb6fa5
   * Control and Alt as accel modifiers here, since Super, Hyper and
Packit Service fb6fa5
   * Meta are often co-located with Mode_Switch, Multi_Key or
Packit Service fb6fa5
   * ISO_Level3_Switch.
Packit Service fb6fa5
   */
Packit Service fb6fa5
  if (!have_hex_mods ||
Packit Service fb6fa5
      (n_compose > 0 && !context_simple->in_hex_sequence) || 
Packit Service fb6fa5
      (n_compose == 0 && !context_simple->in_hex_sequence && !is_hex_start) ||
Packit Service fb6fa5
      (context_simple->in_hex_sequence && !hex_keyval && 
Packit Service fb6fa5
       !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (event->state & GTK_NO_TEXT_INPUT_MOD_MASK ||
Packit Service fb6fa5
	  (context_simple->in_hex_sequence && context_simple->modifiers_dropped &&
Packit Service fb6fa5
	   (event->keyval == GDK_Return || 
Packit Service fb6fa5
	    event->keyval == GDK_ISO_Enter ||
Packit Service fb6fa5
	    event->keyval == GDK_KP_Enter)))
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  return FALSE;
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  /* Handle backspace */
Packit Service fb6fa5
  if (context_simple->in_hex_sequence && have_hex_mods && is_backspace)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (n_compose > 0)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  n_compose--;
Packit Service fb6fa5
	  context_simple->compose_buffer[n_compose] = 0;
Packit Service fb6fa5
          check_hex (context_simple, n_compose);
Packit Service fb6fa5
	}
Packit Service fb6fa5
      else
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  context_simple->in_hex_sequence = FALSE;
Packit Service fb6fa5
	}
Packit Service fb6fa5
Packit Service fb6fa5
      g_signal_emit_by_name (context_simple, "preedit-changed");
Packit Service fb6fa5
Packit Service fb6fa5
      if (!context_simple->in_hex_sequence)
Packit Service fb6fa5
        g_signal_emit_by_name (context_simple, "preedit-end");
Packit Service fb6fa5
      
Packit Service fb6fa5
      return TRUE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  /* Check for hex sequence restart */
Packit Service fb6fa5
  if (context_simple->in_hex_sequence && have_hex_mods && is_hex_start)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (context_simple->tentative_match &&
Packit Service fb6fa5
	  g_unichar_validate (context_simple->tentative_match))
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
Packit Service fb6fa5
	  context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
	}
Packit Service fb6fa5
      else 
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  /* invalid hex sequence */
Packit Service fb6fa5
	  if (n_compose > 0)
Packit Service fb6fa5
	    beep_window (event->window);
Packit Service fb6fa5
	  
Packit Service fb6fa5
	  context_simple->tentative_match = 0;
Packit Service fb6fa5
	  context_simple->in_hex_sequence = FALSE;
Packit Service fb6fa5
	  context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  /* Check for hex sequence start */
Packit Service fb6fa5
  if (!context_simple->in_hex_sequence && have_hex_mods && is_hex_start)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
      context_simple->in_hex_sequence = TRUE;
Packit Service fb6fa5
      context_simple->modifiers_dropped = FALSE;
Packit Service fb6fa5
      context_simple->tentative_match = 0;
Packit Service fb6fa5
Packit Service fb6fa5
      g_signal_emit_by_name (context_simple, "preedit-start");
Packit Service fb6fa5
      g_signal_emit_by_name (context_simple, "preedit-changed");
Packit Service fb6fa5
  
Packit Service fb6fa5
      return TRUE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  /* Then, check for compose sequences */
Packit Service fb6fa5
  if (context_simple->in_hex_sequence)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (hex_keyval)
Packit Service fb6fa5
	context_simple->compose_buffer[n_compose++] = hex_keyval;
Packit Service fb6fa5
      else if (is_escape)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  gtk_im_context_simple_reset (context);
Packit Service fb6fa5
	  
Packit Service fb6fa5
	  return TRUE;
Packit Service fb6fa5
	}
Packit Service fb6fa5
      else if (!is_hex_end)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  /* non-hex character in hex sequence */
Packit Service fb6fa5
	  beep_window (event->window);
Packit Service fb6fa5
	  
Packit Service fb6fa5
	  return TRUE;
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    context_simple->compose_buffer[n_compose++] = event->keyval;
Packit Service fb6fa5
Packit Service fb6fa5
  context_simple->compose_buffer[n_compose] = 0;
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_simple->in_hex_sequence)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      /* If the modifiers are still held down, consider the sequence again */
Packit Service fb6fa5
      if (have_hex_mods)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          /* space or return ends the sequence, and we eat the key */
Packit Service fb6fa5
          if (n_compose > 0 && is_hex_end)
Packit Service fb6fa5
            {
Packit Service fb6fa5
	      if (context_simple->tentative_match &&
Packit Service fb6fa5
		  g_unichar_validate (context_simple->tentative_match))
Packit Service fb6fa5
		{
Packit Service fb6fa5
		  gtk_im_context_simple_commit_char (context, context_simple->tentative_match);
Packit Service fb6fa5
		  context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
		}
Packit Service fb6fa5
	      else
Packit Service fb6fa5
		{
Packit Service fb6fa5
		  /* invalid hex sequence */
Packit Service fb6fa5
		  beep_window (event->window);
Packit Service fb6fa5
Packit Service fb6fa5
		  context_simple->tentative_match = 0;
Packit Service fb6fa5
		  context_simple->in_hex_sequence = FALSE;
Packit Service fb6fa5
		  context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
		}
Packit Service fb6fa5
            }
Packit Service fb6fa5
          else if (!check_hex (context_simple, n_compose))
Packit Service fb6fa5
	    beep_window (event->window);
Packit Service fb6fa5
	  
Packit Service fb6fa5
	  g_signal_emit_by_name (context_simple, "preedit-changed");
Packit Service fb6fa5
Packit Service fb6fa5
	  if (!context_simple->in_hex_sequence)
Packit Service fb6fa5
	    g_signal_emit_by_name (context_simple, "preedit-end");
Packit Service fb6fa5
Packit Service fb6fa5
	  return TRUE;
Packit Service fb6fa5
        }
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
#ifdef GDK_WINDOWING_WIN32
Packit Service fb6fa5
      guint16  output[2];
Packit Service fb6fa5
      gsize    output_size = 2;
Packit Service fb6fa5
Packit Service fb6fa5
      switch (gdk_win32_keymap_check_compose (GDK_WIN32_KEYMAP (gdk_keymap_get_default ()),
Packit Service fb6fa5
                                              context_simple->compose_buffer,
Packit Service fb6fa5
                                              n_compose,
Packit Service fb6fa5
                                              output, &output_size))
Packit Service fb6fa5
        {
Packit Service fb6fa5
        case GDK_WIN32_KEYMAP_MATCH_NONE:
Packit Service fb6fa5
          break;
Packit Service fb6fa5
        case GDK_WIN32_KEYMAP_MATCH_EXACT:
Packit Service fb6fa5
        case GDK_WIN32_KEYMAP_MATCH_PARTIAL:
Packit Service fb6fa5
          for (i = 0; i < output_size; i++)
Packit Service fb6fa5
            {
Packit Service fb6fa5
              guint32 output_char = gdk_keyval_to_unicode (output[i]);
Packit Service fb6fa5
              gtk_im_context_simple_commit_char (GTK_IM_CONTEXT (context_simple),
Packit Service fb6fa5
                                                 output_char);
Packit Service fb6fa5
            }
Packit Service fb6fa5
          context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
          return TRUE;
Packit Service fb6fa5
        case GDK_WIN32_KEYMAP_MATCH_INCOMPLETE:
Packit Service fb6fa5
          return TRUE;
Packit Service fb6fa5
        }
Packit Service fb6fa5
#endif
Packit Service fb6fa5
Packit Service fb6fa5
Packit Service fb6fa5
      tmp_list = context_simple->tables;
Packit Service fb6fa5
      while (tmp_list)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          if (check_table (context_simple, tmp_list->data, n_compose))
Packit Service fb6fa5
            return TRUE;
Packit Service fb6fa5
          tmp_list = tmp_list->next;
Packit Service fb6fa5
        }
Packit Service fb6fa5
Packit Service fb6fa5
      GTK_NOTE (MISC, {
Packit Service fb6fa5
	  g_print ("[ ");
Packit Service fb6fa5
	  for (i = 0; i < n_compose; i++)
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      const gchar *keyval_name = gdk_keyval_name (context_simple->compose_buffer[i]);
Packit Service fb6fa5
	      
Packit Service fb6fa5
	      if (keyval_name != NULL)
Packit Service fb6fa5
		g_print ("%s ", keyval_name);
Packit Service fb6fa5
	      else
Packit Service fb6fa5
		g_print ("%04x ", context_simple->compose_buffer[i]);
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	  g_print ("] ");
Packit Service fb6fa5
	});
Packit Service fb6fa5
Packit Service fb6fa5
#ifdef GDK_WINDOWING_WIN32
Packit Service fb6fa5
      if (check_win32_special_cases (context_simple, n_compose))
Packit Service fb6fa5
	return TRUE;
Packit Service fb6fa5
#endif
Packit Service fb6fa5
Packit Service fb6fa5
#ifdef GDK_WINDOWING_QUARTZ
Packit Service fb6fa5
      if (check_quartz_special_cases (context_simple, n_compose))
Packit Service fb6fa5
        return TRUE;
Packit Service fb6fa5
#endif
Packit Service fb6fa5
Packit Service fb6fa5
      if (check_compact_table (context_simple, &gtk_compose_table_compact, n_compose))
Packit Service fb6fa5
        return TRUE;
Packit Service fb6fa5
  
Packit Service fb6fa5
      if (check_algorithmically (context_simple, n_compose))
Packit Service fb6fa5
	return TRUE;
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  /* The current compose_buffer doesn't match anything */
Packit Service fb6fa5
  return no_sequence_matches (context_simple, n_compose, event);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
gtk_im_context_simple_reset (GtkIMContext *context)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
Packit Service fb6fa5
Packit Service fb6fa5
  context_simple->compose_buffer[0] = 0;
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_simple->tentative_match || context_simple->in_hex_sequence)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      context_simple->in_hex_sequence = FALSE;
Packit Service fb6fa5
      context_simple->tentative_match = 0;
Packit Service fb6fa5
      context_simple->tentative_match_len = 0;
Packit Service fb6fa5
      g_signal_emit_by_name (context_simple, "preedit-changed");
Packit Service fb6fa5
      g_signal_emit_by_name (context_simple, "preedit-end");
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void     
Packit Service fb6fa5
gtk_im_context_simple_get_preedit_string (GtkIMContext   *context,
Packit Service fb6fa5
					  gchar         **str,
Packit Service fb6fa5
					  PangoAttrList **attrs,
Packit Service fb6fa5
					  gint           *cursor_pos)
Packit Service fb6fa5
{
Packit Service fb6fa5
  char outbuf[37]; /* up to 6 hex digits */
Packit Service fb6fa5
  int len = 0;
Packit Service fb6fa5
  
Packit Service fb6fa5
  GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
Packit Service fb6fa5
Packit Service fb6fa5
  if (context_simple->in_hex_sequence)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      int hexchars = 0;
Packit Service fb6fa5
         
Packit Service fb6fa5
      outbuf[0] = 'u';
Packit Service fb6fa5
      len = 1;
Packit Service fb6fa5
Packit Service fb6fa5
      while (context_simple->compose_buffer[hexchars] != 0)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  len += g_unichar_to_utf8 (gdk_keyval_to_unicode (context_simple->compose_buffer[hexchars]),
Packit Service fb6fa5
				    outbuf + len);
Packit Service fb6fa5
	  ++hexchars;
Packit Service fb6fa5
	}
Packit Service fb6fa5
Packit Service fb6fa5
      g_assert (len < 25);
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else if (context_simple->tentative_match)
Packit Service fb6fa5
    len = g_unichar_to_utf8 (context_simple->tentative_match, outbuf);
Packit Service fb6fa5
      
Packit Service fb6fa5
  outbuf[len] = '\0';      
Packit Service fb6fa5
Packit Service fb6fa5
  if (str)
Packit Service fb6fa5
    *str = g_strdup (outbuf);
Packit Service fb6fa5
Packit Service fb6fa5
  if (attrs)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      *attrs = pango_attr_list_new ();
Packit Service fb6fa5
      
Packit Service fb6fa5
      if (len)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
Packit Service fb6fa5
	  attr->start_index = 0;
Packit Service fb6fa5
          attr->end_index = len;
Packit Service fb6fa5
	  pango_attr_list_insert (*attrs, attr);
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (cursor_pos)
Packit Service fb6fa5
    *cursor_pos = len;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/**
Packit Service fb6fa5
 * gtk_im_context_simple_add_table:
Packit Service fb6fa5
 * @context_simple: A #GtkIMContextSimple
Packit Service fb6fa5
 * @data: the table 
Packit Service fb6fa5
 * @max_seq_len: Maximum length of a sequence in the table
Packit Service fb6fa5
 *               (cannot be greater than #GTK_MAX_COMPOSE_LEN)
Packit Service fb6fa5
 * @n_seqs: number of sequences in the table
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Adds an additional table to search to the input context.
Packit Service fb6fa5
 * Each row of the table consists of @max_seq_len key symbols
Packit Service fb6fa5
 * followed by two #guint16 interpreted as the high and low
Packit Service fb6fa5
 * words of a #gunicode value. Tables are searched starting
Packit Service fb6fa5
 * from the last added.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * The table must be sorted in dictionary order on the
Packit Service fb6fa5
 * numeric value of the key symbol fields. (Values beyond
Packit Service fb6fa5
 * the length of the sequence should be zero.)
Packit Service fb6fa5
 **/
Packit Service fb6fa5
void
Packit Service fb6fa5
gtk_im_context_simple_add_table (GtkIMContextSimple *context_simple,
Packit Service fb6fa5
				 guint16            *data,
Packit Service fb6fa5
				 gint                max_seq_len,
Packit Service fb6fa5
				 gint                n_seqs)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkComposeTable *table;
Packit Service fb6fa5
Packit Service fb6fa5
  g_return_if_fail (GTK_IS_IM_CONTEXT_SIMPLE (context_simple));
Packit Service fb6fa5
  g_return_if_fail (data != NULL);
Packit Service fb6fa5
  g_return_if_fail (max_seq_len <= GTK_MAX_COMPOSE_LEN);
Packit Service fb6fa5
  
Packit Service fb6fa5
  table = g_new (GtkComposeTable, 1);
Packit Service fb6fa5
  table->data = data;
Packit Service fb6fa5
  table->max_seq_len = max_seq_len;
Packit Service fb6fa5
  table->n_seqs = n_seqs;
Packit Service fb6fa5
Packit Service fb6fa5
  context_simple->tables = g_slist_prepend (context_simple->tables, table);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
#define __GTK_IM_CONTEXT_SIMPLE_C__
Packit Service fb6fa5
#include "gtkaliasdef.c"