1997-11-24 22:37:52 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
1998-04-13 02:02:47 +00:00
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
1997-11-24 22:37:52 +00:00
|
|
|
*/
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "gtkaccelerator.h"
|
|
|
|
#include "gtksignal.h"
|
|
|
|
#include "gtkwidget.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct _GtkAcceleratorEntry GtkAcceleratorEntry;
|
|
|
|
|
|
|
|
struct _GtkAcceleratorEntry
|
|
|
|
{
|
|
|
|
guint8 modifiers;
|
|
|
|
GtkObject *object;
|
1998-03-09 15:16:28 +00:00
|
|
|
guint signal_id;
|
1997-11-24 22:37:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void gtk_accelerator_table_init (GtkAcceleratorTable *table);
|
|
|
|
static void gtk_accelerator_table_clean (GtkAcceleratorTable *table);
|
|
|
|
|
|
|
|
|
|
|
|
static GtkAcceleratorTable *default_table = NULL;
|
|
|
|
static GSList *tables = NULL;
|
1998-03-09 15:16:28 +00:00
|
|
|
static guint8 gtk_accelerator_table_default_mod_mask = (guint8) ~0;
|
1998-04-24 00:26:01 +00:00
|
|
|
static const gchar *actable_key = "gtk-accelerator-tables";
|
|
|
|
static guint actable_key_id = 0;
|
1997-11-24 22:37:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
GtkAcceleratorTable*
|
1998-05-03 22:41:32 +00:00
|
|
|
gtk_accelerator_table_new (void)
|
1997-11-24 22:37:52 +00:00
|
|
|
{
|
|
|
|
GtkAcceleratorTable *table;
|
|
|
|
|
1998-04-24 00:26:01 +00:00
|
|
|
if (!actable_key_id)
|
|
|
|
actable_key_id = gtk_object_data_force_id (actable_key);
|
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
table = g_new (GtkAcceleratorTable, 1);
|
|
|
|
gtk_accelerator_table_init (table);
|
|
|
|
|
|
|
|
tables = g_slist_prepend (tables, table);
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkAcceleratorTable*
|
|
|
|
gtk_accelerator_table_find (GtkObject *object,
|
|
|
|
const gchar *signal_name,
|
|
|
|
guchar accelerator_key,
|
|
|
|
guint8 accelerator_mods)
|
|
|
|
{
|
|
|
|
GtkAcceleratorTable *table;
|
|
|
|
GtkAcceleratorEntry *entry;
|
|
|
|
GSList *tmp_list;
|
|
|
|
GList *entries;
|
1998-03-09 15:16:28 +00:00
|
|
|
guint signal_id;
|
1997-11-24 22:37:52 +00:00
|
|
|
guint hash;
|
|
|
|
|
|
|
|
g_return_val_if_fail (object != NULL, NULL);
|
|
|
|
g_return_val_if_fail (signal_name != NULL, NULL);
|
|
|
|
|
1998-03-09 15:16:28 +00:00
|
|
|
signal_id = gtk_signal_lookup (signal_name, GTK_OBJECT_TYPE (object));
|
1997-11-24 22:37:52 +00:00
|
|
|
hash = (guint) accelerator_key;
|
|
|
|
|
|
|
|
tmp_list = tables;
|
|
|
|
while (tmp_list)
|
|
|
|
{
|
|
|
|
table = tmp_list->data;
|
|
|
|
tmp_list = tmp_list->next;
|
|
|
|
|
|
|
|
entries = table->entries[hash];
|
|
|
|
while (entries)
|
|
|
|
{
|
|
|
|
entry = entries->data;
|
|
|
|
entries = entries->next;
|
|
|
|
|
|
|
|
if ((entry->object == object) &&
|
1998-03-09 15:16:28 +00:00
|
|
|
(entry->signal_id == signal_id) &&
|
1997-11-24 22:37:52 +00:00
|
|
|
((entry->modifiers & table->modifier_mask) ==
|
|
|
|
(accelerator_mods & table->modifier_mask)))
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkAcceleratorTable*
|
|
|
|
gtk_accelerator_table_ref (GtkAcceleratorTable *table)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (table != NULL, NULL);
|
|
|
|
|
|
|
|
table->ref_count += 1;
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_accelerator_table_unref (GtkAcceleratorTable *table)
|
|
|
|
{
|
|
|
|
g_return_if_fail (table != NULL);
|
1998-04-24 00:26:01 +00:00
|
|
|
g_return_if_fail (table->ref_count > 0);
|
1997-11-24 22:37:52 +00:00
|
|
|
|
|
|
|
table->ref_count -= 1;
|
1998-04-24 00:26:01 +00:00
|
|
|
if (table->ref_count == 0)
|
1997-12-18 02:17:14 +00:00
|
|
|
{
|
|
|
|
tables = g_slist_remove (tables, table);
|
|
|
|
gtk_accelerator_table_clean (table);
|
|
|
|
g_free (table);
|
|
|
|
}
|
1997-11-24 22:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_accelerator_table_install (GtkAcceleratorTable *table,
|
|
|
|
GtkObject *object,
|
|
|
|
const gchar *signal_name,
|
|
|
|
guchar accelerator_key,
|
|
|
|
guint8 accelerator_mods)
|
|
|
|
{
|
|
|
|
GtkAcceleratorEntry *entry;
|
|
|
|
GList *entries;
|
|
|
|
gchar *signame;
|
1998-03-09 15:16:28 +00:00
|
|
|
guint signal_id;
|
1997-11-24 22:37:52 +00:00
|
|
|
guint hash;
|
|
|
|
|
|
|
|
g_return_if_fail (object != NULL);
|
|
|
|
|
|
|
|
if (!table)
|
|
|
|
{
|
|
|
|
if (!default_table)
|
|
|
|
default_table = gtk_accelerator_table_new ();
|
|
|
|
table = default_table;
|
|
|
|
}
|
|
|
|
|
1998-03-09 15:16:28 +00:00
|
|
|
signal_id = gtk_signal_lookup (signal_name, GTK_OBJECT_TYPE (object));
|
|
|
|
g_return_if_fail (signal_id != 0);
|
1997-11-24 22:37:52 +00:00
|
|
|
|
|
|
|
hash = (guint) accelerator_key;
|
|
|
|
entries = table->entries[hash];
|
|
|
|
|
|
|
|
while (entries)
|
|
|
|
{
|
|
|
|
entry = entries->data;
|
|
|
|
|
|
|
|
if ((entry->modifiers & table->modifier_mask) ==
|
|
|
|
(accelerator_mods & table->modifier_mask))
|
|
|
|
{
|
1998-04-24 00:26:01 +00:00
|
|
|
gtk_object_set_data_by_id (entry->object,
|
|
|
|
actable_key_id,
|
|
|
|
g_slist_remove (gtk_object_get_data_by_id (entry->object,
|
|
|
|
actable_key_id),
|
|
|
|
table));
|
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
if (GTK_IS_WIDGET (entry->object))
|
|
|
|
{
|
1998-03-09 15:16:28 +00:00
|
|
|
signame = gtk_signal_name (entry->signal_id);
|
1997-11-24 22:37:52 +00:00
|
|
|
gtk_signal_emit_by_name (entry->object,
|
|
|
|
"remove_accelerator",
|
|
|
|
signame);
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->modifiers = accelerator_mods;
|
|
|
|
entry->object = object;
|
1998-03-09 15:16:28 +00:00
|
|
|
entry->signal_id = signal_id;
|
1998-04-24 00:26:01 +00:00
|
|
|
gtk_object_set_data_by_id (entry->object,
|
|
|
|
actable_key_id,
|
|
|
|
g_slist_prepend (gtk_object_get_data_by_id (entry->object,
|
|
|
|
actable_key_id),
|
|
|
|
table));
|
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = entries->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = g_new (GtkAcceleratorEntry, 1);
|
|
|
|
entry->modifiers = accelerator_mods;
|
|
|
|
entry->object = object;
|
1998-03-09 15:16:28 +00:00
|
|
|
entry->signal_id = signal_id;
|
1998-04-24 00:26:01 +00:00
|
|
|
gtk_object_set_data_by_id (entry->object,
|
|
|
|
actable_key_id,
|
|
|
|
g_slist_prepend (gtk_object_get_data_by_id (entry->object,
|
|
|
|
actable_key_id),
|
|
|
|
table));
|
|
|
|
gtk_accelerator_table_ref (table);
|
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
table->entries[hash] = g_list_prepend (table->entries[hash], entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_accelerator_table_remove (GtkAcceleratorTable *table,
|
|
|
|
GtkObject *object,
|
|
|
|
const gchar *signal_name)
|
|
|
|
{
|
|
|
|
GtkAcceleratorEntry *entry;
|
|
|
|
GList *entries;
|
|
|
|
GList *temp_list;
|
1998-03-09 15:16:28 +00:00
|
|
|
guint signal_id;
|
1997-11-24 22:37:52 +00:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_if_fail (object != NULL);
|
|
|
|
|
|
|
|
if (!table)
|
|
|
|
{
|
|
|
|
if (!default_table)
|
|
|
|
default_table = gtk_accelerator_table_new ();
|
|
|
|
table = default_table;
|
|
|
|
}
|
|
|
|
|
1998-03-09 15:16:28 +00:00
|
|
|
signal_id = gtk_signal_lookup (signal_name, GTK_OBJECT_TYPE (object));
|
|
|
|
g_return_if_fail (signal_id != 0);
|
1997-11-24 22:37:52 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
entries = table->entries[i];
|
|
|
|
|
|
|
|
while (entries)
|
|
|
|
{
|
|
|
|
entry = entries->data;
|
|
|
|
|
1998-03-09 15:16:28 +00:00
|
|
|
if ((entry->object == object) && (entry->signal_id == signal_id))
|
1997-11-24 22:37:52 +00:00
|
|
|
{
|
|
|
|
g_free (entry);
|
|
|
|
|
|
|
|
temp_list = entries;
|
|
|
|
if (entries->next)
|
|
|
|
entries->next->prev = entries->prev;
|
|
|
|
if (entries->prev)
|
|
|
|
entries->prev->next = entries->next;
|
|
|
|
if (table->entries[i] == entries)
|
|
|
|
table->entries[i] = entries->next;
|
|
|
|
|
|
|
|
temp_list->next = NULL;
|
|
|
|
temp_list->prev = NULL;
|
|
|
|
g_list_free (temp_list);
|
1998-04-24 00:26:01 +00:00
|
|
|
|
|
|
|
gtk_object_set_data_by_id (object,
|
|
|
|
actable_key_id,
|
|
|
|
g_slist_remove (gtk_object_get_data_by_id (object,
|
|
|
|
actable_key_id),
|
|
|
|
table));
|
|
|
|
gtk_accelerator_table_unref (table);
|
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = entries->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
gtk_accelerator_table_check (GtkAcceleratorTable *table,
|
|
|
|
const guchar accelerator_key,
|
|
|
|
guint8 accelerator_mods)
|
|
|
|
{
|
|
|
|
GtkAcceleratorEntry *entry;
|
|
|
|
GList *entries;
|
|
|
|
guint hash;
|
|
|
|
|
|
|
|
if (!table)
|
|
|
|
{
|
|
|
|
if (!default_table)
|
|
|
|
default_table = gtk_accelerator_table_new ();
|
|
|
|
table = default_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = (guint) accelerator_key;
|
|
|
|
entries = table->entries[hash];
|
|
|
|
|
|
|
|
while (entries)
|
|
|
|
{
|
|
|
|
entry = entries->data;
|
|
|
|
|
|
|
|
if ((entry->modifiers & table->modifier_mask) ==
|
|
|
|
(accelerator_mods & table->modifier_mask))
|
|
|
|
{
|
1998-03-09 15:16:28 +00:00
|
|
|
gtk_signal_emit (entry->object, entry->signal_id);
|
1997-11-24 22:37:52 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = entries->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isupper (hash))
|
|
|
|
{
|
|
|
|
hash = toupper (hash);
|
|
|
|
entries = table->entries[hash];
|
|
|
|
|
|
|
|
while (entries)
|
|
|
|
{
|
|
|
|
entry = entries->data;
|
|
|
|
|
|
|
|
if (((entry->modifiers & table->modifier_mask) ==
|
|
|
|
(accelerator_mods & table->modifier_mask)) &&
|
|
|
|
(GTK_IS_WIDGET (entry->object) &&
|
|
|
|
GTK_WIDGET_SENSITIVE (entry->object)))
|
|
|
|
{
|
1998-03-09 15:16:28 +00:00
|
|
|
gtk_signal_emit (entry->object, entry->signal_id);
|
1997-11-24 22:37:52 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = entries->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_accelerator_table_set_mod_mask (GtkAcceleratorTable *table,
|
|
|
|
guint8 modifier_mask)
|
|
|
|
{
|
|
|
|
if (table == NULL)
|
|
|
|
{
|
|
|
|
gtk_accelerator_table_default_mod_mask = modifier_mask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table->modifier_mask = modifier_mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_accelerator_table_init (GtkAcceleratorTable *table)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_if_fail (table != NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
table->entries[i] = NULL;
|
|
|
|
|
1997-12-18 02:17:14 +00:00
|
|
|
table->ref_count = 1;
|
1997-11-24 22:37:52 +00:00
|
|
|
table->modifier_mask = gtk_accelerator_table_default_mod_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_accelerator_table_clean (GtkAcceleratorTable *table)
|
|
|
|
{
|
|
|
|
GtkAcceleratorEntry *entry;
|
|
|
|
GList *entries;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_if_fail (table != NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
entries = table->entries[i];
|
1998-04-24 00:26:01 +00:00
|
|
|
if (entries)
|
|
|
|
g_warning ("stale object reference in accelerator table (%d)", i);
|
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
while (entries)
|
|
|
|
{
|
|
|
|
entry = entries->data;
|
|
|
|
entries = entries->next;
|
|
|
|
|
|
|
|
g_free (entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_free (table->entries[i]);
|
|
|
|
table->entries[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
1998-04-24 00:26:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
gtk_accelerator_tables_delete (GtkObject *object)
|
|
|
|
{
|
|
|
|
GSList *slist;
|
|
|
|
|
|
|
|
g_return_if_fail (object != NULL);
|
|
|
|
|
|
|
|
slist = gtk_object_get_data_by_id (object, actable_key_id);
|
|
|
|
if (slist)
|
|
|
|
{
|
|
|
|
for (; slist; slist = slist->next)
|
|
|
|
{
|
|
|
|
GtkAcceleratorTable *table;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
table = slist->data;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
GList *entries;
|
|
|
|
|
|
|
|
entries = table->entries[i];
|
|
|
|
|
|
|
|
while (entries)
|
|
|
|
{
|
|
|
|
GtkAcceleratorEntry *entry;
|
|
|
|
|
|
|
|
entry = entries->data;
|
|
|
|
entries = entries->next;
|
|
|
|
|
|
|
|
if (entry->object == object)
|
|
|
|
{
|
|
|
|
table->entries[i] = g_list_remove (table->entries[i], entry);
|
|
|
|
g_free (entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_accelerator_table_unref (table);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_object_remove_data_by_id (object, actable_key_id);
|
|
|
|
}
|
|
|
|
}
|