
# 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 {} {
    global form_done_flag hardcopy env
    global botform_geo tdv_default
    
    if ![file_hardcopy_form screen $botform_geo] return
    
    set coloropt [list epscolor epsgray epsmono color gray mono]
    set bindir $env(ICEM_ACN)/bin
    switch $hardcopy(type) {
	ps -
	eps {
	    
	    # Is this color/grayscale/Mono Postscript or Ext. Postscript
	    
	    set ps_command_options {}
	    if {$hardcopy(type) == "ps"} {
		set ind 1;
	    } else {
		set ind 0;
	    }
	    append ps_command_options "-color " \
		[lindex $coloropt [expr 3*$ind+$hardcopy(colorps)]]
	    
	    if $hardcopy(orient) {
		append ps_command_options " landscape"
	    }
	    
	    switch $hardcopy(sh) {
		0 { set shade_type "flat" }
		1 { set shade_type "gouraud" }
		2 { set shade_type "wire" }
	    }
	    append ps_command_options " -shade $shade_type"
	    
	    if !$hardcopy(dlight) {
		append ps_command_options " -nodlight"
	    }
	    
	    if $hardcopy(frame) {
		append ps_command_options " -frame 2"
	    }
	    
	    if {$hardcopy(title) != ""} {
		append ps_command_options " -title \"$hardcopy(title)\""
	    }
	    
	    if {$hardcopy(label) != ""} {
		append ps_command_options " -label \"$hardcopy(label)\""
	    }
	    
	    switch $hardcopy(colorps) {
		0 { set color_type "color" }
		1 { set color_type "greyscale" }
		2 { set color_type "monochrome" }
	    }
	    
	    if {([info commands file_hardcopy_print_special] == "") || \
		    ![file_hardcopy_print_special]} {
		mess "Saving $color_type postscript image to file\
			\"$hardcopy(file)\" ...\n"
		command "plot $hardcopy(file) $ps_command_options"
	    }
	}
	image {
	    raise .v
	    mess "Saving SGI image to file \"$hardcopy(file)\" ...\n"
	    update
	    set x [winfo rootx $tdv_default]
	    set y [winfo rooty $tdv_default]
	    set w [winfo width $tdv_default]
	    set h [winfo height $tdv_default]
	    set sh [winfo screenheight $tdv_default]
	    exec [get_path scrsave] $hardcopy(file) $x [expr {$x + $w}] \
		[expr {$sh - $y}] [expr {$sh - ($y + $h + 1)}]
	}
	jpg -
	ppm -
	x - 
	tiff -
	gif {
		command "write_ppm_file $hardcopy(file).ppm"
		switch $hardcopy(type) {
		    tiff {
			catch "exec $bindir/pnmtotiff < $hardcopy(file).ppm > $hardcopy(file)"
		    }
		    x {
			catch "exec $bindir/pnmtoxwd $hardcopy(file).ppm > $hardcopy(file)"
		    }
		    gif {
			catch "exec $bindir/ppmtogif $hardcopy(file).ppm > $hardcopy(file)"
		    }
		    ppm {
			catch "exec mv $hardcopy(file).ppm $hardcopy(file)"
		    }
		    jpg {
			catch "exec $bindir/cjpeg < $hardcopy(file).ppm > $hardcopy(file)"
		    }
		    default {
			mess "Unknown format\n"
		    }
		}
		if { $hardcopy(type) != "ppm" } {
			exec rm $hardcopy(file).ppm
		}
	}
	default {
		mess "Unknown format\n"
	}
    }
    mess "Done.\n"
    
    if $hardcopy(print) {
	switch $hardcopy(type) {
	    ps -
	    eps {
		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_form {pref geo {format_choice 1}} {
    global form_done_flag env hardcopy
    
    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) 2 }
    if ![info exists hardcopy(orient)]  { set hardcopy(orient) 0 }
    if ![info exists hardcopy(sh)]      { set hardcopy(sh) 0}
    if ![info exists hardcopy(dlight)]  { set hardcopy(dlight) 1}
    if ![info exists hardcopy(frame)]   { set hardcopy(frame) 0}
    if ![info exists hardcopy(print)]   { set hardcopy(print) 0}
    if ![info exists hardcopy(title)]   { set hardcopy(title) "" }
    if ![info exists hardcopy(label)]   { set hardcopy(label) "" }
    
    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 {
	    set hps {{hardcopy(type) ps}}
	    set ln 0

	    form_label $d.typel "Hardcopy format:" "$ln 0 -cols -"
	    incr ln

	    form_radiobutton $d.ps "PostScript" hardcopy(type) ps "$ln 1"
	    form_radiobutton $d.cps "Color" hardcopy(colorps) 0 {1 3} $hps
	    incr ln

	    form_radiobutton $d.eps "Encapsulated PostScript" \
		hardcopy(type) eps "$ln 1 -cols 2"
	    form_radiobutton $d.gps "Grey Scale" hardcopy(colorps) 1 {2 3} $hps
	    incr ln
	    
	    set sgi [expr {[get_path scrsave] != ""}]
	    form_radiobutton $d.image "SGI rgb format" hardcopy(type) image \
		"$ln 1 -cols 2" $sgi
	    form_radiobutton $d.mps "Monochrome" hardcopy(colorps) 2 \
			"$ln 3" $hps
	    incr ln

	    form_radiobutton $d.gif "GIF" hardcopy(type) gif \
                "$ln 1 -cols 2"
	    incr ln

	    form_radiobutton $d.tiff "TIFF" hardcopy(type) tiff \
                "$ln 1 -cols 2"
	    incr ln

	    form_radiobutton $d.ppm "PPM" hardcopy(type) ppm \
                "$ln 1 -cols 2"
	    incr ln

	    form_radiobutton $d.jpg "JPEG" hardcopy(type) jpg \
                "$ln 1 -cols 2"
	    incr ln

	    form_radiobutton $d.x "X" hardcopy(type) x \
                "$ln 1 -cols 2"
	    incr ln
	    
	    
	    form_rowheight $d $ln 5
	    incr ln

	    form_label $d.orient "Orientation:" "$ln 0 -cols 2"
	    form_radiobutton $d.ola "Landscape" hardcopy(orient) 1 "$ln 2" $hps
	    form_radiobutton $d.opo "Portrait"  hardcopy(orient) 0 "$ln 3" $hps
	    incr ln
	    
	    form_rowheight $d $ln 5
	    incr ln

	    form_label $d.shade "Solid Shading:" "$ln 0 -cols 2"
	    form_radiobutton $d.sfl   "Flat"    hardcopy(sh) 0 "$ln 2" $hps
	    form_radiobutton $d.sgou  "Gouraud" hardcopy(sh) 1 "$ln 3" $hps
	    form_radiobutton $d.snone "None"    hardcopy(sh) 2 "$ln 4" $hps
	    incr ln

	    form_rowheight $d $ln 5
	    incr ln

	    form_label $d.misc "Misc:" "$ln 0"
	    form_checkbutton $d.dlight "Direct Lighting" hardcopy(dlight) \
		"$ln 2"
	    form_checkbutton $d.frame "Frame" hardcopy(frame) "$ln 3"
	}
	
	form_label $d.titlel "Title:" "$ln 0"
	form_entry $d.title hardcopy(title) string "$ln 1 -cols 3"
	incr ln
	
	form_rowheight $d $ln 5
	incr ln

	form_label $d.labell "Label:" "$ln 0"
	form_entry $d.label hardcopy(label) string "$ln 1 -cols 3"
	incr ln

	form_label $d.filel "Output file:" "$ln 0 -cols -"
	incr ln
	
	form_frame $d.file flat 0 "$ln 1 -cols -"
	file_select_entry $d.file.e hardcopy(file) 0 1 \
	    "File for hardcopy output" {0 0}
	incr ln
	
	form_checkbutton $d.print "Send to printer" hardcopy(print) \
		"$ln 0 -cols - -fill none"
	incr ln

	form_frame $d.printf flat 0 "$ln 1 -cols 2"
	incr ln

	form_label $d.printf.name "Printer name" {0 0} {{hardcopy(print) 1}}
	form_entry $d.printf.er hardcopy(printer) string {0 1} \
		{{hardcopy(print) 1}}
	
	form_colwidth $d 0 7 M
	form_rowheight $d 21 2
	form_finish $geo
    }
    tkwait variable form_done_flag(.hardcopy_$pref)
    set env(PRINTER) $hardcopy(printer)
    return $form_done_flag(.hardcopy_$pref)
}
