A Pair of Listboxes Working Together

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 43.  A User Interface to Bindings


The two listboxes in the interface, $frame.key and $frame.cmd, are set up to work as a unit. A selection in one causes a parallel selection in the other. Only one listbox exports its selection as the PRIMARY selection. Otherwise, the last listbox to assert the selection steals the selection rights from the other widget. The following example shows the bind commands from Bind_Interface and the BindSelect routine that selects an item in both listboxes:

Example 43-3 Related listboxes are configured to select items together.
 foreach l [list $frame.key $frame.cmd] {    bind $l <Button-1> \       [list BindSelect %y $frame.key $frame.cmd]    bind $l <B1-Motion> \       [list BindSelect %y $frame.key $frame.cmd] } proc BindSelect { y args } {    foreach w $args {       $w select clear 0 end       $w select anchor [$w nearest $y]       $w select set anchor [$w nearest $y]    } } 

graphics/tip_icon.gif

A scrollbar for two listboxes.


A single scrollbar scrolls both listboxes. The next example shows the scrollbar command from Bind_Interface and the BindYview procedure that scrolls the listboxes:

Example 43-4 Controlling a pair of listboxes with one scrollbar.
 scrollbar $frame.s -orient vertical \    -command [list BindYview [list $frame.key $frame.cmd]] proc BindYview { lists args } {    foreach l $lists {       eval {$l yview}$args    } } 

The BindYview command is used to change the display of the listboxes associated with the scrollbar. The first argument to BindYview is a list of widgets to scroll, and the remaining arguments are added by the scrollbar to specify how to position the display. The details are essentially private between the scrollbar and the listbox. See page 429 for the details. The args keyword is used to represent these extra arguments, and eval is used to pass them through BindYview. The reasoning for using eval like this is explained in Chapter 10 on page 128.

The Listbox class bindings for <Button-2> and <B2-Motion> cause the listbox to scroll as the user drags the widget with the middle mouse button. These bindings are adjusted so that both listboxes move together. The following example shows the bind commands from the Bind_Interface procedure and the BindMark and BindDrag procedures that scroll the listboxes:

Example 43-5 Drag-scrolling a pair of listboxes together.
 bind $l <B2-Motion>\    [list BindDragto %x %y $frame.key $frame.cmd] bind $l <Button-2> \    [list BindMark %x %y $frame.key $frame.cmd] proc BindDragto { x y args } {    foreach w $args {       $w scan dragto $x $y    } } proc BindMark { x y args } {    foreach w $args {       $w scan mark $x $y    } } 

The BindMark procedure does a scan mark that defines an origin, and BindDragto does a scan dragto that scrolls the widget based on the distance from that origin. All Tk widgets that scroll support yview, scan mark, and scan dragto. Thus the BindYview, BindMark, and BindDragto procedures are general enough to be used with any set of widgets that scroll together.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net