From 3fc61ea40557955d0b2bd37792d4fe31f3cf02ba Mon Sep 17 00:00:00 2001 From: Felix Ruoff Date: Sun, 9 Jan 2011 22:31:47 +0100 Subject: [PATCH] Replace keyname table with gdk-functions This patch introduces the possibility to use gtk keynames or the corresponding character for accelerators in the gpcb-menu.res file. One example: You can use "." or "period" without any functional difference. The same is for (hopefully) all keys avaiable in the gdk/gdkkeysyms.h headerfile. How does the patch work? First, it is checked, if the accelerator-string (just the key, not the modifier) is more than one character long. In this case, the corresponding keyval (a numerical representation of the key) will be selected. If the given string is invalid, keyval will be zero. If the given accelerator-string is just one character long, a character is expected, that must be converted to a keyname. Therefor this character will be converted to an unicode character and, if this is valid, this unicode character will be converted to the correspondign keyval. So, the keyval can be given in two ways - as character or as keyname. After this, the keyval will be converted to a valid keyname if the keyval is larger than zero. Keyval == 0 indicates an error. The error-message will be printed to the log-window. --- src/hid/gtk/gui-top-window.c | 76 ++++++++++++++++------------------------- 1 files changed, 30 insertions(+), 46 deletions(-) diff --git a/src/hid/gtk/gui-top-window.c b/src/hid/gtk/gui-top-window.c index f2cdb99..720b40d 100644 --- a/src/hid/gtk/gui-top-window.c +++ b/src/hid/gtk/gui-top-window.c @@ -3076,32 +3076,6 @@ ghid_append_toggle_action (const char * name, const char *stock_id, tmenuitem_cnt++; } -/* - * Some keys need to be replaced by a name for the gtk accelerators to - * work. This table contains the translations. The "in" character is - * what would appear in gpcb-menu.res and the "out" string is what we - * have to feed to gtk. I was able to find these by using xev to find - * the keycode and then looked at gtk+-2.10.9/gdk/keynames.txt (from the - * gtk source distribution) to figure out the names that go with the - * codes. - */ -typedef struct -{ - const char in; - const char *out; -} KeyTable; -static KeyTable key_table[] = - { - {':', "colon"}, - {'=', "equal"}, - {'/', "slash"}, - {'[', "bracketleft"}, - {']', "bracketright"}, - {'.', "period"}, - {'|', "bar"} - }; -static int n_key_table = sizeof (key_table) / sizeof (key_table[0]); - static void add_resource_to_menu (char * menu, Resource * node, void * callback, int indent) { @@ -3176,7 +3150,6 @@ add_resource_to_menu (char * menu, Resource * node, void * callback, int indent) * */ char *p; - int j; enum {KEY, MOD} state; state = MOD; @@ -3252,27 +3225,38 @@ add_resource_to_menu (char * menu, Resource * node, void * callback, int indent) } else { - ch[0] = *p; - for (j = 0; j < n_key_table; j++) - { - if ( *p == key_table[j].in) - { - strncat (accel, key_table[j].out, accel_n); - accel_n -= strlen (key_table[j].out); - j = n_key_table; - } - } - - if (j == n_key_table) - { - strncat (accel, ch, accel_n); - accel_n -= strlen (ch); - } - - p++; + guint keyval = 0; + /* if strlen(p) > 1, a keyname is expected as 'period' + * or 'colon'. Otherwise a single character + * like ':', 'a' or '/'. + */ + if(strlen(p) > 1) + { + /* Check for valid keyname */ + keyval = gdk_keyval_from_name(p); + } + else + { + /* Check for valid character */ + gunichar unichar = g_utf8_get_char_validated(p, 1); + if(unichar != -2 && unichar != -1) + { + keyval = gdk_unicode_to_keyval(unichar); + } + } + if(keyval != 0) + { + const char *keyval_name = gdk_keyval_name(keyval); + strncat (accel, keyval_name, strlen(keyval_name)); + accel_n -= strlen (keyval_name); + } + else + { + Message (_("Invalid accelerator key '%s' given.\n"), p); + } + p += strlen(p); } break; - } if (G_UNLIKELY (accel_n < 0)) -- 1.7.1