#!/usr/local/bin/perl5.6.1 -w
# $Id: UserLogginV.pm,v 1.1 2003/10/21 15:42:03 jx1 Exp $

package UserLogginV;

use strict;
use warnings;
use Carp qw(croak cluck carp);
use Data::Dumper;
use Class::Struct;
use Tk;

my $setup = { title_f       => "*-times-bold-r-normal--*-180-*-*-p-*-*-*",
	      entry_f       => "-adobe-helvetica-medium-r-normal--17-120-100-100-p-88-iso8859-1",
	      button_f      => "-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1",
	      menu_f        => "-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1",
	      label_f       => "-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1",
	      active_entry  => "pale green",
	      disabled_entry=> "grey85",
	      other_entry   => "white",
	      frame_bordrw  => '5',
	  };


unless( caller )
{
    use UserLogginV;

    my $mw = MainWindow -> new;
#    my $usrbox = UserLogginV->new( $mw );

    my $usrbox = UserLogginV->new( );
    $usrbox -> init( $mw );

    $usrbox->{UserBox} -> pack ( -side => 'top',
				     -expand => 1, 
				     -fill => 'x');

    print "$usrbox->{UserId}\n";

    MainLoop;    
}


struct UserLogginV =>
{
    UserId  => '$',
    UserBox => '$',
};

sub init #new
{
#    my $invocant = shift;
#    my $class = ref($invocant) || $invocant;

#    my $self = { UserId  => '',
#		 UserBox => '',
#	     };

#    bless $self;

    my $self = shift;

    my $parentw = shift;

    my $userid;
    my $user_frame = $parentw -> Frame( -borderwidth => $setup->{frame_bordrw},
					-label => "User ID  ",
					-labelFont => $setup->{button_f},
					-labelPack => [ -side => 'left'] );

    my $userEntry = $user_frame->Entry ( -width            => 15,
					 -font             => $setup->{entry_f},
					 -textvariable     => \$userid 
				       )->pack(-side => 'left');

    # when $userEntry get focus
    #$userEntry -> bind ( "<Enter>" => \&usr_gotFocus ); # through mouse cursor
    $userEntry -> bind ( "<FocusIn>" => \&usr_gotFocus ); # through kb action
    

    $userEntry -> bind ("<Return>" => [\&usr_finishEntry, $self] );

    # when $userEntry loose focus
    #$userEntry -> bind ( "<Leave>" => [\&usr_finishEntry, $self] );  # through mouse cursor
    $userEntry -> bind ( "<FocusOut>" => [\&usr_finishEntry, $self] );  # through kb action
    

    my $logout_button = $user_frame->Button( -font             => $setup->{button_f},
					     -fg               => 'purple',
					     -activebackground => 'black',
					     -activeforeground => 'white',   
					     -text             => "Logout",
					     -command          => [\&logout, $userEntry]
					   ) -> pack(-side => 'right');


    $self->{UserBox} = $user_frame;
    #bless $self;

    return $self;
}


####    callback procedures    ####

# for logout button
sub logout
{
    my $userEntry = shift;

    # the order of the next two statement seems important
    $userEntry -> configure( -background => $setup->{other_entry}, 
			     -state      => 'normal' );
    $userEntry -> delete (0, 'end');
    
    return;
}

sub usr_gotFocus
{
    my $userEntry = shift;
    $userEntry -> configure( -background => $setup->{active_entry},
			     -state      => 'normal' );
    $userEntry -> grab();
    
    return;
}

sub usr_finishEntry
{ 
    my $userEntry = shift;
    my $self = shift;

    $self->{UsrId} = $userEntry->get();

    $userEntry -> grabRelease();
    $userEntry -> configure( -background => $setup->{disabled_entry},		      
			     -state      => 'disabled' );

    # just to make $userEntry loose focus ...    
    $userEntry -> parent() -> focus();  

    return;
} 


1; # keep perl happy
