[libegg/spread-table-dnd: 25/45] treeviewutils/eggaccelerators: Update from g-c-c



commit 4aa3973baaf92e57025af5e6ee6a329cbccf2616
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Mar 22 14:07:13 2011 +0100

    treeviewutils/eggaccelerators: Update from g-c-c
    
    The version in gnome-control-center is apparently the latest.
    See  http://mail.gnome.org/archives/gtk-devel-list/2011-March/msg00087.html

 libegg/treeviewutils/eggaccelerators.c |  288 ++++++++++++--------------------
 libegg/treeviewutils/eggaccelerators.h |   30 ++--
 2 files changed, 123 insertions(+), 195 deletions(-)
---
diff --git a/libegg/treeviewutils/eggaccelerators.c b/libegg/treeviewutils/eggaccelerators.c
index 50af44a..5a672ba 100644
--- a/libegg/treeviewutils/eggaccelerators.c
+++ b/libegg/treeviewutils/eggaccelerators.c
@@ -22,11 +22,9 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include <gdkconfig.h>
-#ifdef GDK_WINDOWING_X11
 #include <gdk/gdkx.h>
-#endif
 #include <gdk/gdkkeysyms.h>
+#include <gtk/gtk.h>
 
 enum
 {
@@ -208,7 +206,7 @@ is_keycode (const gchar *string)
  * can represent various keyboard keys (numlock, meta, hyper, etc.),
  * the virtual modifier represents the keyboard key, the concrete
  * modifier the actual Mod2-Mod5 bits in the key press event.
- * 
+ *
  * Returns: %TRUE on success.
  */
 gboolean
@@ -221,8 +219,7 @@ egg_accelerator_parse_virtual (const gchar            *accelerator,
   GdkModifierType mods;
   gint len;
   gboolean bad_keyval;
-  gint tmp_keycode;
-  
+
   if (accelerator_key)
     *accelerator_key = 0;
   if (accelerator_mods)
@@ -233,8 +230,7 @@ egg_accelerator_parse_virtual (const gchar            *accelerator,
   g_return_val_if_fail (accelerator != NULL, FALSE);
 
   bad_keyval = FALSE;
-  tmp_keycode = 0;
-  
+
   keyval = 0;
   mods = 0;
   len = strlen (accelerator);
@@ -317,7 +313,7 @@ egg_accelerator_parse_virtual (const gchar            *accelerator,
 	  else
 	    {
 	      gchar last_ch;
-	      
+
 	      last_ch = *accelerator;
 	      while (last_ch && last_ch != '>')
 		{
@@ -333,11 +329,12 @@ egg_accelerator_parse_virtual (const gchar            *accelerator,
 
           if (keyval == 0)
 	    {
-	      /* If keyval is 0, than maybe it's a keycode.  Check for 0x## */
+	      /* If keyval is 0, then maybe it's a keycode.  Check for 0x## */
 	      if (len >= 4 && is_keycode (accelerator))
 		{
 		  char keystring[5];
 		  gchar *endptr;
+		  gint tmp_keycode;
 
 		  memcpy (keystring, accelerator, 4);
 		  keystring [4] = '\000';
@@ -348,37 +345,41 @@ egg_accelerator_parse_virtual (const gchar            *accelerator,
 		    {
 		      bad_keyval = TRUE;
 		    }
-		  else
+		  else if (keycode != NULL)
 		    {
+		      *keycode = tmp_keycode;
 		      /* 0x00 is an invalid keycode too. */
-		      if (tmp_keycode == 0)
+		      if (*keycode == 0)
 			bad_keyval = TRUE;
 		    }
 		}
 	    }
+	  else if (keycode != NULL)
+	    {
+	      *keycode = XKeysymToKeycode (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), keyval);
+	      if (*keycode == 0)
+	 	bad_keyval = TRUE;
+	    }
 
           accelerator += len;
           len -= len;
 	}
     }
-  
+
   if (accelerator_key)
     *accelerator_key = gdk_keyval_to_lower (keyval);
   if (accelerator_mods)
     *accelerator_mods = mods;
-  if (keycode)
-    *keycode = tmp_keycode;
 
   return !bad_keyval;
 }
 
-
 /**
  * egg_virtual_accelerator_name:
  * @accelerator_key:  accelerator keyval
  * @accelerator_mods: accelerator modifier mask
  * @returns:          a newly-allocated accelerator name
- * 
+ *
  * Converts an accelerator keyval and modifier mask
  * into a string parseable by egg_accelerator_parse_virtual().
  * For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
@@ -391,122 +392,57 @@ egg_virtual_accelerator_name (guint                  accelerator_key,
 			      guint		     keycode,
                               EggVirtualModifierType accelerator_mods)
 {
-  static const gchar text_release[] = "<Release>";
-  static const gchar text_shift[] = "<Shift>";
-  static const gchar text_control[] = "<Control>";
-  static const gchar text_mod1[] = "<Alt>";
-  static const gchar text_mod2[] = "<Mod2>";
-  static const gchar text_mod3[] = "<Mod3>";
-  static const gchar text_mod4[] = "<Mod4>";
-  static const gchar text_mod5[] = "<Mod5>";
-  static const gchar text_meta[] = "<Meta>";
-  static const gchar text_super[] = "<Super>";
-  static const gchar text_hyper[] = "<Hyper>";
-  guint l;
-  gchar *keyval_name;
-  gchar *accelerator;
-
-  accelerator_mods &= EGG_VIRTUAL_MODIFIER_MASK;
+  gchar *gtk_name;
+  GdkModifierType gdkmods = 0;
+
+  egg_keymap_resolve_virtual_modifiers (NULL, accelerator_mods, &gdkmods);
+  gtk_name = gtk_accelerator_name (accelerator_key, gdkmods);
 
   if (!accelerator_key)
     {
-      keyval_name = g_strdup_printf ("0x%02x", keycode);
-    }
-  else
-    {
-      keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
-      if (!keyval_name)
-        keyval_name = "";
+	gchar *name;
+	name = g_strdup_printf ("%s0x%02x", gtk_name, keycode);
+	g_free (gtk_name);
+	return name;
     }
 
-  l = 0;
-  if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
-    l += sizeof (text_release) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
-    l += sizeof (text_shift) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
-    l += sizeof (text_control) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
-    l += sizeof (text_mod1) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
-    l += sizeof (text_mod2) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
-    l += sizeof (text_mod3) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
-    l += sizeof (text_mod4) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
-    l += sizeof (text_mod5) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_META_MASK)
-    l += sizeof (text_meta) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
-    l += sizeof (text_hyper) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
-    l += sizeof (text_super) - 1;
-  l += strlen (keyval_name);
-
-  accelerator = g_new (gchar, l + 1);
-
-  l = 0;
-  accelerator[l] = 0;
-  if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
-    {
-      strcpy (accelerator + l, text_release);
-      l += sizeof (text_release) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
-    {
-      strcpy (accelerator + l, text_shift);
-      l += sizeof (text_shift) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
-    {
-      strcpy (accelerator + l, text_control);
-      l += sizeof (text_control) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
-    {
-      strcpy (accelerator + l, text_mod1);
-      l += sizeof (text_mod1) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
-    {
-      strcpy (accelerator + l, text_mod2);
-      l += sizeof (text_mod2) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
-    {
-      strcpy (accelerator + l, text_mod3);
-      l += sizeof (text_mod3) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
-    {
-      strcpy (accelerator + l, text_mod4);
-      l += sizeof (text_mod4) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
-    {
-      strcpy (accelerator + l, text_mod5);
-      l += sizeof (text_mod5) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_META_MASK)
-    {
-      strcpy (accelerator + l, text_meta);
-      l += sizeof (text_meta) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
-    {
-      strcpy (accelerator + l, text_hyper);
-      l += sizeof (text_hyper) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
+  return gtk_name;
+}
+
+/**
+ * egg_virtual_accelerator_label:
+ * @accelerator_key:  accelerator keyval
+ * @accelerator_mods: accelerator modifier mask
+ * @returns:          a newly-allocated accelerator label
+ *
+ * Converts an accelerator keyval and modifier mask
+ * into a (possibly translated) string that can be displayed to
+ * a user.
+ * For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
+ * and you use a German locale, this function returns "Strg+Q".
+ *
+ * The caller of this function must free the returned string.
+ */
+gchar*
+egg_virtual_accelerator_label (guint                  accelerator_key,
+			       guint		      keycode,
+			       EggVirtualModifierType accelerator_mods)
+{
+  gchar *gtk_label;
+  GdkModifierType gdkmods = 0;
+
+  egg_keymap_resolve_virtual_modifiers (NULL, accelerator_mods, &gdkmods);
+  gtk_label = gtk_accelerator_get_label (accelerator_key, gdkmods);
+
+  if (!accelerator_key)
     {
-      strcpy (accelerator + l, text_super);
-      l += sizeof (text_super) - 1;
+	gchar *label;
+	label = g_strdup_printf ("%s0x%02x", gtk_label, keycode);
+	g_free (gtk_label);
+	return label;
     }
-  
-  strcpy (accelerator + l, keyval_name);
 
-  return accelerator;
+  return gtk_label;
 }
 
 void
@@ -518,21 +454,18 @@ egg_keymap_resolve_virtual_modifiers (GdkKeymap              *keymap,
   int i;
   const EggModmap *modmap;
 
-  g_return_if_fail (GDK_IS_KEYMAP (keymap));
   g_return_if_fail (concrete_mods != NULL);
-  
+  g_return_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap));
+
   modmap = egg_keymap_get_modmap (keymap);
-  
+
   /* Not so sure about this algorithm. */
-  
+
   concrete = 0;
-  i = 0;
-  while (i < EGG_MODMAP_ENTRY_LAST)
+  for (i = 0; i < EGG_MODMAP_ENTRY_LAST; ++i)
     {
       if (modmap->mapping[i] & virtual_mods)
-        concrete |= (1 << i);
-
-      ++i;
+        concrete |= MODMAP_ENTRY_TO_MODIFIER (i);
     }
 
   *concrete_mods = concrete;
@@ -546,27 +479,26 @@ egg_keymap_virtualize_modifiers (GdkKeymap              *keymap,
   GdkModifierType virtual;
   int i;
   const EggModmap *modmap;
-  
-  g_return_if_fail (GDK_IS_KEYMAP (keymap));
+
   g_return_if_fail (virtual_mods != NULL);
+  g_return_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap));
 
   modmap = egg_keymap_get_modmap (keymap);
-  
+
   /* Not so sure about this algorithm. */
-  
+
   virtual = 0;
-  i = 0;
-  while (i < EGG_MODMAP_ENTRY_LAST)
+  for (i = 0; i < EGG_MODMAP_ENTRY_LAST; ++i)
     {
-      if ((1 << i) & concrete_mods)
+      if (MODMAP_ENTRY_TO_MODIFIER (i) & concrete_mods)
         {
           EggVirtualModifierType cleaned;
-          
+
           cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
                                            EGG_VIRTUAL_MOD3_MASK |
                                            EGG_VIRTUAL_MOD4_MASK |
                                            EGG_VIRTUAL_MOD5_MASK);
-          
+
           if (cleaned != 0)
             {
               virtual |= cleaned;
@@ -579,10 +511,8 @@ egg_keymap_virtualize_modifiers (GdkKeymap              *keymap,
               virtual |= modmap->mapping[i];
             }
         }
-      
-      ++i;
     }
-  
+
   *virtual_mods = virtual;
 }
 
@@ -590,7 +520,6 @@ static void
 reload_modmap (GdkKeymap *keymap,
                EggModmap *modmap)
 {
-#ifdef GDK_WINDOWING_X11
   XModifierKeymap *xmodmap;
   int map_size;
   int i;
@@ -599,13 +528,12 @@ reload_modmap (GdkKeymap *keymap,
   xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
 
   memset (modmap->mapping, 0, sizeof (modmap->mapping));
-  
-  /* there are 8 modifiers, and the first 3 are shift, shift lock,
-   * and control
+
+  /* there are 8 modifiers in the order shift, shift lock,
+   * control, mod1-5 with up to max_keypermod bindings each
    */
   map_size = 8 * xmodmap->max_keypermod;
-  i = 3 * xmodmap->max_keypermod;
-  while (i < map_size)
+  for (i = 3 * xmodmap->max_keypermod; i < map_size; ++i)
     {
       /* get the key code at this point in the map,
        * see if its keysym is one we're interested in
@@ -616,7 +544,7 @@ reload_modmap (GdkKeymap *keymap,
       int n_entries;
       int j;
       EggVirtualModifierType mask;
-      
+
       keys = NULL;
       keyvals = NULL;
       n_entries = 0;
@@ -624,40 +552,35 @@ reload_modmap (GdkKeymap *keymap,
       gdk_keymap_get_entries_for_keycode (keymap,
                                           keycode,
                                           &keys, &keyvals, &n_entries);
-      
+
       mask = 0;
-      j = 0;
-      while (j < n_entries)
-        {          
-          if (keyvals[j] == GDK_Num_Lock)
+      for (j = 0; j < n_entries; ++j)
+        {
+          if (keyvals[j] == GDK_KEY_Num_Lock)
             mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
-          else if (keyvals[j] == GDK_Scroll_Lock)
+          else if (keyvals[j] == GDK_KEY_Scroll_Lock)
             mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
-          else if (keyvals[j] == GDK_Meta_L ||
-                   keyvals[j] == GDK_Meta_R)
+          else if (keyvals[j] == GDK_KEY_Meta_L ||
+                   keyvals[j] == GDK_KEY_Meta_R)
             mask |= EGG_VIRTUAL_META_MASK;
-          else if (keyvals[j] == GDK_Hyper_L ||
-                   keyvals[j] == GDK_Hyper_R)
+          else if (keyvals[j] == GDK_KEY_Hyper_L ||
+                   keyvals[j] == GDK_KEY_Hyper_R)
             mask |= EGG_VIRTUAL_HYPER_MASK;
-          else if (keyvals[j] == GDK_Super_L ||
-                   keyvals[j] == GDK_Super_R)
+          else if (keyvals[j] == GDK_KEY_Super_L ||
+                   keyvals[j] == GDK_KEY_Super_R)
             mask |= EGG_VIRTUAL_SUPER_MASK;
-          else if (keyvals[j] == GDK_Mode_switch)
+          else if (keyvals[j] == GDK_KEY_Mode_switch)
             mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
-          
-          ++j;
         }
 
       /* Mod1Mask is 1 << 3 for example, i.e. the
        * fourth modifier, i / keyspermod is the modifier
        * index
-       */      
+       */
       modmap->mapping[i/xmodmap->max_keypermod] |= mask;
-      
+
       g_free (keyvals);
-      g_free (keys);      
-      
-      ++i;
+      g_free (keys);
     }
 
   /* Add in the not-really-virtual fixed entries */
@@ -669,11 +592,8 @@ reload_modmap (GdkKeymap *keymap,
   modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
   modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
   modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
-  
+
   XFreeModifiermap (xmodmap);
-#else
-  g_error ("Not yet implemented for Win32: eggaccelerators.c:reload_modmap()");
-#endif
 }
 
 const EggModmap*
@@ -681,12 +601,14 @@ egg_keymap_get_modmap (GdkKeymap *keymap)
 {
   EggModmap *modmap;
 
+  if (keymap == NULL)
+    keymap = gdk_keymap_get_default ();
+
   /* This is all a hack, much simpler when we can just
    * modify GDK directly.
    */
-  
-  modmap = g_object_get_data (G_OBJECT (keymap),
-                              "egg-modmap");
+
+  modmap = g_object_get_data (G_OBJECT (keymap), "egg-modmap");
 
   if (modmap == NULL)
     {
@@ -695,9 +617,9 @@ egg_keymap_get_modmap (GdkKeymap *keymap)
       /* FIXME modify keymap change events with an event filter
        * and force a reload if we get one
        */
-      
+
       reload_modmap (keymap, modmap);
-      
+
       g_object_set_data_full (G_OBJECT (keymap),
                               "egg-modmap",
                               modmap,
@@ -705,6 +627,6 @@ egg_keymap_get_modmap (GdkKeymap *keymap)
     }
 
   g_assert (modmap != NULL);
-  
+
   return modmap;
 }
diff --git a/libegg/treeviewutils/eggaccelerators.h b/libegg/treeviewutils/eggaccelerators.h
index d2276d2..9e22fc8 100644
--- a/libegg/treeviewutils/eggaccelerators.h
+++ b/libegg/treeviewutils/eggaccelerators.h
@@ -21,7 +21,7 @@
 #ifndef __EGG_ACCELERATORS_H__
 #define __EGG_ACCELERATORS_H__
 
-#include <gtk/gtkaccelgroup.h>
+#include <gtk/gtk.h>
 #include <gdk/gdk.h>
 
 G_BEGIN_DECLS
@@ -36,7 +36,7 @@ typedef enum
   EGG_VIRTUAL_CONTROL_MASK  = 1 << 2,
 
   EGG_VIRTUAL_ALT_MASK      = 1 << 3, /* fixed as Mod1 */
-  
+
   EGG_VIRTUAL_MOD2_MASK	    = 1 << 4,
   EGG_VIRTUAL_MOD3_MASK	    = 1 << 5,
   EGG_VIRTUAL_MOD4_MASK	    = 1 << 6,
@@ -50,21 +50,23 @@ typedef enum
   GDK_BUTTON5_MASK  = 1 << 12,
   /* 13, 14 are used by Xkb for the keyboard group */
 #endif
-  
-  EGG_VIRTUAL_META_MASK = 1 << 24,
-  EGG_VIRTUAL_SUPER_MASK = 1 << 25,
-  EGG_VIRTUAL_HYPER_MASK = 1 << 26,
-  EGG_VIRTUAL_MODE_SWITCH_MASK = 1 << 27, 
-  EGG_VIRTUAL_NUM_LOCK_MASK = 1 << 28,
-  EGG_VIRTUAL_SCROLL_LOCK_MASK = 1 << 29,
+
+  EGG_VIRTUAL_MODE_SWITCH_MASK = 1 << 23,
+  EGG_VIRTUAL_NUM_LOCK_MASK = 1 << 24,
+  EGG_VIRTUAL_SCROLL_LOCK_MASK = 1 << 25,
+
+  /* Also in GdkModifierType */
+  EGG_VIRTUAL_SUPER_MASK = 1 << 26,
+  EGG_VIRTUAL_HYPER_MASK = 1 << 27,
+  EGG_VIRTUAL_META_MASK = 1 << 28,
 
   /* Also in GdkModifierType */
   EGG_VIRTUAL_RELEASE_MASK  = 1 << 30,
 
   /*     28-31 24-27 20-23 16-19 12-15 8-11 4-7 0-3
-   *       7     f     0     0     0    0    f   f
-   */  
-  EGG_VIRTUAL_MODIFIER_MASK = 0x7f0000ff
+   *       5     f     8     0     0    0    f   f
+   */
+  EGG_VIRTUAL_MODIFIER_MASK = 0x5f8000ff
 
 } EggVirtualModifierType;
 
@@ -83,6 +85,10 @@ gchar* egg_virtual_accelerator_name (guint                  accelerator_key,
 				     guint		    keycode,
                                      EggVirtualModifierType accelerator_mods);
 
+gchar* egg_virtual_accelerator_label (guint                  accelerator_key,
+				      guint		     keycode,
+				      EggVirtualModifierType accelerator_mods);
+
 G_END_DECLS
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]