###############################################
# A 'same-look-and-feel' front end generator
# See platetrack2 as an example of how to call
# Author dn1g
###############################################
#
# $Id: frontend.pm,v 1.16 2004/08/13 10:40:32 js10 Exp $
#
# $Log: frontend.pm,v $
# 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 frontend;

# 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 $choice;

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);

my $web_button;

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

    # Where do the pictures live >?<
    my $BITMAP_DIR = "/nfs/disk54/badger/lib/frontpage/$prog";

	if (!$fe->{execPath}) { $fe->{execPath} = ''; }

    my $who = &user_who;
    
    # Check the display;
    $ENV{'DISPLAY'} || die "$prog: \$DISPLAY environment variable not set, Please set via 'ncd' or 'dis' command.\n";
    
    # Build the main window
    my $mainWindow = MainWindow->new;
    $mainWindow-> configure(-cursor  => 'hand2',
			    -background => 'black'
			    );
    $mainWindow->wm('minsize', 450, 665); 
    $mainWindow->wm('maxsize', 450, 665);
    my $f = $mainWindow->Frame(-height => 665, -width => 450);
    $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
#####################
    my $frame_top = $mainWindow->Frame(-relief => 'flat', -background => 'black');
    $frame_top->pack(-side => 'top', -expand => 'yes');
    my $frame_bottom = $mainWindow->Frame(-relief => 'flat', -background => 'black');
    $frame_bottom->pack(-side => "bottom");


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

######################
# 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 $clean_up = $canvas2->Button(
				    -text => "Clean up", 
				    -font => $button_f, 
				    -width => 10, 
				    -background => 'black', 
				    -foreground => 'white', 
				    -highlightbackground => 'black', 
				    -activebackground => 'black', 
				    -activeforeground => 'red1', 
				    -borderwidth => 3,
				    -command => \&clean_up);
    my $cleanup_id = $canvas2->createWindow("$fe->{clean_up_x}", "$fe->{clean_up_y}", -window => $clean_up);


    my $close = $canvas2->Button(
				 -text => "Close", 
				 -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 "$BITMAP_DIR/bkg.jpg"){
		my $pic1 = $canvas->Photo('-format' => 'jpeg', -file => "$BITMAP_DIR/bkg.jpg");
		$canvas->createImage("$fe->{bkg_jpg_x}", "$fe->{bkg_jpg_y}", -image => $pic1, -anchor => $fe->{anchor});
	}
    
	if (-r "$BITMAP_DIR/version.gif"){
		my $pic2 = $canvas2->Photo(-file => "$BITMAP_DIR/version.gif");
		$canvas2->createImage(405, 90, -image => $pic2, -anchor => $fe->{anchor});
	}
    
	if (-r "$BITMAP_DIR/title.gif"){
		my $pic3 = $canvas2->Photo(-file => "$BITMAP_DIR/title.gif");
		$canvas2->createImage("$fe->{title_gif_x}", "$fe->{title_gif_y}", -image => $pic3, -anchor => $fe->{anchor});
	}



########################
# Make the choices 'so'
########################
	foreach my $ch (@$choice) {
		$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);
		                                              $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 => 18, 
		                                                                                                                   -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;
		                                             }
		                            );
		$ch->{balloon} = $frame_top->Balloon(-background => 'white')->attach($ch->{button_id}, -balloonmsg => "$ch->{balloonmsg}");
	}

	MainLoop();
}

###############
# Subroutines
###############
sub user_who {
    my $who;
    $who =`whoami`;
    chop ($who);
    unless ($who ne ''){
	die "Cannot determine user from OS !\n";
    }
    return $who;
}

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 clean_up {

    foreach my $n (@kids){
	if (kill 0 => $n){
	    kill 9 => $n;
	}
    }	

    # Clean up web access
	if ($web_button) {
		$web_button->configure(-state => "normal", 
		                       -text => "Internet link", 
		                       -font => $button_f, 
		                       -background => 'black', 
		                       -foreground => 'white', 
		                       -highlightbackground => 'black', 
		                       -activebackground => 'black', 
		                       -activeforeground => 'red1', 
		                       -borderwidth => 3);
	}

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

	@kids = ();
}

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


1;
