
# This code creates the user interface for the hexa mesher.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc file_hardcopy_subproc {} {
    global form_done_flag hardcopy_file hardcopy_type hardcopy_print
    global hardcopy_printer hardcopy_colorps
    global botform_geo
    
    if ![file_hardcopy_subproc_form screen $botform_geo] return
    
    case $hardcopy_type in {
	ps {
	    if $hardcopy_colorps {
		mess "Saving color postscript image to file\
			\"$hardcopy_file\" ...\n"
		command "plot $hardcopy_file color"
	    } else {
		mess "Saving monochrome postscript image to file\
			\"$hardcopy_file\" ...\n"
		command "plot $hardcopy_file"
	    }
	}
	image {
	    mess "Saving SGI image to file \"$hardcopy_file\" ...\n"
	    update
	    set x [winfo rootx .v.view]
	    set y [winfo rooty .v.view]
	    set w [winfo width .v.view]
	    set h [winfo height .v.view]
	    set sh [winfo screenheight .v.view]
	    exec [get_path scrsave] $hardcopy_file $x [expr {$x + $w}] \
		[expr {$sh - $y}] [expr {$sh - ($y + $h + 1)}]
	}
    }
    mess "Done.\n"
    
    if $hardcopy_print {
	case $hardcopy_type in {
	    ps {
		mess "Sending to printer...\n"
		exec [get_path lp] -d$hardcopy_printer $hardcopy_file
		mess "Done.\n"
	    }
	    image {
		set tops [get_path tops]
		if {$tops == ""} {
		    mess "Can't print file: the \"tops\" command wasn't\
			    located\n" red
		    return
		}
		mess "Converting to PostScript...\n"
		exec $tops $hardcopy_file -eps > $hardcopy_file.ps
		mess "Sending to printer...\n"
		exec [get_path lp] -d$hardcopy_printer $hardcopy_file.ps
		mess "Done.\n"
	    }
	}
    }
}

proc file_hardcopy_subproc_form {pref geo {format_choice 1}} {
    global form_done_flag hardcopy_file env hardcopy_printer
    global hardcopy_type hardcopy_colorps
    
    if ![info exists hardcopy_printer] { set hardcopy_printer PostScript }
    if ![info exists hardcopy_type]    { set hardcopy_type ps }
    if ![info exists hardcopy_colorps] { set hardcopy_colorps 0 }
    for {set num 1} 1 {incr num} {
	set hardcopy_file $pref.[format %02d $num]
	if ![file exists $hardcopy_file] break
    }
    
    set d [form_init .hardcopy_$pref "Hardcopy creation" file]
    if {$d != ""} {
	form_frame $d.fr sunken 3 {0 0}; set d $d.fr
	
	if $format_choice {
	    form_label $d.typel "Hardcopy format:" {0 0 -cols -}
	    form_radiobutton $d.ps "PostScript" hardcopy_type ps {1 1}
	    set hps {{hardcopy_type ps}}
	    form_radiobutton $d.cps "Color" hardcopy_colorps 1 {1 2} $hps
	    form_radiobutton $d.gps "Monochrome" hardcopy_colorps 0 {2 2} $hps
	    set sgi [expr {[get_path scrsave] != ""}]
	    form_radiobutton $d.image "SGI rgb format" hardcopy_type image \
		{3 1 -cols 2} $sgi
	}
	
	form_label $d.filel "Output file:" {7 0 -cols -}
	form_entry $d.file hardcopy_file string {8 1 -cols - -fill both}
	
	form_checkbutton $d.print "Send to printer" hardcopy_print \
		{9 0 -cols - -fill none}
	form_label $d.pname "Printer name" {10 1} {{hardcopy_print 1}}
	form_entry $d.printer hardcopy_printer string {10 2} \
		{{hardcopy_print 1}}
	
	form_colwidth $d 0 4 M
	form_rowheight $d 20 2
	form_finish $geo
    }
    tkwait variable form_done_flag(.hardcopy_$pref)
    set env(PRINTER) $hardcopy_printer
    return $form_done_flag(.hardcopy_$pref)
}
