source: branches/1.3/gui/scripts/spinint.tcl @ 4406

Last change on this file since 4406 was 3654, checked in by mmc, 12 years ago

Switched the order of +/- buttons for integer spinners.

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