[gtkmm-documentation] examples: Update to latest gtkmm4 (Gtk::GestureClick, etc.)



commit 259a9d46b757d05b57e9b97d5227123eb08ff1a6
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Tue Jun 4 09:16:48 2019 +0200

    examples: Update to latest gtkmm4 (Gtk::GestureClick, etc.)

 .../book/custom/custom_container/mycontainer.cc    |  1 -
 examples/book/custom/custom_widget/myextrainit.cc  |  4 +-
 examples/book/custom/custom_widget/mywidget.cc     | 24 ----------
 examples/book/custom/custom_widget/mywidget.h      |  2 -
 examples/book/drawingarea/joins/myarea.cc          | 51 ++++++++++------------
 examples/book/menus/popup/examplewindow.cc         |  2 +-
 examples/book/menus/popup/examplewindow.h          |  2 +-
 examples/book/popover/examplewindow.cc             |  3 +-
 examples/book/printing/advanced/previewdialog.cc   | 26 ++++++-----
 examples/book/treeview/popup/treeview_withpopup.cc |  2 +-
 .../others/cellrenderercustom/cellrendererlist.cc  |  2 +-
 .../others/cellrenderercustom/cellrendererlist.h   |  2 +-
 .../others/cellrenderercustom/cellrendererpopup.cc | 22 ++++------
 .../others/cellrenderercustom/cellrendererpopup.h  |  2 +-
 examples/others/dnd/dndwindow.cc                   |  2 -
 15 files changed, 56 insertions(+), 91 deletions(-)
---
diff --git a/examples/book/custom/custom_container/mycontainer.cc 
b/examples/book/custom/custom_container/mycontainer.cc
index 18e8b9a..2e7ec41 100644
--- a/examples/book/custom/custom_container/mycontainer.cc
+++ b/examples/book/custom/custom_container/mycontainer.cc
@@ -21,7 +21,6 @@
 MyContainer::MyContainer()
 : m_child_one(nullptr), m_child_two(nullptr)
 {
-  set_has_surface(false);
 }
 
 MyContainer::~MyContainer()
diff --git a/examples/book/custom/custom_widget/myextrainit.cc 
b/examples/book/custom/custom_widget/myextrainit.cc
index 21b2f85..ecd7cfc 100644
--- a/examples/book/custom/custom_widget/myextrainit.cc
+++ b/examples/book/custom/custom_widget/myextrainit.cc
@@ -38,7 +38,9 @@ void instance_init_function(GTypeInstance* instance, void* /* g_class */)
 {
   g_return_if_fail(GTK_IS_WIDGET(instance));
 
-  gtk_widget_set_has_surface(GTK_WIDGET(instance), true);
+  // Nothing to do here.
+  // This extra instance init function just shows how such a function can
+  // be added to a custom widget, if necessary.
 }
 
 } // anonymous namespace
diff --git a/examples/book/custom/custom_widget/mywidget.cc b/examples/book/custom/custom_widget/mywidget.cc
index fcb3c4c..7583a6e 100644
--- a/examples/book/custom/custom_widget/mywidget.cc
+++ b/examples/book/custom/custom_widget/mywidget.cc
@@ -96,18 +96,6 @@ void MyWidget::measure_vfunc(Gtk::Orientation orientation, int /* for_size */,
   natural_baseline = -1;
 }
 
-void MyWidget::on_size_allocate(int width, int height, 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
-  //we might get more)
-
-  if(m_refGdkSurface)
-  {
-    m_refGdkSurface->move_resize(0, 0, width, height);
-  }
-}
-
 void MyWidget::on_map()
 {
   //Call base class:
@@ -130,24 +118,12 @@ void MyWidget::on_realize()
     << ", bottom=" << m_padding.get_bottom()
     << ", left=" << m_padding.get_left() << std::endl;
 
-  if(!m_refGdkSurface)
-  {
-    //Create the GdkSurface:
-    m_refGdkSurface = Gdk::Surface::create_child(get_parent()->get_surface(), get_allocation());
-    set_surface(m_refGdkSurface);
-
-    // Make the widget receive expose events
-    register_surface(m_refGdkSurface);
-  }
-
   //Call base class:
   Gtk::Widget::on_realize();
 }
 
 void MyWidget::on_unrealize()
 {
-  m_refGdkSurface.reset();
-
   //Call base class:
   Gtk::Widget::on_unrealize();
 }
diff --git a/examples/book/custom/custom_widget/mywidget.h b/examples/book/custom/custom_widget/mywidget.h
index c3bb027..49e8fe2 100644
--- a/examples/book/custom/custom_widget/mywidget.h
+++ b/examples/book/custom/custom_widget/mywidget.h
@@ -36,7 +36,6 @@ 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(int width, int height, int baseline) override;
   void on_map() override;
   void on_unmap() override;
   void on_realize() override;
