GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
gh-manager.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_gh_manager_h)
27 #define octave_gh_manager_h 1
28 
29 #include "octave-config.h"
30 
31 #include "graphics.h"
32 #include "gtk-manager.h"
33 
35 
36 class OCTINTERP_API gh_manager
37 {
38 public:
39 
40  typedef std::pair<uint8NDArray /*pixels*/, std::string /*svg*/> latex_data;
41 
42  OCTINTERP_API gh_manager (octave::interpreter& interp);
43 
44  // FIXME: eventually eliminate these static functions and access
45  // gh_manager object through the interpreter.
46 
47  OCTINTERP_API graphics_handle get_handle (bool integer_figure_handle);
48 
49  OCTINTERP_API void free (const graphics_handle& h, bool from_root = false);
50 
51  OCTINTERP_API void renumber_figure (const graphics_handle& old_gh,
52  const graphics_handle& new_gh);
53 
54  graphics_handle lookup (double val) const
55  {
56  const_iterator p = (octave::math::isnan (val)
57  ? m_handle_map.end () : m_handle_map.find (val));
58 
59  return (p != m_handle_map.end ()) ? p->first : graphics_handle ();
60  }
61 
62  graphics_handle lookup (const octave_value& val) const
63  {
64  return (val.is_real_scalar ()
65  ? lookup (val.double_value ()) : graphics_handle ());
66  }
67 
68  graphics_object get_object (double val) const
69  {
70  return get_object (lookup (val));
71  }
72 
74  {
75  const_iterator p = (h.ok () ? m_handle_map.find (h) : m_handle_map.end ());
76 
77  return (p != m_handle_map.end ()) ? p->second : graphics_object ();
78  }
79 
80  OCTINTERP_API graphics_handle
81  make_graphics_handle (const std::string& go_name,
82  const graphics_handle& p,
83  bool integer_figure_handle = false,
84  bool call_createfcn = true,
85  bool notify_toolkit = true);
86 
87  OCTINTERP_API graphics_handle
88  make_figure_handle (double val, bool notify_toolkit = true);
89 
90  OCTINTERP_API void push_figure (const graphics_handle& h);
91 
92  OCTINTERP_API void pop_figure (const graphics_handle& h);
93 
95  {
96  graphics_handle retval;
97 
98  for (const auto& hfig : m_figure_list)
99  {
100  if (is_handle_visible (hfig))
101  retval = hfig;
102  }
103 
104  return retval;
105  }
106 
107  Matrix handle_list (bool show_hidden = false)
108  {
109  Matrix retval (1, m_handle_map.size ());
110 
111  octave_idx_type i = 0;
112  for (const auto& h_iter : m_handle_map)
113  {
114  graphics_handle h = h_iter.first;
115 
116  if (show_hidden || is_handle_visible (h))
117  retval(i++) = h.value ();
118  }
119 
120  retval.resize (1, i);
121 
122  return retval;
123  }
124 
125  void lock () { m_graphics_lock.lock (); }
126 
127  bool try_lock () { return m_graphics_lock.try_lock (); }
128 
129  void unlock () { m_graphics_lock.unlock (); }
130 
131  Matrix figure_handle_list (bool show_hidden = false)
132  {
133  Matrix retval (1, m_figure_list.size ());
134 
135  octave_idx_type i = 0;
136  for (const auto& hfig : m_figure_list)
137  {
138  if (show_hidden || is_handle_visible (hfig))
139  retval(i++) = hfig.value ();
140  }
141 
142  retval.resize (1, i);
143 
144  return retval;
145  }
146 
147  OCTINTERP_API void
148  execute_listener (const graphics_handle& h, const octave_value& l);
149 
151  const std::string& name,
152  const octave_value& data = Matrix ())
153  {
154  octave_value cb;
155 
156  if (true)
157  {
158  octave::autolock guard (graphics_lock ());
159 
160  graphics_object go = get_object (h);
161 
162  if (go.valid_object ())
163  cb = go.get (name);
164  }
165 
166  execute_callback (h, cb, data);
167  }
168 
169  OCTINTERP_API void
170  execute_callback (const graphics_handle& h, const octave_value& cb,
171  const octave_value& data = Matrix ());
172 
173  OCTINTERP_API void
174  post_callback (const graphics_handle& h, const std::string& name,
175  const octave_value& data = Matrix ());
176 
177  OCTINTERP_API void
178  post_function (graphics_event::event_fcn fcn, void *fcn_data = nullptr);
179 
180  OCTINTERP_API void
181  post_set (const graphics_handle& h, const std::string& name,
182  const octave_value& value, bool notify_toolkit = true,
183  bool redraw_figure = false);
184 
185  OCTINTERP_API int process_events (bool force = false);
186 
187  OCTINTERP_API void enable_event_processing (bool enable = true);
188 
189  bool is_handle_visible (const graphics_handle& h) const
190  {
191  bool retval = false;
192 
193  graphics_object go = get_object (h);
194 
195  if (go.valid_object ())
196  retval = go.is_handle_visible ();
197 
198  return retval;
199  }
200 
201  OCTINTERP_API void close_all_figures ();
202 
203  OCTINTERP_API void restore_gcbo ();
204 
205  OCTINTERP_API void post_event (const graphics_event& e);
206 
207  octave::mutex graphics_lock ()
208  {
209  return m_graphics_lock;
210  }
211 
212  latex_data get_latex_data (const std::string& key) const
213  {
214  latex_data retval;
215 
216  const auto it = m_latex_cache.find (key);
217 
218  if (it != m_latex_cache.end ())
219  retval = it->second;
220 
221  return retval;
222  }
223 
224  void set_latex_data (const std::string& key, latex_data val)
225  {
226  // Limit the number of cache entries to 500
227  if (m_latex_keys.size () >= 500)
228  {
229  auto it = m_latex_cache.find (m_latex_keys.front ());
230 
231  if (it != m_latex_cache.end ())
232  m_latex_cache.erase (it);
233 
234  m_latex_keys.pop_front ();
235  }
236 
237  m_latex_cache[key] = val;
238  m_latex_keys.push_back (key);
239  }
240 
241 private:
242 
243  typedef std::map<graphics_handle, graphics_object>::iterator iterator;
244  typedef std::map<graphics_handle, graphics_object>::const_iterator
245  const_iterator;
246 
247  typedef std::set<graphics_handle>::iterator free_list_iterator;
248  typedef std::set<graphics_handle>::const_iterator const_free_list_iterator;
249 
250  typedef std::list<graphics_handle>::iterator figure_list_iterator;
251  typedef std::list<graphics_handle>::const_iterator const_figure_list_iterator;
252 
253  octave::interpreter& m_interpreter;
254 
255  // A map of handles to graphics objects.
256  std::map<graphics_handle, graphics_object> m_handle_map;
257 
258  // The available graphics handles.
259  std::set<graphics_handle> m_handle_free_list;
260 
261  // The next handle available if m_handle_free_list is empty.
262  double m_next_handle;
263 
264  // The allocated figure handles. Top of the stack is most recently
265  // created.
266  std::list<graphics_handle> m_figure_list;
267 
268  // The lock for accessing the graphics sytsem.
269  octave::mutex m_graphics_lock;
270 
271  // The list of events queued by graphics toolkits.
272  std::list<graphics_event> m_event_queue;
273 
274  // The stack of callback objects.
275  std::list<graphics_object> m_callback_objects;
276 
277  // A flag telling whether event processing must be constantly on.
278  int m_event_processing;
279 
280  // Cache of already parsed latex strings. Store a separate list of keys
281  // to allow for erasing oldest entries if cache size becomes too large.
282  std::unordered_map<std::string, latex_data> m_latex_cache;
283  std::list<std::string> m_latex_keys;
284 };
285 
286 OCTAVE_END_NAMESPACE(octave)
287 
288 #endif
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
Definition: dMatrix.h:42
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:158
graphics_handle current_figure() const
Definition: gh-manager.h:94
bool try_lock()
Definition: gh-manager.h:127
Matrix handle_list(bool show_hidden=false)
Definition: gh-manager.h:107
octave::mutex graphics_lock()
Definition: gh-manager.h:207
graphics_object get_object(double val) const
Definition: gh-manager.h:68
latex_data get_latex_data(const std::string &key) const
Definition: gh-manager.h:212
void unlock()
Definition: gh-manager.h:129
bool is_handle_visible(const graphics_handle &h) const
Definition: gh-manager.h:189
Matrix figure_handle_list(bool show_hidden=false)
Definition: gh-manager.h:131
graphics_handle lookup(double val) const
Definition: gh-manager.h:54
std::pair< uint8NDArray, std::string > latex_data
Definition: gh-manager.h:40
graphics_handle lookup(const octave_value &val) const
Definition: gh-manager.h:62
void lock()
Definition: gh-manager.h:125
graphics_object get_object(const graphics_handle &h) const
Definition: gh-manager.h:73
void set_latex_data(const std::string &key, latex_data val)
Definition: gh-manager.h:224
void execute_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: gh-manager.h:150
void(* event_fcn)(void *)
Definition: graphics.h:14452
octave_value get(bool all=false) const
Definition: graphics.h:3037
bool is_handle_visible() const
Definition: graphics.h:3179
bool valid_object() const
Definition: graphics.h:3138
double value() const
Definition: oct-handle.h:78
bool ok() const
Definition: oct-handle.h:113
bool is_real_scalar() const
Definition: ov.h:610
double double_value(bool frc_str_conv=false) const
Definition: ov.h:841
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void close_all_figures()
bool isnan(bool)
Definition: lo-mappers.h:178
void free(void *)