[gtkmm-documentation] ComboBox examples: Use Gtk::EventcontrollerFocus



commit eb4f7f61abbf3f94cdb275bc198eba121a5cf48a
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Sat Jul 9 09:13:34 2022 +0200

    ComboBox examples: Use Gtk::EventcontrollerFocus
    
    * examples/book/combobox/entry_complex/examplewindow.[cc|h]:
    * examples/book/combobox/entry_text/examplewindow.[cc|h]:
    Use Gtk::EventcontrollerFocus::signal_leave() to be informed when the
    keyboard focus leaves the Entry widget. Use Gtk::Entry::signal_activate()
    if it exists, i.e. if gtkmm >= 4.8.0.

 .../book/combobox/entry_complex/examplewindow.cc   | 43 ++++++++++++----------
 .../book/combobox/entry_complex/examplewindow.h    | 14 ++++---
 examples/book/combobox/entry_text/examplewindow.cc | 43 +++++++++++-----------
 examples/book/combobox/entry_text/examplewindow.h  | 14 ++++---
 4 files changed, 62 insertions(+), 52 deletions(-)
---
diff --git a/examples/book/combobox/entry_complex/examplewindow.cc 
b/examples/book/combobox/entry_complex/examplewindow.cc
index 6b241d5..f7e9cce 100644
--- a/examples/book/combobox/entry_complex/examplewindow.cc
+++ b/examples/book/combobox/entry_complex/examplewindow.cc
@@ -15,6 +15,7 @@
  */
 
 #include "examplewindow.h"
+#include <gtkmm/eventcontrollerfocus.h>
 #include <iostream>
 
 ExampleWindow::ExampleWindow()
@@ -68,15 +69,21 @@ ExampleWindow::ExampleWindow()
   set_child(m_Combo);
 
   //Connect signal handlers:
-  m_Combo.signal_changed().connect(sigc::mem_fun(*this, &ExampleWindow::on_combo_changed));
-
   auto entry = m_Combo.get_entry();
   if (entry)
   {
+    // Alternatively you can connect to m_Combo.signal_changed().
     entry->signal_changed().connect(sigc::mem_fun(*this,
-      &ExampleWindow::on_entry_changed) );
-    m_ConnectionHasFocusChanged = entry->property_has_focus().signal_changed().
-      connect(sigc::mem_fun(*this, &ExampleWindow::on_entry_has_focus_changed));
+      &ExampleWindow::on_entry_changed));
+#if HAS_SIGNAL_ACTIVATE
+    entry->signal_activate().connect(sigc::mem_fun(*this,
+      &ExampleWindow::on_entry_activate));
+#endif
+    // The Entry shall receive focus-leave events.
+    auto controller = Gtk::EventControllerFocus::create();
+    m_ConnectionFocusLeave = controller->signal_leave().connect(
+      sigc::mem_fun(*this, &ExampleWindow::on_entry_focus_leave));
+    entry->add_controller(controller);
   }
   else
     std::cout << "No Entry ???" << std::endl;
@@ -84,44 +91,40 @@ ExampleWindow::ExampleWindow()
 
 ExampleWindow::~ExampleWindow()
 {
-  // The has_focus changed signal may be emitted while m_Combo is being destructed.
+  // The focus-leave signal may be emitted while m_Combo is being destructed.
   // The signal handler can generate critical messages, if it's called when
   // m_Combo has been partly destructed.
-  m_ConnectionHasFocusChanged.disconnect();
+  m_ConnectionFocusLeave.disconnect();
 }
 
-void ExampleWindow::on_combo_changed()
+void ExampleWindow::on_entry_changed()
 {
   auto entry = m_Combo.get_entry();
   if (entry)
   {
-    std::cout << "on_combo_changed(): Row=" << m_Combo.get_active_row_number()
+    std::cout << "on_entry_changed(): Row=" << m_Combo.get_active_row_number()
       << ", ID=" << entry->get_text() << std::endl;
   }
 }
 
