###############################################
# A 'same-look-and-feel' front end generator
# See platetrack2 as an example of how to call
# Author dn1g
###############################################
#
# $Id: Frontend.pm,v 1.1 2004/12/17 15:40:18 js10 Exp $
#
# $Log: Frontend.pm,v $
# Revision 1.1  2004/12/17 15:40:18  js10
# this probably shouldn't be here ...
#
# Revision 1.16  2004/08/13  10:40:32  js10
# Added an optional 'execPath' paramater
#
# Revision 1.15  2004/08/02 11:27:52  js10
# added some extra optional paramaters - in particular 'anchor' for the background picture
#
#

package SIFrontend;

use strict;
use Tk;
use Tk::IO;
use Tk::JPEG;
use Tk::Balloon;

use vars qw(@ISA @EXPORT);
use Exporter;
@ISA = ('Exporter');
@EXPORT = qw(&make_it_so);

# Set the fonts up
my $title_f = "-adobe-helvetica-bold-r-normal--24-240-75-75-p-138-iso8859-1";
my $entry_f = "-adobe-helvetica-medium-r-normal--17-120-100-100-p-88-iso8859-1";
my $button_f = "-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1";
my $label_f = "-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1";

# Overide some fonts 
#my $entry_f =  "-adobe-helvetica-medium-r-normal--16-*-100-100-p-88-iso8859-1";
#my $button_f = "-adobe-helvetica-bold-r-normal--16-*-100-100-p-92-iso8859-1";

# Processes
my @kids;
my @buttons;

my $choice;
my $canvas;
my $web_button;
my $fe;
my $frame_top;
my $mainWindow;

sub make_it_so 
{
    ($fe, $choice) = @_;
    my $prog = $fe->{prog};

	if (!$fe->{picPath}) { $fe->{picPath} = "/nfs/disk54/badger/lib/frontpage/$prog"; }
	if (!$fe->{execPath}) { $fe->{execPath} = ''; }

    # Check the display;
    $ENV{'DISPLAY'} || die "$prog: \$DISPLAY environment variable not set, Please set via 'ncd' or 'dis' command.\n";
    
    # Build the main window
    $mainWindow = MainWindow->new;
    $mainWindow-> configure(-cursor  => 'hand2',
			    -background => 'black'
			    );
    $mainWindow->wm('minsize', 450, 665); 
    $mainWindow->wm('maxsize', 450, 665);
    my $f = $mainWindow->Frame();
    $f->pack();
    $mainWindow->idletasks();
    $mainWindow->packPropagate(0);
    $f->packForget();



    $SIG{INT} = 'IGNORE';
    $SIG{QUIT} = 'IGNORE';
    $mainWindow->protocol('WM_DELETE_WINDOW' => sub {
	foreach my $n (@kids){
	    if (kill 0 => $n){
		kill 9 => $n;
	    }
	}
	&Tk::exit;
    });
    
#####################
# Set some frames up
#####################
    $frame_top = $mainWindow->Frame(-relief => 'flat', -background => 'black');
    $frame_top->pack(-side => 'top', -expand => 'yes', -fill=>'both');
    my $frame_bottom = $mainWindow->Frame(-relief => 'flat', -background => 'black');
    $frame_bottom->pack(-side => "bottom", -expand=>1, -fill=>'both');


#####################
# Set the canvas up
#####################
    $canvas = $frame_top->Canvas(-borderwidth => 0, -relief => 'flat', -width => 450, -height => 520, -background => 'black');
    $canvas->pack(-anchor => 'n', -expand=>1, -fill=>'both');
    my $canvas2 = $frame_bottom->Canvas(-borderwidth => 0, -relief => 'flat', -width => 450, -height => 150, -background => 'black');
    $canvas2->pack(-anchor => 'n', -expand=>1, -fill=>'both');
    

######################
# Set some buttons up
######################

    if ( (defined $fe->{web_button_x}) && (defined $fe->{web_button_y}) && (defined $fe->{web_url})){
	$web_button = $canvas->Button(
				      -text                => "Internet link", 
				      -font                => $button_f, 
				      -width               => 18, 
				      -background          => 'black', 
				      -foreground          => 'white', 
				      -highlightbackground => 'black', 
				      -activebackground    => 'black', 
				      -activeforeground    => 'red1', 
				      -borderwidth         => 3,
				      -command             => sub {
					  &weblink($web_button, $fe->{web_url})
					  });

	my $web_button_id = $canvas->createWindow("$fe->{web_button_x}", "$fe->{web_button_y}", -window => $web_button);
    }

    my $close = $canvas2->Button(
				 -text => "Exit", 
				 -font => $button_f, 
				 -width => 10, 
				 -background => 'black', 
				 -foreground => 'white', 
				 -highlightbackground => 'black', 
				 -activebackground => 'black', 
				 -activeforeground => 'red1', 
				 -borderwidth => 3,
				 -command => \&kill);
    my $close_id = $canvas2->createWindow("$fe->{close_x}", "$fe->{close_y}", -window => $close);


	#####################
	# Set some images up
	#####################
	if (!$fe->{anchor}) { $fe->{anchor} = 'center'; }
	if (-r "$fe->{picPath}/bkg.jpg"){
		my $pic1 = $canvas->Photo('-format' => 'jpeg', -file => "$fe->{picPath}/bkg.jpg");
		$canvas->createImage("$fe->{bkg_jpg_x}", "$fe->{bkg_jpg_y}", -image => $pic1, -anchor => $fe->{anchor});
	}
    
	if (-r "$fe->{picPath}/version.gif"){
		my $pic2 = $canvas2->Photo(-file => "$fe->{picPath}/version.gif");
		$canvas2->createImage(405, 90, -image => $pic2, -anchor => $fe->{anchor});
	}
    
	if (-r "$fe->{picPath}/title.gif"){
		my $pic3 = $canvas2->Photo(-file => "$fe->{picPath}/title.gif");
		$canvas2->createImage("$fe->{title_gif_x}", "$fe->{title_gif_y}", -image => $pic3, -anchor => $fe->{anchor});
	}



########################
# Make the choices 'so'
########################
	displayMenu($choice,0);
	MainLoop();
}

