[gtkmm-documentation] Fix custom_container and custom_widget examples



commit 62e94bf0cc9e17bfae09f43806f90f7a6580b7c5
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Thu Jul 27 18:18:02 2017 +0200

    Fix custom_container and custom_widget examples
    
    * examples/book/custom/custom_container/mycontainer.[cc|h]:
    The overridden on_size_allocate() default signal handler now has the out_clip
    output parameter that must be calculated.
    * examples/book/custom/custom_widget/custom_gtk.css:
    Replace the example_scale custom style property by the "padding"
    css property.
    * examples/book/custom/custom_widget/mywidget.[cc|h]:
    * examples/book/custom/custom_widget/mywidget2.[cc|h]:
    The overridden on_size_allocate() now has the out_clip output parameter.
    Custom style properties have been removed from gtk+. Let the "padding" css
    property affect the size of the drawn figure.

 .../book/custom/custom_container/mycontainer.cc    |   43 +++++++++++++++-----
 .../book/custom/custom_container/mycontainer.h     |    3 +-
 examples/book/custom/custom_widget/custom_gtk.css  |   14 +-----
 examples/book/custom/custom_widget/mywidget.cc     |   37 +++++++++--------
 examples/book/custom/custom_widget/mywidget.h      |    8 +--
 examples/book/custom/custom_widget/mywidget2.cc    |   34 ++++++++-------
 examples/book/custom/custom_widget/mywidget2.h     |    8 +--
 7 files changed, 80 insertions(+), 67 deletions(-)
---
diff --git a/examples/book/custom/custom_container/mycontainer.cc 
b/examples/book/custom/custom_container/mycontainer.cc
index 0b8f6d9..7304ab9 100644
--- a/examples/book/custom/custom_container/mycontainer.cc
+++ b/examples/book/custom/custom_container/mycontainer.cc
@@ -126,28 +126,36 @@ void MyContainer::measure_vfunc(Gtk::Orientation orientation, int for_size,
   }
 }
 
-void MyContainer::on_size_allocate(Gtk::Allocation& allocation)
+void MyContainer::on_size_allocate(const Gtk::Allocation& allocation,
+  int  baseline, Gtk::Allocation& out_clip)
 {
   //Do something with the space that we have actually been given:
   //(We will not be given heights or widths less than we have requested, though
   //we might get more.)
 
-  //Use the offered allocation for this container:
-  set_allocation(allocation);
-
   //Get number of visible children.
+  const bool visible_one = m_child_one && m_child_one->get_visible();
+  const bool visible_two = m_child_two && m_child_two->get_visible();
   int nvis_children = 0;
-  if(m_child_one && m_child_one->get_visible())
+  if (visible_one)
     ++nvis_children;
-  if(m_child_two && m_child_two->get_visible())
+  if (visible_two)
     ++nvis_children;
 
-  if(nvis_children <= 0)
+  if (nvis_children <= 0)
+  {
+    // No visible child. The clip rectangle can be empty.
+    out_clip = allocation;
+    out_clip.set_width(0);
+    out_clip.set_height(0);
     return;
+  }
 
   //Assign space to the children:
   Gtk::Allocation child_allocation_one;
   Gtk::Allocation child_allocation_two;
+  Gtk::Allocation child_clip_one;
+  Gtk::Allocation child_clip_two;
 
   //Place the first child at the top-left:
   child_allocation_one.set_x( allocation.get_x() );
@@ -156,11 +164,12 @@ void MyContainer::on_size_allocate(Gtk::Allocation& allocation)
   //Make it take up the full width available:
   child_allocation_one.set_width( allocation.get_width() );
 
-  if(m_child_one && m_child_one->get_visible())
+  if (visible_one)
   {
     //Divide the height equally among the visible children.
     child_allocation_one.set_height( allocation.get_height() / nvis_children);
-    m_child_one->size_allocate(child_allocation_one);
+    child_clip_one = child_allocation_one;
+    m_child_one->size_allocate(child_allocation_one, baseline, child_clip_one);
   }
   else
     child_allocation_one.set_height(0);
@@ -177,8 +186,20 @@ void MyContainer::on_size_allocate(Gtk::Allocation& allocation)
   child_allocation_two.set_height( allocation.get_height() -
           child_allocation_one.get_height());
 
-  if(m_child_two && m_child_two->get_visible())
-    m_child_two->size_allocate(child_allocation_two);
+  if (visible_two)
+  {
+    child_clip_two = child_allocation_two;
+    m_child_two->size_allocate(child_allocation_two, baseline, child_clip_two);
+  }
+
+  if (visible_one)
+  {
+    out_clip = child_clip_one;
+    if (visible_two)
+      out_clip.join(child_clip_two);
+  }
+  else
+    out_clip = child_clip_two;
 }
 
 void MyContainer::forall_vfunc(const ForeachSlot& slot)
