##########################################
#
# ALPHA TEST VERSION
# bsw 27/7/00
#
##########################################
package Utilities;
my $FONT2 		= '-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1';

# Deskpro fix
#my $FONT 		= '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1';
my $FONT                = '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1';


my $DICTIONARY		= {};

sub new {
my ($pkg)  = @_ ;
my $setup 	= {};

bless $setup,$pkg;

return $setup;
}



sub CreateWindow {
my ($pkg, $RefMW, $RefWAIT) = @_;

$$RefMW->geometry("+100+100");

#warn "WM_DELETE_WINDOW is Active at ";
#$$RefMW->protocol('WM_DELETE_WINDOW', 	sub {# Do nothing
#							});


my $ImagePath = "$ENV{'BADGER'}/lib/dodo";
$ImagePath = "/nfs/disk65/bsw/images";

#$ImagePath = '/nfs/teams_home/team77/MIDAS/IMAGES';
my $BannerLabel = $$RefMW->Label()->grid(-row => 1, -column => 1, -columnspan => 2, -sticky => 'we');

#$BannerLabel->configure(-image => $BannerLabel->Photo(-file => "$ImagePath/dodo.gif"));
	
	
########################
# WaitBox For Queries
########################	

$pkg->{WaitBox}  = $$RefMW->WaitBox( -title => 'Please Wait');	
$pkg->{WaitBox}->wm('minsize', 120, 30);
$pkg->{WaitBox}->wm('maxsize', 120, 30);
$pkg->{WaitBox}->geometry("+100+50");

}

sub BrowseActivate {
my ($pkg, $BrowseWidget) = @_;


$$BrowseWidget->configure(
		-state 			=> 'readonly');


$$BrowseWidget->Subwidget("entry")->Subwidget("entry")->configure(
		-bg			=> 'lemonchiffon1');

}

sub BrowseDisable {
my ($pkg, $BrowseWidget) = @_;


$$BrowseWidget->configure(
		-state 			=> 'disabled');


$$BrowseWidget->Subwidget("entry")->Subwidget("entry")->configure(
		-bg			=> 'grey90');

}

sub LabEntryActivate {
my ($pkg, $EntryWidget) = @_;


$$EntryWidget->configure(
		-state 			=> 'normal');


$$EntryWidget->Subwidget("entry")->configure(
		-bg			=> 'lemonchiffon1');
$$EntryWidget->Subwidget("entry")->focus();
$$EntryWidget->Subwidget("entry")->icursor('end');
}

sub LabEntryDisable {
my ($pkg, $EntryWidget) = @_;



$$EntryWidget->Subwidget("entry")->configure(
		-state 			=> 'disabled',
		-bg			=> 'grey90');
}

sub ButtonActivate {
my ($pkg, $ButtonWidget) = @_;


$$ButtonWidget->configure(
		-state 			=> 'normal');


}


sub ButtonDisable {
my ($pkg, $ButtonWidget) = @_;


$$ButtonWidget->configure(
		-state 			=> 'disabled');


}









sub BrowseEntry {
my ($pkg, $Window, $BrowseWidget, $LabelName, $Variable, $State, $BG) = @_;
my $pad = '                ';

if(!$BG){$BG = 'lemonchiffon1';}

# There is a bug in the BrowseEntry widget which causes a grab to be lost when it looses focus
# God, Tk is wonderful !!!!!

if(!$State){$State = 'normal'}

if($State =~ /disabled/){$BG = 'grey90';}



$LabelName = substr($LabelName.$pad,0,16);



$$BrowseWidget = $$Window->BrowseEntry(	
		-width 			=> 20,
		-fg 			=> 'blue1',
		-listcmd		=> sub{},
		-browsecmd 		=> sub{},
		-state 			=> $State,
		-variable 		=> $Variable);
		
$$BrowseWidget->Subwidget("slistbox")->Subwidget("listbox")->configure(	
		-font 			=> $FONT,
		-fg 			=> 'blue1');


$$BrowseWidget->Subwidget("entry")->Subwidget("entry")->configure(
		-fg			=> 'darkgreen',
		-font 			=> $FONT,
		-bg			=> $BG);

$$BrowseWidget->Subwidget("entry")->Subwidget("label")->configure(		
		-text 			=> $LabelName,
		-font 			=> $FONT,
		-fg			=> 'blue1');

}


sub LabEntry {
my ($pkg, $Window, $EntryWidget, $LabelName, $Variable, $Width, $State) = @_;
my $bg = 'grey90';

if(!$State)
{
	$State 	= 'normal';
	$bg 	= 'lemonchiffon2';
};

if(!$Width){$Width = 25;}




my $pad = '                ';
$LabelName = substr($LabelName.$pad,0,16);

$$EntryWidget = $$Window->LabEntry(
		-label			=> $LabelName,
		-textvariable		=> $Variable,
		-relief 		=> 'groove',
		-font			=> $FONT,
		-state			=> $State,
		-labelPack		=> [ -side => 'left'],
		-width 			=> $Width);


$$EntryWidget->Subwidget("label")->configure(
		-fg 			=> 'blue1', 
		-font			=> $FONT),
		-anchor			=> 'e';

$$EntryWidget->Subwidget("entry")->configure(
		-bg 			=> $bg);
		
$$EntryWidget->Subwidget("entry")->focus();
$$EntryWidget->Subwidget("entry")->icursor(1);
}

