Mercurial > octave-libtiff
changeset 24487:aab2355f3a77
maint: move *appdata.m functions from miscellaneous to gui directory.
* scripts/gui/getappdata.m, scripts/gui/isappdata.m, scripts/gui/rmappdata.m,
scripts/gui/setappdata.m: Moved from scripts/miscellaneous directory.
* scripts/gui/module.mk, scripts/miscellaneous/module.mk: Update build system.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 28 Dec 2017 14:49:22 -0800 |
parents | 069071b2ec4b |
children | 0d196d840c02 |
files | scripts/gui/getappdata.m scripts/gui/isappdata.m scripts/gui/module.mk scripts/gui/rmappdata.m scripts/gui/setappdata.m scripts/miscellaneous/getappdata.m scripts/miscellaneous/isappdata.m scripts/miscellaneous/module.mk scripts/miscellaneous/rmappdata.m scripts/miscellaneous/setappdata.m |
diffstat | 10 files changed, 386 insertions(+), 386 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/gui/getappdata.m Thu Dec 28 14:49:22 2017 -0800 @@ -0,0 +1,93 @@ +## Copyright (C) 2010-2017 Ben Abbott +## +## 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 {} {@var{value} =} getappdata (@var{h}, @var{name}) +## @deftypefnx {} {@var{appdata} =} getappdata (@var{h}) +## Return the @var{value} of the application data @var{name} for the graphics +## object with handle @var{h}. +## +## @var{h} may also be a vector of graphics handles. If no second argument +## @var{name} is given then @code{getappdata} returns a structure, +## @var{appdata}, whose fields correspond to the appdata properties. +## +## @seealso{setappdata, isappdata, rmappdata, guidata, get, set, getpref, setpref} +## @end deftypefn + +## Author: Ben Abbott <bpabbott@mac.com> +## Created: 2010-07-15 + +function value = getappdata (h, name) + + if (nargin < 1 || nargin > 2) + print_usage (); + endif + + if (! all (ishghandle (h(:)))) + error ("getappdata: H must be a scalar or vector of graphic handles"); + endif + + if (nargin == 2) + if (! ischar (name)) + error ("getappdata: NAME must be a string"); + endif + + value = cell (numel (h), 1); + for i = 1:numel (h) + try + value{i} = (get (h(i), "__appdata__")).(name); + end_try_catch + endfor + + if (i == 1) + value = value{1}; + endif + + else # nargin == 1 + if (numel (h) != 1) + error ("getappdata: Only one handle H may be used when fetching appdata"); + endif + try + value = get (h, "__appdata__"); + catch + value = struct (); + end_try_catch + endif + +endfunction + + +%!test +%! unwind_protect +%! setappdata (0, "%data1%", ones (3), "%data2%", "hello world"); +%! assert (getappdata (0, "%data1%"), ones (3)); +%! assert (getappdata (0, "%data2%"), "hello world"); +%! appdata = getappdata (0); +%! name1 = "%data1%"; name2 = "%data2%"; +%! assert (appdata.(name1), ones (3)); +%! assert (appdata.(name2), "hello world"); +%! unwind_protect_cleanup +%! rmappdata (0, "%data1%", "%data2%"); +%! end_unwind_protect + +## Test input validation +%!error getappdata () +%!error getappdata (1,2,3) +%!error <H must be a scalar .* graphic handle> getappdata (-1, "hello") +%!error <NAME must be a string> getappdata (0, 1) +%!error <Only one handle H may be used when fetching appdata> getappdata ([0 0])
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/gui/isappdata.m Thu Dec 28 14:49:22 2017 -0800 @@ -0,0 +1,70 @@ +## Copyright (C) 2010-2017 Ben Abbott +## +## 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 {} {@var{valid} =} isappdata (@var{h}, @var{name}) +## Return true if the named application data, @var{name}, exists for the +## graphics object with handle @var{h}. +## +## @var{h} may also be a vector of graphics handles. +## @seealso{getappdata, setappdata, rmappdata, guidata, get, set, getpref, setpref} +## @end deftypefn + +## Author: Ben Abbott <bpabbott@mac.com> +## Created: 2010-07-15 + +function valid = isappdata (h, name) + + if (nargin < 1 || nargin > 2) + print_usage (); + endif + + if (! all (ishghandle (h(:)))) + error ("isappdata: H must be a scalar or vector of graphic handles"); + elseif (! ischar (name)) + error ("isappdata: NAME must be a string"); + endif + + valid = false (size (h)); + for i = 1:numel (h) + try + appdata = get (h(i), "__appdata__"); + if (isfield (appdata, name)) + valid(i) = true; + endif + end_try_catch + endfor + +endfunction + + +%!test +%! unwind_protect +%! setappdata (0, "%hello%", "world"); +%! assert (isappdata (0, "%hello%"), true); +%! assert (isappdata ([0 0], "%hello%"), [true, true]); +%! assert (isappdata (0, "%foobar%"), false); +%! unwind_protect_cleanup +%! rmappdata (0, "%hello%"); +%! end_unwind_protect + +## Test input validation +%!error isappdata () +%!error isappdata (1,2,3) +%!error <H must be a scalar .* graphic handle> isappdata (-1, "hello") +%!error <NAME must be a string> isappdata (0, 1)
--- a/scripts/gui/module.mk Thu Dec 28 14:44:38 2017 -0800 +++ b/scripts/gui/module.mk Thu Dec 28 14:49:22 2017 -0800 @@ -15,13 +15,17 @@ %canon_reldir%_FCN_FILES = \ %reldir%/dialog.m \ %reldir%/errordlg.m \ + %reldir%/getappdata.m \ %reldir%/guidata.m \ %reldir%/guihandles.m \ %reldir%/helpdlg.m \ %reldir%/inputdlg.m \ + %reldir%/isappdata.m \ %reldir%/listdlg.m \ %reldir%/msgbox.m \ %reldir%/questdlg.m \ + %reldir%/rmappdata.m \ + %reldir%/setappdata.m \ %reldir%/uibuttongroup.m \ %reldir%/uicontextmenu.m \ %reldir%/uicontrol.m \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/gui/rmappdata.m Thu Dec 28 14:49:22 2017 -0800 @@ -0,0 +1,81 @@ +## Copyright (C) 2010-2017 Ben Abbott +## +## 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 {} {} rmappdata (@var{h}, @var{name}) +## @deftypefnx {} {} rmappdata (@var{h}, @var{name1}, @var{name2}, @dots{}) +## Delete the application data @var{name} from the graphics object with handle +## @var{h}. +## +## @var{h} may also be a vector of graphics handles. Multiple application data +## names may be supplied to delete several properties at once. +## +## @seealso{setappdata, getappdata, isappdata} +## @end deftypefn + +## Author: Ben Abbott <bpabbott@mac.com> +## Created: 2010-07-15 + +function rmappdata (h, varargin) + + if (nargin < 2) + print_usage (); + endif + + h = h(:).'; + if (! all (ishghandle (h))) + error ("rmappdata: H must be a scalar or vector of graphic handles"); + elseif (! iscellstr (varargin)) + error ("rmappdata: NAME must be a string"); + endif + + for hg = h + if (isprop (hg, "__appdata__")) + appdata = get (hg, "__appdata__"); + vld = isfield (appdata, varargin); + if (! all (vld)) + ## FIXME: Should we bother to error out? Or just silently continue? + missing = varargin{find (! vld, 1)}; + error ("rmappdata: appdata '%s' is not present", missing); + endif + appdata = rmfield (appdata, varargin); + set (hg, "__appdata__", appdata); + endif + endfor + +endfunction + + +%!test +%! setappdata (0, "%hello%", "world"); +%! rmappdata (0, "%hello%"); +%! assert (isappdata (0, "%hello%"), false); + +%!test +%! setappdata (0, "%data1%", ones (3)); +%! setappdata (0, "%data2%", {"hello", "world"}); +%! rmappdata (0, "%data1%", "%data2%"); +%! assert (isappdata (0, "%data1%"), false); +%! assert (isappdata (0, "%data2%"), false); + +## Test input validation +%!error rmappdata () +%!error rmappdata (1) +%!error <H must be a scalar .* graphic handle> rmappdata (-1, "hello") +%!error <NAME must be a string> rmappdata (0, 1) +%!error <appdata 'foobar' is not present> rmappdata (0, "foobar")
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/gui/setappdata.m Thu Dec 28 14:49:22 2017 -0800 @@ -0,0 +1,138 @@ +## Copyright (C) 2010-2017 Ben Abbott +## +## 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 {} {} setappdata (@var{h}, @var{name}, @var{value}) +## @deftypefnx {} {} setappdata (@var{h}, @var{name1}, @var{value1}, @var{name2}, @var{value3}, @dots{}) +## @deftypefnx {} {} setappdata (@var{h}, @{@var{name1}, @var{name2}, @dots{}@}, @{@var{value1}, @var{value2}, @dots{}@}) +## Set the application data @var{name} to @var{value} for the graphics object +## with handle @var{h}. +## +## @var{h} may also be a vector of graphics handles. If the application data +## with the specified @var{name} does not exist, it is created. +## +## Multiple @var{name}/@var{value} pairs can be specified. Alternatively, a +## cell array of @var{names} and a corresponding cell array of @var{values} can +## be specified. +## +## @seealso{getappdata, isappdata, rmappdata, guidata, get, set, getpref, setpref} +## @end deftypefn + +## Author: Ben Abbott <bpabbott@mac.com> +## Created: 2010-07-15 + +function setappdata (h, varargin) + + if (nargin < 3) + print_usage (); + endif + + h = h(:).'; + if (! all (ishghandle (h))) + error ("setappdata: H must be a scalar or vector of graphic handles"); + elseif (mod (numel (varargin), 2) != 0) + error ("setappdata: NAME/VALUE arguments must occur in pairs"); + endif + + if (iscellstr (varargin{1})) + if (nargin != 3) + error ("setappdata: only 3 arguments possible when NAME is a cellstr"); + elseif (! iscell (varargin{2})) + varargin{2} = varargin(2); # convert to cell + endif + names = varargin{1}; + values = varargin{2}; + n_names = numel (names); + n_value = numel (values); + if (n_value == 1 && n_names > 1); + values = repmat (values, [1, n_names]); + elseif (n_names != n_value); + error ("setappdata: number of NAME and VALUE arguments must match"); + endif + varargin = cell (1, 2*numel (names)); + varargin(1:2:end) = names; + varargin(2:2:end) = values; + + elseif (! all (cellfun ("isclass", varargin(1:2:end), "char"))) + error ("setappdata: NAME must be a string or cellstr"); + endif + + for hg = h + try + appdata = get (hg, "__appdata__"); + catch + appdata = struct (); + addproperty ("__appdata__", hg, "any", appdata); + end_try_catch + + ## Slow, but not likely to be that many elements in loop + for narg = 1:2:numel (varargin) + appdata.(varargin{narg}) = varargin{narg+1}; + endfor + + set (hg, "__appdata__", appdata); + endfor + +endfunction + + +%!test +%! unwind_protect +%! setappdata (0, "%hello%", "world"); +%! assert (isappdata (0, "%hello%"), true); +%! assert (getappdata (0, "%hello%"), "world"); +%! unwind_protect_cleanup +%! rmappdata (0, "%hello%"); +%! end_unwind_protect + +%!test +%! unwind_protect +%! setappdata (0, "%data1%", ones (3), "%data2%", "hello world"); +%! assert (getappdata (0, "%data1%"), ones (3)); +%! assert (getappdata (0, "%data2%"), "hello world"); +%! setappdata (0, "%data1%", zeros (3)); +%! assert (getappdata (0, "%data1%"), zeros (3)); +%! unwind_protect_cleanup +%! rmappdata (0, "%data1%", "%data2%"); +%! end_unwind_protect + +%!test +%! unwind_protect +%! setappdata (0, {"%data1%", "%data2%"}, {ones(3), "hello world"}); +%! assert (getappdata (0, "%data1%"), ones (3)); +%! assert (getappdata (0, "%data2%"), "hello world"); +%! setappdata (0, "%data1%", zeros (3)); +%! assert (getappdata (0, "%data1%"), zeros (3)); +%! rmappdata (0, "%data1%", "%data2%"); +%! setappdata (0, {"%data1%", "%data2%"}, pi); +%! assert (getappdata (0, "%data1%"), pi); +%! assert (getappdata (0, "%data2%"), pi); +%! unwind_protect_cleanup +%! rmappdata (0, "%data1%", "%data2%"); +%! end_unwind_protect + +## Test input validation +%!error setappdata () +%!error setappdata (0) +%!error setappdata (0, "name") +%!error <H must be a scalar .* graphic handle> setappdata (-1, "foo", "bar") +%!error <NAME/VALUE arguments must occur in pairs> setappdata (0, "1", 2, "3") +%!error <only 3 arguments possible> setappdata (0, {"1"}, 2, "3", 4) +%!error <number of NAME and VALUE arguments must match> +%! setappdata (0, {"1", "2"}, {2, 3, 4}) +%!error <NAME must be a string> setappdata (0, 1, 2)
--- a/scripts/miscellaneous/getappdata.m Thu Dec 28 14:44:38 2017 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -## Copyright (C) 2010-2017 Ben Abbott -## -## 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 {} {@var{value} =} getappdata (@var{h}, @var{name}) -## @deftypefnx {} {@var{appdata} =} getappdata (@var{h}) -## Return the @var{value} of the application data @var{name} for the graphics -## object with handle @var{h}. -## -## @var{h} may also be a vector of graphics handles. If no second argument -## @var{name} is given then @code{getappdata} returns a structure, -## @var{appdata}, whose fields correspond to the appdata properties. -## -## @seealso{setappdata, isappdata, rmappdata, guidata, get, set, getpref, setpref} -## @end deftypefn - -## Author: Ben Abbott <bpabbott@mac.com> -## Created: 2010-07-15 - -function value = getappdata (h, name) - - if (nargin < 1 || nargin > 2) - print_usage (); - endif - - if (! all (ishghandle (h(:)))) - error ("getappdata: H must be a scalar or vector of graphic handles"); - endif - - if (nargin == 2) - if (! ischar (name)) - error ("getappdata: NAME must be a string"); - endif - - value = cell (numel (h), 1); - for i = 1:numel (h) - try - value{i} = (get (h(i), "__appdata__")).(name); - end_try_catch - endfor - - if (i == 1) - value = value{1}; - endif - - else # nargin == 1 - if (numel (h) != 1) - error ("getappdata: Only one handle H may be used when fetching appdata"); - endif - try - value = get (h, "__appdata__"); - catch - value = struct (); - end_try_catch - endif - -endfunction - - -%!test -%! unwind_protect -%! setappdata (0, "%data1%", ones (3), "%data2%", "hello world"); -%! assert (getappdata (0, "%data1%"), ones (3)); -%! assert (getappdata (0, "%data2%"), "hello world"); -%! appdata = getappdata (0); -%! name1 = "%data1%"; name2 = "%data2%"; -%! assert (appdata.(name1), ones (3)); -%! assert (appdata.(name2), "hello world"); -%! unwind_protect_cleanup -%! rmappdata (0, "%data1%", "%data2%"); -%! end_unwind_protect - -## Test input validation -%!error getappdata () -%!error getappdata (1,2,3) -%!error <H must be a scalar .* graphic handle> getappdata (-1, "hello") -%!error <NAME must be a string> getappdata (0, 1) -%!error <Only one handle H may be used when fetching appdata> getappdata ([0 0])
--- a/scripts/miscellaneous/isappdata.m Thu Dec 28 14:44:38 2017 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -## Copyright (C) 2010-2017 Ben Abbott -## -## 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 {} {@var{valid} =} isappdata (@var{h}, @var{name}) -## Return true if the named application data, @var{name}, exists for the -## graphics object with handle @var{h}. -## -## @var{h} may also be a vector of graphics handles. -## @seealso{getappdata, setappdata, rmappdata, guidata, get, set, getpref, setpref} -## @end deftypefn - -## Author: Ben Abbott <bpabbott@mac.com> -## Created: 2010-07-15 - -function valid = isappdata (h, name) - - if (nargin < 1 || nargin > 2) - print_usage (); - endif - - if (! all (ishghandle (h(:)))) - error ("isappdata: H must be a scalar or vector of graphic handles"); - elseif (! ischar (name)) - error ("isappdata: NAME must be a string"); - endif - - valid = false (size (h)); - for i = 1:numel (h) - try - appdata = get (h(i), "__appdata__"); - if (isfield (appdata, name)) - valid(i) = true; - endif - end_try_catch - endfor - -endfunction - - -%!test -%! unwind_protect -%! setappdata (0, "%hello%", "world"); -%! assert (isappdata (0, "%hello%"), true); -%! assert (isappdata ([0 0], "%hello%"), [true, true]); -%! assert (isappdata (0, "%foobar%"), false); -%! unwind_protect_cleanup -%! rmappdata (0, "%hello%"); -%! end_unwind_protect - -## Test input validation -%!error isappdata () -%!error isappdata (1,2,3) -%!error <H must be a scalar .* graphic handle> isappdata (-1, "hello") -%!error <NAME must be a string> isappdata (0, 1)
--- a/scripts/miscellaneous/module.mk Thu Dec 28 14:44:38 2017 -0800 +++ b/scripts/miscellaneous/module.mk Thu Dec 28 14:49:22 2017 -0800 @@ -24,12 +24,10 @@ %reldir%/fileparts.m \ %reldir%/fullfile.m \ %reldir%/genvarname.m \ - %reldir%/getappdata.m \ %reldir%/getfield.m \ %reldir%/gunzip.m \ %reldir%/info.m \ %reldir%/inputname.m \ - %reldir%/isappdata.m \ %reldir%/isdeployed.m \ %reldir%/ismac.m \ %reldir%/ispc.m \ @@ -53,9 +51,7 @@ %reldir%/perl.m \ %reldir%/python.m \ %reldir%/recycle.m \ - %reldir%/rmappdata.m \ %reldir%/run.m \ - %reldir%/setappdata.m \ %reldir%/setfield.m \ %reldir%/substruct.m \ %reldir%/swapbytes.m \
--- a/scripts/miscellaneous/rmappdata.m Thu Dec 28 14:44:38 2017 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -## Copyright (C) 2010-2017 Ben Abbott -## -## 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 {} {} rmappdata (@var{h}, @var{name}) -## @deftypefnx {} {} rmappdata (@var{h}, @var{name1}, @var{name2}, @dots{}) -## Delete the application data @var{name} from the graphics object with handle -## @var{h}. -## -## @var{h} may also be a vector of graphics handles. Multiple application data -## names may be supplied to delete several properties at once. -## -## @seealso{setappdata, getappdata, isappdata} -## @end deftypefn - -## Author: Ben Abbott <bpabbott@mac.com> -## Created: 2010-07-15 - -function rmappdata (h, varargin) - - if (nargin < 2) - print_usage (); - endif - - h = h(:).'; - if (! all (ishghandle (h))) - error ("rmappdata: H must be a scalar or vector of graphic handles"); - elseif (! iscellstr (varargin)) - error ("rmappdata: NAME must be a string"); - endif - - for hg = h - if (isprop (hg, "__appdata__")) - appdata = get (hg, "__appdata__"); - vld = isfield (appdata, varargin); - if (! all (vld)) - ## FIXME: Should we bother to error out? Or just silently continue? - missing = varargin{find (! vld, 1)}; - error ("rmappdata: appdata '%s' is not present", missing); - endif - appdata = rmfield (appdata, varargin); - set (hg, "__appdata__", appdata); - endif - endfor - -endfunction - - -%!test -%! setappdata (0, "%hello%", "world"); -%! rmappdata (0, "%hello%"); -%! assert (isappdata (0, "%hello%"), false); - -%!test -%! setappdata (0, "%data1%", ones (3)); -%! setappdata (0, "%data2%", {"hello", "world"}); -%! rmappdata (0, "%data1%", "%data2%"); -%! assert (isappdata (0, "%data1%"), false); -%! assert (isappdata (0, "%data2%"), false); - -## Test input validation -%!error rmappdata () -%!error rmappdata (1) -%!error <H must be a scalar .* graphic handle> rmappdata (-1, "hello") -%!error <NAME must be a string> rmappdata (0, 1) -%!error <appdata 'foobar' is not present> rmappdata (0, "foobar")
--- a/scripts/miscellaneous/setappdata.m Thu Dec 28 14:44:38 2017 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,138 +0,0 @@ -## Copyright (C) 2010-2017 Ben Abbott -## -## 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 {} {} setappdata (@var{h}, @var{name}, @var{value}) -## @deftypefnx {} {} setappdata (@var{h}, @var{name1}, @var{value1}, @var{name2}, @var{value3}, @dots{}) -## @deftypefnx {} {} setappdata (@var{h}, @{@var{name1}, @var{name2}, @dots{}@}, @{@var{value1}, @var{value2}, @dots{}@}) -## Set the application data @var{name} to @var{value} for the graphics object -## with handle @var{h}. -## -## @var{h} may also be a vector of graphics handles. If the application data -## with the specified @var{name} does not exist, it is created. -## -## Multiple @var{name}/@var{value} pairs can be specified. Alternatively, a -## cell array of @var{names} and a corresponding cell array of @var{values} can -## be specified. -## -## @seealso{getappdata, isappdata, rmappdata, guidata, get, set, getpref, setpref} -## @end deftypefn - -## Author: Ben Abbott <bpabbott@mac.com> -## Created: 2010-07-15 - -function setappdata (h, varargin) - - if (nargin < 3) - print_usage (); - endif - - h = h(:).'; - if (! all (ishghandle (h))) - error ("setappdata: H must be a scalar or vector of graphic handles"); - elseif (mod (numel (varargin), 2) != 0) - error ("setappdata: NAME/VALUE arguments must occur in pairs"); - endif - - if (iscellstr (varargin{1})) - if (nargin != 3) - error ("setappdata: only 3 arguments possible when NAME is a cellstr"); - elseif (! iscell (varargin{2})) - varargin{2} = varargin(2); # convert to cell - endif - names = varargin{1}; - values = varargin{2}; - n_names = numel (names); - n_value = numel (values); - if (n_value == 1 && n_names > 1); - values = repmat (values, [1, n_names]); - elseif (n_names != n_value); - error ("setappdata: number of NAME and VALUE arguments must match"); - endif - varargin = cell (1, 2*numel (names)); - varargin(1:2:end) = names; - varargin(2:2:end) = values; - - elseif (! all (cellfun ("isclass", varargin(1:2:end), "char"))) - error ("setappdata: NAME must be a string or cellstr"); - endif - - for hg = h - try - appdata = get (hg, "__appdata__"); - catch - appdata = struct (); - addproperty ("__appdata__", hg, "any", appdata); - end_try_catch - - ## Slow, but not likely to be that many elements in loop - for narg = 1:2:numel (varargin) - appdata.(varargin{narg}) = varargin{narg+1}; - endfor - - set (hg, "__appdata__", appdata); - endfor - -endfunction - - -%!test -%! unwind_protect -%! setappdata (0, "%hello%", "world"); -%! assert (isappdata (0, "%hello%"), true); -%! assert (getappdata (0, "%hello%"), "world"); -%! unwind_protect_cleanup -%! rmappdata (0, "%hello%"); -%! end_unwind_protect - -%!test -%! unwind_protect -%! setappdata (0, "%data1%", ones (3), "%data2%", "hello world"); -%! assert (getappdata (0, "%data1%"), ones (3)); -%! assert (getappdata (0, "%data2%"), "hello world"); -%! setappdata (0, "%data1%", zeros (3)); -%! assert (getappdata (0, "%data1%"), zeros (3)); -%! unwind_protect_cleanup -%! rmappdata (0, "%data1%", "%data2%"); -%! end_unwind_protect - -%!test -%! unwind_protect -%! setappdata (0, {"%data1%", "%data2%"}, {ones(3), "hello world"}); -%! assert (getappdata (0, "%data1%"), ones (3)); -%! assert (getappdata (0, "%data2%"), "hello world"); -%! setappdata (0, "%data1%", zeros (3)); -%! assert (getappdata (0, "%data1%"), zeros (3)); -%! rmappdata (0, "%data1%", "%data2%"); -%! setappdata (0, {"%data1%", "%data2%"}, pi); -%! assert (getappdata (0, "%data1%"), pi); -%! assert (getappdata (0, "%data2%"), pi); -%! unwind_protect_cleanup -%! rmappdata (0, "%data1%", "%data2%"); -%! end_unwind_protect - -## Test input validation -%!error setappdata () -%!error setappdata (0) -%!error setappdata (0, "name") -%!error <H must be a scalar .* graphic handle> setappdata (-1, "foo", "bar") -%!error <NAME/VALUE arguments must occur in pairs> setappdata (0, "1", 2, "3") -%!error <only 3 arguments possible> setappdata (0, {"1"}, 2, "3", 4) -%!error <number of NAME and VALUE arguments must match> -%! setappdata (0, {"1", "2"}, {2, 3, 4}) -%!error <NAME must be a string> setappdata (0, 1, 2)