[gtk/wip/matthiasc/shortcut-4: 75/139] shortcut: Change the API for creating shortcuts



commit 11f7d70917d878ee1ebf477386a073e5e816a62c
Author: Benjamin Otte <otte redhat com>
Date:   Sat Aug 18 20:01:43 2018 +0200

    shortcut: Change the API for creating shortcuts
    
    When creating shortcuts, there almost always are a trigger and an action
    available for use. So make gtk_shortcut_new() take those as arguments.
    
    Also add gtk_shortcut_new_with_arguments() so people can easily pass
    those in, too.

 demos/gtk-demo/shortcut_triggers.c   |  5 +--
 docs/reference/gtk/gtk4-sections.txt |  1 +
 gtk/gtklabel.c                       |  5 +--
 gtk/gtkshortcut.c                    | 72 ++++++++++++++++++++++++++++++++++--
 gtk/gtkshortcut.h                    |  8 +++-
 gtk/gtkwidget.c                      | 15 +++-----
 gtk/gtkwindow.c                      | 16 ++++----
 7 files changed, 94 insertions(+), 28 deletions(-)
---
diff --git a/demos/gtk-demo/shortcut_triggers.c b/demos/gtk-demo/shortcut_triggers.c
index 5f06cbe520..d5b59c3de8 100644
--- a/demos/gtk-demo/shortcut_triggers.c
+++ b/demos/gtk-demo/shortcut_triggers.c
@@ -76,9 +76,8 @@ do_shortcut_triggers (GtkWidget *do_widget)
           gtk_shortcut_controller_set_scope (GTK_SHORTCUT_CONTROLLER (controller), 
GTK_SHORTCUT_SCOPE_GLOBAL);
           gtk_widget_add_controller (row, controller);
 
-          shortcut = gtk_shortcut_new ();
-          gtk_shortcut_set_trigger (shortcut, shortcuts[i].create_trigger_func());
-          gtk_shortcut_set_action (shortcut, gtk_callback_action_new (shortcut_activated, row, NULL));
+          shortcut = gtk_shortcut_new (shortcuts[i].create_trigger_func(),
+                                       gtk_callback_action_new (shortcut_activated, row, NULL));
           gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (controller), shortcut);
         }
     }
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 28cd3c9339..002f112f2f 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -6067,6 +6067,7 @@ gtk_shortcut_action_get_type
 <TITLE>GtkShortcut</TITLE>
 GtkShortcut
 gtk_shortcut_new
+gtk_shortcut_new_with_arguments
 gtk_shortcut_get_trigger
 gtk_shortcut_set_trigger
 gtk_shortcut_get_action
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index 90dc9013cf..049927ef2c 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -1822,9 +1822,8 @@ gtk_label_setup_mnemonic (GtkLabel *label)
       priv->mnemonic_controller = gtk_shortcut_controller_new ();
       gtk_event_controller_set_propagation_phase (priv->mnemonic_controller, GTK_PHASE_CAPTURE);
       gtk_shortcut_controller_set_scope (GTK_SHORTCUT_CONTROLLER (priv->mnemonic_controller), 
GTK_SHORTCUT_SCOPE_MANAGED);
-      shortcut = gtk_shortcut_new ();
-      gtk_shortcut_set_trigger (shortcut, gtk_mnemonic_trigger_new (priv->mnemonic_keyval));
-      gtk_shortcut_set_action (shortcut, gtk_mnemonic_action_new ());
+      shortcut = gtk_shortcut_new (gtk_mnemonic_trigger_new (priv->mnemonic_keyval),
+                                   gtk_mnemonic_action_new ());
       gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (priv->mnemonic_controller), shortcut);
       gtk_widget_add_controller (GTK_WIDGET (label), priv->mnemonic_controller);
       g_object_unref (shortcut);