diff --git a/examples/book/custom/custom_container/mycontainer.h 
b/examples/book/custom/custom_container/mycontainer.h
index 1bb6c79..44182f9 100644
--- a/examples/book/custom/custom_container/mycontainer.h
+++ b/examples/book/custom/custom_container/mycontainer.h
@@ -33,7 +33,8 @@ protected:
   Gtk::SizeRequestMode get_request_mode_vfunc() 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_size_allocate(const Gtk::Allocation& allocation, int baseline,
+    Gtk::Allocation& out_clip) override;
 
   void forall_vfunc(const ForeachSlot& slot) override;
 
diff --git a/examples/book/custom/custom_widget/custom_gtk.css 
b/examples/book/custom/custom_widget/custom_gtk.css
index 1ecb3dd..57510ee 100644
--- a/examples/book/custom/custom_widget/custom_gtk.css
+++ b/examples/book/custom/custom_widget/custom_gtk.css
@@ -1,21 +1,13 @@
-/* Example of a CSS style sheet with a custom style property.
- *
- * The name of the style property must have its canonical form, i.e. characters
- * other than ASCII letters, digits, and hyphens must be replaced by hyphens.
-*/
-
-* {
-  /* -<widget class name>-<style property canonical name>: <value>; */
-  -gtkmm__CustomObject_MyWidget-example-scale: 920;
-  -gtkmm__CustomObject_MyWidget2-example-scale: 1000;
-}
+/* Example of a CSS style sheet. */
 
 my-widget {
   background-color: rgb(255,0,0);
   color:            rgb(0,0,255);
+  padding:          0px 0px 20px 0px;
 }
 
 my-widget2 {
   background-color: rgb(255,255,0);
   color:            rgb(255,0,0);
+  padding:          30px 0px 0px 10px;
 }
diff --git a/examples/book/custom/custom_widget/mywidget.cc b/examples/book/custom/custom_widget/mywidget.cc
index 7bde395..ddba3cc 100644
--- a/examples/book/custom/custom_widget/mywidget.cc
+++ b/examples/book/custom/custom_widget/mywidget.cc
@@ -25,18 +25,15 @@ MyWidget::MyWidget() :
   //The GType name will actually be gtkmm__CustomObject_MyWidget
   Glib::ObjectBase("MyWidget"),
   Gtk::WidgetCustomSnapshot(),
-  MyExtraInit("my-widget"),
+  MyExtraInit("my-widget"), // CSS node name, which must be used in the CSS file.
   Gtk::Widget(),
