[gtkmm-documentation] Update custom container, custom widget and advanced printing examples



commit c0cb190f9399e5d21de02b49c376fd89b560e5d0
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Tue Apr 17 15:50:28 2018 +0200

    Update custom container, custom widget and advanced printing examples
    
    Update for the latest gtk+4 and gtkmm-4 changes, such as removed
    Gtk::WidgetCustomDraw, Widget::set_realized(), Widget::get_clip() and
    Gdk::Surface::create_similar_image_surface(),
    modified Widget::signal_size_allocate().

 examples/Makefile.am                               |    4 +-
 .../book/custom/custom_container/mycontainer.cc    |   27 +--
 .../book/custom/custom_container/mycontainer.h     |    3 +-
 examples/book/custom/custom_widget/custom_gtk.css  |    8 +-
 .../book/custom/custom_widget/examplewindow.cc     |    2 -
 examples/book/custom/custom_widget/examplewindow.h |    3 -
 examples/book/custom/custom_widget/mywidget.cc     |   27 +--
 examples/book/custom/custom_widget/mywidget.h      |    3 +-
 examples/book/custom/custom_widget/mywidget2.cc    |  229 --------------------
 examples/book/custom/custom_widget/mywidget2.h     |   56 -----
 examples/book/printing/advanced/previewdialog.cc   |    4 +-
 11 files changed, 21 insertions(+), 345 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index c9996f3..664f8ec 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -284,9 +284,7 @@ book_custom_custom_widget_example_SOURCES =         \
        book/custom/custom_widget/myextrainit.cc \
        book/custom/custom_widget/myextrainit.h \
        book/custom/custom_widget/mywidget.cc           \
-       book/custom/custom_widget/mywidget.h \
-       book/custom/custom_widget/mywidget2.cc  \
-       book/custom/custom_widget/mywidget2.h
+       book/custom/custom_widget/mywidget.h
 
 nodist_book_dialogs_aboutdialog_example_SOURCES = book/dialogs/aboutdialog/resources.c
 book_dialogs_aboutdialog_example_SOURCES =             \
diff --git a/examples/book/custom/custom_container/mycontainer.cc 
b/examples/book/custom/custom_container/mycontainer.cc
index 8a70939..06a8d53 100644
--- a/examples/book/custom/custom_container/mycontainer.cc
+++ b/examples/book/custom/custom_container/mycontainer.cc
@@ -125,8 +125,7 @@ void MyContainer::measure_vfunc(Gtk::Orientation orientation, int for_size,
   }
 }
 
-void MyContainer::on_size_allocate(const Gtk::Allocation& allocation,
-  int  baseline, Gtk::Allocation& out_clip)
+void MyContainer::on_size_allocate(const Gtk::Allocation& allocation, int  baseline)
 {
   //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
@@ -143,18 +142,13 @@ void MyContainer::on_size_allocate(const Gtk::Allocation& allocation,
 
   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);
+    // No visible child.
     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() );
