#! @TCL@ -f
# File : dec_display.tcl
#

# This file sets up the display for the trace passing demo
#

# import globals
#
global p

# withdraw the existing top level so that a new one can be set
#
wm withdraw .

# if required the top level may be destroyed
#
catch {destroy $p}

# now set up the new top level
#
toplevel $p

# set window manager geometry
#
wm title $p $decoder_title
wm geometry $p "+0+0"
wm minsize $p 800 600

# if the window is deleted the program should quit
#
wm protocol $p WM_DELETE_WINDOW {
    quit_proc
    exit
}

# create the two major frames
#
frame $p.display_f -borderwidth 1 -relief flat -height 600 -width 600
frame $p.control_f -borderwidth 1 -relief flat -height 600 -width 200

# pack the two major frames
#
pack $p.display_f -side left -fill both -expand true
pack $p.control_f -side right -fill both

# subframes for display
#
frame $p.words_f -borderwidth 0 -relief flat -height 360 -width 600
frame $p.phones_f -borderwidth 0 -relief flat -height 150 -width 600
frame $p.stats_f -borderwidth 0 -relief flat -height 90 -width 600
pack $p.words_f -side top -padx 1 -pady 1 -fill both -expand true \
	-in $p.display_f
pack $p.phones_f $p.stats_f -side top -padx 1 -pady 1 -fill both \
	-in $p.display_f

# subframes for control
#
frame $p.logo_f -borderwidth 0 -relief flat -height 40 -width 200
frame $p.prm1_f -borderwidth 0 -relief flat -height 40 -width 200
frame $p.prm2_f -borderwidth 0 -relief flat -height 40 -width 200
frame $p.prm3_f -borderwidth 0 -relief flat -height 160 -width 200
frame $p.val1_f -borderwidth 0 -relief flat -height 40 -width 200
frame $p.val2_f -borderwidth 0 -relief flat -height 40 -width 200
frame $p.ctr1_f -borderwidth 0 -relief flat -height 40 -width 200
frame $p.ctr2_f -borderwidth 0 -relief flat -height 40 -width 200
frame $p.ctr3_f -borderwidth 0 -relief flat -height 40 -width 200
pack $p.logo_f $p.prm1_f $p.prm2_f $p.prm3_f -side top -padx 1 \
	-pady 5 -fill both -in $p.control_f
pack $p.val1_f -side top -padx 1 -pady 5 -fill both -expand true \
	-in $p.control_f
pack $p.val2_f -side top -padx 1 -pady 5 -fill both -in $p.control_f
pack $p.ctr3_f $p.ctr2_f $p.ctr1_f -side bottom -padx 1 -pady 5 \
	-fill both -in $p.control_f

# set up the isip logo
#
image create photo isip_logo -file "$trace_bmp/isip_main_logo.gif" \
	-format gif -palette 255/255/255
label $p.isip_logo1 -text "ISIP Decoder"
label $p.isip_logo2 -image isip_logo
pack $p.isip_logo1 $p.isip_logo2 -side left -fill both -expand true \
	-in $p.logo_f

# set up the parameters display
#
frame $p.cr1 -borderwidth 0 -relief flat -height 40 -width 200
frame $p.cr2 -borderwidth 0 -relief flat -height 40 -width 200
pack $p.cr1 $p.cr2 -side top -padx 1 -pady 1 -fill both -expand true \
	-in $p.prm1_f

# title label
#
label $p.par -text "Parameters" -width 18 -relief raised -bg grey -fg black
pack $p.par -side left -padx 1 -pady 1 -fill both -expand true -in $p.cr1

# set up the n-best entry
#
label $p.n_best -text "N-best:" -width 18
entry $p.n_best_entry -width 6 -relief sunken -textvariable num_n_best \
	-justify right
button $p.up_n_best -bitmap "@$trace_bmp/incr.xbm" -command {
    incr num_n_best 1
    if {$num_n_best > $max_n_best} {
	set num_n_best $max_n_best
    }
    reset_proc
}
button $p.dn_n_best -bitmap "@$trace_bmp/decr.xbm" -command {
    incr num_n_best -1
    if {$num_n_best < $min_n_best} {
	set num_n_best $min_n_best
    }
    reset_proc
}
pack $p.n_best -padx 1 -pady 1 -side left -fill both -expand true -in $p.cr2
pack $p.up_n_best $p.n_best_entry $p.dn_n_best -side right -padx 1 -pady 1 \
	-fill both -expand true -in $p.cr2

