#!/usr/local/bin/wish4.0
# RCSid: $Id: trad.wsh,v 2.16 2011/10/26 20:06:17 greg Exp $
# the next line restarts using wish in case of shell execution \
exec wish "$0" "$@"
#
# TCL/TK-based Rad Input File Editor
#

# Insert our autoload directory (may require local system modification)

set radlib /usr/local/lib/ray/tcl
set helplib $radlib

set auto_path [linsert $auto_path 0 $radlib]

# Assign global globbing variables

set myglob(rif) *.rif
set myglob(materials) *.mat
set myglob(illum) *.rad
set myglob(scene) *.rad
set myglob(objects) *

# Create required procedures

proc modified {} {	# check for radvar modification
	global radvar oldradvar
	if {! [info exists oldradvar]} {return 0}
	foreach n [array names oldradvar] {
		if {! [info exists radvar($n)] ||
				"$radvar($n)" != "$oldradvar($n)"} {
			return 1
		}
	}
	return 0
}

proc chksave {} {	# check if RIF is saved and accost user if not
	global readonly rifname oldradvar curmess
	if [modified] {
		if $readonly {
			set warnmess "You have modified variables, but the\
file \"[file tail $rifname]\" was opened read-only.
Do you wish to save this information somehow?"
set yesmess {Go to File Screen}
		} else {
			set warnmess "The file \"[file tail $rifname]\" has\
been modified since it was last saved.
Do you wish to save it now?"
			set yesmess {Save File}
		}
		switch [tk_dialog .dlg {File Modified} $warnmess \
				warning 0 $yesmess {Discard Changes} \
				{Cancel Operation}] {
			1 { return 1 }
			2 { return 0 }
		}
		if $readonly {
			set curmess {Rename file or uncheck read-only to save.}
			.file invoke
			return 0
		} elseif {[save_vars $rifname]} {
			gotfile 1
			return 1
		}
		return 0
	}
	return 1
}

# Set global variable default values

set readonly 0
set alldone 0
set rifname [pwd]/

# Propogate essential variables

proc setrname {name elem op} {
	global rifname
	wm title . $rifname
	wm iconname . [file tail $rifname]
}
trace variable rifname w setrname

# Make main window frames

set smallscrn [expr [winfo screenwidth .] < 830]

message .mess -relief ridge -font fixed -textvariable curmess -anchor nw
if $smallscrn {
	frame .upper -width 640 -height 410
	frame .upper.right -width 120 -height 410
	.mess configure -width 640
} else {
	frame .upper -width 830 -height 410
	frame .upper.right -width 130 -height 410
	.mess configure -width 830
}
pack .upper -side top
place .upper.right -relx .98 -rely 0 -anchor ne
pack .mess -side top -expand yes -fill both
helplink .mess trad trad messages
. configure -cursor top_left_arrow

# Make mode buttons

proc changescreen {} {		# switch screen according to curmode
	global curscreen curmode curmess smallscrn
	if [info exists curscreen] {
		if {"$curmode" == "$curscreen"} {return}
		destroy .upper.left
		do_$curscreen done
		set curmess {}
	}
	set curscreen $curmode
	do_$curscreen .upper.left
	if $smallscrn {
		.upper.left configure -width 520 -height 410
	} else {
		.upper.left configure -width 700 -height 410
	}
	place .upper.left -x 0 -y 0
}

label .upper.right.title -text "- TRAD -"
pack .upper.right.title -side top -pady 10

proc setbutt v {
	radiobutton .$v -text [string toupper $v] \
			-variable curmode -value $v \
			-width 10 -relief groove -anchor w \
			-command changescreen
	pack .$v -in .upper.right -side top -pady 5
	helplink .$v trad $v intro
}

setbutt file
setbutt scene
setbutt zone
setbutt views
setbutt options
setbutt action
setbutt results
rename setbutt {}
button .upper.right.help -text HELP -width 9 \
		-command "gethelp trad trad intro"
pack .upper.right.help -side top -pady 5 -anchor se
helplink .upper.right.help trad trad help
button .upper.right.quit -text QUIT \
		-command {if [chksave] {destroy .}} -width 9
pack .upper.right.quit -side top -pady 5 -anchor se
helplink .upper.right.quit trad trad quit

if $smallscrn {
	wm minsize . 640 460
	wm maxsize . 640 512
} else {
	wm minsize . 830 460
	wm maxsize . 830 512
}
wm iconbitmap . @$radlib/trad.icon

proc gotfile o {		# set file possession state
	global oldradvar radvar
	catch {unset oldradvar}
	if $o {
		set mode normal
		foreach n [array names radvar] {
			set oldradvar($n) $radvar($n)
		}
	} else {
		set mode disabled
	}
	foreach b {scene zone views options action results} {
		.$b configure -state $mode
	}
}

# Decide where to go and start

gotfile 0
if {[llength $argv] == 0} {
	set curmess "Choose a Radiance project file."
	.file invoke
} elseif {[llength $argv] == 1} {
	cd [file dirname $argv]
	set curfile [file tail $argv]
	if {! [file isfile $curfile]} {
		if [newnew $curfile] {
			.scene invoke
		} else {
			.file invoke
		}
	} elseif {[newload $curfile]} {
		if $alldone {
			set curmess "All renderings are finished."
			.results invoke
		} else {
			.action invoke
		}
	} else {
		.file invoke
	}
} else {
	puts stderr {Usage:  trad [rifname]}
	exit 1
}
