source: trunk/gui/scripts/numberentry.tcl @ 2035

Last change on this file since 2035 was 1527, checked in by dkearney, 15 years ago

various code cleanups, mainly tabs. adding units ohms and amps, adding appendf interface to the simple buffer so we can append formatted strings.

File size: 7.4 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: NumberEntry - widget for entering numeric values
3#
4#  This widget represents a <number> entry on a control panel.
5#  It is used to enter numeric values.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004-2005  Purdue Research Foundation
9#
10#  See the file "license.terms" for information on usage and
11#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12# ======================================================================
13package require Itk
14
15itcl::class Rappture::NumberEntry {
16    inherit itk::Widget
17
18    itk_option define -state state State "normal"
19
20    constructor {owner path args} { # defined below }
21
22    public method value {args}
23
24    public method label {}
25    public method tooltip {}
26
27    protected method _newValue {}
28
29    private variable _owner ""    ;# thing managing this control
30    private variable _path ""     ;# path in XML to this number
31}
32
33itk::usual NumberEntry {
34    keep -cursor -font
35    keep -foreground -background
36    keep -textbackground
37    keep -selectbackground -selectforeground -selectborderwidth
38}
39
40# ----------------------------------------------------------------------
41# CONSTRUCTOR
42# ----------------------------------------------------------------------
43itcl::body Rappture::NumberEntry::constructor {owner path args} {
44    if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} {
45        error "bad object \"$owner\": should be Rappture::ControlOwner"
46    }
47    set _owner $owner
48    set _path $path
49
50    #
51    # Figure out what sort of control to create
52    #
53    set presets ""
54    foreach pre [$_owner xml children -type preset $path] {
55        lappend presets \
56            [$_owner xml get $path.$pre.value] \
57            [$_owner xml get $path.$pre.label]
58    }
59
60    set class Rappture::Gauge
61    set units [$_owner xml get $path.units]
62    if {$units != ""} {
63        set desc [Rappture::Units::description $units]
64        if {[string match temperature* $desc]} {
65            set class Rappture::TemperatureGauge
66        }
67    }
68
69    #
70    # Create the widget and configure it properly based on other
71    # hints in the XML.
72    #
73    itk_component add gauge {
74        $class $itk_interior.gauge -units $units -presets $presets
75    }
76    pack $itk_component(gauge) -expand yes -fill both
77    bind $itk_component(gauge) <<Value>> [itcl::code $this _newValue]
78
79    set min [$_owner xml get $path.min]
80    if {"" != $min} { $itk_component(gauge) configure -minvalue $min }
81
82    set max [$_owner xml get $path.max]
83    if {"" != $max} { $itk_component(gauge) configure -maxvalue $max }
84
85    if {$class == "Rappture::Gauge" && "" != $min && "" != $max} {
86        set color [$_owner xml get $path.about.color]
87        if {$color == ""} {
88            # deprecated.  Color should be in "about"
89            set color [$_owner xml get $path.color]
90        }
91        if {$color != ""}  {
92            if {$units != ""} {
93                set min [Rappture::Units::convert $min -to $units -units off]
94                set max [Rappture::Units::convert $max -to $units -units off]
95            }
96            # For compatibility. If only one color use white for min
97            if {[llength $color] == 1} {
98                set color [list $min white $max $color]
99            }
100            $itk_component(gauge) configure \
101                -spectrum [Rappture::Spectrum ::#auto $color -units $units]
102        }
103    }
104
105    # if the control has an icon, plug it in
106    set str [$_owner xml get $path.about.icon]
107    if {$str != ""} {
108        $itk_component(gauge) configure -image [image create photo -data $str]
109    }
110
111    eval itk_initialize $args
112
113    #
114    # Assign the default value to this widget, if there is one.
115    #
116    set str [$_owner xml get $path.default]
117    if {"" != $str != ""} { $itk_component(gauge) value $str }
118}
119
120# ----------------------------------------------------------------------
121# USAGE: value ?-check? ?<newval>?
122#
123# Clients use this to query/set the value for this widget.  With
124# no args, it returns the current value for the widget.  If the
125# <newval> is specified, it sets the value of the widget and
126# sends a <<Value>> event.  If the -check flag is included, the
127# new value is not actually applied, but just checked for correctness.
128# ----------------------------------------------------------------------
129itcl::body Rappture::NumberEntry::value {args} {
130    set onlycheck 0
131    set i [lsearch -exact $args -check]
132    if {$i >= 0} {
133        set onlycheck 1
134        set args [lreplace $args $i $i]
135    }
136
137    if {[llength $args] == 1} {
138        if {$onlycheck} {
139            # someday we may add validation...
140            return
141        }
142        set newval [lindex $args 0]
143        $itk_component(gauge) value $newval
144        return $newval
145
146    } elseif {[llength $args] != 0} {
147        error "wrong # args: should be \"value ?-check? ?newval?\""
148    }
149
150    #
151    # Query the value and return.
152    #
153    return [$itk_component(gauge) value]
154}
155
156# ----------------------------------------------------------------------
157# USAGE: label
158#
159# Clients use this to query the label associated with this widget.
160# Reaches into the XML and pulls out the appropriate label string.
161# ----------------------------------------------------------------------
162itcl::body Rappture::NumberEntry::label {} {
163    set label [$_owner xml get $_path.about.label]
164    if {"" == $label} {
165        set label "Number"
166    }
167    return $label
168}
169
170# ----------------------------------------------------------------------
171# USAGE: tooltip
172#
173# Clients use this to query the tooltip associated with this widget.
174# Reaches into the XML and pulls out the appropriate description
175# string.  Returns the string that should be used with the
176# Rappture::Tooltip facility.
177# ----------------------------------------------------------------------
178itcl::body Rappture::NumberEntry::tooltip {} {
179    set str [$_owner xml get $_path.about.description]
180
181    set units [$_owner xml get $_path.units]
182    set min [$_owner xml get $_path.min]
183    set max [$_owner xml get $_path.max]
184
185    if {$units != "" || $min != "" || $max != ""} {
186        append str "\n\nEnter a number"
187
188        if {$min != "" && $max != ""} {
189            append str " between $min and $max"
190        } elseif {$min != ""} {
191            append str " greater than $min"
192        } elseif {$max != ""} {
193            append str " less than $max"
194        }
195
196        if {$units != ""} {
197            set desc [Rappture::Units::description $units]
198            append str " with units of $desc"
199        }
200    }
201    return [string trim $str]
202}
203
204# ----------------------------------------------------------------------
205# USAGE: _newValue
206#
207# Invoked automatically whenever the value in the gauge changes.
208# Sends a <<Value>> event to notify clients of the change.
209# ----------------------------------------------------------------------
210itcl::body Rappture::NumberEntry::_newValue {} {
211    event generate $itk_component(hull) <<Value>>
212}
213
214# ----------------------------------------------------------------------
215# CONFIGURATION OPTION: -state
216# ----------------------------------------------------------------------
217itcl::configbody Rappture::NumberEntry::state {
218    set valid {normal disabled}
219    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
220        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
221    }
222    $itk_component(gauge) configure -state $itk_option(-state)
223}
Note: See TracBrowser for help on using the repository browser.