1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: BooleanEntry - widget for entering boolean values |
---|
3 | # |
---|
4 | # This widget represents a <boolean> entry on a control panel. |
---|
5 | # It is used to enter yes/no or on/off values. |
---|
6 | # ====================================================================== |
---|
7 | # AUTHOR: Michael McLennan, Purdue University |
---|
8 | # Copyright (c) 2004-2005 |
---|
9 | # Purdue Research Foundation, West Lafayette, IN |
---|
10 | # ====================================================================== |
---|
11 | package require Itk |
---|
12 | |
---|
13 | itcl::class Rappture::BooleanEntry { |
---|
14 | inherit itk::Widget |
---|
15 | |
---|
16 | constructor {owner path args} { # defined below } |
---|
17 | |
---|
18 | public method value {args} |
---|
19 | |
---|
20 | public method label {} |
---|
21 | public method tooltip {} |
---|
22 | |
---|
23 | protected method _newValue {} |
---|
24 | |
---|
25 | private variable _owner "" ;# thing managing this control |
---|
26 | private variable _path "" ;# path in XML to this number |
---|
27 | } |
---|
28 | |
---|
29 | itk::usual BooleanEntry { |
---|
30 | keep -cursor -font |
---|
31 | keep -foreground -background |
---|
32 | keep -textbackground |
---|
33 | keep -selectbackground -selectforeground -selectborderwidth |
---|
34 | } |
---|
35 | |
---|
36 | # ---------------------------------------------------------------------- |
---|
37 | # CONSTRUCTOR |
---|
38 | # ---------------------------------------------------------------------- |
---|
39 | itcl::body Rappture::BooleanEntry::constructor {owner path args} { |
---|
40 | if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} { |
---|
41 | error "bad object \"$owner\": should be Rappture::ControlOwner" |
---|
42 | } |
---|
43 | set _owner $owner |
---|
44 | set _path $path |
---|
45 | |
---|
46 | # |
---|
47 | # Create the widget and configure it properly based on other |
---|
48 | # hints in the XML. |
---|
49 | # |
---|
50 | itk_component add switch { |
---|
51 | Rappture::Switch $itk_interior.switch |
---|
52 | } |
---|
53 | pack $itk_component(switch) -expand yes -fill both |
---|
54 | bind $itk_component(switch) <<Value>> [itcl::code $this _newValue] |
---|
55 | |
---|
56 | set color [$_owner xml get $path.about.color] |
---|
57 | if {$color != ""} { |
---|
58 | $itk_component(switch) configure -oncolor $color |
---|
59 | } |
---|
60 | |
---|
61 | # if the control has an icon, plug it in |
---|
62 | set str [$_owner xml get $path.about.icon] |
---|
63 | if {$str != ""} { |
---|
64 | $itk_component(switch) configure -onimage \ |
---|
65 | [image create photo -data $str] |
---|
66 | } |
---|
67 | |
---|
68 | eval itk_initialize $args |
---|
69 | |
---|
70 | # |
---|
71 | # Assign the default value to this widget, if there is one. |
---|
72 | # |
---|
73 | set str [$_owner xml get $path.default] |
---|
74 | if {"" != $str} { |
---|
75 | $itk_component(switch) value $str |
---|
76 | } else { |
---|
77 | $itk_component(switch) value off |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | # ---------------------------------------------------------------------- |
---|
82 | # USAGE: value ?-check? ?<newval>? |
---|
83 | # |
---|
84 | # Clients use this to query/set the value for this widget. With |
---|
85 | # no args, it returns the current value for the widget. If the |
---|
86 | # <newval> is specified, it sets the value of the widget and |
---|
87 | # sends a <<Value>> event. If the -check flag is included, the |
---|
88 | # new value is not actually applied, but just checked for correctness. |
---|
89 | # ---------------------------------------------------------------------- |
---|
90 | itcl::body Rappture::BooleanEntry::value {args} { |
---|
91 | set onlycheck 0 |
---|
92 | set i [lsearch -exact $args -check] |
---|
93 | if {$i >= 0} { |
---|
94 | set onlycheck 1 |
---|
95 | set args [lreplace $args $i $i] |
---|
96 | } |
---|
97 | |
---|
98 | if {[llength $args] == 1} { |
---|
99 | if {$onlycheck} { |
---|
100 | # someday we may add validation... |
---|
101 | return |
---|
102 | } |
---|
103 | set newval [lindex $args 0] |
---|
104 | $itk_component(switch) value $newval |
---|
105 | return $newval |
---|
106 | |
---|
107 | } elseif {[llength $args] != 0} { |
---|
108 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
109 | } |
---|
110 | |
---|
111 | # |
---|
112 | # Query the value and return. |
---|
113 | # |
---|
114 | return [$itk_component(switch) value] |
---|
115 | } |
---|
116 | |
---|
117 | # ---------------------------------------------------------------------- |
---|
118 | # USAGE: label |
---|
119 | # |
---|
120 | # Clients use this to query the label associated with this widget. |
---|
121 | # Reaches into the XML and pulls out the appropriate label string. |
---|
122 | # ---------------------------------------------------------------------- |
---|
123 | itcl::body Rappture::BooleanEntry::label {} { |
---|
124 | set label [$_owner xml get $_path.about.label] |
---|
125 | if {"" == $label} { |
---|
126 | set label "Boolean" |
---|
127 | } |
---|
128 | return $label |
---|
129 | } |
---|
130 | |
---|
131 | # ---------------------------------------------------------------------- |
---|
132 | # USAGE: tooltip |
---|
133 | # |
---|
134 | # Clients use this to query the tooltip associated with this widget. |
---|
135 | # Reaches into the XML and pulls out the appropriate description |
---|
136 | # string. Returns the string that should be used with the |
---|
137 | # Rappture::Tooltip facility. |
---|
138 | # ---------------------------------------------------------------------- |
---|
139 | itcl::body Rappture::BooleanEntry::tooltip {} { |
---|
140 | set str [$_owner xml get $_path.about.description] |
---|
141 | append str "\n\nEnter a boolean value (1/0, on/off, yes/no, true/false)" |
---|
142 | return [string trim $str] |
---|
143 | } |
---|
144 | |
---|
145 | # ---------------------------------------------------------------------- |
---|
146 | # USAGE: _newValue |
---|
147 | # |
---|
148 | # Invoked automatically whenever the value in the gauge changes. |
---|
149 | # Sends a <<Value>> event to notify clients of the change. |
---|
150 | # ---------------------------------------------------------------------- |
---|
151 | itcl::body Rappture::BooleanEntry::_newValue {} { |
---|
152 | event generate $itk_component(hull) <<Value>> |
---|
153 | } |
---|