[gtkmm-documentation] Don't use the deprecated Gtk::Widget::signal_event()



commit 67c169ff0d17c4c7c466b18ecdaa1caff6ca36a0
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Mon Jan 22 11:02:42 2018 +0100

    Don't use the deprecated Gtk::Widget::signal_event()
    
    * examples/book/combobox/entry_complex/examplewindow.[cc|h]:
    * examples/book/combobox/entry_text/examplewindow.[cc|h]:
    Use Gtk::Widget::property_has_focus()::signal_changed() to see lost
    keyboard focus.
    * examples/book/menus/popup/examplewindow.[cc|h]:
    * examples/others/cellrenderercustom/cellrendererlist.[cc|h]:
    * examples/others/cellrenderercustom/cellrendererpopup.[cc|h]:
    Use Gtk::GestureMultiPress::signal_pressed() or signal_released() to see
    mouse button press or release.

 .../book/combobox/entry_complex/examplewindow.cc   |   29 ++++++++----------
 .../book/combobox/entry_complex/examplewindow.h    |    5 ++-
 examples/book/combobox/entry_text/examplewindow.cc |   29 ++++++++++---------
 examples/book/combobox/entry_text/examplewindow.h  |    5 ++-
 examples/book/menus/popup/examplewindow.cc         |   30 ++++++-------------
 examples/book/menus/popup/examplewindow.h          |    3 +-
 .../others/cellrenderercustom/cellrendererlist.cc  |   21 ++++---------
 .../others/cellrenderercustom/cellrendererlist.h   |    3 +-
 .../others/cellrenderercustom/cellrendererpopup.cc |   28 ++++++++++--------
 .../others/cellrenderercustom/cellrendererpopup.h  |    5 ++-
 10 files changed, 73 insertions(+), 85 deletions(-)
---
diff --git a/examples/book/combobox/entry_complex/examplewindow.cc 
b/examples/book/combobox/entry_complex/examplewindow.cc
index bf3ea11..da60899 100644
--- a/examples/book/combobox/entry_complex/examplewindow.cc
+++ b/examples/book/combobox/entry_complex/examplewindow.cc
@@ -14,9 +14,6 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
-//TODO: Remove this undef when we know what to use instead of signal_event().
-#undef GTKMM_DISABLE_DEPRECATED
-
 #include "examplewindow.h"
 #include <iostream>
 
@@ -79,8 +76,8 @@ ExampleWindow::ExampleWindow()
       &ExampleWindow::on_entry_changed) );
     entry->signal_activate().connect(sigc::mem_fun(*this,
       &ExampleWindow::on_entry_activate) );
-    m_ConnectionFocusOut = entry->signal_event().
-      connect(sigc::mem_fun(*this, &ExampleWindow::on_entry_focus_out_event), true);
+    m_ConnectionHasFocusChanged = entry->property_has_focus().signal_changed().
+      connect(sigc::mem_fun(*this, &ExampleWindow::on_entry_has_focus_changed));
   }
   else
     std::cout << "No Entry ???" << std::endl;
@@ -88,10 +85,10 @@ ExampleWindow::ExampleWindow()
 
 ExampleWindow::~ExampleWindow()
 {
-  // The event signal may be emitted while m_Combo is being destructed.
+  // The has_focus changed 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_ConnectionFocusOut.disconnect();
+  m_ConnectionHasFocusChanged.disconnect();
 }
 
 void ExampleWindow::on_entry_changed()
@@ -114,18 +111,18 @@ void ExampleWindow::on_entry_activate()
   }
 }
 
