Το πρότυπο σχεδίασης Cairo

Η βασική ιδέα σχεδίασης στο Cairo εμπεριέχει τον ορισμό 'αόρατων' μονοπατιών και έπειτα βάψιμο ή γέμισμά τους για να γίνουν ορατά.

To do any drawing in gtkmm with Cairo, you must first get a Cairo::Context object. This class holds all of the graphics state parameters that describe how drawing is to be done. This includes information such as line width, color, the surface to draw to, and many other things. This allows the actual drawing functions to take fewer arguments to simplify the interface. Usually, you use the Cairo::Context that you get as input data to the draw function that you set with the call to set_draw_func(). It's also possible to create a Cairo::Context by calling the Gdk::Surface::create_cairo_context() and Gdk::CairoContext::cairo_create() functions. Since Cairo contexts are reference-counted objects, cairo_create() returns a Cairo::RefPtr<Cairo::Context> object. (Note the difference between Gdk::CairoContext and Cairo::Context.)

Το παρακάτω παράδειγμα εμφανίζει πώς να ρυθμιστεί ένα περιεχόμενο Cairo με ένα χρώμα προσκηνίου κόκκινου και ένα πλάτος 2. Οποιεσδήποτε λειτουργίες σχεδίασης που χρησιμοποιούν αυτό το περιεχόμενο θα χρησιμοποιήσουν αυτές τις ρυθμίσεις.

Gtk::DrawingArea myArea;
auto gdkCairoContext = myArea.get_surface()->create_cairo_context();
auto myContext = gdkCairoContext->cairo_create();
myContext->set_source_rgb(1.0, 0.0, 0.0);
myContext->set_line_width(2.0);
    

Each Cairo::Context is associated with a particular Gdk::Surface, so the first line of the above example creates a Gtk::DrawingArea widget and the next two lines use its associated Gdk::Surface to create a Cairo::Context object. The final two lines change the graphics state of the context.

Υπάρχει ένας αριθμός από μεταβλητές κατάστασης γραφικών που μπορεί να οριστεί για περιεχόμενο Cairo. Τα πιο κοινά γνωρίσματα περιεχομένου είναι χρώμα (χρησιμοποιώντας set_source_rgb() ή set_source_rgba() για ημιδιαφανή χρώματα), πλάτος γραμμής (χρησιμοποιώντας set_line_width()), μοτίβο παύλας γραμμής (χρησιμοποιώντας set_dash()), τεχνοτροπία άκρου γραμμής (χρησιμοποιώντας set_line_cap()), και τεχνοτροπία ένωσης γραμμών (χρησιμοποιώντας set_line_join()), και τεχνοτροπίες γραμματοσειρών (χρησιμοποιώντας set_font_size(), set_font_face() και άλλες). Υπάρχουν επίσης πολλές άλλες ρυθμίσεις, όπως πίνακες μετασχηματισμών, γέμισμα κανόνων, αν θα εκτελεστεί εξομάλυνση κι άλλες. Για περισσότερες πληροφορίες, δείτε την τεκμηρίωση API cairomm.

The current state of a Cairo::Context can be saved to an internal stack of saved states and later be restored to the state it was in when you saved it. To do this, use the save() method and the restore() method. This can be useful if you need to temporarily change the line width and color (or any other graphics setting) in order to draw something and then return to the previous settings. In this situation, you could call Cairo::Context::save(), change the graphics settings, draw the lines, and then call Cairo::Context::restore() to restore the original graphics state. Multiple calls to save() and restore() can be nested; each call to restore() restores the state from the matching paired save().

It is good practice to put all modifications to the graphics state between save()/restore() function calls. For example, if you have a function that takes a Cairo::Context reference as an argument, you might implement it as follows:

void doSomething(const Cairo::RefPtr<Cairo::Context>& context, int x)
{
    context->save();
    // change graphics state
    // perform drawing operations
    context->restore();
}

The draw function that you set with a call to set_draw_func() is called with a Cairo context that you shall use for drawing in the Gtk::DrawingArea widget. It is not necessary to save and restore this Cairo context in the draw function.