
package Utilities;

sub new {
my $pkg  = shift;
my $setup = {};

$setup->{Mainwin}	= '';

bless $setup,$pkg;
return $setup;
}

sub Mainwindow {
my $pkg = shift;
my $w = shift;
if($w)
{
	$pkg->{Mainwin} = $$w;
}
else
{
	return $pkg->{Mainwin};
}
}




sub Radiocheck {
my ($pkg,$chk, @checks) = @_;
my $win = $pkg->{Mainwin}->Toplevel;
my $radio;

$radio = $checks[0];

$win->title("Please Select");

$win->geometry("+210+220");
$win->protocol('WM_DELETE_WINDOW', sub{});

foreach(@checks)
{
$win->Radiobutton(				-font 			=> '-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1',
						-text			=> $_,
						-width			=> 25,
						-anchor			=> 'w',
						-value			=> $_,
						-variable		=> \$radio	
		)->pack(			-side 			=> 'top', 
						-padx 			=> 20, 
						-pady 			=> 5);

}

$win->Button(					-fg 			=> 'black', 
						-bg 			=> 'grey85',
						-activeforeground 	=> 'tomato2',
						-activebackground 	=> 'lemonchiffon1',
						-width			=> 5,
						-text 			=> "OK", 
						-font 			=> '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1',
						-command 		=> sub{		$$chk = $radio; 
											$pkg->Killmessage($win);
											return;  
										}
		)->pack(			-side 			=> 'top',
						-pady			=> 10,
						-padx			=> 60);
		
$win->Button(					-fg 			=> 'black', 
						-bg 			=> 'grey85',
						-activeforeground 	=> 'darkgreen',
						-activebackground 	=> 'lemonchiffon1',
						-width			=> 5,
						-text 			=> "Cancel", 
						-font 			=> '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1',
						-command 		=> sub{		$$chk = 0; 
											$pkg->Killmessage($win);
											return;
										}
		);
		#->pack(			-side 			=> 'right',
		#				-pady			=> 10,
		#				-padx			=> 60);

my $currentgrabber = $win->grabCurrent();
if($currentgrabber){$currentgrabber->grabRelease();}

$win->grab();
}

sub Killmessage {
my ($pkg,$win) = @_;

	$win->grabRelease();
	$win->destroy()
	
}

sub Message {
my ($pkg, $mess, $ref, $cmd) = @_;
my $l = 0;
my $x;
my $y;
my @lines;
my $e;


$mess = substr($mess,0,200);
@lines = split /\n/, $mess;
foreach(@lines)
{
if(length($_) > $l){$l = length($_);}
}



my $win = $pkg->{Mainwin}->Toplevel;
$win->title("Message");
$l = $l/2;
$x = 550 - ($l*6);

$win->geometry("+$x+400");
$win->protocol('WM_DELETE_WINDOW', sub{$pkg->Killmessage($win);});
$win->Label(		-font 			=> '-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1',
			-text			=> $mess
	)->pack(	-side 			=> 'top', 
			-padx 			=> 20, 
			-pady 			=> 15);


if (defined $cmd)
{

$win->Button(		-fg 			=> 'black', 
			-bg 			=> 'grey85',
			-activeforeground 	=> 'tomato2',
			-activebackground 	=> 'lemonchiffon1',
			-width			=> 5,
			-text 			=> "Yes", 
			-font 			=> '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1',
			-command 		=> sub{	$$ref = 1; 
							$pkg->Killmessage($win);  
						}
	)->pack(	-side 			=> 'left',
			-pady			=> 10,
			-padx			=> 60);
		
$e = $win->Button(	-fg 			=> 'black', 
			-bg 			=> 'grey85',
			-activeforeground 	=> 'darkgreen',
			-activebackground 	=> 'lemonchiffon1',
			-width			=> 5,
			-text 			=> "No", 
			-font 			=> '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1',
			-command 		=> sub{	$$ref = 0; 
							$pkg->Killmessage($win);
							}
	)->pack(	-side 			=> 'right',
			-pady			=> 10,
			-padx			=> 60);
}
else
{
$e = $win->Button(	-fg 			=> 'black', 
			-bg 			=> 'grey85',
			-activeforeground 	=> 'darkgreen',
			-activebackground 	=> 'lemonchiffon1',
			-text 			=> "OK", 
			-font 			=> '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1',
			-command 		=> sub{	$$ref = 0;
							$pkg->Killmessage($win);
							}
	)->pack(	-side 			=> 'bottom',
			-padx			=> 5,
			-pady			=> 10);
}

$e->focus();
my $currentgrabber = $win->grabCurrent();
if($currentgrabber){$currentgrabber->grabRelease();}

$win->grab();

}

