]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/uiwait.m
update packages
[CreaPhase.git] / octave_packages / m / plot / uiwait.m
1 ## Copyright (C) 2012 Michael Goffioul
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} {} uiwait
21 ## @deftypefnx {Function File} {} uiwait (@var{h})
22 ## @deftypefnx {Function File} {} uiwait (@var{h}, @var{timeout})
23 ## Suspend program execution until the figure with handle @var{h} is
24 ## deleted or @code{uiresume} is called.  When no figure handle is specified,
25 ## this function uses the current figure.
26 ##
27 ## If the figure handle is invalid or there is no current figure, this
28 ## functions returns immediately.
29 ##
30 ## When specified, @var{timeout} defines the number of seconds to wait
31 ## for the figure deletion or the @code{uiresume} call.  The timeout value
32 ## must be at least 1. If a smaller value is specified, a warning is issued
33 ## and a timeout value of 1 is used instead.  If a non-integer value is
34 ## specified, it is truncated towards 0. If @var{timeout} is not specified,
35 ## the program execution is suspended indefinitely.
36 ## @seealso{uiresume, waitfor}
37 ## @end deftypefn
38
39 ## Author: goffioul
40
41 function uiwait (varargin)
42
43   h = [];
44   timeout = [];
45
46   if (nargin == 0)
47     h = get (0, "currentfigure");
48   else
49     h = varargin{1};
50     if (! ishandle (h) || ! strcmp (get (h, "type"), "figure"))
51       error ("uiwait: invalid figure handle");
52     endif
53     if (nargin > 1)
54       timeout = varargin{2};
55     endif
56   endif
57
58   if (! isempty (h))
59     unwind_protect
60       try
61         addproperty ("__uiwait_state__", h, "radio", "none|{active}|triggered");
62       catch
63         if (! strcmp (get (h, "__uiwait_state__"), "none"))
64           error ("uiwait: an active uiwait call for this figure already exists");
65         endif
66         set (h, "__uiwait_state__", "active");
67       end_try_catch
68       waitfor_args = {h, "__uiwait_state__", "triggered"};
69       if (! isempty (timeout))
70         waitfor_args(end+1:end+2) = {"timeout", timeout};
71       endif
72       waitfor (waitfor_args{:});
73     unwind_protect_cleanup
74       if (ishandle (h) && isprop (h, "__uiwait_state__"))
75         set (h, "__uiwait_state__", "none");
76       endif
77     end_unwind_protect
78   endif
79
80 endfunction