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

Last change on this file since 3443 was 3330, checked in by gah, 12 years ago

merge (by hand) with Rappture1.2 branch

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