[gtkmm-documentation] Keyboard event propagation example: Use in-class initializers



commit 813151aaba21b9c2af26707737a5c030f038e63c
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Sun Jul 17 15:44:59 2022 +0200

    Keyboard event propagation example: Use in-class initializers
    
    and other minor changes.

 .../keyboard_events/propagation/examplewindow.cc   | 26 +++++++++-------------
 .../keyboard_events/propagation/examplewindow.h    | 12 +++++-----
 2 files changed, 16 insertions(+), 22 deletions(-)
---
diff --git a/examples/book/keyboard_events/propagation/examplewindow.cc 
b/examples/book/keyboard_events/propagation/examplewindow.cc
index eb637b3..db4a126 100644
--- a/examples/book/keyboard_events/propagation/examplewindow.cc
+++ b/examples/book/keyboard_events/propagation/examplewindow.cc
@@ -15,18 +15,13 @@
  */
 
 // In gtkmm3, this example used an Entry instead of m_label2. This would not work
-// as intended in gtkmm4. GtkEntry in gtk+4 marks a key press signal as handled
-// in the bubble phase. It's not propagated further up to the Grid and the Window.
+// as intended in gtkmm4. GtkEntry in gtk4 marks a key press signal as handled
+// in the target phase. It's not propagated further up to the Box and the Window.
 
 #include "examplewindow.h"
 #include <iostream>
 
 ExampleWindow::ExampleWindow()
-:
-m_label1("A label"),
-m_label2("Write here"),
-m_checkbutton_can_propagate_down("Can propagate down"),
-m_checkbutton_can_propagate_up("Can propagate up")
 {
   set_title("Event Propagation");
   m_container.set_margin(10);
@@ -44,7 +39,7 @@ m_checkbutton_can_propagate_up("Can propagate up")
   m_container.append(m_checkbutton_can_propagate_down);
   m_container.append(m_checkbutton_can_propagate_up);
 
-  // Events
+  // Event controllers
   const bool after = false; // Run before or after the default signal handlers.
 
   // Called in the capture phase of the event handling.
@@ -57,7 +52,7 @@ m_checkbutton_can_propagate_up("Can propagate up")
   controller = Gtk::EventControllerKey::create();
   controller->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
   controller->signal_key_pressed().connect(
-    sigc::bind(sigc::mem_fun(*this, &ExampleWindow::grid_key_pressed), "capture"), after);
+    sigc::bind(sigc::mem_fun(*this, &ExampleWindow::box_key_pressed), "capture"), after);
   m_container.add_controller(controller);
 
   controller = Gtk::EventControllerKey::create();
@@ -76,7 +71,7 @@ m_checkbutton_can_propagate_up("Can propagate up")
   controller = Gtk::EventControllerKey::create();
   controller->set_propagation_phase(Gtk::PropagationPhase::TARGET);
   controller->signal_key_pressed().connect(
-    sigc::bind(sigc::mem_fun(*this, &ExampleWindow::grid_key_pressed), "target"), after);
+    sigc::bind(sigc::mem_fun(*this, &ExampleWindow::box_key_pressed), "target"), after);
   m_container.add_controller(controller);
 
   controller = Gtk::EventControllerKey::create();
@@ -96,7 +91,7 @@ m_checkbutton_can_propagate_up("Can propagate up")
   controller = Gtk::EventControllerKey::create();
   controller->set_propagation_phase(Gtk::PropagationPhase::BUBBLE);
   controller->signal_key_pressed().connect(
-    sigc::bind(sigc::mem_fun(*this, &ExampleWindow::grid_key_pressed), "bubble"), after);
+    sigc::bind(sigc::mem_fun(*this, &ExampleWindow::box_key_pressed), "bubble"), after);
   m_container.add_controller(controller);
 
   controller = Gtk::EventControllerKey::create();
@@ -136,9 +131,9 @@ bool ExampleWindow::label2_key_pressed(guint keyval, guint, Gdk::ModifierType, c
   return false;
 }
 
-bool ExampleWindow::grid_key_pressed(guint, guint, Gdk::ModifierType, const Glib::ustring& phase)
+bool ExampleWindow::box_key_pressed(guint, guint, Gdk::ModifierType, const Glib::ustring& phase)
 {
-  std::cout << "Grid,   " << phase << " phase" << std::endl;
+  std::cout << "Box,    " << phase << " phase" << std::endl;
 
   // Let it propagate
   return false;
@@ -156,8 +151,8 @@ bool ExampleWindow::window_key_pressed(guint, guint, Gdk::ModifierType, const Gl
   // even if m_checkbutton_can_propagate_up wasn't active.
   if (phase == "bubble" && m_label2.has_focus())
   {
-    m_label1.set_text(m_label2.get_text());
-    std::cout << ", " << m_label2.get_text();
+    m_label1.set_label(m_label2.get_label());
+    std::cout << ", " << m_label2.get_label();
   }
   std::cout << std::endl;
 
@@ -169,4 +164,3 @@ bool ExampleWindow::window_key_pressed(guint, guint, Gdk::ModifierType, const Gl
 ExampleWindow::~ExampleWindow()
 {
 }
-
diff --git a/examples/book/keyboard_events/propagation/examplewindow.h 
b/examples/book/keyboard_events/propagation/examplewindow.h
index 2c8b51e..aa8a4ca 100644
--- a/examples/book/keyboard_events/propagation/examplewindow.h
+++ b/examples/book/keyboard_events/propagation/examplewindow.h
@@ -24,21 +24,21 @@ class ExampleWindow : public Gtk::Window
 public:
 
   ExampleWindow();
-  virtual ~ExampleWindow();
+  ~ExampleWindow() override;
 
 private:
   // Signal handlers:
   bool label2_key_pressed(guint keyval, guint keycode, Gdk::ModifierType state, const Glib::ustring& phase);
-  bool grid_key_pressed(guint keyval, guint keycode, Gdk::ModifierType state, const Glib::ustring& phase);
+  bool box_key_pressed(guint keyval, guint keycode, Gdk::ModifierType state, const Glib::ustring& phase);
   bool window_key_pressed(guint keyval, guint keycode, Gdk::ModifierType state, const Glib::ustring& phase);
 
   bool m_first = true;
   Gtk::Box m_container;
   Gtk::Frame m_frame;
-  Gtk::Label m_label1;
-  Gtk::Label m_label2;
-  Gtk::CheckButton m_checkbutton_can_propagate_down;
-  Gtk::CheckButton m_checkbutton_can_propagate_up;
+  Gtk::Label m_label1 {"A label"};
+  Gtk::Label m_label2 {"Write here"};
+  Gtk::CheckButton m_checkbutton_can_propagate_down {"Can propagate down in the capture phase"};
+  Gtk::CheckButton m_checkbutton_can_propagate_up {"Can propagate up in the bubble phase"};
 };
 
 #endif //GTKMM_EVENT_PROPAGATION_H


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