#!/usr/local/bin/tree_wish

# sccstool - a tool for viewing SCCS files graphically
# Copyright (c) 1998 by Larry McVoy; All rights reserved
# @(#)sccstool 1.1 lm@lap(none)

proc list {canvas tree file rev} {
	foreach kid [exec lsrev $file $rev] {
		addRev $canvas $tree $rev $kid 
		list $canvas $tree $file $kid
	}
}

proc addRev {canvas tree parent kid} {
	$canvas create text 0 0 -text $kid -tags $kid
	set line [$canvas create line 0 0 0 0]
	$tree addlink $parent $kid $line -border 2m
}

proc GetPath {canvas} {
	set id [$canvas select item]
	if {"$id" == ""} {
		return [lindex [$canvas gettags selected] 0]
	}
	return [lindex [$canvas gettags $id] 0]
}

proc prs {canvas} {
	global file

	$canvas select from current 0
	$canvas select to current [string length [$canvas itemcget current -text]]
	set rev [GetPath $canvas]
	set prs [open "| prs -r$rev $file"]
	.bottom.t delete 1.0 end
	while { [gets $prs str] >= 0 } {
		.bottom.t insert end "$str\n"
	}
}

proc diff1 {canvas} {
	global file
	global rev1

	$canvas select from current 0
	$canvas select to current [string length [$canvas itemcget current -text]]
	set rev1 [GetPath $canvas]
}

proc diff2 {canvas} {
	global file
	global rev1
	global rev2

	$canvas select from current 0
	$canvas select to current [string length [$canvas itemcget current -text]]
	set rev2 [GetPath $canvas]
	set diffs [open "| diffs -r$rev1 -r$rev2 $file"]
	.bottom.t delete 1.0 end
	while { [gets $diffs str] >= 0 } {
		.bottom.t insert end "$str\n"
	}
}

proc setBindings {canvas tree} {
         bind $canvas <1> "focus %W; prs $canvas"
         bind $canvas <2> "focus %W; diff1 $canvas"
         bind $canvas <3> "focus %W; diff2 $canvas"
}

# Main

wm geometry . +1+1
	
frame .top -borderwidth 2 -relief sunken
    	scrollbar .top.xscroll -orient horiz -command ".top.c xview"
    	scrollbar .top.yscroll -command ".top.c yview"
    	canvas .top.c -width 480 -height 200 \
	    -xscrollcommand ".top.xscroll set" \
	    -yscrollcommand ".top.yscroll set"
    	bind .top.c <ButtonPress-2> ".top.c scan mark %x %y"
    	bind .top.c <B2-Motion> ".top.c scan dragto %x %y"
	grid .top.c -row 0 -column 0
	grid .top.yscroll -row 0 -column 1 -sticky ns
	grid .top.xscroll -row 1 -column 0 -columnspan 2 -sticky ew
frame .bottom -borderwidth 2 -relief sunken
	text .bottom.t -width 80 -height 24 -font 6x12 \
	    -xscrollcommand { .bottom.xscroll set } \
	    -yscrollcommand { .bottom.yscroll set }
	 scrollbar .bottom.xscroll -orient horizontal \
	    -command { .bottom.t xview }
	scrollbar .bottom.yscroll -orient vertical -command { .bottom.t yview }
	grid .bottom.t -row 0 -column 0
	grid .bottom.yscroll -row 0 -column 1 -sticky ns
	grid .bottom.xscroll -row 1 -column 0 -columnspan 2 -sticky ew
grid .top -row 0 -column 0
grid .bottom -row 1 -column 0
set tree [tree .top.c.t -layout vertical -parentdistance .4c]
set file [lindex $argv 0]
set rev [exec lsrev $file]
addRev .top.c $tree "" $rev
list .top.c $tree $file $rev
setBindings .top.c $tree
