Canvas

namespace ezgl

A library for creating a graphical user interface.

Typedefs

using draw_canvas_fn = void (*)(renderer*)

The signature of a function that draws to an ezgl::canvas.

class canvas
#include <canvas.hpp>

Responsible for creating, destroying, and maintaining the rendering context of a GtkWidget.

Underneath, the class relies on a GtkDrawingArea as its GUI widget along with cairo to provide the rendering context. The class connects to the relevant GTK signals, namely configure and draw events, to remain responsive.

Each canvas is double-buffered. A draw callback (see: ezgl::draw_canvas_fn) is invoked each time the canvas needs to be redrawn. This may be caused by the user (e.g., resizing the screen), but can also be forced by the programmer.

Public Functions

~canvas()

Destructor.

inline char const *id() const

Get the name (identifier) of the canvas.

int width() const

Get the width of the canvas in pixels.

int height() const

Get the height of the canvas in pixels.

void redraw()

Force the canvas to redraw itself.

This will invoke the ezgl::draw_canvas_fn callback and queue a redraw of the GtkWidget.

inline camera const &get_camera() const

Get an immutable reference to this canvas’ camera.

inline camera &get_camera()

Get a mutable reference to this canvas’ camera.

renderer *create_animation_renderer()

Create an animation renderer that can be used to draw on top of the current canvas

bool print_pdf(const char *file_name, int width = 0, int height = 0)

print_pdf, print_svg, and print_png generate a PDF, SVG, or PNG output file showing all the graphical content of the current canvas.

Parameters:

file_name – name of the output file

Returns:

returns true if the function has successfully generated the output file, otherwise failed due to errors such as out of memory occurs.

bool print_svg(const char *file_name, int width = 0, int height = 0)
bool print_png(const char *file_name, int width = 0, int height = 0)

Protected Functions

canvas(std::string canvas_id, draw_canvas_fn draw_callback, rectangle coordinate_system, color background_color)

Create a canvas that can be drawn to.

void initialize(GtkWidget *drawing_area)

Lazy initialization of the canvas class.

This function is required because GTK will not send activate/startup signals to an ezgl::application until control of the program has been reliquished. The GUI is not built until ezgl::application receives an activate signal.

Private Members

std::string m_canvas_id
draw_canvas_fn m_draw_callback
camera m_camera
color m_background_color
GtkWidget *m_drawing_area = nullptr
cairo_surface_t *m_surface = nullptr
cairo_t *m_context = nullptr
renderer *m_animation_renderer = nullptr

Private Static Functions

static gboolean configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
static gboolean draw_surface(GtkWidget *widget, cairo_t *context, gpointer data)

Friends

friend class application