@@ -47,7 +46,6 @@ protected:
   void on_parsing_error(const Glib::RefPtr<const Gtk::CssSection>& section, const Glib::Error& error);
 
   Gtk::Border m_padding;
-  Glib::RefPtr<Gdk::Surface> m_refGdkSurface;
   Glib::RefPtr<Gtk::CssProvider> m_refCssProvider;
 };
 
diff --git a/examples/book/drawingarea/joins/myarea.cc b/examples/book/drawingarea/joins/myarea.cc
index d7de919..1c9726e 100644
--- a/examples/book/drawingarea/joins/myarea.cc
+++ b/examples/book/drawingarea/joins/myarea.cc
@@ -28,33 +28,28 @@ MyArea::~MyArea()
 
 void MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height)
 {
-  // This is where we draw on the surface
-  auto surface = get_surface();
-  if(surface)
-  {
-    cr->set_line_width(10.0);
+  cr->set_line_width(10.0);
 
-    // Scale to unit square
-    cr->scale(width, height);
-    cr->save();
-    cr->set_source_rgb(1, 1, 1);
-    cr->paint();
-    cr->restore();
-    cr->set_line_width(0.12);
-    cr->move_to(0.3, 0.3);
-    cr->rel_line_to(0.2, -0.2);
-    cr->rel_line_to(0.2, 0.2);
-    cr->set_line_join(Cairo::Context::LineJoin::MITER); /* default */
-    cr->stroke();
-    cr->move_to(0.3, 0.6);
-    cr->rel_line_to(0.2, -0.2);
-    cr->rel_line_to(0.2, 0.2);
-    cr->set_line_join(Cairo::Context::LineJoin::BEVEL);
-    cr->stroke();
-    cr->move_to(0.3, 0.9);
-    cr->rel_line_to(0.2, -0.2);
-    cr->rel_line_to(0.2, 0.2);
-    cr->set_line_join(Cairo::Context::LineJoin::ROUND);
-    cr->stroke();
-  }
+  // Scale to unit square
+  cr->scale(width, height);
+  cr->save();
+  cr->set_source_rgb(1, 1, 1);
+  cr->paint();
+  cr->restore();
+  cr->set_line_width(0.12);
+  cr->move_to(0.3, 0.3);
+  cr->rel_line_to(0.2, -0.2);
+  cr->rel_line_to(0.2, 0.2);
+  cr->set_line_join(Cairo::Context::LineJoin::MITER); /* default */
+  cr->stroke();
+  cr->move_to(0.3, 0.6);
+  cr->rel_line_to(0.2, -0.2);
+  cr->rel_line_to(0.2, 0.2);
+  cr->set_line_join(Cairo::Context::LineJoin::BEVEL);
+  cr->stroke();
+  cr->move_to(0.3, 0.9);
+  cr->rel_line_to(0.2, -0.2);
+  cr->rel_line_to(0.2, 0.2);
+  cr->set_line_join(Cairo::Context::LineJoin::ROUND);
+  cr->stroke();
 }
diff --git a/examples/book/menus/popup/examplewindow.cc b/examples/book/menus/popup/examplewindow.cc
index 865b5d8..dc3b67b 100644
--- a/examples/book/menus/popup/examplewindow.cc
+++ b/examples/book/menus/popup/examplewindow.cc
@@ -30,7 +30,7 @@ ExampleWindow::ExampleWindow()
   // Catch button_press events:
   m_Box.add(m_Label);
   m_Label.set_expand();
-  m_refGesture = Gtk::GestureMultiPress::create();
+  m_refGesture = Gtk::GestureClick::create();
   m_refGesture->set_button(GDK_BUTTON_SECONDARY);
   m_refGesture->signal_pressed().connect(
     sigc::mem_fun(*this, &ExampleWindow::on_label_pressed));
diff --git a/examples/book/menus/popup/examplewindow.h b/examples/book/menus/popup/examplewindow.h
index 53cda0a..7da2733 100644
--- a/examples/book/menus/popup/examplewindow.h
+++ b/examples/book/menus/popup/examplewindow.h
@@ -36,7 +36,7 @@ protected:
   Gtk::Label m_Label;
 
   Glib::RefPtr<Gtk::Builder> m_refBuilder;
-  Glib::RefPtr<Gtk::GestureMultiPress> m_refGesture;
+  Glib::RefPtr<Gtk::GestureClick> m_refGesture;
 
   Gtk::Menu* m_pMenuPopup;
 };