diff --git a/gtk/gtkshortcut.c b/gtk/gtkshortcut.c
index 4c7ec59859..6a7ff1a148 100644
--- a/gtk/gtkshortcut.c
+++ b/gtk/gtkshortcut.c
@@ -200,15 +200,81 @@ gtk_shortcut_init (GtkShortcut *self)
 
 /**
  * gtk_shortcut_new:
+ * @trigger: (transfer full) (allow-none): The trigger that will trigger the shortcut
+ * @action: (transfer full) (allow-none): The action that will be activated upon
+ *    triggering
  *
- * Creates a new empty #GtkShortcut that never triggers and activates nothing.
+ * Creates a new #GtkShortcut that is triggered by @trigger and then activates
+ * @action.
  *
  * Returns: a new #GtkShortcut
  **/
 GtkShortcut *
-gtk_shortcut_new (void)
+gtk_shortcut_new (GtkShortcutTrigger *trigger,
+                  GtkShortcutAction  *action)
 {
-  return g_object_new (GTK_TYPE_SHORTCUT, NULL);
+  GtkShortcut *shortcut;
+
+  shortcut = g_object_new (GTK_TYPE_SHORTCUT,
+                           "action", action,
+                           "trigger", trigger,
+                           NULL);
+
+  if (trigger)
+    gtk_shortcut_trigger_unref (trigger);
+  if (action)
+    gtk_shortcut_action_unref (action);
+
+  return shortcut;
+}
+
+/**
+ * gtk_shortcut_new_with_arguments: (skip)
+ * @trigger: (transfer full) (allow-none): The trigger that will trigger the shortcut
+ * @action: (transfer full) (allow-none): The action that will be activated upon
+ *    triggering
+ * @format_string: (allow-none): GVariant format string for arguments or %NULL for
+ *     no arguments
+ * @...: arguments, as given by format string.
+ *
+ * Creates a new #GtkShortcut that is triggered by @trigger and then activates
+ * @action with arguments given by @format_string.
+ *
+ * Returns: a new #GtkShortcut
+ **/
+GtkShortcut *
+gtk_shortcut_new_with_arguments (GtkShortcutTrigger *trigger,
+                                 GtkShortcutAction  *action,
+                                 const gchar        *format_string,
+                                 ...)
+{
+  GtkShortcut *shortcut;
+  GVariant *args;
+
+  if (format_string)
+    {
+      va_list valist;
+      va_start (valist, format_string);
+      args = g_variant_new_va (format_string, NULL, &valist);
+      va_end (valist);
+    }
+  else
+    {
+      args = NULL;
+    }
+
+  shortcut = g_object_new (GTK_TYPE_SHORTCUT,
+                           "action", action,
+                           "arguments", args,
+                           "trigger", trigger,
+                           NULL);
+
+  if (trigger)
+    gtk_shortcut_trigger_unref (trigger);
+  if (action)
+    gtk_shortcut_action_unref (action);
+
+  return shortcut;
 }
 
 /**
diff --git a/gtk/gtkshortcut.h b/gtk/gtkshortcut.h
index 78bc871bf3..77c89754a2 100644
--- a/gtk/gtkshortcut.h
+++ b/gtk/gtkshortcut.h
@@ -30,7 +30,13 @@ GDK_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (GtkShortcut, gtk_shortcut, GTK, SHORTCUT, GObject)
 
 GDK_AVAILABLE_IN_ALL
-GtkShortcut *   gtk_shortcut_new                                (void);
+GtkShortcut *   gtk_shortcut_new                                (GtkShortcutTrigger     *trigger,
+                                                                 GtkShortcutAction      *action);
+GDK_AVAILABLE_IN_ALL
+GtkShortcut *   gtk_shortcut_new_with_arguments                 (GtkShortcutTrigger     *trigger,
+                                                                 GtkShortcutAction      *action,
+                                                                 const gchar            *format_string,
+                                                                 ...);
 
 GDK_AVAILABLE_IN_ALL
 GtkShortcutTrigger *
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index e710789b72..c4766a6848 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -4367,9 +4367,8 @@ gtk_widget_class_add_binding (GtkWidgetClass  *widget_class,
 
   g_return_if_fail (GTK_IS_WIDGET_CLASS (widget_class));
 
-  shortcut = gtk_shortcut_new ();
-  gtk_shortcut_set_trigger (shortcut, gtk_keyval_trigger_new (keyval, mods));
-  gtk_shortcut_set_action (shortcut, gtk_callback_action_new (func, NULL, NULL));
+  shortcut = gtk_shortcut_new (gtk_keyval_trigger_new (keyval, mods),
+                               gtk_callback_action_new (func, NULL, NULL));
   if (format_string)
     {
       va_list args;
@@ -4417,9 +4416,8 @@ gtk_widget_class_add_binding_signal (GtkWidgetClass  *widget_class,
   g_return_if_fail (g_signal_lookup (signal, G_TYPE_FROM_CLASS (widget_class)));
   /* XXX: validate variant format for signal */
 
