[gtkmm-documentation] examples: Update to latest gtkmm4 (removed Gtk::Menu, etc.)



commit 4fae615007251a563ab65b0a99e06df396c68b14
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Sun Jan 12 16:32:08 2020 +0100

    examples: Update to latest gtkmm4 (removed Gtk::Menu, etc.)
    
    Use Gtk::PopoverMenu and Gtk::PopoverMenuBar instead of the removed
    Gtk::Menu and Gtk::MenuBar.

 examples/book/menus/popup/examplewindow.cc         | 27 ++++++++-------
 examples/book/menus/popup/examplewindow.h          |  5 ++-
 examples/book/menus/popup/main.cc                  |  2 +-
 examples/book/menus_and_toolbars/examplewindow.cc  |  9 ++---
 examples/book/printing/advanced/examplewindow.cc   |  4 +--
 examples/book/printing/simple/examplewindow.cc     |  7 ++--
 examples/book/recent_files/examplewindow.cc        |  4 +--
 examples/book/tooltips/examplewindow.cc            | 30 ++++++++---------
 examples/book/tooltips/examplewindow.h             |  6 +---
 examples/book/treeview/popup/treeview_withpopup.cc | 38 +++++++++++++---------
 examples/book/treeview/popup/treeview_withpopup.h  |  2 +-
 .../others/cellrenderercustom/cellrendererpopup.cc |  7 ++--
 12 files changed, 67 insertions(+), 74 deletions(-)
---
diff --git a/examples/book/menus/popup/examplewindow.cc b/examples/book/menus/popup/examplewindow.cc
index 1420295..d6549fa 100644
--- a/examples/book/menus/popup/examplewindow.cc
+++ b/examples/book/menus/popup/examplewindow.cc
@@ -17,10 +17,10 @@
 #include "examplewindow.h"
 #include <iostream>
 
-ExampleWindow::ExampleWindow()
+ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
 : m_Box(Gtk::Orientation::VERTICAL),
   m_Label("Right-click to see the popup menu."),
