Tk Source Code

Artifact [bcc6baf9]
Login

Artifact bcc6baf9f3adb2df22c4538a99d99126b9bffa97:

Attachment "ListBoxMotion.tcl" to ticket [960391ff] added by eriklns 2004-05-27 03:16:56.
# ::tk::ListboxMotion --
#
# This procedure is called to process mouse motion events while
# button 1 is down.  It may move or extend the selection, depending
# on the listbox's selection mode.
#
# Arguments:
# w -		The listbox widget.
# el -		The element under the pointer (must be a number).
#
# Improved execution speed and display handling for the extended selection mode
# 25-May-2004, Erik Leunissen
#
proc ::tk::ListboxMotion {w el} {
    variable ::tk::Priv
    if {$el == $Priv(listboxPrev)} {
        return
    }
    set anchor [$w index anchor]
    switch [$w cget -selectmode] {
        browse {
            $w selection clear 0 end
            $w selection set $el
            set Priv(listboxPrev) $el
            event generate $w <<ListboxSelect>>
        }
        extended {
            set p $Priv(listboxPrev)
            if {[string equal {} $p]} {
                # There is no previous element.
                # Set it to the current element.
                $w selection set anchor $el
                if {![info exists Priv(listboxSelection)]} {
                    set Priv(listboxSelection) [$w curselection]
                }
                set Priv(listboxPrev) $el
                event generate $w <<ListboxSelect>>
                return
            }
            
            # Determine the index ranges to select and deselect
            set Priv(listboxPrev) $el
            set a [$w index anchor]
            if {$el > $a} {
                if {$p <= $el} {
                    if {$p < $a} {
                        # p<a<el
                        set deselectRange(0) [list $p [incr a -1]]
                        set selectRange [list [incr a 2] $el]
                    } else {
                        # a<p<el
                        set selectRange [list [incr p] $el]
                    }
                } else {
                    # a<el<p
                    set deselectRange(0) [list [incr el] $p]
                }
            } elseif {$el < $a} {
                if {$p >= $el} {
                    if {$p > $a} {
                        # el<a<p
                        set deselectRange(0) [list [incr a] $p]
                        set selectRange [list $el [incr a -2]]
                    } else {
                        # a<p<el
                        set selectRange [list $el [incr p -1]]
                    }
                } else {
                    # p<el<a
                    set deselectRange(0) [list $p [incr el -1]]
                }
            } else {
                if {$p > $a} {
                    # el=a<p
                    set deselectRange(0) [list [incr a] $p]
                } else {
                    # p<=a=el
                    set deselectRange(0) [list $p [incr a -1]]
                }
            }
            
            # Correct the selection and deselection range for items
            # that existed in a previous selection.
            if {[info exists Priv(listboxSelection)] } {
                set i 0
                foreach index $Priv(listboxSelection) {
                    # deselectRange($i) may have been unset in a previous iteration
                    if {[info exists deselectRange($i)]} {
                        # We do a detailed inspection of the deselectRange to
                        # exclude all unwanted deselections. This may result in
                        # several contiguous deselectRanges that are not adjacent.
                        set begin [lindex $deselectRange($i) 0]
                        set end [lindex $deselectRange($i) 1]
                        if {($index >= $begin) && ($index <= $end)} {
                            # adjust deselectRange($i)
                            if {$begin == $end} {
                                unset deselectRange($i)
                            } elseif {$index == $begin} {
                                set deselectRange($i) [list [incr begin] $end]
                            } elseif {$index == $end} {
                                set deselectRange($i) [list $begin [incr end -1]]
                            } else {
                                # Split the deselectRange into two separate ranges,
                                # each being contiguous. The range with the higher
                                # index values will be subject to inspection in
                                # the next iteration
                                set deselectRange($i) [list $begin [incr index -1]]
                                set deselectRange([incr i]) [list [incr index 2] $end]
                            }
                        }
                    }
                    
                    # We do a course inspection of the selectRange up to the
                    # level where we can decide whether adjustment leaves us
                    # with anything to select. More detailed inspection doesn't
                    # gain much/any performance.
                    if {[info exists selectRange]} {
                        set begin [lindex $selectRange 0]
                        set end [lindex $selectRange 1]
                        if {($index >= $begin) && ($index <= $end)} {
                            if {$begin == $end} {
                                unset selectRange
                            }
                        }
                    }
                }
            }
            
            # deselect the items in each contiguous deselection range
            foreach i [array names deselectRange] {
                $w selection clear [lindex $deselectRange($i) 0] \
                        [lindex $deselectRange($i) 1]
            }
            # select the items in the selectRange
            if {[info exists selectRange]} {
                $w selection set anchor $el
            }
            
            if {![info exists Priv(listboxSelection)]} {
                set Priv(listboxSelection) [$w curselection]
            }
            
            event generate $w <<ListboxSelect>>
        }
    }
}

# EOF