-bool ExampleWindow::on_entry_focus_out_event(const Glib::RefPtr<Gdk::Event>& event)
+void ExampleWindow::on_entry_has_focus_changed()
 {
-  if (event->get_event_type() == Gdk::Event::Type::FOCUS_CHANGE &&
-    !std::static_pointer_cast<Gdk::EventFocus>(event)->get_focus_in())
+  auto entry = m_Combo.get_entry();
+  if (entry)
   {
-    auto entry = m_Combo.get_entry();
-    if (entry)
+    const bool entry_has_focus = entry->has_focus();
+    if (m_entry_had_focus && !entry_has_focus)
     {
-      std::cout << "on_entry_focus_out_event(): Row=" << m_Combo.get_active_row_number()
-        << ", ID=" << entry->get_text() << std::endl;
-      return true;
+      // 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;
   }
-  return false;
 }
diff --git a/examples/book/combobox/entry_complex/examplewindow.h 
b/examples/book/combobox/entry_complex/examplewindow.h
index 98cb6e5..40cd75b 100644
--- a/examples/book/combobox/entry_complex/examplewindow.h
+++ b/examples/book/combobox/entry_complex/examplewindow.h
@@ -31,10 +31,11 @@ protected:
   //Signal handlers:
   void on_entry_changed();
   void on_entry_activate();
-  bool on_entry_focus_out_event(const Glib::RefPtr<Gdk::Event>& event);
+  void on_entry_has_focus_changed();
 
   //Signal connection:
-  sigc::connection m_ConnectionFocusOut;
+  sigc::connection m_ConnectionHasFocusChanged;
+  bool m_entry_had_focus {false};
 
   //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 04c9631..eb7aecb 100644
--- a/examples/book/combobox/entry_text/examplewindow.cc
+++ b/examples/book/combobox/entry_text/examplewindow.cc
@@ -14,9 +14,6 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
-//TODO: Remove this undef when we know what to use instead of signal_event().
-#undef GTKMM_DISABLE_DEPRECATED
-
 #include "examplewindow.h"
 #include <iostream>
 
@@ -42,8 +39,8 @@ ExampleWindow::ExampleWindow()
   {
     entry->signal_activate().connect(sigc::mem_fun(*this,
       &ExampleWindow::on_entry_activate) );
-    m_ConnectionFocusOut = entry->signal_event().
-      connect(sigc::mem_fun(*this, &ExampleWindow::on_entry_focus_out_event),true);
+    m_ConnectionHasFocusChanged = entry->property_has_focus().signal_changed().
+      connect(sigc::mem_fun(*this, &ExampleWindow::on_entry_has_focus_changed));
   }
   else
     std::cout << "No Entry ???" << std::endl;
@@ -53,10 +50,10 @@ ExampleWindow::ExampleWindow()
 
 ExampleWindow::~ExampleWindow()
 {
-  // The focus_out signal may be emitted while m_Combo is being destructed.
+  // The has_focus changed 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_ConnectionFocusOut.disconnect();
+  m_ConnectionHasFocusChanged.disconnect();
 }
 
 void ExampleWindow::on_combo_changed()
@@ -71,14 +68,18 @@ void ExampleWindow::on_entry_activate()
     << ", Text=" << m_Combo.get_active_text() << std::endl;
 }
 
-bool ExampleWindow::on_entry_focus_out_event(const Glib::RefPtr<Gdk::Event>& event)
+void ExampleWindow::on_entry_has_focus_changed()
 {
-  if (event->get_event_type() == Gdk::Event::Type::FOCUS_CHANGE &&
-    !std::static_pointer_cast<Gdk::EventFocus>(event)->get_focus_in())
+  auto entry = m_Combo.get_entry();
+  if (entry)
   {
-    std::cout << "on_entry_focus_out_event(): Row=" << m_Combo.get_active_row_number()
-      << ", Text=" << m_Combo.get_active_text() << std::endl;
-    return true;
+    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;
   }
-  return false;
 }
diff --git a/examples/book/combobox/entry_text/examplewindow.h 
b/examples/book/combobox/entry_text/examplewindow.h
index e7370ad..8c92156 100644
--- a/examples/book/combobox/entry_text/examplewindow.h
+++ b/examples/book/combobox/entry_text/examplewindow.h
@@ -30,10 +30,11 @@ protected:
   //Signal handlers:
   void on_combo_changed();
   void on_entry_activate();
