[gtk/shortcuts-rebased-again: 102/124] gtk-demo: Redo builder example to use shortcuts



commit e9f427fa85622ae04cc4662964d3395d9946d596
Author: Benjamin Otte <otte redhat com>
Date:   Fri Aug 24 07:01:08 2018 +0200

    gtk-demo: Redo builder example to use shortcuts
    
    Now that shortcuts and controllers are supported by GtkBuilder, demo
    how all of that works.

 demos/gtk-demo/builder.c | 61 ++++++++++++---------------------------
 demos/gtk-demo/demo.ui   | 74 +++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 91 insertions(+), 44 deletions(-)
---
diff --git a/demos/gtk-demo/builder.c b/demos/gtk-demo/builder.c
index cf293c53cd..446fac6b9b 100644
--- a/demos/gtk-demo/builder.c
+++ b/demos/gtk-demo/builder.c
@@ -31,17 +31,27 @@ about_activate (GSimpleAction *action,
 }
 
 static void
-help_activate (GSimpleAction *action,
+menu_activate (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
 {
-  g_print ("Help not available\n");
+  g_print ("%s action not available\n", g_action_get_name (G_ACTION (action)));
 }
 
 static GActionEntry win_entries[] = {
   { "quit", quit_activate, NULL, NULL, NULL },
   { "about", about_activate, NULL, NULL, NULL },
-  { "help", help_activate, NULL, NULL, NULL }
+  { "help", menu_activate, NULL, NULL, NULL }
+};
+
+static GActionEntry menu_entries[] = {
+  { "new", menu_activate, NULL, NULL, NULL },
+  { "open", menu_activate, NULL, NULL, NULL },
+  { "save", menu_activate, NULL, NULL, NULL },
+  { "save_as", menu_activate, NULL, NULL, NULL },
+  { "copy", menu_activate, NULL, NULL, NULL },
+  { "cut", menu_activate, NULL, NULL, NULL },
+  { "paste", menu_activate, NULL, NULL, NULL },
 };
 
 GtkWidget *
@@ -50,8 +60,6 @@ do_builder (GtkWidget *do_widget)
   static GtkWidget *window = NULL;
   GtkWidget *toolbar;
   GActionGroup *actions;
-  GtkAccelGroup *accel_group;
-  GtkWidget *item;
 
   if (!window)
     {
@@ -73,44 +81,11 @@ do_builder (GtkWidget *do_widget)
                                        win_entries, G_N_ELEMENTS (win_entries),
                                        window);
       gtk_widget_insert_action_group (window, "win", actions);
-      accel_group = gtk_accel_group_new ();
-      gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
-
-      item = (GtkWidget*)gtk_builder_get_object (builder, "new_item");
-      gtk_widget_add_accelerator (item, "activate", accel_group,
-                                  GDK_KEY_n, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
-
-      item = (GtkWidget*)gtk_builder_get_object (builder, "open_item");
-      gtk_widget_add_accelerator (item, "activate", accel_group,
-                                  GDK_KEY_o, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
-
-      item = (GtkWidget*)gtk_builder_get_object (builder, "save_item");
-      gtk_widget_add_accelerator (item, "activate", accel_group,
-                                  GDK_KEY_s, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
-
-      item = (GtkWidget*)gtk_builder_get_object (builder, "quit_item");
-      gtk_widget_add_accelerator (item, "activate", accel_group,
-                                  GDK_KEY_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
-
-      item = (GtkWidget*)gtk_builder_get_object (builder, "copy_item");
-      gtk_widget_add_accelerator (item, "activate", accel_group,
-                                  GDK_KEY_c, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
-
-      item = (GtkWidget*)gtk_builder_get_object (builder, "cut_item");
-      gtk_widget_add_accelerator (item, "activate", accel_group,
-                                  GDK_KEY_x, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
-
-      item = (GtkWidget*)gtk_builder_get_object (builder, "paste_item");
-      gtk_widget_add_accelerator (item, "activate", accel_group,
-                                  GDK_KEY_v, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
-
-      item = (GtkWidget*)gtk_builder_get_object (builder, "help_item");
-      gtk_widget_add_accelerator (item, "activate", accel_group,
-                                  GDK_KEY_F1, 0, GTK_ACCEL_VISIBLE);
-
-      item = (GtkWidget*)gtk_builder_get_object (builder, "about_item");
-      gtk_widget_add_accelerator (item, "activate", accel_group,
-                                  GDK_KEY_F7, 0, GTK_ACCEL_VISIBLE);
+      actions = (GActionGroup*)g_simple_action_group_new ();
+      g_action_map_add_action_entries (G_ACTION_MAP (actions),
+                                       menu_entries, G_N_ELEMENTS (menu_entries),
+                                       window);
+      gtk_widget_insert_action_group (window, "menu", actions);
 
       g_object_set_data_full (G_OBJECT(window), "builder", builder, g_object_unref);
     }
diff --git a/demos/gtk-demo/demo.ui b/demos/gtk-demo/demo.ui
index 6fa269822e..61646fa678 100644
--- a/demos/gtk-demo/demo.ui
+++ b/demos/gtk-demo/demo.ui
@@ -33,6 +33,72 @@
     <property name="default-height">250</property>
     <property name="default-width">440</property>
     <property name="title" translatable="yes">Builder</property>
+    <child>
+      <object class="GtkShortcutController" id="controller1">
+        <property name="scope">global</property>
+        <property name="propagation-phase">capture</property>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">&lt;Primary&gt;n</property>
+            <property name="action">action(menu.new)</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">&lt;Primary&gt;o</property>
+            <property name="action">action(menu.open)</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">&lt;Primary&gt;s</property>
+            <property name="action">action(menu.save)</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">&lt;Primary&gt;&lt;Shift&gt;s</property>
+            <property name="action">action(menu.save_as)</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">&lt;Primary&gt;q</property>
+            <property name="action">action(win.quit)</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">&lt;Primary&gt;c</property>
+            <property name="action">action(menu.copy)</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">&lt;Primary&gt;x</property>
+            <property name="action">action(menu.cut)</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">&lt;Primary&gt;v</property>
+            <property name="action">action(menu.paste)</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">F1</property>
+            <property name="action">action(win.help)</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkShortcut">
+            <property name="trigger">F7</property>
+            <property name="action">action(win.about)</property>
+          </object>
+        </child>
+      </object>
+    </child>
     <child>
       <object class="GtkBox" id="vbox1">
         <property name="orientation">vertical</property>
@@ -53,25 +119,28 @@
                       <object class="GtkMenuItem" id="new_item">
                         <property name="label" translatable="yes">_New</property>
                         <property name="use-underline">1</property>
+                        <property name="action-name">menu.new</property>
                       </object>
                     </child>
                     <child>
                       <object class="GtkMenuItem" id="open_item">
                         <property name="label" translatable="yes">_Open</property>
                         <property name="use-underline">1</property>
+                        <property name="action-name">menu.open</property>
                       </object>
                     </child>
                     <child>
                       <object class="GtkMenuItem" id="save_item">
                         <property name="label" translatable="yes">_Save</property>
                         <property name="use-underline">1</property>
+                        <property name="action-name">menu.save</property>
                       </object>
                     </child>
                     <child>
                       <object class="GtkMenuItem" id="save_as_item">
                         <property name="label" translatable="yes">Save _As</property>
                         <property name="use-underline">1</property>
-                        <accelerator key="s" modifiers="primary | shift-mask" signal="activate"/>
+                        <property name="action-name">menu.save_as</property>
                       </object>
                     </child>
                     <child>
@@ -98,18 +167,21 @@
                       <object class="GtkMenuItem" id="copy_item">
                         <property name="label" translatable="yes">_Copy</property>
                         <property name="use-underline">1</property>
+                        <property name="action-name">menu.copy</property>
                       </object>
                     </child>
                     <child>
                       <object class="GtkMenuItem" id="cut_item">
                         <property name="label" translatable="yes">_Cut</property>
                         <property name="use-underline">1</property>
+                        <property name="action-name">menu.cut</property>
                       </object>
                     </child>
                     <child>
                       <object class="GtkMenuItem" id="paste_item">
                         <property name="label" translatable="yes">_Paste</property>
                         <property name="use-underline">1</property>
+                        <property name="action-name">menu.paste</property>
                       </object>
                     </child>
                   </object>


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