Mercurial > hg > octave-lyh
annotate scripts/plot/uigetfile.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | 75ff3db6a687 |
children | 3c6e8aaa9555 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2010-2011 Kai Habel |
11283 | 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}] =} uigetfile (@var{flt}) | |
21 ## @deftypefnx {Function File} {[@dots{}] =} uigetfile (@var{flt}, @var{dialog_name}, @var{default_file}) | |
22 ## @deftypefnx {Function File} {[@dots{}] =} uigetfile (@var{flt}, @var{dialog_name}) | |
23 ## @deftypefnx {Function File} {[@dots{}] =} uigetfile (@dots{},"Position",[@var{px}, @var{py}]) | |
24 ## @deftypefnx {Function File} {[@dots{}] =} uigetfile (@dots{},"Multiselect",@var{mode}) | |
25 ## | |
26 ## Open a GUI dialog to select a file. It returns the filename @var{fname}, | |
27 ## the path to this file @var{fpath} and the filter index @var{fltidx}. | |
28 ## @var{flt} contains a (list of) file filter string(s) in one of the following | |
29 ## formats: | |
30 ## | |
31 ## @table @code | |
32 ## @item "/path/to/filename.ext" | |
33 ## If a filename is given the file extension is extracted and used as filter. | |
34 ## In addtion the path is selected as current path and the filname is selected | |
35 ## as default file. | |
36 ## Example: uigetfile("myfun.m"); | |
37 ## | |
38 ## @item A single file extension "*.ext". | |
39 ## Example: uigetfile("*.ext"); | |
40 ## | |
41 ## @item A 2-column cell array, | |
42 ## containing the file extension in the 1st column and a brief description | |
43 ## in the 2nd column. | |
44 ## Example: uigetfile(@{"*.ext","My Description";"*.xyz","XYZ-Format"@}); | |
45 ## | |
46 ## The filter string can also contain a semicolon separated list of filter | |
47 ## extensions. | |
48 ## Example: uigetfile(@{"*.gif;*.png;*.jpg", "Supported Picture Formats"@}); | |
49 ## @end table | |
50 ## | |
51 ## @var{dialog_name} can be used to customize the dialog title. | |
52 ## If @var{default_file} is given it is selected in the GUI dialog. | |
53 ## If in addtion a path is given it is also used as current path. | |
54 ## | |
55 ## The screen position of the GUI dialog can be set by using the "Position" key | |
56 ## and a 2-valued vector containing the pixel coordinates. | |
57 ## Two or more files can be selected when setting the "Multiselect" key to "on". | |
58 ## In that case @var{fname} is a cell array containing the files. | |
59 ## @end deftypefn | |
60 | |
61 ## Author: Kai Habel | |
62 | |
63 function [retfile, retpath, retindex] = uigetfile (varargin) | |
64 | |
65 if (nargin <= 7) | |
66 | |
67 defaultvals = {"All Files(*)", #FLTK File Filter | |
68 "Open File?", #Dialog Title | |
11284
b9bc32327c4d
ChangeLog fix, set default directory to pwd for ui file functions
Kai Habel <kai.habel@gmx.de>
parents:
11283
diff
changeset
|
69 pwd, #FLTK default file name |
11283 | 70 [240, 120], #Dialog Position (pixel x/y) |
71 "off"}; #Multiselect on/off | |
72 | |
73 outargs = cell (5, 1); | |
74 for i = 1 : 5 | |
75 outargs{i} = defaultvals{i}; | |
76 endfor | |
77 | |
78 idx1 = idx2 = []; | |
79 if (length (varargin) > 0) | |
80 for i = 1 : length (varargin) | |
81 val = varargin{i}; | |
82 if (ischar (val)) | |
83 if (strncmp (tolower (val), "multiselect", 11)) | |
84 idx1 = i; | |
85 elseif (strncmp(tolower (val), "position", 8)) | |
86 idx2 = i; | |
87 endif | |
88 endif | |
89 endfor | |
90 endif | |
91 | |
92 stridx = [idx1, idx2, 0]; | |
93 if (length (stridx) > 1) | |
94 stridx = min (stridx(1 : end - 1)); | |
95 endif | |
96 | |
97 args = varargin; | |
98 if (stridx) | |
99 args = varargin(1 : stridx - 1); | |
100 endif | |
101 | |
102 len = length (args); | |
103 if (len > 0) | |
104 file_filter = args{1}; | |
105 outargs{1} = __fltk_file_filter__ (file_filter); | |
106 if (ischar (file_filter)) | |
107 outargs{3} = file_filter; | |
108 endif | |
109 endif | |
110 | |
111 if (len > 1) | |
112 outargs{2} = args{2}; | |
113 endif | |
114 | |
115 if (len > 2) | |
116 outargs{3} = args{3}; | |
117 endif | |
118 | |
119 if (stridx) | |
120 ## we have string arguments ("position" or "multiselect") | |
121 | |
122 ## check for even number of remaining arguments, prop/value pair(s) | |
123 if (rem (nargin - stridx + 1, 2)) | |
11295
75ff3db6a687
Simplify code for uimenu.m. Fix error messages for ui file functions.
Kai Habel <kai.habel@gmx.de>
parents:
11284
diff
changeset
|
124 error ("uigetfile: expecting property/value pairs"); |
11283 | 125 endif |
126 | |
127 for i = stridx : 2 : nargin | |
128 prop = varargin{i}; | |
129 val = varargin{i + 1}; | |
130 if (strncmp (tolower (prop), "position", 8)) | |
131 if (ismatrix (val) && length(val) == 2) | |
132 outargs{4} = val; | |
133 else | |
11295
75ff3db6a687
Simplify code for uimenu.m. Fix error messages for ui file functions.
Kai Habel <kai.habel@gmx.de>
parents:
11284
diff
changeset
|
134 error ("uigetfile: expecting 2-element vector for position argument") |
11283 | 135 endif |
136 elseif (strncmp (tolower (prop), "multiselect", 11)) | |
137 if (ischar (val)) | |
138 outargs{5} = tolower (val); | |
139 else | |
11295
75ff3db6a687
Simplify code for uimenu.m. Fix error messages for ui file functions.
Kai Habel <kai.habel@gmx.de>
parents:
11284
diff
changeset
|
140 error ("uigetfile: expecting string argument (on/off) for multiselect") |
11283 | 141 endif |
142 else | |
11295
75ff3db6a687
Simplify code for uimenu.m. Fix error messages for ui file functions.
Kai Habel <kai.habel@gmx.de>
parents:
11284
diff
changeset
|
143 error ("uigetfile: unknown argument"); |
11283 | 144 endif |
145 endfor | |
146 endif | |
147 else | |
11295
75ff3db6a687
Simplify code for uimenu.m. Fix error messages for ui file functions.
Kai Habel <kai.habel@gmx.de>
parents:
11284
diff
changeset
|
148 error ("uigetfile: number of input arguments must be less than eight"); |
11283 | 149 endif |
150 | |
151 if (any (cellfun(@(x)strcmp (x, "fltk"), available_backends))) | |
152 [retfile, retpath, retindex] = __fltk_uigetfile__ (outargs{:}); | |
153 else | |
11295
75ff3db6a687
Simplify code for uimenu.m. Fix error messages for ui file functions.
Kai Habel <kai.habel@gmx.de>
parents:
11284
diff
changeset
|
154 error ("uigetfile: fltk backend required."); |
11283 | 155 endif |
156 | |
157 | |
158 endfunction | |
159 | |
160 %!demo | |
161 %! uigetfile({"*.gif;*.png;*.jpg", "Supported Picture Formats"}) |