Alexander Tumin
Alexander Tumin

Reputation: 1591

gtk_widget_draw() alternative for gtkmm3

I am lookng for a way of rendering some gtk widget container to pixbuf for later use in drag'n'drop icon image using gtkmm3.

The correct way in native gtk3 to do this is, as far i researched, - usage of gtk_widget_draw() on supplied cairo surface.

The creation of this surface is not an issue, however the call to gtk_widget_draw() itself is.

Only place where it mentioned directly in gtkmm-3.4.2 sources is Gtk::Widget::draw() function (widget.cc:310), which is declared as `protected'.

So my question is: how do i properly perform such task (rendering widget to pixbuf) in gtkmm3?

At this point i see two options:

The question is: which way is preferable and why? Or maybe there are some other [better] ways to do it?

Upvotes: 3

Views: 577

Answers (1)

You could use a GtkOffscreenWindow to gather a pixbuf of your widget, from the docs :

The idea is to take a widget and manually set the state of it, add it to an OffscreenWindow and then retrieve the snapshot as a Gdk::Pixbuf.

and the function:

Glib::RefPtr<Gdk::Pixbuf> Gtk::OffscreenWindow::get_pixbuf  (       )   
Retrieves a snapshot of the contained widget in the form of a Gdk::Pixbuf.

You can also gather some example code (in python) and extra tips, from this related question:

How to draw any GTK widget on top of Cairo surface

Upvotes: 2

Related Questions