-  bool on_entry_focus_out_event(const Glib::RefPtr<Gdk::Event>& event);
+  void on_entry_has_focus_changed();
 
   //Signal connection:
-  sigc::connection m_ConnectionFocusOut;
+  sigc::connection m_ConnectionHasFocusChanged;
+  bool m_entry_had_focus {false};
 
   //Child widgets:
   Gtk::ComboBoxText m_Combo;
diff --git a/examples/book/menus/popup/examplewindow.cc b/examples/book/menus/popup/examplewindow.cc
index f4da415..cf10294 100644
--- a/examples/book/menus/popup/examplewindow.cc
+++ b/examples/book/menus/popup/examplewindow.cc
@@ -14,9 +14,6 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
-//TODO: Remove this undef when we know what to use instead of signal_event().
-#undef GTKMM_DISABLE_DEPRECATED
-
 #include "examplewindow.h"
 #include <iostream>
 
@@ -32,15 +29,16 @@ ExampleWindow::ExampleWindow()
 
   // Catch button_press events:
   m_Box.pack_start(m_Label, Gtk::PackOptions::EXPAND_WIDGET);
-  m_Label.signal_event().connect(sigc::mem_fun(*this,
-              &ExampleWindow::on_label_button_press_event), true);
+  m_refGesture = Gtk::GestureMultiPress::create(m_Label);
+  m_refGesture->set_button(GDK_BUTTON_SECONDARY);
+  m_refGesture->signal_pressed().connect(
+    sigc::mem_fun(*this, &ExampleWindow::on_label_pressed));
 
   //Create actions:
 
   //Fill menu:
 
-  auto refActionGroup =
-    Gio::SimpleActionGroup::create();
+  auto refActionGroup = Gio::SimpleActionGroup::create();
 
   //File|New sub menu:
   //These menu actions would normally already exist for a main menu, because a
@@ -111,20 +109,12 @@ void ExampleWindow::on_menu_file_popup_generic()
    std::cout << "A popup menu item was selected." << std::endl;
 }
 
-bool ExampleWindow::on_label_button_press_event(const Glib::RefPtr<Gdk::Event>& event)
+void ExampleWindow::on_label_pressed(int /* n_press */, double /* x */, double /* y */)
 {
-  if (event->get_event_type() == Gdk::Event::Type::BUTTON_PRESS &&
-    std::static_pointer_cast<Gdk::EventButton>(event)->shall_trigger_context_menu())
-  {
-    if (m_pMenuPopup && !m_pMenuPopup->get_attach_widget())
-      m_pMenuPopup->attach_to_widget(*this);
+  if (m_pMenuPopup && !m_pMenuPopup->get_attach_widget())
+    m_pMenuPopup->attach_to_widget(*this);
 
-    if (m_pMenuPopup)
-      m_pMenuPopup->popup_at_pointer();
-
-    return true; //It has been handled.
-  }
-  else
-    return false;
+  if (m_pMenuPopup)
+    m_pMenuPopup->popup_at_pointer();
 }
 
diff --git a/examples/book/menus/popup/examplewindow.h b/examples/book/menus/popup/examplewindow.h
index 35e22f0..53cda0a 100644
--- a/examples/book/menus/popup/examplewindow.h
+++ b/examples/book/menus/popup/examplewindow.h
@@ -27,7 +27,7 @@ public:
 
 protected:
   //Signal handlers:
-  bool on_label_button_press_event(const Glib::RefPtr<Gdk::Event>& event);
+  void on_label_pressed(int n_press, double x, double y);
 
   void on_menu_file_popup_generic();
 
@@ -36,6 +36,7 @@ protected:
   Gtk::Label m_Label;
 
   Glib::RefPtr<Gtk::Builder> m_refBuilder;
