1########################################################################
2##
3## Copyright (C) 2017-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 {} {} ishandle (@var{h})
28## Return true if @var{h} is a handle to a graphics or Java object and false
29## otherwise.
30##
31## @var{h} may also be a matrix of handles in which case a logical array is
32## returned that is true where the elements of @var{h} are handles to graphics
33## or Java objects and false where they are not.
34##
35## Programming Note: It is often more useful to test for a specific object
36## type.  To determine if a handle belongs to a graphics object use
37## @code{ishghandle} or @code{isgraphics}.  To determine if a handle belongs
38## to a Java object use @code{isjava}.
39## @seealso{ishghandle, isgraphics, isjava}
40## @end deftypefn
41
42function retval = ishandle (h)
43
44  if (nargin != 1)
45    print_usage ();
46  endif
47
48  retval = ishghandle (h) | isjava (h);
49
50endfunction
51
52
53%!test
54%! hf = figure ("visible", "off");
55%! unwind_protect
56%!   assert (ishandle (hf));
57%!   assert (! ishandle (-hf));
58%!   ax = gca;
59%!   l = line;
60%!   assert (ishandle (ax));
61%!   assert (! ishandle (-ax));
62%!   assert (ishandle ([l, -1, ax, hf]), logical ([1, 0, 1, 1]));
63%!   assert (ishandle ([l, -1, ax, hf]'), logical ([1, 0, 1, 1]'));
64%! unwind_protect_cleanup
65%!   close (hf);
66%! end_unwind_protect
67
68%!assert (ishandle ([-1 0]), [false true])
69
70%!testif HAVE_JAVA; usejava ("jvm")
71%! jobj = javaObject ("java.lang.Double", 1.0);
72%! assert (ishandle (jobj));
73
74## Test input validation
75%!error ishandle ()
76%!error ishandle (1, 2)
77