1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # COMPONENT: switch - on/off switch |
---|
4 | # |
---|
5 | # This widget is used to control a (boolean) on/off value. |
---|
6 | # It is just a wrapper around a button. |
---|
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::Switch { |
---|
17 | inherit itk::Widget |
---|
18 | |
---|
19 | itk_option define -oncolor onColor Color "#00cc00" |
---|
20 | itk_option define -state state State "normal" |
---|
21 | itk_option define -showimage showimage Showimage "true" |
---|
22 | itk_option define -showtext showtext Showtext "true" |
---|
23 | itk_option define -interactcommand interactCommand InteractCommand "" |
---|
24 | |
---|
25 | constructor {args} { # defined below } |
---|
26 | public method value {args} |
---|
27 | |
---|
28 | private method _updateText {} |
---|
29 | private method _toggle {args} |
---|
30 | private variable _value 0 ;# value for this widget |
---|
31 | } |
---|
32 | |
---|
33 | itk::usual Switch { |
---|
34 | keep -cursor -background |
---|
35 | } |
---|
36 | |
---|
37 | # ---------------------------------------------------------------------- |
---|
38 | # CONSTRUCTOR |
---|
39 | # ---------------------------------------------------------------------- |
---|
40 | itcl::body Rappture::Switch::constructor {args} { |
---|
41 | |
---|
42 | itk_component add button { |
---|
43 | button $itk_interior.value -compound left \ |
---|
44 | -overrelief flat -relief flat -padx 3 -pady 0 -bd 0 \ |
---|
45 | -command [itcl::code $this _toggle] |
---|
46 | } { |
---|
47 | #rename -background -textbackground textBackground Background |
---|
48 | } |
---|
49 | pack $itk_component(button) -side left -expand yes -fill both |
---|
50 | eval itk_initialize $args |
---|
51 | } |
---|
52 | |
---|
53 | # ---------------------------------------------------------------------- |
---|
54 | # USAGE: value ?-check? ?<newval>? |
---|
55 | # |
---|
56 | # Clients use this to query/set the value for this widget. With |
---|
57 | # no args, it returns the current value for the widget. If the |
---|
58 | # <newval> is specified, it sets the value of the widget and |
---|
59 | # sends a <<Value>> event. If the -check flag is included, the |
---|
60 | # new value is not actually applied, but just checked for correctness. |
---|
61 | # ---------------------------------------------------------------------- |
---|
62 | itcl::body Rappture::Switch::value {args} { |
---|
63 | set onlycheck 0 |
---|
64 | set i [lsearch -exact $args -check] |
---|
65 | if {$i >= 0} { |
---|
66 | set onlycheck 1 |
---|
67 | set args [lreplace $args $i $i] |
---|
68 | } |
---|
69 | if {[llength $args] == 1} { |
---|
70 | set newval [lindex $args 0] |
---|
71 | # I don't like the "string is" commands. They have too many quirks. |
---|
72 | # If we're calling "expr" anyways, just put a catch on it. |
---|
73 | if { [catch {expr {($newval) ? 1 : 0}} newval] != 0 } { |
---|
74 | error "$newval: should be a yes, no, on, off, true, false, 0, or 1." |
---|
75 | } |
---|
76 | if {$onlycheck} { |
---|
77 | return |
---|
78 | } |
---|
79 | set _value $newval |
---|
80 | event generate $itk_component(hull) <<Value>> |
---|
81 | _updateText |
---|
82 | } elseif {[llength $args] != 0} { |
---|
83 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
84 | } |
---|
85 | return [expr {($_value) ? "yes" : "no"}] |
---|
86 | } |
---|
87 | |
---|
88 | # ---------------------------------------------------------------------- |
---|
89 | # _toggle |
---|
90 | # |
---|
91 | # Use internally to convert the toggled button into the |
---|
92 | # proper boolean format. Yes, right now it's hardcoded to |
---|
93 | # yes/no. But in the future it could be some other text. |
---|
94 | # |
---|
95 | # Can't use old "value" method because _value is already set |
---|
96 | # be the widget and doesn't pass the value on the command line. |
---|
97 | # |
---|
98 | # ---------------------------------------------------------------------- |
---|
99 | itcl::body Rappture::Switch::_toggle {} { |
---|
100 | if {$_value} { |
---|
101 | value off |
---|
102 | } else { |
---|
103 | value on |
---|
104 | } |
---|
105 | if {[string length $itk_option(-interactcommand)] > 0} { |
---|
106 | uplevel #0 $itk_option(-interactcommand) |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | itcl::body Rappture::Switch::_updateText {} { |
---|
111 | set image "" |
---|
112 | set text "" |
---|
113 | if { $_value } { |
---|
114 | if {$itk_option(-showimage)} { |
---|
115 | set image [Rappture::icon switch1] |
---|
116 | } |
---|
117 | if {$itk_option(-showtext)} { |
---|
118 | set text "yes" |
---|
119 | } |
---|
120 | } else { |
---|
121 | if {$itk_option(-showimage)} { |
---|
122 | set image [Rappture::icon switch0] |
---|
123 | } |
---|
124 | if {$itk_option(-showtext)} { |
---|
125 | set text "no" |
---|
126 | } |
---|
127 | } |
---|
128 | $itk_component(button) configure -text $text -image $image |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | # ---------------------------------------------------------------------- |
---|
133 | # CONFIGURATION OPTION: -oncolor |
---|
134 | # ---------------------------------------------------------------------- |
---|
135 | itcl::configbody Rappture::Switch::oncolor { |
---|
136 | # does nothing now, but may come back soon |
---|
137 | } |
---|
138 | |
---|
139 | # ---------------------------------------------------------------------- |
---|
140 | # CONFIGURATION OPTION: -state |
---|
141 | # ---------------------------------------------------------------------- |
---|
142 | itcl::configbody Rappture::Switch::state { |
---|
143 | set valid {normal disabled} |
---|
144 | if {[lsearch -exact $valid $itk_option(-state)] < 0} { |
---|
145 | error "bad value \"$itk_option(-state)\": should be [join $valid {, }]" |
---|
146 | } |
---|
147 | $itk_component(button) configure -state $itk_option(-state) |
---|
148 | } |
---|
149 | |
---|
150 | # ---------------------------------------------------------------------- |
---|
151 | # CONFIGURATION OPTION: -showimage |
---|
152 | # ---------------------------------------------------------------------- |
---|
153 | itcl::configbody Rappture::Switch::showimage { |
---|
154 | if {[string is boolean $itk_option(-showimage)] != 1} { |
---|
155 | error "bad value \"$itk_option(-showimage)\": should be a boolean" |
---|
156 | } |
---|
157 | _updateText |
---|
158 | } |
---|
159 | |
---|
160 | # ---------------------------------------------------------------------- |
---|
161 | # CONFIGURATION OPTION: -showtext |
---|
162 | # ---------------------------------------------------------------------- |
---|
163 | itcl::configbody Rappture::Switch::showtext { |
---|
164 | if {[string is boolean $itk_option(-showtext)] != 1} { |
---|
165 | error "bad value \"$itk_option(-showtext)\": should be a boolean" |
---|
166 | } |
---|
167 | _updateText |
---|
168 | } |
---|