[3330] | 1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
[11] | 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 |
---|
[3177] | 9 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[115] | 10 | # |
---|
| 11 | # See the file "license.terms" for information on usage and |
---|
| 12 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
[11] | 13 | # ====================================================================== |
---|
| 14 | package require Itk |
---|
| 15 | |
---|
| 16 | itcl::class Rappture::NumberEntry { |
---|
| 17 | inherit itk::Widget |
---|
| 18 | |
---|
[437] | 19 | itk_option define -state state State "normal" |
---|
| 20 | |
---|
[22] | 21 | constructor {owner path args} { # defined below } |
---|
[11] | 22 | |
---|
| 23 | public method value {args} |
---|
| 24 | |
---|
| 25 | public method label {} |
---|
| 26 | public method tooltip {} |
---|
| 27 | |
---|
| 28 | protected method _newValue {} |
---|
| 29 | |
---|
[22] | 30 | private variable _owner "" ;# thing managing this control |
---|
[11] | 31 | private variable _path "" ;# path in XML to this number |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | itk::usual NumberEntry { |
---|
| 35 | keep -cursor -font |
---|
| 36 | keep -foreground -background |
---|
| 37 | keep -textbackground |
---|
| 38 | keep -selectbackground -selectforeground -selectborderwidth |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | # ---------------------------------------------------------------------- |
---|
| 42 | # CONSTRUCTOR |
---|
| 43 | # ---------------------------------------------------------------------- |
---|
[22] | 44 | itcl::body Rappture::NumberEntry::constructor {owner path args} { |
---|
| 45 | if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} { |
---|
[1527] | 46 | error "bad object \"$owner\": should be Rappture::ControlOwner" |
---|
[11] | 47 | } |
---|
[22] | 48 | set _owner $owner |
---|
[11] | 49 | set _path $path |
---|
| 50 | |
---|
| 51 | # |
---|
| 52 | # Figure out what sort of control to create |
---|
| 53 | # |
---|
| 54 | set presets "" |
---|
[22] | 55 | foreach pre [$_owner xml children -type preset $path] { |
---|
[1527] | 56 | lappend presets \ |
---|
| 57 | [$_owner xml get $path.$pre.value] \ |
---|
| 58 | [$_owner xml get $path.$pre.label] |
---|
[11] | 59 | } |
---|
| 60 | |
---|
| 61 | set class Rappture::Gauge |
---|
[22] | 62 | set units [$_owner xml get $path.units] |
---|
[11] | 63 | if {$units != ""} { |
---|
[1527] | 64 | set desc [Rappture::Units::description $units] |
---|
| 65 | if {[string match temperature* $desc]} { |
---|
| 66 | set class Rappture::TemperatureGauge |
---|
| 67 | } |
---|
[11] | 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 { |
---|
[3186] | 75 | $class $itk_interior.gauge -units $units -presets $presets -log $path |
---|
[11] | 76 | } |
---|
| 77 | pack $itk_component(gauge) -expand yes -fill both |
---|
| 78 | bind $itk_component(gauge) <<Value>> [itcl::code $this _newValue] |
---|
| 79 | |
---|
[22] | 80 | set min [$_owner xml get $path.min] |
---|
[3186] | 81 | if {$min ne ""} { $itk_component(gauge) configure -minvalue $min } |
---|
[11] | 82 | |
---|
[22] | 83 | set max [$_owner xml get $path.max] |
---|
[3186] | 84 | if {$max ne ""} { $itk_component(gauge) configure -maxvalue $max } |
---|
[1287] | 85 | |
---|
[3186] | 86 | if {$class == "Rappture::Gauge" && $min ne "" && $max ne ""} { |
---|
[1527] | 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 | } |
---|
[11] | 104 | } |
---|
| 105 | |
---|
| 106 | # if the control has an icon, plug it in |
---|
[22] | 107 | set str [$_owner xml get $path.about.icon] |
---|
[3186] | 108 | if {$str ne ""} { |
---|
[1527] | 109 | $itk_component(gauge) configure -image [image create photo -data $str] |
---|
[11] | 110 | } |
---|
| 111 | |
---|
| 112 | eval itk_initialize $args |
---|
| 113 | |
---|
| 114 | # |
---|
| 115 | # Assign the default value to this widget, if there is one. |
---|
| 116 | # |
---|
[22] | 117 | set str [$_owner xml get $path.default] |
---|
[3186] | 118 | if {$str ne ""} { $itk_component(gauge) value $str } |
---|
[11] | 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 | # ---------------------------------------------------------------------- |
---|
| 130 | itcl::body Rappture::NumberEntry::value {args} { |
---|
| 131 | set onlycheck 0 |
---|
| 132 | set i [lsearch -exact $args -check] |
---|
| 133 | if {$i >= 0} { |
---|
[1527] | 134 | set onlycheck 1 |
---|
| 135 | set args [lreplace $args $i $i] |
---|
[11] | 136 | } |
---|
| 137 | |
---|
| 138 | if {[llength $args] == 1} { |
---|
[1527] | 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 |
---|
[11] | 146 | |
---|
| 147 | } elseif {[llength $args] != 0} { |
---|
[1527] | 148 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
[11] | 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 | # ---------------------------------------------------------------------- |
---|
| 163 | itcl::body Rappture::NumberEntry::label {} { |
---|
[22] | 164 | set label [$_owner xml get $_path.about.label] |
---|
[11] | 165 | if {"" == $label} { |
---|
[1527] | 166 | set label "Number" |
---|
[11] | 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 | # ---------------------------------------------------------------------- |
---|
| 179 | itcl::body Rappture::NumberEntry::tooltip {} { |
---|
[22] | 180 | set str [$_owner xml get $_path.about.description] |
---|
[11] | 181 | |
---|
[22] | 182 | set units [$_owner xml get $_path.units] |
---|
| 183 | set min [$_owner xml get $_path.min] |
---|
| 184 | set max [$_owner xml get $_path.max] |
---|
[11] | 185 | |
---|
| 186 | if {$units != "" || $min != "" || $max != ""} { |
---|
[1527] | 187 | append str "\n\nEnter a number" |
---|
[11] | 188 | |
---|
[1527] | 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 | } |
---|
[11] | 196 | |
---|
[1527] | 197 | if {$units != ""} { |
---|
| 198 | set desc [Rappture::Units::description $units] |
---|
| 199 | append str " with units of $desc" |
---|
| 200 | } |
---|
[11] | 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 | # ---------------------------------------------------------------------- |
---|
| 211 | itcl::body Rappture::NumberEntry::_newValue {} { |
---|
| 212 | event generate $itk_component(hull) <<Value>> |
---|
| 213 | } |
---|
[437] | 214 | |
---|
| 215 | # ---------------------------------------------------------------------- |
---|
| 216 | # CONFIGURATION OPTION: -state |
---|
| 217 | # ---------------------------------------------------------------------- |
---|
| 218 | itcl::configbody Rappture::NumberEntry::state { |
---|
| 219 | set valid {normal disabled} |
---|
| 220 | if {[lsearch -exact $valid $itk_option(-state)] < 0} { |
---|
[1527] | 221 | error "bad value \"$itk_option(-state)\": should be [join $valid {, }]" |
---|
[437] | 222 | } |
---|
| 223 | $itk_component(gauge) configure -state $itk_option(-state) |
---|
| 224 | } |
---|