#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################
#
# author: pjm
#
#
# $Id: LemurReprint.pm,v 1.5 2000/04/18 11:23:13 pjm Exp $
#

package LemurReprint;

use strict;
use WrapDBI;
use LemurUtils;

my $debug = 0;

use LemurReprintBatch;
#require "/nfs/weka2/pjm/badger-bin/LemurReprintBatch.pm";

sub new {
    my ($type, $dbUser, $sessionID) = @_;

    my $self = {
	dbUser           => $dbUser,
	session_id       => $sessionID,
    };
    bless($self, $type);

    $self->{user} = getpwuid($<);

    if( $dbUser eq 'dev1' ){
        print "User = dev1\n";
    }

    $self->createWindow();

    return $self;
}

sub createWindow {
    my ($self) = @_;

    my $mw  = new MainWindow;
    $self->{mw} = $mw;

    $mw->protocol('WM_DELETE_WINDOW' => sub { 
	$self->hide();
    });

    $mw->title('Reprint');

    my $b = 
	$mw->Button(-font        => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
		    -text        => 'Reprint',
		    -height      => 1,
		    -bg          => 'white',
		    -relief      => 'groove',
		    -borderwidth => '3',
		    )->pack(-side => 'top');

    $b->bindtags(['EggButton',$b->toplevel,'all']);
    $b->bind('EggButton', '<Control-Button-2>', sub{ 
	warn "\$debug was $debug, setting to 1\n";
	$debug = 1;
    });

    my $frame = $mw->Frame(-relief => 'raised',);

    $frame->Button(-text    => 'Reprint a ligation label', 
		   -command => sub{
                       system 'reprint &';
                       LemurUtils->showInfo($mw,
                                            "Reprint program started");
		   } ,
		   -width => 25
		   )->pack();

    $frame->Button(-text    => 'Reprint all ligations in a batch', 
		   -command => sub{

                       if( defined $self->{refToPrintBatchOfLigs} ){
                           $self->{refToPrintBatchOfLigs}->show();
                       }
                       else {
                           LemurUtils->setWait([$mw]);

                           $self->{refToPrintBatchOfLigs} = 
                               LemurReprintBatch->new($mw,
                                                      $self->{dbUser},
                                                      "",
                                                      $self->{session_id},);
                           LemurUtils->unSetWait([$mw]);
                       }
		   } ,
		   -width => 25
		   )->pack();
    # QUIT
    $frame->Label()->pack;

    $frame->Button(-text    => 'Quit', 
		   -command => sub{
                       if( defined($self->{refToPrintBatchOfLigs})   &&
                           Tk::Exists($self->{refToPrintBatchOfLigs}->{mw})  ){

                           $self->{refToPrintBatchOfLigs}->{mw}->destroy();
                           delete $self->{refToPrintBatchOfLigs};
                       }
                       $self->hide();
                   },
		   -width => 25,
		   )->pack();

    $frame->pack();
    
}

sub show {
    my ($self) = @_;

    $self->{mw}->deiconify();
}

sub hide {
    my ($self) = @_;

    if( defined $self->{refToPrintBatchOfLigs} ){
        $self->{refToPrintBatchOfLigs}->hide() ;
    }
    $self->{mw}->withdraw();
}


1;
