79670708

Date: 2025-06-18 13:28:12
Score: 0.5
Natty:
Report link

So as I have found a working solution, I will answer my own question. Here an abstract code how this can work:

bool drawing_test::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{   
  auto surface = Cairo::ImageSurface::create_from_png_stream(sigc::mem_fun(drawBuffer, &DrawBuffer::read));
  cr->set_source(surface, 0, 0);
  cr->paint(); 

  switch (cmd)
  {
    case 0: /* initialize background */
      cr->set_source_rgba(1, 1, 1, 1);  // white
      cr->paint();
      break;
    case 1: /* draw a line */
      DrawLine(cr, params);
      break;
    case 2: /* draw text */
      DrawText(cr, params);
      break;
    default:
      break;
  }

  Cairo::RefPtr<Cairo::Surface> cs=cr->get_target();
  cs->write_to_png_stream(sigc::mem_fun(drawBuffer, &DrawBuffer::write));
}

drawBuffer is a self created class which implements a read and write function and a buffer for a png stream. With this construction its possible to save and restore the contents of a Gtk:DrawingArea.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: WeirdGuy