-  //Install a style property so that an aspect of this widget may be themed
-  //via a CSS style sheet file:
-  m_scale_prop(*this, "example_scale", 500),
-  m_scale(1000)
+  m_padding()
 {
   // Expand, if there is extra space.
   set_hexpand(true);
   set_vexpand(true);
 
-  //This shows the GType name, which must be used in the CSS file.
+  //This shows the GType name.
   std::cout << "GType name: " << G_OBJECT_TYPE_NAME(gobj()) << std::endl;
 
   //This shows that the GType still derives from GtkWidget:
@@ -111,20 +108,21 @@ void MyWidget::measure_vfunc(Gtk::Orientation orientation, int /* for_size */,
   natural_baseline = -1;
 }
 
-void MyWidget::on_size_allocate(Gtk::Allocation& allocation)
+void MyWidget::on_size_allocate(const Gtk::Allocation& allocation,
+  int /* baseline */, Gtk::Allocation& out_clip)
 {
   //Do something with the space that we have actually been given:
   //(We will not be given heights or widths less than we have requested, though
   //we might get more)
 
-  //Use the offered allocation for this container:
-  set_allocation(allocation);
-
   if(m_refGdkWindow)
   {
     m_refGdkWindow->move_resize( allocation.get_x(), allocation.get_y(),
             allocation.get_width(), allocation.get_height() );
   }
+
+  // Use the offered allocation for this widget:
+  out_clip = allocation;
 }
 
 void MyWidget::on_map()
@@ -146,10 +144,13 @@ void MyWidget::on_realize()
 
   set_realized();
 
-  //Get the themed style from the CSS file:
-  m_scale = m_scale_prop.get_value();
-  std::cout << "m_scale (example_scale from the theme/css-file) is: "
-      << m_scale << std::endl;
+  //Get the themed padding from the CSS file:
+  m_padding = get_style_context()->get_padding();
+  std::cout << "m_padding from the theme/css-file is"
+    << ": top=" << m_padding.get_top()
+    << ", right=" << m_padding.get_right()
+    << ", bottom=" << m_padding.get_bottom()
+    << ", left=" << m_padding.get_left() << std::endl;
 
   if(!m_refGdkWindow)
   {
@@ -175,10 +176,8 @@ void MyWidget::snapshot_vfunc(Gtk::Snapshot& snapshot)
 {
   const auto allocation = get_allocation();
   auto clip = get_clip();
-  clip.set_x(clip.get_x() - allocation.get_x());
-  clip.set_y(clip.get_y() - allocation.get_y());
-  const double scale_x = (double)clip.get_width() / m_scale;
-  const double scale_y = (double)clip.get_height() / m_scale;
+  clip.set_x(clip.get_x() - allocation.get_x() - m_padding.get_left());
+  clip.set_y(clip.get_y() - allocation.get_y() - m_padding.get_top());
   auto refStyleContext = get_style_context();
 
   // Create a cairo context to draw on.
@@ -189,6 +188,8 @@ void MyWidget::snapshot_vfunc(Gtk::Snapshot& snapshot)
     clip.get_x(), clip.get_y(), clip.get_width(), clip.get_height());
 
   // draw the foreground
+  const double scale_x = 0.001 * (clip.get_width() - m_padding.get_left() - m_padding.get_right());
+  const double scale_y = 0.001 * (clip.get_height() - m_padding.get_top() - m_padding.get_bottom());
   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);
diff --git a/examples/book/custom/custom_widget/mywidget.h b/examples/book/custom/custom_widget/mywidget.h
index 679a8f3..f35387d 100644
--- a/examples/book/custom/custom_widget/mywidget.h
+++ b/examples/book/custom/custom_widget/mywidget.h
@@ -19,7 +19,6 @@
 
 #include <gtkmm/widget.h>
 #include <gtkmm/cssprovider.h>
-#include <gtkmm/styleproperty.h>
 #include <gtkmm/widgetcustomsnapshot.h>
 #include "myextrainit.h"
 
@@ -39,7 +38,8 @@ protected:
   Gtk::SizeRequestMode get_request_mode_vfunc() 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_size_allocate(const Gtk::Allocation& allocation, int baseline,
+    Gtk::Allocation& out_clip) override;
   void on_map() override;
   void on_unmap() override;
   void on_realize() override;
@@ -49,11 +49,9 @@ protected:
   //Signal handler:
   void on_parsing_error(const Glib::RefPtr<const Gtk::CssSection>& section, const Glib::Error& error);
 
-  Gtk::StyleProperty<int> m_scale_prop;
+  Gtk::Border m_padding;
   Glib::RefPtr<Gdk::Window> m_refGdkWindow;
   Glib::RefPtr<Gtk::CssProvider> m_refCssProvider;
-
-  int m_scale;
 };
 
 #endif //GTKMM_CUSTOM_WIDGET_MYWIDGET_H
diff --git a/examples/book/custom/custom_widget/mywidget2.cc b/examples/book/custom/custom_widget/mywidget2.cc
index f70f8a9..0b0e4cb 100644
--- a/examples/book/custom/custom_widget/mywidget2.cc
+++ b/examples/book/custom/custom_widget/mywidget2.cc
@@ -24,18 +24,15 @@ MyWidget2::MyWidget2() :
   //The GType name will actually be gtkmm__CustomObject_MyWidget2
   Glib::ObjectBase("MyWidget2"),
   Gtk::WidgetCustomDraw(),
-  MyExtraInit("my-widget2"),
+  MyExtraInit("my-widget2"), // CSS node name, which must be used in the CSS file.
   Gtk::Widget(),
