I needed a simple login dialog from my main program, and decided to try WxPerl. Its mostly derived from Jouke's Tutorial examples, but I needed to figure out how to add a couple of other things to it. I require this module only if needed, since I know in advance some user/passwords, and the module portion of the code is installed somewhere in @INC as LoginDialog.pm.
TBD: I gather from mailing archives that since I'm setting the focus on a text control before the window is opened, that it might be better to set it in an EVT_UPDATE_UI handler (though it works as-is for me), but I'm not quite sure how to go about that...help is appreciated :-)
Updated w/PodMaster's comments as I understand them. The change in SetFocus and use of EVT_UPDATE_UI became necesssary after the change from a Frame to a Dialog. Also, it's interesting that I don't even have to call Wx's MainLoop now...
Update: Agghhh, EVT_UPDATE_UI broke it. I just don't understand how to use that event handler yet...
Update: I think it works now...they only other thing I'd like to do is replace that $done flag in the code with something so that the handler uninstalls itself...watch this space...
Changed EVT_UPDATE_UI to EVT_ACTIVATE. The former handler was being called over one hundred times, while the latter is only called ~3 times. Still it'd be nice if it were possible to have a handler uninstall itself...not sure if that's possible.
Final update(!): Found out how to disconnect event handler, but had to change back to EVT_UPDATE_UI, as the
focus wouldn't get set in EVT_ACTIVATE if I disconnected it while executing the handler.
#!/usr/bin/perl
#
# For simple login dialog
# See example at bottom of file
#
use strict;
use Wx;
###########################################################
#
# Extend the Frame class to our needs
#
package Wx::Perl::LoginDialog;
use Wx qw( wxTE_PASSWORD wxTE_PROCESS_ENTER );
use Wx::Event qw( EVT_BUTTON EVT_TEXT_ENTER EVT_UPDATE_UI );
use base qw/Wx::Dialog/;
sub new {
my $class = shift;
my $user = shift;
my $passwd = shift;
my $self = $class->SUPER::new(@_);
$self->{user_out} = $user;
$self->{passwd_out} = $passwd;
$self->{UserLabel} = Wx::StaticText->new(
$self, # parent
-1, # id
"User:", # label
[10, 30] # position
);
$self->{PasswdLabel} = Wx::StaticText->new(
$self, # parent
-1, # id
"Password:",# label
[10, 50] # position
);
$self->{User} = Wx::TextCtrl->new(
$self,
-1,
${$self->{user_out}} || "",
[70,30],
[70,20],
wxTE_PROCESS_ENTER,
);
$self->{Passwd} = Wx::TextCtrl->new(
$self,
2,
${$self->{passwd_out}} || "",
[70,50],
[70,20],
wxTE_PASSWORD | wxTE_PROCESS_ENTER,
);
$self->{Login} = Wx::Button->new(
$self,
1,
"Login",
[20,90],
);
$self->{Cancel} = Wx::Button->new(
$self,
2,
"Cancel",
[90,90],
);
EVT_UPDATE_UI(
$self,
-1,
sub {
$self->{Passwd}->SetFocus if $$user;
EVT_UPDATE_UI($self, -1, undef);
}
);
EVT_BUTTON(
$self, # Object to bind to
1, # ButtonID
\&Login
);
EVT_BUTTON(
$self, # Object to bind to
2, # ButtonID
\&CancelLogin # Subroutine to execute
);
EVT_TEXT_ENTER(
$self,
-1,
\&Login
);
$self->{Passwd}->SetFocus if $$user;
return $self;
}
sub Login {
my $self = shift;
${$self->{user_out}} = $self->{User}->GetValue;
${$self->{passwd_out}} = $self->{Passwd}->GetValue;
$self->EndModal(1);
}
sub CancelLogin {
my $self = shift;
$self->EndModal(0);
}
###########################################################
#
package Wx::Perl::LoginWindow;
use base qw(Wx::App); # Inherit from Wx::App
use Wx qw(wxCAPTION wxSYSTEM_MENU);
our ($user, $passwd, $ok);
sub BindVars {
my $self = shift;
($user, $passwd, $ok) = @_;
$self;
}
sub OnInit {
my $self = shift;
$$ok = Wx::Perl::LoginDialog->new(
$user,
$passwd,
undef, # Parent window
-1, # Window id
'Login', # Title
[200,200], # position X, Y
[200,150], # size X, Y
wxCAPTION | wxSYSTEM_MENU
)->ShowModal;
0;
}
package LoginDialog;
sub get_login {
shift;
my $ok;
my $app = eval { Wx::Perl::LoginWindow->BindVars(@_, \$ok)->new };
die $@ unless $@ =~ "OnInit must return a true return value";
$ok;
}
1;
###########################################################
#
# The main program
#
package main;
unless( caller ){
if( @ARGV ) {
LoginDialog->get_login(\my ($user, $passwd));
die "USER $user\n\nPASS $passwd\n\n";
} else { # Use defaults if available:
my ($user, $passwd) = @ARGV;
# Default either one if desired
$user = 'username';
unless ($user and $passwd) {
# require LoginDialog;
LoginDialog->get_login(\($user, $passwd));
}
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.