[gtk/wip/ebassi/shortcut: 196/267] shortcutcontroller: Add gtk_shortcut_controller_add_shortcut()



commit 7975e33b9cff6c731022b4c48252bb6ca20c6c9c
Author: Benjamin Otte <otte redhat com>
Date:   Sun Aug 12 01:38:50 2018 +0200

    shortcutcontroller: Add gtk_shortcut_controller_add_shortcut()
    
    ... and gtk_shortcut_controller_remove_shortcut().

 docs/reference/gtk/gtk4-sections.txt |  2 +
 gtk/gtkshortcutcontroller.c          | 71 +++++++++++++++++++++++++++++++++---
 gtk/gtkshortcutcontroller.h          |  7 ++++
 3 files changed, 75 insertions(+), 5 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 5bbc4b75fd..174a82dc83 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -6444,6 +6444,8 @@ gtk_shortcut_get_type
 <TITLE>GtkShortcutController</TITLE>
 GtkShortcutController
 gtk_shortcut_controller_new
+gtk_shortcut_controller_add_shortcut
+gtk_shortcut_controller_remove_shortcut
 
 <SUBSECTION Standard>
 GTK_TYPE_SHORTCUT_CONTROLLER
diff --git a/gtk/gtkshortcutcontroller.c b/gtk/gtkshortcutcontroller.c
index 37b26d8187..f77781a0d0 100644
--- a/gtk/gtkshortcutcontroller.c
+++ b/gtk/gtkshortcutcontroller.c
@@ -42,6 +42,8 @@ struct _GtkShortcutController
 {
   GtkEventController parent_instance;
 
+  GSList *shortcuts;
+
   guint run_class : 1;
 };
 
@@ -54,11 +56,14 @@ G_DEFINE_TYPE (GtkShortcutController, gtk_shortcut_controller,
                GTK_TYPE_EVENT_CONTROLLER)
 
 static void
-gtk_shortcut_controller_finalize (GObject *object)
+gtk_shortcut_controller_dispose (GObject *object)
 {
-  //GtkShortcutController *self = GTK_SHORTCUT_CONTROLLER (object);
+  GtkShortcutController *self = GTK_SHORTCUT_CONTROLLER (object);
+
+  g_slist_free_full (self->shortcuts, g_object_unref);
+  self->shortcuts = NULL;
 
-  G_OBJECT_CLASS (gtk_shortcut_controller_parent_class)->finalize (object);
+  G_OBJECT_CLASS (gtk_shortcut_controller_parent_class)->dispose (object);
 }
 
 static gboolean
@@ -81,10 +86,16 @@ gtk_shortcut_controller_handle_event (GtkEventController *controller,
   GtkWidget *widget;
   const GSList *l;
 
-  widget = gtk_event_controller_get_widget (controller);
+  for (l = self->shortcuts; l; l = l->next)
+    {
+      if (gtk_shortcut_controller_trigger_shortcut (self, l->data, event))
+        return TRUE;
+    }
 
   if (self->run_class)
     {
+      widget = gtk_event_controller_get_widget (controller);
+
       if (event_type == GDK_KEY_PRESS ||
           event_type == GDK_KEY_RELEASE)
         {
@@ -108,7 +119,7 @@ gtk_shortcut_controller_class_init (GtkShortcutControllerClass *klass)
   GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  object_class->finalize = gtk_shortcut_controller_finalize;
+  object_class->dispose = gtk_shortcut_controller_dispose;
   controller_class->handle_event = gtk_shortcut_controller_handle_event;
 }
 
@@ -130,3 +141,53 @@ gtk_shortcut_controller_set_run_class (GtkShortcutController  *controller,
 {
   controller->run_class = run_class;
 }
+
+/**
+ * gtk_shortcut_controller_add_shortcut:
+ * @self: the controller
+ * @shortcut: a #GtkShortcut
+ *
+ * Adds @shortcut to the list of shortcuts handled by @self.
+ *
+ * The shortcut is added to the list so that it is triggered before
+ * all existing shortcuts.
+ *
+ * FIXME: What's supposed to happen if a shortcut gets added twice?
+ **/
+void
+gtk_shortcut_controller_add_shortcut (GtkShortcutController *self,
+                                      GtkShortcut           *shortcut)
+{
+  g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self));
+  g_return_if_fail (GTK_IS_SHORTCUT (shortcut));
+
+  g_object_ref (shortcut);
+  self->shortcuts = g_slist_prepend (self->shortcuts, shortcut);
+}
+
+/**
+ * gtk_shortcut_controller_remove_shortcut:
+ * @self: the controller
+ * @shortcut: a #GtkShortcut
+ *
+ * Removes @shortcut from the list of shortcuts handled by @self.
+ *
+ * If @shortcut had not been added to @controller, this function does
+ * nothing.
+ **/
+void
+gtk_shortcut_controller_remove_shortcut (GtkShortcutController  *self,
+                                         GtkShortcut            *shortcut)
+{
+  GSList *l;
+
+  g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self));
+  g_return_if_fail (GTK_IS_SHORTCUT (shortcut));
+
+  l = g_slist_find (self->shortcuts, shortcut);
+  if (l == NULL)
+    return;
+
+  self->shortcuts = g_slist_delete_link (self->shortcuts, l);
+  g_object_unref (shortcut);
+}
diff --git a/gtk/gtkshortcutcontroller.h b/gtk/gtkshortcutcontroller.h
index 91fb97d67c..e078e3241a 100644
--- a/gtk/gtkshortcutcontroller.h
+++ b/gtk/gtkshortcutcontroller.h
@@ -45,6 +45,13 @@ GType                   gtk_shortcut_controller_get_type                (void) G
 GDK_AVAILABLE_IN_ALL
 GtkEventController *    gtk_shortcut_controller_new                     (void);
 
+GDK_AVAILABLE_IN_ALL
+void                    gtk_shortcut_controller_add_shortcut            (GtkShortcutController  *controller,
+                                                                         GtkShortcut            *shortcut);
+GDK_AVAILABLE_IN_ALL
+void                    gtk_shortcut_controller_remove_shortcut         (GtkShortcutController  *controller,
+                                                                         GtkShortcut            *shortcut);
+
 G_END_DECLS
 
 #endif /* __GTK_SHORTCUT_CONTROLLER_H__ */


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