annotate scripts/plot/util/ginput.m @ 19953:3fc946d5e91f

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.
author John W. Eaton <jwe@octave.org>
date Wed, 18 Feb 2015 14:29:59 -0500
parents 4197fc428c7d
children c040bed12b2e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19898
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19205
diff changeset
1 ## Copyright (C) 2008-2015 David Bateman
7673
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
2 ##
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
3 ## This file is part of Octave.
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
4 ##
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 ## your option) any later version.
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 ##
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ## General Public License for more details.
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 ##
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
18
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 ## -*- texinfo -*-
17122
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
20 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{buttons}] =} ginput (@var{n})
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
21 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{buttons}] =} ginput ()
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
22 ## Return the position and type of mouse button clicks and/or key strokes
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
23 ## in the current figure window.
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
24 ##
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
25 ## If @var{n} is defined, then capture @var{n} events before returning.
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
26 ## When @var{n} is not defined @code{ginput} will loop until the return key
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
27 ## @key{RET} is pressed.
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
28 ##
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
29 ## The return values @var{x}, @var{y} are the coordinates where the mouse
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
30 ## was clicked in the units of the current axes. The return value @var{button}
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
31 ## is 1, 2, or 3 for the left, middle, or right button. If a key is pressed
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
32 ## the ASCII value is returned in @var{button}.
17483
710309214e0d doc: Add seealso links between waitfor, waitforbuttonpress, and ginput.
Rik <rik@octave.org>
parents: 17122
diff changeset
33 ## @seealso{gtext, waitforbuttonpress}
7673
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
34 ## @end deftypefn
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
35
19953
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
36 function varargout = ginput (n = -1)
7673
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
37
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
38 if (nargin > 1)
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
39 print_usage ();
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
40 endif
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
41
19953
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
42 ## Create an axis, if necessary.
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
43 fig = gcf ();
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
44 ax = gca ();
7673
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
45 drawnow ();
19953
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
46
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
47 if (isempty (ax))
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
48 error ("ginput: must have at least one axes");
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
49 endif
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
50
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
51 toolkit = get (fig, "__graphics_toolkit__");
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
52 toolkit_fcn = sprintf ("__%s_ginput__", toolkit);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
53
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
54 if (exist (toolkit_fcn))
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
55 varargout = cell (1, nargout);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
56 if (nargin == 0)
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
57 [varargout{:}] = feval (toolkit_fcn, fig);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
58 else
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
59 [varargout{:}] = feval (toolkit_fcn, fig, n);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
60 endif
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
61 return
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
62 endif
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
63
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
64 x = y = button = [];
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
65 ginput_accumulator (0, 0, 0, 0); # initialize accumulator
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
66
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
67 orig_buttondownfcn = get (fig, "buttondownfcn");
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
68 orig_ginput_keypressfcn = get (fig, "keypressfcn");
7673
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
69
19953
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
70 unwind_protect
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
71
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
72 set (ax, "buttondownfcn", @ginput_buttondownfcn);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
73 set (fig, "keypressfcn", @ginput_keypressfcn);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
74
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
75 do
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
76 if (strcmp (toolkit, "fltk"))
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
77 __fltk_check__ ();
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
78 endif
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
79
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
80 ## Release CPU.
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
81 sleep (0.01);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
82
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
83 [x, y, n0, button] = ginput_accumulator (-1, 0, 0, 0);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
84 until ((n > -1 && n0 >= n) || n0 < 0)
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
85
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
86 if (n0 > n)
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
87 ## More clicks than requested due to double-click or too fast clicking
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
88 x = x(1:n);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
89 y = y(1:n);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
90 button = button(1:n);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
91 endif
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
92
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
93 unwind_protect_cleanup
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
94 set (ax, "buttondownfcn", orig_buttondownfcn);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
95 set (fig, "keypressfcn", orig_ginput_keypressfcn);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
96 end_unwind_protect
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
97
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
98 varargout = {x, y, button};
7673
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
99
e0c930dda642 ginput: new function
David Bateman <dbateman@free.fr>
parents:
diff changeset
100 endfunction
13123
6efa1a691713 Add further tests for scripts/plot.
Kai Habel <kai.habel@gmx.de>
parents: 11576
diff changeset
101
19953
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
102 function [x, y, n, button] = ginput_accumulator (mode, xn, yn, btn)
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
103 persistent x y n button;
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
104
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
105 if (mode == 0)
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
106 ## Initialize.
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
107 x = y = button = [];
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
108 n = 0;
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
109 elseif (mode == 1)
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
110 ## Append mouse button or key press.
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
111 x = [x; xn];
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
112 y = [y; yn];
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
113 button = [button; btn];
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
114 n += 1;
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
115 elseif (mode == 2)
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
116 ## The end due to Enter.
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
117 n = -1;
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
118 endif
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
119
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
120 endfunction
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
121
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
122 function ginput_buttondownfcn (src, button)
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
123 point = get (src, "currentpoint");
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
124 ginput_accumulator (1, point(1,1), point(1,2), button);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
125 endfunction
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
126
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
127 function ginput_keypressfcn (src, evt)
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
128 point = get (ax, "currentpoint");
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
129 key = evt.Key;
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
130 if (key == "return")
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
131 ## Enter key stops ginput.
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
132 ginput_accumulator (2, NaN, NaN, NaN);
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
133 else
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
134 ginput_accumulator (1, point(1,1), point(1,2), uint8 (key(1)));
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
135 endif
3fc946d5e91f make ginput work for all toolkits again (bug #41977)
John W. Eaton <jwe@octave.org>
parents: 19898
diff changeset
136 endfunction
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
137
13123
6efa1a691713 Add further tests for scripts/plot.
Kai Habel <kai.habel@gmx.de>
parents: 11576
diff changeset
138 ## Remove from test statistics. No real tests possible.
6efa1a691713 Add further tests for scripts/plot.
Kai Habel <kai.habel@gmx.de>
parents: 11576
diff changeset
139 %!test
6efa1a691713 Add further tests for scripts/plot.
Kai Habel <kai.habel@gmx.de>
parents: 11576
diff changeset
140 %! assert (1);