Artifact
7f1da600e0ee02208a4b048b15c08cb5604afe87e1ed7ec99d382913c5075642:
Attachment "exercise_drag.tcl" to
ticket [e3888d58]
added by
erikleunissen
2020-12-04 23:25:21.
#! /bin/sh
# If executed as a shell script, the next line replaces the shell \
exec tclsh "$0" ${1+"$@"}
package require Tk
proc twait {duration} {
set ::_twait 1; after $duration {unset ::_twait}
vwait ::_twait
}
proc setcoords {X Y} {
variable Coords
# flash the widget as visual feedback
.l configure -bg black -fg yellow
set Coords(mouseX) $X
set Coords(mouseY) $Y
set Coords(rootX) [winfo rootx .]
set Coords(rootY) [winfo rooty .]
twait 300
.l configure -bg yellow -fg black
}
proc move {X Y} {
variable Coords
# parray Coords
wm geometry . +[expr {
$Coords(rootX) + $X - $Coords(mouseX)
}]+[expr {
$Coords(rootY) + $Y - $Coords(mouseY)
}]
}
# create and map widget
wm withdraw .
wm overrideredirect . 1; # root window must not have been mapped previously
#label .l -text "drag this" -takefocus 0 -cursor hand2 -bg yellow -highlightthickness 1 -highlightbackground black
label .l -text "drag this" -takefocus 0 -cursor hand2 -bg yellow
pack .l -fill both -expand 1
wm deiconify .
wm geometry . +300+300
twait 1000
# define mouse bindings
bind .l <ButtonPress-1> {setcoords %X %Y}
bind .l <B1-Motion> {move %X %Y}
# simulate user dragging the widget
event generate .l <Motion> -warp 1 -x 10 -y 5 -when now; # mouse moves, no problem
twait 1000
event generate .l <ButtonPress-1> -rootx [winfo pointerx .] -rooty [winfo pointery .] -when now
twait 1000
event generate .l <B1-Motion> -warp 1 -x 50 -y 50 -when now; # *** widget moves, but mouse pointer doesn't ***
twait 1000
event generate .l <ButtonRelease-1> -when now
#update idletasks
#event generate .l <Motion> -warp 1 -x 10 -y 5 -when now; # mouse moves, no problem
# EOF