-
Notifications
You must be signed in to change notification settings - Fork 0
/
logindlg.m
276 lines (247 loc) · 9.73 KB
/
logindlg.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
% LOGINDLG Dialog for visually secure login.
% Examples:
% [login password] = logindlg('Title','Login Title'); % Returns the login and
% password with the dialog title 'Login Title'.
%
% password = logindlg; % Returns only the password with a default
% dialog title.
%
% password = logindlg('Password','only'); % Displays only a password
% edit box and returns the password.
%
% password = logindlg('Title','Login Title','Password','only');
% % Creates a password only dialog with 'Login Title' as the
% dialog title.
%
%
%
%
% Author: Jeremy Smith
% Date: September 24, 2005
% Last Edit: January 28, 2010
% Version: 1.3
% Tested on: Matlab 7.0.4.365 (R14) Service Pack 2, Matlab 7.1 SP 3, and
% Matlab 7.4.0.287 (R2007a)
% Description: custom login dialog because Matlab doesn't have an option
% for characters in an edit field to be replaced by asterisks
% (password security).
%
% Note: On very slow computers the first few password characters may
% have a delay before they are converted to asterisks.
% Changelist:
% 1.3: -Pressing the Escape key with focus anywhere on the dialog will
% now cancel the dialog box
% -Typo correction
% -Escape no longer creates an empty character in the password field
% 1.2: -Tab no longer triggers the OK button in the password box
% -Improved the script help
% -Removed horizontal alignment from buttons
% -Added the option to display only the password box
% 1.1: -Added positioning code so it'll display in the center of the screen
% -If only one output is specified the password will be returned
% instead of the login as in Version 1.0
% -Escape will not only close the dialog if neither edit box is active
% -When the dialog appears the first edit box will be active
% -Added a few more comments
% -Removed the clc, it was left in by mistake in Version 1.0
%
% Copyright (c) 2010, Jeremy Smith
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * 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 OWNER 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 [varargout]=logindlg(varargin)
% Number of inputs check
if nargin == 0 || nargin == 2 || nargin == 4
else
error('Incorrect number of input arguments.')
end
% Input Type Check
for i=1:1:length(varargin)
if ~ischar(varargin{i})
error('Inputs must be strings.')
end
end
% Title Option
if nargin == 0
Title = 'Login';
elseif nargin == 2 && ~isempty(strmatch('title',lower(varargin)))
Title = varargin{2};
elseif nargin == 2 && isempty(strmatch('title',lower(varargin)))
Title = 'Login';
elseif nargin == 4 && ~isempty(strmatch('title',lower(varargin)))
S = strmatch('title',lower(varargin));
if S == 1
Title = varargin{2};
elseif S == 3
Title = varargin{4};
else
error('Invalid title.')
end
else
error('Invalid title.')
end
% Password Option
if nargin == 0
Pass = 0;
elseif nargin == 2 && ~isempty(strmatch('password',lower(varargin{1}))) && ~isempty(strmatch('only',lower(varargin{2})))
Pass = 1;
elseif nargin == 4 && ~isempty(strmatch('password',lower(varargin))) && ~isempty(strmatch('only',lower(varargin)))
P = strmatch('password',lower(varargin));
O = strmatch('only',lower(varargin));
if P == 1 && O == 2
Pass = 1;
elseif P == 3 && O == 4
Pass = 1;
end
elseif nargin == 2 && isempty(strmatch('password',lower(varargin))) == 1
Pass = 0;
else
error('Invalid password option.')
end
% Output Error Check
if nargout > 1 && Pass == 1 || nargout > 2
error('Too many output arguments.')
end
% Get Properties
Color = get(0,'DefaultUicontrolBackgroundcolor');
% Determine the size and position of the login interface
if Pass == 0
Height = 9.5;
else
Height = 5.5;
end
set(0,'Units','characters')
Screen = get(0,'screensize');
Position = [Screen(3)/2-17.5 Screen(4)/2-4.75 35 Height];
set(0,'Units','pixels')
% Create the GUI
gui.main = dialog('HandleVisibility','on',...
'IntegerHandle','off',...
'Menubar','none',...
'NumberTitle','off',...
'Name','Login',...
'Tag','logindlg',...
'Color',Color,...
'Units','characters',...
'Userdata','logindlg',...
'Position',Position);
% Set the title
if ischar(Title) == 1
set(gui.main,'Name',Title,'Closerequestfcn',{@Cancel,gui.main},'Keypressfcn',{@Escape})
end
% Texts
if Pass == 0
gui.login_text = uicontrol(gui.main,'Style','text','FontSize',8,'HorizontalAlign','left','Units','characters','String','Login','Position',[1 7.65 20 1]);
end
gui.password_text = uicontrol(gui.main,'Style','text','FontSize',8,'HorizontalAlign','left','Units','characters','String','Password','Position',[1 4.15 20 1]);
% Edits
if Pass == 0
gui.edit1 = uicontrol(gui.main,'Style','edit','FontSize',8,'HorizontalAlign','left','BackgroundColor','white','Units','characters','String','','Position',[1 6.02 33 1.7],'KeyPressfcn',{@Escape});
end
gui.edit2 = uicontrol(gui.main,'Style','edit','FontSize',8,'HorizontalAlign','left','BackgroundColor','white','Units','characters','String','','Position',[1 2.52 33 1.7],'KeyPressfcn',{@KeyPress_Function,gui.main},'Userdata','');
% Buttons
gui.OK = uicontrol(gui.main,'Style','push','FontSize',8,'Units','characters','String','OK','Position',[12 .2 10 1.7],'Callback',{@OK,gui.main},'KeyPressfcn',{@Escape});
gui.Cancel = uicontrol(gui.main,'Style','push','FontSize',8,'Units','characters','String','Cancel','Position',[23 .2 10 1.7],'Callback',{@Cancel,gui.main},'KeyPressfcn',{@Escape});
setappdata(0,'logindlg',gui) % Save handle data
setappdata(gui.main,'Check',0) % Error check setup. If Check remains 0 an empty cell array will be returned
if Pass == 0
uicontrol(gui.edit1) % Make the first edit box active
else
uicontrol(gui.edit2) % Make the second edit box active if the first isn't present
end
% Pause the GUI and wait for a button to be pressed
uiwait(gui.main)
Check = getappdata(gui.main,'Check'); % Check to see if a button was pressed
% Format output
if Check == 1
if Pass == 0
Login = get(gui.edit1,'String');
end
Password = get(gui.edit2,'Userdata');
if nargout == 1 % If only one output specified output Password
varargout(1) = {Password};
elseif nargout == 2 % If two outputs specified output both Login and Password
varargout(1) = {Login};
varargout(2) = {Password};
end
else % If OK wasn't pressed output nothing
if nargout == 1
varargout(1) = {[]};
elseif nargout == 2
varargout(1) = {[]};
varargout(2) = {[]};
end
end
delete(gui.main) % Close the GUI
setappdata(0,'logindlg',[]) % Erase handles from memory
%% Hide Password
function KeyPress_Function(h,eventdata,fig)
% Function to replace all characters in the password edit box with
% asterisks
password = get(h,'Userdata');
key = get(fig,'currentkey');
switch key
case 'backspace'
password = password(1:end-1); % Delete the last character in the password
case 'delete'
password = password(1:end-1);
case 'return' % This cannot be done through callback without making tab to the same thing
gui = getappdata(0,'logindlg');
OK([],[],gui.main);
case 'tab' % Avoid tab triggering the OK button
gui = getappdata(0,'logindlg');
uicontrol(gui.OK);
case 'escape'
% Close the login dialog
Escape(fig,[])
otherwise
password = [password get(fig,'currentcharacter')]; % Add the typed character to the password
end
SizePass = size(password); % Find the number of asterisks
if SizePass(2) > 0
asterisk(1,1:SizePass(2)) = '*'; % Create a string of asterisks the same size as the password
set(h,'String',asterisk) % Set the text in the password edit box to the asterisk string
else
set(h,'String','')
end
set(h,'Userdata',password) % Store the password in its current state
%% Cancel
function Cancel(h,eventdata,fig)
uiresume(fig)
%% OK
function OK(h,eventdata,fig)
% Set the check and resume
setappdata(fig,'Check',1)
uiresume(fig)
%% Escape
function Escape(h,eventdata)
% Close the login if the escape button is pushed and neither edit box is
% active
fig = h;
while ~strcmp(get(fig,'Type'),'figure')
fig = get(fig,'Parent');
end
key = get(fig,'currentkey');
if isempty(strfind(key,'escape')) == 0
Cancel([],[],fig)
end