GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
graphics-toolkit.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2007-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_graphics_toolkit_h)
27 #define octave_graphics_toolkit_h 1
28 
29 #include "octave-config.h"
30 
31 #include <map>
32 #include <memory>
33 #include <string>
34 
35 #include "dMatrix.h"
36 
37 #include "Cell.h"
38 #include "error.h"
39 #include "graphics-handle.h"
40 
42 
43 class graphics_object;
44 class graphics_toolkit;
45 
47 {
48 public:
49 
50  friend class graphics_toolkit;
51 
52 public:
53 
54  base_graphics_toolkit (const std::string& nm)
55  : m_name (nm)
56  { }
57 
58  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (base_graphics_toolkit)
59 
60  virtual ~base_graphics_toolkit () = default;
61 
62  std::string get_name () const
63  {
64  return m_name;
65  }
66 
67  virtual bool is_valid () const
68  {
69  return false;
70  }
71 
72  virtual void redraw_figure (const graphics_object&) const
73  {
74  gripe_if_tkit_invalid ("redraw_figure");
75  }
76 
77  virtual void show_figure (const graphics_object&) const
78  {
79  gripe_if_tkit_invalid ("show_figure");
80  }
81 
82  virtual void print_figure (const graphics_object&, const std::string&,
83  const std::string&,
84  const std::string& = "") const
85  {
86  gripe_if_tkit_invalid ("print_figure");
87  }
88 
89  virtual uint8NDArray get_pixels (const graphics_object&) const
90  {
91  gripe_if_tkit_invalid ("get_pixels");
92  return uint8NDArray ();
93  }
94 
95  virtual Matrix get_canvas_size (const graphics_handle&) const
96  {
97  gripe_if_tkit_invalid ("get_canvas_size");
98  return Matrix (1, 2, 0.0);
99  }
100 
101  virtual double get_screen_resolution () const
102  {
103  gripe_if_tkit_invalid ("get_screen_resolution");
104  return 72.0;
105  }
106 
107  virtual Matrix get_screen_size () const
108  {
109  gripe_if_tkit_invalid ("get_screen_size");
110  return Matrix (1, 2, 0.0);
111  }
112 
113  virtual Matrix get_text_extent (const graphics_object&) const
114  {
115  gripe_if_tkit_invalid ("get_text_extent");
116  return Matrix ();
117  }
118 
119  // Callback function executed when the given graphics object
120  // changes. This allows the graphics toolkit to act on property
121  // changes if needed.
122  virtual void update (const graphics_object&, int)
123  {
124  gripe_if_tkit_invalid ("base_graphics_toolkit::update");
125  }
126 
127  void update (const graphics_handle&, int);
128 
129  // Callback function executed when the given graphics object is
130  // created. This allows the graphics toolkit to do toolkit-specific
131  // initializations for a newly created object.
132  virtual bool initialize (const graphics_object&)
133  {
134  gripe_if_tkit_invalid ("base_graphics_toolkit::initialize");
135  return false;
136  }
137 
138  bool initialize (const graphics_handle&);
139 
140  // Callback function executed just prior to deleting the given
141  // graphics object. This allows the graphics toolkit to perform
142  // toolkit-specific cleanup operations before an object is deleted.
143  virtual void finalize (const graphics_object&)
144  {
145  gripe_if_tkit_invalid ("base_graphics_toolkit::finalize");
146  }
147 
148  void finalize (const graphics_handle&);
149 
150  // Close the graphics toolkit.
151  virtual void close ()
152  {
153  gripe_if_tkit_invalid ("base_graphics_toolkit::close");
154  }
155 
156 private:
157 
158  std::string m_name;
159 
160 private:
161 
162  void gripe_if_tkit_invalid (const std::string& fname) const
163  {
164  if (! is_valid ())
165  error ("%s: invalid graphics toolkit", fname.c_str ());
166  }
167 };
168 
170 {
171 public:
172  graphics_toolkit (const std::string& name = "unknown")
173  : m_rep (new base_graphics_toolkit (name))
174  { }
175 
176  // NEW_REP must be dynamically allocated.
178  : m_rep (std::shared_ptr<base_graphics_toolkit> (new_rep))
179  {
180  if (! m_rep)
181  error ("invalid graphics_toolkit!");
182  }
183 
184  graphics_toolkit (const graphics_toolkit& b) = default;
185 
187 
188  ~graphics_toolkit () = default;
189 
190  operator bool () const
191  {
192  return m_rep->is_valid ();
193  }
194 
195  std::string get_name () const
196  {
197  return m_rep->get_name ();
198  }
199 
200  void redraw_figure (const graphics_object& go) const
201  {
202  m_rep->redraw_figure (go);
203  }
204 
205  void show_figure (const graphics_object& go) const
206  {
207  m_rep->show_figure (go);
208  }
209 
210  void print_figure (const graphics_object& go, const std::string& term,
211  const std::string& file,
212  const std::string& debug_file = "") const
213  {
214  m_rep->print_figure (go, term, file, debug_file);
215  }
216 
218  {
219  return m_rep->get_pixels (go);
220  }
221 
223  {
224  return m_rep->get_canvas_size (fh);
225  }
226 
227  double get_screen_resolution () const
228  {
229  return m_rep->get_screen_resolution ();
230  }
231 
233  {
234  return m_rep->get_screen_size ();
235  }
236 
238  {
239  return m_rep->get_text_extent (go);
240  }
241 
242  // Notifies graphics toolkit that object't property has changed.
243  void update (const graphics_object& go, int id)
244  {
245  m_rep->update (go, id);
246  }
247 
248  void update (const graphics_handle& h, int id)
249  {
250  m_rep->update (h, id);
251  }
252 
253  // Notifies graphics toolkit that new object was created.
254  bool initialize (const graphics_object& go)
255  {
256  return m_rep->initialize (go);
257  }
258 
259  bool initialize (const graphics_handle& h)
260  {
261  return m_rep->initialize (h);
262  }
263 
264  // Notifies graphics toolkit that object was destroyed.
265  // This is called only for explicitly deleted object.
266  // Children are deleted implicitly and graphics toolkit isn't notified.
267  void finalize (const graphics_object& go)
268  {
269  m_rep->finalize (go);
270  }
271 
272  void finalize (const graphics_handle& h)
273  {
274  m_rep->finalize (h);
275  }
276 
277  // Close the graphics toolkit.
278  void close ()
279  {
280  m_rep->close ();
281  }
282 
283 private:
284 
285  std::shared_ptr<base_graphics_toolkit> m_rep;
286 };
287 
288 OCTAVE_END_NAMESPACE(octave)
289 
290 #endif
Definition: dMatrix.h:42
virtual void update(const graphics_object &, int)
virtual void show_figure(const graphics_object &) const
virtual void print_figure(const graphics_object &, const std::string &, const std::string &, const std::string &="") const
virtual void finalize(const graphics_object &)
virtual bool initialize(const graphics_object &)
virtual double get_screen_resolution() const
virtual Matrix get_text_extent(const graphics_object &) const
virtual void redraw_figure(const graphics_object &) const
virtual Matrix get_canvas_size(const graphics_handle &) const
virtual Matrix get_screen_size() const
base_graphics_toolkit(const std::string &nm)
std::string get_name() const
virtual uint8NDArray get_pixels(const graphics_object &) const
virtual bool is_valid() const
Matrix get_canvas_size(const graphics_handle &fh) const
std::string get_name() const
graphics_toolkit(const graphics_toolkit &b)=default
void update(const graphics_object &go, int id)
void show_figure(const graphics_object &go) const
void finalize(const graphics_handle &h)
void redraw_figure(const graphics_object &go) const
bool initialize(const graphics_object &go)
void finalize(const graphics_object &go)
~graphics_toolkit()=default
void update(const graphics_handle &h, int id)
graphics_toolkit(const std::string &name="unknown")
double get_screen_resolution() const
void print_figure(const graphics_object &go, const std::string &term, const std::string &file, const std::string &debug_file="") const
graphics_toolkit & operator=(const graphics_toolkit &b)=default
uint8NDArray get_pixels(const graphics_object &go) const
Matrix get_screen_size() const
graphics_toolkit(base_graphics_toolkit *new_rep)
Matrix get_text_extent(const graphics_object &go) const
bool initialize(const graphics_handle &h)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988
intNDArray< octave_uint8 > uint8NDArray
Definition: uint8NDArray.h:36