GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
__init_fltk__.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2007-2023 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 /*
27 
28 To initialize:
29 
30  graphics_toolkit ("fltk");
31  plot (randn (1e3, 1));
32 
33 */
34 
35 // PKG_ADD: if (__have_feature__ ("FLTK") && __have_feature__ ("OPENGL") && have_window_system () && ! (ismac () && __event_manager_enabled__ ())) register_graphics_toolkit ("fltk"); endif
36 
37 #if defined (HAVE_CONFIG_H)
38 # include "config.h"
39 #endif
40 
41 #include "defun-dld.h"
42 #include "error.h"
43 #include "errwarn.h"
44 
45 #if defined (HAVE_X_WINDOWS)
46 # include <X11/Xlib.h>
47 #endif
48 
49 #include <cmath>
50 
51 #include <locale>
52 #include <map>
53 #include <sstream>
54 #include <string>
55 #include <vector>
56 
57 #if defined (WIN32)
58 # define WIN32_LEAN_AND_MEAN
59 #endif
60 
61 #if defined (HAVE_FLTK)
62 # include <FL/Fl.H>
63 # include <FL/Fl_Box.H>
64 # include <FL/Fl_Button.H>
65 # include <FL/Fl_Choice.H>
66 # include <FL/Fl_File_Chooser.H>
67 # include <FL/Fl_Gl_Window.H>
68 # include <FL/names.h>
69 # include <FL/Fl_Menu_Bar.H>
70 # include <FL/Fl_Menu_Button.H>
71 # include <FL/Fl_Output.H>
72 # include <FL/Fl_Window.H>
73 # include <FL/fl_ask.H>
74 # include <FL/fl_draw.H>
75 # include <FL/gl.h>
76 #endif
77 
78 // FLTK headers may include X11/X.h which defines Complex, and that
79 // conflicts with Octave's Complex typedef. We don't need the X11
80 // Complex definition in this file, so remove it before including Octave
81 // headers which may require Octave's Complex typedef.
82 #undef Complex
83 
84 #include "Array.h"
85 #include "cmd-edit.h"
86 #include "dColVector.h"
87 #include "dMatrix.h"
88 #include "lo-ieee.h"
89 #include "oct-env.h"
90 
91 #include "Cell.h"
92 #include "builtin-defun-decls.h"
93 #include "display.h"
94 #include "gl-render.h"
95 #include "gl2ps-print.h"
96 #include "graphics.h"
97 #include "gtk-manager.h"
98 #include "interpreter-private.h"
99 #include "interpreter.h"
100 #include "oct-map.h"
101 #include "oct-opengl.h"
102 #include "ov-fcn-handle.h"
103 #include "ov.h"
104 #include "ovl.h"
105 #include "parse.h"
106 #include "variables.h"
107 
109 
110 #if defined (HAVE_FLTK)
111 
112 #define FLTK_GRAPHICS_TOOLKIT_NAME "fltk"
113 
114 const char *help_text = "\
115 Keyboard Shortcuts\n\
116 a - autoscale\n\
117 p - pan/zoom\n\
118 r - rotate\n\
119 g - toggle grid\n\
120 \n\
121 Mouse\n\
122 left drag - pan\n\
123 mouse wheel - zoom\n\
124 right drag - rectangle zoom\n\
125 left double click - autoscale\n\
126 ";
127 
128 class OpenGL_fltk : public Fl_Gl_Window
129 {
130 public:
131 
132  OpenGL_fltk (int xx, int yy, int ww, int hh, double num)
133  : Fl_Gl_Window (xx, yy, ww, hh, nullptr), m_number (num),
134  m_glfcns (), m_renderer (m_glfcns), m_in_zoom (false), m_zoom_box ()
135  {
136 #if defined (HAVE_OPENGL)
137 
138  // Ask for double buffering and a depth buffer.
139  mode (FL_DEPTH | FL_DOUBLE | FL_MULTISAMPLE);
140 
141 #else
142 
143  err_disabled_feature ("OpenGL_fltk", "OpenGL");
144 
145 #endif
146  }
147 
148  ~OpenGL_fltk (void) = default;
149 
150  void zoom (bool z)
151  {
152  m_in_zoom = z;
153  if (! m_in_zoom)
154  hide_overlay ();
155  }
156 
157  bool zoom (void) { return m_in_zoom; }
158  void set_zoom_box (const Matrix& zb) { m_zoom_box = zb; }
159 
160  void print (const std::string& cmd, const std::string& term)
161  {
162  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
163 
164  octave::gl2ps_print (m_glfcns, gh_mgr.get_object (m_number), cmd, term);
165  }
166 
167  uint8NDArray get_pixels (void)
168  {
169  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
170 
171  m_renderer.draw (gh_mgr.get_object (m_number));
172 
173  return m_renderer.get_pixels (w (), h ());
174  }
175 
176  void resize (int xx, int yy, int ww, int hh)
177  {
178 #if defined (HAVE_OPENGL)
179 
180  Fl_Gl_Window::resize (xx, yy, ww, hh);
181 
182 #else
183 
184  octave_unused_parameter (xx);
185  octave_unused_parameter (yy);
186  octave_unused_parameter (ww);
187  octave_unused_parameter (hh);
188 
189  // This shouldn't happen because construction of Opengl_fltk
190  // objects is supposed to be impossible if OpenGL is not available.
191 
192  panic_impossible ();
193 
194 #endif
195  }
196 
197  bool renumber (double new_number)
198  {
199  bool retval = false;
200 
201  if (m_number != new_number)
202  {
203  m_number = new_number;
204  retval = true;
205  }
206 
207  return retval;
208  }
209 
210 private:
211 
212  double m_number;
213 
214  octave::opengl_functions m_glfcns;
215  octave::opengl_renderer m_renderer;
216 
217  bool m_in_zoom;
218 
219  // (x1,y1,x2,y2)
220  Matrix m_zoom_box;
221 
222  void draw (void)
223  {
224 #if defined (HAVE_OPENGL)
225 
226  if (! valid ())
227  {
228  m_glfcns.glMatrixMode (GL_PROJECTION);
229  m_glfcns.glLoadIdentity ();
230  m_glfcns.glViewport (0, 0, w (), h ());
231  }
232 
233  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
234 
235  m_renderer.draw (gh_mgr.get_object (m_number));
236 
237  if (zoom ())
238  overlay ();
239 
240 #else
241 
242  // This shouldn't happen because construction of Opengl_fltk
243  // objects is supposed to be impossible if OpenGL is not available.
244 
245  panic_impossible ();
246 
247 #endif
248  }
249 
250  void overlay (void)
251  {
252  Matrix overlaycolor (3, 1);
253  overlaycolor(0) = 0.45;
254  overlaycolor(1) = 0.62;
255  overlaycolor(2) = 0.81;
256  double overlayalpha = 0.1;
257  Matrix bordercolor = overlaycolor;
258  double borderalpha = 0.9;
259  double borderwidth = 1.5;
260 
261  m_renderer.draw_zoom_box (w (), h (),
262  m_zoom_box(0), m_zoom_box(1),
263  m_zoom_box(2), m_zoom_box(3),
264  overlaycolor, overlayalpha,
265  bordercolor, borderalpha, borderwidth);
266  }
267 
268  int handle (int event)
269  {
270 #if defined (HAVE_OPENGL)
271 
272  switch (event)
273  {
274  case FL_ENTER:
275  cursor (FL_CURSOR_CROSS);
276  return 1;
277 
278  case FL_LEAVE:
279  cursor (FL_CURSOR_DEFAULT);
280  return 1;
281  }
282 
283  return Fl_Gl_Window::handle (event);
284 
285 #else
286 
287  octave_unused_parameter (event);
288 
289  // This shouldn't happen because construction of Opengl_fltk
290  // objects is supposed to be impossible if OpenGL is not available.
291 
292  panic_impossible ();
293 
294 #endif
295  }
296 };
297 
298 static void script_cb (Fl_Widget *, void *data)
299 {
300  static_cast<uimenu::properties *> (data)->execute_menuselectedfcn ();
301 }
302 
303 class fltk_uimenu
304 {
305 public:
306 
307  fltk_uimenu (int xx, int yy, int ww, int hh)
308  : m_menubar (new Fl_Menu_Bar (xx, yy, ww, hh))
309  { }
310 
311  int items_to_show (void)
312  {
313  //returns the number of visible menu items
314  int len = m_menubar->size ();
315  int n = 0;
316  for (int t = 0; t < len; t++)
317  {
318  const Fl_Menu_Item *m
319  = static_cast<const Fl_Menu_Item *> (&(m_menubar->menu ()[t]));
320 
321  if (m->label () && m->visible ())
322  n++;
323  }
324 
325  return n;
326  }
327 
328  void show (void)
329  {
330  m_menubar->show ();
331  m_menubar->redraw ();
332  }
333 
334  void hide (void)
335  {
336  m_menubar->hide ();
337  m_menubar->redraw ();
338  }
339 
340  bool is_visible (void)
341  {
342  return m_menubar->visible ();
343  }
344 
345  int find_index_by_name (const std::string& findname)
346  {
347  // This function is derived from Greg Ercolano's function
348  // int GetIndexByName(...), see:
349  // http://seriss.com/people/erco/fltk/#Menu_ChangeLabel
350  // He agreed via PM that it can be included in octave using GPLv3
351  // Kai Habel (14.10.2010)
352 
353  std::string menupath;
354  for (int t = 0; t < m_menubar->size (); t++)
355  {
356  Fl_Menu_Item *m = const_cast<Fl_Menu_Item *> (&(m_menubar->menu ()[t]));
357  if (m->submenu ())
358  {
359  // item has submenu
360  if (! menupath.empty ())
361  menupath += '/';
362  menupath += m->label ();
363 
364  if (menupath == findname)
365  return (t);
366  }
367  else
368  {
369  // End of submenu? Pop back one level.
370  if (! m->label ())
371  {
372  std::size_t idx = menupath.find_last_of ('/');
373  if (idx != std::string::npos)
374  menupath.erase (idx);
375  else
376  menupath.clear ();
377  continue;
378  }
379  // Menu item?
380  std::string itempath = menupath;
381  if (! itempath.empty ())
382  itempath += '/';
383  itempath += m->label ();
384 
385  if (itempath == findname)
386  return (t);
387  }
388  }
389  return (-1);
390  }
391 
392  Matrix find_uimenu_children (uimenu::properties& uimenup) const
393  {
394  Matrix uimenu_childs = uimenup.get_all_children ();
395  Matrix retval = do_find_uimenu_children (uimenu_childs);
396  return retval;
397  }
398 
399  Matrix find_uimenu_children (figure::properties& figp) const
400  {
401  Matrix uimenu_childs = figp.get_all_children ();
402  Matrix retval = do_find_uimenu_children (uimenu_childs);
403  return retval;
404  }
405 
406  Matrix do_find_uimenu_children (Matrix uimenu_childs) const
407  {
408  octave_idx_type k = 0;
409 
410  Matrix pos = Matrix (uimenu_childs.numel (), 1);
411 
412  for (octave_idx_type ii = 0; ii < uimenu_childs.numel (); ii++)
413  {
414  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
415 
416  graphics_object kidgo = gh_mgr.get_object (uimenu_childs (ii));
417 
418  if (kidgo.valid_object () && kidgo.isa ("uimenu"))
419  {
420  uimenu_childs(k) = uimenu_childs(ii);
421  pos(k++) = dynamic_cast<uimenu::properties&> (kidgo.get_properties ()).get_position ();
422  }
423  }
424 
425  uimenu_childs.resize (k, 1);
426  pos.resize (k, 1);
427  Matrix retval = Matrix (k, 1);
428  // Don't know if this is the best method to sort.
429  // Can we avoid the for loop?
431  for (octave_idx_type ii = 0; ii < k; ii++)
432  retval(ii) = uimenu_childs (sidx(ii));
433 
434  return retval;
435  }
436 
437  void delete_entry (uimenu::properties& uimenup)
438  {
439  std::string fltk_label = uimenup.get___fltk_label__ ();
440  int idx = find_index_by_name (fltk_label.c_str ());
441 
442  if (idx >= 0)
443  m_menubar->remove (idx);
444  }
445 
446  void update_accelerator (uimenu::properties& uimenup)
447  {
448  std::string fltk_label = uimenup.get___fltk_label__ ();
449  if (! fltk_label.empty ())
450  {
451  Fl_Menu_Item *item = const_cast<Fl_Menu_Item *> (m_menubar->find_item (fltk_label.c_str ()));
452 
453  if (item)
454  {
455  std::string acc = uimenup.get_accelerator ();
456  if (acc.length () > 0)
457  {
458  int key = FL_CTRL + acc[0];
459  item->shortcut (key);
460  }
461  }
462  }
463  }
464 
465  void update_menuselectedfcn (uimenu::properties& uimenup)
466  {
467  std::string fltk_label = uimenup.get___fltk_label__ ();
468  if (! fltk_label.empty ())
469  {
470  Fl_Menu_Item *item
471  = const_cast<Fl_Menu_Item *> (m_menubar->find_item (fltk_label.c_str ()));
472  if (item)
473  {
474  if (! uimenup.get_menuselectedfcn ().isempty ())
475  item->callback (static_cast<Fl_Callback *> (script_cb),
476  static_cast<void *> (&uimenup));
477  else
478  item->callback (nullptr, static_cast<void *> (nullptr));
479  }
480  }
481  }
482 
483  void update_enable (uimenu::properties& uimenup)
484  {
485  std::string fltk_label = uimenup.get___fltk_label__ ();
486  if (! fltk_label.empty ())
487  {
488  Fl_Menu_Item *item
489  = const_cast<Fl_Menu_Item *> (m_menubar->find_item (fltk_label.c_str ()));
490  if (item)
491  {
492  if (uimenup.is_enable ())
493  item->activate ();
494  else
495  item->deactivate ();
496  }
497  }
498  }
499 
500  void update_foregroundcolor (uimenu::properties& uimenup)
501  {
502  std::string fltk_label = uimenup.get___fltk_label__ ();
503  if (! fltk_label.empty ())
504  {
505  Fl_Menu_Item *item
506  = const_cast<Fl_Menu_Item *> (m_menubar->find_item (fltk_label.c_str ()));
507  if (item)
508  {
509  Matrix rgb = uimenup.get_foregroundcolor_rgb ();
510 
511  uchar r = static_cast<uchar> (std::floor (rgb (0) * 255));
512  uchar g = static_cast<uchar> (std::floor (rgb (1) * 255));
513  uchar b = static_cast<uchar> (std::floor (rgb (2) * 255));
514 
515  item->labelcolor (fl_rgb_color (r, g, b));
516  }
517  }
518  }
519 
520  void update_seperator (const uimenu::properties& uimenup)
521  {
522  // Matlab places the separator before the current
523  // menu entry, while fltk places it after. So we need to find
524  // the previous item in this menu/submenu. (Kai)
525  std::string fltk_label = uimenup.get___fltk_label__ ();
526  if (! fltk_label.empty ())
527  {
528  int itemflags = 0, idx;
529  int curr_idx = find_index_by_name (fltk_label.c_str ());
530 
531  for (idx = curr_idx - 1; idx >= 0; idx--)
532  {
533  Fl_Menu_Item *item
534  = const_cast<Fl_Menu_Item *> (&m_menubar->menu () [idx]);
535  itemflags = item->flags;
536  if (item->label ())
537  break;
538  }
539 
540  if (idx >= 0 && idx < m_menubar->size ())
541  {
542  if (uimenup.is_separator ())
543  {
544  if (! (itemflags & FL_SUBMENU))
545  m_menubar->mode (idx, itemflags | FL_MENU_DIVIDER);
546  }
547  else
548  m_menubar->mode (idx, itemflags & (~FL_MENU_DIVIDER));
549  }
550  }
551  }
552 
553  void update_visible (uimenu::properties& uimenup)
554  {
555  std::string fltk_label = uimenup.get___fltk_label__ ();
556  if (! fltk_label.empty ())
557  {
558  Fl_Menu_Item *item
559  = const_cast<Fl_Menu_Item *> (m_menubar->find_item (fltk_label.c_str ()));
560  if (item)
561  {
562  if (uimenup.is_visible ())
563  item->show ();
564  else
565  item->hide ();
566  }
567  }
568  }
569 
570  void update_position (uimenu::properties& uimenup, int pos)
571  {
572  uimenup.get_property ("position").set (octave_value (static_cast<double> (pos)),
573  true, false);
574  }
575 
576  void add_entry (uimenu::properties& uimenup)
577  {
578 
579  std::string fltk_label = uimenup.get___fltk_label__ ();
580 
581  if (! fltk_label.empty ())
582  {
583  bool item_added = false;
584  do
585  {
586  const Fl_Menu_Item *item
587  = m_menubar->find_item (fltk_label.c_str ());
588 
589  if (item)
590  {
591  //avoid duplicate menulabels
592  std::size_t idx1 = fltk_label.find_last_of ('(');
593  std::size_t idx2 = fltk_label.find_last_of (')');
594  int len = idx2 - idx1;
595  int val = 1;
596  if (len > 0)
597  {
598  std::string valstr = fltk_label.substr (idx1 + 1, len - 1);
599  fltk_label.erase (idx1, len + 1);
600  val = atoi (valstr.c_str ());
601  if (val > 0 && val < 99)
602  val++;
603  }
604  std::ostringstream valstream;
605  valstream << val;
606  fltk_label += '(' + valstream.str () + ')';
607  }
608  else
609  {
610  Matrix uimenu_ch = find_uimenu_children (uimenup);
611  int len = uimenu_ch.numel ();
612  int flags = 0;
613  if (len > 0)
614  flags = FL_SUBMENU;
615  if (len == 0 && uimenup.is_checked ())
616  flags += FL_MENU_TOGGLE + FL_MENU_VALUE;
617  m_menubar->add (fltk_label.c_str (),
618  0, nullptr, nullptr, flags);
619  item_added = true;
620  }
621  }
622  while (! item_added);
623  uimenup.set___fltk_label__ (fltk_label);
624  }
625  }
626 
627  void add_to_menu (uimenu::properties& uimenup)
628  {
629  std::vector<int> delayed_menus;
630  Matrix kids = find_uimenu_children (uimenup);
631  int len = kids.numel ();
632  std::string fltk_label = uimenup.get___fltk_label__ ();
633  int count = 0;
634 
635  add_entry (uimenup);
636  update_foregroundcolor (uimenup);
637  update_menuselectedfcn (uimenup);
638  update_accelerator (uimenup);
639  update_enable (uimenup);
640  update_visible (uimenup);
641  update_seperator (uimenup);
642 
643  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
644 
645  for (octave_idx_type ii = 0; ii < len; ii++)
646  {
647  graphics_object kgo = gh_mgr.get_object (kids (len - (ii + 1)));
648 
649  if (kgo.valid_object ())
650  {
651  uimenu::properties& kprop = dynamic_cast<uimenu::properties&>
652  (kgo.get_properties ());
653 
654  // if no pos yet, delay adding menu until after other menus
655  int pos = kprop.get_position ();
656  if (pos <= 0)
657  delayed_menus.push_back ((len - (ii + 1)));
658  else
659  {
660  add_to_menu (kprop);
661  }
662  }
663  }
664 
665  // create any delayed menus
666  for (std::size_t ii = 0; ii < delayed_menus.size (); ii++)
667  {
668  graphics_object kgo = gh_mgr.get_object (kids (delayed_menus[ii]));
669 
670  if (kgo.valid_object ())
671  {
672  uimenu::properties& kprop = dynamic_cast<uimenu::properties&>
673  (kgo.get_properties ());
674  add_to_menu (kprop);
675  update_position (kprop, ++count);
676  }
677  }
678  }
679 
680  void add_to_menu (figure::properties& figp)
681  {
682  std::vector<int> delayed_menus;
683  Matrix kids = find_uimenu_children (figp);
684  int len = kids.numel ();
685  int count = 0;
686 
687  m_menubar->clear ();
688 
689  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
690 
691  for (octave_idx_type ii = 0; ii < len; ii++)
692  {
693  graphics_object kgo = gh_mgr.get_object (kids (len - (ii + 1)));
694 
695  if (kgo.valid_object ())
696  {
697  uimenu::properties& kprop = dynamic_cast<uimenu::properties&>
698  (kgo.get_properties ());
699 
700  // if no pos yet, delay adding menu until after other menus
701  int pos = kprop.get_position ();
702  if (pos <= 0)
703  delayed_menus.push_back ((len - (ii + 1)));
704  else
705  {
706  add_to_menu (kprop);
707  update_position (kprop, ++count);
708  }
709  }
710  }
711 
712  // create any delayed menus
713  for (std::size_t ii = 0; ii < delayed_menus.size (); ii++)
714  {
715  graphics_object kgo = gh_mgr.get_object (kids (delayed_menus[ii]));
716 
717  if (kgo.valid_object ())
718  {
719  uimenu::properties& kprop = dynamic_cast<uimenu::properties&>
720  (kgo.get_properties ());
721  add_to_menu (kprop);
722  update_position (kprop, ++count);
723  }
724  }
725  }
726 
727  template <typename T_prop>
728  void remove_from_menu (T_prop& prop)
729  {
730  Matrix kids;
731  std::string type = prop.get_type ();
732  kids = find_uimenu_children (prop);
733  int len = kids.numel ();
734 
735  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
736 
737  for (octave_idx_type ii = 0; ii < len; ii++)
738  {
739  graphics_object kgo = gh_mgr.get_object (kids (len - (ii + 1)));
740 
741  if (kgo.valid_object ())
742  {
743  uimenu::properties kprop = dynamic_cast<uimenu::properties&>
744  (kgo.get_properties ());
745  remove_from_menu (kprop);
746  }
747  }
748 
749  if (type == "uimenu")
750  delete_entry (dynamic_cast<uimenu::properties&> (prop));
751  else if (type == "figure")
752  m_menubar->clear ();
753  }
754 
755  // No copying!
756 
757  fltk_uimenu (const fltk_uimenu&) = delete;
758 
759  fltk_uimenu operator = (const fltk_uimenu&) = delete;
760 
761  ~fltk_uimenu (void)
762  {
763  // FLTK is supposed to manage memory for widgets.
764  }
765 
766 private:
767 
768  Fl_Menu_Bar *m_menubar;
769 };
770 
771 #if defined (HAVE_X_WINDOWS)
772 static int
773 xerror_handler (Display *, XErrorEvent *)
774 {
775  return 0;
776 }
777 #endif
778 
779 class plot_window : public Fl_Window
780 {
781  friend class fltk_uimenu;
782 
783 public:
784 
785  plot_window (int xx, int yy, int ww, int hh, figure::properties& xfp,
786  bool internal)
787  : Fl_Window (xx, yy, ww, hh + m_menu_h + m_status_h + 2, "octave"),
788  m_window_label (), m_fp (xfp), m_uimenu (nullptr), m_canvas (nullptr),
789  m_autoscale (nullptr), m_togglegrid (nullptr), m_panzoom (nullptr),
790  m_rotate (nullptr), m_help (nullptr), m_status (nullptr),
791  m_resize_dummy (nullptr), m_ax_obj (), m_pos_x (0), m_pos_y (0)
792  {
793  callback (window_close, static_cast<void *> (this));
794 
795  // The size of the resize_dummy box also determines the minimum
796  // window size.
797  m_resize_dummy = new Fl_Box (5 * m_status_h, m_menu_h,
798  ww - 5 * m_status_h, hh);
799 
800  // See http://fltk.org/articles.php?L415+I0+T+M1000+P1
801  // for how resizable works
802  resizable (m_resize_dummy);
803 
804  // FIXME: The function below is only available in FLTK >= 1.3
805  // At some point support for FLTK 1.1 will be dropped in Octave.
806  // At that point this function should be uncommented.
807  // The current solution is to call xclass() before show() for each window.
808  // Set WM_CLASS which allows window managers to properly group related
809  // windows. Otherwise, the class is just "FLTK"
810  //default_xclass ("Octave");
811 
812  m_uimenu = new fltk_uimenu (0, 0, ww, m_menu_h);
813  m_canvas = new OpenGL_fltk (0, m_menu_h, ww, hh, number ());
814 
815  // The bottom toolbar is a composite of "autoscale", "togglegrid",
816  // "panzoom", "rotate", "help", and "status".
817  // Only "status" should be resized.
818 
819  int toolbar_y = m_menu_h + hh + 1;
820  m_status = new Fl_Output (5 * m_status_h, toolbar_y,
821  ww - 5 * m_status_h, m_status_h, "");
822 
823  m_status->textcolor (FL_BLACK);
824  m_status->color (FL_GRAY);
825  m_status->textfont (FL_COURIER);
826  m_status->textsize (10);
827  m_status->box (FL_ENGRAVED_BOX);
828 
829  m_autoscale = new Fl_Button (0, toolbar_y, m_status_h, m_status_h, "A");
830  m_autoscale->callback (button_callback, static_cast<void *> (this));
831  m_autoscale->tooltip ("Autoscale");
832 
833  m_togglegrid = new Fl_Button (m_status_h, toolbar_y, m_status_h, m_status_h, "G");
834  m_togglegrid->callback (button_callback, static_cast<void *> (this));
835  m_togglegrid->tooltip ("Toggle Grid");
836 
837  m_panzoom = new Fl_Button (2* m_status_h, toolbar_y, m_status_h, m_status_h, "P");
838  m_panzoom->callback (button_callback, static_cast<void *> (this));
839  m_panzoom->tooltip ("Mouse Pan/Zoom");
840 
841  m_rotate = new Fl_Button (3 * m_status_h, toolbar_y, m_status_h, m_status_h, "R");
842  m_rotate->callback (button_callback, static_cast<void *> (this));
843  m_rotate->tooltip ("Mouse Rotate");
844 
845  m_help = new Fl_Button (4 * m_status_h, toolbar_y, m_status_h, m_status_h, "?");
846  m_help->callback (button_callback, static_cast<void *> (this));
847  m_help->tooltip ("Help");
848 
849  end ();
850 
851  set_name ();
852  m_uimenu->add_to_menu (m_fp);
853  if (m_fp.menubar_is ("none") || ! m_uimenu->items_to_show ())
854  hide_menubar ();
855 
856  update_boundingbox (internal);
857 
858  if (m_fp.is_visible ())
859  {
860  // FIXME: This code should be removed when Octave drops support
861  // for FLTK 1.1. Search for default_xclass in this file to find
862  // code that should be uncommented to take its place.
863  //
864  // Set WM_CLASS which allows window managers to properly group
865  // related windows. Otherwise, the class is just "FLTK"
866  xclass ("Octave");
867 
868  show ();
869 
870 #if defined (HAVE_X_WINDOWS)
871  std::string show_gui_msgs
872  = octave::sys::env::getenv ("OCTAVE_SHOW_GUI_MESSAGES");
873 
874  // Installing our handler suppresses the messages.
875  if (show_gui_msgs.empty ())
876  XSetErrorHandler (xerror_handler);
877 #endif
878 
879  if (m_fp.get_currentaxes ().ok ())
880  show_canvas ();
881  else
882  hide_canvas ();
883  }
884  }
885 
886  // No copying!
887 
888  plot_window (const plot_window&) = delete;
889 
890  plot_window& operator = (const plot_window&) = delete;
891 
892  ~plot_window (void)
893  {
894  this->hide ();
895  Fl::check ();
896 
897  delete m_uimenu;
898 
899  // FLTK is supposed to manage memory for widgets.
900  }
901 
902  double number (void) { return m_fp.get___myhandle__ ().value (); }
903 
904  void renumber (double new_number)
905  {
906  if (! m_canvas)
907  error ("unable to renumber figure");
908 
909  if (m_canvas->renumber (new_number))
910  mark_modified ();
911  }
912 
913  void print (const std::string& cmd, const std::string& term)
914  {
915  m_canvas->print (cmd, term);
916  }
917 
918  uint8NDArray get_pixels ()
919  {
920  return m_canvas->get_pixels ();
921  }
922 
923  void show_menubar (void)
924  {
925  m_uimenu->show ();
926  update_toolbar_position ();
927  }
928 
929  void hide_menubar (void)
930  {
931  m_uimenu->hide ();
932  update_toolbar_position ();
933  }
934 
935  void uimenu_update (const graphics_handle& gh, int id)
936  {
937  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
938 
939  graphics_object uimenu_obj = gh_mgr.get_object (gh);
940 
941  if (uimenu_obj.valid_object () && uimenu_obj.isa ("uimenu"))
942  {
943  uimenu::properties& uimenup
944  = dynamic_cast<uimenu::properties&> (uimenu_obj.get_properties ());
945  std::string fltk_label = uimenup.get___fltk_label__ ();
946  graphics_object fig = uimenu_obj.get_ancestor ("figure");
947  figure::properties& figp
948  = dynamic_cast<figure::properties&> (fig.get_properties ());
949 
950  switch (id)
951  {
952  case base_properties::ID_BEINGDELETED:
953  m_uimenu->remove_from_menu (uimenup);
954  break;
955 
956  case base_properties::ID_VISIBLE:
957  m_uimenu->update_visible (uimenup);
958  break;
959 
960  case uimenu::properties::ID_ACCELERATOR:
961  m_uimenu->update_accelerator (uimenup);
962  break;
963 
964  case uimenu::properties::ID_MENUSELECTEDFCN:
965  m_uimenu->update_menuselectedfcn (uimenup);
966  break;
967 
968  case uimenu::properties::ID_CHECKED:
969  m_uimenu->add_to_menu (figp);//rebuilding entire menu
970  break;
971 
972  case uimenu::properties::ID_ENABLE:
973  m_uimenu->update_enable (uimenup);
974  break;
975 
976  case uimenu::properties::ID_FOREGROUNDCOLOR:
977  m_uimenu->update_foregroundcolor (uimenup);
978  break;
979 
980  case uimenu::properties::ID_LABEL:
981  m_uimenu->add_to_menu (figp);//rebuilding entire menu
982  break;
983 
984  case uimenu::properties::ID_POSITION:
985  m_uimenu->add_to_menu (figp);//rebuilding entire menu
986  break;
987 
988  case uimenu::properties::ID_SEPARATOR:
989  m_uimenu->update_seperator (uimenup);
990  break;
991  }
992 
993  if (m_uimenu->items_to_show ())
994  show_menubar ();
995  else
996  hide_menubar ();
997  }
998  }
999 
1000  void show_canvas (void)
1001  {
1002  if (! m_canvas->can_do ())
1003  error ("unable to plot due to insufficient OpenGL support");
1004  else if (m_fp.is_visible ())
1005  {
1006  m_canvas->show ();
1007  m_canvas->make_current ();
1008  }
1009  }
1010 
1011  void hide_canvas (void)
1012  {
1013  m_canvas->hide ();
1014  }
1015 
1016  // Move the toolbar at the bottom of the plot_window.
1017  // The only reason for moving the toolbar is hiding and
1018  // showing the menubar. All other resizing is done by fltk.
1019 
1020  void update_toolbar_position ()
1021  {
1022  int old_canvas_h = m_canvas->h ();
1023 
1024  // keep position fix, change outerposition accordingly
1025  update_boundingbox (true);
1026  m_canvas->resize (0, menu_dy (), w (), old_canvas_h);
1027 
1028  int toolbar_y = m_canvas->h () + menu_dy () + 1;
1029  m_autoscale->position (0, toolbar_y);
1030  m_togglegrid->position (m_status_h, toolbar_y);
1031  m_panzoom->position (2 * m_status_h, toolbar_y);
1032  m_rotate->position (3 * m_status_h, toolbar_y);
1033  m_help->position (4 * m_status_h, toolbar_y);
1034  m_status->resize (5 * m_status_h, toolbar_y,
1035  w () - 5 * m_status_h, m_status_h);
1036  init_sizes ();
1037  redraw ();
1038  }
1039 
1040  Matrix outerposition2position (const Matrix& outerpos)
1041  {
1042  Matrix pos = outerpos;
1043  pos(1) += menu_dy ();
1044  pos(3) -= menu_dy () + m_status_h + 2;
1045  return pos;
1046  }
1047 
1048  Matrix position2outerposition (const Matrix& pos)
1049  {
1050  Matrix outerpos = pos;
1051  outerpos(1) -= menu_dy ();
1052  outerpos(3) += menu_dy () + m_status_h + 2;
1053  return outerpos;
1054  }
1055 
1056  // Called from figure::properties::ID_POSITION if internal = true
1057  // or ID_OUTERPOSITION if false.
1058  // (someone has requested a position change with set (h, "position", [...])
1059  // or set (h, "outerposition", [...])
1060 
1061  void update_boundingbox (bool internal)
1062  {
1063  Matrix bb = m_fp.get_boundingbox (internal);
1064  if (internal)
1065  bb = position2outerposition (bb);
1066  resize (bb(0), bb(1), bb(2), bb(3));
1067  }
1068 
1069  void mark_modified (void)
1070  {
1071  m_canvas->redraw ();
1072  }
1073 
1074  void set_name (void)
1075  {
1076  m_window_label = m_fp.get_title ();
1077  label (m_window_label.c_str ());
1078  }
1079 
1080 private:
1081 
1082  // window name -- this must exists for the duration of the window's
1083  // life
1084  std::string m_window_label;
1085 
1086  // Figure properties.
1087  figure::properties& m_fp;
1088 
1089  // Status area height.
1090  static const int m_status_h = 20;
1091 
1092  // Menu height
1093  static const int m_menu_h = 25;
1094 
1095  fltk_uimenu *m_uimenu;
1096 
1097  OpenGL_fltk *m_canvas;
1098 
1099  Fl_Button *m_autoscale;
1100  Fl_Button *m_togglegrid;
1101  Fl_Button *m_panzoom;
1102  Fl_Button *m_rotate;
1103  Fl_Button *m_help;
1104  Fl_Output *m_status;
1105 
1106  Fl_Box *m_resize_dummy;
1107 
1108  graphics_object m_ax_obj;
1109 
1110  int m_pos_x;
1111  int m_pos_y;
1112 
1113  // Window callback.
1114  static void window_close (Fl_Widget *, void *data)
1115  {
1116  octave_value_list args;
1117  args(0) = static_cast<plot_window *> (data)->number ();
1118  octave::feval ("close", args);
1119  }
1120 
1121  // Button callbacks.
1122  static void button_callback (Fl_Widget *ww, void *data)
1123  {
1124  static_cast<plot_window *> (data)->button_press (ww, data);
1125  }
1126 
1127  void button_press (Fl_Widget *widg, void *)
1128  {
1129  if (widg == m_autoscale)
1130  axis_auto ();
1131  else if (widg == m_togglegrid)
1132  toggle_grid ();
1133  else if (widg == m_panzoom)
1134  m_fp.set___mouse_mode__ ("pan");
1135  else if (widg == m_rotate)
1136  m_fp.set___mouse_mode__ ("rotate");
1137  else if (widg == m_help)
1138  fl_message ("%s", help_text);
1139  }
1140 
1141  void set_on_ax_obj (const std::string& name, const std::string& value)
1142  {
1143  // ax_obj is the last clicked axes object
1144  if (m_ax_obj && m_ax_obj.isa ("axes")
1145  && m_ax_obj.get_properties ().get_tag () != "legend"
1146  && m_ax_obj.get_properties ().get_tag () != "colorbar")
1147  {
1148  axes::properties& ap
1149  = dynamic_cast<axes::properties&>(m_ax_obj.get_properties ());
1150  ap.set (name, value);
1151  }
1152  else // no axes object clicked so far, take currentaxes
1153  {
1154  graphics_handle gh = m_fp.get_currentaxes ();
1155  if (gh.ok ())
1156  {
1157  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
1158 
1159  graphics_object go = gh_mgr.get_object (gh);
1160 
1161  axes::properties& ap
1162  = dynamic_cast<axes::properties&>(go.get_properties ());
1163 
1164  ap.set (name, value);
1165  }
1166  }
1167  }
1168 
1169  void axis_auto (void)
1170  {
1171  octave_value_list args;
1172  if (m_fp.get_currentaxes ().ok ())
1173  {
1174  args(0) = m_fp.get_currentaxes ().as_octave_value ();
1175  args(1) = "auto";
1176  octave::feval ("axis", args);
1177  mark_modified ();
1178  }
1179  }
1180 
1181  void toggle_grid (void)
1182  {
1183  octave_value_list args;
1184  if (m_fp.get_currentaxes ().ok ())
1185  args(0) = m_fp.get_currentaxes ().as_octave_value ();
1186 
1187  octave::feval ("grid", args);
1188  mark_modified ();
1189  }
1190 
1191  void pixel2pos (const graphics_handle& ax, int px, int py, double& xx,
1192  double& yy) const
1193  {
1194  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
1195 
1196  pixel2pos (gh_mgr.get_object (ax), px, py, xx, yy);
1197  }
1198 
1199  void pixel2pos (graphics_object ax, int px, int py, double& xx,
1200  double& yy) const
1201  {
1202  if (ax && ax.isa ("axes"))
1203  {
1204  axes::properties& ap
1205  = dynamic_cast<axes::properties&> (ax.get_properties ());
1206  ColumnVector pp = ap.pixel2coord (px, py);
1207  xx = pp(0);
1208  yy = pp(1);
1209  }
1210  }
1211 
1212  graphics_handle pixel2axes_or_ca (int px, int py)
1213  {
1214  Matrix kids = m_fp.get_children ();
1215  int len = kids.numel ();
1216 
1217  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
1218 
1219  for (int k = 0; k < len; k++)
1220  {
1221  graphics_handle hnd = gh_mgr.lookup (kids(k));
1222 
1223  if (hnd.ok ())
1224  {
1225  graphics_object kid = gh_mgr.get_object (hnd);
1226 
1227  if (kid.valid_object () && kid.isa ("axes"))
1228  {
1229  Matrix bb = kid.get_properties ().get_boundingbox (false);
1230 
1231  if (bb(0) <= px && px < (bb(0)+bb(2))
1232  && bb(1) <= py && py < (bb(1)+bb(3)))
1233  {
1234  return hnd;
1235  }
1236  }
1237  }
1238  }
1239  return m_fp.get_currentaxes ();
1240  }
1241 
1242  void pixel2status (const graphics_handle& ax, int px0, int py0,
1243  int px1 = -1, int py1 = -1)
1244  {
1245  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
1246 
1247  pixel2status (gh_mgr.get_object (ax), px0, py0, px1, py1);
1248  }
1249 
1250  void pixel2status (graphics_object ax, int px0, int py0,
1251  int px1 = -1, int py1 = -1)
1252  {
1253  double x0, y0, x1, y1;
1254  x0 = y0 = x1 = y1 = octave::numeric_limits<double>::NaN ();
1255  std::stringstream cbuf;
1256  cbuf.precision (4);
1257  cbuf.width (6);
1258  pixel2pos (ax, px0, py0, x0, y0);
1259  cbuf << '[' << x0 << ", " << y0 << ']';
1260  if (px1 >= 0)
1261  {
1262  pixel2pos (ax, px1, py1, x1, y1);
1263  cbuf << " -> ["<< x1 << ", " << y1 << ']';
1264  }
1265 
1266  m_status->value (cbuf.str ().c_str ());
1267  }
1268 
1269  void view2status (graphics_object ax)
1270  {
1271  if (ax && ax.isa ("axes"))
1272  {
1273  axes::properties& ap
1274  = dynamic_cast<axes::properties&> (ax.get_properties ());
1275  std::stringstream cbuf;
1276  cbuf.precision (4);
1277  cbuf.width (6);
1278  Matrix v (1, 2, 0);
1279  v = ap.get ("view").matrix_value ();
1280  cbuf << "[azimuth: " << v(0) << ", elevation: " << v(1) << ']';
1281 
1282  m_status->value (cbuf.str ().c_str ());
1283  }
1284  }
1285 
1286  void set_currentpoint (int px, int py)
1287  {
1288  if (! m_fp.is_beingdeleted ())
1289  {
1290  Matrix pos = m_fp.map_from_boundingbox (px, py);
1291  m_fp.set_currentpoint (pos);
1292 
1293  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
1294 
1295  graphics_object robj = gh_mgr.get_object (m_fp.get_parent ());
1296 
1298 
1299  = dynamic_cast<root_figure::properties&> (robj.get_properties ());
1300  rp.set_currentfigure (m_fp.get___myhandle__ ().value ());
1301  }
1302  }
1303 
1304  void set_axes_currentpoint (graphics_object ax, int px, int py)
1305  {
1306  if (ax.valid_object () && ax.isa ("axes"))
1307  {
1308  axes::properties& ap
1309  = dynamic_cast<axes::properties&> (ax.get_properties ());
1310 
1311  Matrix x_zlim = ap.get_transform_zlim ();
1312  Matrix pos (2, 3, 0.0);
1313 
1314  // front point (nearest to the viewer)
1315  ColumnVector tmp = ap.get_transform ().untransform (px, py, x_zlim(0));
1316  pos(0, 0) = tmp(0);
1317  pos(0, 1) = tmp(1);
1318  pos(0, 2) = tmp(2);
1319 
1320  // back point (furthest from the viewer)
1321  tmp = ap.get_transform ().untransform (px, py, x_zlim(1));
1322  pos(1, 0) = tmp(0);
1323  pos(1, 1) = tmp(1);
1324  pos(1, 2) = tmp(2);
1325 
1326  ap.set_currentpoint (pos);
1327  if (ap.get_tag () != "legend" && ap.get_tag () != "colorbar")
1328  m_fp.set_currentaxes (ap.get___myhandle__ ().value ());
1329  }
1330  }
1331 
1332  int menu_dy ()
1333  {
1334  if (m_uimenu->is_visible ())
1335  return m_menu_h;
1336  else
1337  return 0;
1338  }
1339 
1340  octave_scalar_map format_key_event (int e_key, const char *e_text,
1341  int e_state)
1342  {
1343  octave_scalar_map evt;
1344 
1345  evt.assign ("Character", octave_value (e_text));
1346  evt.assign ("Modifier", octave_value (modifier2cell (e_state)));
1347 
1348  std::string key_str;
1349  std::ostringstream tmp_str;
1350 
1351  if (e_key == FL_Escape)
1352  key_str = "escape";
1353  else if (e_key == FL_Tab)
1354  key_str = "tab";
1355  else if (e_key == FL_Caps_Lock)
1356  key_str = "capslock";
1357  else if (e_key == FL_Shift_L || e_key == FL_Shift_R)
1358  key_str = "shift";
1359  else if (e_key == FL_Control_L || e_key == FL_Control_R)
1360  key_str = "control";
1361  else if (e_key == FL_Meta_L || e_key == FL_Meta_R)
1362  key_str = "windows";
1363  else if (e_key == FL_Alt_L || e_key == FL_Alt_R)
1364  key_str = "alt";
1365  else if (e_key == 32)
1366  key_str = "space";
1367  else if (e_key == FL_Enter)
1368  key_str = "return";
1369  else if (e_key == FL_BackSpace)
1370  key_str = "backspace";
1371  else if (e_key == FL_Print)
1372  key_str = "printscreen";
1373  else if (e_key == FL_Pause)
1374  key_str = "pause";
1375  else if (e_key == FL_Home)
1376  key_str = "home";
1377  else if (e_key == FL_End)
1378  key_str = "end";
1379  else if (e_key == FL_Insert)
1380  key_str = "insert";
1381  else if (e_key == FL_Page_Up)
1382  key_str = "pageup";
1383  else if (e_key == FL_Delete)
1384  key_str = "delete";
1385  else if (e_key == FL_Page_Down)
1386  key_str = "pagedown";
1387  else if (e_key == FL_Left)
1388  key_str = "leftarrow";
1389  else if (e_key == FL_Up)
1390  key_str = "uparrow";
1391  else if (e_key == FL_Right)
1392  key_str = "rightarrow";
1393  else if (e_key == FL_Down)
1394  key_str = "downarrow";
1395  else if (e_key == FL_Num_Lock)
1396  key_str = "numlock";
1397  else if (e_key == 0xffaf)
1398  key_str = "divide";
1399  else if (e_key == 0xffaa)
1400  key_str = "multiply";
1401  else if (e_key == 0xffad)
1402  key_str = "subtract";
1403  else if (e_key == 0xffab)
1404  key_str = "add";
1405  else if (e_key == 0xff8d)
1406  key_str = "return";
1407  else if (e_key == 0xffac)
1408  key_str = "separator";
1409  else if (e_key >= 0xffb0 && e_key <= 0xffb9)
1410  {
1411  tmp_str << "numpad" << (e_key - 0xffb0);
1412  key_str = tmp_str.str ();
1413  }
1414  else if (e_key >= (FL_F + 1) && e_key <= (FL_F + 12))
1415  {
1416  tmp_str << 'f' << (e_key - FL_F);
1417  key_str = tmp_str.str ();
1418  }
1419  else if (e_key == ',')
1420  key_str = "comma";
1421  else if (e_key == '.')
1422  key_str = "period";
1423  else if (e_key == '-')
1424  key_str = "hyphen";
1425  else if (e_key == '^' || e_key == '+' || e_key == '#'
1426  || e_key == '<' || e_key == 0xfe03 /*AltGr*/)
1427  key_str = "0";
1428  else if (isalnum (e_key))
1429  key_str = std::tolower (e_key);
1430  else if (isprint (e_text[0]))
1431  key_str = "0";
1432 
1433  evt.assign ("Key", octave_value (key_str));
1434  return evt;
1435  }
1436 
1437  Cell modifier2cell (int e_state)
1438  {
1440 
1441  if (e_state & FL_SHIFT)
1442  mod.append (std::string ("shift"));
1443  if (e_state & FL_CTRL)
1444  mod.append (std::string ("control"));
1445  if (e_state & FL_ALT)
1446  mod.append (std::string ("alt"));
1447  if (e_state & FL_COMMAND)
1448  mod.append (std::string ("command"));
1449  return Cell (mod);
1450  }
1451 
1452  void resize (int xx, int yy, int ww, int hh)
1453  {
1454  Fl_Window::resize (xx, yy, ww, hh);
1455 
1456  Matrix bb (1, 4);
1457  bb(0) = xx;
1458  bb(1) = yy;
1459  bb(2) = ww;
1460  bb(3) = hh;
1461 
1462  // update outerposition
1463  m_fp.set_boundingbox (bb, false, false);
1464 
1465  // update position
1466  m_fp.set_boundingbox (outerposition2position (bb), true, false);
1467  }
1468 
1469  bool pan_enabled (void)
1470  {
1471  // Getting pan mode property:
1472  octave_value ov_pm = m_fp.get___pan_mode__ ();
1473 
1474  octave_scalar_map pm = ov_pm.scalar_map_value ();
1475 
1476  return pm.contents ("Enable").string_value () == "on";
1477  }
1478 
1479  std::string pan_mode (void)
1480  {
1481  // Getting pan mode property:
1482  octave_value ov_pm = m_fp.get___pan_mode__ ();
1483 
1484  octave_scalar_map pm = ov_pm.scalar_map_value ();
1485 
1486  return pm.contents ("Motion").string_value ();
1487  }
1488 
1489  bool rotate_enabled (void)
1490  {
1491  // Getting rotate mode property:
1492  octave_value ov_rm = m_fp.get___rotate_mode__ ();
1493 
1494  octave_scalar_map rm = ov_rm.scalar_map_value ();
1495 
1496  return rm.contents ("Enable").string_value () == "on";
1497  }
1498 
1499  int handle (int event)
1500  {
1501  if (event == FL_FOCUS)
1502  return 1;
1503 
1504  graphics_handle gh;
1505 
1506  if (! m_fp.is_beingdeleted ())
1507  {
1508  // FLTK resends keyboard events with flipped case if all
1509  // widgets rejects the event.
1510  // See Event Propagation http://www.fltk.org/doc-1.3/events.html
1511  static bool key_resent_detected = false;
1512 
1513  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
1514 
1515  switch (event)
1516  {
1517  case FL_SHORTCUT:
1518  {
1519  // check if it a resent event with switched case
1520  static int last_event_key = 0;
1521  static char last_event_text = 0;
1522 
1523  int e_key = Fl::event_key ();
1524  char e_text = Fl::event_text ()[0];
1525  key_resent_detected = (e_key == last_event_key
1526  && std::tolower (last_event_text) == std::tolower (e_text)
1527  && ((islower (last_event_text) && isupper (e_text))
1528  || (isupper (last_event_text) && islower (e_text))));
1529 
1530  last_event_key = e_key;
1531  last_event_text = e_text;
1532  }
1533  break;
1534 
1535  case FL_KEYDOWN:
1536  {
1537  int e_key = Fl::event_key ();
1538  const char *e_text = Fl::event_text ();
1539  int e_state = Fl::event_state ();
1540  octave_scalar_map evt = format_key_event (e_key, e_text, e_state);
1541 
1542  m_fp.set_currentcharacter (std::string (e_text));
1543 
1544  if (! m_fp.get_keypressfcn ().isempty ()
1545  && (evt.contents ("Key").length () > 0))
1546  {
1547  // Update CurrentPoint before callback
1548  if (Fl::event_inside (m_canvas))
1549  {
1550  m_pos_x = Fl::event_x ();
1551  m_pos_y = Fl::event_y () - menu_dy ();
1552 
1553  set_currentpoint (m_pos_x, m_pos_y);
1554 
1555  gh = pixel2axes_or_ca (m_pos_x, m_pos_y);
1556 
1557  if (gh.ok ())
1558  {
1559  m_ax_obj = gh_mgr.get_object (gh);
1560  set_axes_currentpoint (m_ax_obj, m_pos_x, m_pos_y);
1561  }
1562  }
1563 
1564  m_fp.execute_keypressfcn (evt);
1565  }
1566 
1567  // Handle special keys used in toolbar
1568  switch (e_key)
1569  {
1570  case 'a':
1571  case 'A':
1572  axis_auto ();
1573  return 1;
1574 
1575  case 'g':
1576  case 'G':
1577  toggle_grid ();
1578  return 1;
1579 
1580  case 'p':
1581  case 'P':
1582  m_fp.set___mouse_mode__ ("pan");
1583  return 1;
1584 
1585  case 'r':
1586  case 'R':
1587  m_fp.set___mouse_mode__ ("rotate");
1588  return 1;
1589  }
1590  }
1591  break;
1592 
1593  case FL_KEYUP:
1594  {
1595  int e_key = Fl::event_key ();
1596  int e_state = Fl::event_state ();
1597  octave_scalar_map evt;
1598  if (key_resent_detected && Fl::event_length () == 1)
1599  {
1600  // FLTK flipped the case of Fl::event_text because no
1601  // widget wanted the FL_KEYDOWN event.
1602  char tmp_e_text[2];
1603  tmp_e_text[0] = Fl::event_text ()[0];
1604  tmp_e_text[1] = 0;
1605  // Undo the case flip
1606  if (std::islower (tmp_e_text[0]))
1607  tmp_e_text[0] = std::toupper (tmp_e_text[0]);
1608  else
1609  tmp_e_text[0] = std::tolower (tmp_e_text[0]);
1610  evt = format_key_event (e_key, tmp_e_text, e_state);
1611  }
1612  else
1613  {
1614  const char *e_text = Fl::event_text ();
1615  evt = format_key_event (e_key, e_text, e_state);
1616  }
1617 
1618  if (! m_fp.get_keyreleasefcn ().isempty ()
1619  && (evt.contents ("Key").length () > 0))
1620  m_fp.execute_keyreleasefcn (evt);
1621  return 1;
1622  }
1623  break;
1624  }
1625 
1626  // Events we only handle if they are in the canvas area.
1627  if (Fl::event_inside (m_canvas))
1628  switch (event)
1629  {
1630  case FL_MOVE:
1631  pixel2status (pixel2axes_or_ca (Fl::event_x (),
1632  Fl::event_y () - menu_dy ()),
1633  Fl::event_x (), Fl::event_y () - menu_dy ());
1634  return 1;
1635 
1636  case FL_PUSH:
1637  m_pos_x = Fl::event_x ();
1638  m_pos_y = Fl::event_y () - menu_dy ();
1639 
1640  set_currentpoint (m_pos_x, m_pos_y);
1641 
1642  if (Fl::event_clicks ())
1643  m_fp.set_selectiontype ("open");
1644  else if (Fl::event_button () == FL_MIDDLE_MOUSE
1645  || (Fl::event_button () == FL_LEFT_MOUSE
1646  && Fl::event_shift ()))
1647  m_fp.set_selectiontype ("extend");
1648  else if (Fl::event_button () == FL_RIGHT_MOUSE
1649  || (Fl::event_button () == FL_LEFT_MOUSE
1650  && Fl::event_ctrl ()))
1651  m_fp.set_selectiontype ("alt");
1652  else
1653  m_fp.set_selectiontype ("normal");
1654 
1655  gh = pixel2axes_or_ca (m_pos_x, m_pos_y);
1656 
1657  if (gh.ok ())
1658  {
1659  m_ax_obj = gh_mgr.get_object (gh);
1660  set_axes_currentpoint (m_ax_obj, m_pos_x, m_pos_y);
1661  }
1662 
1663  // Ensure windowbuttondownfcn is called after currentpoint
1664  // is updated but before calling buttondownfcn.
1665  if (! m_fp.get_windowbuttondownfcn ().isempty ())
1666  m_fp.execute_windowbuttondownfcn (Fl::event_button ());
1667 
1668  if (gh.ok ())
1669  {
1670  m_fp.set_currentobject (m_ax_obj.get_handle ().value ());
1671 
1672  base_properties& props = m_ax_obj.get_properties ();
1673  if (! props.get_buttondownfcn ().isempty ())
1674  props.execute_buttondownfcn (Fl::event_button ());
1675 
1676  return 1;
1677  }
1678  else if (! m_fp.get_buttondownfcn ().isempty ())
1679  m_fp.execute_buttondownfcn (Fl::event_button ());
1680 
1681  break;
1682 
1683  case FL_DRAG:
1684  if (! m_fp.get_windowbuttonmotionfcn ().isempty ())
1685  {
1686  set_currentpoint (Fl::event_x (), Fl::event_y () - menu_dy ());
1687  m_fp.execute_windowbuttonmotionfcn ();
1688  }
1689 
1690  if (Fl::event_button () == 1)
1691  {
1692  if (m_ax_obj && m_ax_obj.isa ("axes"))
1693  {
1694  axes::properties& ap = dynamic_cast<axes::properties&> (m_ax_obj.get_properties ());
1695 
1696  // Don't pan or rotate legend
1697  if (ap.get_tag () != "legend")
1698  {
1699  if (rotate_enabled ())
1700  view2status (m_ax_obj);
1701  else
1702  pixel2status (m_ax_obj, m_pos_x, m_pos_y,
1703  Fl::event_x (),
1704  Fl::event_y () - menu_dy ());
1705 
1706  double x0, y0, x1, y1;
1707  Matrix pos = m_fp.get_boundingbox (true);
1708  pixel2pos (m_ax_obj, m_pos_x, m_pos_y, x0, y0);
1709  pixel2pos (m_ax_obj, Fl::event_x (),
1710  Fl::event_y () - menu_dy (),
1711  x1, y1);
1712 
1713  if (pan_enabled ())
1714  {
1715  std::string mode = pan_mode ();
1716 
1717  ap.translate_view (mode, x0, x1, y0, y1);
1718  }
1719  else if (rotate_enabled ())
1720  {
1721  double daz, del;
1722  daz = (Fl::event_x () - m_pos_x) / pos(2) * 360;
1723  del = (Fl::event_y () - menu_dy () - m_pos_y)
1724  / pos(3) * 360;
1725  ap.rotate_view (del, daz);
1726  }
1727  }
1728  else
1729  {
1730  // move the position of the legend
1731  Matrix pos = ap.get_position ().matrix_value ();
1732  pos(0) += double (Fl::event_x () - m_pos_x)
1733  / m_canvas->w ();
1734  pos(1) -= double (Fl::event_y () - menu_dy () - m_pos_y)
1735  / m_canvas->h ();
1736  ap.set_position (pos);
1737  }
1738 
1739  m_pos_x = Fl::event_x ();
1740  m_pos_y = Fl::event_y () - menu_dy ();
1741  mark_modified ();
1742  }
1743  return 1;
1744  }
1745  else if (Fl::event_button () == 3)
1746  {
1747  pixel2status (m_ax_obj, m_pos_x, m_pos_y,
1748  Fl::event_x (), Fl::event_y () - menu_dy ());
1749  Matrix zoom_box (1, 4, 0);
1750  zoom_box(0) = m_pos_x;
1751  zoom_box(1) = m_pos_y;
1752  zoom_box(2) = Fl::event_x ();
1753  zoom_box(3) = Fl::event_y () - menu_dy ();
1754  m_canvas->set_zoom_box (zoom_box);
1755  m_canvas->zoom (true);
1756  mark_modified ();
1757  return 1;
1758  }
1759 
1760  break;
1761 
1762  case FL_MOUSEWHEEL:
1763  {
1764  graphics_object ax
1765  = gh_mgr.get_object (pixel2axes_or_ca (Fl::event_x (),
1766  Fl::event_y ()
1767  - menu_dy ()));
1768  if (ax && ax.isa ("axes"))
1769  {
1770  axes::properties& ap
1771  = dynamic_cast<axes::properties&> (ax.get_properties ());
1772 
1773  // Control how fast to zoom when using scroll wheel.
1774  double wheel_zoom_speed = ap.get_mousewheelzoom ();
1775 
1776  // Determine if we're zooming in or out.
1777  const double factor = (Fl::event_dy () < 0
1778  ? 1 / (1.0 - wheel_zoom_speed)
1779  : 1.0 - wheel_zoom_speed);
1780 
1781  // Get the point we're zooming about.
1782  double x1, y1;
1783  pixel2pos (ax, Fl::event_x (), Fl::event_y () - menu_dy (),
1784  x1, y1);
1785 
1786  // FIXME: should we only zoom about point for 2D plots?
1787 
1788  ap.zoom_about_point ("both", x1, y1, factor, false);
1789  mark_modified ();
1790  return 1;
1791  }
1792  }
1793 
1794  break;
1795 
1796  case FL_RELEASE:
1797  if (! m_fp.get_windowbuttonupfcn ().isempty ())
1798  {
1799  set_currentpoint (Fl::event_x (),
1800  Fl::event_y () - menu_dy ());
1801  m_fp.execute_windowbuttonupfcn ();
1802  }
1803 
1804  if ((Fl::event_button () == 1) && Fl::event_clicks ())
1805  {
1806  // Double click
1807  set_on_ax_obj ("xlimmode", "auto");
1808  set_on_ax_obj ("ylimmode", "auto");
1809  set_on_ax_obj ("zlimmode", "auto");
1810  mark_modified ();
1811  return 1;
1812  }
1813  if (Fl::event_button () == 3)
1814  {
1815  // End of drag -- zoom.
1816  if (m_canvas->zoom ())
1817  {
1818  m_canvas->zoom (false);
1819  double x0, y0, x1, y1;
1820  if (m_ax_obj && m_ax_obj.isa ("axes"))
1821  {
1822  axes::properties& ap = dynamic_cast<axes::properties&>
1823  (m_ax_obj.get_properties ());
1824  pixel2pos (m_ax_obj, m_pos_x, m_pos_y, x0, y0);
1825  int pos_x1 = Fl::event_x ();
1826  int pos_y1 = Fl::event_y () - menu_dy ();
1827  pixel2pos (m_ax_obj, pos_x1, pos_y1, x1, y1);
1828  Matrix xl (1, 2, 0);
1829  Matrix yl (1, 2, 0);
1830  int dx = abs (m_pos_x - pos_x1);
1831  int dy = abs (m_pos_y - pos_y1);
1832  // Smallest zoom box must be 4 pixels square
1833  if ((dx > 4) && (dy > 4))
1834  {
1835  if (x0 < x1)
1836  {
1837  xl(0) = x0;
1838  xl(1) = x1;
1839  }
1840  else
1841  {
1842  xl(0) = x1;
1843  xl(1) = x0;
1844  }
1845  if (y0 < y1)
1846  {
1847  yl(0) = y0;
1848  yl(1) = y1;
1849  }
1850  else
1851  {
1852  yl(0) = y1;
1853  yl(1) = y0;
1854  }
1855  ap.zoom ("both", xl, yl);
1856  }
1857  mark_modified ();
1858  return 1;
1859  }
1860  }
1861  }
1862  break;
1863  }
1864  }
1865 
1866  return Fl_Window::handle (event);
1867  }
1868 };
1869 
1870 class figure_manager
1871 {
1872 private:
1873 
1874  figure_manager (void) = default;
1875 
1876 public:
1877 
1878  // No copying!
1879 
1880  figure_manager (const figure_manager&) = delete;
1881 
1882  figure_manager& operator = (const figure_manager&) = delete;
1883 
1884  ~figure_manager (void)
1885  {
1886  close_all ();
1887  }
1888 
1889  static bool instance_ok (void)
1890  {
1891  bool retval = true;
1892 
1893  if (! instance)
1894  instance = new figure_manager ();
1895 
1896  return retval;
1897  }
1898 
1899  static void close_all (void)
1900  {
1901  if (instance_ok ())
1902  instance->do_close_all ();
1903  }
1904 
1905  static void new_window (figure::properties& fp)
1906  {
1907  if (instance_ok ())
1908  instance->do_new_window (fp);
1909  }
1910 
1911  static void delete_window (int idx)
1912  {
1913  if (instance_ok ())
1914  instance->do_delete_window (idx);
1915  }
1916 
1917  static void delete_window (const std::string& idx_str)
1918  {
1919  delete_window (str2idx (idx_str));
1920  }
1921 
1922  static void renumber_figure (const std::string& idx_str, double new_number)
1923  {
1924  if (instance_ok ())
1925  instance->do_renumber_figure (str2idx (idx_str), new_number);
1926  }
1927 
1928  static void toggle_window_visibility (int idx, bool is_visible)
1929  {
1930  if (instance_ok ())
1931  instance->do_toggle_window_visibility (idx, is_visible);
1932  }
1933 
1934  static void toggle_window_visibility (const std::string& idx_str,
1935  bool is_visible)
1936  {
1937  toggle_window_visibility (str2idx (idx_str), is_visible);
1938  }
1939 
1940  static void mark_modified (int idx)
1941  {
1942  if (instance_ok ())
1943  instance->do_mark_modified (idx);
1944  }
1945 
1946  static void mark_modified (const graphics_handle& gh)
1947  {
1948  mark_modified (hnd2idx (gh));
1949  }
1950 
1951  static void set_name (int idx)
1952  {
1953  if (instance_ok ())
1954  instance->do_set_name (idx);
1955  }
1956 
1957  static void set_name (const std::string& idx_str)
1958  {
1959  set_name (str2idx (idx_str));
1960  }
1961 
1962  static Matrix get_size (int idx)
1963  {
1964  return instance_ok () ? instance->do_get_size (idx) : Matrix ();
1965  }
1966 
1967  static Matrix get_size (const graphics_handle& gh)
1968  {
1969  return get_size (hnd2idx (gh));
1970  }
1971 
1972  static void print (const graphics_handle& gh, const std::string& cmd,
1973  const std::string& term)
1974  {
1975  if (instance_ok ())
1976  instance->do_print (hnd2idx (gh), cmd, term);
1977  }
1978 
1979  static uint8NDArray get_pixels (const graphics_handle& gh)
1980  {
1981  uint8NDArray retval;
1982  if (instance_ok ())
1983  retval = instance->do_get_pixels (hnd2idx (gh));
1984 
1985  return retval;
1986  }
1987 
1988  static void uimenu_update (const graphics_handle& figh,
1989  const graphics_handle& uimenuh, int id)
1990  {
1991  if (instance_ok ())
1992  instance->do_uimenu_update (hnd2idx (figh), uimenuh, id);
1993  }
1994 
1995  static void update_canvas (const graphics_handle& gh,
1996  const graphics_handle& ca)
1997  {
1998  if (instance_ok ())
1999  instance->do_update_canvas (hnd2idx (gh), ca);
2000  }
2001 
2002  static void update_boundingbox (const std::string& fig_idx_str,
2003  bool internal)
2004  {
2005  if (instance_ok ())
2006  instance->do_update_boundingbox (str2idx (fig_idx_str), internal);
2007  }
2008 
2009  static void toggle_menubar_visibility (const std::string& fig_idx_str,
2010  bool menubar_is_figure)
2011  {
2012  if (instance_ok ())
2013  instance->do_toggle_menubar_visibility (str2idx (fig_idx_str),
2014  menubar_is_figure);
2015  }
2016 
2017 private:
2018 
2019  static figure_manager *instance;
2020 
2021  // Singleton -- hide all of the above.
2022 
2023  static int curr_index;
2024 
2025  typedef std::map<int, plot_window *> window_map;
2026 
2027  typedef window_map::iterator wm_iterator;;
2028 
2029  window_map windows;
2030 
2031  static std::string fltk_idx_header;
2032 
2033  void do_close_all (void)
2034  {
2035  wm_iterator win;
2036  for (win = windows.begin (); win != windows.end (); win++)
2037  delete win->second;
2038  windows.clear ();
2039  }
2040 
2041  void do_new_window (figure::properties& fp)
2042  {
2043  int idx = figprops2idx (fp);
2044 
2045  if (idx >= 0 && windows.find (idx) == windows.end ())
2046  {
2047  Matrix pos = fp.get_outerposition ().matrix_value ();
2048  bool internal = false;
2049  // check if figure::properties::outerposition is default -1.0
2050  if (pos(2) != -1.0 && pos(3) != -1.0)
2051  {
2052  pos = fp.get_boundingbox (internal);
2053  }
2054  else
2055  {
2056  // use position
2057  internal = true;
2058  pos = fp.get_boundingbox (internal);
2059  }
2060 
2061  idx2figprops (curr_index, fp);
2062 
2063  windows[curr_index++] = new plot_window (pos(0), pos(1), pos(2), pos(3),
2064  fp, internal);
2065  }
2066  }
2067 
2068  void do_delete_window (int idx)
2069  {
2070  wm_iterator win = windows.find (idx);
2071 
2072  if (win != windows.end ())
2073  {
2074  delete win->second;
2075  windows.erase (win);
2076  }
2077  }
2078 
2079  void do_renumber_figure (int idx, double new_number)
2080  {
2081  wm_iterator win = windows.find (idx);
2082 
2083  if (win != windows.end ())
2084  win->second->renumber (new_number);
2085  }
2086 
2087  void do_toggle_window_visibility (int idx, bool is_visible)
2088  {
2089  wm_iterator win = windows.find (idx);
2090 
2091  if (win != windows.end ())
2092  {
2093  if (is_visible)
2094  {
2095  win->second->show ();
2096  win->second->show_canvas ();
2097  }
2098  else
2099  win->second->hide ();
2100 
2101  }
2102  }
2103 
2104  void do_toggle_menubar_visibility (int fig_idx, bool menubar_is_figure)
2105  {
2106  wm_iterator win = windows.find (fig_idx);
2107 
2108  if (win != windows.end ())
2109  {
2110  if (menubar_is_figure)
2111  win->second->show_menubar ();
2112  else
2113  win->second->hide_menubar ();
2114 
2115  win->second->redraw ();
2116  }
2117  }
2118 
2119  void do_mark_modified (int idx)
2120  {
2121  wm_iterator win = windows.find (idx);
2122 
2123  if (win != windows.end ())
2124  {
2125  win->second->mark_modified ();
2126  }
2127  }
2128 
2129  void do_set_name (int idx)
2130  {
2131  wm_iterator win = windows.find (idx);
2132 
2133  if (win != windows.end ())
2134  win->second->set_name ();
2135  }
2136 
2137  Matrix do_get_size (int idx)
2138  {
2139  Matrix sz (1, 2, 0.0);
2140 
2141  wm_iterator win = windows.find (idx);
2142 
2143  if (win != windows.end ())
2144  {
2145  sz(0) = win->second->w ();
2146  sz(1) = win->second->h ();
2147  }
2148 
2149  return sz;
2150  }
2151 
2152  void do_print (int idx, const std::string& cmd, const std::string& term)
2153  {
2154  wm_iterator win = windows.find (idx);
2155 
2156  if (win != windows.end ())
2157  win->second->print (cmd, term);
2158  }
2159 
2160  uint8NDArray do_get_pixels (int idx)
2161  {
2162  uint8NDArray retval;
2163  wm_iterator win = windows.find (idx);
2164 
2165  if (win != windows.end ())
2166  retval = win->second->get_pixels ();
2167 
2168  return retval;
2169  }
2170 
2171  void do_uimenu_update (int idx, const graphics_handle& gh, int id)
2172  {
2173  wm_iterator win = windows.find (idx);
2174 
2175  if (win != windows.end ())
2176  win->second->uimenu_update (gh, id);
2177  }
2178 
2179  void do_update_canvas (int idx, const graphics_handle& ca)
2180  {
2181  wm_iterator win = windows.find (idx);
2182 
2183  if (win != windows.end ())
2184  {
2185  if (ca.ok ())
2186  win->second->show_canvas ();
2187  else
2188  win->second->hide_canvas ();
2189  }
2190  }
2191 
2192  void do_update_boundingbox (int idx, bool internal)
2193  {
2194  wm_iterator win = windows.find (idx);
2195 
2196  if (win != windows.end ())
2197  win->second->update_boundingbox (internal);
2198  }
2199 
2200  static int str2idx (const caseless_str& clstr)
2201  {
2202  int ind;
2203  if (clstr.find (fltk_idx_header, 0) == 0)
2204  {
2205  std::istringstream istr (clstr.substr (fltk_idx_header.size ()));
2206  if (istr >> ind)
2207  return ind;
2208  }
2209 
2210  error ("figure_manager: could not recognize fltk index");
2211  }
2212 
2213  void idx2figprops (int idx, figure::properties& fp)
2214  {
2215  std::ostringstream ind_str;
2216  ind_str << fltk_idx_header << idx;
2217  fp.set___plot_stream__ (ind_str.str ());
2218  }
2219 
2220  static int figprops2idx (const figure::properties& fp)
2221  {
2222  if (fp.get___graphics_toolkit__ () == FLTK_GRAPHICS_TOOLKIT_NAME)
2223  {
2224  octave_value ps = fp.get___plot_stream__ ();
2225  if (ps.is_string ())
2226  return str2idx (ps.string_value ());
2227  else
2228  return 0;
2229  }
2230 
2231  error ("figure_manager: figure is not fltk");
2232  }
2233 
2234  static int hnd2idx (double h)
2235  {
2236  gh_manager& gh_mgr = octave::__get_gh_manager__ ();
2237 
2238  graphics_object fobj = gh_mgr.get_object (h);
2239 
2240  if (fobj && fobj.isa ("figure"))
2241  {
2242  figure::properties& fp
2243  = dynamic_cast<figure::properties&> (fobj.get_properties ());
2244  return figprops2idx (fp);
2245  }
2246 
2247  error ("figure_manager: H (= %g) is not a figure", h);
2248  }
2249 
2250  static int hnd2idx (const graphics_handle& fh)
2251  {
2252  return hnd2idx (fh.value ());
2253  }
2254 };
2255 
2256 figure_manager *figure_manager::instance = nullptr;
2257 
2258 std::string figure_manager::fltk_idx_header="fltk index=";
2259 int figure_manager::curr_index = 1;
2260 
2261 static bool toolkit_loaded = false;
2262 
2263 class fltk_graphics_toolkit : public octave::base_graphics_toolkit
2264 {
2265 public:
2266 
2267  fltk_graphics_toolkit (octave::interpreter& interp)
2268  : octave::base_graphics_toolkit (FLTK_GRAPHICS_TOOLKIT_NAME),
2269  m_interpreter (interp), input_event_hook_fcn_id ()
2270  {
2271  Fl::visual (FL_RGB);
2272  }
2273 
2274  ~fltk_graphics_toolkit (void) = default;
2275 
2276  bool is_valid (void) const { return true; }
2277 
2278  bool initialize (const graphics_object& go)
2279  {
2280  if (go.isa ("figure")
2281  || go.isa ("uimenu"))
2282  {
2283  if (go.isa ("uimenu"))
2284  update (go, uimenu::properties::ID_LABEL);
2285 
2286  return true;
2287  }
2288 
2289  return false;
2290  }
2291 
2292  void finalize (const graphics_object& go)
2293  {
2294  if (go.isa ("figure"))
2295  {
2296  octave_value ov = go.get (caseless_str ("__plot_stream__"));
2297 
2298  if (! ov.isempty ())
2299  figure_manager::delete_window (ov.string_value ());
2300  }
2301  }
2302 
2303  void uimenu_set___fltk_label__ (graphics_object uimenu_obj)
2304  {
2305  if (uimenu_obj.valid_object ())
2306  {
2307  uimenu::properties& uimenup
2308  = dynamic_cast<uimenu::properties&> (uimenu_obj.get_properties ());
2309  std::string fltk_label = uimenup.get_label ();
2310 
2311  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
2312 
2313  graphics_object go = gh_mgr.get_object (uimenu_obj.get_parent ());
2314 
2315  if (go.isa ("uimenu"))
2316  fltk_label = dynamic_cast<const uimenu::properties&>
2317  (go.get_properties ()).get___fltk_label__ ()
2318  + '/'
2319  + fltk_label;
2320  else if (go.isa ("figure") || go.isa ("uicontextmenu"))
2321  ;
2322  else
2323  error ("invalid parent object\n");
2324 
2325  uimenup.set___fltk_label__ (fltk_label);
2326  }
2327  }
2328 
2329  void update (const graphics_object& go, int id)
2330  {
2331  if (go.isa ("figure"))
2332  {
2333  octave_value ov = go.get (caseless_str ("__plot_stream__"));
2334 
2335  if (! ov.isempty ())
2336  {
2337  const figure::properties& fp
2338  = dynamic_cast<const figure::properties&> (go.get_properties ());
2339 
2340  switch (id)
2341  {
2342  case base_properties::ID_VISIBLE:
2343  figure_manager::toggle_window_visibility (ov.string_value (),
2344  fp.is_visible ());
2345  break;
2346 
2347  case figure::properties::ID_MENUBAR:
2348  figure_manager::toggle_menubar_visibility
2349  (ov.string_value (), fp.menubar_is ("figure"));
2350  break;
2351 
2352  case figure::properties::ID_CURRENTAXES:
2353  figure_manager::update_canvas (go.get_handle (),
2354  fp.get_currentaxes ());
2355  break;
2356 
2357  case figure::properties::ID_NAME:
2358  case figure::properties::ID_NUMBERTITLE:
2359  figure_manager::set_name (ov.string_value ());
2360  break;
2361 
2362  case figure::properties::ID_INTEGERHANDLE:
2363  {
2364  std::string tmp = ov.string_value ();
2365  graphics_handle gh = fp.get___myhandle__ ();
2366  figure_manager::renumber_figure (tmp, gh.value ());
2367  figure_manager::set_name (tmp);
2368  }
2369  break;
2370 
2371  case figure::properties::ID_POSITION:
2372  figure_manager::update_boundingbox (ov.string_value (), true);
2373  break;
2374 
2375  case figure::properties::ID_OUTERPOSITION:
2376  figure_manager::update_boundingbox (ov.string_value (), false);
2377  break;
2378  }
2379  }
2380  }
2381  else if (go.isa ("uimenu"))
2382  {
2383  if (id == uimenu::properties::ID_LABEL)
2384  uimenu_set___fltk_label__ (go);
2385 
2386  graphics_object fig = go.get_ancestor ("figure");
2387  figure_manager::uimenu_update (fig.get_handle (), go.get_handle (), id);
2388  }
2389  }
2390 
2391  void redraw_figure (const graphics_object& go) const
2392  {
2393  // We scan all figures and add those which use FLTK.
2394 
2395  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
2396 
2397  graphics_object obj = gh_mgr.get_object (0);
2398 
2399  if (obj && obj.isa ("root"))
2400  {
2401  base_properties& props = obj.get_properties ();
2402  Matrix children = props.get_all_children ();
2403 
2404  for (octave_idx_type n = 0; n < children.numel (); n++)
2405  {
2406  graphics_object fobj = gh_mgr.get_object (children (n));
2407 
2408  if (fobj && fobj.isa ("figure"))
2409  {
2410  figure::properties& fp
2411  = dynamic_cast<figure::properties&> (fobj.get_properties ());
2412 
2413  if (fp.get___graphics_toolkit__ ()
2414  == FLTK_GRAPHICS_TOOLKIT_NAME)
2415  figure_manager::new_window (fp);
2416  }
2417  }
2418  }
2419 
2420  figure_manager::mark_modified (go.get_handle ());
2421  Fl::check ();
2422  }
2423 
2424  void print_figure (const graphics_object& go,
2425  const std::string& term,
2426  const std::string& file_cmd,
2427  const std::string& /*debug_file*/) const
2428  {
2429  figure_manager::print (go.get_handle (), file_cmd, term);
2430  }
2431 
2432  uint8NDArray get_pixels (const graphics_object& go) const
2433  {
2434  return figure_manager::get_pixels (go.get_handle ());
2435  }
2436 
2437  Matrix get_canvas_size (const graphics_handle& fh) const
2438  {
2439  return figure_manager::get_size (fh);
2440  }
2441 
2442  /*
2443  double get_screen_resolution (void) const
2444  {
2445  // FLTK doesn't give this info.
2446  return 72.0;
2447 
2448  // FIXME: FLTK >= 1.3.0 could do this with Fl::screen_dpi (h, v, n)
2449  // but do we need it?
2450  }
2451  */
2452 
2453  Matrix get_screen_size (void) const
2454  {
2455  Matrix sz (1, 2, 0.0);
2456  sz(0) = Fl::w ();
2457  sz(1) = Fl::h ();
2458  return sz;
2459  }
2460 
2461  void close (void)
2462  {
2463  if (toolkit_loaded)
2464  {
2465  m_interpreter.munlock ("__init_fltk__");
2466 
2467  octave_value_list args = input_event_hook_fcn_id;
2468  args.append (false);
2469  Fremove_input_event_hook (m_interpreter, args, 0);
2470  input_event_hook_fcn_id = octave_value_list ();
2471 
2472  figure_manager::close_all ();
2473  }
2474  }
2475 
2476  void set_input_event_hook_id (const octave_value_list& id)
2477  {
2478  input_event_hook_fcn_id = id;
2479  }
2480 
2481 private:
2482 
2483  octave::interpreter& m_interpreter;
2484 
2485  octave_value_list input_event_hook_fcn_id;
2486 };
2487 
2488 #endif
2489 
2490 DEFMETHOD_DLD (__fltk_check__, interp, , ,
2491  doc: /* -*- texinfo -*-
2492 @deftypefn {} {} __fltk_check__ ()
2493 Undocumented internal function. Calls Fl::check ()
2494 @end deftypefn */)
2495 {
2496 #if defined (HAVE_FLTK)
2497  Fl::check ();
2498 
2499  if (Vdrawnow_requested)
2500  Fdrawnow (interp);
2501 
2502  return octave_value_list ();
2503 #else
2504  octave_unused_parameter (interp);
2505 
2506  err_disabled_feature ("__fltk_check__", "OpenGL and FLTK");
2507 #endif
2508 }
2509 
2510 // Initialize the fltk graphics toolkit.
2511 
2512 DEFMETHOD_DLD (__init_fltk__, interp, , ,
2513  doc: /* -*- texinfo -*-
2514 @deftypefn {} {} __init_fltk__ ()
2515 Undocumented internal function.
2516 @end deftypefn */)
2517 {
2518 #if defined (HAVE_FLTK)
2519  octave::display_info& dpy_info = interp.get_display_info ();
2520 
2521  if (! dpy_info.display_available ())
2522  error ("__init_fltk__: no graphics DISPLAY available");
2523  else if (! toolkit_loaded)
2524  {
2525  interp.mlock ();
2526 
2527  octave::gtk_manager& gtk_mgr = interp.get_gtk_manager ();
2528 
2529  fltk_graphics_toolkit *fltk = new fltk_graphics_toolkit (interp);
2530  octave::graphics_toolkit tk (fltk);
2531  gtk_mgr.load_toolkit (tk);
2532  toolkit_loaded = true;
2533 
2535  octave_value fcn_handle (new octave_fcn_handle (fcn));
2536 
2537  octave_value_list id = Fadd_input_event_hook (interp, fcn_handle, 1);
2538 
2539  fltk->set_input_event_hook_id (id);
2540  }
2541 
2542  return octave_value_list ();
2543 
2544 #else
2545  octave_unused_parameter (interp);
2546 
2547  err_disabled_feature ("__init_fltk__", "OpenGL and FLTK");
2548 #endif
2549 }
2550 
2551 /*
2552 ## No test needed for internal helper function.
2553 %!assert (1)
2554 */
2555 
OCTAVE_END_NAMESPACE(octave)
static std::string pan_mode(const graphics_object figObj)
Definition: Canvas.cc:303
static bool pan_enabled(const graphics_object figObj)
Definition: Canvas.cc:291
#define NaN
Definition: Faddeeva.cc:261
OCTAVE_EXPORT octave_value_list F__fltk_check__(octave::interpreter &, const octave_value_list &, int)
OCTARRAY_API void clear(void)
Definition: Array-base.cc:109
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:414
OCTARRAY_API Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array-base.cc:2081
OCTARRAY_API Array< octave_idx_type > find(octave_idx_type n=-1, bool backward=false) const
Find indices of (at most n) nonzero elements.
Definition: Array-base.cc:2240
Definition: Cell.h:43
Definition: dMatrix.h:42
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:158
double value(void) const
Definition: oct-handle.h:78
bool ok(void) const
Definition: oct-handle.h:113
const octave_value & contents(const_iterator p) const
Definition: oct-map.h:205
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:238
octave_value_list & append(const octave_value &val)
Definition: ovl.cc:98
OCTINTERP_API octave_scalar_map scalar_map_value(void) const
bool is_string(void) const
Definition: ov.h:682
std::string string_value(bool force=false) const
Definition: ov.h:1019
bool isempty(void) const
Definition: ov.h:646
OCTINTERP_API octave_idx_type length(void) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFMETHOD_DLD(name, interp_name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin method.
Definition: defun-dld.h:99
void error(const char *fmt,...)
Definition: error.cc:979
#define panic_impossible()
Definition: error.h:508
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
Definition: errwarn.cc:53
void gl2ps_print(opengl_functions &glfcns, const graphics_object &fig, const std::string &stream, const std::string &term)
OCTAVE_EXPORT octave_value_list Fdrawnow(octave::interpreter &interp, const octave_value_list &args, int)
Definition: graphics.cc:13932
bool Vdrawnow_requested
Definition: input.cc:91
gh_manager & __get_gh_manager__(void)
T mod(T x, T y)
Definition: lo-mappers.h:294
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:130
class OCTAVE_API Matrix
Definition: mx-fwd.h:31
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
T * r
Definition: mx-inlines.cc:773
std::complex< double > w(std::complex< double > z, double relerr=0)
T::properties & properties(graphics_object obj)
octave_value_list feval(const char *name, const octave_value_list &args, int nargout)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
Definition: oct-parse.cc:10370
@ DESCENDING
Definition: oct-sort.h:97
static octave_idx_type get_size(double d, const std::string &who)
Definition: oct-stream.cc:129
static void initialize(void)
void draw(QDomElement &parent_elt, pdfpainter &painter)
static T abs(T x)
Definition: pr-output.cc:1678
F77_RET_T len
Definition: xerbla.cc:61