+  Glib::RefPtr<Gtk::GestureMultiPress> m_refGesture;
 
   Gtk::Menu* m_pMenuPopup;
 };
diff --git a/examples/others/cellrenderercustom/cellrendererlist.cc 
b/examples/others/cellrenderercustom/cellrendererlist.cc
index baf57cc..8d025ae 100644
--- a/examples/others/cellrenderercustom/cellrendererlist.cc
+++ b/examples/others/cellrenderercustom/cellrendererlist.cc
@@ -14,9 +14,6 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
-//TODO: Remove this undef when we know what to use instead of signal_event().
-#undef GTKMM_DISABLE_DEPRECATED
-
 #include <gtkmm.h>
 #include "cellrendererlist.h"
 #include "popupentry.h"
@@ -49,8 +46,11 @@ CellRendererList::CellRendererList()
 {
   tree_view_.set_headers_visible(false);
   tree_view_.append_column("", popup_columns().item);
-  tree_view_.signal_event().connect(
-      sigc::mem_fun(*this, &Self::on_tree_view_button_release_event), true);
+
+  gesture_ = Gtk::GestureMultiPress::create(tree_view_);
+  gesture_->set_button(GDK_BUTTON_PRIMARY);
+  gesture_->signal_released().connect(
+    sigc::mem_fun(*this, &Self::on_tree_view_released));
 
   const auto selection = tree_view_.get_selection();
   selection->set_mode(Gtk::SelectionMode::BROWSE);
@@ -91,16 +91,9 @@ void CellRendererList::on_show_popup(const Glib::ustring& path, int x1, int y1,
   CellRendererPopup::on_show_popup(path, x1, y1, x2, y2);
 }
 
-bool CellRendererList::on_tree_view_button_release_event(const Glib::RefPtr<Gdk::Event>& event)
+void CellRendererList::on_tree_view_released(int /* n_press */, double /* x */, double /* y */)
 {
-  if (event->get_event_type() == Gdk::Event::Type::BUTTON_RELEASE &&
-    std::static_pointer_cast<Gdk::EventButton>(event)->get_button() == 1)
-  {
-    hide_popup();
-    return true;
-  }
-
-  return false;
+  hide_popup();
 }
 
 void CellRendererList::on_tree_selection_changed()
diff --git a/examples/others/cellrenderercustom/cellrendererlist.h 
b/examples/others/cellrenderercustom/cellrendererlist.h
index 20ca267..3f9549b 100644
--- a/examples/others/cellrenderercustom/cellrendererlist.h
+++ b/examples/others/cellrenderercustom/cellrendererlist.h
@@ -37,8 +37,9 @@ private:
 
   Glib::RefPtr<Gtk::ListStore>  list_store_;
   Gtk::TreeView                 tree_view_;
+  Glib::RefPtr<Gtk::GestureMultiPress> gesture_;
 
-  bool on_tree_view_button_release_event(const Glib::RefPtr<Gdk::Event>& event);
+  void on_tree_view_released(int n_press, double x, double y);
   void on_tree_selection_changed();
 };
 
diff --git a/examples/others/cellrenderercustom/cellrendererpopup.cc 
b/examples/others/cellrenderercustom/cellrendererpopup.cc
index 1910619..5450b49 100644
--- a/examples/others/cellrenderercustom/cellrendererpopup.cc
+++ b/examples/others/cellrenderercustom/cellrendererpopup.cc
@@ -14,9 +14,6 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
-//TODO: Remove this undef when we know what to use instead of signal_event().
-#undef GTKMM_DISABLE_DEPRECATED
-
 #include <gtkmm.h>
 #include "cellrendererpopup.h"
 #include "popupentry.h"
