• Jumpy mouse

    From saitology9@saitology9@gmail.com to comp.lang.tcl on Thu Mar 14 17:37:45 2024
    From Newsgroup: comp.lang.tcl

    Can anyone spot what is wrong here?
    You can click and drag he label to move the window.
    However, the mouse jumps to the top left corner, instead of maintaining
    its position relative to the window.


    ### code follows ###########
    package require Tk


    label .l -text "Move me to move the window" -width 40 -height 10
    pack .l -fill both -expand 1

    bind .l <B1-Motion> [list moveme %W %x %y]



    proc moveme {win x y} {
    set top [winfo toplevel $win]
    lassign [split [winfo geometry $top] "+"] dims x1 y1

    set newX [expr {$x1 + $x}]
    set newY [expr {$y1 + $y}]

    wm geometry $top "${dims}+${newX}+${newY}"
    }
    ### end of code ############

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Luc@luc@sep.invalid to comp.lang.tcl on Thu Mar 14 20:42:05 2024
    From Newsgroup: comp.lang.tcl

    On Thu, 14 Mar 2024 17:37:45 -0400, saitology9 wrote:


    ### code follows ###########
    package require Tk

    label .l -text "Move me to move the window" -width 40 -height 10
    pack .l -fill both -expand 1

    bind .l <1> {
    set coords "%x %y"
    lassign $coords ::labelx ::labely
    }
    bind .l <B1-Motion> {
    moveme %W %x %y
    }

    proc moveme {win x y} {
    set top [winfo toplevel $win]
    lassign [split [winfo geometry $top] "+"] dims x1 y1

    set newX [expr {$x1 + $x}]
    set newY [expr {$y1 + $y}]
    set newX [expr {$newX - $::labelx}]
    set newY [expr {$newY - $::labely}]

    wm geometry $top "${dims}+${newX}+${newY}"
    }

    ### end of code ############
    --
    Luc


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Thu Mar 14 19:57:32 2024
    From Newsgroup: comp.lang.tcl

    On 3/14/2024 7:42 PM, Luc wrote:
    On Thu, 14 Mar 2024 17:37:45 -0400, saitology9 wrote:


    Works great; thank you!


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Rich@rich@example.invalid to comp.lang.tcl on Fri Mar 15 01:50:52 2024
    From Newsgroup: comp.lang.tcl

    saitology9 <saitology9@gmail.com> wrote:
    Can anyone spot what is wrong here?
    You can click and drag he label to move the window.
    However, the mouse jumps to the top left corner, instead of
    maintaining its position relative to the window.

    To be padantic, it is not the mouse that jumps, it is the window that
    jumps so that the window's top left is just below the mouse hotspot.
    The mouse remains at exactly the same spot on screen as where it
    started.

    Luc supplied updated code that worked, but did not explain why the code worked. You were not accounting for the position of the mouse (offset
    of mouse pointer) from top left corner when setting up your geometry
    string calculations. The geometry string positions the top left corner
    of the window, so you have to include the mouse offset that was present
    when the button was first depressed to keep the window from jumping to
    be aligned top-left with the mouse.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Fri Mar 15 13:00:28 2024
    From Newsgroup: comp.lang.tcl

    On 3/14/2024 9:50 PM, Rich wrote:
    saitology9 <saitology9@gmail.com> wrote:
    Can anyone spot what is wrong here?
    You can click and drag he label to move the window.
    However, the mouse jumps to the top left corner, instead of
    maintaining its position relative to the window.

    To be padantic, it is not the mouse that jumps, it is the window that

    Not to be pedantic, I won't point out the irony above :-)

    Thank you for the nice description.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From aldo.w.buratti@aldo.w.buratti@gmail.com (abu) to comp.lang.tcl on Mon Mar 18 00:34:02 2024
    From Newsgroup: comp.lang.tcl

    ## It can be simplified

    package require Tk

    label .l -text "Move me to move the window" -width 40 -height 10
    pack .l -fill both -expand 1

    bind .l <1> {
    set top [winfo toplevel %W]
    set ::displacementX [expr {%X-[winfo rootx $top]}]
    set ::displacementY [expr {%Y-[winfo rooty $top]}]
    }
    bind .l <B1-Motion> {
    moveme [winfo toplevel %W] [expr {%X-$::displacementX}] [expr {%Y-$::displacementY}]
    }

    proc moveme {top X Y} {
    wm geometry $top +$X+$Y
    }

    ### end of code ############
    --- Synchronet 3.20a-Linux NewsLink 1.114