1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # COMPONENT: BooleanEntry - widget for entering boolean values |
---|
4 | # |
---|
5 | # This widget represents a <boolean> entry on a control panel. |
---|
6 | # It is used to enter yes/no or on/off 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 | # ====================================================================== |
---|
14 | package require Itk |
---|
15 | |
---|
16 | itcl::class Rappture::BooleanEntry { |
---|
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 | |
---|
28 | protected method _newValue {} |
---|
29 | protected method _log {} |
---|
30 | |
---|
31 | private variable _owner "" ;# thing managing this control |
---|
32 | private variable _path "" ;# path in XML to this number |
---|
33 | } |
---|
34 | |
---|
35 | itk::usual BooleanEntry { |
---|
36 | keep -cursor -font |
---|
37 | keep -foreground -background |
---|
38 | keep -textbackground |
---|
39 | keep -selectbackground -selectforeground -selectborderwidth |
---|
40 | } |
---|
41 | |
---|
42 | # ---------------------------------------------------------------------- |
---|
43 | # CONSTRUCTOR |
---|
44 | # ---------------------------------------------------------------------- |
---|
45 | itcl::body Rappture::BooleanEntry::constructor {owner path args} { |
---|
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 | itk_component add -protected vframe { |
---|
53 | frame $itk_interior.vframe |
---|
54 | } |
---|
55 | |
---|
56 | # if the control has an icon, plug it in |
---|
57 | set icon [string trim [$_owner xml get $path.about.icon]] |
---|
58 | if {$icon != ""} { |
---|
59 | itk_component add icon { |
---|
60 | set icon [image create photo -data $icon] |
---|
61 | set w [image width $icon] |
---|
62 | set h [image height $icon] |
---|
63 | canvas $itk_component(vframe).icon -height $h -width $w -borderwidth 0 -highlightthickness 0 |
---|
64 | } { |
---|
65 | usual |
---|
66 | ignore -highlightthickness -highlightbackground -highlightcolor |
---|
67 | } |
---|
68 | set c $itk_component(icon) |
---|
69 | $c create image [expr {0.5*$w}] [expr {0.5*$h}] \ |
---|
70 | -anchor center -image $icon |
---|
71 | pack $itk_component(icon) -fill x -side left |
---|
72 | } |
---|
73 | |
---|
74 | # |
---|
75 | # Create the widget and configure it properly based on other |
---|
76 | # hints in the XML. |
---|
77 | # |
---|
78 | itk_component add switch { |
---|
79 | Rappture::Switch $itk_component(vframe).switch \ |
---|
80 | -interactcommand [itcl::code $this _log] |
---|
81 | } |
---|
82 | set color [string trim [$_owner xml get $path.about.color]] |
---|
83 | if {$color != ""} { |
---|
84 | $itk_component(switch) configure -oncolor $color |
---|
85 | } |
---|
86 | |
---|
87 | bind $itk_component(switch) <<Value>> [itcl::code $this _newValue] |
---|
88 | pack $itk_component(switch) -fill x -side left |
---|
89 | pack $itk_component(vframe) -side left -expand yes -fill both |
---|
90 | eval itk_initialize $args |
---|
91 | |
---|
92 | # |
---|
93 | # Assign the default value to this widget, if there is one. |
---|
94 | # |
---|
95 | set str [string trim [$_owner xml get $path.default]] |
---|
96 | if {"" != $str} { |
---|
97 | $itk_component(switch) value $str |
---|
98 | } else { |
---|
99 | $itk_component(switch) value off |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | # ---------------------------------------------------------------------- |
---|
104 | # USAGE: value ?-check? ?<newval>? |
---|
105 | # |
---|
106 | # Clients use this to query/set the value for this widget. With |
---|
107 | # no args, it returns the current value for the widget. If the |
---|
108 | # <newval> is specified, it sets the value of the widget and |
---|
109 | # sends a <<Value>> event. If the -check flag is included, the |
---|
110 | # new value is not actually applied, but just checked for correctness. |
---|
111 | # ---------------------------------------------------------------------- |
---|
112 | itcl::body Rappture::BooleanEntry::value {args} { |
---|
113 | set onlycheck 0 |
---|
114 | set i [lsearch -exact $args -check] |
---|
115 | if {$i >= 0} { |
---|
116 | set onlycheck 1 |
---|
117 | set args [lreplace $args $i $i] |
---|
118 | } |
---|
119 | |
---|
120 | if {[llength $args] == 1} { |
---|
121 | if {$onlycheck} { |
---|
122 | # someday we may add validation... |
---|
123 | return |
---|
124 | } |
---|
125 | set newval [string trim [lindex $args 0]] |
---|
126 | $itk_component(switch) value $newval |
---|
127 | event generate $itk_component(hull) <<Value>> |
---|
128 | return $newval |
---|
129 | |
---|
130 | } elseif {[llength $args] != 0} { |
---|
131 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
132 | } |
---|
133 | |
---|
134 | # |
---|
135 | # Query the value and return. |
---|
136 | # |
---|
137 | return [$itk_component(switch) value] |
---|
138 | } |
---|
139 | |
---|
140 | # ---------------------------------------------------------------------- |
---|
141 | # USAGE: label |
---|
142 | # |
---|
143 | # Clients use this to query the label associated with this widget. |
---|
144 | # Reaches into the XML and pulls out the appropriate label string. |
---|
145 | # ---------------------------------------------------------------------- |
---|
146 | itcl::body Rappture::BooleanEntry::label {} { |
---|
147 | set label [string trim [$_owner xml get $_path.about.label]] |
---|
148 | if {"" == $label} { |
---|
149 | set label "Boolean" |
---|
150 | } |
---|
151 | return $label |
---|
152 | } |
---|
153 | |
---|
154 | # ---------------------------------------------------------------------- |
---|
155 | # USAGE: tooltip |
---|
156 | # |
---|
157 | # Clients use this to query the tooltip associated with this widget. |
---|
158 | # Reaches into the XML and pulls out the appropriate description |
---|
159 | # string. Returns the string that should be used with the |
---|
160 | # Rappture::Tooltip facility. |
---|
161 | # ---------------------------------------------------------------------- |
---|
162 | itcl::body Rappture::BooleanEntry::tooltip {} { |
---|
163 | set str [string trim [$_owner xml get $_path.about.description]] |
---|
164 | append str "\n\nClick to turn on/off" |
---|
165 | return $str |
---|
166 | } |
---|
167 | |
---|
168 | # ---------------------------------------------------------------------- |
---|
169 | # USAGE: _newValue |
---|
170 | # |
---|
171 | # Invoked automatically whenever the value in the gauge changes. |
---|
172 | # Sends a <<Value>> event to notify clients of the change. |
---|
173 | # ---------------------------------------------------------------------- |
---|
174 | itcl::body Rappture::BooleanEntry::_newValue {} { |
---|
175 | event generate $itk_component(hull) <<Value>> |
---|
176 | } |
---|
177 | |
---|
178 | # ---------------------------------------------------------------------- |
---|
179 | # USAGE: _log |
---|
180 | # |
---|
181 | # Used internally to send info to the logging mechanism. Calls the |
---|
182 | # Rappture::Logger mechanism to log the change to this input. |
---|
183 | # ---------------------------------------------------------------------- |
---|
184 | itcl::body Rappture::BooleanEntry::_log {} { |
---|
185 | Rappture::Logger::log input $_path [$itk_component(switch) value] |
---|
186 | } |
---|
187 | |
---|
188 | # ---------------------------------------------------------------------- |
---|
189 | # CONFIGURATION OPTION: -state |
---|
190 | # ---------------------------------------------------------------------- |
---|
191 | itcl::configbody Rappture::BooleanEntry::state { |
---|
192 | set valid {normal disabled} |
---|
193 | if {[lsearch -exact $valid $itk_option(-state)] < 0} { |
---|
194 | error "bad value \"$itk_option(-state)\": should be [join $valid {, }]" |
---|
195 | } |
---|
196 | $itk_component(switch) configure -state $itk_option(-state) |
---|
197 | } |
---|