gtkmm – example 16

This is part of a more general work dedicated to gtkmm accessible here. This example shows ho to display an image in a drawing area created with Cairo.

capture

Download

Source code

main.cpp

#include <drawing.h>
#include <gtkmm.h>

int main(int argc, char* argv[])
{
	// Initialize gtkmm and create the main window
	Glib::RefPtr app = Gtk::Application::create(argc, argv, "www.lucidarme.me");
	Gtk::Window window;

	// Create the drawing
	Drawing Dwg;

	// Insert the drawing in the window
	window.add(Dwg);

	// Show the drawing
	Dwg.show();

	// Start main loop
	return app->run(window);
}

drawing.h

#ifndef DRAWING_H
#define DRAWING_H

#include <iostream>
#include <gtkmm.h>

class Drawing : public Gtk::DrawingArea
{
public:
	Drawing();

protected:
	//Override default signal handler:
	virtual bool on_draw(const Cairo::RefPtr& cr);

private:
	Glib::RefPtr image;
};

#endif // DRAWING_H

drawing.cpp

#include "drawing.h"

Drawing::Drawing()
{
	// Load the image
	image = Gdk::Pixbuf::create_from_file("gtk.png");
}


bool Drawing::on_draw(const Cairo::RefPtr& cr)
{

	// Place the image at 0,0
	Gdk::Cairo::set_source_pixbuf(cr, image, 0,0);
	// Update the area where the image is located
	cr->rectangle(0, 0, image->get_width(), image->get_height());
	// Fill the area with the image
	cr->fill();

	return true;
}

2 thoughts on “gtkmm – example 16

  1. You’re missing the includes in the .h example.

    If you use “<” and “>” in place of the angle brackets “” on either side of the intended .h file it will show up as it ought to.

    Using “” won’t display as the browser thinks the include is a HTML tag.

    (example 16)

Leave a Reply

Your email address will not be published. Required fields are marked *