sub Button {
my ($pkg, $Window, $ButtonWidget, $LabelName, $State, $BG) = @_;

if(!$State){$State = "normal";}
if(!$BG){$BG = "blue";}


$$ButtonWidget = $$Window->Button(			
		-text 			=> $LabelName,
		-width			=> 10, 
		-state			=> $State,
		-bg			=> $BG,
		-fg			=> 'lemonchiffon2',
		-activebackground	=> 'lemonchiffon2',
		-activeforeground	=> $BG,
		-font 			=> $FONT2);	
						
}

sub Dictionary {
my ($pkg, $dictionary, $data, $append) = @_;

if($data->[0])
{

if(!$append){$DICTIONARY->{$dictionary} = {};}

my $order = 1;
foreach(@$data)
{
	# Remove trailing whitespace.
	$_->[1] =~ s/\s*$//g;
	$DICTIONARY->{$dictionary}->{$_->[1]} = $_->[0];
	
	
	$ORDER->{$dictionary}->{$order} = $_->[1];
	$order++;
}

}
else
{
	$dictionary =~ s/\s*$//g;
	my @keys = sort (keys %{$ORDER->{$dictionary}}) ;
	
	
	
	my $range = scalar @keys;
	undef @keys;
	for (1..$range)
	{	
		push @keys, $ORDER->{$dictionary}->{$_};
	}
	
	
	
	
	
	return @keys;
}

}


sub Frame {
my ($pkg, $RefFrame, $RefWindow, $Row, $Col, $RowSpan,$ColSpan) = @_;


if(!$Row){	$Row 		= 1;}
if(!$Col){	$Col 		= 1;}
if(!$RowSpan){	$RowSpan	= 1;}
if(!$ColSpan){	$ColSpan 	= 1;}



$$RefFrame = $$RefWindow->Frame(
	-relief 	=> 'ridge',
	-borderwidth 	=> 2,
	anchor		=> 'center'
	)->grid(	
	-row 		=> $Row,
	-column 	=> $Col,
	-columnspan	=> $ColSpan,
	-rowspan	=> $RowSpan,
	-sticky		=> 'nswe',
	-pady		=> 4);

}






sub DictionaryID {
my ($pkg, $dictionary, $label) = @_;

$dictionary =~ s/\s*$//g;

if(!$label){$label = 'ANY'};

if($label !~ /^ANY/)
{
	$label =~ s/\s*$//g;
	return $DICTIONARY->{$dictionary}->{$label};
}
else
{
	my @values = sort (values %{$DICTIONARY->{$dictionary}});	
	my $text;
	foreach(@values){$text = "$text,$_";}
	return substr($text,1);
	
}

}


sub Message {
my ($pkg, $Window, $Message) = @_;
my $chk;
my $ButtonExitWidget;
my $MessageWindow = $$Window->Toplevel;

$Message =~ s/\t{2,}//g;
$Message =~ s/^ //g;
$Message =~ s/\t/  /g;

$MessageWindow->geometry("+200+400");

$MessageWindow->title("Message");
								
my $MessageWidget = $MessageWindow->Scrolled( 		'ROText',
					-scrollbars		=> 'se',
					-width 			=> 80,
					-fg 			=> 'black',
					-state			=> 'normal',
					-tabs			=> [qw/1.3i/],
					-bg 			=> 'grey85',
					-selectborderwidth	=> 0,
					-selectforeground 	=> 'black',
					-selectbackground 	=> 'green',
					-font			=> $FONT,
					-height 		=> 12,
					-wrap			=> 'word',
					-borderwidth 		=> 0
				)->grid(-row 			=> 3,
					-column 		=> 1,
					-columnspan		=> 3,
					-sticky			=> 'we');
					
$MessageWidget->insert('end',$Message);

				

$pkg->Button(\$MessageWindow, \$ButtonExitWidget, "Exit",'','red');
$ButtonExitWidget->grid(		-row 			=> 4,
					-column 		=> 3,
					-sticky			=> 'ns');
					
$ButtonExitWidget->configure(		-command 	=> sub{	
					$chk = 1;
					$MessageWindow->grabRelease();
					$MessageWindow->destroy();
								}
				);
$MessageWindow->grab();
$MessageWindow->waitVariable(\$chk);
}


