]> Creatis software - CreaPhase.git/blob - octave_packages/zenity-0.5.7/zenity_file_selection.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / zenity-0.5.7 / zenity_file_selection.m
1 ## Copyright (C) 2006 Søren Hauberg
2 ## 
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ## 
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ## 
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn  {Function File} zenity_file_selection(@var{title}, @var{option1}, ...)
18 ## Opens a file selection dialog.
19 ## The variable @var{title} sets the title of the file selection window.
20 ## The optional string arguments can be
21 ## @table @samp
22 ## @item save
23 ## The file selection dialog is a dialog for saving files.
24 ## @item multiple
25 ## It is possible to select multiple files.
26 ## @item directory
27 ## It is possible to select directories as well as files.
28 ## @item Anything else
29 ## The argument will be the default selected file.
30 ## @end table
31 ## and @code{error}.
32 ##
33 ## @seealso{zenity_calendar, zenity_list, zenity_progress, zenity_entry, zenity_message,
34 ## zenity_text_info, zenity_notification}
35 ## @end deftypefn
36
37 function files = zenity_file_selection(title, varargin)
38   
39   save = multiple = directory = filename = title = "";
40   if (nargin == 0 || isempty(title)), title = "Select a file"; endif
41   for i = 1:length(varargin)
42     option = varargin{i};
43     isc = ischar(option);
44     if (isc && strcmpi(option, "save"))
45       save = "--save";
46     elseif (isc && strcmpi(option, "multiple"))
47       multiple = "--multiple";
48     elseif (isc && strcmpi(option, "directory"))
49       directory = "--directory";
50     elseif (isc)
51       filename = sprintf('--filename="%s"', varargin{i});
52     else
53       error("zenity_file_selection: unsupported option");
54     endif
55   endfor
56   
57   cmd = sprintf('zenity --file-selection --title="%s" --separator=":" %s %s %s %s', ...
58                  title, save, multiple, directory, filename);
59   [status, output] = system(cmd);
60   if (status == 0)
61     if (length(output) > 0 && output(end) == "\n")
62       output = output(1:end-1);
63     endif
64     idx = strfind(output, ":");
65     idx = [0, idx, length(output)+1];
66     l = length(idx);
67     if (l == 2)
68       files = output;
69     else
70       files = cell(1, l-1);
71       for i = 1:l-1
72         files{i} = output((idx(i)+1):(idx(i+1)-1));
73       endfor
74     endif
75   elseif (status == 1)
76     files = "";
77   else
78     error("zenity_file_selection: %s", output);
79   endif
80 endfunction