[gtk/wip/ebassi/shortcut: 183/267] shortcut: Add GtkShortcutTrigger



commit 0d8cc1854e2d849fea069a21c2124b9bbdfbdd33
Author: Benjamin Otte <otte redhat com>
Date:   Sun Aug 5 03:39:04 2018 +0200

    shortcut: Add GtkShortcutTrigger
    
    Triggers are meant to describe how to trigger a shortcut. So far only a
    keyval + modifiers trigger exists.

 docs/reference/gtk/gtk4-sections.txt |  21 ++
 gtk/gtk.h                            |   1 +
 gtk/gtkshortcut.c                    | 116 ++++++----
 gtk/gtkshortcut.h                    |   8 +-
 gtk/gtkshortcuttrigger.c             | 407 +++++++++++++++++++++++++++++++++++
 gtk/gtkshortcuttrigger.h             |  82 +++++++
 gtk/gtktypes.h                       |   1 +
 gtk/gtkwidget.c                      |   7 +-
 gtk/meson.build                      |   2 +
 testsuite/gtk/defaultvalue.c         |   4 +
 10 files changed, 599 insertions(+), 50 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index ab6204c424..448b95f69d 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -6391,6 +6391,25 @@ GTK_EVENT_CONTROLLER_MOTION_GET_CLASS
 gtk_event_controller_motion_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtkshortcuttrigger</FILE>
+<TITLE>GtkShortcutTrigger</TITLE>
+GtkShortcutTrigger
+gtk_shortcut_trigger_ref
+gtk_shortcut_trigger_unref
+GtkShortcutTriggerType
+gtk_shortcut_trigger_get_trigger_type
+gtk_shortcut_trigger_trigger
+
+<SUBSECTION>
+gtk_keyval_trigger_new
+gtk_keyval_trigger_get_modifiers
+gtk_keyval_trigger_get_keyval
+
+<SUBSECTION Private>
+gtk_shortcut_trigger_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtkshortcut</FILE>
 <TITLE>GtkShortcut</TITLE>
@@ -6398,6 +6417,8 @@ GtkShortcut
 gtk_shortcut_new
 gtk_shortcut_set_keyval
 gtk_shortcut_activate
+gtk_shortcut_get_trigger
+gtk_shortcut_set_trigger
 gtk_shortcut_get_arguments
 gtk_shortcut_set_arguments
 gtk_shortcut_get_signal
diff --git a/gtk/gtk.h b/gtk/gtk.h
index a99f49ecfc..192fd186d3 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -206,6 +206,7 @@
 #include <gtk/gtkshortcutssection.h>
 #include <gtk/gtkshortcutsshortcut.h>
 #include <gtk/gtkshortcutswindow.h>
+#include <gtk/gtkshortcuttrigger.h>
 #include <gtk/gtkshow.h>
 #include <gtk/gtksingleselection.h>
 #include <gtk/gtkslicelistmodel.h>
diff --git a/gtk/gtkshortcut.c b/gtk/gtkshortcut.c
index 59187c76b7..90df956e1f 100644
--- a/gtk/gtkshortcut.c
+++ b/gtk/gtkshortcut.c
@@ -23,6 +23,7 @@
 
 #include "gtkbindingsprivate.h"
 #include "gtkintl.h"