-  shortcut = gtk_shortcut_new ();
-  gtk_shortcut_set_trigger (shortcut, gtk_keyval_trigger_new (keyval, mods));
-  gtk_shortcut_set_action (shortcut, gtk_signal_action_new (signal));
+  shortcut = gtk_shortcut_new (gtk_keyval_trigger_new (keyval, mods),
+                               gtk_signal_action_new (signal));
   if (format_string)
     {
       va_list args;
@@ -4466,9 +4464,8 @@ gtk_widget_class_add_binding_action (GtkWidgetClass  *widget_class,
   g_return_if_fail (GTK_IS_WIDGET_CLASS (widget_class));
   /* XXX: validate variant format for action */
 
-  shortcut = gtk_shortcut_new ();
-  gtk_shortcut_set_trigger (shortcut, gtk_keyval_trigger_new (keyval, mods));
-  gtk_shortcut_set_action (shortcut, gtk_action_action_new (action_name));
+  shortcut = gtk_shortcut_new (gtk_keyval_trigger_new (keyval, mods),
+                               gtk_action_action_new (action_name));
   if (format_string)
     {
       va_list args;
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index 83a8bd8cfa..5101d3d31a 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -563,12 +563,11 @@ add_tab_bindings (GtkWidgetClass   *widget_class,
 {
   GtkShortcut *shortcut;
 
-  shortcut = gtk_shortcut_new ();
-  gtk_shortcut_set_trigger (shortcut,
-                            gtk_alternative_trigger_new (gtk_keyval_trigger_new (GDK_KEY_Tab, modifiers),
-                                                         gtk_keyval_trigger_new (GDK_KEY_KP_Tab, 
modifiers)));
-  gtk_shortcut_set_action (shortcut, gtk_signal_action_new ("move-focus"));
-  gtk_shortcut_set_arguments (shortcut, g_variant_new_tuple ((GVariant*[1]) { g_variant_new_int32 
(direction) }, 1));
+  shortcut = gtk_shortcut_new_with_arguments (
+                 gtk_alternative_trigger_new (gtk_keyval_trigger_new (GDK_KEY_Tab, modifiers),
+                                              gtk_keyval_trigger_new (GDK_KEY_KP_Tab, modifiers)),
+                 gtk_signal_action_new ("move-focus"),
+                 "(i)", direction);
 
   gtk_widget_class_add_shortcut (widget_class, shortcut);
 
@@ -1766,9 +1765,8 @@ gtk_window_init (GtkWindow *window)
   controller = gtk_shortcut_controller_new ();
   gtk_event_controller_set_propagation_phase (controller, GTK_PHASE_CAPTURE);
 
-  shortcut = gtk_shortcut_new ();
-  gtk_shortcut_set_trigger (shortcut, gtk_keyval_trigger_new (MENU_BAR_ACCEL, 0));
-  gtk_shortcut_set_action (shortcut, gtk_callback_action_new (gtk_window_activate_menubar, NULL, NULL));
+  shortcut = gtk_shortcut_new (gtk_keyval_trigger_new (MENU_BAR_ACCEL, 0),
+                               gtk_callback_action_new (gtk_window_activate_menubar, NULL, NULL));
   gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (controller), shortcut);
   gtk_event_controller_set_name (controller, "gtk-window-menubar-accel");
   gtk_widget_add_controller (widget, controller);


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