I am attempting to change Gtk::Image
-derived object by giving it pixbuf, but i cannot figure out how to approach that.
The simple setup can be mimicked as:
#include <gtkmm.h>
#include <iostream>
class MyImage : public Gtk::Image
{
public:
void setPixBuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf);
};
void MyImage::setPixBuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
{
// How can i override the existing pixbuf here?
}
void freeImagePixelData(const guint8* data)
{
delete[] data;
}
Glib::RefPtr<Gdk::Pixbuf> generateTestImage()
{
guint8 *data = new guint8[40*40*4];
for(int i=0; i<40*40*4; )
{
data[i++] = (guint8)255; // R
data[i++] = (guint8)255; // G
data[i++] = (guint8)0; // B
data[i++] = (guint8)255; // A
}
Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data(
data, Gdk::Colorspace::COLORSPACE_RGB, true, 8, 40, 40, 40*4, sigc::ptr_fun(&freeImagePixelData));
return pixbuf;
}
int main(int argc, char** argv)
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.almost-university.gtkmm.image.pixbuf");
Gtk::Window window;
MyImage im1;
im1.setPixBuf(generateTestImage());
window.add(im1);
window.show_all_children();
app->run(window);
return 0;
}
(Please note that this is an oversimplified version of what i am trying to figure out, i do know that i should be using Gtk::manage and not add things directly to the window without another container, this is just a mock-up).
I know that if i were to generate the image using a constructor as so:
Gtk::Image im2(generateTestImage());
window.add(im2);
then i would in fact be getting a yellow square.
Somehow i refuse to believe that one can only use pixbuf at the time of object creation. There must be a way to set the image data somehow, and i just cannot find the needed function.
To set Pixbuf in an Gtk::Image
you can use Gtk::Image::set(const Glib::RefPtr< Gdk::Pixbuf >& pixbuf)
method:
void MyImage::setPixBuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
{
set(pixbuf);
}