Search code examples
gtkgnomecairogtkmmglibmm

How to draw a cairo_surface_t in a Gtkmm DrawingArea


I am trying to draw a cairo_surface_t in Gtkmm DrawingArea.

Can the cairo_surface_t * be used to initialize a Glib::RefPtr which can be passed to the Cairo::Context::set_source?

#include <gtkmm.h>
class CDrawingArea : Gtk::DrawingArea
{
public:
   CDrawingArea();
   virtual ~CDrawingArea();
   void DrawSurface (cairo_surface_t * surface); 
protected:
   bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
   cairo_surface_t * m_surface;
};

#include "CDrawingArea.h"

CDrawingArea::CDrawingArea()
{
}

CDrawingArea::~CDrawingArea()
{
}

void CDrawingArea::DrawSurface (cairo_surface_t * surface)
{
   m_surface = surface;
   Glib::RefPtr<Gdk::Window> win = get_window();
   if (win)
   {
      Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
      win->invalidate_rect(r, false);
   }
}

bool CDrawingArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
   // Cairo::RefPtr<Cairo::Surface> refSurface  ??;
   // cr->set_source(refSurface, 0, 0);
   cr->paint();
}

Solution

  • Are you looking for the constructor of Surface?

    Cairo::Surface::Surface (   cairo_surface_t *   cobject,
    bool    has_reference = false    
    )           [explicit]
    Create a C++ wrapper for the C instance.
    
    This C++ instance should then be given to a RefPtr.
    

    (Copy&Paste broke the formatting, sorry) https://cairographics.org/documentation/cairomm/reference/classCairo_1_1Surface.html#a47b90669a5a85d187cc0d28dc99915c8