
# Trash can stuff.
#
# Copyright (c) 2001 ICEM CFD Engineering
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc trash_add {obj {put_undo 1}} {
    global trash_container type_name
    
    if {[$obj getval obtype] == "domain"} {
	user_error "You can't delete the $type_name(domain)"
	return
    }
    if [$obj getval no_delete 0] {
	set tn [$obj getval obtype]
	if [info exists type_name($tn)] { set tn $type_name($tn) }
	user_error "You can't delete the $tn \"[$obj getval name]\"."
	return
    }
    if [$obj getval no_geometry_modify 0] {
	user_error "Please delete that object by changing the settings on the Cabinet form."
	return
    }
    
    set name [$obj getval name]
    set prev [$obj get -model_container]
    set par [$prev getval name]
    $obj setval previous_parent $par
    make_visible $obj 0
    edit_delete_1 $obj 1
    $trash_container add $name $obj
    tree_trash_add $obj
    tree_add_object_children $obj trash_sub_object_node
    
    if $put_undo {
	undo_record_trash $obj 1
    } 
}

proc trash_remove {objs new_parent make_inactive {put_undo 1}} {
    global trash_container model_container_root is_iceboard

    
    foreach obj $objs {
	set ppar [$obj getval previous_parent]
	
	if $is_iceboard {
	    if {$new_parent != $ppar} {
		mess "Moving the object back to its previous location\n" red
		set new_parent ""
	    }
	}
	
	if {$new_parent != ""} {
	    set par $new_parent
	} elseif {$ppar == ""} {
	    set par $model_container_root
	} else {
	    set par [db_get_name $ppar]
	    if {$par == ""} {
		user_error "You can't take \"[$obj getval name]\" out of the trash because there is no longer a part named \"$ppar\""
		continue
	    }
	}
	tree_trash_remove $obj
	$trash_container remove [$obj getval name]
	$par add [$obj getval name] $obj
	$obj configure -model_container $par
	set parsh [$par getval body_shape]
	foreach sh [db_shapes $obj] {
	    $parsh add $sh
	}
	tree_add_object $obj 1
	tree_add_object_children $obj object_node
	make_visible $obj 1
	
	foreach g [$obj getval groups] {
	    coll_add group $g $obj 0
	}
	
	if $make_inactive {
	    $obj setval active 1
	    edit_toggle_current_active $obj 1 ;# this sets it back to inactive
	}
    }
    if $put_undo {
	undo_record_trash $objs 0
    }
}

proc trash_empty {} {
    global trash_container

    if [catch {  ;# there was a bug here that I didn't understand so ...
	foreach obj [$trash_container list_contents] {
	    $trash_container remove [$obj getval name]
	    edit_delete_2 $obj
	}
    } res] {
	global errorInfo
	puts "ERROR: trash_empty: $res\n$errorInfo"
    }
    tree_trash_remove_all
}
