[gtkmm-documentation] Update the custom_container and custom_widget examples



commit 3bc8c74e494a2ecfb31e94417ea42fe277987552
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Fri Nov 18 17:00:04 2016 +0100

    Update the custom_container and custom_widget examples
    
    * examples/book/custom/custom_container/examplewindow.cc:
    * examples/book/custom/custom_widget/examplewindow.cc: Replace
    Gtk::Container::set_border_width() by Gtk::Widget::property_margin().
    * examples/book/custom/custom_container/mycontainer.cc:
    * examples/book/custom/custom_container/mycontainer.h: Override measure_vfunc()
    instead of the removed get_preferred_*_vfunc(). Use measure() instead of
    get_preferred_*().
    * examples/book/custom/custom_widget/mywidget.cc:
    * examples/book/custom/custom_widget/mywidget.h: Override measure_vfunc()
    instead of the removed get_preferred_*_vfunc(). Use Gdk::Window::create_child()
    instead of the removed Gdk::Window::create().
    
    gtk+ now requires that gtk_widget_set_has_window() is called in the
    instance init() function. That has not been fixed in the custom examples.
    Gtk::Widget::set_has_window() is still called in the constructors, which is
    too late.

 .../book/custom/custom_container/examplewindow.cc  |    6 +-
 .../book/custom/custom_container/mycontainer.cc    |  165 ++++++++------------
 .../book/custom/custom_container/mycontainer.h     |    8 +-
 .../book/custom/custom_widget/examplewindow.cc     |    6 +-
 examples/book/custom/custom_widget/mywidget.cc     |   63 ++------
 examples/book/custom/custom_widget/mywidget.h      |    6 +-
 6 files changed, 88 insertions(+), 166 deletions(-)
