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

陈凯 chenkai036 at gmail.com
Sun May 3 19:14:00 PDT 2009


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cairographics.org/archives/cairo/attachments/20090504/9b1c39d5/attachment.html 


More information about the cairo mailing list