-void ExampleWindow::on_entry_changed()
+#if HAS_SIGNAL_ACTIVATE
+void ExampleWindow::on_entry_activate()
 {
   auto entry = m_Combo.get_entry();
   if (entry)
   {
-    std::cout << "on_entry_changed(): Row=" << m_Combo.get_active_row_number()
+    std::cout << "on_entry_activate(): Row=" << m_Combo.get_active_row_number()
       << ", ID=" << entry->get_text() << std::endl;
   }
 }
+#endif
 
-void ExampleWindow::on_entry_has_focus_changed()
+void ExampleWindow::on_entry_focus_leave()
 {
   auto entry = m_Combo.get_entry();
   if (entry)
   {
-    const bool entry_has_focus = entry->has_focus();
-    if (m_entry_had_focus && !entry_has_focus)
-    {
-      // entry->has_focus() has changed from true to false; entry has lost focus.
-      std::cout << "on_entry_has_focus_changed() to not focused: Row="
-        << m_Combo.get_active_row_number() << ", ID=" << entry->get_text() << std::endl;
-    }
-    m_entry_had_focus = entry_has_focus;
+    std::cout << "on_entry_focus_leave(): Row=" << m_Combo.get_active_row_number()
+      << ", ID=" << entry->get_text() << std::endl;
   }
 }
diff --git a/examples/book/combobox/entry_complex/examplewindow.h 
b/examples/book/combobox/entry_complex/examplewindow.h
index 348cc1d..43f59b3 100644
--- a/examples/book/combobox/entry_complex/examplewindow.h
+++ b/examples/book/combobox/entry_complex/examplewindow.h
@@ -20,22 +20,26 @@
 #include <gtkmm/window.h>
 #include <gtkmm/combobox.h>
 #include <gtkmm/liststore.h>
+#include <gtkmm/version.h>
+
+#define HAS_SIGNAL_ACTIVATE GTKMM_CHECK_VERSION(4,8,0)
 
 class ExampleWindow : public Gtk::Window
 {
 public:
   ExampleWindow();
-  virtual ~ExampleWindow();
+  ~ExampleWindow() override;
 
 protected:
   //Signal handlers:
-  void on_combo_changed();
   void on_entry_changed();
-  void on_entry_has_focus_changed();
+#if HAS_SIGNAL_ACTIVATE
+  void on_entry_activate();
+#endif
+  void on_entry_focus_leave();
 
   //Signal connection:
-  sigc::connection m_ConnectionHasFocusChanged;
-  bool m_entry_had_focus {false};
+  sigc::connection m_ConnectionFocusLeave;
 
   //Tree model columns:
   class ModelColumns : public Gtk::TreeModel::ColumnRecord
diff --git a/examples/book/combobox/entry_text/examplewindow.cc 
b/examples/book/combobox/entry_text/examplewindow.cc
index fb95935..5ed302d 100644
--- a/examples/book/combobox/entry_text/examplewindow.cc
+++ b/examples/book/combobox/entry_text/examplewindow.cc
@@ -15,6 +15,7 @@
  */
 
 #include "examplewindow.h"
+#include <gtkmm/eventcontrollerfocus.h>
 #include <iostream>
 
 ExampleWindow::ExampleWindow()
@@ -31,16 +32,22 @@ ExampleWindow::ExampleWindow()
   set_child(m_Combo);
 
   //Connect signal handlers:
+  auto entry = m_Combo.get_entry();
+  // Alternatively you can connect to entry->signal_changed().
   m_Combo.signal_changed().connect(sigc::mem_fun(*this,
     &ExampleWindow::on_combo_changed) );
 
-  auto entry = m_Combo.get_entry();
   if (entry)
   {
-    entry->signal_changed().connect(sigc::mem_fun(*this,
-      &ExampleWindow::on_entry_changed));
-    m_ConnectionHasFocusChanged = entry->property_has_focus().signal_changed().
-      connect(sigc::mem_fun(*this, &ExampleWindow::on_entry_has_focus_changed));
+#if HAS_SIGNAL_ACTIVATE
+    entry->signal_activate().connect(sigc::mem_fun(*this,
+      &ExampleWindow::on_entry_activate));
+#endif
+    // The Entry shall receive focus-leave events.
+    auto controller = Gtk::EventControllerFocus::create();
+    m_ConnectionFocusLeave = controller->signal_leave().connect(
+      sigc::mem_fun(*this, &ExampleWindow::on_entry_focus_leave));
+    entry->add_controller(controller);
   }
   else
     std::cout << "No Entry ???" << std::endl;
@@ -50,10 +57,10 @@ ExampleWindow::ExampleWindow()
 
 ExampleWindow::~ExampleWindow()
 {
-  // The has_focus changed signal may be emitted while m_Combo is being destructed.
+  // The focus-leave signal may be emitted while m_Combo is being destructed.
   // The signal handler can generate critical messages, if it's called when
   // m_Combo has been partly destructed.
-  m_ConnectionHasFocusChanged.disconnect();
+  m_ConnectionFocusLeave.disconnect();
 }
 
 void ExampleWindow::on_combo_changed()
@@ -62,24 +69,16 @@ void ExampleWindow::on_combo_changed()
     << ", Text=" << m_Combo.get_active_text() << std::endl;
 }
 
