[gtkmm-documentation] Update the custom container example



commit a9d3937b55abfb92239db2a21f45fa6efd80e2d6
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Tue Aug 2 16:49:55 2022 +0200

    Update the custom container example
    
    If Widget::signal_destroy() exists, connect to it and create a managed
    custom container.

 .../book/custom/custom_container/examplewindow.cc  |  9 ++++++++-
 .../book/custom/custom_container/mycontainer.cc    | 22 ++++++++++++++++++++--
 .../book/custom/custom_container/mycontainer.h     |  8 ++++++++
 3 files changed, 36 insertions(+), 3 deletions(-)
---
diff --git a/examples/book/custom/custom_container/examplewindow.cc 
b/examples/book/custom/custom_container/examplewindow.cc
index feb1812..7e77b48 100644
--- a/examples/book/custom/custom_container/examplewindow.cc
+++ b/examples/book/custom/custom_container/examplewindow.cc
@@ -34,8 +34,15 @@ ExampleWindow::ExampleWindow()
   m_MyContainer.prepend(*Gtk::make_managed<Gtk::Label>(
     "First line\nSecond line\nThird line"));
   m_MyContainer.set_expand();
-
   m_VBox.append(m_MyContainer);
+
+#if HAS_SIGNAL_DESTROY
+  // A managed custom container.
+  auto container = Gtk::make_managed<MyContainer>();
+  container->prepend(*Gtk::make_managed<Gtk::Label>("Second custom container"));
+  container->set_expand();
+  m_VBox.append(*container);
+#endif
   m_VBox.append(m_ButtonBox);
 
   m_ButtonBox.append(m_Button_Quit);
diff --git a/examples/book/custom/custom_container/mycontainer.cc 
b/examples/book/custom/custom_container/mycontainer.cc
index 73ba51e..214ffe2 100644
--- a/examples/book/custom/custom_container/mycontainer.cc
+++ b/examples/book/custom/custom_container/mycontainer.cc
@@ -14,24 +14,42 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
-#include <iostream>
 #include "mycontainer.h"
 
 // This example container is a simplified vertical Box.
 //
-// It can't be used as a managed widget, managed by another container.
+// It can't be used as a managed widget, managed by another container
+// unless Gtk::Widget::signal_destroy() exists.
 // It would cause an error like
 // Gtk-WARNING **: 08:31:48.137: Finalizing gtkmm__GtkWidget 0x561b777462c0, but it still has children left:
 
 MyContainer::MyContainer()
 {
+#if HAS_SIGNAL_DESTROY
+  signal_destroy().connect(sigc::mem_fun(*this, &MyContainer::on_container_destroy));
+#endif
 }
 
 MyContainer::~MyContainer()
+{
+  // If MyContainer is a managed widget, the underlying C object is destructed
+  // before this C++ destructor is executed.
+  if (!gobj())
+    return;
+
+  // If MyContainer is not a managed widget, unparent all children here.
+  while (Widget* child = get_first_child())
+    child->unparent();
+}
+
+#if HAS_SIGNAL_DESTROY
+// This signal handler is called only if MyContainer is a managed widget.
+void MyContainer::on_container_destroy()
 {
   while (Widget* child = get_first_child())
     child->unparent();
 }
+#endif
 
 // Get number of visible children.
 int MyContainer::get_nvis_children() const
diff --git a/examples/book/custom/custom_container/mycontainer.h 
b/examples/book/custom/custom_container/mycontainer.h
index 8eef093..806831c 100644
--- a/examples/book/custom/custom_container/mycontainer.h
+++ b/examples/book/custom/custom_container/mycontainer.h
@@ -18,6 +18,9 @@
 #define GTKMM_CUSTOM_CONTAINER_MYCONTAINER_H
 
 #include <gtkmm/widget.h>
+#include <gtkmm/version.h>
+
+#define HAS_SIGNAL_DESTROY GTKMM_CHECK_VERSION(4,8,0)
 
 class MyContainer : public Gtk::Widget
 {
@@ -37,6 +40,11 @@ protected:
   void measure_vfunc(Gtk::Orientation orientation, int for_size, int& minimum, int& natural,
     int& minimum_baseline, int& natural_baseline) const override;
   void size_allocate_vfunc(int width, int height, int baseline) override;
+
+#if HAS_SIGNAL_DESTROY
+  // Signal handler:
+  void on_container_destroy();
+#endif
 };
 
 #endif //GTKMM_CUSTOM_CONTAINER_MYCONTAINER_H


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