@@ -58,7 +55,10 @@ CellRendererPopup::CellRendererPopup()
   signal_show_popup_.connect(sigc::mem_fun(*this, &Self::on_show_popup));
   signal_hide_popup_.connect(sigc::mem_fun(*this, &Self::on_hide_popup));
 
-  popup_window_.signal_event().connect(sigc::mem_fun(*this, &Self::on_button_press_event), true);
+  gesture_ = Gtk::GestureMultiPress::create(popup_window_);
+  gesture_->set_button(GDK_BUTTON_PRIMARY);
+  gesture_->signal_pressed().connect(
+    sigc::mem_fun(*this, &Self::on_popup_window_pressed));
   popup_window_.signal_key_press_event().connect(sigc::mem_fun(*this, &Self::on_key_press_event), true);
   popup_window_.signal_style_updated().connect(sigc::mem_fun(*this, &Self::on_style_updated));
 }
@@ -226,17 +226,21 @@ void CellRendererPopup::on_hide_popup()
   editing_canceled_ = false;
 }
 
-bool CellRendererPopup::on_button_press_event(const Glib::RefPtr<Gdk::Event>& event)
+void CellRendererPopup::on_popup_window_pressed(int /* n_press */, double /* x */, double /* y */)
 {
-  if (!(event->get_event_type() == Gdk::Event::Type::BUTTON_PRESS &&
-      std::static_pointer_cast<Gdk::EventButton>(event)->get_button() == 1))
-    return false;
-
   // If the event happened outside the popup, cancel editing.
 
+  // The (x,y) coords passed to this method are based on Gdk::EventButton::get_coords(),
+  // which can't be trusted when input is grabbed by the popup window.
+  // Use Gdk::EventButton::get_root_coords().
+
+  auto event = gesture_->get_last_event(gesture_->get_current_sequence());
+  if (!(event && event->get_event_type() == Gdk::Event::Type::BUTTON_PRESS))
+    return;
+
   double x = 0.0;
   double y = 0.0;
-  std::static_pointer_cast<Gdk::EventButton>(event)->get_root_coords(x, y);
+  std::static_pointer_cast<const Gdk::EventButton>(event)->get_root_coords(x, y);
 
   int xoffset = 0, yoffset = 0;
   popup_window_.get_window()->get_root_origin(xoffset, yoffset);
@@ -252,12 +256,10 @@ bool CellRendererPopup::on_button_press_event(const Glib::RefPtr<Gdk::Event>& ev
   const int y2 = y1 + alloc.get_height();
 
   if(x > x1 && x < x2 && y > y1 && y < y2)
-    return false;
+    return;
 
   editing_canceled_ = true;
   signal_hide_popup_();
-
-  return false;
 }
 
 bool CellRendererPopup::on_key_press_event(const Glib::RefPtr<Gdk::EventKey>& event)
diff --git a/examples/others/cellrenderercustom/cellrendererpopup.h 
b/examples/others/cellrenderercustom/cellrendererpopup.h
index 2f41b86..ef914e2 100644
--- a/examples/others/cellrenderercustom/cellrendererpopup.h
+++ b/examples/others/cellrenderercustom/cellrendererpopup.h
@@ -60,7 +60,7 @@ protected:
 private:
   using Self = CellRendererPopup;
 
-  SignalShowPopup  signal_show_popup_;
+  SignalShowPopup signal_show_popup_;
   SignalHidePopup signal_hide_popup_;
 
   mutable int   button_width_; //mutable because it is just a cache.
@@ -69,8 +69,9 @@ private:
   PopupEntry*   popup_entry_;
   bool          shown_;
   bool          editing_canceled_;
+  Glib::RefPtr<Gtk::GestureMultiPress> gesture_;
 
-  bool on_button_press_event(const Glib::RefPtr<Gdk::Event>& event);
+  void on_popup_window_pressed(int n_press, double x, double y);
   bool on_key_press_event(const Glib::RefPtr<Gdk::EventKey>& event);
   void on_style_updated();
 


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