
# This code creates the user interface for the mesh editor.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc make_geom_window {w} {
    global geom_visible display_menu_info str_option
    
    #frame $w.f -height 4
    
    checkbutton $w.point -text "Points" -anchor w \
	-variable geom_visible(point) -command "update_geom_visible point v"
    checkbutton $w.curve -text Curves -anchor w \
	-variable geom_visible(curve) -command "update_geom_visible curve v"
    checkbutton $w.surface -text Surfaces -anchor w \
	-variable geom_visible(surface) -command "update_geom_visible surface v"
    checkbutton $w.material -text "Materials" -anchor w \
	-variable geom_visible(material) -command "update_geom_visible material v"
    checkbutton $w.density -text "Density" -anchor w \
	-variable geom_visible(density) -command "update_geom_visible density v"
    checkbutton $w.loop -text "Loops" -anchor w \
	-variable geom_visible(loop) -command "update_geom_visible loop v"
    
    foreach t {point curve surface material density loop} {
	set geom_visible($t) 0
	set str_option($t,dtype) simple
	set str_option($t,stype) wire
	set str_option($t,sizes) none
    }
    set geom_visible(curve) 1
    
    menubutton $w.surface_opt -text Opts -menu $w.surface_opt.m \
	-relief raised -borderwidth 2
    menubutton $w.curve_opt -text Opts -menu $w.curve_opt.m \
	-relief raised -borderwidth 2
    menubutton $w.point_opt -text Opts -menu $w.point_opt.m \
	-relief raised -borderwidth 2
    menubutton $w.material_opt -text Opts -menu $w.material_opt.m \
	-relief raised -borderwidth 2
    menubutton $w.density_opt -text Opts -menu $w.density_opt.m \
	-relief raised -borderwidth 2
    menubutton $w.loop_opt -text Opts -menu $w.loop_opt.m \
	-relief raised -borderwidth 2
    
    menu $w.surface_opt.m -tearoffcommand "disp_set_tearoff_name Surface"
    menu $w.curve_opt.m -tearoffcommand "disp_set_tearoff_name Curve"
    menu $w.point_opt.m -tearoffcommand "disp_set_tearoff_name Point"
    menu $w.material_opt.m -tearoffcommand "disp_set_tearoff_name Material"
    menu $w.density_opt.m -tearoffcommand "disp_set_tearoff_name Density"
    menu $w.loop_opt.m -tearoffcommand "disp_set_tearoff_name Loop"
    
    foreach type {point curve surface material density loop} {
	set m $w.${type}_opt.m
	foreach opt $display_menu_info($type) {
	    switch [llength $opt] {
		0 {
		    $m add separator
		}
		2 {
		    $m add command -label [lindex $opt 0] \
			-command [lindex $opt 1]
		}
		3 {
		    $m add checkbutton -label [lindex $opt 0] \
			-variable str_option([lindex $opt 1]) \
			-command [lindex $opt 2]
		}
		4 {
		    $m add radiobutton -label [lindex $opt 0] \
			-variable str_option([lindex $opt 1]) \
			-value [lindex $opt 2] -command [lindex $opt 3]
		}
	    }
	}
    }
    if ![info exists no_geom_buttons(point)] {
	grid $w.point         -row 1 -column 0  -sticky w
	grid $w.point_opt     -row 1 -column 1  -sticky we
    }
    if ![info exists no_geom_buttons(curve)] {
	grid $w.curve         -row 2 -column 0  -sticky w
	grid $w.curve_opt     -row 2 -column 1  -sticky we
    }
    if ![info exists no_geom_buttons(surface)] {
	grid $w.surface       -row 3 -column 0  -sticky w
	grid $w.surface_opt   -row 3 -column 1  -sticky we
    }
    if ![info exists no_geom_buttons(material)] {
	grid $w.material      -row 4 -column 0  -sticky w
	grid $w.material_opt  -row 4 -column 1  -sticky we
    }
    if ![info exists no_geom_buttons(density)] {
	grid $w.density       -row 5 -column 0  -sticky w
	grid $w.density_opt   -row 5 -column 1  -sticky we
    }
    if ![info exists no_geom_buttons(loop)] {
	grid $w.loop          -row 6 -column 0  -sticky w
	grid $w.loop_opt      -row 6 -column 1  -sticky we
    }
}

proc update_geom_window {} {
    global tk_version
    
    if ![info exists tk_version] return
    
    update_fam_window
}

