source: trunk/gui/scripts/spinint.tcl @ 22

Last change on this file since 22 was 22, checked in by mmc, 19 years ago

Lots of changes to support Huckel-IV:

  • Support for embedded <tool> declarations
  • New <integer> entry
  • Support for numbers and structures as output elements
  • Atom numbers/symbols in MoleculeViewer?
File size: 5.4 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: spinint - spinner for integer values
3#
4#  This widget is a spinner with up/down arrows for managing integer
5#  values.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004-2005
9#  Purdue Research Foundation, West Lafayette, IN
10# ======================================================================
11package require Itk
12package require BLT
13
14option add *Spinint.width 5 widgetDefault
15option add *Spinint.textBackground white widgetDefault
16
17blt::bitmap define Spinint-up {
18#define up_width 8
19#define up_height 4
20static unsigned char up_bits[] = {
21   0x10, 0x38, 0x7c, 0xfe};
22}
23
24blt::bitmap define Spinint-down {
25#define arrow_width 8
26#define arrow_height 4
27static unsigned char arrow_bits[] = {
28   0xfe, 0x7c, 0x38, 0x10};
29}
30
31itcl::class Rappture::Spinint {
32    inherit itk::Widget
33
34    itk_option define -min min Min ""
35    itk_option define -max max Max ""
36    itk_option define -delta delta Delta 1
37
38    constructor {args} { # defined below }
39
40    public method value {args}
41    public method bump {{delta up}}
42
43    protected method _validate {char}
44}
45
46itk::usual Spinint {
47    keep -cursor -font
48    keep -foreground -background
49    keep -textforeground -textbackground
50    keep -selectbackground -selectforeground -selectborderwidth
51}
52
53# ----------------------------------------------------------------------
54# CONSTRUCTOR
55# ----------------------------------------------------------------------
56itcl::body Rappture::Spinint::constructor {args} {
57    itk_component add entry {
58        entry $itk_interior.entry
59    } {
60        usual
61        keep -width
62        rename -background -textbackground textBackground Background
63        rename -foreground -textforeground textForeground Foreground
64        rename -highlightbackground -background background Background
65    }
66    pack $itk_component(entry) -side left -expand yes -fill x
67
68    bind $itk_component(entry) <KeyPress> \
69        [itcl::code $this _validate %A]
70    bind $itk_component(entry) <KeyPress-Return> \
71        "$this value \[$itk_component(entry) get\]"
72
73    itk_component add controls {
74        frame $itk_interior.cntls
75    }
76    pack $itk_component(controls) -side right
77
78    itk_component add up {
79        button $itk_component(controls).spinup -bitmap Spinint-up \
80            -borderwidth 1 -relief raised -highlightthickness 0 \
81            -command [itcl::code $this bump up]
82    } {
83        usual
84        ignore -borderwidth -highlightthickness
85    }
86    pack $itk_component(up) -side top -expand yes -fill both
87
88    itk_component add down {
89        button $itk_component(controls).spindn -bitmap Spinint-down \
90            -borderwidth 1 -relief raised -highlightthickness 0 \
91            -command [itcl::code $this bump down]
92    } {
93        usual
94        ignore -borderwidth -highlightthickness
95    }
96    pack $itk_component(down) -side bottom -expand yes -fill both
97
98    eval itk_initialize $args
99}
100
101# ----------------------------------------------------------------------
102# USAGE: value ?<newval>?
103#
104# Clients use this to query/set the value for this widget.  With
105# no args, it returns the current value for the widget.  If the
106# <newval> is specified, it sets the value of the widget and
107# sends a <<Value>> event.
108# ----------------------------------------------------------------------
109itcl::body Rappture::Spinint::value {args} {
110    if {[llength $args] == 1} {
111        set newval [lindex $args 0]
112
113        if {"" != $newval} {
114            if {"" != $itk_option(-min) && $newval < $itk_option(-min)} {
115                set newval $itk_option(-min)
116            }
117            if {"" != $itk_option(-max) && $newval > $itk_option(-max)} {
118                set newval $itk_option(-max)
119            }
120        }
121
122        $itk_component(entry) delete 0 end
123        $itk_component(entry) insert 0 $newval
124        after 10 [list event generate $itk_component(hull) <<Value>>]
125    } elseif {[llength $args] != 0} {
126        error "wrong # args: should be \"value ?newval?\""
127    }
128    return [$itk_component(entry) get]
129}
130
131# ----------------------------------------------------------------------
132# USAGE: bump ?<delta>?
133#
134# Used internally when you click on the up/down arrows.  Clients
135# can also use it directly to bump values up/down.  The optional
136# <delta> can be an integer value or the keyword "up" or "down".
137# ----------------------------------------------------------------------
138itcl::body Rappture::Spinint::bump {{delta up}} {
139    if {"up" == $delta} {
140        set delta $itk_option(-delta)
141    } elseif {"down" == $delta} {
142        set delta [expr {-$itk_option(-delta)}]
143    } elseif {![string is integer $delta]} {
144        error "bad delta \"$delta\": should be up, down, or integer"
145    }
146
147    set val [$itk_component(entry) get]
148    if {$val == ""} {
149        set val 0
150    }
151    value [expr {$val+$delta}]
152}
153
154# ----------------------------------------------------------------------
155# USAGE: _validate <char>
156#
157# Validates each character as it is typed into the spinner.
158# If the <char> is not a digit, then this procedure beeps and
159# prevents the character from being inserted.
160# ----------------------------------------------------------------------
161itcl::body Rappture::Spinint::_validate {char} {
162    if {[string match "\[ -~\]" $char]} {
163        if {![string match "\[0-9\]" $char]} {
164            bell
165            return -code break
166        }
167    }
168}
Note: See TracBrowser for help on using the repository browser.