ezgl  1.0.1
An Easy Graphics & GUI Library
canvas.hpp
1 /*
2  * Copyright 2019-2022 University of Toronto
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Authors: Mario Badr, Sameh Attia, Tanner Young-Schultz and Vaughn Betz
17  */
18 
19 #ifndef EZGL_CANVAS_HPP
20 #define EZGL_CANVAS_HPP
21 
22 #include "ezgl/camera.hpp"
23 #include "ezgl/rectangle.hpp"
24 #include "ezgl/graphics.hpp"
25 #include "ezgl/color.hpp"
26 
27 #include <cairo.h>
28 #include <cairo-pdf.h>
29 #include <cairo-svg.h>
30 #include <gtk/gtk.h>
31 
32 #include <string>
33 
34 namespace ezgl {
35 
36 /**** Functions in this class are for ezgl internal use; application code doesn't need to call them ****/
37 
38 class renderer;
39 
43 using draw_canvas_fn = void (*)(renderer*);
44 
54 class canvas {
55 public:
60 
64  char const *id() const
65  {
66  return m_canvas_id.c_str();
67  }
68 
72  int width() const;
73 
77  int height() const;
78 
84  void redraw();
85 
89  camera const &get_camera() const
90  {
91  return m_camera;
92  }
93 
98  {
99  return m_camera;
100  }
101 
106 
115  bool print_pdf(const char *file_name, int width = 0, int height = 0);
116  bool print_svg(const char *file_name, int width = 0, int height = 0);
117  bool print_png(const char *file_name, int width = 0, int height = 0);
118 
119 
120 protected:
121  // Only the ezgl::application can create and initialize a canvas object.
122  friend class application;
123 
127  canvas(std::string canvas_id, draw_canvas_fn draw_callback, rectangle coordinate_system, color background_color);
128 
135  void initialize(GtkWidget *drawing_area);
136 
137 private:
138  // Name of the canvas in XML.
139  std::string m_canvas_id;
140 
141  // The function to call when the widget needs to be redrawn.
142  draw_canvas_fn m_draw_callback;
143 
144  // The transformations between the GUI and the world.
145  camera m_camera;
146 
147  // The background color of the drawing area
148  color m_background_color;
149 
150  // A non-owning pointer to the drawing area inside a GTK window.
151  GtkWidget *m_drawing_area = nullptr;
152 
153  // The off-screen surface that can be drawn to.
154  cairo_surface_t *m_surface = nullptr;
155 
156  // The off-screen cairo context that can be drawn to
157  cairo_t *m_context = nullptr;
158 
159  // The animation renderer
160  renderer *m_animation_renderer = nullptr;
161 
162 private:
163  // Called each time our drawing area widget has changed (e.g., in size).
164  static gboolean configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data);
165 
166  // Called each time we need to draw to our drawing area widget.
167  static gboolean draw_surface(GtkWidget *widget, cairo_t *context, gpointer data);
168 };
169 }
170 
171 #endif //EZGL_CANVAS_HPP
The core application.
Definition: application.hpp:95
Manages the transformations between coordinate systems.
Definition: camera.hpp:40
Responsible for creating, destroying, and maintaining the rendering context of a GtkWidget.
Definition: canvas.hpp:54
camera const & get_camera() const
Get an immutable reference to this canvas' camera.
Definition: canvas.hpp:89
void initialize(GtkWidget *drawing_area)
Lazy initialization of the canvas class.
int width() const
Get the width of the canvas in pixels.
int height() const
Get the height of the canvas in pixels.
canvas(std::string canvas_id, draw_canvas_fn draw_callback, rectangle coordinate_system, color background_color)
Create a canvas that can be drawn to.
char const * id() const
Get the name (identifier) of the canvas.
Definition: canvas.hpp:64
~canvas()
Destructor.
camera & get_camera()
Get a mutable reference to this canvas' camera.
Definition: canvas.hpp:97
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...
void redraw()
Force the canvas to redraw itself.
Represents a rectangle as two diagonally opposite points.
Definition: rectangle.hpp:31
Provides functions to draw primitives (e.g., lines, shapes) to a rendering context (the MainCanvas).
Definition: graphics.hpp:173
A library for creating a graphical user interface.
Definition: application.hpp:40
void(*)(renderer *) draw_canvas_fn
The signature of a function that draws to an ezgl::canvas.
Definition: canvas.hpp:43
Represents a color as a mixture or red, green, and blue as well as the transparency level.
Definition: color.hpp:31