Search code examples
cairoondrawgtkmm3drawingarea

How to trigger on_draw method in gtkmm 3.0 for a DrawingArea object


Using gtkmm 3.0, in a Gtk::Window object I have an object of a self defined class derived from the Gtk::DrawingArea named Anzeige.

#include <gtkmm/drawingarea.h>

class Anzeige : public Gtk::DrawingArea
{
public:
  Anzeige();
  virtual ~Anzeige(); 
  
private:
  //Override default signal handler:
  bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
  void zeigeTemp(const Cairo::RefPtr<Cairo::Context>& cr, float t_IST, float t_SOLL, int x, int y);

  //member data
  //...//
  float t_Elt_IST;
  float t_Elt_SOLL;
};

An override function of the on_draw method is there and gets a Cairo::Context as a parameter named cr. In the function definition of this on_draw override, I successfully make use of cr to show a bitmap and some pango layouts. The function zeigeTemp shows a pango layout at position x and y. zeigeTemp is called in the on_draw function and gets the same Cairo::Context cr.

When values are updated in my program, I want to call either the on_draw method or zeigeTemp manually, just so the screen is up to date. But in order to call the methods, I need to pass the Cairo::Context to them.

I do not make explicit use of the Cairo::Context anywhere else in my code by now. The on_draw method is called by the program magically by itself and some Cairo::Context is used for it.

Is there an easier way to tell Gtk to redraw a DrawingArea without having to specify a Cairo::Context? When I click in the DrawingArea it is redrawn without additional input. So there must be a way to make Gtk do this on command. Which command is it?

When I try to extract the Cairo::Context from the Gtk::Window with the create_cairo_context method I get a segmentation fault error on runtime. I do not understand, when Gtk uses a Cairo::Context by itself and when I need to create it to pass it to for example the method zeigeTemp.


Solution

  • I searched for "redraw" on https://developer-old.gnome.org/gtkmm/stable/classGtk_1_1DrawingArea.html and found this paragraph:

    You can also force a redraw by adding to the “damage region” of the drawing area’s window using Gtk::Widget::queue_draw_region(), Gtk::Widget::queue_draw_area() or Gtk::Widget::queue_draw().