37 #if defined (HAVE_CONFIG_H)
45 #if defined (HAVE_X_WINDOWS)
46 # include <X11/Xlib.h>
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>
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>
92 #include "builtin-defun-decls.h"
110 #if defined (HAVE_FLTK)
112 #define FLTK_GRAPHICS_TOOLKIT_NAME "fltk"
114 const char *help_text =
"\
115 Keyboard Shortcuts\n\
123 mouse wheel - zoom\n\
124 right drag - rectangle zoom\n\
125 left double click - autoscale\n\
128 class 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;
214 octave::opengl_functions m_glfcns;
215 octave::opengl_renderer m_renderer;
224 #if defined (HAVE_OPENGL)
228 m_glfcns.glMatrixMode (GL_PROJECTION);
229 m_glfcns.glLoadIdentity ();
230 m_glfcns.glViewport (0, 0,
w (), h ());
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;
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);
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);
298 static 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
416 graphics_object kidgo = gh_mgr.get_object (uimenu_childs (ii));
418 if (kidgo.valid_object () && kidgo.isa (
"uimenu"))
420 uimenu_childs(k) = uimenu_childs(ii);
421 pos(k++) =
dynamic_cast<uimenu::properties&
> (kidgo.get_properties ()).get_position ();
425 uimenu_childs.
resize (k, 1);
432 retval(ii) = uimenu_childs (sidx(ii));
439 std::string fltk_label = uimenup.get___fltk_label__ ();
440 int idx = find_index_by_name (fltk_label.c_str ());
443 m_menubar->remove (idx);
448 std::string fltk_label = uimenup.get___fltk_label__ ();
449 if (! fltk_label.empty ())
451 Fl_Menu_Item *item =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
455 std::string acc = uimenup.get_accelerator ();
456 if (acc.length () > 0)
458 int key = FL_CTRL + acc[0];
459 item->shortcut (key);
467 std::string fltk_label = uimenup.get___fltk_label__ ();
468 if (! fltk_label.empty ())
471 =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
474 if (! uimenup.get_menuselectedfcn ().isempty ())
475 item->callback (
static_cast<Fl_Callback *
> (script_cb),
476 static_cast<void *
> (&uimenup));
478 item->callback (
nullptr,
static_cast<void *
> (
nullptr));
485 std::string fltk_label = uimenup.get___fltk_label__ ();
486 if (! fltk_label.empty ())
489 =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
492 if (uimenup.is_enable ())
502 std::string fltk_label = uimenup.get___fltk_label__ ();
503 if (! fltk_label.empty ())
506 =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
509 Matrix rgb = uimenup.get_foregroundcolor_rgb ();
511 uchar
r =
static_cast<uchar
> (
std::floor (rgb (0) * 255));
512 uchar g =
static_cast<uchar
> (
std::floor (rgb (1) * 255));
513 uchar b =
static_cast<uchar
> (
std::floor (rgb (2) * 255));
515 item->labelcolor (fl_rgb_color (
r, g, b));
525 std::string fltk_label = uimenup.get___fltk_label__ ();
526 if (! fltk_label.empty ())
528 int itemflags = 0, idx;
529 int curr_idx = find_index_by_name (fltk_label.c_str ());
531 for (idx = curr_idx - 1; idx >= 0; idx--)
534 =
const_cast<Fl_Menu_Item *
> (&m_menubar->menu () [idx]);
535 itemflags = item->flags;
540 if (idx >= 0 && idx < m_menubar->size ())
542 if (uimenup.is_separator ())
544 if (! (itemflags & FL_SUBMENU))
545 m_menubar->mode (idx, itemflags | FL_MENU_DIVIDER);
548 m_menubar->mode (idx, itemflags & (~FL_MENU_DIVIDER));
555 std::string fltk_label = uimenup.get___fltk_label__ ();
556 if (! fltk_label.empty ())
559 =
const_cast<Fl_Menu_Item *
> (m_menubar->find_item (fltk_label.c_str ()));
562 if (uimenup.is_visible ())
572 uimenup.get_property (
"position").set (
octave_value (
static_cast<double> (pos)),
579 std::string fltk_label = uimenup.get___fltk_label__ ();
581 if (! fltk_label.empty ())
583 bool item_added =
false;
586 const Fl_Menu_Item *item
587 = m_menubar->find_item (fltk_label.c_str ());
592 std::size_t idx1 = fltk_label.find_last_of (
'(');
593 std::size_t idx2 = fltk_label.find_last_of (
')');
594 int len = idx2 - idx1;
598 std::string valstr = fltk_label.substr (idx1 + 1,
len - 1);
599 fltk_label.erase (idx1,
len + 1);
600 val = atoi (valstr.c_str ());
601 if (val > 0 && val < 99)
604 std::ostringstream valstream;
606 fltk_label +=
'(' + valstream.str () +
')';
610 Matrix uimenu_ch = find_uimenu_children (uimenup);
615 if (
len == 0 && uimenup.is_checked ())
616 flags += FL_MENU_TOGGLE + FL_MENU_VALUE;
617 m_menubar->add (fltk_label.c_str (),
618 0,
nullptr,
nullptr, flags);
622 while (! item_added);
623 uimenup.set___fltk_label__ (fltk_label);
629 std::vector<int> delayed_menus;
630 Matrix kids = find_uimenu_children (uimenup);
632 std::string fltk_label = uimenup.get___fltk_label__ ();
636 update_foregroundcolor (uimenup);
637 update_menuselectedfcn (uimenup);
638 update_accelerator (uimenup);
639 update_enable (uimenup);
640 update_visible (uimenup);
641 update_seperator (uimenup);
647 graphics_object kgo = gh_mgr.get_object (kids (
len - (ii + 1)));
649 if (kgo.valid_object ())
652 (kgo.get_properties ());
655 int pos = kprop.get_position ();
657 delayed_menus.push_back ((
len - (ii + 1)));
666 for (std::size_t ii = 0; ii < delayed_menus.size (); ii++)
668 graphics_object kgo = gh_mgr.get_object (kids (delayed_menus[ii]));
670 if (kgo.valid_object ())
673 (kgo.get_properties ());
675 update_position (kprop, ++count);
682 std::vector<int> delayed_menus;
683 Matrix kids = find_uimenu_children (figp);
693 graphics_object kgo = gh_mgr.get_object (kids (
len - (ii + 1)));
695 if (kgo.valid_object ())
698 (kgo.get_properties ());
701 int pos = kprop.get_position ();
703 delayed_menus.push_back ((
len - (ii + 1)));
707 update_position (kprop, ++count);
713 for (std::size_t ii = 0; ii < delayed_menus.size (); ii++)
715 graphics_object kgo = gh_mgr.get_object (kids (delayed_menus[ii]));
717 if (kgo.valid_object ())
720 (kgo.get_properties ());
722 update_position (kprop, ++count);
727 template <
typename T_prop>
728 void remove_from_menu (T_prop& prop)
731 std::string type = prop.get_type ();
732 kids = find_uimenu_children (prop);
739 graphics_object kgo = gh_mgr.get_object (kids (
len - (ii + 1)));
741 if (kgo.valid_object ())
744 (kgo.get_properties ());
745 remove_from_menu (kprop);
749 if (type ==
"uimenu")
751 else if (type ==
"figure")
757 fltk_uimenu (
const fltk_uimenu&) =
delete;
759 fltk_uimenu operator = (
const fltk_uimenu&) =
delete;
768 Fl_Menu_Bar *m_menubar;
771 #if defined (HAVE_X_WINDOWS)
773 xerror_handler (Display *, XErrorEvent *)
779 class plot_window :
public Fl_Window
781 friend class fltk_uimenu;
787 : Fl_Window (xx, yy, ww, hh + m_menu_h + m_status_h + 2,
"octave"),
788 m_window_label (), m_fp (xfp), m_uimenu (nullptr), m_canvas (nullptr),
789 m_autoscale (nullptr), m_togglegrid (nullptr), m_panzoom (nullptr),
790 m_rotate (nullptr), m_help (nullptr), m_status (nullptr),
791 m_resize_dummy (nullptr), m_ax_obj (), m_pos_x (0), m_pos_y (0)
793 callback (window_close,
static_cast<void *
> (
this));
797 m_resize_dummy =
new Fl_Box (5 * m_status_h, m_menu_h,
798 ww - 5 * m_status_h, hh);
802 resizable (m_resize_dummy);
812 m_uimenu =
new fltk_uimenu (0, 0, ww, m_menu_h);
813 m_canvas =
new OpenGL_fltk (0, m_menu_h, ww, hh, number ());
819 int toolbar_y = m_menu_h + hh + 1;
820 m_status =
new Fl_Output (5 * m_status_h, toolbar_y,
821 ww - 5 * m_status_h, m_status_h,
"");
823 m_status->textcolor (FL_BLACK);
824 m_status->color (FL_GRAY);
825 m_status->textfont (FL_COURIER);
826 m_status->textsize (10);
827 m_status->box (FL_ENGRAVED_BOX);
829 m_autoscale =
new Fl_Button (0, toolbar_y, m_status_h, m_status_h,
"A");
830 m_autoscale->callback (button_callback,
static_cast<void *
> (
this));
831 m_autoscale->tooltip (
"Autoscale");
833 m_togglegrid =
new Fl_Button (m_status_h, toolbar_y, m_status_h, m_status_h,
"G");
834 m_togglegrid->callback (button_callback,
static_cast<void *
> (
this));
835 m_togglegrid->tooltip (
"Toggle Grid");
837 m_panzoom =
new Fl_Button (2* m_status_h, toolbar_y, m_status_h, m_status_h,
"P");
838 m_panzoom->callback (button_callback,
static_cast<void *
> (
this));
839 m_panzoom->tooltip (
"Mouse Pan/Zoom");
841 m_rotate =
new Fl_Button (3 * m_status_h, toolbar_y, m_status_h, m_status_h,
"R");
842 m_rotate->callback (button_callback,
static_cast<void *
> (
this));
843 m_rotate->tooltip (
"Mouse Rotate");
845 m_help =
new Fl_Button (4 * m_status_h, toolbar_y, m_status_h, m_status_h,
"?");
846 m_help->callback (button_callback,
static_cast<void *
> (
this));
847 m_help->tooltip (
"Help");
852 m_uimenu->add_to_menu (m_fp);
853 if (m_fp.menubar_is (
"none") || ! m_uimenu->items_to_show ())
856 update_boundingbox (
internal);
858 if (m_fp.is_visible ())
870 #if defined (HAVE_X_WINDOWS)
871 std::string show_gui_msgs
872 = octave::sys::env::getenv (
"OCTAVE_SHOW_GUI_MESSAGES");
875 if (show_gui_msgs.empty ())
876 XSetErrorHandler (xerror_handler);
879 if (m_fp.get_currentaxes ().ok ())
888 plot_window (
const plot_window&) =
delete;
890 plot_window& operator = (
const plot_window&) =
delete;
902 double number (
void) {
return m_fp.get___myhandle__ ().value (); }
904 void renumber (
double new_number)
907 error (
"unable to renumber figure");
909 if (m_canvas->renumber (new_number))
913 void print (
const std::string& cmd,
const std::string& term)
915 m_canvas->print (cmd, term);
920 return m_canvas->get_pixels ();
923 void show_menubar (
void)
926 update_toolbar_position ();
929 void hide_menubar (
void)
932 update_toolbar_position ();
939 graphics_object uimenu_obj = gh_mgr.get_object (gh);
941 if (uimenu_obj.valid_object () && uimenu_obj.isa (
"uimenu"))
945 std::string fltk_label = uimenup.get___fltk_label__ ();
946 graphics_object fig = uimenu_obj.get_ancestor (
"figure");
952 case base_properties::ID_BEINGDELETED:
953 m_uimenu->remove_from_menu (uimenup);
956 case base_properties::ID_VISIBLE:
957 m_uimenu->update_visible (uimenup);
960 case uimenu::properties::ID_ACCELERATOR:
961 m_uimenu->update_accelerator (uimenup);
964 case uimenu::properties::ID_MENUSELECTEDFCN:
965 m_uimenu->update_menuselectedfcn (uimenup);
968 case uimenu::properties::ID_CHECKED:
969 m_uimenu->add_to_menu (figp);
972 case uimenu::properties::ID_ENABLE:
973 m_uimenu->update_enable (uimenup);
976 case uimenu::properties::ID_FOREGROUNDCOLOR:
977 m_uimenu->update_foregroundcolor (uimenup);
980 case uimenu::properties::ID_LABEL:
981 m_uimenu->add_to_menu (figp);
984 case uimenu::properties::ID_POSITION:
985 m_uimenu->add_to_menu (figp);
988 case uimenu::properties::ID_SEPARATOR:
989 m_uimenu->update_seperator (uimenup);
993 if (m_uimenu->items_to_show ())
1000 void show_canvas (
void)
1002 if (! m_canvas->can_do ())
1003 error (
"unable to plot due to insufficient OpenGL support");
1004 else if (m_fp.is_visible ())
1007 m_canvas->make_current ();
1011 void hide_canvas (
void)
1020 void update_toolbar_position ()
1022 int old_canvas_h = m_canvas->h ();
1025 update_boundingbox (
true);
1026 m_canvas->resize (0, menu_dy (),
w (), old_canvas_h);
1028 int toolbar_y = m_canvas->h () + menu_dy () + 1;
1029 m_autoscale->position (0, toolbar_y);
1030 m_togglegrid->position (m_status_h, toolbar_y);
1031 m_panzoom->position (2 * m_status_h, toolbar_y);
1032 m_rotate->position (3 * m_status_h, toolbar_y);
1033 m_help->position (4 * m_status_h, toolbar_y);
1034 m_status->resize (5 * m_status_h, toolbar_y,
1035 w () - 5 * m_status_h, m_status_h);
1043 pos(1) += menu_dy ();
1044 pos(3) -= menu_dy () + m_status_h + 2;
1051 outerpos(1) -= menu_dy ();
1052 outerpos(3) += menu_dy () + m_status_h + 2;
1061 void update_boundingbox (
bool internal)
1063 Matrix bb = m_fp.get_boundingbox (
internal);
1065 bb = position2outerposition (bb);
1066 resize (bb(0), bb(1), bb(2), bb(3));
1069 void mark_modified (
void)
1071 m_canvas->redraw ();
1074 void set_name (
void)
1076 m_window_label = m_fp.get_title ();
1077 label (m_window_label.c_str ());
1084 std::string m_window_label;
1090 static const int m_status_h = 20;
1093 static const int m_menu_h = 25;
1095 fltk_uimenu *m_uimenu;
1097 OpenGL_fltk *m_canvas;
1099 Fl_Button *m_autoscale;
1100 Fl_Button *m_togglegrid;
1101 Fl_Button *m_panzoom;
1102 Fl_Button *m_rotate;
1104 Fl_Output *m_status;
1106 Fl_Box *m_resize_dummy;
1108 graphics_object m_ax_obj;
1114 static void window_close (Fl_Widget *,
void *data)
1117 args(0) =
static_cast<plot_window *
> (data)->number ();
1122 static void button_callback (Fl_Widget *ww,
void *data)
1124 static_cast<plot_window *
> (data)->button_press (ww, data);
1127 void button_press (Fl_Widget *widg,
void *)
1129 if (widg == m_autoscale)
1131 else if (widg == m_togglegrid)
1133 else if (widg == m_panzoom)
1134 m_fp.set___mouse_mode__ (
"pan");
1135 else if (widg == m_rotate)
1136 m_fp.set___mouse_mode__ (
"rotate");
1137 else if (widg == m_help)
1138 fl_message (
"%s", help_text);
1141 void set_on_ax_obj (
const std::string& name,
const std::string& value)
1144 if (m_ax_obj && m_ax_obj.isa (
"axes")
1145 && m_ax_obj.get_properties ().get_tag () !=
"legend"
1146 && m_ax_obj.get_properties ().get_tag () !=
"colorbar")
1150 ap.set (name, value);
1159 graphics_object go = gh_mgr.get_object (gh);
1164 ap.set (name, value);
1169 void axis_auto (
void)
1172 if (m_fp.get_currentaxes ().ok ())
1174 args(0) = m_fp.get_currentaxes ().as_octave_value ();
1181 void toggle_grid (
void)
1184 if (m_fp.get_currentaxes ().ok ())
1185 args(0) = m_fp.get_currentaxes ().as_octave_value ();
1191 void pixel2pos (
const graphics_handle& ax,
int px,
int py,
double& xx,
1196 pixel2pos (gh_mgr.get_object (ax), px, py, xx, yy);
1199 void pixel2pos (graphics_object ax,
int px,
int py,
double& xx,
1202 if (ax && ax.isa (
"axes"))
1214 Matrix kids = m_fp.get_children ();
1219 for (
int k = 0; k <
len; k++)
1225 graphics_object kid = gh_mgr.get_object (hnd);
1227 if (kid.valid_object () && kid.isa (
"axes"))
1229 Matrix bb = kid.get_properties ().get_boundingbox (
false);
1231 if (bb(0) <= px && px < (bb(0)+bb(2))
1232 && bb(1) <= py && py < (bb(1)+bb(3)))
1239 return m_fp.get_currentaxes ();
1243 int px1 = -1,
int py1 = -1)
1247 pixel2status (gh_mgr.get_object (ax), px0, py0, px1, py1);
1250 void pixel2status (graphics_object ax,
int px0,
int py0,
1251 int px1 = -1,
int py1 = -1)
1253 double x0, y0, x1, y1;
1255 std::stringstream cbuf;
1258 pixel2pos (ax, px0, py0, x0, y0);
1259 cbuf <<
'[' << x0 <<
", " << y0 <<
']';
1262 pixel2pos (ax, px1, py1, x1, y1);
1263 cbuf <<
" -> ["<< x1 <<
", " << y1 <<
']';
1266 m_status->value (cbuf.str ().c_str ());
1269 void view2status (graphics_object ax)
1271 if (ax && ax.isa (
"axes"))
1275 std::stringstream cbuf;
1279 v = ap.get (
"view").matrix_value ();
1280 cbuf <<
"[azimuth: " << v(0) <<
", elevation: " << v(1) <<
']';
1282 m_status->value (cbuf.str ().c_str ());
1286 void set_currentpoint (
int px,
int py)
1288 if (! m_fp.is_beingdeleted ())
1290 Matrix pos = m_fp.map_from_boundingbox (px, py);
1291 m_fp.set_currentpoint (pos);
1295 graphics_object robj = gh_mgr.get_object (m_fp.get_parent ());
1300 rp.set_currentfigure (m_fp.get___myhandle__ ().value ());
1304 void set_axes_currentpoint (graphics_object ax,
int px,
int py)
1306 if (ax.valid_object () && ax.isa (
"axes"))
1311 Matrix x_zlim = ap.get_transform_zlim ();
1315 ColumnVector tmp = ap.get_transform ().untransform (px, py, x_zlim(0));
1321 tmp = ap.get_transform ().untransform (px, py, x_zlim(1));
1326 ap.set_currentpoint (pos);
1327 if (ap.get_tag () !=
"legend" && ap.get_tag () !=
"colorbar")
1328 m_fp.set_currentaxes (ap.get___myhandle__ ().value ());
1334 if (m_uimenu->is_visible ())
1348 std::string key_str;
1349 std::ostringstream tmp_str;
1351 if (e_key == FL_Escape)
1353 else if (e_key == FL_Tab)
1355 else if (e_key == FL_Caps_Lock)
1356 key_str =
"capslock";
1357 else if (e_key == FL_Shift_L || e_key == FL_Shift_R)
1359 else if (e_key == FL_Control_L || e_key == FL_Control_R)
1360 key_str =
"control";
1361 else if (e_key == FL_Meta_L || e_key == FL_Meta_R)
1362 key_str =
"windows";
1363 else if (e_key == FL_Alt_L || e_key == FL_Alt_R)
1365 else if (e_key == 32)
1367 else if (e_key == FL_Enter)
1369 else if (e_key == FL_BackSpace)
1370 key_str =
"backspace";
1371 else if (e_key == FL_Print)
1372 key_str =
"printscreen";
1373 else if (e_key == FL_Pause)
1375 else if (e_key == FL_Home)
1377 else if (e_key == FL_End)
1379 else if (e_key == FL_Insert)
1381 else if (e_key == FL_Page_Up)
1383 else if (e_key == FL_Delete)
1385 else if (e_key == FL_Page_Down)
1386 key_str =
"pagedown";
1387 else if (e_key == FL_Left)
1388 key_str =
"leftarrow";
1389 else if (e_key == FL_Up)
1390 key_str =
"uparrow";
1391 else if (e_key == FL_Right)
1392 key_str =
"rightarrow";
1393 else if (e_key == FL_Down)
1394 key_str =
"downarrow";
1395 else if (e_key == FL_Num_Lock)
1396 key_str =
"numlock";
1397 else if (e_key == 0xffaf)
1399 else if (e_key == 0xffaa)
1400 key_str =
"multiply";
1401 else if (e_key == 0xffad)
1402 key_str =
"subtract";
1403 else if (e_key == 0xffab)
1405 else if (e_key == 0xff8d)
1407 else if (e_key == 0xffac)
1408 key_str =
"separator";
1409 else if (e_key >= 0xffb0 && e_key <= 0xffb9)
1411 tmp_str <<
"numpad" << (e_key - 0xffb0);
1412 key_str = tmp_str.str ();
1414 else if (e_key >= (FL_F + 1) && e_key <= (FL_F + 12))
1416 tmp_str <<
'f' << (e_key - FL_F);
1417 key_str = tmp_str.str ();
1419 else if (e_key ==
',')
1421 else if (e_key ==
'.')
1423 else if (e_key ==
'-')
1425 else if (e_key ==
'^' || e_key ==
'+' || e_key ==
'#'
1426 || e_key ==
'<' || e_key == 0xfe03 )
1428 else if (isalnum (e_key))
1429 key_str = std::tolower (e_key);
1430 else if (isprint (e_text[0]))
1437 Cell modifier2cell (
int e_state)
1441 if (e_state & FL_SHIFT)
1442 mod.append (std::string (
"shift"));
1443 if (e_state & FL_CTRL)
1444 mod.append (std::string (
"control"));
1445 if (e_state & FL_ALT)
1446 mod.append (std::string (
"alt"));
1447 if (e_state & FL_COMMAND)
1448 mod.append (std::string (
"command"));
1452 void resize (
int xx,
int yy,
int ww,
int hh)
1454 Fl_Window::resize (xx, yy, ww, hh);
1463 m_fp.set_boundingbox (bb,
false,
false);
1466 m_fp.set_boundingbox (outerposition2position (bb),
true,
false);
1489 bool rotate_enabled (
void)
1499 int handle (
int event)
1501 if (event == FL_FOCUS)
1506 if (! m_fp.is_beingdeleted ())
1511 static bool key_resent_detected =
false;
1520 static int last_event_key = 0;
1521 static char last_event_text = 0;
1523 int e_key = Fl::event_key ();
1524 char e_text = Fl::event_text ()[0];
1525 key_resent_detected = (e_key == last_event_key
1526 && std::tolower (last_event_text) == std::tolower (e_text)
1527 && ((islower (last_event_text) && isupper (e_text))
1528 || (isupper (last_event_text) && islower (e_text))));
1530 last_event_key = e_key;
1531 last_event_text = e_text;
1537 int e_key = Fl::event_key ();
1538 const char *e_text = Fl::event_text ();
1539 int e_state = Fl::event_state ();
1542 m_fp.set_currentcharacter (std::string (e_text));
1544 if (! m_fp.get_keypressfcn ().isempty ()
1548 if (Fl::event_inside (m_canvas))
1550 m_pos_x = Fl::event_x ();
1551 m_pos_y = Fl::event_y () - menu_dy ();
1553 set_currentpoint (m_pos_x, m_pos_y);
1555 gh = pixel2axes_or_ca (m_pos_x, m_pos_y);
1559 m_ax_obj = gh_mgr.get_object (gh);
1560 set_axes_currentpoint (m_ax_obj, m_pos_x, m_pos_y);
1564 m_fp.execute_keypressfcn (evt);
1582 m_fp.set___mouse_mode__ (
"pan");
1587 m_fp.set___mouse_mode__ (
"rotate");
1595 int e_key = Fl::event_key ();
1596 int e_state = Fl::event_state ();
1598 if (key_resent_detected && Fl::event_length () == 1)
1603 tmp_e_text[0] = Fl::event_text ()[0];
1606 if (std::islower (tmp_e_text[0]))
1607 tmp_e_text[0] = std::toupper (tmp_e_text[0]);
1609 tmp_e_text[0] = std::tolower (tmp_e_text[0]);
1610 evt = format_key_event (e_key, tmp_e_text, e_state);
1614 const char *e_text = Fl::event_text ();
1615 evt = format_key_event (e_key, e_text, e_state);
1618 if (! m_fp.get_keyreleasefcn ().isempty ()
1620 m_fp.execute_keyreleasefcn (evt);
1627 if (Fl::event_inside (m_canvas))
1631 pixel2status (pixel2axes_or_ca (Fl::event_x (),
1632 Fl::event_y () - menu_dy ()),
1633 Fl::event_x (), Fl::event_y () - menu_dy ());
1637 m_pos_x = Fl::event_x ();
1638 m_pos_y = Fl::event_y () - menu_dy ();
1640 set_currentpoint (m_pos_x, m_pos_y);
1642 if (Fl::event_clicks ())
1643 m_fp.set_selectiontype (
"open");
1644 else if (Fl::event_button () == FL_MIDDLE_MOUSE
1645 || (Fl::event_button () == FL_LEFT_MOUSE
1646 && Fl::event_shift ()))
1647 m_fp.set_selectiontype (
"extend");
1648 else if (Fl::event_button () == FL_RIGHT_MOUSE
1649 || (Fl::event_button () == FL_LEFT_MOUSE
1650 && Fl::event_ctrl ()))
1651 m_fp.set_selectiontype (
"alt");
1653 m_fp.set_selectiontype (
"normal");
1655 gh = pixel2axes_or_ca (m_pos_x, m_pos_y);
1659 m_ax_obj = gh_mgr.get_object (gh);
1660 set_axes_currentpoint (m_ax_obj, m_pos_x, m_pos_y);
1665 if (! m_fp.get_windowbuttondownfcn ().isempty ())
1666 m_fp.execute_windowbuttondownfcn (Fl::event_button ());
1670 m_fp.set_currentobject (m_ax_obj.get_handle ().value ());
1672 base_properties& props = m_ax_obj.get_properties ();
1673 if (! props.get_buttondownfcn ().isempty ())
1674 props.execute_buttondownfcn (Fl::event_button ());
1678 else if (! m_fp.get_buttondownfcn ().isempty ())
1679 m_fp.execute_buttondownfcn (Fl::event_button ());
1684 if (! m_fp.get_windowbuttonmotionfcn ().isempty ())
1686 set_currentpoint (Fl::event_x (), Fl::event_y () - menu_dy ());
1687 m_fp.execute_windowbuttonmotionfcn ();
1690 if (Fl::event_button () == 1)
1692 if (m_ax_obj && m_ax_obj.isa (
"axes"))
1697 if (ap.get_tag () !=
"legend")
1699 if (rotate_enabled ())
1700 view2status (m_ax_obj);
1702 pixel2status (m_ax_obj, m_pos_x, m_pos_y,
1704 Fl::event_y () - menu_dy ());
1706 double x0, y0, x1, y1;
1707 Matrix pos = m_fp.get_boundingbox (
true);
1708 pixel2pos (m_ax_obj, m_pos_x, m_pos_y, x0, y0);
1709 pixel2pos (m_ax_obj, Fl::event_x (),
1710 Fl::event_y () - menu_dy (),
1717 ap.translate_view (mode, x0, x1, y0, y1);
1719 else if (rotate_enabled ())
1722 daz = (Fl::event_x () - m_pos_x) / pos(2) * 360;
1723 del = (Fl::event_y () - menu_dy () - m_pos_y)
1725 ap.rotate_view (del, daz);
1731 Matrix pos = ap.get_position ().matrix_value ();
1732 pos(0) += double (Fl::event_x () - m_pos_x)
1734 pos(1) -= double (Fl::event_y () - menu_dy () - m_pos_y)
1736 ap.set_position (pos);
1739 m_pos_x = Fl::event_x ();
1740 m_pos_y = Fl::event_y () - menu_dy ();
1745 else if (Fl::event_button () == 3)
1747 pixel2status (m_ax_obj, m_pos_x, m_pos_y,
1748 Fl::event_x (), Fl::event_y () - menu_dy ());
1749 Matrix zoom_box (1, 4, 0);
1750 zoom_box(0) = m_pos_x;
1751 zoom_box(1) = m_pos_y;
1752 zoom_box(2) = Fl::event_x ();
1753 zoom_box(3) = Fl::event_y () - menu_dy ();
1754 m_canvas->set_zoom_box (zoom_box);
1755 m_canvas->zoom (
true);
1765 = gh_mgr.get_object (pixel2axes_or_ca (Fl::event_x (),
1768 if (ax && ax.isa (
"axes"))
1774 double wheel_zoom_speed = ap.get_mousewheelzoom ();
1777 const double factor = (Fl::event_dy () < 0
1778 ? 1 / (1.0 - wheel_zoom_speed)
1779 : 1.0 - wheel_zoom_speed);
1783 pixel2pos (ax, Fl::event_x (), Fl::event_y () - menu_dy (),
1788 ap.zoom_about_point (
"both", x1, y1, factor,
false);
1797 if (! m_fp.get_windowbuttonupfcn ().isempty ())
1799 set_currentpoint (Fl::event_x (),
1800 Fl::event_y () - menu_dy ());
1801 m_fp.execute_windowbuttonupfcn ();
1804 if ((Fl::event_button () == 1) && Fl::event_clicks ())
1807 set_on_ax_obj (
"xlimmode",
"auto");
1808 set_on_ax_obj (
"ylimmode",
"auto");
1809 set_on_ax_obj (
"zlimmode",
"auto");
1813 if (Fl::event_button () == 3)
1816 if (m_canvas->zoom ())
1818 m_canvas->zoom (
false);
1819 double x0, y0, x1, y1;
1820 if (m_ax_obj && m_ax_obj.isa (
"axes"))
1823 (m_ax_obj.get_properties ());
1824 pixel2pos (m_ax_obj, m_pos_x, m_pos_y, x0, y0);
1825 int pos_x1 = Fl::event_x ();
1826 int pos_y1 = Fl::event_y () - menu_dy ();
1827 pixel2pos (m_ax_obj, pos_x1, pos_y1, x1, y1);
1830 int dx =
abs (m_pos_x - pos_x1);
1831 int dy =
abs (m_pos_y - pos_y1);
1833 if ((dx > 4) && (dy > 4))
1855 ap.zoom (
"both", xl, yl);
1866 return Fl_Window::handle (event);
1870 class figure_manager
1874 figure_manager (
void) =
default;
1880 figure_manager (
const figure_manager&) =
delete;
1882 figure_manager& operator = (
const figure_manager&) =
delete;
1884 ~figure_manager (
void)
1889 static bool instance_ok (
void)
1894 instance =
new figure_manager ();
1899 static void close_all (
void)
1902 instance->do_close_all ();
1908 instance->do_new_window (fp);
1911 static void delete_window (
int idx)
1914 instance->do_delete_window (idx);
1917 static void delete_window (
const std::string& idx_str)
1919 delete_window (str2idx (idx_str));
1922 static void renumber_figure (
const std::string& idx_str,
double new_number)
1925 instance->do_renumber_figure (str2idx (idx_str), new_number);
1928 static void toggle_window_visibility (
int idx,
bool is_visible)
1931 instance->do_toggle_window_visibility (idx, is_visible);
1934 static void toggle_window_visibility (
const std::string& idx_str,
1937 toggle_window_visibility (str2idx (idx_str), is_visible);
1940 static void mark_modified (
int idx)
1943 instance->do_mark_modified (idx);
1948 mark_modified (hnd2idx (gh));
1951 static void set_name (
int idx)
1954 instance->do_set_name (idx);
1957 static void set_name (
const std::string& idx_str)
1959 set_name (str2idx (idx_str));
1964 return instance_ok () ? instance->do_get_size (idx) :
Matrix ();
1973 const std::string& term)
1976 instance->do_print (hnd2idx (gh), cmd, term);
1983 retval = instance->do_get_pixels (hnd2idx (gh));
1992 instance->do_uimenu_update (hnd2idx (figh), uimenuh,
id);
1999 instance->do_update_canvas (hnd2idx (gh), ca);
2002 static void update_boundingbox (
const std::string& fig_idx_str,
2006 instance->do_update_boundingbox (str2idx (fig_idx_str),
internal);
2009 static void toggle_menubar_visibility (
const std::string& fig_idx_str,
2010 bool menubar_is_figure)
2013 instance->do_toggle_menubar_visibility (str2idx (fig_idx_str),
2019 static figure_manager *instance;
2023 static int curr_index;
2025 typedef std::map<int, plot_window *> window_map;
2027 typedef window_map::iterator wm_iterator;;
2031 static std::string fltk_idx_header;
2033 void do_close_all (
void)
2036 for (win = windows.begin (); win != windows.end (); win++)
2043 int idx = figprops2idx (fp);
2045 if (idx >= 0 && windows.find (idx) == windows.end ())
2047 Matrix pos = fp.get_outerposition ().matrix_value ();
2048 bool internal =
false;
2050 if (pos(2) != -1.0 && pos(3) != -1.0)
2052 pos = fp.get_boundingbox (
internal);
2058 pos = fp.get_boundingbox (
internal);
2061 idx2figprops (curr_index, fp);
2063 windows[curr_index++] =
new plot_window (pos(0), pos(1), pos(2), pos(3),
2068 void do_delete_window (
int idx)
2070 wm_iterator win = windows.find (idx);
2072 if (win != windows.end ())
2075 windows.erase (win);
2079 void do_renumber_figure (
int idx,
double new_number)
2081 wm_iterator win = windows.find (idx);
2083 if (win != windows.end ())
2084 win->second->renumber (new_number);
2087 void do_toggle_window_visibility (
int idx,
bool is_visible)
2089 wm_iterator win = windows.find (idx);
2091 if (win != windows.end ())
2095 win->second->show ();
2096 win->second->show_canvas ();
2099 win->second->hide ();
2104 void do_toggle_menubar_visibility (
int fig_idx,
bool menubar_is_figure)
2106 wm_iterator win = windows.find (fig_idx);
2108 if (win != windows.end ())
2110 if (menubar_is_figure)
2111 win->second->show_menubar ();
2113 win->second->hide_menubar ();
2115 win->second->redraw ();
2119 void do_mark_modified (
int idx)
2121 wm_iterator win = windows.find (idx);
2123 if (win != windows.end ())
2125 win->second->mark_modified ();
2129 void do_set_name (
int idx)
2131 wm_iterator win = windows.find (idx);
2133 if (win != windows.end ())
2134 win->second->set_name ();
2137 Matrix do_get_size (
int idx)
2141 wm_iterator win = windows.find (idx);
2143 if (win != windows.end ())
2145 sz(0) = win->second->w ();
2146 sz(1) = win->second->h ();
2152 void do_print (
int idx,
const std::string& cmd,
const std::string& term)
2154 wm_iterator win = windows.find (idx);
2156 if (win != windows.end ())
2157 win->second->print (cmd, term);
2163 wm_iterator win = windows.
find (idx);
2165 if (win != windows.end ())
2166 retval = win->second->get_pixels ();
2173 wm_iterator win = windows.
find (idx);
2175 if (win != windows.end ())
2176 win->second->uimenu_update (gh,
id);
2181 wm_iterator win = windows.
find (idx);
2183 if (win != windows.end ())
2186 win->second->show_canvas ();
2188 win->second->hide_canvas ();
2192 void do_update_boundingbox (
int idx,
bool internal)
2194 wm_iterator win = windows.
find (idx);
2196 if (win != windows.end ())
2197 win->second->update_boundingbox (
internal);
2203 if (clstr.find (fltk_idx_header, 0) == 0)
2205 std::istringstream istr (clstr.substr (fltk_idx_header.size ()));
2210 error (
"figure_manager: could not recognize fltk index");
2215 std::ostringstream ind_str;
2216 ind_str << fltk_idx_header << idx;
2217 fp.set___plot_stream__ (ind_str.str ());
2222 if (fp.get___graphics_toolkit__ () == FLTK_GRAPHICS_TOOLKIT_NAME)
2231 error (
"figure_manager: figure is not fltk");
2234 static int hnd2idx (
double h)
2238 graphics_object fobj = gh_mgr.get_object (h);
2240 if (fobj && fobj.isa (
"figure"))
2244 return figprops2idx (fp);
2247 error (
"figure_manager: H (= %g) is not a figure", h);
2252 return hnd2idx (fh.
value ());
2256 figure_manager *figure_manager::instance =
nullptr;
2258 std::string figure_manager::fltk_idx_header=
"fltk index=";
2259 int figure_manager::curr_index = 1;
2261 static bool toolkit_loaded =
false;
2263 class fltk_graphics_toolkit :
public octave::base_graphics_toolkit
2267 fltk_graphics_toolkit (octave::interpreter& interp)
2269 m_interpreter (interp), input_event_hook_fcn_id ()
2271 Fl::visual (FL_RGB);
2274 ~fltk_graphics_toolkit (
void) =
default;
2276 bool is_valid (
void)
const {
return true; }
2280 if (go.isa (
"figure")
2281 || go.isa (
"uimenu"))
2283 if (go.isa (
"uimenu"))
2284 update (go, uimenu::properties::ID_LABEL);
2292 void finalize (
const graphics_object& go)
2294 if (go.isa (
"figure"))
2303 void uimenu_set___fltk_label__ (graphics_object uimenu_obj)
2305 if (uimenu_obj.valid_object ())
2309 std::string fltk_label = uimenup.get_label ();
2311 gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
2313 graphics_object go = gh_mgr.get_object (uimenu_obj.get_parent ());
2315 if (go.isa (
"uimenu"))
2317 (go.get_properties ()).get___fltk_label__ ()
2320 else if (go.isa (
"figure") || go.isa (
"uicontextmenu"))
2323 error (
"invalid parent object\n");
2325 uimenup.set___fltk_label__ (fltk_label);
2329 void update (
const graphics_object& go,
int id)
2331 if (go.isa (
"figure"))
2342 case base_properties::ID_VISIBLE:
2343 figure_manager::toggle_window_visibility (ov.
string_value (),
2347 case figure::properties::ID_MENUBAR:
2348 figure_manager::toggle_menubar_visibility
2352 case figure::properties::ID_CURRENTAXES:
2353 figure_manager::update_canvas (go.get_handle (),
2354 fp.get_currentaxes ());
2357 case figure::properties::ID_NAME:
2358 case figure::properties::ID_NUMBERTITLE:
2362 case figure::properties::ID_INTEGERHANDLE:
2366 figure_manager::renumber_figure (tmp, gh.
value ());
2367 figure_manager::set_name (tmp);
2371 case figure::properties::ID_POSITION:
2372 figure_manager::update_boundingbox (ov.
string_value (),
true);
2375 case figure::properties::ID_OUTERPOSITION:
2376 figure_manager::update_boundingbox (ov.
string_value (),
false);
2381 else if (go.isa (
"uimenu"))
2383 if (
id == uimenu::properties::ID_LABEL)
2384 uimenu_set___fltk_label__ (go);
2386 graphics_object fig = go.get_ancestor (
"figure");
2387 figure_manager::uimenu_update (fig.get_handle (), go.get_handle (),
id);
2391 void redraw_figure (
const graphics_object& go)
const
2395 gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
2397 graphics_object obj = gh_mgr.get_object (0);
2399 if (obj && obj.isa (
"root"))
2401 base_properties& props = obj.get_properties ();
2402 Matrix children = props.get_all_children ();
2406 graphics_object fobj = gh_mgr.get_object (children (
n));
2408 if (fobj && fobj.isa (
"figure"))
2413 if (fp.get___graphics_toolkit__ ()
2414 == FLTK_GRAPHICS_TOOLKIT_NAME)
2415 figure_manager::new_window (fp);
2420 figure_manager::mark_modified (go.get_handle ());
2424 void print_figure (
const graphics_object& go,
2425 const std::string& term,
2426 const std::string& file_cmd,
2427 const std::string& )
const
2429 figure_manager::print (go.get_handle (), file_cmd, term);
2432 uint8NDArray get_pixels (
const graphics_object& go)
const
2434 return figure_manager::get_pixels (go.get_handle ());
2453 Matrix get_screen_size (
void)
const
2465 m_interpreter.munlock (
"__init_fltk__");
2469 Fremove_input_event_hook (m_interpreter, args, 0);
2472 figure_manager::close_all ();
2478 input_event_hook_fcn_id = id;
2483 octave::interpreter& m_interpreter;
2496 #if defined (HAVE_FLTK)
2504 octave_unused_parameter (interp);
2518 #if defined (HAVE_FLTK)
2519 octave::display_info& dpy_info = interp.get_display_info ();
2521 if (! dpy_info.display_available ())
2522 error (
"__init_fltk__: no graphics DISPLAY available");
2523 else if (! toolkit_loaded)
2527 octave::gtk_manager& gtk_mgr = interp.get_gtk_manager ();
2529 fltk_graphics_toolkit *fltk =
new fltk_graphics_toolkit (interp);
2530 octave::graphics_toolkit tk (fltk);
2531 gtk_mgr.load_toolkit (tk);
2532 toolkit_loaded =
true;
2539 fltk->set_input_event_hook_id (
id);
2545 octave_unused_parameter (interp);
static std::string pan_mode(const graphics_object figObj)
static bool pan_enabled(const graphics_object figObj)
OCTAVE_EXPORT octave_value_list F__fltk_check__(octave::interpreter &, const octave_value_list &, int)
OCTARRAY_API void clear(void)
OCTARRAY_OVERRIDABLE_FUNC_API 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)
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
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFMETHOD_DLD(name, interp_name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin method.
void error(const char *fmt,...)
#define panic_impossible()
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
void gl2ps_print(opengl_functions &glfcns, const graphics_object &fig, const std::string &stream, const std::string &term)
OCTAVE_EXPORT octave_value_list Fdrawnow(octave::interpreter &interp, const octave_value_list &args, int)
gh_manager & __get_gh_manager__(void)
std::complex< T > floor(const std::complex< T > &x)
std::complex< double > w(std::complex< double > z, double relerr=0)
T::properties & properties(graphics_object obj)
octave_value_list feval(const char *name, const octave_value_list &args, int nargout)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
static octave_idx_type get_size(double d, const std::string &who)
static void initialize(void)
void draw(QDomElement &parent_elt, pdfpainter &painter)