home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.tcl
- Path: sparky!uunet!eco.twg.com!twg.com!news
- From: "David Herron" <david@twg.com>
- Subject: keyboard-driven listbox widget
- Message-ID: <1992Jul28.215429.19609@twg.com>
- Sensitivity: Personal
- Encoding: 7 TEXT , 82 TEXT
- Sender: news@twg.com (USENET News System)
- Conversion: Prohibited
- Organization: The Wollongong Group, Inc., Palo Alto, CA
- Conversion-With-Loss: Prohibited
- Date: Tue, 28 Jul 1992 21:56:13 GMT
- Lines: 90
-
- Greetings!
-
- The following is code I use for driving listboxes from a keyboard. This
- lets you use UP, DOWN, PgUp and PgDn keys to scroll the listbox. It requires
- that keyboard focus has been pointed at the listbox, one easy way to do
- this is with the FOCUS module.
-
-
- #!/usr/local/bin/wish -f
- #
- # $Id: util.tcl,v 1.4 1992/07/23 05:09:46 david Exp $
- # util -- Some utility procedures.
- #
- # $Log: util.tcl,v $
- # Revision 1.4 1992/07/23 05:09:46 david
- # Add function to bind the listbox properly.
- #
- # Revision 1.3 1992/07/15 04:17:24 david
- # Add code for doing BUSY indicators.
- #
- # Revision 1.2 1992/06/05 03:56:34 david
- # Had problems with foreach{}'s controlled by a [$list curselection]
- # derived list. If we modified the list (added or deleted members) the
- # offsets were thrown off. Meaning the actions acted on the wrong
- # list members. Changed to repeatadly do [$list curselection] ...
- #
- # Revision 1.1 1992/05/27 06:51:29 david
- # Initial revision.
- #
- #
-
-
- # LISTBOX:scroll -- Scroll a listbox by one line in the given direction.
- proc LISTBOX:scroll {list scroll direction} {
- set cur [$scroll get]
- set first [lindex $cur 2]
-
- if { $direction == "down" } {
- set new [expr $first+1]
- $list yview $new
- } else {
- set new [expr $first-1]
- $list yview $new
- }
-
- $list select from $new
- }
-
- # LISTBOX:scrollPage -- Scroll a listbox by one `page' in the given direction.
- # A page is the current `height' of the listbox.
- proc LISTBOX:scrollPage {list scroll direction} {
- set cur [$scroll get]
- set win [lindex $cur 1]
- set first [lindex $cur 2]
-
- if { $direction == "down" } {
- set new [expr $first+$win]
- $list yview $new
- } else {
- set new [expr $first-$win]
- $list yview $new
- }
-
- $list select from $new
- }
-
-
- proc LISTBOX:bind {{list} {scroll}} {
- bind $list <Key-Down> "LISTBOX:scroll %W $scroll down "
- bind $list <Key-Up> "LISTBOX:scroll %W $scroll up "
-
- # On Sun type-4 keyboard these are marked: PgDn & PgUp
- # If this does not work for you, use `xev' to find
- # the values for the keys you prefer to use.
- # <Shift-Up> or <M-Up> are both good possibilities.
- bind $list <Key-F35> "LISTBOX:scrollPage %W $scroll down "
- bind $list <Key-F29> "LISTBOX:scrollPage %W $scroll up "
-
- # Next & Prior are from the HDS FX-15
- bind $list <Key-Next> "LISTBOX:scrollPage %W $scroll down "
- bind $list <Key-Prior> "LISTBOX:scrollPage %W $scroll up "
- }
-
- # LISTBOX:firstSel -- Return the first selected entry.
- proc LISTBOX:firstSel list {
- set l [$list curselection]
- if {$l == ""} { return "" }
- return [lindex $l 0]
- }
-
-