author | Claus Gittinger <cg@exept.de> |
Fri, 09 Feb 1996 18:56:54 +0100 | |
changeset 340 | f820bcd29a51 |
parent 198 | c0f39f9440a7 |
child 586 | 032b3245e53a |
permissions | -rw-r--r-- |
0 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1991 by Claus Gittinger |
59 | 3 |
All Rights Reserved |
0 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
7 | 13 |
OrderedCollection subclass:#RadioButtonGroup |
155 | 14 |
instanceVariableNames:'valueChannel groupID' |
59 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
17 |
category:'Views-Support' |
|
0 | 18 |
! |
19 |
||
38 | 20 |
!RadioButtonGroup class methodsFor:'documentation '! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
59 | 25 |
All Rights Reserved |
0 | 26 |
|
38 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
RadioButtonGroups control the interaction between RadioButtons |
|
39 |
turning off other button(s) when one of the group is pressed. |
|
63 | 40 |
To group some buttons (and have one-on behavior) use: |
41 |
||
42 |
|g| |
|
43 |
||
44 |
g := RadioButtonGroup new. |
|
45 |
... |
|
46 |
b1 := RadioButton label:.... |
|
47 |
g add:b1 |
|
48 |
... |
|
49 |
b2 := RadioButton label:.... |
|
50 |
g add:b2 |
|
51 |
... |
|
116 | 52 |
|
131 | 53 |
A radioButtonGroup installs itself as the model of its toggles or |
54 |
radioButtons. Therefore, you can no longer operate the individual |
|
55 |
elements on models. |
|
56 |
Instead, the group now provides a valueHolder which takes as value |
|
57 |
the index of the selected toggle. You can set/get this valueHolder |
|
58 |
via the valueChannel:/valueChannel messages. |
|
59 |
See the last example for how this is done. |
|
155 | 60 |
|
61 |
The groupID instance variable is not required for normal operation; |
|
62 |
the interface builder uses it to give a button-group some descriptive |
|
63 |
(unique) name. |
|
131 | 64 |
" |
65 |
! |
|
66 |
||
67 |
examples |
|
68 |
" |
|
116 | 69 |
example (using Toggles for 'at most one-on behavior'): |
70 |
||
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
71 |
|top panel toggle b group| |
116 | 72 |
|
73 |
top := StandardSystemView new. |
|
74 |
panel := HorizontalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top. |
|
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
75 |
group := RadioButtonGroup new. |
116 | 76 |
b := Toggle label:'one' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
77 |
group add:b. |
116 | 78 |
b := Toggle label:'two' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
79 |
group add:b. |
116 | 80 |
b := Toggle label:'three' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
81 |
group add:b. |
130 | 82 |
top extent:(panel preferredExtent). |
116 | 83 |
top open. |
84 |
||
177 | 85 |
|
116 | 86 |
example (using RadioButtons for 'one-on behavior'): |
87 |
||
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
88 |
|top panel toggle b group| |
116 | 89 |
|
90 |
top := StandardSystemView new. |
|
91 |
panel := HorizontalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top. |
|
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
92 |
group := RadioButtonGroup new. |
116 | 93 |
b := RadioButton label:'one' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
94 |
group add:b. |
116 | 95 |
b := RadioButton label:'two' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
96 |
group add:b. |
116 | 97 |
b := RadioButton label:'three' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
98 |
group add:b. |
130 | 99 |
top extent:(panel preferredExtent). |
116 | 100 |
top open. |
101 |
||
177 | 102 |
|
103 |
example (using Buttons for 'none-on behavior'; but the groups value remembers the last button): |
|
104 |
||
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
105 |
|top panel toggle b group| |
177 | 106 |
|
107 |
top := StandardSystemView new. |
|
108 |
panel := HorizontalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top. |
|
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
109 |
group := RadioButtonGroup new. |
177 | 110 |
b := Button label:'one' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
111 |
group add:b. |
177 | 112 |
b := Button label:'two' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
113 |
group add:b. |
177 | 114 |
b := Button label:'three' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
115 |
group add:b. |
177 | 116 |
top extent:(panel preferredExtent). |
117 |
top open. |
|
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
118 |
group valueChannel inspect |
177 | 119 |
|
120 |
||
116 | 121 |
example (same, with button 'two' initially on): |
122 |
||
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
123 |
|top panel toggle b group| |
116 | 124 |
|
125 |
top := StandardSystemView new. |
|
126 |
panel := HorizontalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top. |
|
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
127 |
group := RadioButtonGroup new. |
116 | 128 |
b := RadioButton label:'one' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
129 |
group add:b. |
116 | 130 |
b := RadioButton label:'two' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
131 |
group add:b. |
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
132 |
group value:2. |
116 | 133 |
b := RadioButton label:'three' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
134 |
group add:b. |
130 | 135 |
top extent:(panel preferredExtent). |
116 | 136 |
top open. |
131 | 137 |
|
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
138 |
|
163 | 139 |
using checkBoxes (these have a label included): |
140 |
||
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
141 |
|top panel toggle b group| |
163 | 142 |
|
143 |
top := StandardSystemView new. |
|
144 |
panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top. |
|
177 | 145 |
panel horizontalLayout:#left. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
146 |
group := RadioButtonGroup new. |
163 | 147 |
b := CheckBox label:'one' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
148 |
group add:b. |
163 | 149 |
b := CheckBox label:'two' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
150 |
group add:b. |
163 | 151 |
b := CheckBox label:'three' in:panel. |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
152 |
group add:b. |
163 | 153 |
top extent:(panel preferredExtent). |
154 |
top open. |
|
155 |
||
156 |
||
131 | 157 |
example (operating on a model) |
158 |
||
159 |
|selected dialog group| |
|
160 |
||
161 |
selected := 2 asValue. |
|
162 |
dialog := Dialog new. |
|
163 |
group := RadioButtonGroup new. |
|
164 |
group add:(dialog addCheckBox:'one' on:nil) toggleView. |
|
165 |
group add:(dialog addCheckBox:'two' on:nil) toggleView. |
|
166 |
group add:(dialog addCheckBox:'three' on:nil) toggleView. |
|
167 |
group valueChannel:selected. |
|
168 |
dialog addAbortButton; addOkButton. |
|
169 |
dialog open. |
|
170 |
dialog accepted ifTrue:[ |
|
171 |
Transcript showCr:'you selected: ' , selected value printString |
|
163 | 172 |
] ifFalse:[ |
173 |
Transcript showCr:'aborted' |
|
131 | 174 |
] |
38 | 175 |
" |
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
176 |
|
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
177 |
"Created: 15.11.1995 / 17:14:53 / cg" |
198 | 178 |
! |
179 |
||
180 |
version |
|
181 |
^ '$Header: /cvs/stx/stx/libwidg/RadioButtonGroup.st,v 1.19 1995-11-23 02:28:44 cg Exp $' |
|
38 | 182 |
! ! |
0 | 183 |
|
155 | 184 |
!RadioButtonGroup methodsFor:'accessing'! |
185 |
||
198 | 186 |
groupID |
187 |
^ groupID |
|
188 |
||
189 |
"Modified: 5.9.1995 / 16:04:33 / claus" |
|
190 |
! |
|
191 |
||
155 | 192 |
groupID:aSymbol |
193 |
groupID := aSymbol |
|
194 |
||
195 |
"Modified: 5.9.1995 / 16:04:28 / claus" |
|
196 |
! ! |
|
197 |
||
131 | 198 |
!RadioButtonGroup methodsFor:'accessing-mvc'! |
199 |
||
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
200 |
value |
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
201 |
^ valueChannel value |
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
202 |
! |
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
203 |
|
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
204 |
value:anIndex |
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
205 |
valueChannel value:anIndex |
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
206 |
! |
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
207 |
|
131 | 208 |
valueChannel |
209 |
^ valueChannel |
|
210 |
! |
|
211 |
||
212 |
valueChannel:aValueHolder |
|
213 |
|oldValue newValue| |
|
214 |
||
215 |
valueChannel notNil ifTrue:[oldValue := valueChannel value]. |
|
216 |
valueChannel := aValueHolder. |
|
217 |
(newValue := valueChannel value) ~~ oldValue ifTrue:[ |
|
178
5c28593c2a3b
turnOn/ turnOff changed back - now added turnOn/OffWithAction (sorry stefan)
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
218 |
(self at:newValue) turnOnWithAction |
131 | 219 |
] |
220 |
! ! |
|
221 |
||
0 | 222 |
!RadioButtonGroup methodsFor:'adding / removing'! |
223 |
||
224 |
add:aRadioButton |
|
225 |
super add:aRadioButton. |
|
131 | 226 |
|
227 |
" |
|
228 |
this would work, if dependencies were not weak ... |
|
229 |
" |
|
105 | 230 |
"/ aRadioButton addDependent:self. |
131 | 231 |
|
232 |
||
233 |
" |
|
234 |
therefore, we have to install ourself as model of the buttons/toggles |
|
235 |
creating a valueChannel, if there is none yet. |
|
236 |
" |
|
237 |
valueChannel isNil ifTrue:[ |
|
238 |
valueChannel := 0 asValue |
|
239 |
]. |
|
240 |
aRadioButton isOn ifTrue:[valueChannel value:self size]. |
|
177 | 241 |
aRadioButton model:self; aspectMessage:#queryFrom:; changeMessage:#elementChanged:from:. |
131 | 242 |
|
0 | 243 |
! ! |
244 |
||
245 |
!RadioButtonGroup methodsFor:'update'! |
|
246 |
||
105 | 247 |
elementChanged:aToggle |
131 | 248 |
"historic compatibility leftover - will vanish" |
249 |
||
250 |
^ self elementChanged:true from:aToggle |
|
251 |
! |
|
252 |
||
253 |
elementChanged:value from:aToggle |
|
254 |
"some element changed its value - find it, turn the others off |
|
255 |
and update my value" |
|
256 |
||
257 |
|newValue| |
|
258 |
||
177 | 259 |
self keysAndValuesDo:[:index :aComponent | |
260 |
((aToggle == aComponent) or:[aToggle isSubViewOf:aComponent]) ifTrue:[ |
|
131 | 261 |
value ifTrue:[newValue := index] |
177 | 262 |
] ifFalse:[ |
263 |
"/ aComponent isOn ifTrue:[ |
|
264 |
"/ aComponent turnOff |
|
265 |
"/ ] |
|
116 | 266 |
] |
131 | 267 |
]. |
177 | 268 |
"/ aToggle turnOn. |
269 |
valueChannel value:newValue. |
|
270 |
self changed:#queryFrom: |
|
0 | 271 |
|
164 | 272 |
"Modified: 21.9.1995 / 13:42:06 / claus" |
198 | 273 |
! |
274 |
||
275 |
queryFrom:aToggle |
|
276 |
"a query from a toggle, if its the selected one" |
|
277 |
||
278 |
self keysAndValuesDo:[:index :aComponent | |
|
279 |
((aToggle == aComponent) or:[aToggle isSubViewOf:aComponent]) ifTrue:[ |
|
280 |
^ valueChannel value == index |
|
281 |
] |
|
282 |
]. |
|
283 |
^ false |
|
284 |
||
285 |
"Modified: 14.11.1995 / 21:21:27 / cg" |
|
286 |
! |
|
287 |
||
288 |
update:something with:someArgument from:changedButton |
|
289 |
" |
|
290 |
a RadioButton in this group has changed - notify the others |
|
291 |
" |
|
292 |
||
293 |
self elementChanged:(changedButton isOn) from:changedButton |
|
0 | 294 |
! ! |
198 | 295 |