#!/usr/local/bin/wish -f
# 1. Change line above to point to the executable wish on your system
# 2. Change line below to point to the directory with these tcl-files
set auto_path "/usr/lib/Xspin $auto_path"

# Tcl/Tk Spin Controller Version 2.2.1,  23 February 1995
# written by Gerard J. Holzmann
# Copyright (c) 1995 by AT&T Corporation. All Rights Reserved.
# see the README.xspin file for installation notes

wm title . "SPIN CONTROL"
wm iconname . "SPIN"
wm geometry . +100+100
wm minsize  . 400 200 

# allow no more than one entry per selection
tk_listboxSingleSelect Listbox

# abort if no spin is available
set version [exec spin -V]
	# deal with outdated spin versions
	if {[string first "Spin Version 2" $version] != 0} {
		set version "Installed Spin: older than Version 2.0)"
	}

frame .menu -relief raised
	# top level menu bar
	menubutton .menu.file -text "File.." -relief sunken -menu .menu.file.m
	button .menu.val -text "Validator.." -relief sunken -command validation
	button .menu.sim -text "Simulator.." -relief sunken -command simulation
	button .menu.fsm -text "Fsm view.." -relief sunken -command fsmview
	button .menu.dtf -text "Dataflow.." -relief sunken -command dataflow

	label .menu.lno -text "Show line:" -relief sunken
	entry .menu.ent -width 6 -relief sunken -textvariable lno
	bind  .menu.ent <Return> {
		.inp.t tag remove hilite 0.0 end
		.inp.t tag add hilite $lno.0 $lno.1000
		.inp.t tag configure hilite -background black -foreground white
		.inp.t yview -pickplace [expr $lno-1]
	}

	label .menu.fnd1 -text "Find:" -relief sunken
	entry .menu.fnd2 -width 6 -relief sunken -textvariable pat
	bind  .menu.fnd2 <Return> {
		.inp.t tag remove hilite 0.0 end
		forAllMatches .inp.t $pat
	}

	menubutton .menu.help -text "Help" -relief sunken -menu .menu.help.m
	label .menu.spacer
	label .menu.name2 -text " filename: " -relief flat
	entry .menu.name -relief flat; .menu.name insert end "<none>"

	pack append .menu \
		.menu.file {left frame w} \
		.menu.help {left frame w} \
		.menu.val {left frame w} \
		.menu.sim {left frame w} \
		.menu.fsm {left frame w} \
		.menu.dtf {left frame w} \
		.menu.lno {left frame w} \
		.menu.ent {left frame w} \
		.menu.fnd1 {left frame w} \
		.menu.fnd2 {left frame w} \
		.menu.spacer {right expand fill} \
		.menu.name2 {left frame w} \
		.menu.name {left frame w}

frame .inp
	# view of spin input
	scrollbar .inp.s -relief flat \
		-command ".inp.t yview"
	text      .inp.t -relief raised -bd 2 \
		-yscrollcommand ".inp.s set" \
		-wrap word
	pack append .inp \
		.inp.s {left filly} \
		.inp.t {left expand fill}

set l_typ 0;	# used by both simulator and validator
set ol_typ -1;	# remembers setting last used in compilation
set m_typ 2;	# used by simulator

menu .menu.file.m
	.menu.file.m add command -label "Clear"  -command ".inp.t delete 0.0 end"
	.menu.file.m add command -label "Load.." -command "browse"
	.menu.file.m add command -label "ReOpen" -command "reopen"
	.menu.file.m add command -label "Save"   -command "save_spec"
	.menu.file.m add command -label "Quit"   -command "cleanup"

menu .menu.help.m
	.menu.help.m add command -label "Xspin Usage" -command "helper"
	.menu.help.m add command -label "Simulation" -command "roadmap1"
	.menu.help.m add command -label "Validation" -command "roadmap2"
	.menu.help.m add command -label "Reducing Complexity" -command "roadmap3"

set Log ""
set logwindow 0
set maxlog 0

proc cleanup {} {
	catch { exec rm -f pan pan.b pan.c pan.h pan.m pan.t pan.tmp }
	catch { exec rm -f pan.in pan.oin pan.out pan.in.trail trail.out }
	destroy .
	exit
}

proc show_log {} {
	global Log

	catch { destroy .out };	# probably redundant
	toplevel .out
	wm title .out "SPIN LOG"
	wm iconname .out "SpinLog"

	text .out.t -relief raised -bd 2 -setgrid 1 -width 30 -height 15 \
		-font -Adobe-Helvetica-Bold-R-Normal-*-120-* 
	pack append .out .out.t {left expand fill}
	.out.t insert end $Log
}

proc add_log {{y ""}} {
	global Log maxlog logwindow

	if {$logwindow == 0} {
		puts "xspin:  $y"
		return
	}

	set Log "${Log}$y\n"
	catch {
		.out.t insert end "$y"
		.out.t yview -pickplace end
		.out.t insert end "\n"
		if {[string length $y] > $maxlog} {
			set maxlog [string length $y]
			.out.t configure -width $maxlog
		}
	}
}

	if {$logwindow} {
		show_log
		# wait for the spin log window to exist
		tkwait visibility .out
	}


pack append . \
	.inp  {bot frame w expand fill} \
	.menu {top fillx}

# simulation parameters - initial settings
	set fvars 1
	set gvars 1;	set lvars 1
	set msc   1;	set svars 1
	set rvars 1;	set s_typ 0;
	set stop  0;	set seed	"1"

add_log $version

if {$argc == 1} {
	.menu.name delete 0 end
	.menu.name insert end "$argv"
	catch { exec rm -f pan pan.b pan.c pan.h pan.m pan.t pan.tmp }
	catch { exec rm -f pan.in pan.oin pan.out pan.in.trail trail.out }
	catch { eval exec cp $argv.trail pan.in.trail }
	reopen
}

# .inp.t configure -background white -foreground black
focus .inp.t
update
