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