proc update_geom_visible {what how { redraw 1 } { force 0 } } {
    global str_option geom_visible batch_mode fam_visible fam_visible_prev
    global tdv_default
     
    if {$batch_mode || ![ic_geo_is_loaded] || ![ic_geo_num_objects all]} return 
 
    set str_option(material,wide) 0

    if {$what == "all"} {
        foreach type {surface curve point material density loop body} {
	        update_geom_visible $type $how 0 $force
	    }
	    global fam_active
	    set fam_active ""
	    foreach f [array names fam_visible] {
	        if $fam_visible($f) { lappend fam_active $f }
	    }
	
    } elseif {[info exists str_option($what,dtype)] && [info exists str_option($what,stype)]} {
	    switch $str_option($what,stype) {
	        solid - flat - flat_wire {
		        set shade $str_option($what,stype)
		        if {[info exists str_option($what,stype_previous)] && ($str_option($what,stype_previous) != "solid") && ($str_option($what,stype_previous) != "flat_wire")} {
		            set str_option($what,dwiretype_prev) $str_option($what,dtype)
		            if [info exists str_option($what,dsolidtype_prev)] {
			            set str_option($what,dtype) $str_option($what,dsolidtype_prev)
			            unset str_option($what,dsolidtype_prev)
		            }
		        }
	        }
	        wire {
		        if {[info exists str_option($what,stype_previous)] && ($str_option($what,stype_previous) != "wire")} {
		            set str_option($what,dsolidtype_prev) $str_option($what,dtype)
		            if [info exists str_option($what,dwiretype_prev)] {
			            set str_option($what,dtype) $str_option($what,dwiretype_prev)
			            unset str_option($what,dwiretype_prev)
		            }
		        }
		        set shade wire
	        }
	        default {
		        error "unknown display type $str_option($what,stype)"
	        }
	    }
        # display type
	    switch $str_option($what,dtype) {
	        simplest { set simp 5 }
	        simpler  { set simp 3 }
	        simple   { set simp 1 }
	        detailed { set simp 0 }
	        default  { 
			    error "unknown display type $str_option($what,dtype)"
	        }
	    }
        # display nodes (or not)
	    set dnodes 0
	    if {[info exists str_option($what,nodes)] && $str_option($what,nodes)} {
	        set dnodes 1
	    }
	    set nnum 0
	    if {[info exists str_option($what,node_numbers)] && $str_option($what,node_numbers)} {
	        set nnum 1
	    }
	    set tnum 0
	    if {[info exists str_option($what,tri_numbers)] && $str_option($what,tri_numbers)} {
	        set tnum 1
	    }
	    set norms 0
	    if {[info exists str_option($what,norms)]} {
	        set norms $str_option($what,norms)
	    }
	    set thickness 0
	    if {[info exists str_option($what,thickness)] && $str_option($what,thickness)} {
	        set thickness 1
	    }
	    set count ""
	    if {$what == "curve" && [info exists str_option(curve,count)]} {
	        if $str_option(curve,count) {
		        set count "by_count"
	        }
	        foreach i {0 1 2 3} {
		        lappend count $str_option(curve,show$i)
	        }
	    }
	    set comp 0
        if {$what == "curve" && [info exists str_option(curve,show_composite)]} {
            if $str_option(curve,show_composite) {
                set comp 1
            }
        }
	    set grey_scale 0
        if {$what == "surface" && [info exists str_option(surface,grey_scale)]} {
            if $str_option(surface,grey_scale) {
                set grey_scale 1
            }
        }
	    set transparent 0
        if {$what == "surface" && [info exists str_option(surface,transparent)]} {
            if $str_option(surface,transparent) {
                set transparent 1
            }
        }
	    set protect 0
        if {$what == "point" && [info exists str_option(point,protect)]} {
            if $str_option(point,protect) {
                set protect 1
            }
        }
        set lshow ""; set lfull 0; set lele 0; set lsq 0
	    if {$what == "loop" && [info exists str_option(loop,lshow0)]} {
	        set lele [get_quad_option element]
	        foreach i {0 1 2} {
		        lappend lshow $str_option(loop,lshow$i)
	        }
	        set lfull $str_option(loop,lfull)
	        set lsq $str_option(loop,lsizes)
	    }	
	    set st 0; set sp 0; set sh 0; set sq 0
	    if [info exists str_option($what,sizes)] {
	        switch $str_option($what,sizes) {
	            none  {}
	            tetra { set st 1 }
	            quad  { set sq 1 }
	            prism { set sp 1 }
	            hexa  { set sh 1 }
	        }
	    }
	    set count_quad 0
	    if {[info exists str_option($what,count_quad)] && $str_option($what,count_quad)} {
	        set count_quad 1
	    }
	    set dormant 0
	    if {[info exists str_option($what,dormant)]} {
	        set dormant $str_option($what,dormant)
	    }
    	set show_names($what) 0
	    if [info exists str_option($what,names)] {
	        set show_names($what) $str_option($what,names)
	    }
	    set show_wide($what) 0
	    if [info exists str_option($what,wide)] {
	        set show_wide($what) $str_option($what,wide)
	    }

	    if {($how == "p") || ($how == "a")} {
	        ic_geo_configure_objects $what $simp $shade $st $sp $sh $sq \
		        $show_names($what) $show_wide($what) $dnodes \
		        $count $nnum $tnum $norms $thickness $lshow $lfull $lele \
		        $lsq $comp $grey_scale $transparent $count_quad $dormant $protect
	        set str_option($what,stype_previous) $shade
	    }
	    if {($how == "v") || ($how == "a")} {
	        set on_fams ""
	        set off_fams ""

	        foreach f [array names fam_visible] {
		        if {$force || (![info exists fam_visible_prev($what,$f)] || ($fam_visible($f) != $fam_visible_prev($what,$f))) ||
		            (![info exists fam_visible_prev($what)] || ($geom_visible($what) != $fam_visible_prev($what)))} {
		            if $fam_visible($f) {
			            lappend on_fams $f
			            set fam_visible_prev($what,$f) 1
		            } else {
			            lappend off_fams $f
			            set fam_visible_prev($what,$f) 0
		            }
		        }
	        }
	        set fam_visible_prev($what) $geom_visible($what)
	        if {$on_fams != ""} {
		        ic_geo_update_visibility $what $on_fams $geom_visible($what)
	        }
	        if {$off_fams != ""} {
		        ic_geo_update_visibility $what $off_fams 0
	        }
	        if {$what == "density"} { # Don't belong to any family 
	            ic_geo_update_visibility $what "" $geom_visible($what)
	        }
	        set str_option($what,stype_previous) $shade
	    }
    }
    
    if $redraw {
    	busy 1
	    $tdv_default redraw
	    busy 0
    }
}