sub displayMenu
{
	my $choice = shift;
	my $saveButtons = shift;

	foreach my $ch (@$choice) {
		$ch->{state} = 'normal' if !$ch->{state};

		$ch->{button_id} = $canvas->Button(-text => "$ch->{title}",
		                                   -font => $button_f,
		                                   -width => $ch->{width},
		                                   -background => 'black',
		                                   -foreground => 'white',
		                                   -highlightbackground => 'black',
		                                   -activebackground => 'black',
		                                   -activeforeground => 'red1',
		                                   -borderwidth => 3, 
		                                   -state => "$ch->{state}");

		$ch->{button_canvasid} = $canvas->createWindow("$ch->{button_x}", "$ch->{button_y}", -window => $ch->{button_id});
	
		$ch->{button_id}->configure( -command => sub {
		                                my ($child, $return);
										if ($ch->{menu}) { 
		                                	foreach my $b (@buttons) {
		                                    	$b->destroy();
											}
		                                	@buttons = ();
		                                	displayMenu($ch->{menu},1);
										} else {
											if (-x $fe->{execPath}.$ch->{exec}) {
			                                $ch->{button_id}->configure(-state => 'disabled', 
		                                                                -text => "Please wait", 
		                                                                -background => 'Lightgoldenrod');
			                                $child = Tk::IO->new(-linecommand => sub {
		                                                        $ch->{button_id}->configure(-state => 'normal', 
		                                                        -text => "$ch->{title}", 
		                                                        -font => $button_f, 
		                                                        -width => $ch->{width}, 
		                                                        -background => 'black', 
		                                                        -foreground => 'white', 
		                                                        -highlightbackground => 'black', 
		                                                        -activebackground => 'black', 
		                                                        -activeforeground => 'red1', 
		                                                        -borderwidth => 3);
		                                                     }, 
		                                                     -childcommand => sub { $child->wait() });
			                                 $return = $child->exec($fe->{execPath}.$ch->{exec});
			                                 push @kids, $return;
											} else {
											$mainWindow->messageBox(-title => "Error",
											                        -message => "Can't execute $fe->{execPath}$ch->{exec}",
											                        -type => 'OK',
											                        -icon => 'error');
											}
										 }
	                                 }
	                            );
		$ch->{balloon} = $frame_top->Balloon(-background => 'white')->attach($ch->{button_id}, -balloonmsg => $ch->{balloonmsg});
		if ($saveButtons) { push @buttons, $ch->{button_id}; }
	}
	return \@buttons;
}


###############
# Subroutines
###############
sub weblink {

    my ($webb, $url) = @_;

    my $child;
    $webb->configure(-text => "Web link started", -state => 'disabled', -background => 'Lightgoldenrod');
    $child = Tk::IO->new(
			 -linecommand => sub {$webb->configure(-state => 'normal', -text => "Internet link", -font => $button_f, -width => 18, -background => 'black', -foreground => 'white', -highlightbackground => 'black', -activebackground => 'black', -activeforeground => 'red1', -borderwidth => 3);}, 
			 -childcommand => sub {$child->wait()}
			 );

    my $goto_web = "netscape"." $url";
    my $return = $child->exec("$goto_web");
    push @kids, $return;
}



sub kill 
{
	foreach my $n (@kids) {
		if (kill 0 => $n) {
			kill 9 => $n;
		}
	}
	@kids = ();
	&Tk::exit;
}


1;
