tags:

views:

11

answers:

0

I am drawing a little graphical display that is expensive to calculate. So I wish to draw the picture into a pixmap, retaining the pixmap and using that pixmap to refresh the drawing area when the drawing area gets expose events.

First of all, is that a reasonable procedure or pointless over-engineering?

If it is reasonable, then I need to know how to get the contents of the pixmap into the drawing area. Here's my code so far...

Class definition:

class set_display_drawing_area : public Gtk::DrawingArea
{
    public:
        set_display_drawing_area          ();
        virtual ~set_display_drawing_area ();
    protected:
        virtual bool on_expose_event(GdkEventExpose* event);
    private:
        int                       saved_width;
        int                       saved_height;
        Glib::RefPtr<Gdk::Pixmap> pixmap;
};

And the expose event callback:

bool set_display_drawing_area::on_expose_event(GdkEventExpose* event)
{
    Glib::RefPtr<Gdk::Window> window = get_window();

    if (window)
    {
        Gtk::Allocation allocation = get_allocation();
        const int width = allocation.get_width();
        const int height = allocation.get_height();

        if (width  != saved_width ||
            height != saved_height)
        {
            saved_width                      = width;
            saved_height                     = height;
            pixmap                           = Gdk::Pixmap::create(window, width, height);
            Cairo::RefPtr<Cairo::Context> cr = pixmap->create_cairo_context();
            // Lots of code here to draw into the pixmap.
        }

        // Need to copy the pixmap to the drawing area - how?
    }

    return true;
}