-
Notifications
You must be signed in to change notification settings - Fork 247
/
inputgui.m
310 lines (292 loc) · 14.2 KB
/
inputgui.m
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
% INPUTGUI - A comprehensive gui automatic builder. This function helps
% to create GUI very quickly without bothering about the
% positions of the elements. After creating a geometry,
% elements just place themselves in the predefined
% locations. It is especially useful for figures in which
% you intend to put text buttons and descriptions.
%
% Usage:
% >> [ outparam ] = inputgui( 'key1', 'val1', 'key2', 'val2', ... );
% >> [ outparam userdat strhalt outstruct tags] = ...
% inputgui( 'key1', 'val1', 'key2', 'val2', ... );
%
% Inputs:
% 'geom' - cell array of cell array of integer vector. Each cell
% array defines the coordinate of a given input in the
% following manner: { nb_row nb_col [x_topcorner y_topcorner]
% [x_bottomcorner y_bottomcorner] };
% 'geometry' - cell array describing horizontal geometry. This corresponds
% to the supergui function input 'geomhoriz'
% 'geomvert' - vertical geometry argument, this argument is passed on to
% the supergui function
% 'uilist' - list of uicontrol lists describing elements properties
% { { ui1 }, { ui2 }... }, { 'uiX' } being GUI matlab
% uicontrol arguments such as { 'style', 'radiobutton',
% 'String', 'hello' }. See Matlab function UICONTROL for details.
% 'helpcom' - optional help command
% 'helpbut' - text for help button (default is "Help")
% 'okbut' - text for OK button (default is "OK")
% 'title' - optional figure title
% 'userdata' - optional userdata input for the figure
% 'mode' - ['normal'|'noclose'|'plot' fignumber]. Either wait for
% user to press OK or CANCEL ('normal'), return without
% closing window input ('noclose'), only draw the gui ('plot')
% or process an existing window which number is given as
% input (fignumber). Default is 'normal'.
% 'eval' - [string] command to evaluate at the end of the creation
% of the GUI but before waiting for user input.
% 'screenpos' - see supergui.m help message.
% 'skipline' - ['on'|'off'] skip a row before the "OK" and "Cancel"
% button. Default is 'on'.
%
% Output:
% outparam - list of outputs. The function scans all lines and
% add up an output for each interactive uicontrol, i.e
% edit box, radio button, checkbox and listbox.
% userdat - 'userdata' value of the figure.
% strhalt - the function returns when the 'userdata' field of the
% button with the tag 'ok' is modified. This returns the
% new value of this field.
% outstruct - returns outputs as a structure (only tagged ui controls
% are considered). The field name of the structure is
% the tag of the ui and contain the ui value or string.
% instruct - returns inputs provided in the same format as 'outstruct'
% This allow to compare in/outputs more easy.
% tags - uicontrols by tags
%
% Note: the function also adds three buttons at the bottom of each
% interactive windows: 'CANCEL', 'HELP' (if callback command
% is provided) and 'OK'.
%
% Example:
% res = inputgui('geometry', { 1 1 }, 'uilist', ...
% { { 'style' 'text' 'string' 'Enter a value' } ...
% { 'style' 'edit' 'string' '' } });
%
% res = inputgui('geom', { {2 1 [0 0] [1 1]} {2 1 [1 0] [1 1]} }, 'uilist', ...
% { { 'style' 'text' 'string' 'Enter a value' } ...
% { 'style' 'edit' 'string' '' } });
%
% Author: Arnaud Delorme, CNL / Salk Institute, La Jolla, 1 Feb 2002
%
% See also: SUPERGUI, EEGLAB
% Copyright (C) Arnaud Delorme, CNL/Salk Institute, 27 Jan 2002, arno@salk.edu
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
function [result, userdat, strhalt, resstruct, instruct, alltags] = inputgui( varargin);
result = [];
userdat = [];
strhalt = [];
resstruct = [];
if nargin < 2
help inputgui;
return;
end
% decoding input and backward compatibility
% -----------------------------------------
if ischar(varargin{1})
options = varargin;
else
options = { 'geometry' 'uilist' 'helpcom' 'title' 'userdata' 'mode' 'geomvert' };
options = { options{1:length(varargin)}; varargin{:} };
options = options(:)';
end
% checking inputs
% ---------------
g = finputcheck(options, { 'geom' 'cell' [] {}; ...
'geometry' {'cell','integer'} [] []; ...
'uilist' 'cell' [] {}; ...
'cancel' 'string' [] 'Cancel'; ...
'helpcom' { 'string','cell' } { [] [] } ''; ...
'title' 'string' [] ''; ...
'eval' 'string' [] ''; ...
'helpbut' 'string' [] 'Help'; ...
'okbut' 'string' [] 'OK'; ...
'skipline' 'string' { 'on' 'off' } 'on'; ...
'addbuttons' 'string' { 'on' 'off' } 'on'; ...
'userdata' '' [] []; ...
'getresult' 'real' [] []; ...
'minwidth' 'real' [] 200; ...
'screenpos' '' [] []; ...
'mode' '' [] 'normal'; ...
'geomvert' 'real' [] [] ...
}, 'inputgui');
if ischar(g), error(g); end
if isempty(g.getresult)
if ischar(g.mode)
fig = figure('visible', 'off');
set(fig, 'name', g.title);
set(fig, 'userdata', g.userdata);
if ~iscell( g.geometry )
oldgeom = g.geometry;
g.geometry = {};
for row = 1:length(oldgeom)
g.geometry = { g.geometry{:} ones(1, oldgeom(row)) };
end
end
% skip a line
if strcmpi(g.skipline, 'on')
g.geometry = { g.geometry{:} [1] };
if ~isempty(g.geom)
for ind = 1:length(g.geom)
g.geom{ind}{2} = g.geom{ind}{2}+1; % add one row
end
g.geom = { g.geom{:} {1 g.geom{1}{2} [0 g.geom{1}{2}-2] [1 1] } };
end
g.uilist = { g.uilist{:}, {} };
end
% add buttons
if strcmpi(g.addbuttons, 'on')
g.geometry = { g.geometry{:} [1 1 1 1] };
if ~isempty(g.geom)
for ind = 1:length(g.geom)
g.geom{ind}{2} = g.geom{ind}{2}+1; % add one row
end
g.geom = { g.geom{:} ...
{4 g.geom{1}{2} [0 g.geom{1}{2}-1] [1 1] }, ...
{4 g.geom{1}{2} [1 g.geom{1}{2}-1] [1 1] }, ...
{4 g.geom{1}{2} [2 g.geom{1}{2}-1] [1 1] }, ...
{4 g.geom{1}{2} [3 g.geom{1}{2}-1] [1 1] } };
end
if ~isempty(g.helpcom)
if ~iscell(g.helpcom)
g.uilist = { g.uilist{:}, { 'width' 80 'align' 'left' 'Style', 'pushbutton', 'string', g.helpbut, 'tag', 'help', 'callback', g.helpcom } {} };
else
g.uilist = { g.uilist{:}, { 'width' 80 'align' 'left' 'Style', 'pushbutton', 'string', 'Help gui', 'callback', g.helpcom{1} } };
g.uilist = { g.uilist{:}, { 'width' 80 'align' 'left' 'Style', 'pushbutton', 'string', 'More help', 'callback', g.helpcom{2} } };
end
else
g.uilist = { g.uilist{:}, {} {} };
end
g.uilist = { g.uilist{:}, { 'width' 80 'align' 'right' 'Style', 'pushbutton', 'string', g.cancel, 'tag' 'cancel' 'callback', 'close(gcbf)' } };
g.uilist = { g.uilist{:}, { 'width' 80 'align' 'right' 'stickto' 'on' 'Style', 'pushbutton', 'tag', 'ok', 'string', g.okbut, 'callback', 'set(gcbo, ''userdata'', ''retuninginputui'');' } };
end
% add the three buttons (CANCEL HELP OK) at the bottom of the GUI
% ---------------------------------------------------------------
if ~isempty(g.geom)
[~, ~, allobj, alltags] = supergui( 'fig', fig, 'minwidth', g.minwidth, 'geom', g.geom, 'uilist', g.uilist, 'screenpos', g.screenpos);
elseif isempty(g.geomvert)
[~, ~, allobj, alltags] = supergui( 'fig', fig, 'minwidth', g.minwidth, 'geomhoriz', g.geometry, 'uilist', g.uilist, 'screenpos', g.screenpos);
else
if strcmpi(g.skipline, 'on'), g.geomvert = [g.geomvert(:)' 1]; end
if strcmpi(g.addbuttons, 'on'),g.geomvert = [g.geomvert(:)' 1]; end
[~, ~, allobj, alltags] = supergui( 'fig', fig, 'minwidth', g.minwidth, 'geomhoriz', g.geometry, 'uilist', g.uilist, 'screenpos', g.screenpos, 'geomvert', g.geomvert(:)' );
end
else
fig = g.mode;
set(findobj('parent', fig, 'tag', g.okbut), 'userdata', []);
allobj = findobj('parent',fig);
allobj = allobj(end:-1:1);
end
% evaluate command before waiting?
% --------------------------------
if ~isempty(g.eval), eval(g.eval); end
instruct = outstruct(allobj); % Getting default values in the GUI.
% create figure and wait for return
% ---------------------------------
if ischar(g.mode) && (strcmpi(g.mode, 'plot') || strcmpi(g.mode, 'return') )
if strcmpi(g.mode, 'plot')
return; % only plot and returns
end
else
waitfor( findobj('parent', fig, 'tag', 'ok'), 'userdata');
end
else
fig = g.getresult;
allobj = findobj('parent',fig);
allobj = allobj(end:-1:1);
end
result = {};
userdat = [];
strhalt = '';
resstruct = [];
if ~(ishandle(fig)), return; end % Check if figure still exist
% output parameters
% -----------------
strhalt = get(findobj('parent', fig, 'tag', g.okbut), 'userdata');
[resstruct,result] = outstruct(allobj); % Output parameters
userdat = get(fig, 'userdata');
if isempty(g.getresult) && ischar(g.mode) && ( strcmp(g.mode, 'normal') || strcmp(g.mode, 'return') )
close(fig);
end
drawnow; % for windows
% function for gui res (deprecated)
% --------------------
% function g = myguihandles(fig, g)
% h = findobj('parent', fig);
% if ~isempty(get(h(index), 'tag'))
% try,
% switch get(h(index), 'style')
% case 'edit', g = setfield(g, get(h(index), 'tag'), get(h(index), 'string'));
% case { 'value' 'radio' 'checkbox' 'listbox' 'popupmenu' 'radiobutton' }, ...
% g = setfield(g, get(h(index), 'tag'), get(h(index), 'value'));
% end
% catch, end
% end
function [resstructout, resultout] = outstruct(allobj)
counter = 1;
resultout = {};
resstructout = [];
for index=1:length(allobj)
if iscell(allobj), currentobj = allobj{index};
else currentobj = allobj(index);
end
if isnumeric(currentobj) || ~isprop(currentobj,'GetPropertySpecification') % To allow new object handles
try
objstyle = get(currentobj, 'style');
switch lower( objstyle )
case { 'listbox', 'checkbox', 'radiobutton' 'popupmenu' 'radio' }
resultout{counter} = get( currentobj, 'value');
if ~isempty(get(currentobj, 'tag')),
try
resstructout = setfield(resstructout, get(currentobj, 'tag'), resultout{counter});
catch
fprintf('Warning: tag "%" may not be use as field in output structure', get(currentobj, 'tag'));
end
end
counter = counter+1;
case 'edit'
resultout{counter} = get( currentobj, 'string');
if ~isempty(get(currentobj, 'tag')),
try
resstructout = setfield(resstructout, get(currentobj, 'tag'), resultout{counter});
catch
fprintf('Warning: tag "%" may not be use as field in output structure', get(currentobj, 'tag'));
end
end
counter = counter+1;
end
catch, end
else
ps = currentobj.GetPropertySpecification;
resultout{counter} = arg_tovals(ps,false);
count = 1;
while isfield(resstructout, ['propgrid' int2str(count)])
count = count + 1;
end
resstructout = setfield(resstructout, ['propgrid' int2str(count)], arg_tovals(ps,false));
end
end