-
Notifications
You must be signed in to change notification settings - Fork 0
/
passdlg.m
339 lines (304 loc) · 8.99 KB
/
passdlg.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
function answer = passdlg(username, varargin)
% PASSDLG Create and open password dialog box
%
% PASSDLG(UITYPE) It will always produce one masked password field plus the
% optional components:
% - 'u' or 'UsernameField'
% - 'c' or 'ConfirmPass'
% - 's' or 'ShowHideCheckBox'
%
% UITYPE can be a cell array with above named fields or a string with
% the initial letters 'u', 'c' and 's', e.g. passdlg('cs').
%
% PASSDLG(..., Name, Value) Supports Name/Value pairs of <a href="matlab: doc figure-properties">figure properties</a>.
%
%
% ANSWER = ...
% Returns a structure with cellstring fields (by default {''})
% .User
% .Pass
%
% NOTE: pressing the button Cancel, Esc or closing the figure will
% return empty fields.
%
%
% Examples:
% % Simple password dialog
% out = passdlg();
%
% % Add username and password confirmation field
% out = passdlg('uc'); % or passdlg({'UsernameField','ConfirmPass'})
%
% See also: PASSFIELD, INPUTDLG
% Author: Oleg Komarov (oleg.komarov@hotmail.it)
% Tested on R2014a Win7 64bit
% 2014 Sep 21 - Created
uitype = 'us';
% Parse UI type
[hasUsernameField, hasConfirmPassword, hasShowCheckBox] = getUItype(uitype);
offset = (hasUsernameField + hasConfirmPassword)*40 + hasShowCheckBox*30 + ~hasShowCheckBox*10;
% Figure
fh = figure('DockControls' , 'off',...
'IntegerHandle' , 'off',...
'InvertHardcopy', 'off',...
'KeyPressFcn' , @kpf_figure, ...
'MenuBar' , 'none',...
'NumberTitle' , 'off',...
'Resize' , 'on',...
'UserData' , 'Cancel',...
'Visible' , 'off',...
'WindowStyle' , 'modal',...
'Name' , 'BrainSTEM credentials',...
'Position' , [0, 0, 175 83.6 + offset]);
% Params for resize
h.FigMinWidth = 250;
h.FigHeight = 83.6 + offset;
h.OkFromRight = h.FigMinWidth - 59;
h.CancelFromRight = h.FigMinWidth - 117;
% Axes (for text labels)
ah = axes('Parent',fh,'Position',[0 0 1 1],'Visible','off');
% Get some default properties
defaults = getDefaults;
% Preallocate edit handles
h.edit = {};
% Username field and label
if hasUsernameField
h.edit{end+1} = uicontrol(fh, ...
defaults.EdInfo , ...
'Max' ,1, ...
'Position' ,[5, 36.6 + offset, 165, 23]);
h.labeluser = text('Parent',ah, ...
defaults.TextInfo , ...
'Position' ,[5 59.6 + offset], ...
'String' ,'Username' , ...
'Interpreter','none');
offset = offset - 40;
end
% Password field
h.edit{end+1} = passfield('Parent',fh,'Position', [5, 36.6 + offset, 165, 23], 'BackgroundColor', [1,1,1]);
% Password label
h.labelpass = text('Parent',ah,...
defaults.TextInfo,...
'Position' ,[5 59.6 + offset],...
'String' , 'Password',...
'Interpreter','none');
% Confirm password
if hasConfirmPassword
offset = offset - 40;
h.edit{end+1} = passfield('Parent',fh,...
'Position' , [5, 36.6 + offset, 165, 23],...
'BackgroundColor', [1,1,1]);
h.labelpassconf = text('Parent',ah,...
defaults.TextInfo,...
'Position' ,[5 59.6 + offset],...
'String' , 'Confirm password',...
'Interpreter','none');
end
% Show/hide password checkbox
if hasShowCheckBox
offset = offset - 28;
h.cbshow = uicontrol(fh, ...
defaults.CbInfo,...
'Position',[5, 36.6 + offset, 165, 23],...
'String' , 'Show password',...
'Callback', {@clb_checkbox,h});
end
% OK button
h.button(1) = uicontrol(fh,...
defaults.BtnInfo , ...
'Position' ,[59, 5, 53, 26.6] , ...
'KeyPressFcn',@kpf_button, ...
'String' ,'OK',...
'UserData' ,'OK',...
'Callback' ,@clb_button);
% Cancel button
h.button(2) = uicontrol(fh,...
defaults.BtnInfo , ...
'Position' ,[117 5 53 26.6],...
'KeyPressFcn',@kpf_button,...
'String' ,'Cancel',...
'UserData' ,'Cancel',...
'Callback' ,@clb_button);
% fh.setDefaultButton(h.btnok);
% Add password check on confirmation field
if hasConfirmPassword
set(h.edit{end}, 'KeyPressFcn', {@kpf_confirmpass,h})
end
% Add resize function
set(fh,'ResizeFcn', {@doResize, h});
% Make it visible and apply additional settings
movegui(fh,'center')
if nargin > 1
set(fh, varargin{:});
end
set(fh,'Visible','on');
drawnow
h.edit{1}.String = username;
uicontrol(h.edit{1})
% Go into uiwait/modal
if ishghandle(fh), uiwait(fh); end
% Parse outputs
if ishghandle(fh)
[answer.User, answer.Pass] = deal({''});
if strcmp(get(fh,'UserData'),'OK'),
if hasUsernameField
answer.User = get(h.edit{1},{'String'});
end
answer.Pass = get(h.edit{end},{'Password'});
end
delete(fh);
else
[answer.User, answer.Pass] = deal({''});
end
drawnow
end
function kpf_figure(obj, evd)
switch(evd.Key)
case {'return','space'}
uiresume(gcbf);
case {'escape'}
delete(gcbf);
end
end
function kpf_button(obj, evd)
switch(evd.Key)
case {'return'}
if ~strcmp(get(obj,'UserData'),'Cancel')
set(gcbf,'UserData','OK');
uiresume(gcbf);
else
delete(gcbf)
end
case 'escape'
delete(gcbf)
end
end
function kpf_confirmpass(obj,evd,varargin)
Data = varargin{1};
passedit = Data.edit{end-1};
if numel(obj.Password) >= numel(passedit.Password) || all(obj.BorderColor == [0 1 0])
if strcmp(obj.Password, passedit.Password)
green = [0 1 0];
obj.BorderColor = green;
passedit.BorderColor = green;
set(Data.button(1), 'Enable','on');
else
red = [1 0 0];
obj.BorderColor = red;
passedit.BorderColor = red;
set(Data.button(1), 'Enable','off');
end
end
end
function clb_button(obj, evd)
if ~strcmp(get(obj,'UserData'),'Cancel')
set(gcbf,'UserData','OK');
uiresume(gcbf);
else
delete(gcbf)
end
end
% Show/hide password
function clb_checkbox(obj,evd,varargin)
Data = varargin{1};
isChecked = get(obj,'Value');
for ii = 1:numel(Data.edit)
editbox = Data.edit{ii};
if isa(editbox,'passfield')
if isChecked
editbox.show;
else
editbox.hide;
end
end
end
end
% Horizontal resize
function doResize(fh, evd, varargin)
Data = varargin{1};
resetPos = false;
FigPos = get(fh,'Position');
FigWidth = FigPos(3);
FigHeight = FigPos(4);
% Keep min width
widthDiff = Data.FigMinWidth - FigWidth;
if widthDiff >= 1
FigWidth = Data.FigMinWidth;
FigPos(3) = Data.FigMinWidth;
resetPos = true;
end
% Resize edit fields
for ii = 1:length(Data.edit)
EditPos = get(Data.edit{ii},'Position');
EditPos(3) = FigWidth - 10;
set(Data.edit{ii},'Position',EditPos);
end
% Reposition buttons
ButtonPos = get(Data.button(1),'Position');
ButtonPos(1) = FigWidth - Data.OkFromRight;
set(Data.button(1),'Position',ButtonPos);
ButtonPos = get(Data.button(2),'Position');
ButtonPos(1) = FigWidth - Data.CancelFromRight;
set(Data.button(2),'Position',ButtonPos);
% Keep height fixed
heightDiff = abs(FigHeight - Data.FigHeight);
if heightDiff >= 1
FigPos(4) = Data.FigHeight;
resetPos = true;
end
if resetPos, set(fh,'Position',FigPos); end
end
% Parse arguments to main function
function [u, c, s] = getUItype(type)
ucs = false(1,3);
if iscellstr(type)
validoptions = {'UsernameField','ConfirmPass','ShowHideCheckBox'};
for ii = 1:numel(type)
n = numel(type{ii});
tmp = strncmpi(type{ii}, validoptions, n);
if ~any(tmp)
warning('passdlg:unrecognizeType','Unrecognized option ''%s''.',type{ii})
continue
end
ucs = ucs | tmp;
if all(ucs), break, end
end
elseif ischar(type) && isrow(type)
ucs = any(bsxfun(@eq, 'ucs',type'),1);
end
u = ucs(1);
c = ucs(2);
s = ucs(3);
end
% Retrieve hardcoded defaults
function s = getDefaults
s.FigColor = get(0,'DefaultFigureColor');
s.TextInfo = struct('Units','pixels',...
'FontSize' ,8,...
'FontWeight' ,'normal',...
'HorizontalAlignment' ,'left',...
'HandleVisibility' ,'callback',...
'VerticalAlignment' , 'bottom',...
'BackgroundColor' ,s.FigColor);
s.BtnInfo = struct('Units', 'pixels',...
'FontSize' ,8,...
'FontWeight' ,'normal',...
'HorizontalAlignment' ,'center',...
'HandleVisibility' ,'callback',...
'Style' ,'pushbutton',...
'BackgroundColor' , s.FigColor);
s.EdInfo = struct('Units', 'pixels',...
'FontSize' ,8,...
'FontWeight' ,'normal',...
'HorizontalAlignment' ,'left',...
'HandleVisibility' ,'callback',...
'Style' ,'edit',...
'BackgroundColor' ,[1,1,1]);
s.CbInfo = struct('Units', 'pixels',...
'FontSize' ,8,...
'FontWeight' ,'normal',...
'HorizontalAlignment' ,'left',...
'HandleVisibility' ,'callback',...
'Style' ,'checkbox',...
'BackgroundColor' , s.FigColor);
end