Gtk::Image, Gtk::ImageSurface, Cairo::Context, Gtk::Pixbuf : how to combine?



Hi

What i want to do is to get a cairo context to draw in, and then
somehow get a pixbuf which i can use to fill a Gtk::Image.
Here's how i do it:
- create an ImageSurface,
- get its Cairo::Context,
- draw into the Context
- create a Pixbuf from th ImageSurface (which now should hold the
drawing made via the Context
- call the Gtk::Image's set-method with the PixBuf
But that way i get a Segmenrtation fault.

In particular, I have an application with a Gtk::Window containing a
Gtk::Image (m_imgMain).
When a button is pressed, i call the window's method paint_image
(which does the steps described above):

void XLoopWin::paint_image() {
    // get current size of image
    Gtk::Allocation allocation = m_imgMain.get_allocation();
    const int iW = allocation.get_width();
    const int iH = allocation.get_height();

    // create  image surface
    Cairo::RefPtr<Cairo::ImageSurface> surface =
        Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, iW, iH);

    // create a context to draw on
    Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface);

    // here i would draw in the cr, but have crash without drawing

     // create a Pixbuf from the ImageSurface
     Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data(
         surface->get_data(), Gdk::COLORSPACE_RGB, true, 8, iW, iH,
surface->get_stride());

    // set the drawing
    m_imgMain.set(pixbuf);

    // make sure image is redrawn
    m_imgMain.queue_draw();
}


This method completes, but immediately afterwards i have a
Segmentation fault deep down

#0  0x00007ffff5fb9227 in gdk_cairo_set_source_pixbuf ()
   from /usr/lib64/libgdk-3.so.0
#1  0x00007ffff68c51d7 in gtk_theming_engine_render_icon ()
   from /usr/lib64/libgtk-3.so.0
#2  0x00007ffff6888333 in gtk_render_icon () from /usr/lib64/libgtk-3.so.0
#3  0x00007ffff67e88ef in _gtk_icon_helper_draw ()
   from /usr/lib64/libgtk-3.so.0
#4  0x00007ffff67f72c9 in gtk_image_draw () from /usr/lib64/libgtk-3.so.0
#5  0x00007ffff7a8372c in
Gtk::Widget_Class::draw_callback(_GtkWidget*, _cairo*) () from
/usr/lib64/libgtkmm-3.0.so.1
...
#43 0x00007ffff478c3ac in g_main_loop_run () from /usr/lib64/libglib-2.0.so.0
#44 0x00007ffff680eda2 in gtk_main () from /usr/lib64/libgtk-3.so.0
#45 0x00007ffff7a2b047 in Gtk::Main::run(Gtk::Window&) ()
   from /usr/lib64/libgtkmm-3.0.so.1
#46 0x0000000000406897 in main (iArgC=1, apArgV=0x7fffffffddd8) at XLoop.cpp:18


Can anybody tell me the correct way to do this?

Thank You
  Jody


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]