[ANN] CairoPad (wxCairoContext?)

Hakki Dogusan dogusanh at dynaset.org
Fri Dec 14 08:40:01 PST 2007


Hi,

Robert Roebling wrote:
>> ps. I did some experiment with Cairo using wx/cpp, before using it with 
>> wxLua. I modified a local copy of src/generic/graphicc.cpp to use Cairo 
>> in win32. Drawing sample's alpha test worked. But, I think, to use Cairo 
>> API directly, a wxCairoContext and wxCairoCanvas (similar to 
>> wxGLContext,wxGLCanvas) would be a good addition.
> 
> I wonder what this would do. Wouldn't it be enough to to add a
> function like 
> 
> cairo_t* GetCairoFromWindow( wxWindow *);
> 

Exactly. I was thinking a minimalist class.

To create a cairo_t for screen surface(wxDC), I'm doing following 
hip-hops (these are for Lua usage, but..):

///// for wxLua side:
static int LUACALL get_drawable(lua_State *L)
{
     wxLuaState wxlState(L);

#ifdef __WXMSW__
     wxPaintDC * dc = (wxPaintDC *)wxlState.GetUserDataType(1, 
s_wxluatag_wxDC);
     HDC hdc = (HDC)dc->GetHDC();
     lua_pushlightuserdata(L, hdc);
     return 1;
#endif

#ifdef __WXGTK__
     wxWindowDC * dc = (wxWindowDC *)wxlState.GetUserDataType(1, 
s_wxluatag_wxDC);
     GdkDrawable *drawable = dc->m_window;
     lua_pushlightuserdata(L, drawable);
     return 1;
#endif

     return 0;
}

///// for Cairo side:
static int l_CreateContext(lua_State* L)
{
#if CAIRO_HAS_WIN32_SURFACE
     HDC hdc = (HDC) check_lightuserdata(L, 1);
     cairo_t *cr = cairo_create(cairo_win32_surface_create( hdc ));
     lua_pushlightuserdata(L, cr);
     return 1;
#endif

#ifdef GDK_WINDOWING_X11
     GdkDrawable *drawable = (GdkDrawable *) check_lightuserdata(L, 1);
     cairo_t *cr = gdk_cairo_create( drawable ) ;
     lua_pushlightuserdata(L, cr);
     return 1;
#endif

     return 0;
}


/////// and using it in Lua in OnPaint event
local dc = wx.wxPaintDC(panel)
local cr = cairo.CreateContext(wx.get_drawable(dc)) --HACK!
... use Cairo API here
cairo.destroy (cr)
dc:delete()


> and be done with it? 

Yes, that is enough for me!

 From then on you'd just use the Cairo API
> which is documented. We might need to keep track of update
> regions when painting or help convert from a wxRegion to whatever
> Cairo uses. OK, maybe a class which also takes care of destroying
> the cairo_t would be a plus. So we might have a 
> 
> wxCairoContext ctx( this );
> cairo_draw_line( ctx );
> 
> and in a wxScrolledWindow we'd have to do something like this
> 
> wxCairoContext ctx( this );
> ctx.OnPrepare( this );
> cairo_draw_line( ctx, 10, 10, 100, 100 );
> 
> Is there any sense in wrapping the entire Cairo library plus
> its Pango cousin? I can agree that writing 
> 
> ctx.draw_line( 10, 10, 100, 100 );
> 
> would be a little nicer, but it doesn't seem to be worth the
> effort.
> 

Absolutely!

> Any other ideas or suggestions welcome.
> 
>   Robert


Thanks for sharing your thoughts.


--
Regards,
Hakki Dogusan




More information about the wx-users mailing list