#!/usr/local/bin/perl -w
#

package Penguin_FrontEnd;


use lib "/nfs/europa2/holroyd/badger-bin/penguin_new/";
use Penguin_Tools;

use strict;
use Carp;


my ( $title_frame, $menu_frame );
my ( $width, $height );

my $opts;
my $option_check;

my 	( 	
		$title_lft_canvas,
		$title_mdl_canvas,
		$title_rgt_canvas, 
		$menu_bttm_canvas, 
		$menu_lft_canvas, 
		$menu_rgt_canvas, 
	);

my $blank_frame_width = 5;
my $rgt_canvas_width  = 37;
my $lft_canvas_width  = 360;#145;
my $mdl_canvas_height = 150;

my $IMAGE_HOME 		= "/nfs/europa2/holroyd/badger-bin/penguin/images/";

my $PENGUIN_PIC 		= "$IMAGE_HOME/emppenguin_small1.gif";
my $PENGUIN_SUBTITLE 	= "$IMAGE_HOME/penguin_subTitle1.gif";

my $penguin_tools = Penguin_Tools->new();

sub new {
	my ( $type, $progname, $mw ) = @_;
	croak "ERROR: No MainWindow handle or program name!\n" unless( $mw || $progname );	
	
	my $self = 	{
					mw 		=> $mw,
					progname 	=> $progname, 
				};
				
	$self->{version}{$progname} = '1.0(beta)';								
	
	bless(  $self, $type );
	return( $self );
}

sub createBackground {
	my ( $self, $bttns, $options ) = @_;
	
	$self->createOptions() 			if $options;
	$self->checkOptions( $options ) 	if $options;
	
	$self->createFrames( $options);
	$self->createBorder( $options );
	$self->createImages( $options );
	$self->createMenuBttns( $bttns, $options );
	
}

sub createFrames {
	my ( $self, $options ) = @_;
	
	( $width, $height ) = $self->getSizes();
	
	$self->{mw}->configure( -bg => 'white' );
	
	my $blank_frame = 
		$self->{mw}->Frame( #-relief => 'ridge',
						#-borderwidth => 2,
						-bg          => 'white',
						-height       => 5,
						)->pack( 	-side => 'top',
								-fill => 'x' );
	my $blank_frame1 =
		$self->{mw}->Frame( #-relief => 'ridge',
						#-borderwidth => 2,
						-bg          => 'white',
						-width       => 5,
						)->pack( 	-side => 'left',
								-fill => 'y' );
	
	$title_frame = 
		$self->{mw}->Frame(	#-relief      => 'ridge',
						#-borderwidth => 2,
						-bg          => 'white',
						-height      => 150,
						)->pack( 	-side => 'top',
								-fill => 'x' );
	$menu_frame =
		$self->{mw}->Frame( #-relief      => 'ridge',
						#-borderwidth => 2,
						-bg          => 'white',#'#add8e6',
						-width       => 121,
						)->pack( 	-side => 'left',
								-fill => 'y' );
						
	$self->{main} =
		$self->{mw}->Frame( -relief      => 'ridge',
						-borderwidth => 2,
						-bg          => 'white',						
						-height      => $height
					)->pack( 	-padx => 10,
							-pady => 10,
							-fill => 'x' );
						
	foreach my $f ( $title_frame, $menu_frame, $self->{main} ) {
		$f->packPropagate( 0 );
	}
}

