################################################################################
# Author: pjm
# Description;
#
# I wrote this so instead of 1 error message poping up after another this would
# collect together all of the messages and display them in one go.
#
# If the XTerm is low on memory this may help it not to crash :)
#
# Show_Error is a wrapper that calls this module/class with fixed text for
# line 1 and line2 headings.
#
# $Id: Show_Messages.pm,v 1.1 2000/07/03 14:32:36 pjm Exp $
#
################################################################################

package Show_Messages;

use strict;
use Mail;

my $bug_mail = 'bug';
my $debug    = 0;
my ($label1, $label2);

sub new {
    my ($type, $parent_window, $line1, $line2, @messages) = @_;

    my $self = {
        parent_window => $parent_window,
        messages      => \@messages,
    };
    bless($self, $type);

    $self->create_window($line1, $line2);

    return $self;
}

sub create_window {
    my ($self, $line1, $line2) = @_;

    my $mw = $self->{parent_window}->Toplevel();
    $self->{mw} = $mw;

    $mw->title('Information');

    # TITLE MESSAGE/INSTRICTIONS
    my $clone_frame = $mw->Frame();

    $clone_frame->Label()->pack();
    my $label1 = $clone_frame->Label(-text  => $line1)->pack();
    my $label2 = $clone_frame->Label(-text  => $line2)->pack();

    $clone_frame->pack();

    # BLANK SPACE
    $mw->Label()->pack();

    # PREP DETAILS
    $self->{list_widget} =
        $mw->Scrolled('Listbox',
                      -width  => 95,
                      -height => 25,
                      -relief => 'groove',
                      -font   =>
                 '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
                      )->pack(-expand => 1,
                              -fill   => 'both');

    $mw->Button(-text    => 'Close',
                -command => sub{
                    $self->{button_pressed} = 'Close';
                    $self->hide();
                },
                )->pack;

    # Fill the list_widget wi the messages
    $self->show_messages();

    # make sure whole window appears on screen, not just the title.

    my $f = $self->{mw}->Frame(-width => 400, -height => 200);
    $f->pack(-side => 'top');
    $self->{mw}->idletasks();
    $self->{mw}->packPropagate(0);
    $f->packForget(); 

    # center window on screen

    my $x = int(($self->{mw}->screenwidth  - $self->{mw}->reqwidth)  / 2);
    my $y = int(($self->{mw}->screenheight - $self->{mw}->reqheight) / 2);
    
    $self->{mw}->geometry("+$x+$y");

    # grap focus and wait till the user has pressed one of the buttons

    $self->{mw}->raise();
    $self->{mw}->grab();
    $self->{mw}->waitVisibility;
    $self->{mw}->update;

    $self->{mw}->waitVariable(\$self->{button_pressed});
    $self->{mw}->grabRelease;
    $self->{mw}->withdraw;

    return $self->{button_pressed};
}



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

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

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

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


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

    $self->{list_widget}->delete('0', 'end');

    print "\nMessages\n----------------------\n";

    foreach my $message (@{$self->{messages}}){

        print "  $message\n";
        my @lines = split /\.\s/, $message;

        if( @lines ){
            foreach my $line (@lines){
                $self->{list_widget}->insert('end', $line);
            }
        }
        else {
            $self->{list_widget}->insert('end', $message);
        }

        $self->{list_widget}->insert('end', '***');
    }
}

sub set_title {
    my ($text1, $text2) = @_;

    $label1->configure(-text => $text1);
    $label2->configure(-text => $text2);
}

# If this object goes out of scope, Tk will still hang on to the window. This is
# very pants, hence you call this function to make the window goaway and free
# ~200k (on my machine anyway).
#
sub go_away {
    my ($self) = @_;

    if( defined($self->{mw}) ){
        if( Tk::Exists($self->{mw}) ){
            $self->{mw}->destroy();
        }
    }
}

END {
    my ($self) = @_;

    if( $self->{mw} ){
        if( Tk::Exists($self->{mw}) ){
            $self->{mw}->destroy();
        }
    }
}

###############
# END OF MODULE
###############
1;

#
# $Log: Show_Messages.pm,v $
# Revision 1.1  2000/07/03  14:32:36  pjm
# Initial revision
#
#
