
# Netscape-style option editor.
#
# Copyright (c) 2003 Ansys Inc..  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
#
# pages is of the form {title setup_func init_func save_func reset_func icon
#    {page page ...}}

proc option_edit {w title save_func pages bottext} {
    global option_edit form_label_text
    
    set buts [list [list "This project" [list option_edit_done $w 0 $save_func]] \
		  [list "All projects" [list option_edit_done $w 1 $save_func]] \
		  [list "Reset all" "option_edit_reset $w"] \
		  cancel help]
    set d [form_init $w $title "" $buts "" 1 1 1]
    if {$d != ""} {
	set ch_opts [list "-weight 0" "-weight 1"]
	set panes [form_paned $d.p $ch_opts h {}]
	pack $d.p -fill both -expand 1 -side top ;# for some strange reason grid doesn't work
	set tw [lindex $panes 0]; set ow [lindex $panes 1]
	set option_edit($w,tree) \
	    [form_tree $tw.t {0 0} \
		 {-relief flat -background white -linesfill gray50 -deltay 21}]
	$option_edit($w,tree) bindText <ButtonRelease-1> "option_edit_click $w"
	$option_edit($w,tree) bindImage <ButtonRelease-1> "option_edit_click $w"
	set option_edit($w,pnum) 0
	
	set subf [form_scrollwin $ow.f 400 500 {0 0 -sticky nwes}]
    
	#set sw [ScrolledWindow $ow.f]
	#pack $ow.f -fill both -expand 1
	#set sf [ScrollableFrame $sw.s]
	#pack $sf -fill both -expand 1
	#set subf [$sf getframe]
	set option_edit($w,optw) $subf
	foreach page $pages {
	    option_edit_fill_tree $w $option_edit($w,tree) $subf root $page
	}
	futil_setup_switch $subf option_edit($w,choice) "" "" 2
	
	if {$bottext != ""} {
	    if [info exists form_label_text($bottext)] {
		set bottext $form_label_text($bottext)
	    }
	    label $d.bottext -text $bottext -justify left
	    pack $d.bottext -side top
	}
	form_finish bot
	grid columnconfigure $w.main 0 -weight 1000
    }
    foreach page $pages {
	option_edit_init_forms $page
    }
    
    option_edit_click $w page1
    $option_edit($w,tree) selection set page1
}

proc option_edit_fill_tree {w tree optw parentnode page} {
    global option_edit nonboldfont
    
    set pname page[incr option_edit($w,pnum)]
    set option_edit($w,$pname,seen) 0
    set option_edit($w,$pname,save_func) [lindex $page 3]
    set option_edit($w,$pname,reset_func) [lindex $page 4]
    set icon [lindex $page 5]
    $tree insert end $parentnode $pname -open 1	-font $nonboldfont \
	-text [form_label_text [lindex $page 0]] \
	-image [tree_get_image $icon]
    
    form_choiceframe $optw.$pname
    if {[lindex $page 1] != ""} { eval [lindex $page 1] $optw.$pname }
    if {$option_edit($w,pnum) == 1} {
	set option_edit($w,choice) $pname
	$tree selection set $pname
    }
    foreach pch [lindex $page 6] {
	option_edit_fill_tree $w $tree $optw $pname $pch
    }
}

proc option_edit_init_forms {page} {
    if {[lindex $page 2] != ""} { eval [lindex $page 2] }
    foreach pch [lindex $page 6] {
	option_edit_init_forms $pch
    }
}

proc option_edit_click {w node} {
    global option_edit
    
    set option_edit($w,choice) $node
    set option_edit($w,$node,seen) 1
}

proc option_edit_done {w allp save_func} {
    global option_edit
    
    form_accept_hack $w
    
    for {set i 1} {$i <= $option_edit($w,pnum)} {incr i} {
	set sf $option_edit($w,page$i,save_func)
	if {$option_edit($w,page$i,seen) && ($sf != "")} {
	    eval $sf $w $allp
	}
    }
    if {$save_func != ""} { eval $save_func $w $allp }
}

proc option_edit_reset {w} {
    global option_edit
    
    for {set i 1} {$i <= $option_edit($w,pnum)} {incr i} {
	set sf $option_edit($w,page$i,reset_func)
	if {$option_edit($w,page$i,seen) && ($sf != "")} {
	    eval $sf
	}
    }
}