diff --git a/examples/book/popover/examplewindow.cc b/examples/book/popover/examplewindow.cc
index 7fd16e3..0dd97eb 100644
--- a/examples/book/popover/examplewindow.cc
+++ b/examples/book/popover/examplewindow.cc
@@ -74,7 +74,7 @@ void ExampleWindow::configure_form_popover()
   m_toggle_form_popover.add(m_toggle_form_grid);
   m_toggle_form_popover.set_position(Gtk::PositionType::BOTTOM);
   m_toggle_form_popover.set_margin(6);
-  m_toggle_form_popover.set_modal(false);
+  m_toggle_form_popover.set_autohide(false);
 }
 
 void ExampleWindow::configure_form_buttons()
@@ -153,7 +153,6 @@ void ExampleWindow::on_day_selected()
   double x = 0.0;
   double y = 0.0;
   current_event_button->get_coords(x, y);
-  current_event_button->get_surface()->coords_to_parent(x, y, x, y);
   Gdk::Rectangle rect;
   auto allocation = m_calendar.get_allocation();
   rect.set_x(x - allocation.get_x());
diff --git a/examples/book/printing/advanced/previewdialog.cc 
b/examples/book/printing/advanced/previewdialog.cc
index 7bc582c..31a1076 100644
--- a/examples/book/printing/advanced/previewdialog.cc
+++ b/examples/book/printing/advanced/previewdialog.cc
@@ -80,18 +80,22 @@ PreviewDialog::~PreviewDialog()
 
 void PreviewDialog::on_drawing_area_realized()
 {
-  auto gdk_surface = m_DrawingArea.get_surface();
-  if (gdk_surface)
+  auto gtk_native = m_DrawingArea.get_native();
+  if (gtk_native)
   {
-    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_surface(
-      Cairo::Content::CONTENT_COLOR, width, height);
-    m_refCairoContext = Cairo::Context::create(cairo_surface);
-
-    if (m_refPrintContext)
-      m_refPrintContext->set_cairo_context(m_refCairoContext, 72, 72);
+    auto gdk_surface = gtk_native->get_surface();
+    if (gdk_surface)
+    {
+      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_surface(
+        Cairo::Content::CONTENT_COLOR, width, height);
+      m_refCairoContext = Cairo::Context::create(cairo_surface);
+
+      if (m_refPrintContext)
+        m_refPrintContext->set_cairo_context(m_refCairoContext, 72, 72);
+    }
   }
 }
 
diff --git a/examples/book/treeview/popup/treeview_withpopup.cc 
b/examples/book/treeview/popup/treeview_withpopup.cc
index 57191f2..96c6a1d 100644
--- a/examples/book/treeview/popup/treeview_withpopup.cc
+++ b/examples/book/treeview/popup/treeview_withpopup.cc
@@ -41,7 +41,7 @@ TreeView_WithPopup::TreeView_WithPopup()
   append_column("Name", m_Columns.m_col_name);
 
   // Catch button press events:
-  auto refGesture = Gtk::GestureMultiPress::create();
+  auto refGesture = Gtk::GestureClick::create();
   refGesture->set_button(GDK_BUTTON_SECONDARY);
   refGesture->signal_pressed().connect(
     sigc::mem_fun(*this, &TreeView_WithPopup::on_popup_button_pressed));
diff --git a/examples/others/cellrenderercustom/cellrendererlist.cc 
b/examples/others/cellrenderercustom/cellrendererlist.cc
index 1b6815e..92f7e08 100644
--- a/examples/others/cellrenderercustom/cellrendererlist.cc
+++ b/examples/others/cellrenderercustom/cellrendererlist.cc
@@ -47,7 +47,7 @@ CellRendererList::CellRendererList()
   tree_view_.set_headers_visible(false);
   tree_view_.append_column("", popup_columns().item);
 
-  gesture_ = Gtk::GestureMultiPress::create();
+  gesture_ = Gtk::GestureClick::create();
   gesture_->set_button(GDK_BUTTON_PRIMARY);
   gesture_->signal_released().connect(
     sigc::mem_fun(*this, &Self::on_tree_view_released));
diff --git a/examples/others/cellrenderercustom/cellrendererlist.h 
b/examples/others/cellrenderercustom/cellrendererlist.h
index 3f9549b..dc7c0ca 100644
--- a/examples/others/cellrenderercustom/cellrendererlist.h
+++ b/examples/others/cellrenderercustom/cellrendererlist.h
@@ -37,7 +37,7 @@ private:
 
   Glib::RefPtr<Gtk::ListStore>  list_store_;
   Gtk::TreeView                 tree_view_;
