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

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