# HG changeset patch # User David Bateman # Date 1206995429 14400 # Node ID e0c930dda642dd939a937d091761e24f300b3ef4 # Parent 2f0920d1edd4c61cb1683cfc75a14ff92fa334db ginput: new function diff -r 2f0920d1edd4 -r e0c930dda642 scripts/ChangeLog --- a/scripts/ChangeLog Mon Mar 31 16:10:09 2008 -0400 +++ b/scripts/ChangeLog Mon Mar 31 16:30:29 2008 -0400 @@ -1,3 +1,10 @@ +2008-03-31 David Bateman + + * plot/ginput.m: New function. + * plot/__gnuplot_ginput__.m: New function based on a version of + ginput.m from Petr Mikulik . + * plot/Makefile.in (SOURCES): Add them to the list. + 2008-03-31 Dmitri A. Sergatskov * miscellaneous/run.m: Fix check for existence of file. diff -r 2f0920d1edd4 -r e0c930dda642 scripts/plot/Makefile.in --- a/scripts/plot/Makefile.in Mon Mar 31 16:10:09 2008 -0400 +++ b/scripts/plot/Makefile.in Mon Mar 31 16:30:29 2008 -0400 @@ -47,6 +47,7 @@ __go_close_all__.m \ __go_draw_axes__.m \ __go_draw_figure__.m \ + __gnuplot_ginput__.m \ __gnuplot_version__.m \ __line__.m \ __next_line_color__.m \ @@ -107,6 +108,7 @@ fplot.m \ gca.m \ gcf.m \ + ginput.m \ grid.m \ hidden.m \ hist.m \ diff -r 2f0920d1edd4 -r e0c930dda642 scripts/plot/__gnuplot_ginput__.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/plot/__gnuplot_ginput__.m Mon Mar 31 16:30:29 2008 -0400 @@ -0,0 +1,110 @@ +## Copyright (C) 2004, 2006, 2008 Petr Mikulik +## +## 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 +## . + +## This is ginput.m implementation for gnuplot and X11. +## It requires gnuplot 4.1 and later. + +## This file initially bore the copyright statement +## Petr Mikulik +## History: June 2006; August 2005; June 2004; April 2004 +## License: public domain + +## -*- texinfo -*- +## @deftypefn {Function File} {[@var{x}, @var{y}, @var{buttons}] =} __gnuplot_ginput__ (@var{f}, @var{n}) +## Undocumented internal function. +## @end deftypefn + +function [x, y, button] = __gnuplot_ginput__ (f, n) + + stream = get (f, "__plot_stream__"); + + if (compare_versions (__gnuplot_version__ (), "4.0", "<=")) + error ("ginput: version %s of gnuplot not supported", gnuplot_version ()); + endif + + if (nargin == 0) + x = zeros (n, 1); + y = zeros (n, 1); + button = zeros (n, 1); + else + x = zeros (n, 1); + y = zeros (n, 1); + button = zeros (n, 1); + endif + + gpin_name = tmpnam (); + + ## 6*8*8 == 0600 + [err, msg] = mkfifo (gpin_name, 6*8*8); + if (err != 0) + error ("ginput: Can not open fifo (%s)", msg); + endif + + unwind_protect + + k = 0; + while (true) + k++; + fprintf (stream, "set print \"%s\";\n", gpin_name); + fflush (stream); + gpin = fopen (gpin_name, "r"); + fputs (stream, "pause mouse any;\n\n"); + + ## Notes: MOUSE_* can be undefined if user closes gnuplot by "q" + ## or Alt-F4. Further, this abrupt close also requires the leading + ## "\n" on the next line. + fputs (stream, "\nif (exists(\"MOUSE_KEY\") && exists(\"MOUSE_X\")) print MOUSE_X, MOUSE_Y, MOUSE_KEY; else print \"0 0 -1\"\n"); + + ## Close output file, otherwise all blocks (why?!). + fputs (stream, "set print;\n"); + fflush (stream); + + ## Now read from fifo. + [x(k), y(k), button(k), count] = fscanf (gpin, "%f %f %d", "C"); + fclose (gpin); + + if ([x(k), y(k), button(k)] == [0, 0, -1]) + ## Mousing not active (no plot yet). + break; + endif + + if (nargin > 0) + ## Input argument n was given => stop when k == n. + if (k == n) + break; + endif + else + ## Input argument n not given => stop when hitting a return key. + ## if (button(k) == 0x0D || button(k) == 0x0A) + ## ## hit Return or Enter + if (button(k) == 0x0D) + ## hit Return + x(k:end) = []; + y(k:end) = []; + button(k:end) = []; + break; + endif + endif + endwhile + + unwind_protect_cleanup + unlink (gpin_name); + end_unwind_protect + +endfunction + diff -r 2f0920d1edd4 -r e0c930dda642 scripts/plot/ginput.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/plot/ginput.m Mon Mar 31 16:30:29 2008 -0400 @@ -0,0 +1,47 @@ +## Copyright (C) 2008 David Bateman +## +## 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}] =} ginput (@var{n}) +## Return which mouse buttons were pressed and keys were hit on the current +## figure. If @var{n} is defined, then wait for @var{n} mouse clicks +## before returning. If @var{n} is not defined, then @code{ginput} will +## loop until the return key is pressed. +## @end deftypefn + +function varargout = ginput (n) + + if (nargin > 1) + print_usage (); + elseif (nargin == 0) + ## A startup-value + n = 100; + endif + + f = gcf (); + drawnow (); + backend = (get (f, "__backend__")); + + varargout = cell (1, nargout); + if (nargin == 0) + [varargout{:}] = feval (strcat ("__", backend, "_ginput__"), f); + else + [varargout{:}] = feval (strcat ("__", backend, "_ginput__"), f, n); + endif + +endfunction