]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkaccelgroup.c
GtkToolPalette: Change gtk_tool_palette_get_drop_group() return.
[~andy/gtk] / gtk / gtkaccelgroup.c
index 4ca34fd8336c5e30e66e69993759e13fabc21db6..4d760b58802d0ecebccca930e3a062bf58c8c128 100644 (file)
@@ -24,7 +24,7 @@
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#include <config.h>
+#include "config.h"
 #include <string.h>
 #include <stdlib.h>
 
 #include "gtkmarshalers.h"
 #include "gtkalias.h"
 
+/**
+ * SECTION:gtkaccelgroup
+ * @Short_description: Groups of global keyboard accelerators for an entire GtkWindow
+ * @Title: Accelerator Groups
+ * @See_also:gtk_window_add_accel_group(), gtk_accel_map_change_entry(),
+ * gtk_item_factory_new(), gtk_label_new_with_mnemonic()
+ * 
+ * A #GtkAccelGroup represents a group of keyboard accelerators,
+ * typically attached to a toplevel #GtkWindow (with
+ * gtk_window_add_accel_group()). Usually you won't need to create a
+ * #GtkAccelGroup directly; instead, when using #GtkItemFactory, GTK+
+ * automatically sets up the accelerators for your menus in the item
+ * factory's #GtkAccelGroup.
+ * 
+ * 
+ * Note that <firstterm>accelerators</firstterm> are different from
+ * <firstterm>mnemonics</firstterm>. Accelerators are shortcuts for
+ * activating a menu item; they appear alongside the menu item they're a
+ * shortcut for. For example "Ctrl+Q" might appear alongside the "Quit"
+ * menu item. Mnemonics are shortcuts for GUI elements such as text
+ * entries or buttons; they appear as underlined characters. See
+ * gtk_label_new_with_mnemonic(). Menu items can have both accelerators
+ * and mnemonics, of course.
+ */
+
 
 /* --- prototypes --- */
-static void gtk_accel_group_class_init (GtkAccelGroupClass     *class);
-static void gtk_accel_group_init       (GtkAccelGroup          *accel_group);
-static void gtk_accel_group_finalize   (GObject                *object);
+static void gtk_accel_group_finalize     (GObject    *object);
+static void gtk_accel_group_get_property (GObject    *object,
+                                          guint       param_id,
+                                          GValue     *value,
+                                          GParamSpec *pspec);
+static void accel_closure_invalidate     (gpointer    data,
+                                          GClosure   *closure);
 
 
 /* --- variables --- */
-static GObjectClass     *parent_class = NULL;
-static guint            signal_accel_activate = 0;
-static guint            signal_accel_changed = 0;
-static guint            quark_acceleratable_groups = 0;
-static guint            default_accel_mod_mask = (GDK_SHIFT_MASK |
-                                                  GDK_CONTROL_MASK |
-                                                  GDK_MOD1_MASK);
-
+static guint  signal_accel_activate      = 0;
+static guint  signal_accel_changed       = 0;
+static guint  quark_acceleratable_groups = 0;
+static guint  default_accel_mod_mask     = (GDK_SHIFT_MASK   |
+                                            GDK_CONTROL_MASK |
+                                            GDK_MOD1_MASK    |
+                                            GDK_SUPER_MASK   |
+                                            GDK_HYPER_MASK   |
+                                            GDK_META_MASK);
 
-/* --- functions --- */
-/**
- * gtk_accel_group_get_type:
- * @returns: the type ID for accelerator groups.
- */
-GType
-gtk_accel_group_get_type (void)
-{
-  static GType object_type = 0;
 
-  if (!object_type)
-    {
-      static const GTypeInfo object_info = {
-       sizeof (GtkAccelGroupClass),
-       (GBaseInitFunc) NULL,
-       (GBaseFinalizeFunc) NULL,
-       (GClassInitFunc) gtk_accel_group_class_init,
-       NULL,   /* class_finalize */
-       NULL,   /* class_data */
-       sizeof (GtkAccelGroup),
-       0,      /* n_preallocs */
-       (GInstanceInitFunc) gtk_accel_group_init,
-      };
-
-      object_type = g_type_register_static (G_TYPE_OBJECT, g_intern_static_string ("GtkAccelGroup"),
-                                           &object_info, 0);
-    }
+enum {
+  PROP_0,
+  PROP_IS_LOCKED,
+  PROP_MODIFIER_MASK,
+};
 
-  return object_type;
-}
+G_DEFINE_TYPE (GtkAccelGroup, gtk_accel_group, G_TYPE_OBJECT)
 
