
# 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
    global botform_geo
    
    if ![file_hardcopy_form screen $botform_geo] return
    
    set coloropt [list epscolor epsgray epsmono color gray mono]
    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" }
	    }
	    
	    mess "Saving $color_type postscript image to file\
			\"$hardcopy(file)\" ...\n"
	    command "plot $hardcopy(file) $ps_command_options"
	}
	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) {
	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 {
	    form_label $d.typel "Hardcopy format:" {0 0 -cols -}
	    form_radiobutton $d.ps "PostScript" hardcopy(type) ps {1 1}
	    form_radiobutton $d.eps "Extended PostScript" \
		hardcopy(type) eps {2 1 -cols 2}
	    set hps {{hardcopy(type) ps}}
	    
	    set sgi [expr {[get_path scrsave] != ""}]
	    form_radiobutton $d.image "SGI rgb format" hardcopy(type) image \
		{3 1 -cols 2} $sgi
	    
	    form_radiobutton $d.cps "Color" hardcopy(colorps) 0 {1 3} $hps
	    form_radiobutton $d.gps "Grey Scale" hardcopy(colorps) 1 {2 3} $hps
	    form_radiobutton $d.mps "Monochrome" hardcopy(colorps) 2 {3 3} $hps
	    
	    form_rowheight $d 4 5
	    form_label $d.orient "Orientation:" {5 0 -cols 2}
	    form_radiobutton $d.ola "Landscape" hardcopy(orient) 1 {5 2} $hps
	    form_radiobutton $d.opo "Portrait"  hardcopy(orient) 0 {5 3} $hps
	    
	    form_rowheight $d 6 5
	    form_label $d.shade "Solid Shading:" {7 0 -cols 2}
	    form_radiobutton $d.sfl   "Flat"    hardcopy(sh) 0 {7 2} $hps
	    form_radiobutton $d.sgou  "Gouraud" hardcopy(sh) 1 {7 3} $hps
	    form_radiobutton $d.snone "None"    hardcopy(sh) 2 {7 4} $hps
	    
	    form_rowheight $d 8 5
	    form_label $d.misc "Misc:" {9 0}
	    form_checkbutton $d.dlight "Direct Lighting" hardcopy(dlight) \
		{9 2}
	    form_checkbutton $d.frame "Frame" hardcopy(frame) {9 3}
	}
	
	form_label $d.titlel "Title:" {10 0}
	form_entry $d.title hardcopy(title) string {10 1 -cols 3}
	
	form_rowheight $d 11 5
	form_label $d.labell "Label:" {12 0}
	form_entry $d.label hardcopy(label) string {12 1 -cols 3}
	
	form_label $d.filel "Output file:" {14 0 -cols -}
	form_entry $d.file hardcopy(file) string {15 1 -cols 3}
	
	form_checkbutton $d.print "Send to printer" hardcopy(print) \
		{16 0 -cols - -fill none}
	form_label $d.pname "Printer name" {17 1} {{hardcopy(print) 1}}
	form_entry $d.printer hardcopy(printer) string {17 2} \
		{{hardcopy(print) 1}}
	
	form_colwidth $d 0 7 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)
}
