#  Copyright (c) 1997-2004
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: BackgroundViewer.pm,v 1.7 2004/10/26 19:00:06 gawrilow Exp $

use Poly::BackgroundProcess;
use Poly::Tempfile;

package BackgroundViewer;

use Struct (
   '$graphics',
   [ '$tempfile' => 'new Poly::Tempfile' ],
);

sub run {
   my ($self, $synchronous) = @_;
   $self->graphics->print_it;
   my $command=$self->command;
   dbg_print( "running '$command'" ) if $Switches::d;
   $synchronous
   ? system($command)
   : new Poly::BackgroundProcess(to_destroy => $self->tempfile, $command);
   undef $self->tempfile;
}

# execution in background
sub finish {
   run(shift);
}

# old style synchronous execution 
sub launch {
   run(shift,1);
}

sub active {
   my $pkg=shift;
   $main::scope->at_end->{$pkg} ||= $pkg->new(@_);
}


package SimpleViewerList;

sub new { bless [ ] };

sub finish {
   my ($self) = @_;
   foreach my $job (@$self) {
      $job->run;
   }
}


package SimpleViewer;
# for viewer not capable to show multiple pages in one process

use Struct (
   [ '@ISA' => 'BackgroundViewer' ],
);

sub new_drawing {
   my $pkg=shift;
   my $list=($main::scope->at_end->{$pkg} ||= new SimpleViewerList);
   push @$list, (my $self=$pkg->new(@_));
   $self;
}

1

# Local Variables:
# c-basic-offset:3
# End:
