[gtk/no-app-menu: 3/5] examples: Stop using application menu apis



commit b23a6219ea51994c3c014c36e5f2015a662939a2
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu May 21 00:37:37 2020 -0400

    examples: Stop using application menu apis
    
    The menu-related GtkApplication and GtkAppliationWindow
    apis are going away. Use regular menubars and buttons
    where appropriate.

 examples/bp/bloatpad.c   | 34 +++++++++++++++++++++-------------
 examples/bp/gtk/menus.ui | 45 +++++++++++++++++++++++----------------------
 examples/plugman.c       | 45 +++++++++++++++++++++++++--------------------
 examples/sunny.c         | 16 ++++++++++------
 4 files changed, 79 insertions(+), 61 deletions(-)
---
diff --git a/examples/bp/bloatpad.c b/examples/bp/bloatpad.c
index 21fdfc98d9..c9c4f5222f 100644
--- a/examples/bp/bloatpad.c
+++ b/examples/bp/bloatpad.c
@@ -6,6 +6,7 @@ typedef struct
   GtkApplication parent_instance;
 
   guint quit_inhibit;
+  GMenu *menu;
   GMenu *time;
   guint timeout;
 } BloatPad;
@@ -212,6 +213,7 @@ new_window (GApplication *app,
             GFile        *file)
 {
   GtkWidget *window, *grid, *scrolled, *view;
+  GtkWidget *menubar;
   GtkWidget *toolbar;
   GtkWidget *button;
   GtkWidget *sw, *box, *label;
@@ -224,6 +226,9 @@ new_window (GApplication *app,
   grid = gtk_grid_new ();
   gtk_window_set_child (GTK_WINDOW (window), grid);
 
+  menubar = gtk_popover_menu_bar_new_from_model (G_MENU_MODEL (((BloatPad *)app)->menu));
+  gtk_grid_attach (GTK_GRID (grid), menubar, 0, -1, 1, 1);
+
   toolbar = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
   button = gtk_toggle_button_new ();
   gtk_button_set_icon_name (GTK_BUTTON (button), "format-justify-left");
@@ -525,18 +530,20 @@ bloat_pad_startup (GApplication *application)
     { "win.justify::center", { "<Control>m", NULL } },
     { "win.justify::right", { "<Control>r", NULL } }
   };
+  GtkBuilder *builder;
 
-  G_APPLICATION_CLASS (bloat_pad_parent_class)
-    ->startup (application);
+  G_APPLICATION_CLASS (bloat_pad_parent_class)->startup (application);
 
   g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), 
application);
 
   for (i = 0; i < G_N_ELEMENTS (accels); i++)
     gtk_application_set_accels_for_action (app, accels[i].action_and_target, accels[i].accelerators);
 
-  menu = gtk_application_get_menu_by_id (GTK_APPLICATION (application), "icon-menu");
+  builder = gtk_builder_new ();
+  gtk_builder_add_from_resource (builder, "/org/gtk/bloatpad/gtk/menus.ui", NULL);
+  menu = G_MENU (gtk_builder_get_object (builder, "icon-menu"));
 
-  file = g_file_new_for_uri ("resource:///org/gtk/libgtk/icons/16x16/actions/gtk-select-color.png");
+  file = g_file_new_for_uri ("resource:///org/gtk/libgtk/icons/16x16/actions/media-record.png");
   icon = g_file_icon_new (file);
   item = g_menu_item_new ("File Icon", NULL);
   g_menu_item_set_icon (item, icon);
@@ -552,7 +559,7 @@ bloat_pad_startup (GApplication *application)
   g_object_unref (item);
   g_object_unref (icon);
 
-  bytes = g_resources_lookup_data ("/org/gtk/libgtk/icons/16x16/actions/gtk-select-font.png", 0, NULL);
+  bytes = g_resources_lookup_data ("/org/gtk/libgtk/icons/16x16/actions/bookmark-new.png", 0, NULL);
   icon = g_bytes_icon_new (bytes);
   item = g_menu_item_new ("Bytes Icon", NULL);
   g_menu_item_set_icon (item, icon);
@@ -561,19 +568,19 @@ bloat_pad_startup (GApplication *application)
   g_object_unref (icon);
   g_bytes_unref (bytes);
 
-  icon = G_ICON (gdk_pixbuf_new_from_resource ("/org/gtk/libgtk/icons/16x16/actions/gtk-preferences.png", 
NULL));
+  icon = G_ICON (gdk_pixbuf_new_from_resource ("/org/gtk/libgtk/icons/16x16/actions/media-eject.png", NULL));
   item = g_menu_item_new ("Pixbuf", NULL);
   g_menu_item_set_icon (item, icon);
   g_menu_append_item (menu, item);
   g_object_unref (item);
   g_object_unref (icon);
 
-  file = g_file_new_for_uri ("resource:///org/gtk/libgtk/icons/16x16/actions/gtk-page-setup.png");
+  file = g_file_new_for_uri ("resource:///org/gtk/libgtk/icons/16x16/actions/insert-image.png");
   icon = g_file_icon_new (file);
   emblem = g_emblem_new (icon);
   g_object_unref (icon);
   g_object_unref (file);
