]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/uiputfile.m
update packages
[CreaPhase.git] / octave_packages / m / plot / uiputfile.m
1 ## Copyright (C) 2010-2012 Kai Habel
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} {[@var{fname}, @var{fpath}, @var{fltidx}] =} uiputfile ()
21 ## @deftypefnx {Function File} {[@var{fname}, @var{fpath}, @var{fltidx}] =} uiputfile (@var{flt})
22 ## @deftypefnx {Function File} {[@var{fname}, @var{fpath}, @var{fltidx}] =} uiputfile (@var{flt}, @var{dialog_name})
23 ## @deftypefnx {Function File} {[@var{fname}, @var{fpath}, @var{fltidx}] =} uiputfile (@var{flt}, @var{dialog_name}, @var{default_file})
24 ## Open a GUI dialog for selecting a file.  @var{flt} contains a (list of) file
25 ## filter string(s) in one of the following formats:
26 ##
27 ## @table @code
28 ## @item "/path/to/filename.ext"
29 ## If a filename is given the file extension is
30 ## extracted and used as filter.
31 ## In addition the path is selected as current path and the filename is selected
32 ## as default file.
33 ## Example: uiputfile("myfun.m");
34 ##
35 ## @item "*.ext"
36 ## A single file extension.
37 ## Example: uiputfile("*.ext");
38 ##
39 ## @item @{"*.ext","My Description"@}
40 ## A 2-column cell array containing the file extension in the 1st column and
41 ## a brief description in the 2nd column.
42 ## Example: uiputfile(@{"*.ext","My Description";"*.xyz","XYZ-Format"@});
43 ## @end table
44 ##
45 ## The filter string can also contain a semicolon separated list of filter
46 ## extensions.
47 ## Example: uiputfile(@{"*.gif;*.png;*.jpg", "Supported Picture Formats"@});
48 ##
49 ## @var{dialog_name} can be used to customize the dialog title.
50 ## If @var{default_file} is given it is preselected in the GUI dialog.
51 ## If, in addition, a path is given it is also used as current path.
52 ## @end deftypefn
53
54 ## Author: Kai Habel
55
56 function [retfile, retpath, retindex] = uiputfile (varargin)
57
58   defaulttoolkit = get (0, "defaultfigure__graphics_toolkit__");
59   funcname = ["__uiputfile_", defaulttoolkit, "__"];
60   functype = exist (funcname);
61   if (! __is_function__ (funcname))
62     funcname = "__uiputfile_fltk__";
63     if (! __is_function__ (funcname))
64       error ("uiputfile: fltk graphics toolkit required");
65     elseif (! strcmp (defaulttoolkit, "gnuplot"))
66       warning ("uiputfile: no implementation for toolkit `%s', using `fltk' instead",
67                defaulttoolkit);
68     endif
69   endif
70
71   if (nargin > 3)
72     print_usage ();
73   endif
74
75   defaultvals = {cell(0, 2),     # File Filter
76                  "Save File",    # Dialog Title
77                  "",             # Default file name
78                  [240, 120],     # Dialog Position (pixel x/y)
79                  "create",
80                  pwd};           # Default directory
81
82   outargs = cell(6, 1);
83   for i = 1 : 6
84     outargs{i} = defaultvals{i};
85   endfor
86
87   if (nargin > 0)
88     file_filter = varargin{1};
89     [outargs{1}, outargs{3}, defdir] = __file_filter__ (file_filter);
90     if (length (defdir) > 0)
91       outargs{6} = defdir;
92     endif
93   else
94     outargs{1} = __file_filter__ (outargs{1});
95   endif
96
97   if (nargin > 1)
98     if (ischar (varargin{2}))
99       outargs{2} = varargin{2};
100     elseif (! isempty (varargin{2}))
101       print_usage ();
102     endif
103   endif
104
105   if (nargin > 2)
106     if (ischar (varargin{3}))
107       [fdir, fname, fext] = fileparts (varargin{3});
108       if (! isempty (fdir))
109         outargs{6} = fdir;
110       endif
111       if (! isempty (fname) || ! isempty (fext))
112         outargs{3} = strcat (fname, fext);
113       endif
114     elseif (! isempty (varargin{3}))
115       print_usage ();
116     endif
117   endif
118
119   [retfile, retpath, retindex] = feval (funcname, outargs{:});
120
121 endfunction
122
123 %!demo
124 %! uiputfile({"*.gif;*.png;*.jpg", "Supported Picture Formats"})
125
126 ## Remove from test statistics.  No real tests possible.
127 %!test
128 %! assert (1);