# HG changeset patch
# User Richard Stephens <richard@sansanc.co.uk>
# Date 1666693929 -3600
#      Tue Oct 25 11:32:09 2022 +0100
# Node ID e62d81b969c89748a1cf20053ecabd231e5fec3d
# Parent  0411c5c5041190ee8b7d4deaaaeeeea08c393f8c
uiopen.m; uisave.m: created new scripts (bug #55065)

* uiopen.m: added file for Matlab compatibility. Uses uigetfile and open.
* uisave.m: added file for Matlab compatibility. Uses uiputfile.

diff -r 0411c5c50411 -r e62d81b969c8 scripts/gui/uiopen.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/gui/uiopen.m	Tue Oct 25 11:32:09 2022 +0100
@@ -0,0 +1,139 @@
+########################################################################
+##
+## Copyright (C) 2022 The Octave Project Developers
+##
+## See the file COPYRIGHT.md in the top-level directory of this
+## distribution or <https://octave.org/copyright/>.
+##
+## 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
+## <https://www.gnu.org/licenses/>.
+##
+########################################################################
+
+## -*- texinfo -*-
+## @deftypefn  {} {} uiopen ()
+## @deftypefnx {} {} uiopen (@var{type})
+## @deftypefnx {} {} uiopen (@var{flt})
+## @deftypefnx {} {} uiopen (@var{fname})
+## @deftypefnx {} {} uiopen (@var{fname}, @var{tf})
+##
+## Open a GUI dialog for selecting an Octave file and open the selected file in
+## the appropriate application.
+##
+## @var{type} applies a filter according to the file type. The following options
+## can be used:
+##
+## @table @asis
+## @item @qcode{"octave"}
+## The filter will include all Octave file types (*.m; *.mat; *.ofig).
+## Example: @code{uiopen ("octave")}
+##
+## @item @qcode{"matlab"}
+## For compatibility with MATLAB, the filter will include all Octave file types
+## (*.m; *.mat; *.ofig). Example: @code{uiopen ("matlab")}
+##
+## @item @qcode{"editor"}
+## The filter will include all Octave editor file types (*.m). Example:
+## @code{uiopen ("editor")}
+##
+## @item @qcode{"load"}
+## The filter will include all Octave data file types (*.mat). Example:
+## @code{uiopen ("load")}
+##
+## @item @qcode{"figure"}
+## The filter will include all Octave figure file types (*.ofig). Example:
+## @code{uiopen ("figure")}
+## @end table
+##
+## @var{flt} can be set to a character string giving the file filter
+## specification. Example: @code{uiopen ("*.m; *.mat")}
+##
+## @var{fname} specifies a filename which is entered as the default file name in
+## the select dialog and the file type filter uses the extension of the file.
+## Example: @code{uiopen ("my_file.m")}
+##
+## @var{tf} is a flag which, when set to @code{true} and used in conjunction
+## with @var{fname} opens the specified file without invoking a dialog. Example:
+## @code{uiopen ("my_file.m", true)}
+##
+## @seealso{uisave}
+## @end deftypefn
+
+function uiopen (varargin)
+
+  if (nargin > 2)
+    error ("uiopen: number of input arguments must be less than three");
+  endif
+
+  if (nargin == 0)
+    ## Default to Octave files.
+    type = "octave";
+  else
+    type  = varargin{1};
+  endif
+
+  check_arg2 = false;
+  switch (lower (type))
+    case "figure"
+      filter_str = {"*.ofig", "Octave figure files"};
+
+    case {"octave", "matlab"}
+      filter_str = {"*.m; *.mat; *.ofig", "All Octave files"; ...
+      "*.m", "Editor files"; "*.mat", "Data files"; ...
+	    "*.ofig", "Octave figure files"};
+
+    case "editor"
+      filter_str = {"*.m", "Editor files"};
+
+    case "load"
+      filter_str = {"*.mat", "Data files"};
+
+    otherwise
+      ## The type entry is a file specifier. To allow the user to use '\'
+      ## replace it with '/'.
+      filter_str = type;
+      ## Need to check for the no-GUI flag.
+      check_arg2 = true;
+  endswitch
+
+  ## Use uigetfile with the selected filters.
+  ## If the second argument is true, don't open the GUI.
+  if (! check_arg2 || nargin < 2 || ! varargin {2})
+    [fname, fpath] = uigetfile (filter_str);
+  else
+    [fpath, fname, ext] = fileparts(filter_str);
+    fname = [fname ext];
+  endif
+
+  if (fname != 0)
+    ## Check for .mat files which need to use load(). Otherwise open()
+    ## can decide how to treat each file type.
+    [~, ~, ext] = fileparts (fname);
+    if (strcmpi (ext, "mat"))
+      load (fullfile (fpath, fname));
+    else
+      open (fullfile (fpath, fname));
+    endif
+  endif
+
+endfunction
+
+%!demo
+%! uiopen ("octave");
+
+## Remove from test statistics.  No real tests possible.
+%!assert (1)
+
diff -r 0411c5c50411 -r e62d81b969c8 scripts/gui/uisave.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/gui/uisave.m	Tue Oct 25 11:32:09 2022 +0100
@@ -0,0 +1,90 @@
+########################################################################
+##
+## Copyright (C) 2022 The Octave Project Developers
+##
+## See the file COPYRIGHT.md in the top-level directory of this
+## distribution or <https://octave.org/copyright/>.
+##
+## 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
+## <https://www.gnu.org/licenses/>.
+##
+########################################################################
+
+## -*- texinfo -*-
+## @deftypefn  {} {} uisave ()
+## @deftypefnx {} {} uisave (@var{vars})
+## @deftypefnx {} {} uisave (@var{vars}, @var{fname})
+##
+## Opens a GUI dialog for saving one or more workspace variable to a .mat file.
+##
+## With no arguments, it saves the whole workspace.
+##
+## @var{vars} specifies the workspace variable to save.
+## Example: @code{uisave ("x")} saves a single variable @code{x}.
+##
+## Use a cell array to save more than one variable.
+## Example: @code{uisave (@{"x", "y"@})}
+##
+## @var{fname} specifies the default file name to use.
+## Example: @code{uisave (@{"x", "y"@}, "file1")}
+##
+## @seealso{uiopen}
+## @end deftypefn
+
+function uisave (vars, fname)
+
+  if (nargin > 2)
+    error ("uisave: number of input arguments must be less than three");
+  endif
+
+  if (nargin < 1 || isempty (vars))
+    ## No variables specified. Get the names of all variables in the calling
+    ## workspace.
+    vars = evalin ("caller", "who;");
+  endif
+
+  if (nargin < 2)
+    filtstr = {"*.mat", "MAT-files"};
+  else
+    filtstr = fname;
+  endif
+
+  ## Use uiputfile for the dialog.
+  [fname, fpath] = uiputfile (filtstr);
+
+  ## If "cancel" button pressed then fname will be zero.
+  if (fname != 0)
+    ## The save command must be performed in the caller workspace, so build a
+    ## function string for use with evalin.
+    if (iscell (vars))
+      varStr = sprintf (" '%s',", vars{:});
+      ## Remove final comma.
+      varStr = varStr(1:end-1);
+    else
+      varStr = sprintf ("'%s'", vars);
+    endif
+    funcstr = sprintf ("save ('%s', %s);", fullfile (fpath, fname), varStr);
+    evalin ("caller", funcstr);
+  endif
+
+endfunction
+
+%!demo
+%! uisave ();
+
+## Remove from test statistics.  No real tests possible.
+%!assert (1)
+
