source: trunk/gui/scripts/spinint.tcl

Last change on this file was 5659, checked in by ldelgass, 9 years ago

whitespace

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