# set up the the step size entry
#
label $p.step -text "Step size:" -width 18
entry $p.step_entry -width 6 -relief sunken -textvariable num_step \
	-justify right
button $p.up_step -bitmap "@$trace_bmp/incr.xbm" -command {
    incr num_step 1
}
button $p.dn_step -bitmap "@$trace_bmp/decr.xbm" -command {
    incr num_step -1
    if {$num_step < 0} {
	set num_step 0
    }
}
pack $p.step -side left -padx 1 -pady 1 -fill both -expand true -in $p.prm2_f
pack $p.up_step $p.step_entry $p.dn_step -padx 1 -pady 1 -side right \
	-fill both -expand true -in $p.prm2_f

# set up the scale factors for beam pruning
#
frame $p.pr1 -borderwidth 0 -relief flat -height 40 -width 200
frame $p.pr2 -borderwidth 0 -relief flat -height 40 -width 200
frame $p.pr3 -borderwidth 0 -relief flat -height 40 -width 200
frame $p.pr4 -borderwidth 0 -relief flat -height 40 -width 200
frame $p.pr5 -borderwidth 0 -relief flat -height 40 -width 200
pack $p.pr1 $p.pr2 $p.pr3 $p.pr4 $p.pr5 -side top -padx 1 -pady 1 \
	-fill both -expand true -in $p.prm3_f

# title label
#
label $p.prune -text "Pruning" -width 18 -relief raised -bg grey -fg black
pack $p.prune -side left -padx 1 -pady 1 -fill both -expand true -in $p.pr1

# state-level beam pruning
#
label $p.stprune -text "State beam:" -width 18
entry $p.st_ent -width 6 -relief sunken -textvariable st_beam -justify right
button $p.st_inc -bitmap "@$trace_bmp/incr.xbm" -command {
    set st_beam [expr $st_beam + $beam_step]
}
button $p.st_dec -bitmap "@$trace_bmp/decr.xbm" -command {
    set st_beam [expr $st_beam - $beam_step]
    if {$st_beam < $beam_min} {
	set st_beam $beam_min
    }
}
pack $p.stprune -side left -padx 1 -pady 1 -fill both -expand true -in $p.pr2
pack $p.st_inc $p.st_ent $p.st_dec -side right -padx 1 -pady 1 -fill both \
	-expand true -in $p.pr2

# phone-level beam pruning
#
label $p.phprune -text "Phone beam:" -width 18
entry $p.ph_ent -width 6 -relief sunken -textvariable ph_beam -justify right
button $p.ph_inc -bitmap "@$trace_bmp/incr.xbm" -command {
    set ph_beam [expr $ph_beam + $beam_step]
}
button $p.ph_dec -bitmap "@$trace_bmp/decr.xbm" -command {
    set ph_beam [expr $ph_beam - $beam_step]
    if {$ph_beam < $beam_min} {
	set ph_beam $beam_min
    }
}
pack $p.phprune -side left -padx 1 -pady 1 -fill both -expand true -in $p.pr3
pack $p.ph_inc $p.ph_ent $p.ph_dec -side right -padx 1 -pady 1 -fill both \
	-expand true -in $p.pr3

# word-level beam pruning
#
label $p.wdprune -text "Word beam:" -width 18
entry $p.wd_ent -width 6 -relief sunken -textvariable wd_beam -justify right
button $p.wd_inc -bitmap "@$trace_bmp/incr.xbm" -command {
    set wd_beam [expr $wd_beam + $beam_step]
}
button $p.wd_dec -bitmap "@$trace_bmp/decr.xbm" -command {
    set wd_beam [expr $wd_beam - $beam_step]
    if {$wd_beam < $beam_min} {
	set wd_beam $beam_min
    }
}
pack $p.wdprune -side left -padx 1 -pady 1 -fill both -expand true -in $p.pr4
pack $p.wd_inc $p.wd_ent $p.wd_dec -side right -padx 1 -pady 1 -fill both \
	-expand true -in $p.pr4

# maximum active trace pruning
#
label $p.matprune -text "MAPMI:" -width 18
entry $p.mat_ent -width 6 -relief sunken -textvariable mapmi -justify right
button $p.mat_inc -bitmap "@$trace_bmp/incr.xbm" -command {
    set mapmi [expr $mapmi + $mat_step]
}
button $p.mat_dec -bitmap "@$trace_bmp/decr.xbm" -command {
    set mapmi [expr $mapmi - $mat_step]
    if {$mapmi < $mat_min} {
	set mapmi $mat_min
    }
}
pack $p.matprune -side left -padx 1 -pady 1 -fill both -expand true -in $p.pr5
pack $p.mat_inc $p.mat_ent $p.mat_dec -side right -padx 1 -pady 1 -fill both \
	-expand true -in $p.pr5

