source: branches/uq/gui/scripts/numberentry.tcl @ 5029

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

puq integration snap

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