Tk Source Code

Artifact [56a27612]
Login

Artifact 56a2761208e8c01f13c54b559d91bfecef6ef3553dd302517bfaee5bd8ef126c:

Attachment "dragIssue.tcl" to ticket [71a2cadb] added by nab 2022-01-06 21:11:26.
package require Tk
package require tkdnd

catch {namespace delete obj}
namespace eval obj {
    set offset 10
    array set dNDSelection {}
    array set x {}
    array set y {}
}


#############define the proc
proc buildPosition {namespace} {
    set bPoint 1
    set lY 1
    set lX 0
    for {set x 0} {$x<501} {incr x} {
        if {0 == [expr {$x % 20}]} {
            set lY [expr {(30*$bPoint)}]
            set lX 0
            incr bPoint
        }
        set ${namespace}::x($x) [expr {(40*$lX) + 10}]
        set ${namespace}::y($x) $lY
        incr lX
    }
}

proc fromProc {event w X Y action namespace} {
        puts "$X"
    switch $event {
        <<DragInitCmd>> {
            return [list link DND_Text $namespace]
        }
        <<DragEndCmd>>  {
            return
        }
    }
}

proc onReceiveCanvasDropEnterOrPos {win rootX rootY action buttons namespace} {

    namespace upvar $namespace dNDSelection localSelecta
    namespace upvar $namespace offset localOffset
    
    set y [expr {[$win canvasy $rootY] - [winfo rooty $win]}]
    set x [expr {$rootX - [winfo rootx $win]}]
    set init [expr {[$win find withtag to[$win find closest $x $y]] - 2}]
    foreach item [array names localSelecta *] {
            $win itemconfigure $localSelecta($item) -fill black
    }
    

    array set localSelecta {}
    
    for {set this $init} {$this<[expr {$init + $localOffset}]} {incr this} {
        if {[$win itemcget to$this -fill] ne {lightgreen}} {
            $win itemconfigure to$this -fill lightgreen
        }
        #puts "canvas to$this should be [$win itemcget to$this -fill]"
        set localSelecta([expr {$this - $init}]) to$this
    }
    

    return link
 
}


proc onCanvasDropLeave {win namespace} {
    namespace upvar $namespace dNDSelection localSelecta

    foreach item [array names localSelecta *] {
        $win itemconfigure $localSelecta($item) -fill lightgreen
    }
    update idletasks
    array set localSelecta {}
}

proc onCanvasDrop {win action data namespace rootX rootY} {
    onCanvasDropLeave $win $namespace
    return $action
}


#############define the widgets
canvas .from -width 50 -height 800
.from create rectangle 0 0 50 800 -fill black
tkdnd::drag_source register .from DND_Text
bind .from <<DragInitCmd>> [list fromProc %e %W %x %y %A obj]
bind .from <<DragEndCmd>> [list fromProc %e %W %x %y %A obj]


ttk::label .l -text "drag\nfrom\n< here\nto\nthere >"

canvas .to -width 810 -height 800 -bg gray33
tkdnd::drop_target register .to DND_Text

buildPosition obj

for {set x 0} {$x<500} {incr x} {
    .to create rectangle \
    [expr {$::obj::x($x)}] [expr {$::obj::y($x) - 5}] \
    [expr {$::obj::x($x) + 30}] [expr {$::obj::y($x) + 15}] \
    -fill black -tags to$x
}
bind .to <<DropEnter>>    [list onReceiveCanvasDropEnterOrPos %W %X %Y %A %b obj]
bind .to <<DropPosition>> [list onReceiveCanvasDropEnterOrPos %W %X %Y %A %b obj]
bind .to <<Drop>>         [list onCanvasDrop %W %A %D obj %X %Y]
bind .to <<DropLeave>>    [list onCanvasDropLeave %W obj]


################grid everything
grid .from .l .to -sticky ns -padx 10