[gtk/wip/matthiasc/shortcut: 30/106] shortcutcontroller: Add gtk_shortcut_controller_add_shortcut()



commit c1312d2205add711eb6fd17cb777da58e10113cb
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 d46a2d39a5..72c04bb574 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -6117,6 +6117,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 005c480033..01d0f49792 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
@@ -83,10 +88,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)
         {
@@ -110,7 +121,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;
 }
 
@@ -132,3 +143,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]