Σχεδίαση εικόνων
Υπάρχει μια μέθοδος για σχεδίαση από μια Gdk::Pixbuf σε μια Cairo::Context. Μια ενδιάμεση μνήμη Gdk::Pixbuf είναι ένας χρήσιμος συσκευαστής γύρω από μια συλλογή εικονοστοιχείων, που μπορεί να διαβαστεί από αρχεία και χειρίζεται με ποικίλους τρόπους.
Προφανώς ο πιο συνηθισμένος τρόπος δημιουργίας Gdk::Pixbufs είναι η χρήση Gdk::Pixbuf::create_from_file(), που μπορεί να διαβάσει ένα αρχείο εικόνας, όπως ένα αρχείο png σε ένα pixbuf έτοιμο για απόδοση.
Η Gdk::Pixbuf μπορεί να απεικονιστεί ορίζοντας την ως το πηγαίο υπόδειγμα του περιεχομένου Cairo με την Gdk::Cairo::set_source_pixbuf(). Έπειτα, σχεδιάστε την εικόνα με είτε Cairo::Context::paint() (για σχεδίαση ολόκληρης της εικόνας), ή Cairo::Context::rectangle() και Cairo::Context::fill() (για γέμισμα του συγκεκριμένου ορθογωνίου). Η set_source_pixbuf() δεν είναι μέλος της Cairo::Context. Παίρνει μια Cairo::Context ως την πρώτη της παράμετρο.
Ιδού ένα μικρό τμήμα κώδικα για σύζευξη τους όλων μαζί: (Σημειώστε ότι συνήθως δεν θα πρέπει να φορτώνετε την εικόνα κάθε φορά στον χειριστή σήματος σχεδίασης! Εμφανίζεται απλά εδώ για να τα διατηρήσει όλα μαζί.)
bool MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_file("myimage.png");
// Draw the image at 110, 90, εκτός από το πιο εξωτερικό 10 pixels.
Gdk::Cairo::set_source_pixbuf(cr, image, 100, 80);
cr->rectangle(110, 90, image->get_width()-20, image->get_height()-20);
cr->fill();
return true;
}- 17.6.1. Παράδειγμα
17.6.1. Παράδειγμα
Ιδού ένα παράδειγμα απλού προγράμματος που σχεδιάζει μια εικόνα.
File: myarea.h (For use with gtkmm 3, not gtkmm 2)
#ifndef GTKMM_EXAMPLE_MYAREA_H
#define GTKMM_EXAMPLE_MYAREA_H
#include <gtkmm/drawingarea.h>
#include <gdkmm/pixbuf.h>
class MyArea : public Gtk::DrawingArea
{
public:
MyArea();
virtual ~MyArea();
protected:
//Override default signal handler:
virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
Glib::RefPtr<Gdk::Pixbuf> m_image;
};
#endif // GTKMM_EXAMPLE_MYAREA_H
File: myarea.cc (For use with gtkmm 3, not gtkmm 2)
#include "myarea.h"
#include <cairomm/context.h>
#include <gdkmm/general.h> // set_source_pixbuf()
#include <glibmm/fileutils.h>
#include <iostream>
MyArea::MyArea()
{
try
{
// The fractal image has been created by the XaoS program.
// http://xaos.sourceforge.net
m_image = Gdk::Pixbuf::create_from_file("fractal_image.png");
}
catch(const Glib::FileError& ex)
{
std::cerr << "FileError: " << ex.what() << std::endl;
}
catch(const Gdk::PixbufError& ex)
{
std::cerr << "PixbufError: " << ex.what() << std::endl;
}
// Show at least a quarter of the image.
if (m_image)
set_size_request(m_image->get_width()/2, m_image->get_height()/2);
}
MyArea::~MyArea()
{
}
bool MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
if (!m_image)
return false;
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
// Draw the image in the middle of the drawing area, or (if the image is
// larger than the drawing area) draw the middle part of the image.
Gdk::Cairo::set_source_pixbuf(cr, m_image,
(width - m_image->get_width())/2, (height - m_image->get_height())/2);
cr->paint();
return true;
}
File: main.cc (For use with gtkmm 3, not gtkmm 2)
#include "myarea.h"
#include <gtkmm/application.h>
#include <gtkmm/window.h>
int main(int argc, char** argv)
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
Gtk::Window win;
win.set_title("DrawingArea");
win.set_default_size(300, 200);
MyArea area;
win.add(area);
area.show();
return app->run(win);
}