# create the message display area
#
frame $p.v1 -borderwidth 0 -relief flat -height 40 -width 200
frame $p.v2 -borderwidth 0 -relief flat -height 40 -width 200
pack $p.v1 -side top -padx 1 -pady 1 -fill both -in $p.val1_f
pack $p.v2 -side top -padx 1 -pady 1 -fill both -expand true -in $p.val1_f

# title label
#
label $p.msg -text "Messages" -width 18 -relief raised -bg grey -fg black
pack $p.msg -side left -padx 1 -pady 1 -fill both -expand true -in $p.v1

# set up a display message area
#
message $p.message -width 180 -justify left -anchor nw -relief sunken
pack $p.message -padx 1 -pady 1 -fill both -expand true -in $p.v2

# set up the current frame display
#
label $p.curr_f -text "Current Frame:" -width 18 -relief flat -anchor w
label $p.curr_f_ind -textvariable current_frame -relief sunken -width 12
pack $p.curr_f -side left -padx 1 -pady 1 -fill both -in $p.val2_f
pack $p.curr_f_ind -side right -padx 1 -pady 1 -fill both -in $p.val2_f

# set up the control buttons
#
frame $p.bt1 -borderwidth 0 -relief flat -height 40 -width 200
frame $p.bt2 -borderwidth 0 -relief flat -height 40 -width 200
pack $p.bt1 $p.bt2 -side top -padx 1 -pady 1 -fill both -expand true \
	-in $p.ctr1_f

# title label
#
label $p.bt -text "Controls" -width 18 -relief raised -bg grey -fg black
pack $p.bt -side left -padx 1 -pady 1 -fill both -expand true -in $p.bt1


button $p.reset -text "Reset" -width 10 -command {
    reset_proc
    set num_step 1
    set num_n_best 5
}
button $p.config -text "Configure" -width 10 -command {
    config_proc
}
pack $p.reset -side left -padx 1 -pady 1 -fill both -expand true \
	-in $p.bt2
pack $p.config -side right -padx 1 -pady 1 -fill both -expand true \
	-in $p.bt2

button $p.run -text "Run" -width 10 -command {
    run_proc
}
button $p.next -text "Next Frame" -width 10 -command {
    next_frame_proc
    message_proc "Please press next or run to continue"
}
pack $p.run -side left -padx 1 -pady 1 -fill both -expand true \
	-in $p.ctr2_f
pack $p.next -side right -padx 1 -pady 1 -fill both -expand true \
	-in $p.ctr2_f

button $p.stop -text "Stop" -width 10 -state disabled -command {
    if {$stop_flag == 0} {
	set stop_flag 1
    } else {
	set stop_flag 0
    }
}
button $p.quit -text "Quit" -width 10 -command {
    quit_proc
}
pack $p.stop -side left -padx 1 -pady 1 -fill both -expand true \
	-in $p.ctr3_f
pack $p.quit -side right -padx 1 -pady 1 -fill both -expand true \
	-in $p.ctr3_f

# the canvas widget to draw the graph of connections
#
frame $p.wd1 -borderwidth 0 -relief flat -height 30 -width 600
frame $p.wd2 -borderwidth 0 -relief flat -height 300 -width 600
frame $p.wd2a -borderwidth 0 -relief flat -height 300 -width 570
frame $p.wd2b -borderwidth 0 -relief flat -height 300 -width 30
frame $p.wd3 -borderwidth 0 -relief flat -height 30 -width 600
pack $p.wd1 -side top -padx 1 -pady 1 -fill x -in $p.words_f
pack $p.wd2 -side top -padx 1 -pady 1 -fill both -expand true -in $p.words_f
pack $p.wd2a -side left -padx 1 -pady 1 -fill both -expand true -in $p.wd2
pack $p.wd2b -side right -padx 1 -pady 1 -fill y -in $p.wd2
pack $p.wd3 -side top -padx 1 -pady 1 -fill x -in $p.words_f

label $p.word_label -text "Word Network" -relief raised -bg grey -fg black
canvas $p.words -height $word_height -width $canvas_width -relief sunken \
	-yscrollcommand "$p.y1scroll set" -xscrollcommand "$p.x1scroll set" \
	-borderwidth 1
