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