[cairo] How to refer Cairo::Context outside on_expose_event

Diego Jacobi jacobidiego at gmail.com
Mon May 4 04:56:30 PDT 2009


You can draw to a surface outside expose-event, but then you HAVE TO
draw the surface IN an expose-event. This is oftern more efficient for
complicated programs, but a simple call to set_line_width wont make
your program slow.

Cheers.
Diego


2009/5/4 陈凯 <chenkai036 at gmail.com>:
> Greetings,
>
> Maybe a silly question but I still wonder why.  Can it possible for
> Cairo::Context to be referred outside on_expose_event?  I just want to
> preserve some configurations of the context, like line width and dash
> pattern, instead of always call set_line_width() inside on_expose_event,
> which I think is inefficient.
>
> I tried to add a Cairo::RefPtr<Cairo::Context> member in my class and
> implement it like this
>
> ////////////////////////////////////////////////////////////////////////////////////////////////////////////
> class MyDrawingArea : public Gtk::DrawingArea
> {
> // bla bla bla
> public:
>         Cairo::RefPtr<Cairo::Context> ctx_;
> };
>
> MyDrawingArea::MyDrawingArea()
> {
>         Glib::RefPtr<Gdk::Window> window = get_window();
>         if (window)
>         {
>                 ctx_ = window->create_cairo_context();
>                 // set basic graphical style for ctx_
>         }
> }
>
> bool MyDrawingArea::on_expose_event(GdkEventExpose* event)
> {
>         // Call cairo drawing primitives via ctx_
>         ctx_->XXX
>         return true;
> }
> /////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> Unfortunately, the execution always crashes because get_window() in the
> constructor returns NULL.  I have no idea at why this goes wrong.  Could any
> one give me some explanations or solutions, thanks in advance.
>
> ========
> CHEN Kai
>
> _______________________________________________
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo
>

<div><br></div>


More information about the cairo mailing list