# ACTIVESTATE TEAPOT-PKG BEGIN TM -*- tcl -*- # -- Tcl Module # @@ Meta Begin # Package canvas::drag 0.1 # Meta as::build::date 2015-05-26 # Meta as::origin http://sourceforge.net/projects/tcllib # Meta category Variations on a canvas # Meta description Manage the dragging of canvas items or item groups # Meta license BSD # Meta platform tcl # Meta require {Tcl 8.5} # Meta require Tk # Meta subject canvas dragging # Meta summary canvas::drag # @@ Meta End # ACTIVESTATE TEAPOT-PKG BEGIN DECLARE package provide canvas::drag 0.1 # ACTIVESTATE TEAPOT-PKG END DECLARE # ACTIVESTATE TEAPOT-PKG END TM ## -*- tcl -*- # ### ### ### ######### ######### ######### # Canvas Behavior Module. Dragging items and groups of items. # ### ### ### ######### ######### ######### ## Requisites package require Tcl 8.5 package require Tk # ### ### ### ######### ######### ######### ## API namespace eval ::canvas::drag { namespace export \ item group on off namespace ensemble create } proc ::canvas::drag::item {c tag args} { # Set up dragging of single items identified by the on $c $tag [namespace code Item1] {*}$args return } proc ::canvas::drag::group {c tag cmdprefix args} { # Set up dragging a group of items, with each group's drag # handle(s) identified by , and the taking the # handle item which triggered the drag and returning a tag which # identifies the whole group to move. on $c $tag [namespace code [list ItemGroup $cmdprefix]] {*}$args return } proc ::canvas::drag::on {c tag cmdprefix args} { # Setting up a general drag, with the drag handles identified by # and providing start/move methods invoked to # initialize and perform the drag. The cmdprefix is fully # responsible for how the dragging of a particular handle is # handled. variable attached # Process options (-event) set events [dict get [Options {*}$args] event] # Save the (canvas, tag) combination for use by 'off'. set k [list $c $tag] set attached($k) $events # Install the bindings doing the drag lassign $events trigger motion untrigger $c bind $tag $trigger [namespace code [list Start $c $cmdprefix %x %y]] $c bind $tag $motion [namespace code [list Move $c $cmdprefix %x %y]] $c bind $tag $untrigger [namespace code [list Done $c $cmdprefix %x %y]] return } proc ::canvas::drag::off {c tag} { # Remove a drag identified by canvas and tag. variable attached # Find and remove the bindings for this particular canvas,tag # combination. set k [list $c $tag] foreach event $attached($k) { $c bind $tag $event {} } # Update our database unset attached($k) return } # ### ### ### ######### ######### ######### ## Option processing. proc ::canvas::drag::Options {args} { # Button 3 is default for dragging. set config [list event [Validate 3]] foreach {option value} $args { switch -exact -- $option { -event { dict set config event [Validate $value] } default { return -code error "Unknown option \"$option\", expected -event" } } } return $config } # ### ### ### ######### ######### ######### ## Event parsing and transformation proc ::canvas::drag::Validate {event} { # Assumes that events are specified in the forms # -