#! @TCL@ -f
#
# file: dec_config.tcl
#
# procedures to configure the trace-pass demo
#

# procedure to set up a configuration pop up window
#
proc config_proc {} {

    # declare globals
    #
    global w
    set w ".config"

    global decoder_title

    global mfcc_file
    global mfccfile

    global wordlist_file
    global wrdfile

    global lattice_file
    global latfile

    global lm_file
    global lmfile

    global transcription_file
    global transfile

    global function_mode
    
    global context_mode
    
    global mfcc_format
    
    global data_dir
    global trace_bmp
    
    # don't recreate the window, try to reuse existing one
    #
    if { [winfo exists $w] == 1 } {
        wm deiconify $w
        raise $w
	
    } else {
	
	# set the default values
	#
	set mfccfile $mfcc_file
	set wrdfile $wordlist_file
	set latfile $lattice_file
	set lmfile $lm_file
	set transfile $transcription_file
	set mfcc_format $mfcc_format
	
	
	# create a new window
        #
        toplevel $w
        wm title $w "$decoder_title Configuration"
        wm geometry $w "+200+200"
	
        # make a top, mid and bottom frame
        #
        frame $w.top_f -borderwidth 0
	frame $w.mid_f -borderwidth 0
	frame $w.midf_f -borderwidth 0
        frame $w.foot -borderwidth 1
	
        pack $w.top_f -side top -fill both -expand true 
	pack $w.mid_f -side top -fill both -expand true
	pack $w.midf_f -side top -fill both -expand true
        pack $w.foot -side bottom -fill both -expand true -pady 2
	
	# create button for the Context Mode
	#
	set context_mode "monophone"
	label $w.context_mode -text "Context Mode" -width 20 -anchor w
	radiobutton $w.monophone -text monophone -variable context_mode \
		-value monophone -width 0
	radiobutton $w.cross_word -text crossword -variable context_mode \
		-value cross_word -width 0
	radiobutton $w.word -text word -variable context_mode \
		-value word -width 0
	
	# create the ngram order buttons
	#
	frame $w.lmorder -borderwidth 0 -width 18
	label $w.lmorder.label -text "ngram order:" -width 18
	entry $w.lmorder.entry -width 6 -relief sunken -textvariable \
		gram_size -justify right
	button $w.lmorder.up_order -bitmap "@$trace_bmp/incr.xbm" -command {
	    
	    if {$gram_size < 4} {
		$w.lmorder.entry configure -state normal -foreground black
		incr gram_size 1
	    } else {
		$w.lmorder.entry configure -state disabled -foreground grey
	    }
	}
	button $w.lmorder.dn_order -bitmap "@$trace_bmp/decr.xbm" -command {
	    
	    if {$gram_size > 1 } {

		$w.lmorder.entry configure -state normal -foreground black
		incr gram_size -1
	    } else {
		$w.lmorder.entry configure -state disabled -foreground grey
	    }
	}
	pack $w.lmorder.label -side left -padx 1 -pady 1 -fill both \
		-expand true -in $w.lmorder
	pack $w.lmorder.up_order $w.lmorder.entry $w.lmorder.dn_order \
		-padx 1 -pady 1 -side right -fill both -expand true \
		-in $w.lmorder

	# pack the context_mode and n_gram 
	#
	pack $w.context_mode $w.monophone $w.cross_word $w.word -side left -in $w.midf_f \
		-fill x -padx 1 -pady 1
	pack $w.lmorder -side left -in $w.midf_f -padx 1 -pady 1
	
        bind $w <Return> {config_ok_proc}

        # create buttons to accept / cancel
        #
        button $w.ok -text Ok -command {config_ok_proc} -width 12
        button $w.cancel -text Cancel -command "destroy $w" -width 12

	# create button for the input mfcc format and ngram order
	#
	set mfcc_format "binary"
	label $w.mfcc_format -text "Mfcc format" -width 20 -anchor w
	radiobutton $w.binary -text binary -variable mfcc_format \
		-value binary -width 0
	radiobutton $w.ascii -text ascii -variable mfcc_format \
		-value ascii -width 0
	
	
	# pack the ok, cancel button and the mfcc_format button 
	#
	pack $w.mfcc_format $w.ascii $w.binary -side left -in $w.foot \
		-fill x -padx 1 -pady 1

	pack $w.ok -side right -in $w.foot -fill x -padx 1 -pady 1
        pack $w.cancel -side right -in $w.foot -fill x -padx 1 -pady 1
	
        bind $w <Return> {config_ok_proc}


	set function_mode $function_mode
	
	# create radio buttons for function mode
	#
	label $w.mode_label -text "Function"  -anchor w  -width 20
	radiobutton $w.func_0 -text forced_alignment -variable function_mode \
		-value forced_alignment -command {
	    $w.lat_e configure -state disabled -foreground grey
	    $w.lat_l configure -foreground grey
	    
	    $w.lm_e configure -state disabled -foreground grey
	    $w.lm_l configure -foreground grey

	    $w.trans_e configure -state normal -foreground black
	    $w.trans_l configure -foreground black
	}
	
	radiobutton $w.func_1 -text lattice_rescoring -variable function_mode \
		-value lattice_rescoring -command {
	    $w.lat_e configure -state normal -foreground black
	    $w.lat_l configure -foreground black

	    $w.lm_e configure -state disabled -foreground grey
	    $w.lm_l configure -foreground grey

	    $w.trans_e configure -state disabled -foreground grey
	    $w.trans_l configure -foreground grey
	    
	}
	radiobutton $w.func_2 -text Ngram_decoding -variable function_mode\
		-value ngram_decoding  -command {
	    $w.lat_e configure -state disabled -foreground grey
	    $w.lat_l configure -foreground grey
	
	    $w.lm_e configure -state normal -foreground black
	    $w.lm_l configure -foreground black
	}
	radiobutton $w.func_3 -text lattice_verification -variable \
		function_mode -value  lattice_verification -command {
	    $w.lat_e configure -state normal -foreground black
	    $w.lat_l configure -foreground black

	    $w.lm_e configure -state normal -foreground black
	    $w.lm_l configure -foreground black
	    
	    $w.trans_e configure -state disabled -foreground grey
	    $w.trans_l configure -foreground grey
	}

	pack $w.mode_label $w.func_0 $w.func_1 $w.func_2 $w.func_3 -side left \
		-fill both -expand true -padx 1 -pady 1 -in $w.top_f

	# make frames for lattice entry
	#
	frame $w.lat_f
	pack $w.lat_f -side top -fill both -in $w.mid_f
	    
	# set up the lattice file
	#
	label $w.lat_l -text "Lattice file:" -anchor w -width 20
	entry $w.lat_e -textvariable latfile -width 80 -state normal
	bind $w.lat_e <Return> {config_ok_proc}
	button $w.lat_b -text Browse -command {
	    set latfile [expand_env_proc [tk_filesel .filesel -filemask \
		    "*.*" -directory "$data_dir/lattices"]]
	}
	pack $w.lat_l $w.lat_e $w.lat_b -side left -fill both \
		-expand true -padx 1 -pady 1 -in $w.lat_f
	
	
	# make frames for ngram entry
	#
	frame $w.lm_f
	pack $w.lm_f -side top -fill both -in $w.mid_f
	    
	# set up the ngram file
	#
	label $w.lm_l -text "ngram file:" -anchor w -width 20  -foreground grey
	entry $w.lm_e -textvariable lmfile -width 80 -state disabled \
		-foreground grey
	bind $w.lm_e <Return> {config_ok_proc}
	button $w.lm_b -text Browse -command {
	    set lmfile [expand_env_proc [tk_filesel .filesel -filemask \
		    "*.*" -directory "$data_dir/models"]]
	}
	pack $w.lm_l $w.lm_e $w.lm_b -side left -fill both \
		-expand true -padx 1 -pady 1 -in $w.lm_f


	# make frames for transcription entry
	#
	frame $w.trans_f
	pack $w.trans_f -side top -fill both -in $w.mid_f

	# set up the transcription file
	#
	label $w.trans_l -text "Transcription file:" -anchor w -width 20 \
		-foreground grey
	entry $w.trans_e -textvariable transfile -width 80 -state disabled \
		-foreground grey
	bind $w.trans_e <Return> {config_ok_proc}
	button $w.trans_b -text Browse -command {
	    set transfile [expand_env_proc [tk_filesel .filesel -filemask \
		    "*.*" -directory "$data_dir/trans"]]
	}
	pack $w.trans_l $w.trans_e $w.trans_b -side left -fill both \
		-expand true -padx 1 -pady 1 -in $w.trans_f

	# make frames for input entry
	#
	frame $w.mfcc_f
	pack $w.mfcc_f -side top -fill both -in $w.mid_f

	# set up the input file
	#
	label $w.mfcc_l -text "Input file:" -anchor w -width 20
	entry $w.mfcc_e -textvariable mfccfile -width 80
	bind $w.mfcc_e <Return> {config_ok_proc}
	button $w.mfcc_b -text Browse -command {
	    set mfccfile [expand_env_proc [tk_filesel .filesel -filemask \
		    "*.*" -directory "$data_dir/mfcc"]]
	}
	pack $w.mfcc_l $w.mfcc_e $w.mfcc_b -side left -fill both -expand true \
		-padx 1 -pady 1 -in $w.mfcc_f

	# make frames for output entry
	#
	frame $w.outp_f
	pack $w.outp_f -side top -fill both -in $w.mid_f

	# set up the output file
	#
	label $w.wrd_l -text "Output file:" -anchor w -width 20
	entry $w.wrd_e -textvariable wrdfile -width 80
	bind $w.wrd_e <Return> {config_ok_proc}
	button $w.wrd_b -text Browse -command {
	    set wrdfile [expand_env_proc [tk_filesel .filesel -filemask \
		    "*.*" -directory "$data_dir/tmp"]]
	}
	pack $w.wrd_l $w.wrd_e $w.wrd_b -side left -fill both -expand true \
		-padx 1 -pady 1 -in $w.outp_f

    }
}

# procedure to set entered filenames
#
proc config_ok_proc {} {
 
    # declare globals
    #
    global w
    
    global mfcc_file
    global mfccfile

    global wordlist_file
    global wrdfile

    global lattice_file
    global latfile

    global lm_file
    global lmfile

    global transcription_file
    global transfile
    
    # set the variables
    #
    if {$mfccfile != ""} {
	set mfcc_file $mfccfile
    }
    if {$wrdfile != ""} {
	set wordlist_file $wrdfile
    }
    if {$latfile != ""} {
	set lattice_file $latfile
    }

    if {$lmfile != ""} {
	set lm_file $lmfile
    }

    if {$transfile != ""} {
	set transcription_file $transfile
    }

    # destroy the config application
    #
    destroy $w
    reset_proc

    make_tmp_proc
    
}

# end of file
#
