37#if defined (HAVE_CONFIG_H)
45#if defined (HAVE_X_WINDOWS)
58# define WIN32_LEAN_AND_MEAN
61#if defined (HAVE_FLTK)
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>
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>
92#include "builtin-defun-decls.h"
108OCTAVE_NAMESPACE_BEGIN
110#if defined (HAVE_FLTK)
112#define FLTK_GRAPHICS_TOOLKIT_NAME "fltk"
114const char *help_text =
"\
124right drag - rectangle zoom\n\
125left double click - autoscale\n\
128class OpenGL_fltk :
public Fl_Gl_Window
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 ()
136#if defined (HAVE_OPENGL)
139 mode (FL_DEPTH | FL_DOUBLE | FL_MULTISAMPLE);
148 ~OpenGL_fltk (
void) =
default;
157 bool zoom (
void) {
return m_in_zoom; }
158 void set_zoom_box (
const Matrix& zb) { m_zoom_box = zb; }
160 void print (
const std::string& cmd,
const std::string& term)
171 m_renderer.draw (gh_mgr.get_object (m_number));
173 return m_renderer.get_pixels (
w (), h ());
176 void resize (
int xx,
int yy,
int ww,
int hh)
178#if defined (HAVE_OPENGL)
180 Fl_Gl_Window::resize (xx, yy, ww, hh);
184 octave_unused_parameter (xx);
185 octave_unused_parameter (yy);
186 octave_unused_parameter (ww);
187 octave_unused_parameter (hh);
197 bool renumber (
double new_number)
201 if (m_number != new_number)
203 m_number = new_number;
224#if defined (HAVE_OPENGL)
235 m_renderer.
draw (gh_mgr.get_object (m_number));
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;
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);
268 int handle (
int event)
270#if defined (HAVE_OPENGL)
275 cursor (FL_CURSOR_CROSS);
279 cursor (FL_CURSOR_DEFAULT);
283 return Fl_Gl_Window::handle (event);
287 octave_unused_parameter (event);
298static void script_cb (Fl_Widget *,
void *data)
307 fltk_uimenu (
int xx,
int yy,
int ww,
int hh)
308 : m_menubar (new Fl_Menu_Bar (xx, yy, ww, hh))
311 int items_to_show (
void)
314 int len = m_menubar->size ();
316 for (
int t = 0; t <
len; t++)
318 const Fl_Menu_Item *m
319 =
static_cast<const Fl_Menu_Item *
> (&(m_menubar->menu ()[t]));
321 if (m->label () && m->visible ())
331 m_menubar->redraw ();
337 m_menubar->redraw ();
340 bool is_visible (
void)
342 return m_menubar->visible ();
345 int find_index_by_name (
const std::string& findname)
353 std::string menupath;
354 for (
int t = 0; t < m_menubar->size (); t++)
356 Fl_Menu_Item *m =
const_cast<Fl_Menu_Item *
> (&(m_menubar->menu ()[t]));
360 if (! menupath.empty ())
362 menupath += m->label ();
364 if (menupath == findname)
372 std::size_t idx = menupath.find_last_of (
'/');
373 if (idx != std::string::npos)
374 menupath.erase (idx);
380 std::string itempath = menupath;
381 if (! itempath.empty ())
383 itempath += m->label ();
385 if (itempath == findname)
394 Matrix uimenu_childs = uimenup.get_all_children ();
395 Matrix retval = do_find_uimenu_children (uimenu_childs);
401 Matrix uimenu_childs = figp.get_all_children ();
402 Matrix retval = do_find_uimenu_children (uimenu_childs);
406 Matrix do_find_uimenu_children (
Matrix uimenu_childs)
const
417 graphics_object kidgo = gh_mgr.get_object (uimenu_childs (ii));
419 if (kidgo.valid_object () && kidgo.isa (
"uimenu"))
421 uimenu_childs(k) = uimenu_childs(ii);
422 pos(k++) =
dynamic_cast<uimenu::properties&
> (kidgo.get_properties ()).get_position ();
426 uimenu_childs.
resize (k, 1);
433 retval(ii) = uimenu_childs (sidx(ii));
440 std::string fltk_label = uimenup.get___fltk_label__ ();
441 int idx = find_index_by_name (fltk_label.c_str ());
444 m_menubar->remove (idx);
449 std::string fltk_label = uimenup.get___fltk_label__ ();
450 if (! fltk_label.empty ())
452 Fl_Menu_Item *item =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
456 std::string acc = uimenup.get_accelerator ();
457 if (acc.length () > 0)
459 int key = FL_CTRL + acc[0];
460 item->shortcut (key);
468 std::string fltk_label = uimenup.get___fltk_label__ ();
469 if (! fltk_label.empty ())
472 =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
475 if (! uimenup.get_callback ().isempty ())
476 item->callback (
static_cast<Fl_Callback *
> (script_cb),
477 static_cast<void *
> (&uimenup));
479 item->callback (
nullptr,
static_cast<void *
> (
nullptr));
486 std::string fltk_label = uimenup.get___fltk_label__ ();
487 if (! fltk_label.empty ())
490 =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
493 if (uimenup.is_enable ())
503 std::string fltk_label = uimenup.get___fltk_label__ ();
504 if (! fltk_label.empty ())
507 =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
510 Matrix rgb = uimenup.get_foregroundcolor_rgb ();
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));
516 item->labelcolor (fl_rgb_color (r, g, b));
526 std::string fltk_label = uimenup.get___fltk_label__ ();
527 if (! fltk_label.empty ())
529 int itemflags = 0, idx;
530 int curr_idx = find_index_by_name (fltk_label.c_str ());
532 for (idx = curr_idx - 1; idx >= 0; idx--)
535 =
const_cast<Fl_Menu_Item *
> (&m_menubar->menu () [idx]);
536 itemflags = item->flags;
541 if (idx >= 0 && idx < m_menubar->size ())
543 if (uimenup.is_separator ())
545 if (! (itemflags & FL_SUBMENU))
546 m_menubar->mode (idx, itemflags | FL_MENU_DIVIDER);
549 m_menubar->mode (idx, itemflags & (~FL_MENU_DIVIDER));
556 std::string fltk_label = uimenup.get___fltk_label__ ();
557 if (! fltk_label.empty ())
560 =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
563 if (uimenup.is_visible ())
573 uimenup.get_property (
"position").set (
octave_value (
static_cast<double> (pos)),
580 std::string fltk_label = uimenup.get___fltk_label__ ();
582 if (! fltk_label.empty ())
584 bool item_added =
false;
587 const Fl_Menu_Item *item
588 = m_menubar->find_item (fltk_label.c_str ());
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;
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)
605 std::ostringstream valstream;
607 fltk_label +=
'(' + valstream.str () +
')';
611 Matrix uimenu_ch = find_uimenu_children (uimenup);
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);
623 while (! item_added);
624 uimenup.set___fltk_label__ (fltk_label);
630 std::vector<int> delayed_menus;
631 Matrix kids = find_uimenu_children (uimenup);
633 std::string fltk_label = uimenup.get___fltk_label__ ();
637 update_foregroundcolor (uimenup);
638 update_callback (uimenup);
639 update_accelerator (uimenup);
640 update_enable (uimenup);
641 update_visible (uimenup);
642 update_seperator (uimenup);
648 graphics_object kgo = gh_mgr.get_object (kids (
len - (ii + 1)));
650 if (kgo.valid_object ())
653 (kgo.get_properties ());
656 int pos = kprop.get_position ();
658 delayed_menus.push_back ((
len - (ii + 1)));
667 for (std::size_t ii = 0; ii < delayed_menus.size (); ii++)
669 graphics_object kgo = gh_mgr.get_object (kids (delayed_menus[ii]));
671 if (kgo.valid_object ())
674 (kgo.get_properties ());
676 update_position (kprop, ++count);
683 std::vector<int> delayed_menus;
684 Matrix kids = find_uimenu_children (figp);
694 graphics_object kgo = gh_mgr.get_object (kids (
len - (ii + 1)));
696 if (kgo.valid_object ())
699 (kgo.get_properties ());
702 int pos = kprop.get_position ();
704 delayed_menus.push_back ((
len - (ii + 1)));
708 update_position (kprop, ++count);
714 for (std::size_t ii = 0; ii < delayed_menus.size (); ii++)
716 graphics_object kgo = gh_mgr.get_object (kids (delayed_menus[ii]));
718 if (kgo.valid_object ())
721 (kgo.get_properties ());
723 update_position (kprop, ++count);
728 template <
typename T_prop>
729 void remove_from_menu (T_prop& prop)
732 std::string type = prop.get_type ();
733 kids = find_uimenu_children (prop);
740 graphics_object kgo = gh_mgr.get_object (kids (
len - (ii + 1)));
742 if (kgo.valid_object ())
745 (kgo.get_properties ());
746 remove_from_menu (kprop);
750 if (type ==
"uimenu")
752 else if (type ==
"figure")
758 fltk_uimenu (
const fltk_uimenu&) =
delete;
760 fltk_uimenu operator = (
const fltk_uimenu&) =
delete;
769 Fl_Menu_Bar *m_menubar;
772#if defined (HAVE_X_WINDOWS)
774xerror_handler (Display *, XErrorEvent *)
780class plot_window :
public Fl_Window
782 friend class fltk_uimenu;
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)
794 callback (window_close,
static_cast<void *
> (
this));
798 m_resize_dummy =
new Fl_Box (5 * m_status_h, m_menu_h,
799 ww - 5 * m_status_h, hh);
803 resizable (m_resize_dummy);
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 ());
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,
"");
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);
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");
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");
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");
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");
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");
853 m_uimenu->add_to_menu (m_fp);
854 if (m_fp.menubar_is (
"none") || ! m_uimenu->items_to_show ())
857 update_boundingbox (internal);
859 if (m_fp.is_visible ())
871#if defined (HAVE_X_WINDOWS)
872 std::string show_gui_msgs
876 if (show_gui_msgs.empty ())
877 XSetErrorHandler (xerror_handler);
880 if (m_fp.get_currentaxes ().ok ())
889 plot_window (
const plot_window&) =
delete;
891 plot_window& operator = (
const plot_window&) =
delete;
903 double number (
void) {
return m_fp.get___myhandle__ ().value (); }
905 void renumber (
double new_number)
908 error (
"unable to renumber figure");
910 if (m_canvas->renumber (new_number))
914 void print (
const std::string& cmd,
const std::string& term)
916 m_canvas->print (cmd, term);
921 return m_canvas->get_pixels ();
924 void show_menubar (
void)
927 update_toolbar_position ();
930 void hide_menubar (
void)
933 update_toolbar_position ();
940 graphics_object uimenu_obj = gh_mgr.get_object (gh);
942 if (uimenu_obj.valid_object () && uimenu_obj.isa (
"uimenu"))
946 std::string fltk_label = uimenup.get___fltk_label__ ();
947 graphics_object fig = uimenu_obj.get_ancestor (
"figure");
953 case base_properties::ID_BEINGDELETED:
954 m_uimenu->remove_from_menu (uimenup);
957 case base_properties::ID_VISIBLE:
958 m_uimenu->update_visible (uimenup);
961 case uimenu::properties::ID_ACCELERATOR:
962 m_uimenu->update_accelerator (uimenup);
965 case uimenu::properties::ID_CALLBACK:
966 m_uimenu->update_callback (uimenup);
969 case uimenu::properties::ID_CHECKED:
970 m_uimenu->add_to_menu (figp);
973 case uimenu::properties::ID_ENABLE:
974 m_uimenu->update_enable (uimenup);
977 case uimenu::properties::ID_FOREGROUNDCOLOR:
978 m_uimenu->update_foregroundcolor (uimenup);
981 case uimenu::properties::ID_LABEL:
982 m_uimenu->add_to_menu (figp);
985 case uimenu::properties::ID_POSITION:
986 m_uimenu->add_to_menu (figp);
989 case uimenu::properties::ID_SEPARATOR:
990 m_uimenu->update_seperator (uimenup);
994 if (m_uimenu->items_to_show ())
1001 void show_canvas (
void)
1003 if (! m_canvas->can_do ())
1004 error (
"unable to plot due to insufficient OpenGL support");
1005 else if (m_fp.is_visible ())
1008 m_canvas->make_current ();
1012 void hide_canvas (
void)
1021 void update_toolbar_position ()
1023 int old_canvas_h = m_canvas->h ();
1026 update_boundingbox (
true);
1027 m_canvas->resize (0, menu_dy (),
w (), old_canvas_h);
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);
1044 pos(1) += menu_dy ();
1045 pos(3) -= menu_dy () + m_status_h + 2;
1052 outerpos(1) -= menu_dy ();
1053 outerpos(3) += menu_dy () + m_status_h + 2;
1062 void update_boundingbox (
bool internal)
1064 Matrix bb = m_fp.get_boundingbox (internal);
1066 bb = position2outerposition (bb);
1067 resize (bb(0), bb(1), bb(2), bb(3));
1070 void mark_modified (
void)
1072 m_canvas->redraw ();
1075 void set_name (
void)
1077 m_window_label = m_fp.get_title ();
1078 label (m_window_label.c_str ());
1085 std::string m_window_label;
1091 static const int m_status_h = 20;
1094 static const int m_menu_h = 25;
1096 fltk_uimenu *m_uimenu;
1098 OpenGL_fltk *m_canvas;
1100 Fl_Button *m_autoscale;
1101 Fl_Button *m_togglegrid;
1102 Fl_Button *m_panzoom;
1103 Fl_Button *m_rotate;
1105 Fl_Output *m_status;
1107 Fl_Box *m_resize_dummy;
1109 graphics_object m_ax_obj;
1115 static void window_close (Fl_Widget *,
void *data)
1118 args(0) =
static_cast<plot_window *
> (data)->number ();
1123 static void button_callback (Fl_Widget *ww,
void *data)
1125 static_cast<plot_window *
> (data)->button_press (ww, data);
1128 void button_press (Fl_Widget *widg,
void *)
1130 if (widg == m_autoscale)
1132 else if (widg == m_togglegrid)
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);
1142 void set_on_ax_obj (
const std::string&
name,
const std::string& value)
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")
1151 ap.set (
name, value);
1161 graphics_object go = gh_mgr.get_object (gh);
1166 ap.set (
name, value);
1171 void axis_auto (
void)
1174 if (m_fp.get_currentaxes ().ok ())
1176 args(0) = m_fp.get_currentaxes ().as_octave_value ();
1183 void toggle_grid (
void)
1186 if (m_fp.get_currentaxes ().ok ())
1187 args(0) = m_fp.get_currentaxes ().as_octave_value ();
1193 void pixel2pos (
const graphics_handle& ax,
int px,
int py,
double& xx,
1198 pixel2pos (gh_mgr.get_object (ax), px, py, xx, yy);
1201 void pixel2pos (graphics_object ax,
int px,
int py,
double& xx,
1204 if (ax && ax.isa (
"axes"))
1216 Matrix kids = m_fp.get_children ();
1221 for (
int k = 0; k <
len; k++)
1227 graphics_object kid = gh_mgr.get_object (hnd);
1229 if (kid.valid_object () && kid.isa (
"axes"))
1231 Matrix bb = kid.get_properties ().get_boundingbox (
false);
1233 if (bb(0) <= px && px < (bb(0)+bb(2))
1234 && bb(1) <= py && py < (bb(1)+bb(3)))
1241 return m_fp.get_currentaxes ();
1245 int px1 = -1,
int py1 = -1)
1249 pixel2status (gh_mgr.get_object (ax), px0, py0, px1, py1);
1252 void pixel2status (graphics_object ax,
int px0,
int py0,
1253 int px1 = -1,
int py1 = -1)
1255 double x0, y0, x1, y1;
1257 std::stringstream cbuf;
1260 pixel2pos (ax, px0, py0, x0, y0);
1261 cbuf <<
'[' << x0 <<
", " << y0 <<
']';
1264 pixel2pos (ax, px1, py1, x1, y1);
1265 cbuf <<
" -> ["<< x1 <<
", " << y1 <<
']';
1268 m_status->value (cbuf.str ().c_str ());
1271 void view2status (graphics_object ax)
1273 if (ax && ax.isa (
"axes"))
1277 std::stringstream cbuf;
1281 v = ap.get (
"view").matrix_value ();
1282 cbuf <<
"[azimuth: " << v(0) <<
", elevation: " << v(1) <<
']';
1284 m_status->value (cbuf.str ().c_str ());
1288 void set_currentpoint (
int px,
int py)
1290 if (! m_fp.is_beingdeleted ())
1292 Matrix pos = m_fp.map_from_boundingbox (px, py);
1293 m_fp.set_currentpoint (pos);
1298 graphics_object robj = gh_mgr.get_object (m_fp.get_parent ());
1303 rp.set_currentfigure (m_fp.get___myhandle__ ().value ());
1307 void set_axes_currentpoint (graphics_object ax,
int px,
int py)
1309 if (ax.valid_object () && ax.isa (
"axes"))
1314 Matrix x_zlim = ap.get_transform_zlim ();
1318 ColumnVector tmp = ap.get_transform ().untransform (px, py, x_zlim(0));
1324 tmp = ap.get_transform ().untransform (px, py, x_zlim(1));
1329 ap.set_currentpoint (pos);
1330 if (ap.get_tag () !=
"legend" && ap.get_tag () !=
"colorbar")
1331 m_fp.set_currentaxes (ap.get___myhandle__ ().value ());
1337 if (m_uimenu->is_visible ())
1350 std::string key_str;
1351 std::ostringstream tmp_str;
1353 if (e_key == FL_Escape)
1355 else if (e_key == FL_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)
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)
1367 else if (e_key == 32)
1369 else if (e_key == FL_Enter)
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)
1377 else if (e_key == FL_Home)
1379 else if (e_key == FL_End)
1381 else if (e_key == FL_Insert)
1383 else if (e_key == FL_Page_Up)
1385 else if (e_key == FL_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)
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)
1407 else if (e_key == 0xff8d)
1409 else if (e_key == 0xffac)
1410 key_str =
"separator";
1411 else if (e_key >= 0xffb0 && e_key <= 0xffb9)
1413 tmp_str <<
"numpad" << (e_key - 0xffb0);
1414 key_str = tmp_str.str ();
1416 else if (e_key >= (FL_F + 1) && e_key <= (FL_F + 12))
1418 tmp_str <<
'f' << (e_key - FL_F);
1419 key_str = tmp_str.str ();
1421 else if (e_key ==
',')
1423 else if (e_key ==
'.')
1425 else if (e_key ==
'-')
1427 else if (e_key ==
'^' || e_key ==
'+' || e_key ==
'#'
1428 || e_key ==
'<' || e_key == 0xfe03 )
1430 else if (isalnum (e_key))
1431 key_str = std::tolower (e_key);
1432 else if (isprint (e_text[0]))
1439 Cell modifier2cell (
int e_state)
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"));
1454 void resize (
int xx,
int yy,
int ww,
int hh)
1456 Fl_Window::resize (xx, yy, ww, hh);
1465 m_fp.set_boundingbox (bb,
false,
false);
1468 m_fp.set_boundingbox (outerposition2position (bb),
true,
false);
1491 bool rotate_enabled (
void)
1501 int handle (
int event)
1503 if (event == FL_FOCUS)
1508 if (! m_fp.is_beingdeleted ())
1513 static bool key_resent_detected =
false;
1522 static int last_event_key = 0;
1523 static char last_event_text = 0;
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))));
1532 last_event_key = e_key;
1533 last_event_text = e_text;
1539 int e_key = Fl::event_key ();
1540 const char *e_text = Fl::event_text ();
1541 int e_state = Fl::event_state ();
1544 m_fp.set_currentcharacter (std::string (e_text));
1546 if (! m_fp.get_keypressfcn ().isempty ()
1550 if (Fl::event_inside (m_canvas))
1552 m_pos_x = Fl::event_x ();
1553 m_pos_y = Fl::event_y () - menu_dy ();
1555 set_currentpoint (m_pos_x, m_pos_y);
1557 gh = pixel2axes_or_ca (m_pos_x, m_pos_y);
1561 m_ax_obj = gh_mgr.get_object (gh);
1562 set_axes_currentpoint (m_ax_obj, m_pos_x, m_pos_y);
1566 m_fp.execute_keypressfcn (evt);
1584 m_fp.set___mouse_mode__ (
"pan");
1589 m_fp.set___mouse_mode__ (
"rotate");
1597 int e_key = Fl::event_key ();
1598 int e_state = Fl::event_state ();
1600 if (key_resent_detected && Fl::event_length () == 1)
1605 tmp_e_text[0] = Fl::event_text ()[0];
1608 if (std::islower (tmp_e_text[0]))
1609 tmp_e_text[0] = std::toupper (tmp_e_text[0]);
1611 tmp_e_text[0] = std::tolower (tmp_e_text[0]);
1612 evt = format_key_event (e_key, tmp_e_text, e_state);
1616 const char *e_text = Fl::event_text ();
1617 evt = format_key_event (e_key, e_text, e_state);
1620 if (! m_fp.get_keyreleasefcn ().isempty ()
1622 m_fp.execute_keyreleasefcn (evt);
1629 if (Fl::event_inside (m_canvas))
1633 pixel2status (pixel2axes_or_ca (Fl::event_x (),
1634 Fl::event_y () - menu_dy ()),
1635 Fl::event_x (), Fl::event_y () - menu_dy ());
1639 m_pos_x = Fl::event_x ();
1640 m_pos_y = Fl::event_y () - menu_dy ();
1642 set_currentpoint (m_pos_x, m_pos_y);
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");
1655 m_fp.set_selectiontype (
"normal");
1657 gh = pixel2axes_or_ca (m_pos_x, m_pos_y);
1661 m_ax_obj = gh_mgr.get_object (gh);
1662 set_axes_currentpoint (m_ax_obj, m_pos_x, m_pos_y);
1667 if (! m_fp.get_windowbuttondownfcn ().isempty ())
1668 m_fp.execute_windowbuttondownfcn (Fl::event_button ());
1672 m_fp.set_currentobject (m_ax_obj.get_handle ().value ());
1674 base_properties& props = m_ax_obj.get_properties ();
1675 if (! props.get_buttondownfcn ().isempty ())
1676 props.execute_buttondownfcn (Fl::event_button ());
1680 else if (! m_fp.get_buttondownfcn ().isempty ())
1681 m_fp.execute_buttondownfcn (Fl::event_button ());
1686 if (! m_fp.get_windowbuttonmotionfcn ().isempty ())
1688 set_currentpoint (Fl::event_x (), Fl::event_y () - menu_dy ());
1689 m_fp.execute_windowbuttonmotionfcn ();
1692 if (Fl::event_button () == 1)
1694 if (m_ax_obj && m_ax_obj.isa (
"axes"))
1699 if (ap.get_tag () !=
"legend")
1701 if (rotate_enabled ())
1702 view2status (m_ax_obj);
1704 pixel2status (m_ax_obj, m_pos_x, m_pos_y,
1706 Fl::event_y () - menu_dy ());
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 (),
1719 ap.translate_view (mode, x0, x1, y0, y1);
1721 else if (rotate_enabled ())
1724 daz = (Fl::event_x () - m_pos_x) / pos(2) * 360;
1725 del = (Fl::event_y () - menu_dy () - m_pos_y)
1727 ap.rotate_view (del, daz);
1733 Matrix pos = ap.get_position ().matrix_value ();
1734 pos(0) +=
double (Fl::event_x () - m_pos_x)
1736 pos(1) -=
double (Fl::event_y () - menu_dy () - m_pos_y)
1738 ap.set_position (pos);
1741 m_pos_x = Fl::event_x ();
1742 m_pos_y = Fl::event_y () - menu_dy ();
1747 else if (Fl::event_button () == 3)
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);
1767 = gh_mgr.get_object (pixel2axes_or_ca (Fl::event_x (),
1770 if (ax && ax.isa (
"axes"))
1776 double wheel_zoom_speed = ap.get_mousewheelzoom ();
1779 const double factor = (Fl::event_dy () < 0
1780 ? 1 / (1.0 - wheel_zoom_speed)
1781 : 1.0 - wheel_zoom_speed);
1785 pixel2pos (ax, Fl::event_x (), Fl::event_y () - menu_dy (),
1790 ap.zoom_about_point (
"both", x1, y1, factor,
false);
1799 if (! m_fp.get_windowbuttonupfcn ().isempty ())
1801 set_currentpoint (Fl::event_x (), Fl::event_y () - menu_dy ());
1802 m_fp.execute_windowbuttonupfcn ();
1805 if ((Fl::event_button () == 1) && Fl::event_clicks ())
1808 set_on_ax_obj (
"xlimmode",
"auto");
1809 set_on_ax_obj (
"ylimmode",
"auto");
1810 set_on_ax_obj (
"zlimmode",
"auto");
1814 if (Fl::event_button () == 3)
1817 if (m_canvas->zoom ())
1819 m_canvas->zoom (
false);
1820 double x0, y0, x1, y1;
1821 if (m_ax_obj && m_ax_obj.isa (
"axes"))
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);
1831 int dx =
abs (m_pos_x - pos_x1);
1832 int dy =
abs (m_pos_y - pos_y1);
1834 if ((dx > 4) && (dy > 4))
1856 ap.zoom (
"both", xl, yl);
1867 return Fl_Window::handle (event);
1875 figure_manager (
void) =
default;
1881 figure_manager (
const figure_manager&) =
delete;
1883 figure_manager& operator = (
const figure_manager&) =
delete;
1885 ~figure_manager (
void)
1890 static bool instance_ok (
void)
1895 instance =
new figure_manager ();
1900 static void close_all (
void)
1903 instance->do_close_all ();
1909 instance->do_new_window (fp);
1912 static void delete_window (
int idx)
1915 instance->do_delete_window (idx);
1918 static void delete_window (
const std::string& idx_str)
1920 delete_window (str2idx (idx_str));
1923 static void renumber_figure (
const std::string& idx_str,
double new_number)
1926 instance->do_renumber_figure (str2idx (idx_str), new_number);
1929 static void toggle_window_visibility (
int idx,
bool is_visible)
1932 instance->do_toggle_window_visibility (idx, is_visible);
1935 static void toggle_window_visibility (
const std::string& idx_str,
1938 toggle_window_visibility (str2idx (idx_str), is_visible);
1941 static void mark_modified (
int idx)
1944 instance->do_mark_modified (idx);
1949 mark_modified (hnd2idx (gh));
1952 static void set_name (
int idx)
1955 instance->do_set_name (idx);
1958 static void set_name (
const std::string& idx_str)
1960 set_name (str2idx (idx_str));
1965 return instance_ok () ? instance->do_get_size (idx) :
Matrix ();
1974 const std::string& term)
1977 instance->do_print (hnd2idx (gh), cmd, term);
1984 retval = instance->do_get_pixels (hnd2idx (gh));
1993 instance->do_uimenu_update (hnd2idx (figh), uimenuh,
id);
2000 instance->do_update_canvas (hnd2idx (gh), ca);
2003 static void update_boundingbox (
const std::string& fig_idx_str,
2007 instance->do_update_boundingbox (str2idx (fig_idx_str), internal);
2010 static void toggle_menubar_visibility (
const std::string& fig_idx_str,
2011 bool menubar_is_figure)
2014 instance->do_toggle_menubar_visibility (str2idx (fig_idx_str),
2020 static figure_manager *instance;
2024 static int curr_index;
2026 typedef std::map<int, plot_window *> window_map;
2028 typedef window_map::iterator wm_iterator;;
2032 static std::string fltk_idx_header;
2034 void do_close_all (
void)
2037 for (win = windows.begin (); win != windows.end (); win++)
2044 int idx = figprops2idx (fp);
2046 if (idx >= 0 && windows.find (idx) == windows.end ())
2048 Matrix pos = fp.get_outerposition ().matrix_value ();
2049 bool internal =
false;
2051 if (pos(2) != -1.0 && pos(3) != -1.0)
2053 pos = fp.get_boundingbox (internal);
2059 pos = fp.get_boundingbox (internal);
2062 idx2figprops (curr_index, fp);
2064 windows[curr_index++] =
new plot_window (pos(0), pos(1), pos(2), pos(3),
2069 void do_delete_window (
int idx)
2071 wm_iterator win = windows.find (idx);
2073 if (win != windows.end ())
2076 windows.erase (win);
2080 void do_renumber_figure (
int idx,
double new_number)
2082 wm_iterator win = windows.find (idx);
2084 if (win != windows.end ())
2085 win->second->renumber (new_number);
2088 void do_toggle_window_visibility (
int idx,
bool is_visible)
2090 wm_iterator win = windows.find (idx);
2092 if (win != windows.end ())
2096 win->second->show ();
2097 win->second->show_canvas ();
2100 win->second->hide ();
2105 void do_toggle_menubar_visibility (
int fig_idx,
bool menubar_is_figure)
2107 wm_iterator win = windows.find (fig_idx);
2109 if (win != windows.end ())
2111 if (menubar_is_figure)
2112 win->second->show_menubar ();
2114 win->second->hide_menubar ();
2116 win->second->redraw ();
2120 void do_mark_modified (
int idx)
2122 wm_iterator win = windows.find (idx);
2124 if (win != windows.end ())
2126 win->second->mark_modified ();
2130 void do_set_name (
int idx)
2132 wm_iterator win = windows.find (idx);
2134 if (win != windows.end ())
2135 win->second->set_name ();
2138 Matrix do_get_size (
int idx)
2142 wm_iterator win = windows.find (idx);
2144 if (win != windows.end ())
2146 sz(0) = win->second->w ();
2147 sz(1) = win->second->h ();
2153 void do_print (
int idx,
const std::string& cmd,
const std::string& term)
2155 wm_iterator win = windows.find (idx);
2157 if (win != windows.end ())
2158 win->second->print (cmd, term);
2164 wm_iterator win = windows.
find (idx);
2166 if (win != windows.end ())
2167 retval = win->second->get_pixels ();
2174 wm_iterator win = windows.
find (idx);
2176 if (win != windows.end ())
2177 win->second->uimenu_update (gh,
id);
2182 wm_iterator win = windows.
find (idx);
2184 if (win != windows.end ())
2187 win->second->show_canvas ();
2189 win->second->hide_canvas ();
2193 void do_update_boundingbox (
int idx,
bool internal)
2195 wm_iterator win = windows.
find (idx);
2197 if (win != windows.end ())
2198 win->second->update_boundingbox (internal);
2204 if (clstr.find (fltk_idx_header, 0) == 0)
2206 std::istringstream istr (clstr.substr (fltk_idx_header.size ()));
2211 error (
"figure_manager: could not recognize fltk index");
2216 std::ostringstream ind_str;
2217 ind_str << fltk_idx_header << idx;
2218 fp.set___plot_stream__ (ind_str.str ());
2223 if (fp.get___graphics_toolkit__ () == FLTK_GRAPHICS_TOOLKIT_NAME)
2232 error (
"figure_manager: figure is not fltk");
2235 static int hnd2idx (
double h)
2239 graphics_object fobj = gh_mgr.get_object (h);
2241 if (fobj && fobj.isa (
"figure"))
2245 return figprops2idx (fp);
2248 error (
"figure_manager: H (= %g) is not a figure", h);
2253 return hnd2idx (fh.
value ());
2257figure_manager *figure_manager::instance =
nullptr;
2259std::string figure_manager::fltk_idx_header=
"fltk index=";
2260int figure_manager::curr_index = 1;
2262static bool toolkit_loaded =
false;
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 ()
2272 Fl::visual (FL_RGB);
2275 ~fltk_graphics_toolkit (
void) =
default;
2277 bool is_valid (
void)
const {
return true; }
2281 if (go.isa (
"figure")
2282 || go.isa (
"uimenu"))
2284 if (go.isa (
"uimenu"))
2285 update (go, uimenu::properties::ID_LABEL);
2293 void finalize (
const graphics_object& go)
2295 if (go.isa (
"figure"))
2304 void uimenu_set___fltk_label__ (graphics_object uimenu_obj)
2306 if (uimenu_obj.valid_object ())
2310 std::string fltk_label = uimenup.get_label ();
2312 gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
2314 graphics_object go = gh_mgr.get_object (uimenu_obj.get_parent ());
2316 if (go.isa (
"uimenu"))
2318 (go.get_properties ()).get___fltk_label__ ()
2321 else if (go.isa (
"figure") || go.isa (
"uicontextmenu"))
2324 error (
"invalid parent object\n");
2326 uimenup.set___fltk_label__ (fltk_label);
2330 void update (
const graphics_object& go,
int id)
2332 if (go.isa (
"figure"))
2343 case base_properties::ID_VISIBLE:
2344 figure_manager::toggle_window_visibility (ov.
string_value (),
2348 case figure::properties::ID_MENUBAR:
2349 figure_manager::toggle_menubar_visibility
2353 case figure::properties::ID_CURRENTAXES:
2354 figure_manager::update_canvas (go.get_handle (),
2355 fp.get_currentaxes ());
2358 case figure::properties::ID_NAME:
2359 case figure::properties::ID_NUMBERTITLE:
2363 case figure::properties::ID_INTEGERHANDLE:
2367 figure_manager::renumber_figure (tmp, gh.
value ());
2368 figure_manager::set_name (tmp);
2372 case figure::properties::ID_POSITION:
2373 figure_manager::update_boundingbox (ov.
string_value (),
true);
2376 case figure::properties::ID_OUTERPOSITION:
2377 figure_manager::update_boundingbox (ov.
string_value (),
false);
2382 else if (go.isa (
"uimenu"))
2384 if (
id == uimenu::properties::ID_LABEL)
2385 uimenu_set___fltk_label__ (go);
2387 graphics_object fig = go.get_ancestor (
"figure");
2388 figure_manager::uimenu_update (fig.get_handle (), go.get_handle (),
id);
2396 gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
2398 graphics_object obj = gh_mgr.get_object (0);
2400 if (obj && obj.isa (
"root"))
2402 base_properties& props = obj.get_properties ();
2403 Matrix children = props.get_all_children ();
2407 graphics_object fobj = gh_mgr.get_object (children (n));
2409 if (fobj && fobj.isa (
"figure"))
2414 if (fp.get___graphics_toolkit__ ()
2415 == FLTK_GRAPHICS_TOOLKIT_NAME)
2416 figure_manager::new_window (fp);
2421 figure_manager::mark_modified (go.get_handle ());
2426 const std::string& term,
2427 const std::string& file_cmd,
2428 const std::string& )
const
2430 figure_manager::print (go.get_handle (), file_cmd, term);
2435 return figure_manager::get_pixels (go.get_handle ());
2466 m_interpreter.munlock (
"__init_fltk__");
2470 Fremove_input_event_hook (m_interpreter, args, 0);
2473 figure_manager::close_all ();
2479 input_event_hook_fcn_id = id;
2484 octave::interpreter& m_interpreter;
2497#if defined (HAVE_FLTK)
2505 octave_unused_parameter (interp);
2519#if defined (HAVE_FLTK)
2520 octave::display_info& dpy_info = interp.get_display_info ();
2522 if (! dpy_info.display_available ())
2523 error (
"__init_fltk__: no graphics DISPLAY available");
2524 else if (! toolkit_loaded)
2530 fltk_graphics_toolkit *fltk =
new fltk_graphics_toolkit (interp);
2533 toolkit_loaded =
true;
2540 fltk->set_input_event_hook_id (
id);
2546 octave_unused_parameter (interp);
OCTAVE_NAMESPACE_BEGIN OCTAVE_EXPORT octave_value_list F__fltk_check__(octave::interpreter &, const octave_value_list &, int)
OCTARRAY_API void clear(void)
octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_API Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
OCTARRAY_API Array< octave_idx_type > find(octave_idx_type n=-1, bool backward=false) const
Find indices of (at most n) nonzero elements.
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
void load_toolkit(const graphics_toolkit &tk)
virtual void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
virtual void glLoadIdentity(void)
virtual void glMatrixMode(GLenum mode)
virtual void draw(const graphics_object &go, bool toplevel=true)
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)
static std::string getenv(const std::string &name)
const octave_value & contents(const_iterator p) const
void assign(const std::string &k, const octave_value &val)
octave_value_list & append(const octave_value &val)
OCTINTERP_API octave_scalar_map scalar_map_value(void) const
bool is_string(void) const
std::string string_value(bool force=false) const
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.
void error(const char *fmt,...)
#define panic_impossible()
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
OCTAVE_EXPORT octave_value_list Fdrawnow(octave::interpreter &interp, const octave_value_list &args, int)
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)
static std::string pan_mode(const graphics_object figObj)
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)
static octave_idx_type get_size(double d, const std::string &who)
octave_int< T > mod(const octave_int< T > &x, const octave_int< T > &y)
void draw(QDomElement &parent_elt, pdfpainter &painter)