changeset 15979:2c81ebc7bf0b

Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Wed, 04 Apr 2007 03:47:13 +0000
parents 2a82bc8d57f7
children 7adb832667fd
files finch/libgnt/gntkeys.c finch/libgnt/gntkeys.h finch/libgnt/gntutils.c finch/libgnt/gntutils.h
diffstat 4 files changed, 73 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/finch/libgnt/gntkeys.c	Sun Apr 01 08:05:59 2007 +0000
+++ b/finch/libgnt/gntkeys.c	Wed Apr 04 03:47:13 2007 +0000
@@ -134,6 +134,28 @@
 	return g_hash_table_lookup(specials, name);
 }
 
+typedef struct {
+	const char *name;
+	const char *key;
+} gntkey;
+
+static void
+get_key_name(gpointer key, gpointer value, gpointer data)
+{
+	gntkey *k = data;
+	if (k->name)
+		return;
+	if (g_utf8_collate(value, k->key) == 0)
+		k->name = key;
+}
+
+const char *gnt_key_lookup(const char *key)
+{
+	gntkey k = {NULL, key};
+	g_hash_table_foreach(specials, get_key_name, &k);
+	return k.name;
+}
+
 /**
  * The key-bindings will be saved in a tree. When a keystroke happens, GNT will
  * find the sequence that matches a binding and return the length.
--- a/finch/libgnt/gntkeys.h	Sun Apr 01 08:05:59 2007 +0000
+++ b/finch/libgnt/gntkeys.h	Wed Apr 04 03:47:13 2007 +0000
@@ -82,6 +82,7 @@
 void gnt_init_keys(void);
 void gnt_keys_refine(char *text);
 const char *gnt_key_translate(const char *name);
+const char *gnt_key_lookup(const char *key);
 
 void gnt_keys_add_combination(const char *path);
 void gnt_keys_del_combination(const char *path);
--- a/finch/libgnt/gntutils.c	Sun Apr 01 08:05:59 2007 +0000
+++ b/finch/libgnt/gntutils.c	Wed Apr 04 03:47:13 2007 +0000
@@ -1,4 +1,5 @@
 #include "gntutils.h"
+#include "gnttree.h"
 
 #include <stdlib.h>
 #include <string.h>
@@ -145,3 +146,47 @@
 	return continue_emission;
 }
 
+typedef struct {
+	GHashTable *hash;
+	GntTree *tree;
+} BindingView;
+
+static void
+add_binding(gpointer key, gpointer value, gpointer data)
+{
+	BindingView *bv = data;
+	GntBindableActionParam *act = value;
+	const char *name = g_hash_table_lookup(bv->hash, act->action);
+	if (name && *name) {
+		const char *k = gnt_key_lookup(key);
+		if (!k)
+			k = key;
+		gnt_tree_add_row_after(bv->tree, (gpointer)k,
+				gnt_tree_create_row(bv->tree, k, name), NULL, NULL);
+	}
+}
+
+static void
+add_action(gpointer key, gpointer value, gpointer data)
+{
+	BindingView *bv = data;
+	g_hash_table_insert(bv->hash, value, key);
+}
+
+GntWidget *gnt_widget_bindings_view(GntWidget *widget)
+{
+	GntBindable *bind = GNT_BINDABLE(widget);
+	GntWidget *tree = gnt_tree_new_with_columns(2);
+	GntBindableClass *klass = GNT_BINDABLE_CLASS(GNT_BINDABLE_GET_CLASS(bind));
+	GHashTable *hash = g_hash_table_new(g_direct_hash, g_direct_equal);
+	BindingView bv = {hash, GNT_TREE(tree)};
+
+	gnt_tree_set_compare_func(bv.tree, (GCompareFunc)g_utf8_collate);
+	g_hash_table_foreach(klass->actions, add_action, &bv);
+	g_hash_table_foreach(klass->bindings, add_binding, &bv);
+	gnt_tree_adjust_columns(bv.tree);
+	g_hash_table_destroy(hash);
+
+	return tree;
+}
+
--- a/finch/libgnt/gntutils.h	Sun Apr 01 08:05:59 2007 +0000
+++ b/finch/libgnt/gntutils.h	Wed Apr 04 03:47:13 2007 +0000
@@ -34,3 +34,8 @@
 				  const GValue          *handler_return,
 				  gpointer               dummy);
 
+/**
+ * Returns a GntTree populated with "key" -> "binding" for the widget.
+ */
+GntWidget *gnt_widget_bindings_view(GntWidget *widget);
+