-  //Install a style property so that an aspect of this widget may be themed
-  //via a CSS style sheet file:
-  m_scale_prop(*this, "example_scale", 500),
-  m_scale(1000)
+  m_padding()
 {
   // Expand, if there is extra space.
   set_hexpand(true);
   set_vexpand(true);
 
-  //This shows the GType name, which must be used in the CSS file.
+  //This shows the GType name.
   std::cout << "GType name: " << G_OBJECT_TYPE_NAME(gobj()) << std::endl;
 
   //This shows that the GType still derives from GtkWidget:
@@ -110,20 +107,21 @@ void MyWidget2::measure_vfunc(Gtk::Orientation orientation, int /* for_size */,
   natural_baseline = -1;
 }
 
-void MyWidget2::on_size_allocate(Gtk::Allocation& allocation)
+void MyWidget2::on_size_allocate(const Gtk::Allocation& allocation,
+  int /* baseline */, Gtk::Allocation& out_clip)
 {
   //Do something with the space that we have actually been given:
   //(We will not be given heights or widths less than we have requested, though
   //we might get more)
 
-  //Use the offered allocation for this container:
-  set_allocation(allocation);
-
   if(m_refGdkWindow)
   {
     m_refGdkWindow->move_resize( allocation.get_x(), allocation.get_y(),
             allocation.get_width(), allocation.get_height() );
   }
+
+  // Use the offered allocation for this widget:
+  out_clip = allocation;
 }
 
 void MyWidget2::on_map()
@@ -145,10 +143,13 @@ void MyWidget2::on_realize()
 
   set_realized();
 
-  //Get the themed style from the CSS file:
-  m_scale = m_scale_prop.get_value();
-  std::cout << "m_scale (example_scale from the theme/css-file) is: "
-      << m_scale << std::endl;
+  //Get the themed padding from the CSS file:
+  m_padding = get_style_context()->get_padding();
+  std::cout << "m_padding from the theme/css-file is"
+    << ": top=" << m_padding.get_top()
+    << ", right=" << m_padding.get_right()
+    << ", bottom=" << m_padding.get_bottom()
+    << ", left=" << m_padding.get_left() << std::endl;
 
   if(!m_refGdkWindow)
   {
@@ -176,8 +177,6 @@ bool MyWidget2::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
   auto clip = get_clip();
   clip.set_x(clip.get_x() - allocation.get_x());
   clip.set_y(clip.get_y() - allocation.get_y());
-  const double scale_x = (double)clip.get_width() / m_scale;
-  const double scale_y = (double)clip.get_height() / m_scale;
   auto refStyleContext = get_style_context();
 
   // paint the background
@@ -185,6 +184,9 @@ bool MyWidget2::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
     clip.get_x(), clip.get_y(), clip.get_width(), clip.get_height());
 
   // draw the foreground
+  cr->translate(m_padding.get_left(), m_padding.get_top());
+  const double scale_x = 0.001 * (clip.get_width() - m_padding.get_left() - m_padding.get_right());
+  const double scale_y = 0.001 * (clip.get_height() - m_padding.get_top() - m_padding.get_bottom());
   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);
diff --git a/examples/book/custom/custom_widget/mywidget2.h b/examples/book/custom/custom_widget/mywidget2.h
index c80b4d3..4a6ea80 100644
--- a/examples/book/custom/custom_widget/mywidget2.h
+++ b/examples/book/custom/custom_widget/mywidget2.h
@@ -18,7 +18,6 @@
 
 #include <gtkmm/widget.h>
 #include <gtkmm/cssprovider.h>
-#include <gtkmm/styleproperty.h>
 #include <gtkmm/widgetcustomdraw.h>
 #include "myextrainit.h"
 
@@ -38,7 +37,8 @@ protected:
   Gtk::SizeRequestMode get_request_mode_vfunc() 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_size_allocate(const Gtk::Allocation& allocation, int baseline,
+    Gtk::Allocation& out_clip) override;
   void on_map() override;
   void on_unmap() override;
   void on_realize() override;
@@ -48,11 +48,9 @@ protected:
   //Signal handler:
   void on_parsing_error(const Glib::RefPtr<const Gtk::CssSection>& section, const Glib::Error& error);
 
-  Gtk::StyleProperty<int> m_scale_prop;
+  Gtk::Border m_padding;
   Glib::RefPtr<Gdk::Window> m_refGdkWindow;
   Glib::RefPtr<Gtk::CssProvider> m_refCssProvider;
-
-  int m_scale;
 };
 
 #endif //GTKMM_CUSTOM_WIDGET_MYWIDGET2_H


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