-  Glib::RefPtr<Gtk::GestureMultiPress> gesture_;
+  Glib::RefPtr<Gtk::GestureClick> gesture_;
 
   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 9142f5c..b86e1d6 100644
--- a/examples/others/cellrenderercustom/cellrendererpopup.cc
+++ b/examples/others/cellrenderercustom/cellrendererpopup.cc
@@ -55,7 +55,7 @@ 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));
 
-  gesture_ = Gtk::GestureMultiPress::create();
+  gesture_ = Gtk::GestureClick::create();
   gesture_->set_button(GDK_BUTTON_PRIMARY);
   gesture_->signal_pressed().connect(
     sigc::mem_fun(*this, &Self::on_popup_window_pressed));
@@ -165,10 +165,6 @@ void CellRendererPopup::on_show_popup(const Glib::ustring&, int, int y1, int x2,
 {
   // I'm not sure this is ok to do, but we need to show the window to be
   // able to get the allocation right.
-  // 2019-04-07: gtk_window_move() and Gtk::Window::move() have been removed.
-  // Use set_position() for the time being.
-  //popup_window_.move(-500, -500);
-  popup_window_.set_position(Gtk::WindowPosition::MOUSE);
   popup_window_.show();
 
   const auto alloc = popup_window_.get_allocation();
@@ -179,7 +175,7 @@ void CellRendererPopup::on_show_popup(const Glib::ustring&, int, int y1, int x2,
   const int button_height = y2 - y1;
 
   auto display = Gdk::Display::get_default();
-  auto monitor = display->get_monitor_at_point(x2, y2);
+  auto monitor = display->get_primary_monitor();
   Gdk::Rectangle workarea;
   monitor->get_workarea(workarea);
   int monitor_height = workarea.get_height() - y;
@@ -236,24 +232,21 @@ void CellRendererPopup::on_hide_popup()
   editing_canceled_ = false;
 }
 
-void CellRendererPopup::on_popup_window_pressed(int /* n_press */, double /* x */, double /* y */)
+void CellRendererPopup::on_popup_window_pressed(int /* n_press */, double x, double y)
 {
   // 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().
+  // 2019-06-03: Gdk::EventButton::get_root_coords() is gone. Use the input 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<const Gdk::EventButton>(event)->get_root_coords(x, y);
-
   int xoffset = 0, yoffset = 0;
-  popup_window_.get_surface()->get_root_origin(xoffset, yoffset);
+  //popup_window_.get_surface()->get_root_origin(xoffset, yoffset);
 
   const auto alloc = popup_window_.get_allocation();
 
@@ -317,13 +310,14 @@ void CellRendererPopup::on_popup_arrow_clicked()
     return;
   }
 
-  if(!grab_on_window(popup_entry_->get_surface()))
+  auto native = popup_entry_->get_native();
+  if(!native || !grab_on_window(native->get_surface()))
     return;
 
   popup_entry_->select_region(0, 0);
 
   int x = 0, y = 0;
-  popup_entry_->get_surface()->get_origin(x, y);
+  native->get_surface()->get_position(x, y);
 
   const auto alloc = popup_entry_->get_allocation();
 
diff --git a/examples/others/cellrenderercustom/cellrendererpopup.h 
b/examples/others/cellrenderercustom/cellrendererpopup.h
index 4b1dc63..b57aaa3 100644
--- a/examples/others/cellrenderercustom/cellrendererpopup.h
+++ b/examples/others/cellrenderercustom/cellrendererpopup.h
@@ -69,7 +69,7 @@ private:
   PopupEntry*   popup_entry_;
   bool          shown_;
   bool          editing_canceled_;
-  Glib::RefPtr<Gtk::GestureMultiPress> gesture_;
+  Glib::RefPtr<Gtk::GestureClick> gesture_;
 
   void on_popup_window_pressed(int n_press, double x, double y);
   bool on_popup_window_key_pressed(guint keyval, guint keycode, Gdk::ModifierType state);
diff --git a/examples/others/dnd/dndwindow.cc b/examples/others/dnd/dndwindow.cc
index adc1252..adbb963 100644
--- a/examples/others/dnd/dndwindow.cc
+++ b/examples/others/dnd/dndwindow.cc
@@ -235,8 +235,6 @@ bool DnDWindow::on_popup_timeout()
 
 void DnDWindow::create_popup()
 {
-  m_PopupWindow.set_position(Gtk::WindowPosition::MOUSE);
-
   //Create Grid and fill it:
   auto pGrid = Gtk::make_managed<Gtk::Grid>();
 


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