[29a0f3]: inst / zenity_file_selection.m  Maximize  Restore  History

Download this file

178 lines (168 with data), 7.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
## Copyright (C) 2006 Sřren Hauberg <soren@hauberg.org>
## Copyright (C) 2010, 2012 Carnë Draug <carandraug+dev@gmail.com>
##
## This program 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.
##
## This program 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
## this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {[@var{files} @var{status}] =} zenity_file_selection (@var{param1}, @var{value1}, @dots{})
## Opens a file selection dialog using Zenity.
##
## @var{files} is a string or a cell array of strings depending on whether the
## function has been set to allow selection of multiple files or directories.
##
## @var{status} will be @code{0} if user pressed @option{OK} (and selected
## something), @code{1} if closed the window without selecting something, and
## @code{5} if timeout has been reached (and therefore no file was selected).
##
## All @var{parameters} are optional, but if given, may require a corresponding
## @var{value}. All possible parameters are:
##
## @table @samp
## @item directory
## Activates directory-only selection. No value is required.
##
## @item filename
## Sets the default selected file. Requires a string as value.
##
## @item filter
## Sets a filename filter. Requires a string as value. Only one filter can be set
## but it can have multiple regex. Filters can also be named which blocks the user
## from actually seeing the filter.
## @example
## @group
## ## only txt files will be selectable
## zenity_file_selection ("filter", "*.txt")
## ## both txt and m files will be selectable
## zenity_file_selection ("filter", "*.txt *.m")
## ## both txt and m files will be selectable but the users will see `text files'
## zenity_file_selection ("filter", "Text files | *.txt *.m")
## @end group
## @end example
##
## @item icon
## Sets the icon of the window. Requires a string as value with the file path to
## an image, or one of the four stock icons:
##
## @table @samp
## @item error
## @item info
## @item question
## @item warning
## @end table
##
## @item multiple
## Allows selection of multiple files. No value is required. @var{files} will
## hold a cell array, even if user selects only one or no file.
##
## @item save
## The file selection dialog is a dialog for saving files. No value is required.
##
## @item timeout
## Sets the time in seconds after which the dialog is closed. Requires a scalar
## as value.
##
## @item title
## Sets the title of the window. Requires a string as value.
##
## @item height
## Sets the height of the dialog window. Requires a scalar as value.
##
## @item width
## Sets the width of the dialog window. Requires a scalar as value.
## @end table
##
## @strong{Note:} ultimately, the availability of some parameters is dependent
## on the user's system preferences and zenity version.
##
## @seealso{zenity_list, zenity_entry, zenity_message, zenity_text_info}
## @end deftypefn
## TODO
## for some time (but only on the development version, never released version) it
## was possible to set multiple filters filter with this function (see the help text created below).
## This was when using a manual parsing of the options. Once using @inputParser
## this was no longer working. Maybe when inputParser allows for an option to be
## used multiple times... all the code is ready for this, just uncomment some lines
## @item filter
## Sets a filename filter. Requires a string as value. Multiple filters can be
## set with multiple calls of this parameter, or one filter can be made with
## multiple regexp. Filters can also be named which blocks the user from actualy
## seeing the filter.. The following examples shows how to create two filters,
## two named filters, one filter for two different extensions, and the same
## filter but named:
## @example
## @code{zenity_file_selection ("filter", "*.txt", "filter", "*.m")}
## @code{zenity_file_selection ("filter", "text files| *.txt", "filter", "octave files| *.m")}
## @code{zenity_file_selection ("filter", "*.tif *.png")}
## @code{zenity_file_selection ("filter", "Images | *.tif *.png")}
## @end example
function [files, status] = zenity_file_selection(varargin)
## Update figures so they are show before the dialog. To not be shown at this
## step, turn them off with 'figure(N, "visible", "off")
## This is similar to the functions input and pause
drawnow;
options = zenity_options ("file selection", varargin);
if ( !isempty(options.save) && (!isempty(options.multiple) || !isempty(options.directory)) )
error ("Parameter 'save' cannot be set together with 'multiple' or directory'.");
endif
# The separator is set to "/" because it's an invalid character for filenames
# in most filesystems and so, unlikely to exist in the middle of filenames.
# It's also the fileseparator so filenames will always already start with a
# '/' which is good since we can look for double '//' as separator for filepaths
pre_cmd = sprintf("%s ", ...
options.directory, ...
options.filename, ...
options.height, ...
options.multiple, ...
options.save, ...
options.timeout, ...
options.title, ...
options.width, ...
options.filter, ...
options.icon);
cmd = sprintf("zenity --file-selection --separator=\"/\" %s", pre_cmd);
[status, output] = system(cmd);
# Exit code -1 = An unexpected error has occurred
# Exit code 0 = The user has pressed either OK or Close.
# Exit code 1 = The user has either pressed Cancel, or used the window
# functions to close the dialog
# Exit code 5 = The dialog has been closed because the timeout has been reached
if (status == 0)
if (output(end) == "\n")
output = output(1:end-1);
endif
# With 'multiple', always place the output in a cell array, even if only one
# file is selected.
if (options.multiple)
idx = strfind(output, "//");
if (idx)
files = cell(length(idx)+1, 1);
idx = [0, idx, length(output)+1];
for i = 1 : (length(idx)-1)
files{i} = output( idx(i)+1 : idx(i+1)-1 );
endfor
else
files = cell(1);
files{1} = output;
endif
else
files = output;
endif
elseif (options.multiple && (status == 1 || status == 5) )
files = cell(1);
elseif (status == 1 || status == 5)
files = "";
else
error("An unexpected error occurred with exit code '%i' and output '%s'",...
status, output);
endif
endfunction