[gtkmm-documentation] application/menubar example: Make the keyboard shortcuts work
- From: Kjell Ahlstedt <kjellahl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtkmm-documentation] application/menubar example: Make the keyboard shortcuts work
- Date: Mon, 28 Mar 2022 16:13:07 +0000 (UTC)
commit 58116667c83a72c0f5b8bade56187c5e5cc98194
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date: Mon Mar 28 17:18:07 2022 +0200
application/menubar example: Make the keyboard shortcuts work
and avoid the Gtk-CRITICAL message:
gtk_widget_child_focus: assertion 'GTK_IS_WIDGET (widget)' failed
by adding a child widget to the ExampleWindow.
examples/book/application/menubar/exampleapplication.cc | 17 ++++++++---------
examples/book/application/menubar/examplewindow.cc | 5 +++++
examples/book/application/menubar/examplewindow.h | 2 ++
3 files changed, 15 insertions(+), 9 deletions(-)
---
diff --git a/examples/book/application/menubar/exampleapplication.cc
b/examples/book/application/menubar/exampleapplication.cc
index 10a82dc..612295a 100644
--- a/examples/book/application/menubar/exampleapplication.cc
+++ b/examples/book/application/menubar/exampleapplication.cc
@@ -52,14 +52,8 @@ void ExampleApplication::on_startup()
auto submenu_edit = Gio::Menu::create();
submenu_edit->append("_Cut", "win.cut");
- auto item = Gio::MenuItem::create("_Copy", "win.copy");
- //Setting "accel" works, but might be deprecated soon: See
https://bugzilla.gnome.org/show_bug.cgi?id=708908
- //When it is deprecated, use Gtk::Application::set_accel_for_action() instead.
- item->set_attribute_value("accel", Glib::Variant<Glib::ustring>::create("<Primary>c"));
- submenu_edit->append_item(item);
- item = Gio::MenuItem::create("_Paste", "win.paste");
- item->set_attribute_value("accel", Glib::Variant<Glib::ustring>::create("<Primary>v"));
- submenu_edit->append_item(item);
+ submenu_edit->append("_Copy", "win.copy");
+ submenu_edit->append("_Paste", "win.paste");
win_menu->append_submenu("Edit", submenu_edit);
auto submenu_notification = Gio::Menu::create();
@@ -69,6 +63,12 @@ void ExampleApplication::on_startup()
set_menubar(win_menu);
+ // Set accelerator keys:
+ set_accel_for_action("app.quit", "<Primary>q");
+ set_accel_for_action("win.cut", "<Primary>x");
+ set_accel_for_action("win.copy", "<Primary>c");
+ set_accel_for_action("win.paste", "<Primary>v");
+
//Create an action with a parameter. This action can be activated from
//a Gio::Notification, sent by the Notification/win.send-note menu selection.
auto action_print =
@@ -81,7 +81,6 @@ void ExampleApplication::on_startup()
void ExampleApplication::create_window()
{
auto window = new ExampleWindow();
- window->set_default_size(300, 100);
//Make sure that the application runs for as long this window is still open:
add_window(*window);
diff --git a/examples/book/application/menubar/examplewindow.cc
b/examples/book/application/menubar/examplewindow.cc
index eadc758..9f80fa7 100644
--- a/examples/book/application/menubar/examplewindow.cc
+++ b/examples/book/application/menubar/examplewindow.cc
@@ -21,6 +21,11 @@
ExampleWindow::ExampleWindow()
{
set_title("Gtk::Application example");
+ set_default_size(350, 100);
+
+ // Some GTK functions can misbehave if a window does not have a child.
+ // An empty Box is acceptable.
+ set_child(m_box);
// We can use add_action because Gtk::ApplicationWindow derives from ActionMap.
// This Action Map uses a "win." prefix for the actions.
diff --git a/examples/book/application/menubar/examplewindow.h
b/examples/book/application/menubar/examplewindow.h
index a982ce1..f7142a2 100644
--- a/examples/book/application/menubar/examplewindow.h
+++ b/examples/book/application/menubar/examplewindow.h
@@ -29,6 +29,8 @@ private:
void on_action_close();
void on_action_send_notification();
void on_action_withdraw_notification();
+
+ Gtk::Box m_box;
};
#endif /* GTKMM_EXAMPLEWINDOW_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]