-  file = g_file_new_for_uri 
("resource:///org/gtk/libgtk/icons/16x16/actions/gtk-orientation-reverse-portrait.png");
+  file = g_file_new_for_uri ("resource:///org/gtk/libgtk/icons/16x16/actions/list-add.png");
   icon2 = g_file_icon_new (file);
   icon = g_emblemed_icon_new (icon2, emblem);
   item = g_menu_item_new ("Emblemed Icon", NULL);
@@ -596,8 +603,11 @@ bloat_pad_startup (GApplication *application)
   gtk_application_set_accels_for_action (GTK_APPLICATION (application), "app.new", new_accels);
 
   dump_accels (GTK_APPLICATION (application));
-  //gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object 
(builder, "app-menu")));
-  bloatpad->time = gtk_application_get_menu_by_id (GTK_APPLICATION (application), "time-menu");
+
+  bloatpad->menu = g_object_ref (G_MENU (gtk_builder_get_object (builder, "menubar")));
+  bloatpad->time = g_object_ref (G_MENU (gtk_builder_get_object (builder, "time-menu")));
+
+  g_object_unref (builder);
 }
 
 static void
@@ -611,8 +621,7 @@ bloat_pad_shutdown (GApplication *application)
       bloatpad->timeout = 0;
     }
 
-  G_APPLICATION_CLASS (bloat_pad_parent_class)
-    ->shutdown (application);
+  G_APPLICATION_CLASS (bloat_pad_parent_class)->shutdown (application);
 }
 
 static void
@@ -632,7 +641,6 @@ bloat_pad_class_init (BloatPadClass *class)
   application_class->open = bloat_pad_open;
 
   object_class->finalize = bloat_pad_finalize;
-
 }
 
 static BloatPad *
diff --git a/examples/bp/gtk/menus.ui b/examples/bp/gtk/menus.ui
index 1df1ac0f56..0bd002da1a 100644
--- a/examples/bp/gtk/menus.ui
+++ b/examples/bp/gtk/menus.ui
@@ -1,28 +1,29 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
-  <menu id="app-menu">
-    <section>
-      <item>
-        <attribute name="label" translatable="yes">_New Window</attribute>
-        <attribute name="action">app.new</attribute>
-      </item>
-    </section>
-    <section>
-      <item>
-        <attribute name="label" translatable="yes">_About Bloatpad</attribute>
-        <attribute name="hidden-when">macos-menubar</attribute>
-        <attribute name="action">app.about</attribute>
-      </item>
-    </section>
-    <section>
-      <item>
-        <attribute name="label" translatable="yes">_Quit</attribute>
-        <attribute name="hidden-when">macos-menubar</attribute>
-        <attribute name="action">app.quit</attribute>
-      </item>
-    </section>
-  </menu>
   <menu id="menubar">
+    <submenu>
+      <attribute name="label" translatable="yes">_BloatPad</attribute>
+      <section>
+        <item>
+          <attribute name="label" translatable="yes">_New Window</attribute>
+          <attribute name="action">app.new</attribute>
+        </item>
+      </section>
+      <section>
+        <item>
+          <attribute name="label" translatable="yes">_About Bloatpad</attribute>
+          <attribute name="hidden-when">macos-menubar</attribute>
+          <attribute name="action">app.about</attribute>
+        </item>
+      </section>
+      <section>
+        <item>
+          <attribute name="label" translatable="yes">_Quit</attribute>
+          <attribute name="hidden-when">macos-menubar</attribute>
+          <attribute name="action">app.quit</attribute>
+        </item>
+      </section>
+    </submenu>
     <submenu>
       <attribute name="label" translatable="yes">_Edit</attribute>
       <section>
