#
# QCad
# (c) Q Solutions 1998
#
#
#
#
# Obj item Edit
#
#

proc procEditObj {name {item ""}} {
	upvar #0 $name data

	if { $item == "" } {
		set item $data(edit,objTag)
	}
	if { $item == "Group" || $item == "Obj" || ($data(edit,objTag) != $data(edit,objID))} {
		set data(Message) "Cannot edit composite objects"
		bell
		return ""
	}
	set win .edit_$name
	destroy $win
	if { ! [winfo exists $win] } {
		editMakeWin $name $item
		scan [wm geometry [winfo toplevel $data(Win)]] "%dx%d+%d+%d" w h x y
		wm geometry $win +$x+$y
	}
	editFillWin $name $item
	wm deiconify $win
	raise $win
}

proc editMakeWin {name tag} {
	upvar #0 $name data

	set win .edit_$name
	toplevel $win
	wm withdraw $win
	pack [frame $win.top -bd 1 -relief raised] -side top -anc nw -fill x
	pack [label $win.top.title -text "Obj Data for $tag"] -anc c 
	pack [frame $win.cmd -bd 1 -relief raised] -side left -anc nw -fill x
	pack [frame $win.data -bd 1 -relief ridge] -side left -anc nw -fill both -expand 1

	pack [button $win.cmd.b1 -text Hide -command "wm withdraw $win"] -anc nw -fill x
	pack [button $win.cmd.b2 -text Refresh -command "editFillWin $name $tag"] -anc nw -fill x
	pack [button $win.cmd.b3 -text Update -command "editObjUpdate $name $tag"] -anc nw -fill x

}

proc editFillWin { name tag} {
	upvar #0 $name data

	set win .edit_$name

	eval destroy [pack slaves $win.data]
	set c $data(Canvas)
	set type [$c type $tag]
	set coords [$c coords $tag]
	set tags [canvasGetTags $c $tag]
	set opts [canvasGetOpts $c $tag]

	editMakeEntry $name $win.data 1 Tag $tag 0
	editMakeEntry $name $win.data 2 Type $type 0
	editMakeEntry $name $win.data 3 Coords $coords 1

	set i 4
	foreach {opt val} $opts {
		editMakeEntry $name $win.data $i $opt $val 1
		incr i
	}
}
proc editObjUpdate { name tag} {
	upvar #0 $name data

	set c $data(Canvas)
	set type [$c type $tag]
	if { $type == "" } { return ""}

	eval $c coords 	$tag $data(var,Coords)
	foreach {opt val} [canvasGetOpts $c $tag] {
		$c itemconfig $tag $opt $data(var,$opt)
	}
	return ""
}


proc editMakeEntry { name win i var val edit} {
	upvar #0 $name data

	set win $win.fr$i
	pack [frame $win -bd 1 -relief solid] -anc nw -side top -fill x -pady 2
	pack [label $win.l -width 10 -text $var] -anc w -side left -padx 5
	pack [entry $win.e -width 20 -textvariable $name\(var,$var)] -padx 5
	uplevel #0 [list set $name\(var,$var) "$val"]
}