sub createCanvas {
	my ( $self, $options ) = @_;
	
	#########################
	# Title Canvases
	#########################
	
	$title_rgt_canvas =
		$title_frame->Canvas(-width             => $rgt_canvas_width,
						 -bg                => 'white',
						 -highlightthickness => 0,
						)->pack(  -side => 'right',
								-fill => 'y' );
	$title_mdl_canvas = 
		$title_frame->Canvas(-height			 => $mdl_canvas_height,
						 -width              => $width, 
						 -bg                 => 'white',
						 -highlightthickness => 0,
						)->pack(  -side => 'right' );
								
	$title_lft_canvas =
		$title_frame->Canvas(-width             => $lft_canvas_width,
						 -bg                => 'white',
						 -highlightthickness => 0,
						)->pack(  -side => 'left',
								-fill => 'y' );
	
	
	
	#########################
	# Menu Canvases
	#########################
	
	$menu_bttm_canvas = 
		$menu_frame->Canvas(-height	          => 25,
						-bg		          => 'white',
						-borderwidth        => 0,
						-highlightthickness => 0,
						)->pack( 	-side => 'bottom',
						    		-fill => 'x' );		
	
	my $c =0;
	foreach my $sc ( $menu_lft_canvas, $menu_rgt_canvas ) {
		my 	$side = 'left';
			$side = 'right' if $c;
		$sc 	= 
			$menu_frame->Canvas(-width  	=> 3,
							-bg   	=> 'white',
							-highlightthickness => 0,
							)->pack( 	-side => $side,
									-fill => 'y' );
		$c++;
	}


}

sub createBorder {
	my ( $self, $options ) = @_;
	
	$self->createCanvas();
	
	#########################
	# Title Border
	#########################
	
	# Title top left corner.	left line (down), top line (right).
	$title_lft_canvas->createArc( 1,1,50,50, -start => 90, -style => 'arc', -width => 2, );# -outline => 'yellow' );
	$title_lft_canvas->createLine( 1,200, 2,24, -width => 2, );# -fill => 'yellow' );						
	$title_lft_canvas->createLine( 25,2, 800,2, -width => 2, );# -fill => 'yellow' );
	
	# Title bottom left corner, bottom line (right).
	$title_lft_canvas->createArc ( 119,167, 145,136, -start => 90, -style => 'arc', -width => 2, );# -outline => 'yellow' );
	$title_lft_canvas->createLine( 132,137, 400,137, -width => 2, );# -fill => 'yellow' );
	$title_mdl_canvas->createLine( 0,137, 1000,137, -width => 2, );# -fill => 'yellow' ); 
	$title_mdl_canvas->createLine( 0,2, 1000,1, -width => 2, );# -fill => 'yellow' );
	
	# Title right canvas. End of title frame border.
	$title_rgt_canvas->createArc ( -50,1, 30,52, -style => 'arc', -width => 2, );# -outline => 'yellow' );
	$title_rgt_canvas->createLine( 30,26, 30,114, -width => 2, );# -fill => 'yellow' );
	$title_rgt_canvas->createArc ( -50,88, 30,137, -start => 270, -style => 'arc', -width => 2);# -outline => 'yellow' );
	
	# Rectangle ready for application details depending on hierahcal calls, program name, author ... etc.
	$title_lft_canvas->createRectangle( 128,5, 348, 133 );	
	
	
	#########################
	# Menu Border
	#########################
	
	$title_lft_canvas->createLine( 10,149, 110,149, );# -fill => 'yellow' );
	
	$menu_lft_canvas->createLine( 2,0, 2,1000, -width => 2, );# -fill => 'yellow' );
	$menu_rgt_canvas->createLine( 2,0, 2,1000, -width => 2, );# -fill => 'yellow' );

	$menu_bttm_canvas->createArc( 1,-23, 30,20, -start => 180, -style => 'arc', -width => 2, -tags => 'bottom', );# -outline => 'yellow' );
	$menu_bttm_canvas->createLine( 15,20, 106,20, -width => 2, -tags => 'bottom', );# -fill => 'yellow' );
	$menu_bttm_canvas->createArc( 92,-23, 120,20, -start => 270, -style => 'arc', -width => 2, -tags => 'bottom', );# -outline => 'yellow' );

	$menu_bttm_canvas->createLine( 10,0, 110,0, );# -fill => 'yellow' );
}