+#include "gtkshortcuttrigger.h"
 #include "gtkwidget.h"
 
 /**
@@ -53,9 +54,7 @@ struct _GtkShortcut
 {
   GObject parent_instance;
 
-  GdkModifierType mods;
-  guint keyval;
-
+  GtkShortcutTrigger *trigger;
   char *signal;
   GVariant *args;
 };
@@ -65,6 +64,7 @@ enum
   PROP_0,
   PROP_ARGUMENTS,
   PROP_SIGNAL,
+  PROP_TRIGGER,
 
   N_PROPS
 };
@@ -78,6 +78,7 @@ gtk_shortcut_dispose (GObject *object)
 {
   GtkShortcut *self = GTK_SHORTCUT (object);
 
+  g_clear_pointer (&self->trigger, gtk_shortcut_trigger_unref);
   g_clear_pointer (&self->signal, g_free);
   g_clear_pointer (&self->args, g_variant_unref);
 
@@ -102,6 +103,10 @@ gtk_shortcut_get_property (GObject    *object,
       g_value_set_string (value, self->signal);
       break;
 
+    case PROP_TRIGGER:
+      g_value_set_boxed (value, self->trigger);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -126,6 +131,10 @@ gtk_shortcut_set_property (GObject      *object,
       gtk_shortcut_set_signal (self, g_value_get_string (value));
       break;
 
+    case PROP_TRIGGER:
+      gtk_shortcut_set_trigger (self, g_value_dup_boxed (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -166,12 +175,25 @@ gtk_shortcut_class_init (GtkShortcutClass *klass)
                          NULL,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
+  /**
+   * GtkShortcut:trigger:
+   *
+   * The trigger that triggers this shortcut.
+   */
+  properties[PROP_TRIGGER] =
+    g_param_spec_boxed ("trigger",
+                        P_("Trigger"),
+                        P_("The trigger for this shortcut"),
+                        GTK_TYPE_SHORTCUT_TRIGGER,
+                        G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
   g_object_class_install_properties (gobject_class, N_PROPS, properties);
 }
 
 static void
 gtk_shortcut_init (GtkShortcut *self)
 {
+  self->trigger = gtk_shortcut_trigger_ref (gtk_never_trigger_get ());
 }
 
 /**
@@ -187,50 +209,11 @@ gtk_shortcut_new (void)
   return g_object_new (GTK_TYPE_SHORTCUT, NULL);
 }
 
-void
-gtk_shortcut_set_keyval (GtkShortcut     *self,
-                         guint            keyval,
-                         GdkModifierType  mods)
-{
-  g_return_if_fail (GTK_IS_SHORTCUT (self));
-
-  /* To deal with <Shift>, we need to uppercase
-   * or lowercase depending on situation.
-   */
-  if (mods & GDK_SHIFT_MASK)
-    {
-      if (keyval == GDK_KEY_Tab)
-        keyval = GDK_KEY_ISO_Left_Tab;
-      else
-        keyval = gdk_keyval_to_upper (keyval);
-    }
-  else
-    {
-      if (keyval == GDK_KEY_ISO_Left_Tab)
-        keyval = GDK_KEY_Tab;
-      else
-        keyval = gdk_keyval_to_lower (keyval);
-    }
-
-  self->keyval = keyval;
-  self->mods = mods;
-}
-
 gboolean
 gtk_shortcut_trigger (GtkShortcut    *self,
                       const GdkEvent *event)
 {
-  GdkModifierType mods;
-  guint keyval;
-
-  if (gdk_event_get_event_type (event) != GDK_KEY_PRESS)
-    return FALSE;
-
-  /* XXX: This needs to deal with groups */
-  gdk_event_get_state (event, &mods);
-  gdk_event_get_keyval (event, &keyval);
-
-  return keyval == self->keyval && mods == self->mods;
+  return gtk_shortcut_trigger_trigger (self->trigger, event);
 }
 
 gboolean
@@ -251,7 +234,7 @@ gtk_shortcut_activate (GtkShortcut *self,
                                     &handled,
                                     &error))
         {
-          char *accelerator = gtk_accelerator_name (self->keyval, self->mods);
+          char *accelerator = gtk_shortcut_trigger_to_string (self->trigger);
           g_warning ("gtk_shortcut_activate(): \":%s\": %s",
                      accelerator,
                      error->message);
@@ -268,6 +251,51 @@ gtk_shortcut_activate (GtkShortcut *self,
     }
 }
 
