gtk2/gtk/gtkkeyhash.c

380 lines
9.8 KiB
C
Raw Normal View History

Implement "fuzzy" key binding lookups; allow matches on key and level but Wed Feb 20 14:26:47 2002 Owen Taylor <otaylor@redhat.com> * gtk/gtkkeyhash.[ch]: Implement "fuzzy" key binding lookups; allow matches on key and level but not group. Also, implement ignoring "consumed modifiers correctly." * gtk/gtkaccelgroup.c gtk/gtkbindings.c: Convert to using GtkKeyHash. * gtk/gtkdebug.h gtk/gtkmain.c: Support GTK_DEBUG=keybindings * gdk/x11/gdkevents-x11.c (gdk_event_translate): Fill in the group for key release events as well as key press events. * gdk/gdkkeys.h gdk/x11/gdkkeys-x11.c (gdk_keymap_translate_keyboard_state): Rename unused_modifiers to consumed_modifiers, make the docs and non-Xkb implementation match the Xkb implementation. * gdk/linux-fb/gdkkeyboard-fb.c gdk/win32/gdkkeys-win32.c: Propagate doc and parameter name changes. * gdk/x11/gdkkeys-x11.c (gdk_keymap_translate_keyboard_state): XkbTranslateKeyCode doesn't handle LockMask, we need to handle it ourselves. * gdk/x11/gdkkeys-x11.c (gdk_keymap_translate_keyboard_state): Force <Shift>Tab to give GDK_ISO_Left_Tab, since we need consistency to allow dealing with ISO_Left_Tab. * gtk/gtkwindow.c gtk/gtktextview.c gtk/gtkscrolledwindow.c gtk/gtkpaned.c gtk/gtkcombo.c gtk/gtknotebook.c: Remove inappropriate uses of GDK_ISO_Left_Tab. (GDK_ISO_Left_Tab or <Shift>Tab both are equivalent as a binding specifier.) * gtk/gtkbutton.c (gtk_button_class_init): Make ::activate GTK_RUN_ACTION, so you can bind an accelerator to it. * gtk/gtklabel.c (gtk_label_set_uline_text_internal): Call gdk_unicode_to_keyval on the mnemonic character. * tests/testgtk.c: Add a test for the new fuzzy key binding matching.
2002-02-21 17:14:10 +00:00
/* gtkkeyhash.c: Keymap aware matching of key bindings
*
* GTK - The GIMP Toolkit
* Copyright (C) 2002, Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "gtkdebug.h"
#include "gtkkeyhash.h"
/* We need to add a ::changed signal to GdkKeyMap to properly deal
* with changes to the key map while we are running.
*/
#undef HAVE_CHANGED_SIGNAL
typedef struct _GtkKeyHashEntry GtkKeyHashEntry;
struct _GtkKeyHashEntry
{
guint keyval;
GdkModifierType modifiers;
GdkKeymapKey *keys;
gint n_keys;
gpointer value;
};
struct _GtkKeyHash
{
GdkKeymap *keymap;
GHashTable *keycode_hash;
GHashTable *reverse_hash;
GDestroyNotify destroy_notify;
};
static void
key_hash_clear_keycode (gpointer key,
gpointer value,
gpointer data)
{
GSList *keys = value;
g_slist_free (keys);
}
static void
key_hash_insert_entry (GtkKeyHash *key_hash,
GtkKeyHashEntry *entry)
{
gint i;
gdk_keymap_get_entries_for_keyval (key_hash->keymap,
entry->keyval,
&entry->keys, &entry->n_keys);
for (i = 0; i < entry->n_keys; i++)
{
GSList *old_keys = g_hash_table_lookup (key_hash->keycode_hash,
GUINT_TO_POINTER (entry->keys[i].keycode));
old_keys = g_slist_prepend (old_keys, entry);
g_hash_table_insert (key_hash->keycode_hash,
GUINT_TO_POINTER (entry->keys[i].keycode),
old_keys);
}
}
#ifdef HAVE_CHANGED_SIGNAL
static void
key_hash_reinsert_entry (gpointer key,
gpointer value,
gpointer data)
{
GtkKeyHashEntry *entry = value;
GtkKeyHash *key_hash = data;
g_free (entry->keys);
key_hash_insert_entry (key_hash, entry);
}
static void
key_hash_keymap_changed (GdkKeymap *keymap,
GtkKeyHash *key_hash)
{
/* The keymap changed, so we have to clear and reinsert all our entries
*/
g_hash_table_foreach (key_hash->keycode_hash, key_hash_clear_keycode, NULL);
/* FIXME: Here we reinsert in random order, but I think we actually have to
* insert in the same order as the original order to keep GtkBindingSet happy.
*/
g_hash_table_foreach (key_hash->reverse_hash, key_hash_reinsert_entry, key_hash);
}
#endif /* HAVE_CHANGED_SIGNAL */
/**
* _gtk_key_hash_new:
* @keymap: a #GdkKeymap
* @item_destroy_notify: function to be called when items are removed
* from the hash or %NULL.
*
* Create a new key hash object for doing binding resolution.
*
* Return value: the newly created object. Free with _gtk_key_hash_free().
**/
GtkKeyHash *
_gtk_key_hash_new (GdkKeymap *keymap,
GDestroyNotify item_destroy_notify)
{
GtkKeyHash *key_hash = g_new (GtkKeyHash, 1);
key_hash->keymap = keymap;
#ifdef HAVE_CHANGED_SIGNAL
g_signal_connect (keymap, "changed",
G_CALLBACK (key_hash_keymap_changed), key_hash);
#endif
key_hash->keycode_hash = g_hash_table_new (g_direct_hash, NULL);
key_hash->reverse_hash = g_hash_table_new (g_direct_hash, NULL);
key_hash->destroy_notify = item_destroy_notify;
return key_hash;
}
static void
key_hash_free_entry (GtkKeyHash *key_hash,
GtkKeyHashEntry *entry)
{
if (key_hash->destroy_notify)
(*key_hash->destroy_notify) (entry->value);
g_free (entry->keys);
g_free (entry);
}
static void
key_hash_free_entry_foreach (gpointer key,
gpointer value,
gpointer data)
{
GtkKeyHashEntry *entry = value;
GtkKeyHash *key_hash = data;
key_hash_free_entry (key_hash, entry);
}
/**
* gtk_key_hash_free:
* @key_hash: a #GtkKeyHash
*
* Destroys a key hash created with gtk_key_hash_new()
**/
void
_gtk_key_hash_free (GtkKeyHash *key_hash)
{
#if HAVE_CHANGED_SIGNAL
g_signal_handlers_disconnect_by_func (key_hash->keymap,
G_CALLBACK (key_hash_keymap_changed), key_hash);
#endif
g_hash_table_foreach (key_hash->keycode_hash, key_hash_clear_keycode, NULL);
g_hash_table_foreach (key_hash->reverse_hash, key_hash_free_entry_foreach, key_hash);
g_hash_table_destroy (key_hash->keycode_hash);
g_hash_table_destroy (key_hash->reverse_hash);
g_free (key_hash);
}
/**
* _gtk_key_hash_add_entry:
* @key_hash: a #GtkKeyHash
* @keyval: key symbol for this binding
* @modifiers: modifiers for this binding
* @value: value to insert in the key hash
*
* Inserts a pair of key symbol and modifier mask into the key hash.
**/
void
_gtk_key_hash_add_entry (GtkKeyHash *key_hash,
guint keyval,
GdkModifierType modifiers,
gpointer value)
{
GtkKeyHashEntry *entry = g_new (GtkKeyHashEntry, 1);
entry->value = value;
entry->keyval = keyval;
entry->modifiers = modifiers;
g_hash_table_insert (key_hash->reverse_hash, value, entry);
key_hash_insert_entry (key_hash, entry);
}
/**
* _gtk_key_hash_remove_entry:
* @key_hash: a #GtkKeyHash
* @value: value previously added with _gtk_key_hash_add_entry()
*
* Removes a value previously added to the key hash with
* _gtk_key_hash_add_entry().
**/
void
_gtk_key_hash_remove_entry (GtkKeyHash *key_hash,
gpointer value)
{
GtkKeyHashEntry *entry = g_hash_table_lookup (key_hash->reverse_hash, value);
if (entry)
{
gint i;
for (i = 0; i < entry->n_keys; i++)
{
GSList *old_keys = g_hash_table_lookup (key_hash->keycode_hash,
GUINT_TO_POINTER (entry->keys[i].keycode));
GSList *new_keys = g_slist_remove (old_keys, entry);
if (old_keys != new_keys)
{
if (old_keys)
g_hash_table_insert (key_hash->keycode_hash,
GUINT_TO_POINTER (entry->keys[i].keycode),
old_keys);
else
g_hash_table_remove (key_hash->keycode_hash,
GUINT_TO_POINTER (entry->keys[i].keycode));
}
}
g_hash_table_remove (key_hash->reverse_hash, value);
key_hash_free_entry (key_hash, entry);
g_free (entry);
}
}
/**
* _gtk_key_hash_lookup:
* @key_hash: a #GtkKeyHash
* @hardware_keycode: hardware keycode field from a #GdkEventKey
* @state: state field from a #GdkEventKey
* @group: group field from a #GdkEventKey
*
* Looks up the best matching entry or entries in the hash table for
* a given event.
*
* Return value: A #GSList of all matching entries. If there were exact
* matches, they are returned, otherwise all fuzzy matches are
* returned. (A fuzzy match is a match in keycode and level, but not
* in group.)
**/
GSList *
_gtk_key_hash_lookup (GtkKeyHash *key_hash,
guint16 hardware_keycode,
GdkModifierType state,
gint group)
{
GSList *keys = g_hash_table_lookup (key_hash->keycode_hash, GUINT_TO_POINTER ((guint)hardware_keycode));
GSList *results = NULL;
gboolean have_exact = FALSE;
guint keyval;
gint effective_group;
gint level;
GdkModifierType consumed_modifiers;
gdk_keymap_translate_keyboard_state (key_hash->keymap,
hardware_keycode, state, group,
&keyval, &effective_group, &level, &consumed_modifiers);
GTK_NOTE (KEYBINDINGS,
g_message ("Looking up keycode = %u, modifiers = 0x%04x,\n"
" keyval = %u, group = %d, level = %d, consumed_modifiers = 0x%04x",
hardware_keycode, state, keyval, effective_group, level, consumed_modifiers));
if (keys)
{
GSList *tmp_list = keys;
while (tmp_list)
{
GtkKeyHashEntry *entry = tmp_list->data;
if ((entry->modifiers & ~consumed_modifiers) == (state & ~consumed_modifiers))
{
gint i;
if (keyval == entry->keyval) /* Exact match */
{
GTK_NOTE (KEYBINDINGS,
g_message (" found exact match, keyval = %u, modifiers = 0x%04x",
entry->keyval, entry->modifiers));
if (!have_exact)
{
g_slist_free (results);
results = NULL;
}
have_exact = TRUE;
results = g_slist_prepend (results, entry->value);
}
if (!have_exact)
{
for (i = 0; i < entry->n_keys; i++)
{
if (entry->keys[i].keycode == hardware_keycode &&
entry->keys[i].level == level) /* Match for all but group */
{
GTK_NOTE (KEYBINDINGS,
g_message (" found group = %d, level = %d",
entry->keys[i].group, entry->keys[i].level));
results = g_slist_prepend (results, entry->value);
break;
}
}
}
}
tmp_list = tmp_list->next;
}
}
return results;
}
/**
* _gtk_key_hash_lookup_keyval:
* @key_hash: a #GtkKeyHash
* @event: a #GtkEvent
*
* Looks up the best matching entry or entries in the hash table for a
* given keyval/modifiers pair. It's better to use
* _gtk_key_hash_lookup() if you have the original #GdkEventKey
* available.
*
* Return value: A #GSList of all matching entries.
**/
GSList *
_gtk_key_hash_lookup_keyval (GtkKeyHash *key_hash,
guint keyval,
GdkModifierType modifiers)
{
GdkKeymapKey *keys;
gint n_keys;
GSList *results = NULL;
/* Find some random keycode for this keycode
*/
gdk_keymap_get_entries_for_keyval (key_hash->keymap, keyval,
&keys, &n_keys);
if (n_keys)
{
GSList *entries = g_hash_table_lookup (key_hash->keycode_hash, GUINT_TO_POINTER (keys[0].keycode));
while (entries)
{
GtkKeyHashEntry *entry = entries->data;
if (entry->keyval == keyval && entry->modifiers == modifiers)
results = g_slist_prepend (results, entry->value);
entries = entries->next;
}
}
g_free (keys);
return results;
}