sub createImages {
	my ( $self, $options ) = @_;
	
	my $penguin 		= $self->{mw}->Photo( -file  => "$PENGUIN_PIC"      );
	my $penguinSubTitle = $self->{mw}->Photo( -file  => "$PENGUIN_SUBTITLE" );

	$title_lft_canvas->createImage( 65,77,  -image => $penguin );
	$title_lft_canvas->createImage( 238,35, -image => $penguinSubTitle );	
	
	# Create rectangle with 'Penguin' sub title, underline.
	$title_lft_canvas->createLine( 140,70, 335,70 );
	$title_lft_canvas->createText( 240,83, 	-text => $self->{progname}, );
	$title_lft_canvas->createLine( 140,93, 335,93 );
	
	my $authorTitle =
		$title_lft_canvas->createText( 240,105, -text => "Author: holroyd\@sanger.ac.uk", 
										-tags => [ "author" ], 
										-font => '-adobe-helvetica-bold-r-normal--11-80-100-100-p-60-iso8859-1' ) unless $option_check->{noAuthor};
	$self->createAuthorBinds() unless( $option_check->{noAuthor} || $option_check->{noAuthorBinds} );
	
	my $version =
		$title_lft_canvas->createText( 240,120, -text => "Version $self->{version}",
										-font => '-adobe-helvetica-bold-r-normal--11-80-100-100-p-60-iso8859-1' ) unless $option_check->{noVersion} || !$self->{version};

	my $listboxSize = $self->getListboxSize() unless $option_check->{noListbox};
	
	$self->{listbox} =
		$title_mdl_canvas->Scrolled( 	"Listbox",
								-font   		=> '*times*-r*20*',
								-width 		=> $listboxSize,
								-height 		=> 5,
								-scrollbars 	=> 'se',
								-bg 			=> 'white',
							)->pack() unless $option_check->{noListbox};			
	my $w = $width / 2;				
	my $wid1 = $title_mdl_canvas->createWindow( $w,66, -window => $self->{listbox}, ) unless $option_check->{noListbox};
	
	$self->updateListBox( "Welcome to $self->{progname}" ) unless $option_check->{noListbox};
	

}

sub createToolBarMenu {
	my ( $self, $menuOpts ) = @_;
	
	$self->{listbox}->configure( -height => 4 );
	
	my $c=0;
	
	my $w1 = 0; my $w2 = (length( $menuOpts->[0]->{text} )*10)+20;
	my $tp = 60;
	
	foreach my $menu ( @{$menuOpts} ) {
	
		( $w1, $w2, $tp ) = getToolBarMenuSpacing( $w1, $w2, (length($menu->{text})*10), $tp ) if $c;
	
		$self->{menu}{$menu->{text}} =
			$title_mdl_canvas->createRectangle( $w1,110, $w2,133, -tags => $menu->{text}, -fill => '#4682b4' );
		$title_mdl_canvas->createText( $tp,122, -tags => $menu->{text}, -text => $menu->{text}, -font => '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1' );

		#print STDERR "Length of $menu->{text}: ", length( $menu->{text} ), "\n";
		#print STDERR "W1: $w1 W2: $w2 TW: ", length($menu->{text})*10, "TP: $tp\n";
		
		$c++;
	}
	$self->createToolBarMenuBinds($menuOpts);
}

sub createToolBarMenuBinds {
	my ( $self, $menuOpts ) = @_;
	
	foreach my $menuItem ( @{$menuOpts} ) {
		$title_mdl_canvas->bind( $menuItem->{text}, '<Button-1>', sub 	{
																$title_mdl_canvas->Menu( -menuitems => [[ 'command' => "poo", -command => sub {print STDERR "Hello\n";} ]] )->pack();	 
															} );
	}
}

sub showToolBarMenuOpts {
	my ( $self, $menuTitle, $menuOptions ) = @_;
	
	
	
}

sub getToolBarMenuSpacing {
	my ( $w1, $w2, $tw, $tp ) = @_;
	
	my $orw1 = $w2;
	
	return( 	$w1 = $w2 + 5, 
			$w2 = (($orw1 + 5)+($tw+20)),
			$tp = $w1 + (($tw/2)+5));
}

