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


proc domain_put_info {string} {
    global domain_info
    append domain_info $string
}

proc domain_clear_info {} {
    global domain_info
    set domain_info ""
}

proc domain_print_info {} {
    global domain_info
    if {![info exists domain_info] || ($domain_info == "")} {
	mess "No domain file is loaded\n" red
    } else {
	mess "Domain file information:\n"
	mess $domain_info
	mess \n
    }
}

proc ic_start_bigunsop {{reset_verts 0}} {
    global undo_redo uns_cur in_bigunsop
    global uns_cur

    if {([ic_uns_is_loaded]) && ([info exists undo_redo]) 
	&& ([info exists uns_cur]) && ![ic_undo_suspended]} {
	$undo_redo newop
	busy 1
	$undo_redo save_mesh_whole $uns_cur $reset_verts
	busy 0
	set in_bigunsop 1
	ic_undo_suspend
    }
}

proc ic_finish_bigunsop {} {    
    global in_bigunsop
    
    set in_bigunsop 0
    ic_undo_resume
}

proc ic_newop {} {
    global undo_redo
    if [info exists undo_redo] {
	$undo_redo newop
    }
}
set dont_record_command(ic_undo_suspend) 1

proc newop_save {whole {reset_verts 0}} {
    global undo_redo uns_cur

    if [ic_undo_suspended] {
	return
    }
    if {[info exists undo_redo] && [info exists uns_cur]} {
	$undo_redo newop
	busy 1
	$undo_redo save_mesh_whole $uns_cur $reset_verts
	busy 0
    }
}

proc undo_on {on} {
    global undo_redo
    if [info exists undo_redo] {
	if $on { $undo_redo resume_undo} else { $undo_redo suspend_undo }
    }
}

proc undo_redo_update {nu nr} {
    global uns_cur tk_version menus_are_disabled mainwindow_menubar

    if {$uns_cur != ""} {
	$uns_cur configure -modified 1
    }
    
    if {![info exists menus_are_disabled] && [info exists tk_version] &&
	[info exists mainwindow_menubar]} {
	if $mainwindow_menubar {
	    set un .v.top.panel1.extra3
	    set re .v.top.panel1.extra4
	} else {
	    set un .v.top.extras.extra3
	    set re .v.top.extras.extra4
	}
	$un configure -state [expr {$nu ? "normal" : "disabled"}]
	$re configure -state [expr {$nr ? "normal" : "disabled"}]
    }
}

proc after_phase_enable {} {
    global undo_redo

    if [info exists undo_redo] {
	eval undo_redo_update [$undo_redo check]
    }
}
proc undo_tmp_delete_graphics { type num state param val } {
    global projlib
    
    if { $type == "message" || $type == "model"} {
	return ""
    }

    set bsp [$projlib get_object $type $num]
    if {$bsp == ""} return
    set name [$bsp get -name]
    $bsp delete
    set result "$type $name $state"


    return $result
}

proc undo_geom_update_graphics { type num state param val } {
    global projlib
    
    set result ""
    if {$state == "modified"} {
    	if { $type == "message" } {
	    set result $param
	} elseif { $type == "model" } {
	    reinit_geom_objects	    
	    set result "Previous model state restored"
	} else {
	    set bsp [$projlib get_object $type $num]
	    if {$bsp == "" } return
	    if {$val != "NOT_SET"} {
		$bsp configure -$param $val
		if {$param == "family"} {
		    $bsp configure -color [family_color_for_name $val]
		}
	    }
	}
    } elseif { $state == "created" } {
	set bsp [$projlib get_object $type $num]
	if {$bsp != "" } {
	    set name [$bsp get -name]
	    set result "$type $name $state"
	}
    }
    
    return $result
}

proc old_junk_undo_geom_update_graphics { type num state param val } {
    global projlib

    set result ""
    switch $state {
        created {
            set bsp [define_geom_object $type $num]
	    if { $bsp != "" } {
		set name [$bsp get -name]
		update_geom_visible_obj $bsp
		set result "$type $name $state"	
		$projlib signal_change 1
	    } else {
		mess "Error: $type: #$num not found in Undo create operation\n" red
		return $result
	    }
	}
        deleted {
            set bsp [$projlib get_object $type $num]
	    if { $bsp != "" } {
		set name [$bsp get -name]
		delete_geom_object $bsp
		set result "$type $name $state"
	    } else {
		mess "Error: $type: #$num not found in Undo delete operation\n" red
		return $result
	    }
        }
        modified {
	    if { $type == "message" } {
		set result $param
	    } elseif { $type == "model" } {
			reinit_geom_objects	    
			# update_bsp_consistency
		set result "Previous model state restored"
	    } else {
		set bsp [$projlib get_object $type $num]
		if {$bsp != "" } {
		    set name [$bsp get -name]
		    # $projlib configure -modified 1
		    $bsp signal_change 1
		    if {$param != "NOT_SET"} {
			set result "$type $name $param "
		    }
		    if {$val != "NOT_SET"} {
			$bsp configure -$param $val
			if {$param == "family"} {
			    $bsp configure -color [family_color_for_name $val]
			}
			set result "$type $name $state $param $val"
		    }
		}
	    }
        }
    }
    return $result
}
