#  Copyright (c) 1997-2006
#  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: postscript.rules 5216 2004-10-26 19:00:52Z gawrilow $

package Graphviz;

# path to the dot program
custom $dot;

# path to the neato program
custom $neato;

CONFIGURE {
   $dot=find_via_path("dot")
   or die "'dot' program from the graphviz package not found\n";
   ($neato=$dot) =~ s/\bdot$/neato/;
}

use Graphviz;
use BackgroundViewer;

package Graphviz::Viewer;
use Struct [ '@ISA' => 'Poly::SimpleViewer' ];

# category: Visualization
# Use @see external.graphviz to draw graphs and face lattices
label graphviz

sub new_drawing {
   my ($self, $title)=@_;
   $self->new_instance;
   $self->graphics=new Graphviz::File($self->tempfile.".dot", $title);
   $self;
}

sub command {
   my $self=shift;
   my $dotfile=$self->graphics->workfile;
   (my $psfile=$dotfile) =~ s/\.dot/.ps/;
   my $cmd=$self->graphics->command;
   "$cmd -Tps -o$psfile $dotfile; $Postscript::viewer $psfile"
}

global_method graphviz.graph: draw(Visual::Graph) {
   my ($self, $Graph)=@_;
   $self->graphics->addGraph($Graph);
}

global_method graphviz.lattice: draw(Visual::Lattice) {
   my ($self, $Lattice)=@_;
   $self->graphics->addLattice($Lattice);
}

prefer graphviz.lattice

###########################################################################################
#
#  Writes to a dot file without Postscript conversion
#

package Graphviz::File::Writer;
import Visual::FileWriter(suffix => ".dot");

sub new_drawing {
   my ($self, $title)=@_;
   $self->title=$title;
   $self;
}

###########################################################################################

package default;

# Category: Visualization
# Draw the given graph or face lattice object using @see external.graphviz program @c neato or @c dot
# correspondingly.
# The output is rendered in PostSript format and fed into a viewer program, if one is configured.
# If you prefer to produce another output format, please use the @c File option and call the @c neato
# or @c dor utility manually.
#
# args: VisualObject, ...
# option: File => "filename" | "AUTO"
#	  Only store the drawing in a @c .dot file, don't convert to Postscript nor start the viewer.
#
#	  The @c .dot suffix is automatically added to the file name.
#
#	  Specify @a AUTO if you want the filename be automatically derived from the drawing title.
#
#         You can also use any expression allowed for the perl @c open function,
#         like "-" for terminal output, "&HANDLE" for an already opened file handle,
#         or "| program" for a pipe.

user_function graphviz(Visual::Object+, { File => undef }) {
   visualize_explicit(@_, "Graphviz");
}


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