sub createMenuBttns {
	my ( $self, $bttns, $options ) = @_;
	
	foreach my $bttn ( @{$bttns} ) {
		
		$bttn->{width} 	= 10 	unless $bttn->{width};
		$bttn->{font} 		= "-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1" unless $bttn->{font};
		$bttn->{bg}		= "#4682b4" unless $bttn->{bg};
		$bttn->{active_bg} 	= "#6495ed" unless $bttn->{active_bg};
		$bttn->{fg}		= "#b0e0e6" unless $bttn->{fg};
		
		my $side = 'top';
		$side    = 'bottom' if( $bttn->{text} =~/^[E|e]xit|[C|c]ancel|[P|p]rint|[P|p]rint[L|l]og$/ );
		
		$self->{bttns}{$bttn->{text}} = 
			$menu_frame->Button(-text 			=> $bttn->{text},
							-width 			=> $bttn->{width},
							-font 			=> $bttn->{font},
							-bg   			=> $bttn->{bg},
							-activebackground 	=> $bttn->{active_bg},
							-fg   			=> $bttn->{fg},
							)->pack( 	-side => $side,
									-pady => 10 ); 
		
	}

}

sub getSizes {
	my ( $self ) = @_;
	
	my ( $xMax, $yMax ) = $self->{mw}->maxsize();
	my ( $xMin, $yMin ) = $self->{mw}->minsize();

	my ( $width, $height );
	if ( $xMin == $xMax && $yMin == $yMax ) {
		$width  = $xMin - ( $blank_frame_width + $rgt_canvas_width + $lft_canvas_width );
		$height = $yMin - $mdl_canvas_height;
	}else{
		croak "ERROR: You have to set the WindowManager 'maxsize' and 'minsize'!\n";
	}
	
	return( $width, $height );

}

sub getListboxSize {
	my ( $self, $size ) = @_;

	my $listboxSize = int( $width / 8 ) - 1;
	#$listboxSize--;
	#print STDERR "ListboxSize: $listboxSize\n";
	return( $listboxSize );
}

sub updateListBox {
	my ( $self, $message ) = @_;
	
	$self->{listbox}->insert( 'end', "o $message" );
	$self->{listbox}->update( 'idletasks' );
}

sub createAuthorBinds {
	my ( $self ) = @_;
	
	$title_lft_canvas->bind( "author", "<Enter>",    sub { $title_lft_canvas->itemconfigure( "author", -fill => 'blue' ); } );

	$title_lft_canvas->bind( "author", "<Button-1>", sub { $penguin_tools->EmailAuthor(); } );

	$title_lft_canvas->bind( "author", "<Leave>",    sub { $title_lft_canvas->itemconfigure( "author", -fill => 'black' ); } );
	
}

sub createOptions {
	my ( $self ) = @_;
	
	$opts =	{ 	
				"noAuthor"    		=> "No Author Title to click", 
				"noAuthorBinds"	=> "Removes binds to open an email window",
				"noListbox"  		=> "No Listbox for activity updates", 		
			};		
}

sub checkOptions {
	my ( $self, $options ) = @_;
	
	foreach my $user_option ( @{$options} ) {
		if( !exists( $opts->{$user_option} )) {
			croak "ERROR: Invalid option: $user_option\n";
		}else{
			$option_check->{$user_option} = 1;
		}
	}
	
}
	


sub showOptions {
	my ( $self ) = @_;
	
	$self->createOptions() unless $opts;
	
	print STDERR sprintf( "%-40s\n%-10s%-20s%-10s\n%-40s\n", "-"x40, "", "Available Options", "", "-"x40 );
	print STDERR sprintf( "%-5s%-10s%-5s%-20s\n\n", "", "Option", "", "Description" );
	foreach my $key ( keys( %{$opts} )) {
		 print STDERR sprintf( "%-5s%-10s%-5s%-40s\n", "", $key, "", $opts->{$key} );
	}	
	return();	
}



1;

#
# $Id: Penguin_FrontEnd.pm,v 1.2 2001/11/28 18:10:28 holroyd Exp $
#
# $Log: Penguin_FrontEnd.pm,v $
# Revision 1.2  2001/11/28  18:10:28  holroyd
# *** empty log message ***
#
#
