
set Echo 0

proc itemStartDrag {c x y} {
    global firstX firstY lastX lastY
    set lastX [$c canvasx $x]
    set lastY [$c canvasy $y]
    set firstX $lastX
    set firstY $lastY
    $c addtag dragged withtag current

    global Echo
    if {$Echo == 0} {
	return
    }

    global Others
    set id [$c find withtag dragged]
puts "id=$id"
    set coords [$c coords $id]
    set x [expr ([lindex $coords 0] + [lindex $coords 2]) / 2]
    set y [expr ([lindex $coords 1] + [lindex $coords 3]) / 2]
    foreach w $Others($c) {
	$w create rectangle [expr $x - 6] [expr $y - 4] [expr $x + 10] [expr $y + 8] \
		-fill red -tags icon
	$w create rectangle [expr $x - 10] [expr $y - 8] [expr $x + 6] [expr $y + 4] \
		-fill red -tags icon
    }
}

proc itemDrag {c x y} {
    global lastX lastY
    set x [$c canvasx $x]
    set y [$c canvasy $y]

    $c move dragged [expr $x-$lastX] [expr $y-$lastY]

    global Echo
    if {$Echo == 0} {
	global Others
	set id [$c find withtag dragged]
	foreach w $Others($c) {
		$w move $id [expr $x-$lastX] [expr $y-$lastY]
	}
    }

    set lastX $x
    set lastY $y
}

proc itemStopDrag {c x y} {
    global Echo
    if {$Echo == 0} {
	$c dtag dragged
	return
    }

    global Others firstX firstY lastX lastY
    set id [$c find withtag dragged]
    set xinc [expr ($lastX - $firstX) / 10]
    set yinc [expr ($lastY - $firstY) / 10]
    foreach w $Others($c) {
	move $w $id $xinc $yinc 10
	$w delete icon
    }
    $c dtag dragged
}

proc move {w id xinc yinc nb} {
	$w move $id $xinc $yinc
	incr nb -1
	if {$nb > 0} {
		after 100 [list move $w $id $xinc $yinc $nb]
	}
	update idletasks
}

toplevel .t
pack [canvas .c -width 200 -height 200]
pack [canvas .t.c -width 200 -height 200]

set Others(.c) .t.c
set Others(.t.c) .c

wm title . Michel
wm title .t Alain

wm geometry . +50+50
wm geometry .t +300+50

foreach w {.c .t.c} {
	$w bind item <ButtonPress-1> "itemStartDrag %W %x %y"
	$w bind item <B1-Motion> "itemDrag %W %x %y"
	$w bind item <ButtonRelease-1> "itemStopDrag %W %x %y"

	bind $w <Enter> {
		focus %W
	}
	bind $w <Key> {
		global Echo
		set Echo [expr 1 - $Echo]
	}

	$w create oval 10 10 60 40 -fill blue -tags item
	$w create rectangle 30 50 90 80 -fill green -tags item
}

