Blame gtk/gtkimcontextsimple.c

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