changeset 7674:52d8d50e74c1

Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
author David Bateman <dbateman@free.fr>
date Mon, 31 Mar 2008 21:41:30 -0400
parents e0c930dda642
children 8abada567409
files scripts/ChangeLog scripts/plot/Makefile.in scripts/plot/__gnuplot_ginput__.m scripts/plot/ginput.m scripts/plot/gtext.m scripts/plot/waitforbuttonpress.m
diffstat 6 files changed, 157 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Mon Mar 31 16:30:29 2008 -0400
+++ b/scripts/ChangeLog	Mon Mar 31 21:41:30 2008 -0400
@@ -1,5 +1,12 @@
 2008-03-31  David Bateman  <dbateman@free.fr>
 
+	* plot/gtext.m: New function to place text on a plot.
+	* plot/waitforbuttonpress.m: New function.
+	* plot/Makefile.in (SOURCES): Add them to the list.
+	* plot/__gnuplot_ginput__.m: Bug fix for nargin==1. Workaround for
+	missing mkfifo under Windows.
+	* plot/ginput.m: Eliminate setting of n.
+	
 	* plot/ginput.m: New function.
 	* plot/__gnuplot_ginput__.m: New function based on a version of
 	ginput.m from Petr Mikulik <mikulik@physics.muni.cz>.
--- a/scripts/plot/Makefile.in	Mon Mar 31 16:30:29 2008 -0400
+++ b/scripts/plot/Makefile.in	Mon Mar 31 21:41:30 2008 -0400
@@ -110,6 +110,7 @@
   gcf.m \
   ginput.m \
   grid.m \
+  gtext.m \
   hidden.m \
   hist.m \
   hold.m \
@@ -165,6 +166,7 @@
   text.m \
   title.m \
   view.m \
+  waitforbuttonpress.m \
   xlabel.m \
   xlim.m \
   ylabel.m \
--- a/scripts/plot/__gnuplot_ginput__.m	Mon Mar 31 16:30:29 2008 -0400
+++ b/scripts/plot/__gnuplot_ginput__.m	Mon Mar 31 21:41:30 2008 -0400
@@ -16,6 +16,11 @@
 ## along with Octave; see the file COPYING.  If not, see
 ## <http://www.gnu.org/licenses/>.
 
+## -*- texinfo -*-
+## @deftypefn {Function File} {[@var{x}, @var{y}, @var{buttons}] =} __gnuplot_ginput__ (@var{f}, @var{n})
+## Undocumented internal function.
+## @end deftypefn
+
 ## This is ginput.m implementation for gnuplot and X11.
 ## It requires gnuplot 4.1 and later.
 
@@ -24,12 +29,9 @@
 ## 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)
 
-function [x, y, button] = __gnuplot_ginput__ (f, n)
+  persistent have_mkfifo = ! ispc ();
 
   stream = get (f, "__plot_stream__");
 
@@ -37,10 +39,10 @@
     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);
+  if (nargin == 1)
+    x = zeros (100, 1);
+    y = zeros (100, 1);
+    button = zeros (100, 1);
   else
     x = zeros (n, 1);
     y = zeros (n, 1);
@@ -49,11 +51,14 @@
 
   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    
+  if (have_mkfifo)
+    ## Use pipes if not on Windows. Mode: 6*8*8 ==  0600
+    [err, msg] = mkfifo(gpin_name, 6*8*8);
+
+    if (err != 0)
+      error ("ginput: Can not open fifo (%s)", msg);
+    endif
+  endif
 
   unwind_protect
 
@@ -62,7 +67,12 @@
       k++;
       fprintf (stream, "set print \"%s\";\n", gpin_name);
       fflush (stream);
-      gpin = fopen (gpin_name, "r");
+      if (have_mkfifo)
+	[gpin, err] = fopen (gpin_name, "r");
+	if (err != 0)
+	  error ("ginput: Can not open fifo (%s)", msg);
+	endif
+      endif
       fputs (stream, "pause mouse any;\n\n");
 
       ## Notes: MOUSE_* can be undefined if user closes gnuplot by "q"
@@ -74,8 +84,27 @@
       fputs (stream, "set print;\n");
       fflush (stream);
 
-      ## Now read from fifo.
-      [x(k), y(k), button(k), count] = fscanf (gpin, "%f %f %d", "C");
+      if (! have_mkfifo)
+	while (exist (gpin_name, "file") == 0)
+	endwhile
+	[gpin, msg] = fopen (gpin_name, "r");
+
+	if (gpin < 0)
+	  error ("ginput: Can not open file (%s)", msg);
+	endif
+
+	## Now read from file
+	count = 0;
+	while (count == 0)
+	  [xk, yk, buttonk, count] = fscanf (gpin, "%f %f %d", "C");
+	endwhile
+	x(k) = xk;
+	y(k) = yk;
+	button (k) = buttonk;
+      else
+	## Now read from fifo.
+	[x(k), y(k), button(k), count] = fscanf (gpin, "%f %f %d", "C");
+      endif
       fclose (gpin);
 
       if ([x(k), y(k), button(k)] == [0, 0, -1])
@@ -83,7 +112,7 @@
 	break;
       endif
 
-      if (nargin > 0)
+      if (nargin > 1)
 	## Input argument n was given => stop when k == n.
 	if (k == n) 
 	  break; 
--- a/scripts/plot/ginput.m	Mon Mar 31 16:30:29 2008 -0400
+++ b/scripts/plot/ginput.m	Mon Mar 31 21:41:30 2008 -0400
@@ -28,9 +28,6 @@
 
   if (nargin > 1)
     print_usage ();
-  elseif (nargin == 0)
-    ## A startup-value
-    n = 100;
   endif
 
   f = gcf ();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/plot/gtext.m	Mon Mar 31 21:41:30 2008 -0400
@@ -0,0 +1,54 @@
+## 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
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} gtext (@var{s})
+## @deftypefnx {Function File} {} gtext (@dots{}, @var{prop}, @var{val})
+## Place text on the current figure. The text can be defined by the
+## string @var{s}. If @var{s} is a cell array, each element of the cell
+## array is written to a separate line.
+##
+## Additional arguments are passed to the underlying text object as
+## properties.
+## @seealso{ginput}
+## @end deftypefn
+
+function gtext (s, varargin)
+
+  if (nargin > 0)
+    if (iscellstr (s))
+      if (isempty (s))
+	s = "";
+      else
+	s = sprintf ("%s\n", s{:});
+      endif
+    endif
+    if (ischar (s))
+      if (! isempty (s))
+	[x, y] = ginput (1);
+	text (x, y, s, varargin{:});
+      endif
+    else
+      error ("gtext: expecting a string or cell array of strings");
+    endif 
+  else
+    print_usage ();
+  endif
+
+endfunction
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/plot/waitforbuttonpress.m	Mon Mar 31 21:41:30 2008 -0400
@@ -0,0 +1,47 @@
+## 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
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{b} =} waitforbuttonpress ()
+## Wait for button or mouse press.over a figure window. The value of
+## @var{b} returns 0 if a mouse button was pressed or 1 is a key was
+## pressed.
+## @seealso{ginput}
+## @end deftypefn
+
+## The original version of this code bore the copyright
+## Author: Petr Mikulik
+## License: public domain
+
+function a = waitforbuttonpress ()
+
+  if (nargin != 0 || nargout > 1)
+    print_usage ();
+  endif
+
+  [x, y, k] = ginput (1);
+
+  if (nargout == 1)
+    if (k <= 5) 
+      a = 0;
+    else 
+      a = 1;
+    endif
+  endif
+
+endfunction