---
diff --git a/examples/book/custom/custom_container/examplewindow.cc 
b/examples/book/custom/custom_container/examplewindow.cc
index 5143cc0..4e5c428 100644
--- a/examples/book/custom/custom_container/examplewindow.cc
+++ b/examples/book/custom/custom_container/examplewindow.cc
@@ -1,5 +1,3 @@
-//$Id: examplewindow.cc 836 2007-05-09 03:02:38Z jjongsma $ -*- c++ -*-
-
 /* gtkmm example Copyright (C) 2004 gtkmm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -26,9 +24,9 @@ ExampleWindow::ExampleWindow()
   m_Button_Quit("Quit")
 {
   set_title("Custom Container example");
-  set_border_width(6);
   set_default_size(400, 200);
 
+  m_VBox.property_margin() = 6;
   add(m_VBox);
 
   //Add the child widgets to the custom container:
@@ -38,7 +36,7 @@ ExampleWindow::ExampleWindow()
   m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);
 
   m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
-  m_ButtonBox.set_border_width(6);
+  m_ButtonBox.property_margin() = 6;
   m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
   m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this,
               &ExampleWindow::on_button_quit) );
diff --git a/examples/book/custom/custom_container/mycontainer.cc 
b/examples/book/custom/custom_container/mycontainer.cc
index 70818da..97ef9d7 100644
--- a/examples/book/custom/custom_container/mycontainer.cc
+++ b/examples/book/custom/custom_container/mycontainer.cc
@@ -1,5 +1,3 @@
-//$Id: mycontainer.cc 836 2007-05-09 03:02:38Z jjongsma $ -*- c++ -*-
-
 /* gtkmm example Copyright (C) 2004 gtkmm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -29,19 +27,11 @@ MyContainer::MyContainer()
 
 MyContainer::~MyContainer()
 {
-/*
-  // These calls to Gtk::Widget::unparent() are necessary if MyContainer is
-  // deleted before its children. But if you use a version of gtkmm where bug
-  // https://bugzilla.gnome.org/show_bug.cgi?id=605728
-  // has not been fixed (gtkmm 3.7.10 or earlier) and the children are deleted
-  // before the container, these calls can make the program crash.
-  // That's because on_remove() is not called, when the children are deleted.
   if (m_child_one)
     m_child_one->unparent();
 
   if (m_child_two)
     m_child_two->unparent();
-*/
 }
 
 void MyContainer::set_child_widgets(Gtk::Widget& child_one,
@@ -54,117 +44,86 @@ void MyContainer::set_child_widgets(Gtk::Widget& child_one,
   m_child_two->set_parent(*this);
 }
 
-//This example container is a simplified VBox with at most two children.
+// This example container is a simplified vertical Box with at most two children.
 Gtk::SizeRequestMode MyContainer::get_request_mode_vfunc() const
 {
   return Gtk::SIZE_REQUEST_HEIGHT_FOR_WIDTH;
 }
 
-//Discover the total amount of minimum space and natural space needed by
-//this container and its children.
-void MyContainer::get_preferred_width_vfunc(int& minimum_width, int& natural_width) const
+// Discover the total amount of minimum space and natural space needed by
+// this container and its children.
+void MyContainer::measure_vfunc(Gtk::Orientation orientation, int for_size,
+  int& minimum, int& natural, int& minimum_baseline, int& natural_baseline) const
 {
-  int child_minimum_width[2] = {0, 0};
-  int child_natural_width[2] = {0, 0};
+  // Don't use baseline alignment.
+  minimum_baseline = -1;
+  natural_baseline = -1;
 
-  if(m_child_one && m_child_one->get_visible())
-    m_child_one->get_preferred_width(child_minimum_width[0], child_natural_width[0]);
+  int dummy_minimum_baseline = 0;
+  int dummy_natural_baseline = 0;
 
-  if(m_child_two && m_child_two->get_visible())
-    m_child_two->get_preferred_width(child_minimum_width[1], child_natural_width[1]);
+  if (orientation == Gtk::ORIENTATION_HORIZONTAL)
+  {
+    int height_per_child = for_size;
 
-  //Request a width equal to the width of the widest visible child.
-  minimum_width = std::max(child_minimum_width[0], child_minimum_width[1]);
-  natural_width = std::max(child_natural_width[0], child_natural_width[1]);
-}
+    if (for_size >= 0)
+    {
+      int nvis_children = 0;
 
-void MyContainer::get_preferred_height_for_width_vfunc(int width,
-   int& minimum_height, int& natural_height) const
-{
-  int child_minimum_height[2] = {0, 0};
-  int child_natural_height[2] = {0, 0};
-  int nvis_children = 0;
+      // Get number of visible children.
+      if (m_child_one && m_child_one->get_visible())
+        ++nvis_children;
+      if (m_child_two && m_child_two->get_visible())
+        ++nvis_children;
 
-  if(m_child_one && m_child_one->get_visible())
-  {
-    ++nvis_children;
-    m_child_one->get_preferred_height_for_width(width, child_minimum_height[0],
-                                                child_natural_height[0]);
-  }
+      // Divide the height equally among the visible children.
+      if (nvis_children > 0)
+        height_per_child = for_size / nvis_children;
+    }
 
-  if(m_child_two && m_child_two->get_visible())
-  {
-    ++nvis_children;
-    m_child_two->get_preferred_height_for_width(width, child_minimum_height[1],
-                                                child_natural_height[1]);
-  }
+    int child_minimum_width[2] = {0, 0};
+    int child_natural_width[2] = {0, 0};
 
-  //The allocated height will be divided equally among the visible children.
-  //Request a height equal to the number of visible children times the height
-  //of the highest child.
-  minimum_height = nvis_children * std::max(child_minimum_height[0],
-                                            child_minimum_height[1]);
-  natural_height = nvis_children * std::max(child_natural_height[0],
-                                            child_natural_height[1]);
-}
+    if (m_child_one && m_child_one->get_visible())
+      m_child_one->measure(orientation, height_per_child, child_minimum_width[0],
+        child_natural_width[0], dummy_minimum_baseline, dummy_natural_baseline);
 
-void MyContainer::get_preferred_height_vfunc(int& minimum_height, int& natural_height) const
-{
-  int child_minimum_height[2] = {0, 0};
-  int child_natural_height[2] = {0, 0};
-  int nvis_children = 0;
+    if (m_child_two && m_child_two->get_visible())
+      m_child_two->measure(orientation, height_per_child, child_minimum_width[1],
+        child_natural_width[1], dummy_minimum_baseline, dummy_natural_baseline);
 
-  if(m_child_one && m_child_one->get_visible())
-  {
-    ++nvis_children;
-    m_child_one->get_preferred_height(child_minimum_height[0], child_natural_height[0]);
+    // Request a width equal to the width of the widest visible child.
+    minimum = std::max(child_minimum_width[0], child_minimum_width[1]);
+    natural = std::max(child_natural_width[0], child_natural_width[1]);
   }
-
-  if(m_child_two && m_child_two->get_visible())
+  else // Gtk::ORIENTATION_VERTICAL
   {
-    ++nvis_children;
-    m_child_two->get_preferred_height(child_minimum_height[1], child_natural_height[1]);
-  }
-
-  //The allocated height will be divided equally among the visible children.
-  //Request a height equal to the number of visible children times the height
-  //of the highest child.
-  minimum_height = nvis_children * std::max(child_minimum_height[0],
-                                            child_minimum_height[1]);
-  natural_height = nvis_children * std::max(child_natural_height[0],
-                                            child_natural_height[1]);
-}
-
-void MyContainer::get_preferred_width_for_height_vfunc(int height,
-   int& minimum_width, int& natural_width) const
-{
-  int child_minimum_width[2] = {0, 0};
-  int child_natural_width[2] = {0, 0};
-  int nvis_children = 0;
-
-  //Get number of visible children.
-  if(m_child_one && m_child_one->get_visible())
-    ++nvis_children;
-  if(m_child_two && m_child_two->get_visible())
-    ++nvis_children;
+    int child_minimum_height[2] = {0, 0};
+    int child_natural_height[2] = {0, 0};
+    int nvis_children = 0;
 
-  if(nvis_children > 0)
-  {
-    //Divide the height equally among the visible children.
-    const int height_per_child = height / nvis_children;
+    if (m_child_one && m_child_one->get_visible())
+    {
+      ++nvis_children;
+      m_child_one->measure(orientation, for_size, child_minimum_height[0],
+        child_natural_height[0], dummy_minimum_baseline, dummy_natural_baseline);
+    }
 
-    if(m_child_one && m_child_one->get_visible())
-      m_child_one->get_preferred_width_for_height(height_per_child,
-                   child_minimum_width[0], child_natural_width[0]);
+    if (m_child_two && m_child_two->get_visible())
+    {
+      ++nvis_children;
+      m_child_two->measure(orientation, for_size, child_minimum_height[1],
+        child_natural_height[1], dummy_minimum_baseline, dummy_natural_baseline);
+    }
 
-    if(m_child_two && m_child_two->get_visible())
-      m_child_two->get_preferred_width_for_height(height_per_child,
-                   child_minimum_width[1], child_natural_width[1]);
+    // The allocated height will be divided equally among the visible children.
+    // Request a height equal to the number of visible children times the height
+    // of the highest child.
+    minimum = nvis_children * std::max(child_minimum_height[0],
+                                       child_minimum_height[1]);
+    natural = nvis_children * std::max(child_natural_height[0],
+                                       child_natural_height[1]);
   }
-
-  //Request a width equal to the width of the widest child.
-  minimum_width = std::max(child_minimum_width[0], child_minimum_width[1]);
-  natural_width = std::max(child_natural_width[0], child_natural_width[1]);
 }
 
 void MyContainer::on_size_allocate(Gtk::Allocation& allocation)
diff --git a/examples/book/custom/custom_container/mycontainer.h 
b/examples/book/custom/custom_container/mycontainer.h
index faac1e9..2758eb3 100644
--- a/examples/book/custom/custom_container/mycontainer.h
+++ b/examples/book/custom/custom_container/mycontainer.h
@@ -1,5 +1,3 @@
-//$Id: mycontainer.h 246 2004-10-10 21:49:33Z murrayc $ -*- c++ -*-
-
 /* gtkmm example Copyright (C) 2004 gtkmm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -33,10 +31,8 @@ protected:
 
   //Overrides:
   Gtk::SizeRequestMode get_request_mode_vfunc() const override;
-  void get_preferred_width_vfunc(int& minimum_width, int& natural_width) const override;
-  void get_preferred_height_for_width_vfunc(int width, int& minimum_height, int& natural_height) const 
override;
-  void get_preferred_height_vfunc(int& minimum_height, int& natural_height) const override;
-  void get_preferred_width_for_height_vfunc(int height, int& minimum_width, int& natural_width) const 
override;
+  void measure_vfunc(Gtk::Orientation orientation, int for_size, int& minimum, int& natural,
+    int& minimum_baseline, int& natural_baseline) const override;
   void on_size_allocate(Gtk::Allocation& allocation) override;
 
   void forall_vfunc(gboolean include_internals, GtkCallback callback, gpointer callback_data) override;
diff --git a/examples/book/custom/custom_widget/examplewindow.cc 
b/examples/book/custom/custom_widget/examplewindow.cc
index e7bba83..4795490 100644
--- a/examples/book/custom/custom_widget/examplewindow.cc
+++ b/examples/book/custom/custom_widget/examplewindow.cc
@@ -1,5 +1,3 @@
-//$Id: examplewindow.cc 341 2005-01-18 20:52:18Z murrayc $ -*- c++ -*-
-
 /* gtkmm example Copyright (C) 2004 gtkmm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -23,9 +21,9 @@ ExampleWindow::ExampleWindow()
   m_Button_Quit("Quit")
 {
   set_title("Custom Widget example");
-  set_border_width(6);
   set_default_size(400, 200);
 
+  m_VBox.property_margin() = 6;
   add(m_VBox);
   m_VBox.pack_start(m_MyWidget, Gtk::PACK_EXPAND_WIDGET);
   m_MyWidget.show();
@@ -33,7 +31,7 @@ ExampleWindow::ExampleWindow()
   m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);
 
   m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
-  m_ButtonBox.set_border_width(6);
+  m_ButtonBox.property_margin() = 6;
   m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
   m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this, &ExampleWindow::on_button_quit) );
 
diff --git a/examples/book/custom/custom_widget/mywidget.cc b/examples/book/custom/custom_widget/mywidget.cc
index b64021f..71afcce 100644
--- a/examples/book/custom/custom_widget/mywidget.cc
+++ b/examples/book/custom/custom_widget/mywidget.cc
@@ -20,9 +20,6 @@
 //#include <gtk/gtkwidget.h> //For GTK_IS_WIDGET()
 #include <cstring>
 
-// The MyWidget class uses API which was added in gtkmm 3.15.3 (Gtk::CssProviderError,
-// Gtk::CssProvider::signal_parsing_error() and Gtk::CssSection) and in gtkmm 3.15.2
-// (Gtk::StyleProperty).
 
 MyWidget::MyWidget() :
   //The GType name will actually be gtkmm__CustomObject_mywidget
@@ -93,30 +90,23 @@ Gtk::SizeRequestMode MyWidget::get_request_mode_vfunc() const
 //this widget.
 //Let's make this simple example widget always need minimum 60 by 50 and
 //natural 100 by 70.
-void MyWidget::get_preferred_width_vfunc(int& minimum_width, int& natural_width) const
+void MyWidget::measure_vfunc(Gtk::Orientation orientation, int /* for_size */,
+  int& minimum, int& natural, int& minimum_baseline, int& natural_baseline) const
 {
-  minimum_width = 60;
-  natural_width = 100;
-}
-
-void MyWidget::get_preferred_height_for_width_vfunc(int /* width */,
-   int& minimum_height, int& natural_height) const
-{
-  minimum_height = 50;
-  natural_height = 70;
-}
-
-void MyWidget::get_preferred_height_vfunc(int& minimum_height, int& natural_height) const
-{
-  minimum_height = 50;
-  natural_height = 70;
-}
+  if (orientation == Gtk::ORIENTATION_HORIZONTAL)
+  {
+    minimum = 60;
+    natural = 100;
+  }
+  else
+  {
+    minimum = 50;
+    natural = 70;
+  }
 
-void MyWidget::get_preferred_width_for_height_vfunc(int /* height */,
-   int& minimum_width, int& natural_width) const
-{
-  minimum_width = 60;
-  natural_width = 100;
+  // Don't use baseline alignment.
+  minimum_baseline = -1;
+  natural_baseline = -1;
 }
 
 void MyWidget::on_size_allocate(Gtk::Allocation& allocation)
@@ -162,24 +152,8 @@ void MyWidget::on_realize()
   if(!m_refGdkWindow)
   {
     //Create the GdkWindow:
-
-    GdkWindowAttr attributes;
-    memset(&attributes, 0, sizeof(attributes));
-
-    Gtk::Allocation allocation = get_allocation();
-
-    //Set initial position and size of the Gdk::Window:
-    attributes.x = allocation.get_x();
-    attributes.y = allocation.get_y();
-    attributes.width = allocation.get_width();
-    attributes.height = allocation.get_height();
-
-    attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK;
-    attributes.window_type = GDK_WINDOW_CHILD;
-    attributes.wclass = GDK_INPUT_OUTPUT;
-
-    m_refGdkWindow = Gdk::Window::create(get_parent_window(), &attributes,
-            GDK_WA_X | GDK_WA_Y);
+    m_refGdkWindow = Gdk::Window::create_child(get_parent_window(),
+      get_events () | Gdk::EXPOSURE_MASK, get_allocation());
     set_window(m_refGdkWindow);
 
     //make the widget receive expose events
@@ -208,8 +182,7 @@ bool MyWidget::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
     allocation.get_width(), allocation.get_height());
 
   // draw the foreground
-  const auto state = refStyleContext->get_state();
-  Gdk::Cairo::set_source_rgba(cr, refStyleContext->get_color(state));
+  Gdk::Cairo::set_source_rgba(cr, refStyleContext->get_color());
   cr->move_to(155.*scale_x, 165.*scale_y);
   cr->line_to(155.*scale_x, 838.*scale_y);
   cr->line_to(265.*scale_x, 900.*scale_y);
diff --git a/examples/book/custom/custom_widget/mywidget.h b/examples/book/custom/custom_widget/mywidget.h
index 6fd6d1a..84f4e31 100644
--- a/examples/book/custom/custom_widget/mywidget.h
+++ b/examples/book/custom/custom_widget/mywidget.h
@@ -31,10 +31,8 @@ protected:
 
   //Overrides:
   Gtk::SizeRequestMode get_request_mode_vfunc() const override;
-  void get_preferred_width_vfunc(int& minimum_width, int& natural_width) const override;
-  void get_preferred_height_for_width_vfunc(int width, int& minimum_height, int& natural_height) const  
override;
-  void get_preferred_height_vfunc(int& minimum_height, int& natural_height) const override;
-  void get_preferred_width_for_height_vfunc(int height, int& minimum_width, int& natural_width) const 
override;
+  void measure_vfunc(Gtk::Orientation orientation, int for_size, int& minimum, int& natural,
+    int& minimum_baseline, int& natural_baseline) const override;
   void on_size_allocate(Gtk::Allocation& allocation) override;
   void on_map() override;
   void on_unmap() override;


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