]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/private/__fltk_ginput__.m
update packages
[CreaPhase.git] / octave_packages / m / plot / private / __fltk_ginput__.m
1 ## Copyright (C) 2010-2012 Shai Ayal
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{buttons}] =} __fltk_ginput__ (@var{f}, @var{n})
21 ## Undocumented internal function.
22 ## @end deftypefn
23
24 ## This is ginput.m implementation for fltk.
25
26 ## FIXME -- Key presses cannot toggle menu items nor hotkey functionality
27 ## (grid, autoscale) during ginput!
28
29 function [x, y, button] = __fltk_ginput__ (f, n = -1)
30
31   if (isempty (get (f, "currentaxes")))
32     error ("ginput: must have at least one axes");
33   endif
34
35   x = y = button = [];
36   ginput_aggregator (0, 0, 0, 0);
37
38   unwind_protect
39
40     orig_windowbuttondownfcn = get (f, "windowbuttondownfcn");
41     set (f, "windowbuttondownfcn", @ginput_windowbuttondownfcn);
42
43     orig_ginput_keypressfcn = get (f, "keypressfcn");
44     set (f, "keypressfcn", @ginput_keypressfcn);
45
46     while (true)
47       __fltk_redraw__ ();
48
49       ## Release CPU.
50       sleep (0.01);
51
52       [x, y, n0, button] = ginput_aggregator (-1, 0, 0, 0);
53       if (n0 == n || n0 < 0)
54         break;
55       endif
56     endwhile
57
58   unwind_protect_cleanup
59     set (f, "windowbuttondownfcn", orig_windowbuttondownfcn);
60     set (f, "keypressfcn", orig_ginput_keypressfcn);
61   end_unwind_protect
62
63 endfunction
64
65 function [x, y, n, button] = ginput_aggregator (mode, xn, yn, btn)
66   persistent x y n button;
67
68   if (mode == 0)
69     ## Initialize.
70     x = [];
71     y = [];
72     button = [];
73     n = 0;
74   elseif (mode == 1)
75     ## Accept mouse button or key press.
76     x = [x; xn];
77     y = [y; yn];
78     button = [button; btn];
79     n += 1;
80   elseif (mode == 2)
81     ## The end due to Enter.
82     n = -1;
83  endif
84 endfunction
85
86 function ginput_windowbuttondownfcn (src, data)
87   point = get (get (src,"currentaxes"), "currentpoint");
88   ## FIXME -- How to get the actual mouse button pressed (1,2,3) into
89   ## "button"?
90   button = 1;
91   ginput_aggregator (1, point(1,1), point(2,1), button);
92 endfunction
93
94 function ginput_keypressfcn (src, evt)
95   point = get (get (src, "currentaxes"), "currentpoint");
96   ## FIXME -- use evt.Key or evt.Character?
97   key = evt.Key;
98   if (key == 10)
99     ## Enter key.
100     ginput_aggregator (2, point(1,1), point(2,1), key);
101   else
102     ginput_aggregator (1, point(1,1), point(2,1), key);
103   endif
104 endfunction
105