# HG changeset patch # User John W. Eaton # Date 1424287799 18000 # Node ID 3fc946d5e91fa27e97f3b6acc8a406b28e4e6d9c # Parent 56157a7505ed27a6ed4371c735d8fc1220754c13 make ginput work for all toolkits again (bug #41977) * ginput.m: Fold contents of __fltk_ginput__.m here. Dispatch to toolkit-specific ginput function if one exists. * __fltk_ginput__.m: Delete. * module.mk: Update. diff -r 56157a7505ed -r 3fc946d5e91f scripts/plot/util/ginput.m --- a/scripts/plot/util/ginput.m Sat Feb 07 21:51:20 2015 +0100 +++ b/scripts/plot/util/ginput.m Wed Feb 18 14:29:59 2015 -0500 @@ -33,27 +33,108 @@ ## @seealso{gtext, waitforbuttonpress} ## @end deftypefn -function varargout = ginput (n) +function varargout = ginput (n = -1) if (nargin > 1) print_usage (); endif - a = gca (); # Create an axis, if necessary + ## Create an axis, if necessary. + fig = gcf (); + ax = gca (); drawnow (); - toolkit = get (gcf, "__graphics_toolkit__"); + + if (isempty (ax)) + error ("ginput: must have at least one axes"); + endif + + toolkit = get (fig, "__graphics_toolkit__"); + toolkit_fcn = sprintf ("__%s_ginput__", toolkit); + + if (exist (toolkit_fcn)) + varargout = cell (1, nargout); + if (nargin == 0) + [varargout{:}] = feval (toolkit_fcn, fig); + else + [varargout{:}] = feval (toolkit_fcn, fig, n); + endif + return + endif + + x = y = button = []; + ginput_accumulator (0, 0, 0, 0); # initialize accumulator + + orig_buttondownfcn = get (fig, "buttondownfcn"); + orig_ginput_keypressfcn = get (fig, "keypressfcn"); - varargout = cell (1, nargout); - if (nargin == 0) - [varargout{:}] = feval (["__" toolkit "_ginput__"]); - else - [varargout{:}] = feval (["__" toolkit "_ginput__"], n); - endif + unwind_protect + + set (ax, "buttondownfcn", @ginput_buttondownfcn); + set (fig, "keypressfcn", @ginput_keypressfcn); + + do + if (strcmp (toolkit, "fltk")) + __fltk_check__ (); + endif + + ## Release CPU. + sleep (0.01); + + [x, y, n0, button] = ginput_accumulator (-1, 0, 0, 0); + until ((n > -1 && n0 >= n) || n0 < 0) + + if (n0 > n) + ## More clicks than requested due to double-click or too fast clicking + x = x(1:n); + y = y(1:n); + button = button(1:n); + endif + + unwind_protect_cleanup + set (ax, "buttondownfcn", orig_buttondownfcn); + set (fig, "keypressfcn", orig_ginput_keypressfcn); + end_unwind_protect + + varargout = {x, y, button}; endfunction +function [x, y, n, button] = ginput_accumulator (mode, xn, yn, btn) + persistent x y n button; + + if (mode == 0) + ## Initialize. + x = y = button = []; + n = 0; + elseif (mode == 1) + ## Append mouse button or key press. + x = [x; xn]; + y = [y; yn]; + button = [button; btn]; + n += 1; + elseif (mode == 2) + ## The end due to Enter. + n = -1; + endif + +endfunction + +function ginput_buttondownfcn (src, button) + point = get (src, "currentpoint"); + ginput_accumulator (1, point(1,1), point(1,2), button); +endfunction + +function ginput_keypressfcn (src, evt) + point = get (ax, "currentpoint"); + key = evt.Key; + if (key == "return") + ## Enter key stops ginput. + ginput_accumulator (2, NaN, NaN, NaN); + else + ginput_accumulator (1, point(1,1), point(1,2), uint8 (key(1))); + endif +endfunction ## Remove from test statistics. No real tests possible. %!test %! assert (1); - diff -r 56157a7505ed -r 3fc946d5e91f scripts/plot/util/module.mk --- a/scripts/plot/util/module.mk Sat Feb 07 21:51:20 2015 +0100 +++ b/scripts/plot/util/module.mk Wed Feb 18 14:29:59 2015 -0500 @@ -7,7 +7,6 @@ plot_util_PRIVATE_FCN_FILES = \ plot/util/private/__add_default_menu__.m \ - plot/util/private/__fltk_ginput__.m \ plot/util/private/__ghostscript__.m \ plot/util/private/__gnuplot_get_var__.m \ plot/util/private/__gnuplot_ginput__.m \ diff -r 56157a7505ed -r 3fc946d5e91f scripts/plot/util/private/__fltk_ginput__.m --- a/scripts/plot/util/private/__fltk_ginput__.m Sat Feb 07 21:51:20 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,101 +0,0 @@ -## Copyright (C) 2010-2015 Shai Ayal -## -## This file is part of Octave. -## -## Octave is free software; you can redistribute it and/or modify it -## under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 3 of the License, or (at -## your option) any later version. -## -## Octave is distributed in the hope that it will be useful, but -## WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -## General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with Octave; see the file COPYING. If not, see -## . - -## -*- texinfo -*- -## @deftypefn {Function File} {[@var{x}, @var{y}, @var{buttons}] =} __fltk_ginput__ (@var{n}) -## Undocumented internal function. -## @end deftypefn - -## This is ginput.m implementation for fltk. - -function [x, y, button] = __fltk_ginput__ (n = -1) - - if (isempty (gca)) - error ("ginput: must have at least one axes"); - endif - - x = y = button = []; - ginput_accumulator (0, 0, 0, 0); # initialize accumulator - - unwind_protect - - orig_buttondownfcn = get (gca, "buttondownfcn"); - set (gca, "buttondownfcn", @ginput_buttondownfcn); - - orig_ginput_keypressfcn = get (gcf, "keypressfcn"); - set (gcf, "keypressfcn", @ginput_keypressfcn); - - do - __fltk_check__ (); - - ## Release CPU. - sleep (0.01); - - [x, y, n0, button] = ginput_accumulator (-1, 0, 0, 0); - until ((n > -1 && n0 >= n) || n0 < 0) - - if (n0 > n) - ## More clicks than requested due to double-click or too fast clicking - x = x(1:n); - y = y(1:n); - button = button(1:n); - endif - - unwind_protect_cleanup - set (gca, "buttondownfcn", orig_buttondownfcn); - set (gcf, "keypressfcn", orig_ginput_keypressfcn); - end_unwind_protect - -endfunction - -function [x, y, n, button] = ginput_accumulator (mode, xn, yn, btn) - persistent x y n button; - - if (mode == 0) - ## Initialize. - x = y = button = []; - n = 0; - elseif (mode == 1) - ## Append mouse button or key press. - x = [x; xn]; - y = [y; yn]; - button = [button; btn]; - n += 1; - elseif (mode == 2) - ## The end due to Enter. - n = -1; - endif - -endfunction - -function ginput_buttondownfcn (src, button) - point = get (src, "currentpoint"); - ginput_accumulator (1, point(1,1), point(1,2), button); -endfunction - -function ginput_keypressfcn (src, evt) - point = get (gca, "currentpoint"); - key = evt.Key; - if (key == "return") - ## Enter key stops ginput. - ginput_accumulator (2, NaN, NaN, NaN); - else - ginput_accumulator (1, point(1,1), point(1,2), uint8 (key(1))); - endif -endfunction -