+/* --- functions --- */
 static void
 gtk_accel_group_class_init (GtkAccelGroupClass *class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
 
-  parent_class = g_type_class_peek_parent (class);
-
   quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
 
   object_class->finalize = gtk_accel_group_finalize;
+  object_class->get_property = gtk_accel_group_get_property;
 
   class->accel_changed = NULL;
 
+  g_object_class_install_property (object_class,
+                                   PROP_IS_LOCKED,
+                                   g_param_spec_boolean ("is-locked",
+                                                         "Is locked",
+                                                         "Is the accel group locked",
+                                                         FALSE,
+                                                         G_PARAM_READABLE));
+
+  g_object_class_install_property (object_class,
+                                   PROP_MODIFIER_MASK,
+                                   g_param_spec_flags ("modifier-mask",
+                                                       "Modifier Mask",
+                                                       "Modifier Mask",
+                                                       GDK_TYPE_MODIFIER_TYPE,
+                                                       default_accel_mod_mask,
+                                                       G_PARAM_READABLE));
+
   /**
    * GtkAccelGroup::accel-activate:
    * @accel_group: the #GtkAccelGroup which received the signal
@@ -111,7 +136,7 @@ gtk_accel_group_class_init (GtkAccelGroupClass *class)
    * Returns: %TRUE if the accelerator was activated
    */
   signal_accel_activate =
-    g_signal_new ("accel_activate",
+    g_signal_new (I_("accel-activate"),
                  G_OBJECT_CLASS_TYPE (class),
                  G_SIGNAL_DETAILED,
                  0,
@@ -136,7 +161,7 @@ gtk_accel_group_class_init (GtkAccelGroupClass *class)
    * their visual representation if the @accel_closure is theirs.
    */
   signal_accel_changed =
-    g_signal_new ("accel_changed",
+    g_signal_new (I_("accel-changed"),
                  G_OBJECT_CLASS_TYPE (class),
                  G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
                  G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
@@ -160,15 +185,41 @@ gtk_accel_group_finalize (GObject *object)
 
       if (entry->accel_path_quark)
        {
-         const gchar *accel_path = g_quark_to_string (entry[i].accel_path_quark);
+         const gchar *accel_path = g_quark_to_string (entry->accel_path_quark);
 
          _gtk_accel_map_remove_group (accel_path, accel_group);
        }
+      g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
+
+      /* remove quick_accel_add() refcount */
+      g_closure_unref (entry->closure);
     }
 
   g_free (accel_group->priv_accels);
 
-  G_OBJECT_CLASS (parent_class)->finalize (object);
+  G_OBJECT_CLASS (gtk_accel_group_parent_class)->finalize (object);
+}
+
+static void
+gtk_accel_group_get_property (GObject    *object,
+                              guint       param_id,
+                              GValue     *value,
+                              GParamSpec *pspec)
+{
+  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
+
+  switch (param_id)
+    {
+    case PROP_IS_LOCKED:
+      g_value_set_boolean (value, accel_group->lock_count > 0);
+      break;
+    case PROP_MODIFIER_MASK:
+      g_value_set_flags (value, accel_group->modifier_mask);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+      break;
+    }
 }
 
 static void
@@ -193,6 +244,45 @@ gtk_accel_group_new (void)
   return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
 }
 
+/**
+ * gtk_accel_group_get_is_locked:
+ * @accel_group: a #GtkAccelGroup
+ *
+ * Locks are added and removed using gtk_accel_group_lock() and
+ * gtk_accel_group_unlock().
+ *
+ * Returns: %TRUE if there are 1 or more locks on the @accel_group,
+ * %FALSE otherwise.
+ *
+ * Since: 2.14
+ */
+gboolean
+gtk_accel_group_get_is_locked (GtkAccelGroup *accel_group)
+{
+  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
+
+  return accel_group->lock_count > 0;
+}
+
+/**
+ * gtk_accel_group_get_modifier_mask:
+ * @accel_group: a #GtkAccelGroup
+ *
+ * Gets a #GdkModifierType representing the mask for this
+ * @accel_group. For example, #GDK_CONTROL_MASK, #GDK_SHIFT_MASK, etc.
+ *
+ * Returns: the modifier mask for this accel group.
+ *
+ * Since: 2.14
+ */
+GdkModifierType
+gtk_accel_group_get_modifier_mask (GtkAccelGroup *accel_group)
+{
+  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), 0);
+
+  return accel_group->modifier_mask;
+}
+
 static void
 accel_group_weak_ref_detach (GSList  *free_list,
                             GObject *stale_object)