scrollbar $p.y1scroll -command "$p.words yview" -borderwidth 1
scrollbar $p.x1scroll -command "$p.words xview" -orient horizontal \
	-borderwidth 1

pack $p.word_label -fill both -expand true -in $p.wd1
pack $p.y1scroll -fill both -expand true  -in $p.wd2b
pack $p.words -fill both -expand true -in $p.wd2a
pack $p.x1scroll -fill both -expand true  -in $p.wd3

# the canvas widget to draw the phone sequences
#
frame $p.ph1 -borderwidth 0 -relief flat -height 30 -width 600
frame $p.ph2 -borderwidth 0 -relief flat -height 300 -width 600
frame $p.ph2a -borderwidth 0 -relief flat -height 300 -width 570
frame $p.ph2b -borderwidth 0 -relief flat -height 300 -width 30
frame $p.ph3 -borderwidth 0 -relief flat -height 30 -width 600
pack $p.ph1 -side top -padx 1 -pady 1 -fill x -in $p.phones_f
pack $p.ph2 -side top -padx 1 -pady 1 -fill both -expand true -in $p.phones_f
pack $p.ph2a -side left -padx 1 -pady 1 -fill both -expand true -in $p.ph2
pack $p.ph2b -side right -padx 1 -pady 1 -fill y -in $p.ph2
pack $p.ph3 -side top -padx 1 -pady 1 -fill x -in $p.phones_f

label $p.phone_label -text "Phone Sequence" -relief raised -bg grey -fg black
canvas $p.phones -height $phone_height -width $canvas_width -relief sunken \
	-yscrollcommand "$p.y2scroll set" -xscrollcommand "$p.x2scroll set" \
	-borderwidth 1
scrollbar $p.y2scroll -command "$p.phones yview" -borderwidth 1
scrollbar $p.x2scroll -command "$p.phones xview" -orient horizontal \
	-borderwidth 1

pack $p.phone_label -fill both -expand true -in $p.ph1
pack $p.y2scroll -fill both -expand true  -in $p.ph2b
pack $p.phones -fill both -expand true -in $p.ph2a
pack $p.x2scroll -fill both -expand true  -in $p.ph3

# the text widget to display the stat sequences
#
frame $p.tr1 -borderwidth 0 -relief flat -height 30 -width 600
frame $p.tr2 -borderwidth 0 -relief flat -height 300 -width 600
frame $p.tr2a -borderwidth 0 -relief flat -height 300 -width 570
frame $p.tr2b -borderwidth 0 -relief flat -height 300 -width 30
frame $p.tr3 -borderwidth 0 -relief flat -height 30 -width 600
pack $p.tr1 -side top -padx 1 -pady 1 -fill x -in $p.stats_f
pack $p.tr2 -side top -padx 1 -pady 1 -fill both -expand true -in $p.stats_f
pack $p.tr2a -side left -padx 1 -pady 1 -fill both -expand true -in $p.tr2
pack $p.tr2b -side right -padx 1 -pady 1 -fill y -in $p.tr2
pack $p.tr3 -side top -padx 1 -pady 1 -fill x -in $p.stats_f

label $p.stat_label -text "Path Statistics" -relief raised -bg grey -fg black
text $p.stats -height 7 -width 10 -relief sunken \
	-yscrollcommand "$p.y3scroll set" -xscrollcommand "$p.x3scroll set" \
	-borderwidth 1 -font $font_2 -state disabled
scrollbar $p.y3scroll -command "$p.stats yview" -borderwidth 1
scrollbar $p.x3scroll -command "$p.stats xview" -orient horizontal \
	-borderwidth 1

pack $p.stat_label -fill both -expand true -in $p.tr1
pack $p.y3scroll -fill both -expand true  -in $p.tr2b
pack $p.stats -fill both -expand true -in $p.tr2a
pack $p.x3scroll -fill both -expand true  -in $p.tr3

# define key bindings
#
bind $p <Alt-n> {
    $p config -cursor watch
    next_frame_proc
    $p config -cursor top_left_arrow
}

bind $p <Alt-c> {config_proc}
bind $p <Alt-q> {quit_proc}
bind $p <Alt-r> {reset_proc}

bind $p <Alt-z> {
    for {set fr $current_frame} {$fr < $num_frames} {incr fr} {
	next_frame_proc
    }
}

#
# end of file
