#!/usr/local/bin/wish -f
# Copyright (C) 1997 Tomohiro Tanaka

set in file0
set openCounter 1
set closedCounter 0
set currentOpenCounter 0
set currentTRS 0
set counter 0
set thcounter 0
set closedCounterDisp 0
set currentOpenCounterDisp 0
set meterWidth 500
set backgroundcolor lemonchiffon
set threshold 0.8
set measure 100
set ratio [expr 1000 * $meterWidth/$measure]
set ratioOld [expr $ratio]

frame .top -borderwidth 2 -relief raised -bg $backgroundcolor
frame .top.left -bg $backgroundcolor
frame .top.mid -borderwidth 2 -relief raised -bg $backgroundcolor
frame .top.right -bg $backgroundcolor
frame .r1 -borderwidth 2 -relief raised -bg $backgroundcolor
frame .r2 -borderwidth 2 -relief raised -bg $backgroundcolor
frame .r3 -borderwidth 2 -relief raised -bg $backgroundcolor
frame .r1.left -bg $backgroundcolor
frame .r1.mid -bg $backgroundcolor
frame .r2.left  -bg $backgroundcolor
frame .r2.mid -bg $backgroundcolor
frame .r2.right -width $meterWidth -bg $backgroundcolor
frame .r3.left  -bg $backgroundcolor
frame .r3.mid -bg $backgroundcolor
frame .r3.right -width $meterWidth -bg $backgroundcolor

pack .top -side top -fill x -pady 10
pack .r3 -after .top -fill x -pady 10
pack .r2 -after .r3 -fill x -pady 10
pack .r1 -after .r2 -fill x -pady 10
pack .top.left .top.mid .top.right -side left -expand yes 
pack .r1.left .r1.mid -side left -expand yes 
pack .r2.left .r2.mid .r2.right -side left -expand yes 
pack .r3.left .r3.mid .r3.right -side left -expand yes 

canvas .r2.right.c2 -height 26 -width $meterWidth -bg $backgroundcolor
canvas .r3.right.c3 -height 26 -width $meterWidth -bg $backgroundcolor
pack .r2.right.c2 .r3.right.c3

.r2.right.c2 create line 0 0 0 24
.r3.right.c3 create line 0 0 0 24

.r2.right.c2 create line 0 11 0 11 -width 18 -fill darkgreen
.r3.right.c3 create line 0 11 0 11 -width 18 -fill red

button .top.left.t1 -text "Cancel" -command exec_exit
message .top.mid.t -relief flat -justify center \
		-text "Branch Monitor"  -bg $backgroundcolor \
		-font lucidasans-bold-18
message .top.right.t1 -relief flat -justify left -text "Measure :" \
			-bg $backgroundcolor -width 100
message .top.right.t2 -relief sunken -justify left -text $measure \
			-width 150
message .r1.left.s1m -relief flat -justify left -text "Current Tree Size" \
			-bg $backgroundcolor -width 200
message .r1.mid.s1 -relief sunken -justify center -text "0" -width 100
message .r2.left.s2m -relief flat -justify left -text "Closed Branches" \
			-bg $backgroundcolor
message .r2.mid.s2 -relief sunken -justify center -text "0" -width 100
message .r3.left.s3m -relief flat -justify left -text "Current Open Branches" \
			-bg $backgroundcolor
message .r3.mid.s3 -relief sunken -justify center -text "0" -width 100

pack .top.left.t1 -side left -fill both
pack .top.mid.t 
pack .top.right.t1 .top.right.t2 -side left -fill both
pack .r1.left.s1m .r1.mid.s1 -fill x -padx 20
pack .r2.left.s2m .r2.mid.s2 -fill x -padx 20
pack .r3.left.s3m .r3.mid.s3 -fill x -padx 20
update

proc exec_exit {} {
        exec cat << "" > syuuryoushimashita
        exit
}

proc th_test {measure threshold b c} {
	if {$b >= $c} { set tmp $b } 
	if {$c >= $b} { set tmp $c } 
	if {$tmp > [expr $measure * $threshold]} {
		return 1 } { return 0 }
}
	
while {1 != 2} {
    global measure
    if {[eof $in] == 1} {break}
    set st [read $in 1]
    if {$st == "+"} {set openCounter [expr $openCounter + 1]}  \
    elseif {$st == "-"} {set closedCounter [expr $closedCounter + 1]}  \
    elseif {$st == "/"} {set currentTRS [expr $currentTRS + 1]}
    set currentOpenCounter [expr $openCounter - $closedCounter]
    if {$counter==4} { 
      set tmp [expr $measure * $threshold]
      if {$thcounter==3} {
        set thcounter 0 
        set ans [th_test $measure $threshold \
	  $closedCounter $currentOpenCounter]
        if {$ans == 1} {
	  set measure [expr $measure * 2]
	  set ratio [expr 1000 * $meterWidth/$measure] 
	  .r2.right.c2 create line 0 11 500 11 -width 18 \
		  -fill $backgroundcolor
	  .r3.right.c3 create line 0 11 500 11 -width 18 \
		  -fill $backgroundcolor
	  .r2.right.c2 create line 0 0 0 24
	  .r3.right.c3 create line 0 0 0 24
	  .top.right.t2 config -text $measure
          update}}
      set closedCounterDisp [expr $closedCounter * $ratio / 1000]
      set currentOpenCounterDisp [expr $currentOpenCounter * $ratio / 1000]
      .r1.mid.s1 config -text  $currentTRS
      .r2.mid.s2 config -text  $closedCounter
      .r3.mid.s3 config -text  $currentOpenCounter
      .r2.right.c2 create line 0 11 $closedCounterDisp 11 \
			-width 18 -fill darkgreen
      .r3.right.c3 create line 0 11 500 11 -width 18 \
		-fill $backgroundcolor
      .r3.right.c3 create line 0 11 $currentOpenCounterDisp 11 \
			-width 18 -fill red
      .r2.right.c2 create line 0 0 0 24
      .r3.right.c3 create line 0 0 0 24
      update
      set counter 0 
      set thcounter [expr $thcounter + 1]} {
    set counter [expr $counter + 1]}
}

update
.top.left.t1 configure -text "Exit" -borderwidth 4