@@ -261,10 +351,11 @@ _gtk_accel_group_detach (GtkAccelGroup *accel_group,
 
 /**
  * gtk_accel_groups_from_object:
- * @object:        a #GObject, usually a #GtkWindow 
- * @returns: a list of all accel groups which are attached to @object
+ * @object:        a #GObject, usually a #GtkWindow
  *
  * Gets a list of all accel groups which are attached to @object.
+ *
+ * Returns: (element-type GtkAccelGroup) (transfer none): a list of all accel groups which are attached to @object
  */
 GSList*
 gtk_accel_groups_from_object (GObject *object)
@@ -331,6 +422,11 @@ gtk_accel_group_lock (GtkAccelGroup *accel_group)
   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
   
   accel_group->lock_count += 1;
+
+  if (accel_group->lock_count == 1) {
+    /* State change from unlocked to locked */
+    g_object_notify (G_OBJECT (accel_group), "is-locked");
+  }
 }
 
 /**
@@ -346,6 +442,11 @@ gtk_accel_group_unlock (GtkAccelGroup *accel_group)
   g_return_if_fail (accel_group->lock_count > 0);
 
   accel_group->lock_count -= 1;
+
+  if (accel_group->lock_count < 1) {
+    /* State change from locked to unlocked */
+    g_object_notify (G_OBJECT (accel_group), "is-locked");
+  }
 }
 
 static void
