[gtk/wip/matthiasc/shortcut: 59/102] gtk-demo: Add a dumb demo for shortcut triggers



commit 742e3dfd24a1b85362ec1be4edd1b9f5f012e5ef
Author: Benjamin Otte <otte redhat com>
Date:   Sun Aug 12 01:40:03 2018 +0200

    gtk-demo: Add a dumb demo for shortcut triggers

 demos/gtk-demo/demo.gresource.xml  |  1 +
 demos/gtk-demo/meson.build         |  1 +
 demos/gtk-demo/shortcut_triggers.c | 92 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
---
diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml
index 4dde63322a..7e4046cb6a 100644
--- a/demos/gtk-demo/demo.gresource.xml
+++ b/demos/gtk-demo/demo.gresource.xml
@@ -210,6 +210,7 @@
     <file>search_entry.c</file>
     <file>search_entry2.c</file>
     <file>shortcuts.c</file>
+    <file>shortcut_triggers.c</file>
     <file>sizegroup.c</file>
     <file>sidebar.c</file>
     <file>sliding_puzzle.c</file>
diff --git a/demos/gtk-demo/meson.build b/demos/gtk-demo/meson.build
index 8dbd14cc2d..08694a7355 100644
--- a/demos/gtk-demo/meson.build
+++ b/demos/gtk-demo/meson.build
@@ -62,6 +62,7 @@ demos = files([
   'search_entry.c',
   'search_entry2.c',
   'shortcuts.c',
+  'shortcut_triggers.c',
   'sidebar.c',
   'sizegroup.c',
   'sliding_puzzle.c',
diff --git a/demos/gtk-demo/shortcut_triggers.c b/demos/gtk-demo/shortcut_triggers.c
new file mode 100644
index 0000000000..6d4c910583
--- /dev/null
+++ b/demos/gtk-demo/shortcut_triggers.c
@@ -0,0 +1,92 @@
+/* Shortcuts
+ *
+ * GtkShortcut is the abstraction used by GTK to handle shortcuts from
+ * keyboard or other input devices.
+ *
+ * Shortcut triggers can be used to weave complex sequences of key
+ * presses into sophisticated mechanisms to activate shortcuts.
+ *
+ * This demo code shows creative ways to do that.
+ */
+
+#include <gtk/gtk.h>
+
+static GtkWidget *window = NULL;
+
+static gboolean
+shortcut_activated (GtkWidget *widget,
+                    GVariant  *unused,
+                    gpointer   row)
+{
+  g_print ("activated %s\n", gtk_label_get_label (row));
+  return TRUE;
+}
+
+static GtkShortcutTrigger *
+create_ctrl_g (void)
+{
+  return gtk_keyval_trigger_new (GDK_KEY_g, GDK_CONTROL_MASK);
+}
+
+static GtkShortcutTrigger *
+create_x (void)
+{
+  return gtk_keyval_trigger_new (GDK_KEY_x, 0);
+}
+
+struct {
+  const char *description;
+  GtkShortcutTrigger * (* create_trigger_func) (void);
+} shortcuts[] = {
+  { "Press Ctrl-G", create_ctrl_g },
+  { "Press X", create_x },
+};
+
+GtkWidget *
+do_shortcut_triggers (GtkWidget *do_widget)
+{
+  guint i;
+
+  if (!window)
+    {
+      GtkWidget *list;
+      GtkEventController *controller;
+
+      window = gtk_window_new ();
+      gtk_window_set_display (GTK_WINDOW (window),
+                              gtk_widget_get_display (do_widget));
+      gtk_window_set_title (GTK_WINDOW (window), "Shortcuts");
+
+      g_signal_connect (window, "destroy",
+                        G_CALLBACK (gtk_widget_destroyed), &window);
+
+      list = gtk_list_box_new ();
+      g_object_set (list, "margin", 6, NULL);
+      gtk_container_add (GTK_CONTAINER (window), list);
+
+      for (i = 0; i < G_N_ELEMENTS (shortcuts); i++)
+        {
+          GtkShortcut *shortcut;
+          GtkWidget *row;
+
+          row = gtk_label_new (shortcuts[i].description);
+          gtk_container_add (GTK_CONTAINER (list), row);
+
+          controller = gtk_shortcut_controller_new ();
+          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_callback (shortcut, shortcut_activated, row, NULL);
+          gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (controller), shortcut);
+        }
+    }
+
+  if (!gtk_widget_get_visible (window))
+    gtk_widget_show (window);
+  else
+    gtk_widget_destroy (window);
+
+  return window;
+}


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