sub VerificationWindow {
my ($pkg, $Window, $message) = @_;
my $NewOrdersNote;
my $chk;
my $ToplevelNotesWidget;
my $ButtonExitWidget;
my $ButtonPrintWidget;

$ToplevelNotesWidget = $$Window->Toplevel;
$ToplevelNotesWidget->title("Plate Verfication Window");
$ToplevelNotesWidget->geometry("+80+200");


						
my $history = $ToplevelNotesWidget->Scrolled( 		'ROText',
					-scrollbars		=> 'se',
					-width 			=> 130,
					-fg 			=> 'red',
					-state			=> 'normal',
					-tabs			=> [qw/1.3i/],
					-bg 			=> 'grey90',
					-selectborderwidth	=> 0,
					-selectforeground 	=> 'black',
					-selectbackground 	=> 'green',
					-font			=> $FONT,
					-height 		=> 12,
					-borderwidth 		=> 0
				)->grid(-row 			=> 3,
					-column 		=> 1,
					-columnspan		=> 4,
					-sticky			=> 'we');

$history->tagConfigure('RED', 		-foreground => 'red');
$history->tagConfigure('ORANGE', 	-foreground => 'tomato');
$history->tagConfigure('YELLOW', 	-foreground => 'lemonchiffon1');
$history->tagConfigure('BLUE', 		-foreground => 'blue');
$history->tagConfigure('GREEN', 	-foreground => 'darkgreen');
my $CurrentPlate;
my $pad = '                    ';

foreach(@{$message})
{
	my $Colour = shift @{$_};
	
	
	foreach $col (@{$_})
	{
		$col = $col.$pad;
		$col = substr($col,0,18);
		$history->insert('end',"$col | ",$Colour);
	}
	$history->insert('end',"\n",$Colour);
	
	
}
if(!$message->[0][0]){$history->insert('end',"\n\nAll Wells In Plates Good\n\n",'PLATE');}


$history->see('0.0');


$pkg->Button(\$ToplevelNotesWidget, \$ButtonPrintWidget, "Print");
$ButtonPrintWidget->grid(		-row 		=> 4,
					-column 	=> 2,
					-padx		=> 200,
					-pady		=> 5);
					
$ButtonPrintWidget->configure(		-command 	=> sub{	
					eval {
					$pkg->PrintVerification(\$history);
					};if($@){$pkg->Message(\$ToplevelNotesWidget,"Error : Print Verification\n$@");}
								}
				);

$pkg->Button(\$ToplevelNotesWidget, \$ButtonExitWidget, "Exit",'','red');
$ButtonExitWidget->grid(		-row 		=> 4,
					-column 	=> 3,
					-padx		=> 40,
					-pady		=> 5);
					
$ButtonExitWidget->configure(		-command 	=> sub{	
					$chk = 1;
					$ToplevelNotesWidget->grabRelease();
					$ToplevelNotesWidget->destroy();
								}
				);



								
$ToplevelNotesWidget->grab();					
$ToplevelNotesWidget->waitVariable(\$chk);

}

sub PrintVerification {
my ($pkg, $TextWidget) = @_;
my $printer 	= $ENV{'PRINTER'};


my $line = 0;
my $Page = 1;

if(!$printer)
{
	die "\n\nYour Printer Environment Variable Has Not Been Set\nContact Bug If You Don't Know How To Set This Variable\n\n\UUntil This Is Set You Will Not be Able To Print\n";
}

my $y = `lpq -P $printer`;

if($y =~ /no spool directory/)
{	
	die "\n\nInValid Printer $printer\n";
}

my $P = $$TextWidget->get('0.0','end');


open(OUT,"| genscript -T Platetrack -P$printer  --title \'Platetrack\'")|| die "Print Verification::Cannot open pipe to printer\n";
print OUT "\n--------------- Plate Verification ----------------\n";
print OUT "$P";

close OUT;

}

sub AccessMessage {
my ($pkg, $RefMainWidget, $RefFrameWidget, $DefaultMessage, $DefaultBG) = @_;

$pkg->{AccessDefault} 	= $DefaultMessage;
$pkg->{MainWindow} 	= $$RefMainWidget;
$pkg->{BackGround} 	= $DefaultBG;

if(!$pkg->{BackGround}){$pkg->{BackGround} = 'darkgreen';}

$pkg->{AccessWidget} 	= $$RefFrameWidget->Label(		
		-width			=> 78,
		-font			=> $FONT2,
		-fg			=> 'lemonchiffon2'
)->grid(	-row 			=> 1,
		-column 		=> 1,
		-sticky			=> 'we');	

}


sub UpdateAccessMessage {
my ($pkg,$message,$bg) = @_; 
my $idle;

if(!$message)	
{
	$message 	= $pkg->{AccessDefault};
}
else
{
	$idle = 1;
}

if(!$bg)	{$bg 		= $pkg->{BackGround};}


$pkg ->{AccessWidget}->configure(
		-bg			=> $bg,
		-text			=> $message);

if($idle){$pkg->{MainWindow}->update('idletasks');}

}
1;