@@ -424,10 +525,10 @@ quick_accel_add (GtkAccelGroup  *accel_group,
 
 static void
 quick_accel_remove (GtkAccelGroup      *accel_group,
-                   GtkAccelGroupEntry *entry)
+                    guint               pos)
 {
-  guint pos = entry - accel_group->priv_accels;
   GQuark accel_quark = 0;
+  GtkAccelGroupEntry *entry = accel_group->priv_accels + pos;
   guint accel_key = entry->key.accel_key;
   GdkModifierType accel_mods = entry->key.accel_mods;
   GClosure *closure = entry->closure;
@@ -552,6 +653,10 @@ gtk_accel_group_connect (GtkAccelGroup     *accel_group,
  * for the path.
  *
  * The signature used for the @closure is that of #GtkAccelGroupActivate.
+ * 
+ * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
+ * pass a static string, you can save some memory by interning it first with 
+ * g_intern_static_string().
  */
 void
 gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
@@ -586,11 +691,14 @@ gtk_accel_group_connect_by_path (GtkAccelGroup    *accel_group,
 /**
  * gtk_accel_group_disconnect:
  * @accel_group: the accelerator group to remove an accelerator from
- * @closure:     the closure to remove from this accelerator group
+ * @closure:     the closure to remove from this accelerator group, or %NULL
+ *               to remove all closures
  * @returns:     %TRUE if the closure was found and got disconnected
  *
  * Removes an accelerator previously installed through
  * gtk_accel_group_connect().
+ *
+ * Since 2.20 @closure can be %NULL.
  */
 gboolean
 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
@@ -601,10 +709,10 @@ gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
 
   for (i = 0; i < accel_group->n_accels; i++)
-    if (accel_group->priv_accels[i].closure == closure)
+    if (accel_group->priv_accels[i].closure == closure || !closure)
       {
        g_object_ref (accel_group);
-       quick_accel_remove (accel_group, accel_group->priv_accels + i);
+       quick_accel_remove (accel_group, i);
        g_object_unref (accel_group);
        return TRUE;
       }
@@ -750,6 +858,21 @@ gtk_accel_group_from_accel_closure (GClosure *closure)
   return NULL;
 }
 
+/**
+ * gtk_accel_group_activate:
+ * @accel_group:   a #GtkAccelGroup
+ * @accel_quark:   the quark for the accelerator name
+ * @acceleratable: the #GObject, usually a #GtkWindow, on which
+ *                 to activate the accelerator.
+ * @accel_key:     accelerator keyval from a key event
+ * @accel_mods:    keyboard state mask from a key event
+ * 
+ * Finds the first accelerator in @accel_group 
+ * that matches @accel_key and @accel_mods, and
+ * activates it.
+ *
+ * Returns: %TRUE if an accelerator was activated and handled this keypress
+ */
 gboolean
 gtk_accel_group_activate (GtkAccelGroup   *accel_group,
                           GQuark          accel_quark,
@@ -775,13 +898,12 @@ gtk_accel_group_activate (GtkAccelGroup   *accel_group,
  *                 to activate the accelerator.
  * @accel_key:     accelerator keyval from a key event
  * @accel_mods:    keyboard state mask from a key event
- * @returns:       %TRUE if the accelerator was handled, %FALSE otherwise
  * 
  * Finds the first accelerator in any #GtkAccelGroup attached
  * to @object that matches @accel_key and @accel_mods, and
  * activates that accelerator.
- * If an accelerator was activated and handled this keypress, %TRUE
- * is returned.
+ *
+ * Returns: %TRUE if an accelerator was activated and handled this keypress
  */
 gboolean
 gtk_accel_groups_activate (GObject       *object,
@@ -963,6 +1085,41 @@ is_release (const gchar *string)
          (string[8] == '>'));
 }
 
+static inline gboolean
+is_meta (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'm' || string[1] == 'M') &&
+         (string[2] == 'e' || string[2] == 'E') &&
+         (string[3] == 't' || string[3] == 'T') &&
+         (string[4] == 'a' || string[4] == 'A') &&
+         (string[5] == '>'));
+}
+
+static inline gboolean
+is_super (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 's' || string[1] == 'S') &&
+         (string[2] == 'u' || string[2] == 'U') &&
+         (string[3] == 'p' || string[3] == 'P') &&
+         (string[4] == 'e' || string[4] == 'E') &&
+         (string[5] == 'r' || string[5] == 'R') &&
+         (string[6] == '>'));
+}
+
+static inline gboolean
+is_hyper (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'h' || string[1] == 'H') &&
+         (string[2] == 'y' || string[2] == 'Y') &&
+         (string[3] == 'p' || string[3] == 'P') &&
+         (string[4] == 'e' || string[4] == 'E') &&
+         (string[5] == 'r' || string[5] == 'R') &&
+         (string[6] == '>'));
+}
+
 /**
  * gtk_accelerator_parse:
  * @accelerator:      string representing an accelerator
@@ -1054,6 +1211,24 @@ gtk_accelerator_parse (const gchar     *accelerator,
              len -= 5;
              mods |= GDK_MOD1_MASK;
            }
+          else if (len >= 6 && is_meta (accelerator))
+           {
+             accelerator += 6;
+             len -= 6;
+             mods |= GDK_META_MASK;
+           }
+          else if (len >= 7 && is_hyper (accelerator))
+           {
+             accelerator += 7;
+             len -= 7;
+             mods |= GDK_HYPER_MASK;
+           }
+          else if (len >= 7 && is_super (accelerator))
+           {
+             accelerator += 7;
+             len -= 7;
+             mods |= GDK_SUPER_MASK;
+           }
          else
            {
              gchar last_ch;
@@ -1108,6 +1283,9 @@ gtk_accelerator_name (guint           accelerator_key,
   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;
@@ -1136,6 +1314,12 @@ gtk_accelerator_name (guint           accelerator_key,
   if (accelerator_mods & GDK_MOD5_MASK)
     l += sizeof (text_mod5) - 1;
   l += strlen (keyval_name);
+  if (accelerator_mods & GDK_META_MASK)
+    l += sizeof (text_meta) - 1;
+  if (accelerator_mods & GDK_HYPER_MASK)
+    l += sizeof (text_hyper) - 1;
+  if (accelerator_mods & GDK_SUPER_MASK)
+    l += sizeof (text_super) - 1;
 
   accelerator = g_new (gchar, l + 1);
 
@@ -1181,6 +1365,21 @@ gtk_accelerator_name (guint           accelerator_key,
       strcpy (accelerator + l, text_mod5);
       l += sizeof (text_mod5) - 1;
     }
+  if (accelerator_mods & GDK_META_MASK)
+    {
+      strcpy (accelerator + l, text_meta);
+      l += sizeof (text_meta) - 1;
+    }
+  if (accelerator_mods & GDK_HYPER_MASK)
+    {
+      strcpy (accelerator + l, text_hyper);
+      l += sizeof (text_hyper) - 1;
+    }
+  if (accelerator_mods & GDK_SUPER_MASK)
+    {
+      strcpy (accelerator + l, text_super);
+      l += sizeof (text_super) - 1;
+    }
   strcpy (accelerator + l, keyval_name);
 
   return accelerator;
@@ -1220,10 +1419,12 @@ gtk_accelerator_get_label (guint           accelerator_key,
  *
  * Sets the modifiers that will be considered significant for keyboard
  * accelerators. The default mod mask is #GDK_CONTROL_MASK |
- * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
- * Other modifiers will by default be ignored by #GtkAccelGroup.
- * You must include at least the three default modifiers in any
- * value you pass to this function.
+ * #GDK_SHIFT_MASK | #GDK_MOD1_MASK | #GDK_SUPER_MASK | 
+ * #GDK_HYPER_MASK | #GDK_META_MASK, that is, Control, Shift, Alt, 
+ * Super, Hyper and Meta. Other modifiers will by default be ignored 
+ * by #GtkAccelGroup.
+ * You must include at least the three modifiers Control, Shift
+ * and Alt in any value you pass to this function.
  *
  * The default mod mask should be changed on application startup,
  * before using any accelerator groups.