@@ -166,9 +160,8 @@ void MyContainer::on_size_allocate(const Gtk::Allocation& allocation,
   if (visible_one)
   {
     //Divide the height equally among the visible children.
-    child_allocation_one.set_height( allocation.get_height() / nvis_children);
-    child_clip_one = child_allocation_one;
-    m_child_one->size_allocate(child_allocation_one, baseline, child_clip_one);
+    child_allocation_one.set_height(allocation.get_height() / nvis_children);
+    m_child_one->size_allocate(child_allocation_one, baseline);
   }
   else
     child_allocation_one.set_height(0);
@@ -187,18 +180,8 @@ void MyContainer::on_size_allocate(const Gtk::Allocation& allocation,
 
   if (visible_two)
   {
-    child_clip_two = child_allocation_two;
-    m_child_two->size_allocate(child_allocation_two, baseline, child_clip_two);
+    m_child_two->size_allocate(child_allocation_two, baseline);
   }
-
-  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 44182f9..c21f336 100644
--- a/examples/book/custom/custom_container/mycontainer.h
+++ b/examples/book/custom/custom_container/mycontainer.h
@@ -33,8 +33,7 @@ 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(const Gtk::Allocation& allocation, int baseline,
-    Gtk::Allocation& out_clip) override;
+  void on_size_allocate(const Gtk::Allocation& allocation, int baseline) 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 57510ee..515048c 100644
--- a/examples/book/custom/custom_widget/custom_gtk.css
+++ b/examples/book/custom/custom_widget/custom_gtk.css
@@ -3,11 +3,5 @@
 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;
+  padding:          10px 15px 20px 5px; /* top right bottom left */
 }
diff --git a/examples/book/custom/custom_widget/examplewindow.cc 
b/examples/book/custom/custom_widget/examplewindow.cc
index c738a63..e999ded 100644
--- a/examples/book/custom/custom_widget/examplewindow.cc
+++ b/examples/book/custom/custom_widget/examplewindow.cc
@@ -30,8 +30,6 @@ ExampleWindow::ExampleWindow()
 
   m_Grid.attach(m_MyWidgetS1, 0, 0);
   m_Grid.attach(m_MyWidgetS2, 1, 1);
-  m_Grid.attach(m_MyWidgetD1, 0, 1);
-  m_Grid.attach(m_MyWidgetD2, 1, 0);
 
   m_Grid.attach(m_ButtonBox, 0, 2, 2, 1);
 
diff --git a/examples/book/custom/custom_widget/examplewindow.h 
b/examples/book/custom/custom_widget/examplewindow.h
index 3a6e392..d6ac5bd 100644
--- a/examples/book/custom/custom_widget/examplewindow.h
+++ b/examples/book/custom/custom_widget/examplewindow.h
@@ -19,7 +19,6 @@
 
 #include <gtkmm.h>
 #include "mywidget.h"
-#include "mywidget2.h"
 
 class ExampleWindow : public Gtk::Window
 {
@@ -35,8 +34,6 @@ protected:
   Gtk::Grid m_Grid;
   MyWidget m_MyWidgetS1;
   MyWidget m_MyWidgetS2;
-  MyWidget2 m_MyWidgetD1;
-  MyWidget2 m_MyWidgetD2;
   Gtk::ButtonBox m_ButtonBox;
   Gtk::Button m_Button_Quit;
 };
diff --git a/examples/book/custom/custom_widget/mywidget.cc b/examples/book/custom/custom_widget/mywidget.cc
index 6eb5da4..9bf404c 100644
--- a/examples/book/custom/custom_widget/mywidget.cc
+++ b/examples/book/custom/custom_widget/mywidget.cc
@@ -108,8 +108,7 @@ void MyWidget::measure_vfunc(Gtk::Orientation orientation, int /* for_size */,
   natural_baseline = -1;
 }
 
-void MyWidget::on_size_allocate(const Gtk::Allocation& allocation,
-  int /* baseline */, Gtk::Allocation& out_clip)
+void MyWidget::on_size_allocate(const Gtk::Allocation& allocation, int /* baseline */)
 {
   //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
@@ -120,9 +119,6 @@ void MyWidget::on_size_allocate(const Gtk::Allocation& allocation,
     m_refGdkSurface->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()
@@ -139,11 +135,6 @@ void MyWidget::on_unmap()
 
 void MyWidget::on_realize()
 {
-  //Do not call base class Gtk::Widget::on_realize().
-  //It's intended only for widgets that set_has_surface(false).
-
-  set_realized();
-
   //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"
@@ -161,6 +152,9 @@ void MyWidget::on_realize()
     //make the widget receive expose events
     m_refGdkSurface->set_user_data(gobj());
   }
+
+  //Call base class:
+  Gtk::Widget::on_realize();
 }
 
 void MyWidget::on_unrealize()
@@ -174,22 +168,21 @@ void MyWidget::on_unrealize()
 void MyWidget::snapshot_vfunc(const Glib::RefPtr<Gtk::Snapshot>& snapshot)
 {
   const auto allocation = get_allocation();
-  auto clip = get_clip();
-  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());
+  const Gdk::Rectangle rect(0, 0, allocation.get_width(), allocation.get_height());
   auto refStyleContext = get_style_context();
 
   // Create a cairo context to draw on.
-  auto cr = snapshot->append_cairo(clip, "MyCairoNode");
+  auto cr = snapshot->append_cairo(rect, "MyCairoNode");
 
   // paint the background
   refStyleContext->render_background(cr,
-    clip.get_x(), clip.get_y(), clip.get_width(), clip.get_height());
+    -m_padding.get_left(), -m_padding.get_top(), allocation.get_width(), allocation.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());
+  const double scale_x = 0.001 * (allocation.get_width() - m_padding.get_left() - m_padding.get_right());
+  const double scale_y = 0.001 * (allocation.get_height() - m_padding.get_top() - m_padding.get_bottom());
   Gdk::Cairo::set_source_rgba(cr, refStyleContext->get_color());
+  cr->rectangle(0.0, 0.0, 1000.0*scale_x, 1000.0*scale_y);
   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 3b7c1f2..6af3f19 100644
--- a/examples/book/custom/custom_widget/mywidget.h
+++ b/examples/book/custom/custom_widget/mywidget.h
@@ -38,8 +38,7 @@ 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(const Gtk::Allocation& allocation, int baseline,
-    Gtk::Allocation& out_clip) override;
+  void on_size_allocate(const Gtk::Allocation& allocation, int baseline) override;
   void on_map() override;
   void on_unmap() override;
   void on_realize() override;
diff --git a/examples/book/printing/advanced/previewdialog.cc 
b/examples/book/printing/advanced/previewdialog.cc
index fb532d1..562383e 100644
--- a/examples/book/printing/advanced/previewdialog.cc
+++ b/examples/book/printing/advanced/previewdialog.cc
@@ -84,8 +84,8 @@ void PreviewDialog::on_drawing_area_realized()
     const int scale = gdk_surface->get_scale_factor();
     const int width = gdk_surface->get_width() * scale;
     const int height = gdk_surface->get_height() * scale;
-    auto cairo_surface = gdk_surface->create_similar_image_surface(
-      Cairo::Surface::Format::ARGB32, width, height, scale);
+    auto cairo_surface = gdk_surface->create_similar_surface(
+      Cairo::Content::CONTENT_COLOR, width, height);
     m_refCairoContext = Cairo::Context::create(cairo_surface);
 
     if (m_refPrintContext)


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