[22] | 1 | # ---------------------------------------------------------------------- |
---|
| 2 | # COMPONENT: IntegerEntry - widget for entering integer values |
---|
| 3 | # |
---|
| 4 | # This widget represents an <integer> entry on a control panel. |
---|
| 5 | # It is used to enter integral values with no units. |
---|
| 6 | # ====================================================================== |
---|
| 7 | # AUTHOR: Michael McLennan, Purdue University |
---|
[115] | 8 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
| 9 | # |
---|
| 10 | # See the file "license.terms" for information on usage and |
---|
| 11 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
[22] | 12 | # ====================================================================== |
---|
| 13 | package require Itk |
---|
| 14 | |
---|
| 15 | itcl::class Rappture::IntegerEntry { |
---|
| 16 | inherit itk::Widget |
---|
| 17 | |
---|
[437] | 18 | itk_option define -state state State "normal" |
---|
| 19 | |
---|
[22] | 20 | constructor {owner path args} { # defined below } |
---|
| 21 | |
---|
| 22 | public method value {args} |
---|
| 23 | |
---|
| 24 | public method label {} |
---|
| 25 | public method tooltip {} |
---|
| 26 | |
---|
| 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 | |
---|
| 33 | itk::usual IntegerEntry { |
---|
| 34 | keep -cursor -font |
---|
| 35 | keep -foreground -background |
---|
| 36 | keep -textbackground |
---|
| 37 | keep -selectbackground -selectforeground -selectborderwidth |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | # ---------------------------------------------------------------------- |
---|
| 41 | # CONSTRUCTOR |
---|
| 42 | # ---------------------------------------------------------------------- |
---|
| 43 | itcl::body Rappture::IntegerEntry::constructor {owner path args} { |
---|
| 44 | if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} { |
---|
[1342] | 45 | error "bad object \"$owner\": should be Rappture::ControlOwner" |
---|
[22] | 46 | } |
---|
| 47 | set _owner $owner |
---|
| 48 | set _path $path |
---|
| 49 | |
---|
| 50 | # |
---|
| 51 | # Create the widget and configure it properly based on other |
---|
| 52 | # hints in the XML. |
---|
| 53 | # |
---|
| 54 | itk_component add spinner { |
---|
[1342] | 55 | Rappture::Gauge $itk_interior.spinner -type integer |
---|
[22] | 56 | } |
---|
| 57 | pack $itk_component(spinner) -expand yes -fill both |
---|
| 58 | bind $itk_component(spinner) <<Value>> [itcl::code $this _newValue] |
---|
| 59 | |
---|
| 60 | # if there are min/max values, plug them in. |
---|
| 61 | set min [$_owner xml get $path.min] |
---|
| 62 | if {"" != $min} { |
---|
[1342] | 63 | $itk_component(spinner) configure -minvalue $min |
---|
[22] | 64 | } |
---|
| 65 | |
---|
| 66 | set max [$_owner xml get $path.max] |
---|
| 67 | if {"" != $max} { |
---|
[1342] | 68 | $itk_component(spinner) configure -maxvalue $max |
---|
[22] | 69 | } |
---|
| 70 | |
---|
| 71 | # if there is a color, use it for the min/max spectrum |
---|
| 72 | set color [$_owner xml get $path.about.color] |
---|
| 73 | if {$color != "" && $min != "" && $max != ""} { |
---|
[1342] | 74 | # For compatibility. If only one color use white for min |
---|
| 75 | if {[llength $color] == 1} { |
---|
| 76 | set color [list $min white $max $color] |
---|
| 77 | } |
---|
| 78 | $itk_component(spinner) configure \ |
---|
| 79 | -spectrum [Rappture::Spectrum ::#auto $color] |
---|
[22] | 80 | } |
---|
| 81 | |
---|
| 82 | # if the control has an icon, plug it in |
---|
| 83 | set str [$_owner xml get $path.about.icon] |
---|
| 84 | if {$str != ""} { |
---|
[1342] | 85 | $itk_component(spinner) configure -image \ |
---|
[1646] | 86 | [image create picture -data $str] |
---|
[22] | 87 | } |
---|
| 88 | |
---|
| 89 | eval itk_initialize $args |
---|
| 90 | |
---|
| 91 | # |
---|
| 92 | # Assign the default value to this widget, if there is one. |
---|
| 93 | # |
---|
| 94 | set str [$_owner xml get $path.default] |
---|
| 95 | if {"" != $str != ""} { $itk_component(spinner) value $str } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | # ---------------------------------------------------------------------- |
---|
| 99 | # USAGE: value ?-check? ?<newval>? |
---|
| 100 | # |
---|
| 101 | # Clients use this to query/set the value for this widget. With |
---|
| 102 | # no args, it returns the current value for the widget. If the |
---|
| 103 | # <newval> is specified, it sets the value of the widget and |
---|
| 104 | # sends a <<Value>> event. If the -check flag is included, the |
---|
| 105 | # new value is not actually applied, but just checked for correctness. |
---|
| 106 | # ---------------------------------------------------------------------- |
---|
| 107 | itcl::body Rappture::IntegerEntry::value {args} { |
---|
| 108 | set onlycheck 0 |
---|
| 109 | set i [lsearch -exact $args -check] |
---|
| 110 | if {$i >= 0} { |
---|
[1342] | 111 | set onlycheck 1 |
---|
| 112 | set args [lreplace $args $i $i] |
---|
[22] | 113 | } |
---|
| 114 | |
---|
| 115 | if {[llength $args] == 1} { |
---|
[1342] | 116 | if {$onlycheck} { |
---|
| 117 | # someday we may add validation... |
---|
| 118 | return |
---|
| 119 | } |
---|
| 120 | set newval [lindex $args 0] |
---|
| 121 | $itk_component(spinner) value $newval |
---|
| 122 | return $newval |
---|
[22] | 123 | |
---|
| 124 | } elseif {[llength $args] != 0} { |
---|
[1342] | 125 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
[22] | 126 | } |
---|
| 127 | |
---|
| 128 | # |
---|
| 129 | # Query the value and return. |
---|
| 130 | # |
---|
| 131 | return [$itk_component(spinner) value] |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | # ---------------------------------------------------------------------- |
---|
| 135 | # USAGE: label |
---|
| 136 | # |
---|
| 137 | # Clients use this to query the label associated with this widget. |
---|
| 138 | # Reaches into the XML and pulls out the appropriate label string. |
---|
| 139 | # ---------------------------------------------------------------------- |
---|
| 140 | itcl::body Rappture::IntegerEntry::label {} { |
---|
| 141 | set label [$_owner xml get $_path.about.label] |
---|
| 142 | if {"" == $label} { |
---|
[1342] | 143 | set label "Integer" |
---|
[22] | 144 | } |
---|
| 145 | return $label |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | # ---------------------------------------------------------------------- |
---|
| 149 | # USAGE: tooltip |
---|
| 150 | # |
---|
| 151 | # Clients use this to query the tooltip associated with this widget. |
---|
| 152 | # Reaches into the XML and pulls out the appropriate description |
---|
| 153 | # string. Returns the string that should be used with the |
---|
| 154 | # Rappture::Tooltip facility. |
---|
| 155 | # ---------------------------------------------------------------------- |
---|
| 156 | itcl::body Rappture::IntegerEntry::tooltip {} { |
---|
| 157 | set str [$_owner xml get $_path.about.description] |
---|
| 158 | |
---|
| 159 | set min [$_owner xml get $_path.min] |
---|
| 160 | set max [$_owner xml get $_path.max] |
---|
| 161 | |
---|
| 162 | if {$min != "" || $max != ""} { |
---|
[1342] | 163 | append str "\n\nEnter an integer" |
---|
[22] | 164 | |
---|
[1342] | 165 | if {$min != "" && $max != ""} { |
---|
| 166 | append str " between $min and $max" |
---|
| 167 | } elseif {$min != ""} { |
---|
| 168 | append str " greater than $min" |
---|
| 169 | } elseif {$max != ""} { |
---|
| 170 | append str " less than $max" |
---|
| 171 | } |
---|
[22] | 172 | } |
---|
| 173 | return [string trim $str] |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | # ---------------------------------------------------------------------- |
---|
| 177 | # USAGE: _newValue |
---|
| 178 | # |
---|
| 179 | # Invoked automatically whenever the value in the gauge changes. |
---|
| 180 | # Sends a <<Value>> event to notify clients of the change. |
---|
| 181 | # ---------------------------------------------------------------------- |
---|
| 182 | itcl::body Rappture::IntegerEntry::_newValue {} { |
---|
| 183 | event generate $itk_component(hull) <<Value>> |
---|
| 184 | } |
---|
[437] | 185 | |
---|
| 186 | # ---------------------------------------------------------------------- |
---|
| 187 | # CONFIGURATION OPTION: -state |
---|
| 188 | # ---------------------------------------------------------------------- |
---|
| 189 | itcl::configbody Rappture::IntegerEntry::state { |
---|
| 190 | set valid {normal disabled} |
---|
| 191 | if {[lsearch -exact $valid $itk_option(-state)] < 0} { |
---|
[1342] | 192 | error "bad value \"$itk_option(-state)\": should be [join $valid {, }]" |
---|
[437] | 193 | } |
---|
| 194 | $itk_component(spinner) configure -state $itk_option(-state) |
---|
| 195 | } |
---|