[gtk/wip/ebassi/shortcut: 21/85] shortcutcontroller: Add gtk_shortcut_controller_add_shortcut()



commit e3857c4515339e21ca4b88b438c30b990b18228e
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 66d90a9c0c..32a4de9bb4 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -6452,6 +6452,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 e26da3fb62..44ab5019f0 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
@@ -80,10 +85,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 (gtk_bindings_activate_event (G_OBJECT (widget), (GdkEventKey *) event))
         return TRUE;
 
@@ -103,7 +114,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;
 }
 
@@ -125,3 +136,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]