diff --git a/examples/plugman.c b/examples/plugman.c
index 0e0882fcb5..2b6ca22188 100644
--- a/examples/plugman.c
+++ b/examples/plugman.c
@@ -76,6 +76,8 @@ new_window (GApplication *app,
             GFile        *file)
 {
   GtkWidget *window, *grid, *scrolled, *view;
+  GtkWidget *menubar;
+  GMenuModel *model;
 
   window = gtk_application_window_new (GTK_APPLICATION (app));
   gtk_window_set_default_size ((GtkWindow*)window, 640, 480);
@@ -85,6 +87,10 @@ new_window (GApplication *app,
   grid = gtk_grid_new ();
   gtk_window_set_child (GTK_WINDOW (window), grid);
 
+  model = G_MENU_MODEL (g_object_get_data (G_OBJECT (app), "menubar"));
+  menubar = gtk_popover_menu_bar_new_from_model (model);
+  gtk_grid_attach (GTK_GRID (grid), menubar, 0, -1, 1, 1);
+
   scrolled = gtk_scrolled_window_new (NULL, NULL);
   gtk_widget_set_hexpand (scrolled, TRUE);
   gtk_widget_set_vexpand (scrolled, TRUE);
@@ -401,31 +407,31 @@ plug_man_startup (GApplication *application)
 {
   GtkBuilder *builder;
 
-  G_APPLICATION_CLASS (plug_man_parent_class)
-    ->startup (application);
+  G_APPLICATION_CLASS (plug_man_parent_class)->startup (application);
 
   g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), 
application);
 
   builder = gtk_builder_new ();
   gtk_builder_add_from_string (builder,
                                "<interface>"
-                               "  <menu id='app-menu'>"
-                               "    <section>"
-                               "      <item>"
-                               "        <attribute name='label' translatable='yes'>_About 
Plugman</attribute>"
-                               "        <attribute name='action'>app.about</attribute>"
-                               "      </item>"
-                               "    </section>"
-                               "    <section>"
-                               "      <item>"
-                               "        <attribute name='label' translatable='yes'>_Quit</attribute>"
-                               "        <attribute name='action'>app.quit</attribute>"
-                               "        <attribute name='accel'>&lt;Primary&gt;q</attribute>"
-                               "      </item>"
-                               "    </section>"
-                               "  </menu>"
                                "  <menu id='menubar'>"
                                "    <submenu>"
+                               "      <attribute name='label' translatable='yes'>Plugman</attribute>"
+                               "      <section>"
+                               "        <item>"
+                               "          <attribute name='label' translatable='yes'>_About 
Plugman</attribute>"
+                               "          <attribute name='action'>app.about</attribute>"
+                               "        </item>"
+                               "      </section>"
+                               "      <section>"
+                               "        <item>"
+                               "          <attribute name='label' translatable='yes'>_Quit</attribute>"
+                               "          <attribute name='action'>app.quit</attribute>"
+                               "          <attribute name='accel'>&lt;Primary&gt;q</attribute>"
+                               "        </item>"
+                               "      </section>"
+                               "    </submenu>"
+                               "    <submenu>"
                                "      <attribute name='label' translatable='yes'>_Edit</attribute>"
                                "      <section>"
                                "        <item>"
@@ -457,9 +463,8 @@ plug_man_startup (GApplication *application)
                                "    </submenu>"
                                "  </menu>"
                                "</interface>", -1, NULL);
-  gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object 
(builder, "app-menu")));
-  gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, 
"menubar")));
-  g_object_set_data_full (G_OBJECT (application), "plugin-menu", gtk_builder_get_object (builder, 
"plugins"), g_object_unref);
+  g_object_set_data_full (G_OBJECT (application), "menubar", g_object_ref (gtk_builder_get_object (builder, 
"menubar")), g_object_unref);
+  g_object_set_data_full (G_OBJECT (application), "plugin-menu", g_object_ref (gtk_builder_get_object 
(builder, "plugins")), g_object_unref);
   g_object_unref (builder);
 }
 
diff --git a/examples/sunny.c b/examples/sunny.c
index 1bb3284eeb..9ca7d57c6f 100644
--- a/examples/sunny.c
+++ b/examples/sunny.c
@@ -7,9 +7,10 @@ new_window (GApplication *app,
 {
   GtkWidget *window, *scrolled, *view, *overlay;
   GtkWidget *header;
+  GMenuModel *model;
+  GtkWidget *menu;
 
   window = gtk_application_window_new (GTK_APPLICATION (app));
-  gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (window), FALSE);
   gtk_window_set_default_size ((GtkWindow*)window, 640, 480);
   gtk_window_set_title (GTK_WINDOW (window), "Sunny");
   gtk_window_set_icon_name (GTK_WINDOW (window), "sunny");
@@ -18,6 +19,11 @@ new_window (GApplication *app,
   gtk_header_bar_set_show_title_buttons (GTK_HEADER_BAR (header), TRUE);
   gtk_window_set_titlebar (GTK_WINDOW (window), header);
 
+  model = G_MENU_MODEL (g_object_get_data (G_OBJECT (app), "menu"));
+  menu = gtk_menu_button_new ();
+  gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menu), model);
+  gtk_header_bar_pack_start (GTK_HEADER_BAR (header), menu);
+
   overlay = gtk_overlay_new ();
   gtk_window_set_child (GTK_WINDOW (window), overlay);
 
@@ -131,10 +137,6 @@ startup (GApplication *application)
   G_APPLICATION_CLASS (menu_button_parent_class)->startup (application);
 
   g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), 
application);
-
-  if (g_getenv ("APP_MENU_FALLBACK"))
-    g_object_set (gtk_settings_get_default (), "gtk-shell-shows-app-menu", FALSE, NULL);
- 
   builder = gtk_builder_new ();
   gtk_builder_add_from_string (builder,
                                "<interface>"
@@ -156,7 +158,9 @@ startup (GApplication *application)
                                "    </section>"
                                "  </menu>"
                                "</interface>", -1, NULL);
-  gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object 
(builder, "app-menu")));
+  g_object_set_data_full (G_OBJECT (application), "menu",
+                          g_object_ref (gtk_builder_get_object (builder, "app-menu")),
+                          g_object_unref);
   g_object_unref (builder);
 }
 


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