+/**
+ * gtk_shortcut_get_trigger:
+ * @self: a #GtkShortcut
+ *
+ * Gets the trigger used to trigger @self.
+ *
+ * Returns: (transfer none): the trigger used
+ **/
+GtkShortcutTrigger *
+gtk_shortcut_get_trigger (GtkShortcut *self)
+{
+  g_return_val_if_fail (GTK_IS_SHORTCUT (self), NULL);
+
+  return self->trigger;
+}
+
+/**
+ * gtk_shortcut_set_trigger:
+ * @self: a #GtkShortcut
+ * @trigger: (transfer full) (nullable): The new trigger.
+ *     If the @trigger is %NULL, the never trigger will be used.
+ *
+ * Sets the new trigger for @self to be @trigger.
+ **/
+void
+gtk_shortcut_set_trigger (GtkShortcut *self,
+                          GtkShortcutTrigger *trigger)
+{
+  g_return_if_fail (GTK_IS_SHORTCUT (self));
+
+  if (trigger == NULL)
+    trigger = gtk_shortcut_trigger_ref (gtk_never_trigger_get ());
+
+  if (self->trigger == trigger)
+    {
+      gtk_shortcut_trigger_unref (trigger);
+      return;
+    }
+  
+  gtk_shortcut_trigger_unref (self->trigger);
+  self->trigger = trigger;
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TRIGGER]);
+}
+
 GVariant *
 gtk_shortcut_get_arguments (GtkShortcut *self)
 {
diff --git a/gtk/gtkshortcut.h b/gtk/gtkshortcut.h
index a4f2bf1a4b..5cc18c6738 100644
--- a/gtk/gtkshortcut.h
+++ b/gtk/gtkshortcut.h
@@ -33,9 +33,11 @@ GDK_AVAILABLE_IN_ALL
 GtkShortcut *   gtk_shortcut_new                                (void);
 
 GDK_AVAILABLE_IN_ALL
-void            gtk_shortcut_set_keyval                         (GtkShortcut            *self,
-                                                                 guint                   keyval,
-                                                                 GdkModifierType         mods);
+void            gtk_shortcut_set_trigger                        (GtkShortcut            *self,
+                                                                 GtkShortcutTrigger     *trigger);
+GDK_AVAILABLE_IN_ALL
+GtkShortcutTrigger *
+                gtk_shortcut_get_trigger                        (GtkShortcut            *self);
 
 GDK_AVAILABLE_IN_ALL
 gboolean        gtk_shortcut_trigger                            (GtkShortcut            *self,
diff --git a/gtk/gtkshortcuttrigger.c b/gtk/gtkshortcuttrigger.c
new file mode 100644
index 0000000000..6bd64268bc
--- /dev/null
+++ b/gtk/gtkshortcuttrigger.c
@@ -0,0 +1,407 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+/**
+ * SECTION:GtkShortcutTrigger
+ * @Title: GtkShortcutTrigger
+ * @Short_description: Triggers to track if shortcuts should be activated
+ * @See_also: #GtkShortcut
+ *
+ * #GtkShortcutTrigger is the object used to track if a #GtkShortcut should be
+ * activated. For this purpose, gtk_shortcut_trigger_trigger() can be called
+ * on a #GdkEvent.
+ *
+ * #GtkShortcutTriggers contain functions that allow easy presentation to end
+ * users as well as being printed for debugging.
+ *
+ * All #GtkShortcutTriggers are immutable, you can only specify their properties
+ * during construction. If you want to change a trigger, you have to replace it
+ * with a new one.
+ */
+
+#include "config.h"
+
+#include "gtkshortcuttrigger.h"
+
+#include "gtkaccelgroup.h"
+
+typedef struct _GtkShortcutTriggerClass GtkShortcutTriggerClass;
+
+#define GTK_IS_SHORTCUT_TRIGGER_TYPE(trigger,type) (GTK_IS_SHORTCUT_TRIGGER (trigger) && 
(trigger)->trigger_class->trigger_type == (type))
+
+struct _GtkShortcutTrigger
+{
+  const GtkShortcutTriggerClass *trigger_class;
+
+  volatile int ref_count;
+};
+
+struct _GtkShortcutTriggerClass
+{
+  GtkShortcutTriggerType trigger_type;
+  gsize struct_size;
+  const char *type_name;
+
+  void            (* finalize)    (GtkShortcutTrigger  *trigger);
+  gboolean        (* trigger)     (GtkShortcutTrigger  *trigger,
+                                   const GdkEvent      *event);
+  void            (* print)       (GtkShortcutTrigger  *trigger,
+                                   GString             *string);
+};
+
+static GtkShortcutTrigger *     gtk_shortcut_trigger_new                (const GtkShortcutTriggerClass  
*trigger_class,
+                                                                         gsize                           
extra_size);
+
+
+G_DEFINE_BOXED_TYPE (GtkShortcutTrigger, gtk_shortcut_trigger,
+                     gtk_shortcut_trigger_ref,
+                     gtk_shortcut_trigger_unref)
+
+static void
+gtk_shortcut_trigger_finalize (GtkShortcutTrigger *self)
+{
+  self->trigger_class->finalize (self);
+
+  g_free (self);
+}
+
+/*< private >
+ * gtk_shortcut_trigger_new:
+ * @trigger_class: class structure for this trigger
+ *
+ * Returns: (transfer full): the newly created #GtkShortcutTrigger
+ */
+GtkShortcutTrigger *
+gtk_shortcut_trigger_new (const GtkShortcutTriggerClass *trigger_class,
+                          gsize                          extra_size)
+{
+  GtkShortcutTrigger *self;
+
+  g_return_val_if_fail (trigger_class != NULL, NULL);
+
+  self = g_malloc0 (trigger_class->struct_size + extra_size);
+
+  self->trigger_class = trigger_class;
+
+  self->ref_count = 1;
+
+  return self;
+}
+
+/**
+ * gtk_shortcut_trigger_ref:
+ * @trigger: a #GtkShortcutTrigger
+ *
+ * Acquires a reference on the given #GtkShortcutTrigger.
+ *
+ * Returns: (transfer full): the #GtkShortcutTrigger with an additional reference
+ */
+GtkShortcutTrigger *
+gtk_shortcut_trigger_ref (GtkShortcutTrigger *trigger)
+{
+  g_return_val_if_fail (GTK_IS_SHORTCUT_TRIGGER (trigger), NULL);
+
+  g_atomic_int_inc (&trigger->ref_count);
+
+  return trigger;
+}
+
+/**
+ * gtk_shortcut_trigger_unref:
+ * @trigger: (transfer full): a #GtkShortcutTrigger
+ *
+ * Releases a reference on the given #GtkShortcutTrigger.
+ *
+ * If the reference was the last, the resources associated to the @trigger are
+ * freed.
+ */
+void
+gtk_shortcut_trigger_unref (GtkShortcutTrigger *trigger)
+{
+  g_return_if_fail (GTK_IS_SHORTCUT_TRIGGER (trigger));
+
+  if (g_atomic_int_dec_and_test (&trigger->ref_count))
+    gtk_shortcut_trigger_finalize (trigger);
+}
+
+/**
+ * gtk_shortcut_trigger_get_trigger_type:
+ * @self: a #GtkShortcutTrigger
+ *
+ * Returns the type of the @trigger.
+ *
+ * Returns: the type of the #GtkShortcutTrigger
+ */
+GtkShortcutTriggerType
+gtk_shortcut_trigger_get_trigger_type (GtkShortcutTrigger *self)
+{
+  g_return_val_if_fail (GTK_IS_SHORTCUT_TRIGGER (self), GTK_SHORTCUT_TRIGGER_NEVER);
+
+  return self->trigger_class->trigger_type;
+}
+
+/**
+ * gtk_shortcut_trigger_trigger:
+ * @self: a #GtkShortcutTrigger
+ * @event: the event to check
+ *
+ * Checks if the given @event triggers @self. If so, returns %TRUE.
+ *
+ * Returns: %TRUE if this event triggered the trigger
+ **/
+gboolean
+gtk_shortcut_trigger_trigger (GtkShortcutTrigger *self,
+                              const GdkEvent     *event)
+{
+  g_return_val_if_fail (GTK_IS_SHORTCUT_TRIGGER (self), FALSE);
+  g_return_val_if_fail (GDK_IS_EVENT (event), FALSE);
+
+  return self->trigger_class->trigger (self, event);
+}
+
+/**
+ * gtk_shortcut_trigger_to_string:
+ * @self: a #GtkShortcutTrigger
+ *
+ * Prints the given trigger into a human-readable string.
+ * This is a small wrapper around gdk_content_formats_print() to help
+ * when debugging.
+ *
+ * Returns: (transfer full): a new string
+ **/
+char *
+gtk_shortcut_trigger_to_string (GtkShortcutTrigger *self)
+{
+  GString *string;
+
+  g_return_val_if_fail (self != NULL, NULL);
+
+  string = g_string_new (NULL);
+  gtk_shortcut_trigger_print (self, string);
+
+  return g_string_free (string, FALSE);
+}
+
+/**
+ * gtk_shortcut_trigger_print:
+ * @self: a #GtkShortcutTrigger
+ * @string: a #GString to print into
+ *
+ * Prints the given trigger into a string for the developer.
+ * This is meant for debugging and logging.
+ *
+ * The form of the representation may change at any time and is
+ * not guaranteed to stay identical.
+ **/
+void
+gtk_shortcut_trigger_print (GtkShortcutTrigger *self,
+                            GString            *string)
+{
+  g_return_if_fail (GTK_IS_SHORTCUT_TRIGGER (self));
+  g_return_if_fail (string != NULL);
+
+  return self->trigger_class->print (self, string);
+}
+
+/*** GTK_SHORTCUT_TRIGGER_NEVER ***/
+
+typedef struct _GtkNeverTrigger GtkNeverTrigger;
+
+struct _GtkNeverTrigger
+{
+  GtkShortcutTrigger trigger;
+
+  guint never;
+  GdkModifierType modifiers;
+};
+
+static void
+gsk_never_trigger_finalize (GtkShortcutTrigger *trigger)
+{
+  g_assert_not_reached ();
+}
+
+static gboolean
+gsk_never_trigger_trigger (GtkShortcutTrigger *trigger,
+                           const GdkEvent     *event)
+                  
+{
+  return FALSE;
+}
+
+static void
+gsk_never_trigger_print (GtkShortcutTrigger *trigger,
+                         GString            *string)
+                  
+{
+  g_string_append (string, "<never>");
+}
+
+static const GtkShortcutTriggerClass GTK_NEVER_TRIGGER_CLASS = {
+  GTK_SHORTCUT_TRIGGER_NEVER,
+  sizeof (GtkNeverTrigger),
+  "GtkNeverTrigger",
+  gsk_never_trigger_finalize,
+  gsk_never_trigger_trigger,
+  gsk_never_trigger_print
+};
+
+static GtkNeverTrigger never = { { &GTK_NEVER_TRIGGER_CLASS, 1 } };
+
+/**
+ * gsk_never_trigger_get:
+ *
+ * Gets the never trigger. This is a singleton for a trigger that never triggers.
+ * Use this trigger instead of %NULL because it implements all virtual functions.
+ *
+ * Returns: (transfer none): The never trigger
+ */
+GtkShortcutTrigger *
+gtk_never_trigger_get (void)
+{
+  return &never.trigger;
+}
+
+/*** GTK_KEYVAL_TRIGGER ***/
+
+typedef struct _GtkKeyvalTrigger GtkKeyvalTrigger;
+
+struct _GtkKeyvalTrigger
+{
+  GtkShortcutTrigger trigger;
+
+  guint keyval;
+  GdkModifierType modifiers;
+};
+
+static void
+gsk_keyval_trigger_finalize (GtkShortcutTrigger *trigger)
+{
+}
+
+static gboolean
+gsk_keyval_trigger_trigger (GtkShortcutTrigger *trigger,
+                            const GdkEvent     *event)
+{
+  GtkKeyvalTrigger *self = (GtkKeyvalTrigger *) trigger;
+  GdkModifierType modifiers;
+  guint keyval;
+
+  if (gdk_event_get_event_type (event) != GDK_KEY_PRESS)
+    return FALSE;
+
+  /* XXX: This needs to deal with groups */
+  gdk_event_get_state (event, &modifiers);
+  gdk_event_get_keyval (event, &keyval);
+
+  if (keyval == GDK_KEY_ISO_Left_Tab)
+    keyval = GDK_KEY_Tab;
+  else
+    keyval = gdk_keyval_to_lower (keyval);
+
+  return keyval == self->keyval && modifiers == self->modifiers;
+}
+
+static void
+gsk_keyval_trigger_print (GtkShortcutTrigger *trigger,
+                          GString            *string)
+                  
+{
+  GtkKeyvalTrigger *self = (GtkKeyvalTrigger *) trigger;
+  char *accelerator_name;
+
+  accelerator_name = gtk_accelerator_name (self->keyval, self->modifiers);
+  g_string_append (string, accelerator_name);
+  g_free (accelerator_name);
+}
+
+static const GtkShortcutTriggerClass GTK_KEYVAL_TRIGGER_CLASS = {
+  GTK_SHORTCUT_TRIGGER_KEYVAL,
+  sizeof (GtkKeyvalTrigger),
+  "GtkKeyvalTrigger",
+  gsk_keyval_trigger_finalize,
+  gsk_keyval_trigger_trigger,
+  gsk_keyval_trigger_print
+};
+
+/**
+ * gsk_keyval_trigger_new:
+ * @keyval: The keyval to trigger for
+ * @modifiers: the modifiers that need to be present
+ *
+ * Creates a #GtkShortcutTrigger that will trigger whenever the key with
+ * the given @keyval and @modifiers is pressed.
+ *
+ * Returns: A new #GtkShortcutTrigger
+ */
+GtkShortcutTrigger *
+gtk_keyval_trigger_new (guint           keyval,
+                        GdkModifierType modifiers)
+{
+  GtkKeyvalTrigger *self;
+
+  self = (GtkKeyvalTrigger *) gtk_shortcut_trigger_new (&GTK_KEYVAL_TRIGGER_CLASS, 0);
+
+  /* We store keyvals as lower key */
+  if (keyval == GDK_KEY_ISO_Left_Tab)
+    self->keyval = GDK_KEY_Tab;
+  else
+    self->keyval = gdk_keyval_to_lower (keyval);
+  self->modifiers = modifiers;
+
+  return &self->trigger;
+}
+
+/**
+ * gtk_keyval_trigger_get_modifiers:
+ * @trigger: a keyval #GtkShortcutTrigger
+ *
+ * Gets the modifiers that must be present to succeed triggering @self.
+ *
+ * Returns: the modifiers
+ **/
+GdkModifierType
+gtk_keyval_trigger_get_modifiers (GtkShortcutTrigger *trigger)
+{
+  GtkKeyvalTrigger *self = (GtkKeyvalTrigger *) trigger;
+
+  g_return_val_if_fail (GTK_IS_SHORTCUT_TRIGGER_TYPE (trigger, GTK_SHORTCUT_TRIGGER_KEYVAL), 0);
+
+  return self->modifiers;
+}
+
+/**
+ * gtk_keyval_trigger_get_keyval:
+ * @trigger: a keyval #GtkShortcutTrigger
+ *
+ * Gets the keyval that must be pressed to succeed triggering @self.
+ *
+ * Returns: the keyval
+ **/
+guint
+gtk_keyval_trigger_get_keyval (GtkShortcutTrigger *trigger)
+{
+  GtkKeyvalTrigger *self = (GtkKeyvalTrigger *) trigger;
+
+  g_return_val_if_fail (GTK_IS_SHORTCUT_TRIGGER_TYPE (trigger, GTK_SHORTCUT_TRIGGER_KEYVAL), 0);
+
+  return self->keyval;
+}
+
+
diff --git a/gtk/gtkshortcuttrigger.h b/gtk/gtkshortcuttrigger.h
new file mode 100644
index 0000000000..83e54ec61c
--- /dev/null
+++ b/gtk/gtkshortcuttrigger.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __GTK_SHORTCUT_TRIGGER_H__
+#define __GTK_SHORTCUT_TRIGGER_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtktypes.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SHORTCUT_TRIGGER (gtk_shortcut_trigger_get_type ())
+
+#define GTK_IS_SHORTCUT_TRIGGER(obj) ((obj) != NULL)
+
+/**
+ * GtkShortcutTriggerType:
+ * @GTK_SHORTCUT_TRIGGER_NEVER: Never ever trigger
+ * @GTK_SHORTCUT_TRIGGER_KEYVAL: Trigger if a key even with matching
+ *     modifiers and keyval is received.
+ *
+ * The type of a trigger determines what the trigger triggers on.
+ **/
+typedef enum {
+  GTK_SHORTCUT_TRIGGER_NEVER,
+  GTK_SHORTCUT_TRIGGER_KEYVAL
+} GtkShortcutTriggerType;
+
+GDK_AVAILABLE_IN_ALL
+GType                   gtk_shortcut_trigger_get_type           (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_ALL
+GtkShortcutTrigger *    gtk_shortcut_trigger_ref                (GtkShortcutTrigger *self);
+GDK_AVAILABLE_IN_ALL
+void                    gtk_shortcut_trigger_unref              (GtkShortcutTrigger *self);
+
+GDK_AVAILABLE_IN_ALL
+GtkShortcutTriggerType  gtk_shortcut_trigger_get_trigger_type   (GtkShortcutTrigger *self);
+
+GDK_AVAILABLE_IN_ALL
+char *                  gtk_shortcut_trigger_to_string          (GtkShortcutTrigger *self);
+GDK_AVAILABLE_IN_ALL
+void                    gtk_shortcut_trigger_print              (GtkShortcutTrigger *self,
+                                                                 GString            *string);
+
+GDK_AVAILABLE_IN_ALL
+gboolean                gtk_shortcut_trigger_trigger            (GtkShortcutTrigger *self,
+                                                                 const GdkEvent     *event);
+
+GDK_AVAILABLE_IN_ALL
+GtkShortcutTrigger *    gtk_never_trigger_get                   (void);
+
+GDK_AVAILABLE_IN_ALL
+GtkShortcutTrigger *    gtk_keyval_trigger_new                  (guint               keyval,
+                                                                 GdkModifierType     modifiers);
+GDK_AVAILABLE_IN_ALL
+GdkModifierType         gtk_keyval_trigger_get_modifiers        (GtkShortcutTrigger *self);
+GDK_AVAILABLE_IN_ALL
+guint                   gtk_keyval_trigger_get_keyval           (GtkShortcutTrigger *self);
+
+G_END_DECLS
+
+#endif /* __GTK_SHORTCUT_TRIGGER_H__ */
diff --git a/gtk/gtktypes.h b/gtk/gtktypes.h
index 4214eb91cf..a2ec019214 100644
--- a/gtk/gtktypes.h
+++ b/gtk/gtktypes.h
@@ -47,6 +47,7 @@ typedef struct _GtkRoot              GtkRoot;
 typedef struct _GtkSelectionData       GtkSelectionData;
 typedef struct _GtkSettings            GtkSettings;
 typedef struct _GtkShortcut            GtkShortcut;
+typedef struct _GtkShortcutTrigger     GtkShortcutTrigger;
 typedef GdkSnapshot                    GtkSnapshot;
 typedef struct _GtkStyleContext        GtkStyleContext;
 typedef struct _GtkTooltip             GtkTooltip;
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index df3ba2d5b3..3568579c65 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -61,6 +61,7 @@
 #include "gtksettingsprivate.h"
 #include "gtkshortcut.h"
 #include "gtkshortcutcontroller.h"
+#include "gtkshortcuttrigger.h"
 #include "gtksizegroup-private.h"
 #include "gtksnapshotprivate.h"
 #include "gtkstylecontextprivate.h"
@@ -4355,8 +4356,8 @@ gtk_widget_adjust_size_allocation (GtkWidget         *widget,
 /**
  * gtk_widget_class_add_binding_signal: (skip)
  * @widget_class: the class to add the binding to
- * @mods: key modifier of binding to install
  * @keyval: key value of binding to install
+ * @mods: key modifier of binding to install
  * @signal: the signal to execute
  * @format_string: GVariant format string for arguments or %NULL for
  *     no arguments
@@ -4373,8 +4374,8 @@ gtk_widget_adjust_size_allocation (GtkWidget         *widget,
  */
 void
 gtk_widget_class_add_binding_signal (GtkWidgetClass  *widget_class,
-                                     GdkModifierType  mods,
                                      guint            keyval,
+                                     GdkModifierType  mods,
                                      const gchar     *signal,
                                      const gchar     *format_string,
                                      ...)
@@ -4386,7 +4387,7 @@ gtk_widget_class_add_binding_signal (GtkWidgetClass  *widget_class,
   /* XXX: validate variant format for signal */
 
   shortcut = gtk_shortcut_new ();
-  gtk_shortcut_set_keyval (shortcut, mods, keyval);
+  gtk_shortcut_set_trigger (shortcut, gtk_keyval_trigger_new (keyval, mods));
   gtk_shortcut_set_signal (shortcut, signal);
   if (format_string)
     {
diff --git a/gtk/meson.build b/gtk/meson.build
index 3a178f4773..87e202631b 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -338,6 +338,7 @@ gtk_public_sources = files([
   'gtkshortcutssection.c',
   'gtkshortcutsshortcut.c',
   'gtkshortcutswindow.c',
+  'gtkshortcuttrigger.c',
   'gtkshow.c',
   'gtksidebarrow.c',
   'gtksingleselection.c',
@@ -582,6 +583,7 @@ gtk_public_headers = files([
   'gtkshortcutssection.h',
   'gtkshortcutsshortcut.h',
   'gtkshortcutswindow.h',
+  'gtkshortcuttrigger.h',
   'gtkshow.h',
   'gtksingleselection.h',
   'gtksizegroup.h',
diff --git a/testsuite/gtk/defaultvalue.c b/testsuite/gtk/defaultvalue.c
index cfe9623cf2..00def6893f 100644
--- a/testsuite/gtk/defaultvalue.c
+++ b/testsuite/gtk/defaultvalue.c
@@ -324,6 +324,10 @@ G_GNUC_END_IGNORE_DEPRECATIONS
       if (g_type_is_a (type, GTK_TYPE_SETTINGS))
         continue;
 
+      if (g_type_is_a (type, GTK_TYPE_SHORTCUT) &&
+          strcmp (pspec->name, "trigger") == 0)
+        continue;
+
       if (g_type_is_a (type, GTK_TYPE_SPIN_BUTTON) &&
           (strcmp (pspec->name, "adjustment") == 0))
         continue;


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