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

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

merge (by hand) with Rappture1.2 branch

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