1########################################################################
2##
3## Copyright (C) 2012-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} uiwait
28## @deftypefnx {} {} uiwait (@var{h})
29## @deftypefnx {} {} uiwait (@var{h}, @var{timeout})
30## Suspend program execution until the figure with handle @var{h} is deleted
31## or @code{uiresume} is called.
32##
33## When no figure handle is specified this function uses the current figure.
34## If the figure handle is invalid or there is no current figure, this
35## functions returns immediately.
36##
37## When specified, @var{timeout} defines the number of seconds to wait
38## for the figure deletion or the @code{uiresume} call.  The timeout value
39## must be at least 1.  If a smaller value is specified, a warning is issued
40## and a timeout value of 1 is used instead.  If a non-integer value is
41## specified, it is truncated towards 0.  If @var{timeout} is not specified,
42## the program execution is suspended indefinitely.
43## @seealso{uiresume, waitfor}
44## @end deftypefn
45
46function uiwait (varargin)
47
48  h = [];
49  timeout = [];
50
51  if (nargin == 0)
52    h = get (0, "currentfigure");
53  else
54    h = varargin{1};
55    if (! isfigure (h))
56      error ("uiwait: invalid figure handle H");
57    endif
58    if (nargin > 1)
59      timeout = varargin{2};
60    endif
61  endif
62
63  if (! isempty (h))
64    unwind_protect
65      try
66        addproperty ("__uiwait_state__", h, "radio", "none|{active}|triggered");
67      catch
68        if (! strcmp (get (h, "__uiwait_state__"), "none"))
69          error ("uiwait: an active uiwait call for this figure already exists");
70        endif
71        set (h, "__uiwait_state__", "active");
72      end_try_catch
73      waitfor_args = {h, "__uiwait_state__", "triggered"};
74      if (! isempty (timeout))
75        waitfor_args(end+1:end+2) = {"timeout", timeout};
76      endif
77      waitfor (waitfor_args{:});
78    unwind_protect_cleanup
79      if (ishghandle (h) && isprop (h, "__uiwait_state__"))
80        set (h, "__uiwait_state__", "none");
81      endif
82    end_unwind_protect
83  endif
84
85endfunction
86