sub InputBox {
my ($pkg,$title, $chk) = @_;
my $win = $pkg->{Mainwin}->Toplevel;
my $radio;

$win->title("InputBox");

$win->geometry("+210+220");
$win->protocol('WM_DELETE_WINDOW', sub{$pkg->Killmessage($win);});

$win->Label(					-font 			=> '-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1',
						-text			=> $title,
						-width			=> 35,
						-anchor			=> 'w',	
		)->pack(			-side 			=> 'top', 
						-padx 			=> 20, 
						-pady 			=> 5);	


my $e = $win->Entry(				-font 			=> '-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1',
						-width			=> 20,
						-bg			=> 'lemonchiffon1',
						-textvariable		=> \$radio	
		)->pack(			-side 			=> 'top', 
						-padx 			=> 20, 
						-pady 			=> 5);
$e->focus();						
$e->bind('<Key-Return>', sub{$$chk = $radio;$pkg->Killmessage($win);return;});

$win->Button(					-fg 			=> 'black', 
						-bg 			=> 'grey85',
						-activeforeground 	=> 'tomato2',
						-activebackground 	=> 'lemonchiffon1',
						-width			=> 5,
						-text 			=> "OK", 
						-font 			=> '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1',
						-command 		=> sub{
											$$chk = $radio;
											$pkg->Killmessage($win);
											return;  
										}
		)->pack(			-side 			=> 'left',
						-pady			=> 10,
						-padx			=> 60);
		
$win->Button(					-fg 			=> 'black', 
						-bg 			=> 'grey85',
						-activeforeground 	=> 'darkgreen',
						-activebackground 	=> 'lemonchiffon1',
						-width			=> 5,
						-text 			=> "Cancel", 
						-font 			=> '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1',
						-command 		=> sub{		
											$$chk = 0; 
											$pkg->Killmessage($win);
											return;
										}
		)->pack(			-side 			=> 'right',
						-pady			=> 10,
						-padx			=> 60);

my $currentgrabber = $win->grabCurrent();
if($currentgrabber){$currentgrabber->grabRelease();}
$win->grab();


}
sub PrintPlate {
my ($pkg, $plate, $data) = @_;
my $desc = "Digest Rack Check";
my $worker = 'bsw';
my $row;
my $col;

my $printer = $ENV{'PRINTER'};

open(OUT,"| lpr -P$printer")|| die "Printplate::Cannot open pipe to printer\n";

#open(OUT,">chrom.ps");
my $fhdl = *OUT;


printf $fhdl qq#%\!PS-Adobe-1.0
 %%Creator: printmap
 %%Title: printfMap
 %%Orientation: Landscape
 %%Pages: 1
 %%DocumentFonts: Helvetica-Bold Helvetica
 %%EndComments
 %%EndProlog
 %%Page: 1 1

/gs { gsave } def
/rectstroke {
  gsave newpath
  4 2 roll moveto
  exch dup 0 rlineto
  exch 0 exch rlineto
  neg 0 rlineto
  closepath
  0 setlinewidth stroke
  grestore
} def
/rectfill {
  gsave newpath
  4 2 roll moveto
  exch dup 0 rlineto
  exch 0 exch rlineto
  neg 0 rlineto
  closepath
  0.7 setgray
  fill
  grestore
} def
#;

printf $fhdl "\n";
printf $fhdl "90 rotate\n";
printf $fhdl "0 -595 translate\n";
printf $fhdl "/Helvetica-Bold findfont 18 scalefont setfont\n";
printf $fhdl  "%d %d moveto (%s) show\n",300,510,"STOAT Sample Tracking";
printf $fhdl "/Helvetica-Bold findfont 12 scalefont setfont\n";
printf $fhdl  "%d %d moveto (%s) show\n",65,490,"Plate";
printf $fhdl  "%d %d moveto (%s) show\n",65,475,"Description";
printf $fhdl  "%d %d moveto (%s) show\n",65,460,"Printed By ";
printf $fhdl  "%d %d moveto (%s) show\n",65,445,"Date";

printf $fhdl  "%d %d moveto (%s) show\n",551,490,"Volume In Wells";
printf $fhdl  "%d %d moveto (%s) show\n",551,475,"Source";
printf $fhdl  "%d %d moveto (%s) show\n",551,460,"Library";
printf $fhdl  "%d %d moveto (%s) show\n",551,445,"Vector";


printf $fhdl "/Helvetica findfont 12 scalefont setfont\n";

$plate =~ s/[(]|[)]//g;

printf $fhdl  "%d %d moveto (%s) show\n",135,490,$plate;
printf $fhdl  "%d %d moveto (%s) show\n",135,475,$desc;
printf $fhdl  "%d %d moveto (%s) show\n",135,460,$worker;
printf $fhdl  "%d %d moveto (%s) show\n",135,445,scalar(localtime(time));
printf $fhdl "/Helvetica-Bold findfont 8 scalefont setfont\n";

printf $fhdl  "%d %d moveto %d %d lineto stroke\n",550 ,500 ,799, 500;
printf $fhdl  "%d %d moveto %d %d lineto stroke\n",550 ,485 ,799, 485;
printf $fhdl  "%d %d moveto %d %d lineto stroke\n",550 ,470 ,799, 470;
printf $fhdl  "%d %d moveto %d %d lineto stroke\n",550 ,455 ,799, 455;
printf $fhdl  "%d %d moveto %d %d lineto stroke\n",550 ,440 ,799, 440;

printf $fhdl  "%d %d moveto %d %d lineto stroke\n",550 ,500 ,550, 440;
printf $fhdl  "%d %d moveto %d %d lineto stroke\n",676 ,500 ,676, 440;
printf $fhdl  "%d %d moveto %d %d lineto stroke\n",799 ,500 ,799, 440;
#previous
my $old= 0;
foreach(@{$data})
	{
	# Find well
	($_->[2], $_->[2]) = split /-/,$_->[2];
	
	$row = int(($_->[2]-1)/12);
	$col = ($_->[2]- ($row*12))-1;
	
	$_->[0] =~ s/ //g;
	$_->[0] = "$_->[0]-$_->[1]";
	
	printf $fhdl  "%d %d moveto (%s) show\n",58 + ($col * 62),410 - ($row * 45),$_->[0];
	
	printf $fhdl  "%d %d moveto (%s) show\n",58 + ($col * 62),394 - ($row * 45),$_->[6];
	
	printf $fhdl  "%d %d moveto (%s) show\n",58 + ($col * 62),386 - ($row* 45),$_->[4];
	
	printf $fhdl  "%d %d moveto (%s) show\n",58 + ($col * 62),378 - ($row * 45),"Chr-$_->[5]";
	
	}
	


printf $fhdl "/Helvetica findfont 10 scalefont setfont\n";
for $row (0..7)
{
printf $fhdl  "%d %d moveto\n(%s) show\n",40,400 - ($row * 45),chr(65 + $row);
printf $fhdl  "%d %d moveto %d %d lineto stroke\n",55 ,420 - ($row * 45) ,799, 420 - ($row * 45);
}
printf $fhdl  "%d %d moveto %d %d lineto stroke\n",55 ,420 - (8 * 45) ,799, 420 - (8 * 45);

for $col(0..11)
{
	printf $fhdl  "%d %d moveto\n(%d) show\n",88 + ($col * 61.5),425,$col+1;
	printf $fhdl  "%d %d moveto %d %d lineto stroke\n",55 + ($col * 62) ,420 ,55+ ($col * 62), 60;
}
printf $fhdl  "%d %d moveto %d %d lineto stroke\n",55 + (12 * 62) ,420 ,55+ (12 * 62), 60;

printf $fhdl "\nshowpage\n";
# $fhdl->close;#|| die "could not close file\n";
close($fhdl)|| die "could not close pipe to printer\n";



return $printer;










}
1;
