#!nviz -f
# This file contains the following script tools:
#
#    1. Vanilla looping construct - Allows the user to specify a bracket of
#       a script to be looped a specified number of times.
#
#    2. File Seq. looping construct - Allows the user to specify a sequence
#       of files to looped over for a specific section of code.  Specifically,
#       any mapBrowser interactions will use the current file name specified
#       in the current iteration.

destroy .wait_ok

global src_boot

source $src_boot/etc/nviz2.2/scripts/config.tcl

# Create a simple list of script tools which are invoked by pressing
# the specified buttons

# Globals
global loop_stack loop_flag
global floop_stack floop_flag floop_names
global ScriptState

set ScriptState 0
set loop_stack 0
set floop_stack 0
set floop_names [list]

# Tools for vanilla looping
button .loop_start -text "Open Loop"  -command open_loop   -bg aquamarine
button .loop_end   -text "Close Loop" -command close_loop  -bg aquamarine
pack .loop_start .loop_end -side top -fill both -expand 1

# Tools for file sequence looping
button .floop_start -text "Open File Seq. Loop"  -command fopen_loop  -bg burlywood
button .floop_end   -text "Close File Seq. Loop" -command fclose_loop -bg burlywood
pack .floop_start .floop_end -side top -fill both -expand 1

# Tools for more general file sequence looping
button .file_sequence_tools -text "File Sequence Tool" \
    -command fopen_tools -bg cornflowerblue
pack .file_sequence_tools -side top -fill both -expand 1

# Quit button
button .quit -text "Close" -command "exit"
pack .quit -side top -fill both -expand 1

# File Sequence Tools open function
proc fopen_tools {} {
    global src_boot
    global env

    source $src_boot/etc/nviz2.2/scripts/config.tcl

    # Just start the external tools process
    exec nviz -f $default_panel_path/script_file_tools -q &
    exit
}

# File Seq. Loop functions
proc fopen_loop {} {
    global floop_stack floop_flag floop_names ProcessName

    # Determine the list of files to iterate through
    set file_list [create_multimap_browser .browse_new_file all 1]
    if {$file_list == -1 } then { return }

    # Generate new loop variable and increment the loop stack
    set new_name [unique lseq]
    set new_name2 [unique lseq]
    incr floop_stack
    lappend floop_names $new_name2

    # Send nviz necessary commands to add the for loop to the script
    send $ProcessName "Nv_script_add_string \"set $new_name2 \\\$Nv_mapLoopFile\""
    send $ProcessName "Nv_script_add_string \"foreach $new_name \{$file_list\} \{\""
    send $ProcessName "Nv_script_add_string \"set Nv_mapLoopMode 1\""
    send $ProcessName "Nv_script_add_string \"set Nv_mapLoopFile \\\$$new_name\""
}

proc fclose_loop {} {
    global floop_stack floop_names ProcessName

    if $floop_stack then {
	send $ProcessName "Nv_script_add_string \"\}\""
	send $ProcessName "Nv_script_add_string \"set Nv_mapLoopMode 0\""
	incr floop_stack -1
	set old_name [lindex $floop_names $floop_stack]
	send $ProcessName "Nv_script_add_string \"set Nv_mapLoopFile \\\$$old_name\""
    } else {
	bgerror "No corresponding Open File Seq. Loop"
    }
}

# Vanilla Loop functions
proc open_loop {} {
    global loop_stack loop_flag ProcessName

    # Determine how many iterations the loop should go through
    set loop_flag 0
    toplevel .loop_data
    label .loop_data.start_val_l -text "Loop Start Value"
    entry .loop_data.start_val -relief sunken
    label .loop_data.end_val_l   -text "Loop End Value"
    entry .loop_data.end_val -relief sunken
    label .loop_data.incr_val_l  -text "Loop Increment Value"
    entry .loop_data.incr_val -relief sunken
    button .loop_data.ok         -text "Accept" -command "set loop_flag 1"
    button .loop_data.cancel     -text "Cancel" -command "set loop_flag -1"
    pack .loop_data.start_val_l .loop_data.start_val \
	.loop_data.end_val_l .loop_data.end_val \
	.loop_data.incr_val_l .loop_data.incr_val \
	.loop_data.ok .loop_data.cancel -side top -expand 1 -fill both
    grab .loop_data
    tkwait variable loop_flag

    if {$loop_flag == -1} then {
	destroy .loop_data
	return
    }
    
    # Get loop parameters before destroying window
    set loop_start [.loop_data.start_val get]
    set loop_end   [.loop_data.end_val get]
    set loop_incr  [.loop_data.incr_val get]
    destroy .loop_data

    # Generate new loop variable and increment the loop stack
    set new_name [unique lp]
    incr loop_stack
    
    # Send nviz necessary commands to add the for loop to the script
    send $ProcessName "Nv_script_add_string \"for \{set $new_name $loop_start\} \{\\\$$new_name != $loop_end\} \{incr $new_name $loop_incr\} \{\""

}

proc close_loop {} {
    global loop_stack ProcessName

    if $loop_stack then {
	send $ProcessName "Nv_script_add_string \"\}\""
	incr loop_stack -1
    } else {
	bgerror "No corresponding Open Loop"
    }
}








