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

Last change on this file since 4767 was 3654, checked in by mmc, 11 years ago

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

File size: 5.6 KB
RevLine 
[3330]1# -*- mode: tcl; indent-tabs-mode: nil -*-
[1491]2
[22]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
[3177]10#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
[115]11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
[22]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 {
[1929]47        entry $itk_interior.entry
[22]48    } {
[1929]49        usual
50        keep -width
51        rename -background -textbackground textBackground Background
52        rename -foreground -textforeground textForeground Foreground
53        rename -highlightbackground -background background Background
[22]54    }
55    pack $itk_component(entry) -side left -expand yes -fill x
56
57    bind $itk_component(entry) <KeyPress> \
[1929]58        [itcl::code $this _validate %A]
[3454]59    bind $itk_component(entry) <Return> \
[1929]60        "$this value \[$itk_component(entry) get\]"
[3454]61    bind $itk_component(entry) <KP_Enter> \
62        "$this value \[$itk_component(entry) get\]"
[22]63
64    itk_component add controls {
[1929]65        frame $itk_interior.cntls
[22]66    }
67    pack $itk_component(controls) -side right
68
[3654]69    itk_component add up {
70        button $itk_component(controls).spinup \
71            -image [Rappture::icon intplus] \
[1929]72            -borderwidth 1 -relief raised -highlightthickness 0 \
[3654]73            -command [itcl::code $this bump up]
[22]74    } {
[1929]75        usual
76        ignore -borderwidth -highlightthickness
[3642]77        rename -background -buttonbackground buttonBackground Background
[22]78    }
[3654]79    pack $itk_component(up) -side left -expand yes -fill both
[22]80
[3654]81    itk_component add down {
82        button $itk_component(controls).spindn \
83            -image [Rappture::icon intminus] \
[1929]84            -borderwidth 1 -relief raised -highlightthickness 0 \
[3654]85            -command [itcl::code $this bump down]
[22]86    } {
[1929]87        usual
88        ignore -borderwidth -highlightthickness
[3642]89        rename -background -buttonbackground buttonBackground Background
[22]90    }
[3654]91    pack $itk_component(down) -side right -expand yes -fill both
[22]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} {
[1929]106        set newval [lindex $args 0]
[22]107
[1929]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        }
[22]116
[1929]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>>]]
[22]120    } elseif {[llength $args] != 0} {
[1929]121        error "wrong # args: should be \"value ?newval?\""
[22]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} {
[1929]135        set delta $itk_option(-delta)
[22]136    } elseif {"down" == $delta} {
[1929]137        set delta [expr {-$itk_option(-delta)}]
[22]138    } elseif {![string is integer $delta]} {
[1929]139        error "bad delta \"$delta\": should be up, down, or integer"
[22]140    }
141
142    set val [$itk_component(entry) get]
143    if {$val == ""} {
[1929]144        set val 0
[22]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]} {
[1929]158        if {![string match "\[0-9\]" $char]} {
159            bell
160            return -code break
161        }
[22]162    }
163}
Note: See TracBrowser for help on using the repository browser.