Home > m > mysql > logindlg.m

logindlg

PURPOSE ^

LOGINDLG Dialog for visually secure login.

SYNOPSIS ^

function [varargout]=logindlg(varargin)

DESCRIPTION ^

 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.




 Author: Jeremy Smith
 Date: September 24, 2005
 Last Edit: July 7, 2006
 Version: 1.2
 Tested on: Matlab 7.0.4.365 (R14) Service Pack 2 and Matlab 7.1 SP 3
 Description: custom login dialog because Matlab doesn't have an option
       for characters in an edit field to be replaced by asterixes
       (password security)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % LOGINDLG   Dialog for visually secure login.
0002 %   Examples:
0003 %       [login password] = logindlg('Title','Login Title');  % Returns the login and
0004 %          password with the dialog title 'Login Title'.
0005 %
0006 %       password = logindlg;  % Returns only the password with a default
0007 %          dialog title.
0008 %
0009 %       password = logindlg('Password','only');  % Displays only a password
0010 %          edit box and returns the password.
0011 %
0012 %
0013 %
0014 %
0015 % Author: Jeremy Smith
0016 % Date: September 24, 2005
0017 % Last Edit: July 7, 2006
0018 % Version: 1.2
0019 % Tested on: Matlab 7.0.4.365 (R14) Service Pack 2 and Matlab 7.1 SP 3
0020 % Description: custom login dialog because Matlab doesn't have an option
0021 %       for characters in an edit field to be replaced by asterixes
0022 %       (password security)
0023 
0024 % Changelist:
0025 %   1.2: -Tab no longer triggers the OK button in the password box
0026 %        -Improved the script help
0027 %        -Removed horizontal alignment from buttons
0028 %        -Added the option to display only the password box
0029 %   1.1: -Added positioning code so it'll display in the center of the screen
0030 %        -If only one output is specified the password will be returned
0031 %            instead of the login as in Version 1.0
0032 %        -Escape will not only close the dialog if neither edit box is active
0033 %        -When the dialog appears the first edit box will be active
0034 %        -Added a few more comments
0035 %        -Removed the clc, it was left in by mistake in Version 1.0
0036 
0037 function [varargout]=logindlg(varargin)
0038 
0039 % Number of inputs check
0040 if nargin ==  0 || nargin == 2 || nargin == 4
0041 else
0042     error('Incorrect number of input arguments.')
0043 end
0044 
0045 % Input Type Check
0046 if ~isempty(find(cellfun(@ischar,varargin) == 0))
0047     error('Inputs must be strings.')
0048 end
0049 
0050 % Title Option
0051 if nargin == 0
0052     Title = 'Login';
0053 elseif nargin == 2 && ~isempty(strmatch('title',lower(varargin)))
0054     Title = varargin{2};
0055 elseif nargin == 2 && isempty(strmatch('title',lower(varargin)))
0056     Title = 'Login';
0057 elseif nargin == 4 && ~isempty(strmatch('title',lower(varargin)))
0058     S = strmatch('title',lower(varargin));
0059     if S == 1
0060         Title = varargin{2};
0061     elseif S == 3
0062         Title = varargin{4};
0063     else
0064         error('Invalid title.')
0065     end
0066 else
0067     error('Invalid title.')
0068 end
0069 
0070 % Password Option
0071 if nargin == 0
0072     Pass = 0;
0073 elseif nargin == 2 && ~isempty(strmatch('password',lower(varargin{1}))) && ~isempty(strmatch('only',lower(varargin{2})))
0074     Pass = 1;
0075 elseif nargin == 4 && ~isempty(strmatch('password',lower(varargin))) && ~isempty(strmatch('only',lower(varargin)))
0076     P = strmatch('password',lower(varargin));
0077     O = strmatch('only',lower(varargin));
0078     if P == 1 && O == 2
0079         Pass = 1;
0080     elseif P == 3 && O == 4
0081         Pass = 1;
0082     end
0083 elseif nargin == 2 && isempty(strmatch('password',lower(varargin))) == 1
0084     Pass = 0;
0085 else
0086     error('Invalid password option.')
0087 end
0088 
0089 % Output Error Check
0090 if nargout > 1 && Pass == 1 || nargout > 2
0091     error('Too many output arguments.')
0092 end
0093 
0094 % Get Properties
0095 Color = [240 240 240]/255; %get(0,'DefaultUicontrolBackgroundcolor');
0096 
0097 % Determine the size and position of the login interface
0098 if Pass == 0
0099     Height = 9.5;
0100 else
0101     Height = 5.5;
0102 end
0103 set(0,'Units','characters')
0104 Screen = get(0,'screensize');
0105 Position = [Screen(3)/2-17.5 Screen(4)/2-4.75 35 Height];
0106 set(0,'Units','pixels')
0107 
0108 % Create the GUI
0109 gui.main = dialog('HandleVisibility','on',...
0110     'IntegerHandle','off',...
0111     'Menubar','none',...
0112     'NumberTitle','off',...
0113     'Name','Login',...
0114     'Tag','logindlg',...
0115     'Color',Color,...
0116     'Units','characters',...
0117     'Userdata','logindlg',...
0118     'Position',Position);
0119 
0120 % Set the title
0121 if ischar(Title) == 1
0122     set(gui.main,'Name',Title,'Closerequestfcn',{@Cancel,gui.main},'Keypressfcn',{@Escape,gui.main})
0123 end
0124 
0125 FONTSIZE = 12;
0126 
0127 % Texts
0128 if Pass == 0
0129     gui.login_text = uicontrol(gui.main,'Style','text','BackgroundColor', Color, 'FontSize',FONTSIZE,'HorizontalAlign','left','Units','characters','String','Login','Position',[1 7.65 20 1]);
0130 end
0131 gui.password_text = uicontrol(gui.main,'Style','text','BackgroundColor', Color, 'FontSize',FONTSIZE,'HorizontalAlign','left','Units','characters','String','Password','Position',[1 4.15 20 1]);
0132 
0133 % Edits
0134 if Pass == 0
0135     gui.edit1 = uicontrol(gui.main,'Style','edit','FontSize',FONTSIZE,'HorizontalAlign','left','BackgroundColor','white','Units','characters','String','','Position',[1 6.02 33 1.7]);
0136 end
0137 gui.edit2 = uicontrol(gui.main,'Style','edit','FontSize',FONTSIZE,'HorizontalAlign','left','BackgroundColor','white','Units','characters','String','','Position',[1 2.52 33 1.7],'KeyPressfcn',{@KeyPress_Function,gui.main},'Userdata','');
0138 
0139 % Buttons
0140 gui.OK = uicontrol(gui.main,'Style','push','BackgroundColor', Color, 'FontSize',FONTSIZE,'Units','characters','String','OK','Position',[12 .2 10 1.7],'Callback',{@OK,gui.main});
0141 gui.Cancel = uicontrol(gui.main,'Style','push','BackgroundColor', Color, 'FontSize',FONTSIZE,'Units','characters','String','Cancel','Position',[23 .2 10 1.7],'Callback',{@Cancel,gui.main});
0142 
0143 setappdata(0,'logindlg',gui) % Save handle data
0144 setappdata(gui.main,'Check',0) % Error check setup. If Check remains 0 an empty cell array will be returned
0145 
0146 if Pass == 0
0147     uicontrol(gui.edit1) % Make the first edit box active
0148 else
0149     uicontrol(gui.edit2)  % Make the second edit box active if the first isn't present
0150 end
0151 
0152 % Pause the GUI and wait for a button to be pressed
0153 uiwait(gui.main)
0154 
0155 Check = getappdata(gui.main,'Check'); % Check to see if a button was pressed
0156 
0157 % Format output
0158 if Check == 1
0159     if Pass == 0
0160         Login = get(gui.edit1,'String');
0161     end
0162     Password = get(gui.edit2,'Userdata');
0163     
0164     if nargout == 1 % If only one output specified output Password
0165         varargout(1) = {Password};
0166     elseif nargout == 2 % If two outputs specified output both Login and Password
0167         varargout(1) = {Login};
0168         varargout(2) = {Password};
0169     end
0170 else % If OK wasn't pressed output nothing
0171     if nargout == 1
0172         varargout(1) = {[]};
0173     elseif nargout == 2
0174         varargout(1) = {[]};
0175         varargout(2) = {[]};
0176     end
0177 end
0178 
0179 delete(gui.main) % Close the GUI
0180 setappdata(0,'logindlg',[]) % Erase handles from memory
0181 
0182 %% Hide Password
0183 function KeyPress_Function(h,eventdata,fig)
0184 % Function to replace all characters in the password edit box with
0185 % asterixes
0186 password = get(h,'Userdata');
0187 key = get(fig,'currentkey');
0188 
0189 switch key
0190     case 'backspace'
0191         password = password(1:end-1); % Delete the last character in the password
0192     case 'return'  % This cannot be done through callback without making tab to the same thing
0193         gui = getappdata(0,'logindlg');
0194         OK([],[],gui.main);
0195     case 'tab'  % Avoid tab triggering the OK button
0196         gui = getappdata(0,'logindlg');
0197         uicontrol(gui.OK);
0198     otherwise
0199         password = [password get(fig,'currentcharacter')]; % Add the typed character to the password
0200 end
0201 
0202 SizePass = size(password); % Find the number of asterixes
0203 if SizePass(2) > 0
0204     asterix(1,1:SizePass(2)) = '*'; % Create a string of asterixes the same size as the password
0205     set(h,'String',asterix) % Set the text in the password edit box to the asterix string
0206 else
0207     set(h,'String','')
0208 end
0209 
0210 set(h,'Userdata',password) % Store the password in its current state
0211 
0212 %% Cancel
0213 function Cancel(h,eventdata,fig)
0214 uiresume(fig)
0215 
0216 %% OK
0217 function OK(h,eventdata,fig)
0218 % Set the check and resume
0219 setappdata(fig,'Check',1)
0220 uiresume(fig)
0221 
0222 %% Escape
0223 function Escape(h,eventdata,fig)
0224 % Close the login if the escape button is pushed and neither edit box is
0225 % active
0226 key = get(fig,'currentkey');
0227 
0228 if isempty(strfind(key,'escape')) == 0 && h == fig
0229     Cancel([],[],fig)
0230 end

Generated on Mon 03-Sep-2007 12:12:34 by m2html © 2003