
# This code creates the ICEM CFD Manager.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# Stuff to control the message window, and misc utility routines.

proc goto_end {} {
    .diag.text yview -pickplace end
}

proc infomsg {text} {
    global standalone
    
    if $standalone then {
	puts stdout $text
    } else {
	.diag.text insert end $text
	.diag.text insert end "\n"
	goto_end
	update idletasks
    }
}

proc catfile {msg file} {
    infomsg "$msg"
    if {![file exists $file]} {errormsg "No such file $file"; return}
    
    infomsg "-------------------------"
    set fd [open $file r]
    while 1 {
	set line [gets $fd]
	if [eof $fd] break
	infomsg "$line"
    }
    close $fd
    infomsg "-------------------------"
}

proc warningmsg {text} {
    global standalone warningcolor
    
    if $standalone then {
	puts stdout $text
    } else {
	set before [.diag.text index "end -1c"]
	.diag.text insert end "Warning: $text"
	.diag.text insert end "\n"
	goto_end
	set after [.diag.text index "end -1c"]
	.diag.text tag add red $before $after
	.diag.text tag configure red -foreground $warningcolor
	update idletasks
    }
}

proc errormsg {text} {
    global standalone errorcolor
    
    if $standalone then {
	puts stdout $text
    } else {
	set before [.diag.text index "end -1c"]
	.diag.text insert end "Error: $text"
	.diag.text insert end "\n"
	goto_end
	set after [.diag.text index "end -1c"]
	.diag.text tag add red $before $after
	.diag.text tag configure red -foreground $errorcolor
	update idletasks
    }
    beep
}

if [info exists env(LPDEST)] {
    set printer_name $env(LPDEST)
} elseif [info exists env(PRINTER)] {
    set printer_name $env(PRINTER)
} else {
    set printer_name PostScript
}

proc print_file {filename} {
    global printer_name

    if [file exists /usr/bin/lp] {
	set com "/usr/bin/lp -d$printer_name $filename"
    } elseif [file exists /usr/bin/lpr] {
	set com "/usr/bin/lpr -P$printer_name $filename"
    } elseif [file exists /usr/ucb/lpr] {
	set com "/usr/ucb/lpr -P$printer_name $filename"
    } else {
	errormsg "I don't know how to print files on this system"
	return
    }
    if [catch {eval exec $com} ret] {
	errormsg "Couldn't print the file: $ret"
    } else {
	infomsg "File sent to printer."
    }
}

proc putcenter {w} {
    wm withdraw $w
    update idletask
    
    wm sizefrom $w user
    wm positionfrom $w user
    
    set sw [winfo screenwidth .]
    set sh [winfo screenheight .]
    set ww [winfo reqwidth $w]
    set wh [winfo reqheight $w]
    set x [expr {($sw - $ww) / 2}]
    set y [expr {($sh - $wh) / 2}]
    if {$x < 0} {
	set x 0
	puts stderr "Warning: window $w too wide for screen."
    }
    if {$y < 0} {
	set y 0
	puts stderr "Warning: window $w too tall for screen."
    }
    
    wm geometry $w +$x+$y
    
    update idletask
    wm deiconify $w
    update idletask
}

proc sf {file} {
    global scriptlib gvscriptlib
    
    if {[info exists gvscriptlib] && [file exists $gvscriptlib/$file.tcl]} {
	source $gvscriptlib/$file.tcl
    } elseif {[info exists scriptlib] && [file exists $scriptlib/$file.tcl]} {
	source $scriptlib/$file.tcl
    } else {
	return "No such file"
    }
}

proc numsort {names} {
    set fnames {}
    foreach n $names {set fnames "$fnames \"[format "%10d" $n]\""}
    set list [lsort $fnames]
    set fnames {}
    foreach n $list {eval append fnames {" "} $n}
    return $fnames
}
