]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkaccelgroup.c
Updated Basque translation.
[~andy/gtk] / gtk / gtkaccelgroup.c
index 038d73179ebaf458862336ad03f672d83f0d639b..3bf9415bbbc99bbbb3260209dae56fe943d75b58 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#include <config.h>
+#include "config.h"
 #include <string.h>
 #include <stdlib.h>
 
-#include "gtkalias.h"
 #include "gtkaccelgroup.h"
 #include "gtkaccellabel.h" /* For _gtk_accel_label_class_get_accelerator_label */
 #include "gtkaccelmap.h"
 #include "gtkmain.h"           /* For _gtk_boolean_handled_accumulator */
 #include "gdk/gdkkeysyms.h"
 #include "gtkmarshalers.h"
+#include "gtkalias.h"
 
 
 /* --- 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, "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
+   * @acceleratable: the object on which the accelerator was activated
+   * @keyval: the accelerator keyval
+   * @modifier: the modifier combination of the accelerator
+   *
+   * The accel-activate signal is an implementation detail of
+   * #GtkAccelGroup and not meant to be used by applications.
+   * 
+   * 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,
@@ -108,8 +121,22 @@ gtk_accel_group_class_init (GtkAccelGroupClass *class)
                  G_TYPE_OBJECT,
                  G_TYPE_UINT,
                  GDK_TYPE_MODIFIER_TYPE);
+  /**
+   * GtkAccelGroup::accel-changed:
+   * @accel_group: the #GtkAccelGroup which received the signal
+   * @keyval: the accelerator keyval
+   * @modifier: the modifier combination of the accelerator
+   * @accel_closure: the #GClosure of the accelerator
+   *
+   * The accel-changed signal is emitted when a #GtkAccelGroupEntry
+   * is added to or removed from the accel group. 
+   *
+   * Widgets like #GtkAccelLabel which display an associated 
+   * accelerator should connect to this signal, and rebuild 
+   * 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),
@@ -133,15 +160,38 @@ 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);
     }
 
   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
@@ -166,6 +216,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)
@@ -304,6 +393,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");
+  }
 }
 
 /**
@@ -319,6 +413,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
@@ -397,10 +496,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;
@@ -525,6 +624,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,
@@ -577,7 +680,7 @@ gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
     if (accel_group->priv_accels[i].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;
       }
@@ -723,6 +826,22 @@ 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
+ * @returns:       %TRUE if the accelerator was handled, %FALSE otherwise
+ * 
+ * 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,
@@ -753,8 +872,8 @@ gtk_accel_group_activate (GtkAccelGroup   *accel_group,
  * 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,
@@ -936,6 +1055,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
@@ -1027,6 +1181,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;
@@ -1081,6 +1253,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;
@@ -1109,6 +1284,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);
 
@@ -1154,24 +1335,26 @@ 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;
 }
 
-/* Underscores in key names are better displayed as spaces
- * E.g., Page_Up should be "Page Up"
- */
-static void
-substitute_underscores (char *str)
-{
-  char *p;
-
-  for (p = str; *p; p++)
-    if (*p == '_')
-      *p = ' ';
-}
-
 /**
  * gtk_accelerator_get_label:
  * @accelerator_key:  accelerator keyval
@@ -1206,10 +1389,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.
@@ -1232,3 +1417,6 @@ gtk_accelerator_get_default_mod_mask (void)
 {
   return default_accel_mod_mask;
 }
+
+#define __GTK_ACCEL_GROUP_C__
+#include "gtkaliasdef.c"