-void ExampleWindow::on_entry_changed()
+#if HAS_SIGNAL_ACTIVATE
+void ExampleWindow::on_entry_activate()
 {
-  std::cout << "on_entry_changed(): Row=" << m_Combo.get_active_row_number()
+  std::cout << "on_entry_activate(): Row=" << m_Combo.get_active_row_number()
     << ", Text=" << m_Combo.get_active_text() << std::endl;
 }
+#endif
 
-void ExampleWindow::on_entry_has_focus_changed()
+void ExampleWindow::on_entry_focus_leave()
 {
-  auto entry = m_Combo.get_entry();
-  if (entry)
-  {
-    const bool entry_has_focus = entry->has_focus();
-    if (m_entry_had_focus && !entry_has_focus)
-    {
-      // entry->has_focus() has changed from true to false; entry has lost focus.
-      std::cout << "on_entry_has_focus_changed() to not focused: Row="
-        << m_Combo.get_active_row_number() << ", ID=" << entry->get_text() << std::endl;
-    }
-    m_entry_had_focus = entry_has_focus;
-  }
+  std::cout << "on_entry_focus_leave(): Row=" << m_Combo.get_active_row_number()
+    << ", Text=" << m_Combo.get_active_text() << std::endl;
 }
diff --git a/examples/book/combobox/entry_text/examplewindow.h 
b/examples/book/combobox/entry_text/examplewindow.h
index 1ddc470..06a336c 100644
--- a/examples/book/combobox/entry_text/examplewindow.h
+++ b/examples/book/combobox/entry_text/examplewindow.h
@@ -19,22 +19,26 @@
 
 #include <gtkmm/window.h>
 #include <gtkmm/comboboxtext.h>
+#include <gtkmm/version.h>
+
+#define HAS_SIGNAL_ACTIVATE GTKMM_CHECK_VERSION(4,8,0)
 
 class ExampleWindow : public Gtk::Window
 {
 public:
   ExampleWindow();
-  virtual ~ExampleWindow();
+  ~ExampleWindow() override;
 
 protected:
   //Signal handlers:
   void on_combo_changed();
-  void on_entry_changed();
-  void on_entry_has_focus_changed();
+#if HAS_SIGNAL_ACTIVATE
+  void on_entry_activate();
+#endif
+  void on_entry_focus_leave();
 
   //Signal connection:
-  sigc::connection m_ConnectionHasFocusChanged;
-  bool m_entry_had_focus {false};
+  sigc::connection m_ConnectionFocusLeave;
 
   //Child widgets:
   Gtk::ComboBoxText m_Combo;


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