source: branches/1.6/gui/scripts/numberentry.tcl @ 6363

Last change on this file since 6363 was 5851, checked in by mmh, 9 years ago

bug fix to correctly get tool.uq

File size: 8.1 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    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    set varname [lindex [split $path ()] end-1]
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        set value [string trim [$_owner xml get $path.$pre.value]]
57        set label [string trim [$_owner xml get $path.$pre.label]]
58        lappend presets $value $label
59    }
60
61    set class Rappture::Gauge
62    set units [string trim [$_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    # check for number UQ flag
71    set use_uq [$_owner xml get $path.uq]
72    if {$use_uq == ""} {
73        # check for global UQ flag
74        set use_uq [[$_owner tool] xml get tool.uq]
75    }
76
77    if {$use_uq == ""} {
78        set use_uq no
79    } else {
80        set use_uq [string trim $use_uq]
81    }
82
83    #
84    # Create the widget and configure it properly based on other
85    # hints in the XML.
86    #
87    itk_component add gauge {
88        $class $itk_interior.gauge -uq $use_uq -units $units -presets $presets \
89            -log $path -varname $varname -label [label]
90    }
91    pack $itk_component(gauge) -expand yes -fill both
92    bind $itk_component(gauge) <<Value>> [itcl::code $this _newValue]
93
94    set min [string trim [$_owner xml get $path.min]]
95    if {$min ne ""} {
96        $itk_component(gauge) configure -minvalue $min
97    }
98
99    set max [string trim [$_owner xml get $path.max]]
100    if {$max ne ""} {
101        $itk_component(gauge) configure -maxvalue $max
102    }
103
104    if {$class == "Rappture::Gauge" && $min ne "" && $max ne ""} {
105        set color [string trim [$_owner xml get $path.about.color]]
106        if {$color == ""} {
107            # deprecated.  Color should be in "about"
108            set color [string trim [$_owner xml get $path.color]]
109        }
110        if {$color != ""}  {
111            if {$units != ""} {
112                set min [Rappture::Units::convert $min -to $units -units off]
113                set max [Rappture::Units::convert $max -to $units -units off]
114            }
115            # For compatibility. If only one color use white for min
116            if {[llength $color] == 1} {
117                set color [list $min white $max $color]
118            }
119            $itk_component(gauge) configure \
120                -spectrum [Rappture::Spectrum ::\#auto $color -units $units]
121        }
122    }
123
124    # if the control has an icon, plug it in
125    set str [string trim [$_owner xml get $path.about.icon]]
126    if {$str ne ""} {
127        $itk_component(gauge) configure -image [image create photo -data $str]
128    }
129    eval itk_initialize $args
130
131    #
132    # Assign the default value to this widget, if there is one.
133    #
134    set str [string trim [$_owner xml get $path.default]]
135    if {$str ne ""} {
136        $itk_component(gauge) value $str
137    }
138}
139
140# ----------------------------------------------------------------------
141# USAGE: value ?-check? ?<newval>?
142#
143# Clients use this to query/set the value for this widget.  With
144# no args, it returns the current value for the widget.  If the
145# <newval> is specified, it sets the value of the widget and
146# sends a <<Value>> event.  If the -check flag is included, the
147# new value is not actually applied, but just checked for correctness.
148# ----------------------------------------------------------------------
149itcl::body Rappture::NumberEntry::value {args} {
150    set onlycheck 0
151    set i [lsearch -exact $args -check]
152    if {$i >= 0} {
153        set onlycheck 1
154        set args [lreplace $args $i $i]
155    }
156
157    if {[llength $args] == 1} {
158        if {$onlycheck} {
159            # someday we may add validation...
160            return
161        }
162        set newval [string trim [lindex $args 0]]
163        $itk_component(gauge) value $newval
164        return $newval
165
166    } elseif {[llength $args] != 0} {
167        error "wrong # args: should be \"value ?-check? ?newval?\""
168    }
169
170    #
171    # Query the value and return.
172    #
173    return [$itk_component(gauge) value]
174}
175
176# ----------------------------------------------------------------------
177# USAGE: label
178#
179# Clients use this to query the label associated with this widget.
180# Reaches into the XML and pulls out the appropriate label string.
181# ----------------------------------------------------------------------
182itcl::body Rappture::NumberEntry::label {} {
183    set label [string trim [$_owner xml get $_path.about.label]]
184    if {"" == $label} {
185        set label "Number"
186    }
187    return $label
188}
189
190# ----------------------------------------------------------------------
191# USAGE: tooltip
192#
193# Clients use this to query the tooltip associated with this widget.
194# Reaches into the XML and pulls out the appropriate description
195# string.  Returns the string that should be used with the
196# Rappture::Tooltip facility.
197# ----------------------------------------------------------------------
198itcl::body Rappture::NumberEntry::tooltip {} {
199    set str   [string trim [$_owner xml get $_path.about.description]]
200
201    set units [string trim [$_owner xml get $_path.units]]
202    set min   [string trim [$_owner xml get $_path.min]]
203    set max   [string trim [$_owner xml get $_path.max]]
204
205    if {$units != "" || $min != "" || $max != ""} {
206        append str "\n\nEnter a number"
207
208        if {$min != "" && $max != ""} {
209            append str " between $min and $max"
210        } elseif {$min != ""} {
211            append str " greater than $min"
212        } elseif {$max != ""} {
213            append str " less than $max"
214        }
215
216        if {$units != ""} {
217            set desc [Rappture::Units::description $units]
218            append str " with units of $desc"
219        }
220    }
221    return [string trim $str]
222}
223
224# ----------------------------------------------------------------------
225# USAGE: _newValue
226#
227# Invoked automatically whenever the value in the gauge changes.
228# Sends a <<Value>> event to notify clients of the change.
229# ----------------------------------------------------------------------
230itcl::body Rappture::NumberEntry::_newValue {} {
231    event generate $itk_component(hull) <<Value>>
232}
233
234# ----------------------------------------------------------------------
235# CONFIGURATION OPTION: -state
236# ----------------------------------------------------------------------
237itcl::configbody Rappture::NumberEntry::state {
238    set valid {normal disabled}
239    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
240        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
241    }
242    $itk_component(gauge) configure -state $itk_option(-state)
243}
Note: See TracBrowser for help on using the repository browser.