proc disp_geo_command_mode_update {in} {
    global geo_command_mode geom_visible
    global tdv_default

    if !$in return
    
# this is turned off because it is extremely annoying for large models.
#    set g $geo_command_mode
#    if {[info exists geom_visible($g)] && !$geom_visible($g)} {
#	set geom_visible($g) 1
#	update_geom_visible $g v
#    }
}

proc gui_present_geometry {obj_list mess answers {center_obj ""}} {
    global present_geo_selection
    global tdv_default

    if {$center_obj == ""} {
        foreach {type names} $obj_list {
            ic_show_geo_selected $type $names 1 white 1
        }
        gui_center_geometry $obj_list
    } else {
        set ctype [lindex $center_obj 0]; set cname [lindex $center_obj 1]
        foreach {type names} $obj_list {
	        foreach n $names {
	            if {$ctype == $type && $cname == $n} {
	                ic_show_geo_selected $type $n 1 yellow 1
	            } else {
	                ic_show_geo_selected $type $n 1 white 1
	            }
	        }
        }
	    gui_center_geometry $center_obj
    }
    
    set n 0
    foreach {key name} $answers {
	    if $n { append mess "; " }
	    set m 0
	    foreach k $key {
	        switch $k {
		        left {
		            bind $tdv_default <ButtonPress-1> { set present_geo_selection $n }
		        }
		        middle {
		            bind $tdv_default <ButtonPress-2> { set present_geo_selection $n }
		            tdv_set_key_command bracketright "set present_geo_selection $n"
		        }
		        right {
		            bind $tdv_default <ButtonPress-3> { set present_geo_selection $n }
		            tdv_set_key_command bracketleft "set present_geo_selection $n"
		        }
		        default {
		            tdv_set_key_command $k "set present_geo_selection $n"
		            tdv_set_key_command [string toupper $k] "set present_geo_selection $n"
		        }
	        }
	        if $m { append mess ", " }
	        append mess "$k"
	        incr m
	    }
	    append mess " = $name"
	    incr n
    }
    append mess . 
    banner $mess
    tkwait variable present_geo_selection
    banner ""
    if {$center_obj == ""} {
        foreach {type names} $obj_list {
            ic_show_geo_selected $type $names 1 white 1
        }
    } else {
        set ctype [lindex $center_obj 0]; set cname [lindex $center_obj 1]
        foreach {type names} $obj_list {
	        foreach n $names {
	            if {$ctype == $type && $cname == $n} {
	                ic_show_geo_selected $type $n 1 yellow 1
	            } else {
	                ic_show_geo_selected $type $n 1 white 1
	            }
	        }
        }
    }
    foreach {key name} $answers {
	    foreach k $key {
	        tdv_set_key_command $k ""
	        tdv_set_key_command [string toupper $k] ""
	    }
    }
    return $present_geo_selection
}

proc gui_center_geometry {obj_list} {
    global tdv_default
    set bb [ic_geo_bounding_box $obj_list]
    tdv_scale_to_box $tdv_default [lindex $bb 0] [lindex $bb 1] 1.2
}

proc disp_toggle_solid {} {
    global str_option
    
    switch $str_option(surface,stype) {
	wire { set str_option(surface,stype) solid }
	solid - flat_wire { set str_option(surface,stype) wire }
    }
    update_geom_visible surface p
}

