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

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

snapshot of uq work

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