-  m_pMenuPopup(nullptr)
+  m_MenuPopup(m_Label)
 {
   set_title("popup example");
   set_default_size(200, 200);
@@ -37,9 +37,6 @@ ExampleWindow::ExampleWindow()
   m_Label.add_controller(m_refGesture);
 
   //Create actions:
-
-  //Fill menu:
-
   auto refActionGroup = Gio::SimpleActionGroup::create();
 
   //File|New sub menu:
@@ -50,7 +47,7 @@ ExampleWindow::ExampleWindow()
   refActionGroup->add_action("edit",
     sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
 
-  refActionGroup->add_action("process", //TODO: How to specify "<control>P" as an accelerator.
+  refActionGroup->add_action("process",
     sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
 
   refActionGroup->add_action("remove",
@@ -58,6 +55,10 @@ ExampleWindow::ExampleWindow()
 
   insert_action_group("examplepopup", refActionGroup);
 
+  // Set accelerator keys:
+  app->set_accel_for_action("examplepopup.edit", "<Primary>e");
+  app->set_accel_for_action("examplepopup.process", "<Primary>p");
+  app->set_accel_for_action("examplepopup.remove", "<Primary>r");
 
   m_refBuilder = Gtk::Builder::create();
 
@@ -99,7 +100,8 @@ ExampleWindow::ExampleWindow()
   if(!gmenu)
     g_warning("GMenu not found");
 
-  m_pMenuPopup = std::make_unique<Gtk::Menu>(gmenu);
+  m_MenuPopup.set_menu_model(gmenu);
+  m_MenuPopup.set_has_arrow(false);
 }
 
 ExampleWindow::~ExampleWindow()
@@ -111,12 +113,9 @@ void ExampleWindow::on_menu_file_popup_generic()
    std::cout << "A popup menu item was selected." << std::endl;
 }
 
-void ExampleWindow::on_label_pressed(int /* n_press */, double /* x */, double /* y */)
+void ExampleWindow::on_label_pressed(int /* n_press */, double x, double y)
 {
-  if (m_pMenuPopup && !m_pMenuPopup->get_attach_widget())
-    m_pMenuPopup->attach_to_widget(*this);
-
-  if (m_pMenuPopup)
-    m_pMenuPopup->popup_at_pointer();
+  const Gdk::Rectangle rect(x, y, 1, 1);
+  m_MenuPopup.set_pointing_to(rect);
+  m_MenuPopup.popup();
 }
-
diff --git a/examples/book/menus/popup/examplewindow.h b/examples/book/menus/popup/examplewindow.h
index 23769eb..a948550 100644
--- a/examples/book/menus/popup/examplewindow.h
+++ b/examples/book/menus/popup/examplewindow.h
@@ -24,7 +24,7 @@
 class ExampleWindow : public Gtk::Window
 {
 public:
-  ExampleWindow();
+  ExampleWindow(const Glib::RefPtr<Gtk::Application>& app);
   virtual ~ExampleWindow();
 
 protected:
@@ -36,11 +36,10 @@ protected:
   //Child widgets:
   Gtk::Box m_Box;
   Gtk::Label m_Label;
+  Gtk::PopoverMenu m_MenuPopup;
 
   Glib::RefPtr<Gtk::Builder> m_refBuilder;
   Glib::RefPtr<Gtk::GestureClick> m_refGesture;
-
-  std::unique_ptr<Gtk::Menu> m_pMenuPopup;
 };
 
 #endif //GTKMM_EXAMPLEWINDOW_H
diff --git a/examples/book/menus/popup/main.cc b/examples/book/menus/popup/main.cc
index 8a8594a..6eeaea6 100644
--- a/examples/book/menus/popup/main.cc
+++ b/examples/book/menus/popup/main.cc
@@ -21,7 +21,7 @@ int main(int argc, char *argv[])
 {
   auto app = Gtk::Application::create("org.gtkmm.example");
 
-  ExampleWindow window;
+  ExampleWindow window(app);
 
   //Shows the window and returns when it is closed.
   return app->run(window, argc, argv);
diff --git a/examples/book/menus_and_toolbars/examplewindow.cc 
b/examples/book/menus_and_toolbars/examplewindow.cc
index dcfc441..65352c7 100644
--- a/examples/book/menus_and_toolbars/examplewindow.cc
+++ b/examples/book/menus_and_toolbars/examplewindow.cc
@@ -34,10 +34,8 @@ ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
   m_refActionGroup->add_action("open",
     sigc::mem_fun(*this, &ExampleWindow::on_action_others) );
 
-
   m_refActionRain = m_refActionGroup->add_action_bool("rain",
-    sigc::mem_fun(*this, &ExampleWindow::on_action_toggle),
-    false);
+    sigc::mem_fun(*this, &ExampleWindow::on_action_toggle), false);
 
   m_refActionGroup->add_action("quit",
     sigc::mem_fun(*this, &ExampleWindow::on_action_file_quit) );
@@ -111,7 +109,6 @@ ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
   // automatically fetched from the Gio::Menu.
   // See the examples/book/menus/main_menu example for an alternative way of
   // adding the menubar when using Gtk::ApplicationWindow.
-  // Gtk::Application::set_accel_for_action() is new in gtkmm 3.11.9.
   app->set_accel_for_action("example.new", "<Primary>n");
   app->set_accel_for_action("example.open", "<Primary>o");
   app->set_accel_for_action("example.quit", "<Primary>q");
@@ -136,9 +133,9 @@ ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
     g_warning("GMenu not found");
   else
   {
-    auto pMenuBar = Gtk::make_managed<Gtk::MenuBar>(gmenu);
+    auto pMenuBar = Gtk::make_managed<Gtk::PopoverMenuBar>(gmenu);
 
-    //Add the MenuBar to the window:
+    //Add the PopoverMenuBar to the window:
     m_Box.add(*pMenuBar);
   }
 
diff --git a/examples/book/printing/advanced/examplewindow.cc 
b/examples/book/printing/advanced/examplewindow.cc
index b20d80a..159a24f 100644
--- a/examples/book/printing/advanced/examplewindow.cc
+++ b/examples/book/printing/advanced/examplewindow.cc
@@ -214,9 +214,9 @@ void ExampleWindow::build_main_menu(const Glib::RefPtr<Gtk::Application>& app)
     g_warning("GMenu not found");
   else
   {
-    auto pMenuBar = Gtk::make_managed<Gtk::MenuBar>(gmenu);
+    auto pMenuBar = Gtk::make_managed<Gtk::PopoverMenuBar>(gmenu);
 
-    // Add the MenuBar to the window:
+    // Add the PopoverMenuBar to the window:
     m_VBox.add(*pMenuBar);
   }
 
diff --git a/examples/book/printing/simple/examplewindow.cc b/examples/book/printing/simple/examplewindow.cc
index 03cad7e..159a24f 100644
--- a/examples/book/printing/simple/examplewindow.cc
+++ b/examples/book/printing/simple/examplewindow.cc
@@ -60,8 +60,7 @@ ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
 
   m_Grid.attach(m_CommentsLabel, 0, 2);
   m_Grid.attach(m_ScrolledWindow, 1, 2);
-  m_ScrolledWindow.set_hexpand(true);
-  m_ScrolledWindow.set_vexpand(true);
+  m_ScrolledWindow.set_expand(true);
 
   m_refTextBuffer = Gtk::TextBuffer::create();
   m_TextView.set_buffer(m_refTextBuffer);
@@ -215,9 +214,9 @@ void ExampleWindow::build_main_menu(const Glib::RefPtr<Gtk::Application>& app)
     g_warning("GMenu not found");
   else
   {
-    auto pMenuBar = Gtk::make_managed<Gtk::MenuBar>(gmenu);
+    auto pMenuBar = Gtk::make_managed<Gtk::PopoverMenuBar>(gmenu);
 
-    // Add the MenuBar to the window:
+    // Add the PopoverMenuBar to the window:
     m_VBox.add(*pMenuBar);
   }
 
diff --git a/examples/book/recent_files/examplewindow.cc b/examples/book/recent_files/examplewindow.cc
index 51b482b..354e8f6 100644
--- a/examples/book/recent_files/examplewindow.cc
+++ b/examples/book/recent_files/examplewindow.cc
@@ -24,7 +24,7 @@ ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
   set_title("Recent files example");
   set_default_size(300, 150);
 
-  //We can put a MenuBar at the top of the box and other stuff below it.
+  //We can put a PopoverMenuBar at the top of the box and other stuff below it.
   add(m_Box);
 
   //Create actions for menus and toolbars:
@@ -117,7 +117,7 @@ ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
   if (gmenu)
   {
     //Menubar:
-    auto pMenubar = Gtk::make_managed<Gtk::MenuBar>(gmenu);
+    auto pMenubar = Gtk::make_managed<Gtk::PopoverMenuBar>(gmenu);
     m_Box.add(*pMenubar);
   }
   else
diff --git a/examples/book/tooltips/examplewindow.cc b/examples/book/tooltips/examplewindow.cc
index b15538a..621f456 100644
--- a/examples/book/tooltips/examplewindow.cc
+++ b/examples/book/tooltips/examplewindow.cc
@@ -15,7 +15,6 @@
  */
 
 #include "examplewindow.h"
-
 #include <vector>
 
 const Glib::ustring app_title = "gtkmm tooltips example";
@@ -27,8 +26,7 @@ ExampleWindow::ExampleWindow()
   m_vbox(Gtk::Orientation::VERTICAL, 3),
   m_checkbutton("Click to alternate markup in tooltip"),
   m_label("A label"),
-  m_button("Custom widget in tooltip window"),
-  m_button_tooltip_window(Gtk::WindowType::POPUP)
+  m_button("Button with a custom tooltip widget")
 {
   //Set up window and the top-level container:
   set_title(app_title);
@@ -48,16 +46,15 @@ ExampleWindow::ExampleWindow()
   prepare_textview();
 
   //Button:
-  // set_tooltip_window(), like set_tooltip_text(),
-  // will call set_has_tooltip() for us.
-  m_button.set_tooltip_window(m_button_tooltip_window);
+  //When only connecting to the query-tooltip signal, and not using any
+  //of set_tooltip_text() or set_tooltip_markup(), we need to explicitly
+  //tell GTK that the widget has a tooltip which we'll show.
+  m_button.set_has_tooltip();
   m_vbox.add(m_button);
 
-  //Button's custom tooltip window:
-  m_button_tooltip_window.set_default_size(250, 30);
-  auto label = Gtk::make_managed<Gtk::Label>("A label in a custom tooltip window");
-  label->show();
-  m_button_tooltip_window.add(*label);
+  //Button's custom tooltip widget:
+  auto label = Gtk::make_managed<Gtk::Label>("A label in a custom tooltip widget");
+  m_button_tooltip_widget.add(*label);
 
   connect_signals();
 }
@@ -69,7 +66,7 @@ ExampleWindow::~ExampleWindow()
 void ExampleWindow::prepare_textview()
 {
   Gtk::TextIter iter;
-  std::vector< Glib::RefPtr<Gtk::TextTag> > tags;
+  std::vector<Glib::RefPtr<Gtk::TextTag>> tags;
 
   //Set up a scrolled window:
   m_scrolled_window.add(m_text_view);
@@ -102,9 +99,8 @@ void ExampleWindow::prepare_textview()
   m_text_view.set_size_request(320, 50);
 
   //When only connecting to the query-tooltip signal, and not using any
-  //of set_tooltip_text(), set_tooltip_markup() or set_tooltip_window(),
-  //we need to explicitly tell GTK+ that the widget has a tooltip which
-  //we'll show.
+  //of set_tooltip_text() or set_tooltip_markup(), we need to explicitly
+  //tell GTK that the widget has a tooltip which we'll show.
   m_text_view.set_has_tooltip();
 }
 
@@ -164,8 +160,8 @@ bool ExampleWindow::on_textview_query_tooltip(int x, int y, bool keyboard_toolti
   return true;
 }
 
-bool ExampleWindow::on_button_query_tooltip(int, int, bool, const Glib::RefPtr<Gtk::Tooltip>&)
+bool ExampleWindow::on_button_query_tooltip(int, int, bool, const Glib::RefPtr<Gtk::Tooltip>& tooltip)
 {
-  //We already have a custom window ready, just return true to show it:
+  tooltip->set_custom(m_button_tooltip_widget);
   return true;
 }
diff --git a/examples/book/tooltips/examplewindow.h b/examples/book/tooltips/examplewindow.h
index 33bf3e2..5ab8021 100644
--- a/examples/book/tooltips/examplewindow.h
+++ b/examples/book/tooltips/examplewindow.h
@@ -1,5 +1,3 @@
-/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-
 /* gtkmm example Copyright (C) 2007 gtkmm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -51,9 +49,7 @@ protected:
   Glib::RefPtr<Gtk::TextTag> m_ref_bold_tag;
 
   Gtk::Button m_button;
-  Gtk::Window m_button_tooltip_window;
-
+  Gtk::Box m_button_tooltip_widget;
 };
 
 #endif // GTKMM_EXAMPLEWINDOW_H
-
diff --git a/examples/book/treeview/popup/treeview_withpopup.cc 
b/examples/book/treeview/popup/treeview_withpopup.cc
index 96c6a1d..972eb3c 100644
--- a/examples/book/treeview/popup/treeview_withpopup.cc
+++ b/examples/book/treeview/popup/treeview_withpopup.cc
@@ -18,6 +18,7 @@
 #include <iostream>
 
 TreeView_WithPopup::TreeView_WithPopup()
+: m_MenuPopup(*this)
 {
   //Create the Tree model:
   m_refTreeModel = Gtk::ListStore::create(m_Columns);
@@ -47,32 +48,37 @@ TreeView_WithPopup::TreeView_WithPopup()
     sigc::mem_fun(*this, &TreeView_WithPopup::on_popup_button_pressed));
   add_controller(refGesture);
 
-  //Fill popup menu:
-  auto item = Gtk::make_managed<Gtk::MenuItem>("_Edit", true);
-  item->signal_activate().connect(
-    sigc::mem_fun(*this, &TreeView_WithPopup::on_menu_file_popup_generic) );
-  m_Menu_Popup.append(*item);
+  // Fill popup menu:
+  auto gmenu = Gio::Menu::create();
+  gmenu->append("_Edit", "popup.edit");
+  gmenu->append("_Process", "popup.process");
+  gmenu->append("_Remove", "popup.remove");
 
-  item = Gtk::make_managed<Gtk::MenuItem>("_Process", true);
-  item->signal_activate().connect(
-    sigc::mem_fun(*this, &TreeView_WithPopup::on_menu_file_popup_generic) );
-  m_Menu_Popup.append(*item);
+  m_MenuPopup.set_menu_model(gmenu);
+  m_MenuPopup.set_has_arrow(false);
 
-  item = Gtk::make_managed<Gtk::MenuItem>("_Remove", true);
-  item->signal_activate().connect(
-    sigc::mem_fun(*this, &TreeView_WithPopup::on_menu_file_popup_generic) );
-  m_Menu_Popup.append(*item);
+  // Create actions:
+  auto refActionGroup = Gio::SimpleActionGroup::create();
 
-  m_Menu_Popup.accelerate(*this);
+  refActionGroup->add_action("edit",
+    sigc::mem_fun(*this, &TreeView_WithPopup::on_menu_file_popup_generic));
+  refActionGroup->add_action("process",
+    sigc::mem_fun(*this, &TreeView_WithPopup::on_menu_file_popup_generic));
+  refActionGroup->add_action("remove",
+    sigc::mem_fun(*this, &TreeView_WithPopup::on_menu_file_popup_generic));
+
+  insert_action_group("popup", refActionGroup);
 }
 
 TreeView_WithPopup::~TreeView_WithPopup()
 {
 }
 
-void TreeView_WithPopup::on_popup_button_pressed(int /* n_press */, double /* x */, double /* y */)
+void TreeView_WithPopup::on_popup_button_pressed(int /* n_press */, double x, double y)
 {
-  m_Menu_Popup.popup_at_pointer();
+  const Gdk::Rectangle rect(x, y, 1, 1);
+  m_MenuPopup.set_pointing_to(rect);
+  m_MenuPopup.popup();
 }
 
 void TreeView_WithPopup::on_menu_file_popup_generic()
diff --git a/examples/book/treeview/popup/treeview_withpopup.h 
b/examples/book/treeview/popup/treeview_withpopup.h
index 5d5b057..ab04297 100644
--- a/examples/book/treeview/popup/treeview_withpopup.h
+++ b/examples/book/treeview/popup/treeview_withpopup.h
@@ -50,7 +50,7 @@ protected:
   //The Tree model:
   Glib::RefPtr<Gtk::ListStore> m_refTreeModel;
 
-  Gtk::Menu m_Menu_Popup;
+  Gtk::PopoverMenu m_MenuPopup;
 };
 
 #endif //GTKMM_EXAMPLE_TREEVIEW_WITHPOPUP_H
diff --git a/examples/others/cellrenderercustom/cellrendererpopup.cc 
b/examples/others/cellrenderercustom/cellrendererpopup.cc
index b86e1d6..b977a60 100644
--- a/examples/others/cellrenderercustom/cellrendererpopup.cc
+++ b/examples/others/cellrenderercustom/cellrendererpopup.cc
@@ -23,15 +23,16 @@
 namespace
 {
 
-bool grab_on_window(const Glib::RefPtr<Gdk::Surface>& surface)
+bool grab_on_window(const Glib::RefPtr<Gdk::Surface>& /* surface */)
 {
   Glib::RefPtr<Gdk::Device> device (Glib::wrap(gtk_get_current_event_device(), true));
 
   if(device)
   {
     auto seat = device->get_seat();
-    if (seat &&
-        seat->grab(surface, Gdk::Seat::Capabilities::ALL, true) == Gdk::GrabStatus::SUCCESS)
+//    if (seat &&
+//        seat->grab(surface, Gdk::Seat::Capabilities::ALL, true) == Gdk::GrabStatus::SUCCESS)
+    if (seat)
       return true;
   }
 


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