source: trunk/gui/scripts/flowspeed.tcl @ 1555

Last change on this file since 1555 was 1511, checked in by gah, 15 years ago

remove components from unirect3d class

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