author | Claus Gittinger <cg@exept.de> |
Wed, 13 Dec 1995 17:30:35 +0100 | |
changeset 253 | f071ff350cd5 |
parent 243 | 5c411425097d |
child 355 | a55f62047156 |
permissions | -rw-r--r-- |
0 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 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 |
||
13 |
WarningBox subclass:#YesNoBox |
|
253
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
14 |
instanceVariableNames:'' |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
15 |
classVariableNames:'RequestBitmap' |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
16 |
poolDictionaries:'' |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
17 |
category:'Views-DialogBoxes' |
0 | 18 |
! |
19 |
||
38 | 20 |
!YesNoBox class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
59 | 25 |
All Rights Reserved |
38 | 26 |
|
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 |
this class implements yes-no boxes by adding another (no-) Button to the WarnBox-View. |
|
39 |
They are created with: |
|
40 |
||
59 | 41 |
aBox := YesNoBox title:'some title'. |
42 |
aBox okAction:[ .. some action to be performed when ok is pressed ]. |
|
38 | 43 |
|
44 |
and finally shown with: |
|
0 | 45 |
|
59 | 46 |
aBox showAtPointer |
38 | 47 |
|
48 |
The default box shows 'yes' and 'no' in its buttons; this can be changed with: |
|
49 |
||
59 | 50 |
aBox yesText:'some string'. |
51 |
aBox noText:'some string'. |
|
38 | 52 |
|
53 |
There is also protocol to set both button titles in one message. |
|
54 |
Also, the action associated to the noButton can be changed. |
|
55 |
||
63 | 56 |
For very simple yes/no queries, you can also use the much simpler confirm:. |
57 |
Since implemented in Object, everyone understands confirm. You can pass |
|
58 |
a question message (but not change the buttons labels). |
|
59 |
Use is: |
|
60 |
self confirm:'some question' |
|
61 |
and will return true or false. |
|
125 | 62 |
|
63 |
For compatibility with ST-80, use: |
|
64 |
Dialog confirm:'hello' |
|
63 | 65 |
" |
66 |
! |
|
67 |
||
68 |
examples |
|
69 |
" |
|
38 | 70 |
Examples: |
71 |
||
59 | 72 |
|aBox| |
38 | 73 |
|
59 | 74 |
aBox := YesNoBox title:'Coffee or tee ?'. |
75 |
aBox noText:'tee'. |
|
76 |
aBox yesText:'coffee'. |
|
77 |
aBox yesAction:[Transcript showCr:'make coffee']. |
|
78 |
aBox noAction:[Transcript showCr:'make tee']. |
|
79 |
aBox showAtPointer. |
|
38 | 80 |
|
81 |
or, shorter: |
|
82 |
||
59 | 83 |
|aBox| |
38 | 84 |
|
59 | 85 |
aBox := YesNoBox new. |
86 |
aBox title:'Coffee or Tee ?' |
|
87 |
yesAction:[Transcript showCr:'make coffee'] |
|
88 |
noAction:[Transcript showCr:'make tee']. |
|
89 |
aBox yesText:'Coffee' noText:'Tee'. |
|
90 |
aBox showAtPointer |
|
38 | 91 |
|
92 |
Also, have a look at the inherited protocol; for example, this allows changing |
|
93 |
the bitmap (default: a question mark) and other properties. |
|
59 | 94 |
|
95 |
If the box is needed to ask for a simple boolean, you can also use the |
|
96 |
#confirm method, to bring up a box, let it ask for something and return |
|
97 |
true or false. |
|
98 |
Example: |
|
99 |
||
100 |
|box value| |
|
101 |
||
102 |
box := YesNoBox new. |
|
103 |
value := box confirm:'yes or no:'. |
|
104 |
value ifTrue:[ |
|
105 |
Transcript showCr:'yes' |
|
106 |
] ifFalse:[ |
|
107 |
Transcript showCr:'no' |
|
108 |
] |
|
109 |
||
110 |
of course, this can also be written shorter as: |
|
111 |
||
112 |
(YesNoBox new confirm:'yes or no:') ifTrue:[ |
|
113 |
Transcript showCr:'yes' |
|
114 |
] ifFalse:[ |
|
115 |
Transcript showCr:'no' |
|
116 |
] |
|
125 | 117 |
|
118 |
or: |
|
119 |
||
120 |
(Dialog confirm:'yes or no:') ifTrue:[ |
|
121 |
Transcript showCr:'yes' |
|
122 |
] ifFalse:[ |
|
123 |
Transcript showCr:'no' |
|
124 |
] |
|
38 | 125 |
" |
126 |
! ! |
|
0 | 127 |
|
197 | 128 |
!YesNoBox class methodsFor:'instance creation'! |
59 | 129 |
|
197 | 130 |
title:t yesText:yesString noText:noString |
131 |
^ (self new) title:t yesText:yesString noText:noString |
|
59 | 132 |
! ! |
133 |
||
134 |
!YesNoBox class methodsFor:'icon bitmap'! |
|
135 |
||
136 |
iconBitmap |
|
137 |
"return the bitmap shown as icon in my instances" |
|
138 |
||
139 |
RequestBitmap isNil ifTrue:[ |
|
109 | 140 |
RequestBitmap := Image fromFile:'bitmaps/Request.xbm'. |
141 |
RequestBitmap notNil ifTrue:[ |
|
142 |
RequestBitmap := RequestBitmap on:Display |
|
143 |
]. |
|
59 | 144 |
]. |
145 |
^ RequestBitmap |
|
146 |
! ! |
|
147 |
||
197 | 148 |
!YesNoBox class methodsFor:'styles'! |
149 |
||
150 |
updateStyleCache |
|
151 |
|img| |
|
152 |
||
153 |
img := StyleSheet at:'requestBoxIcon'. |
|
154 |
img notNil ifTrue:[RequestBitmap := img on:Display]. |
|
155 |
! ! |
|
156 |
||
157 |
!YesNoBox methodsFor:'accessing'! |
|
158 |
||
159 |
noAction:aBlock |
|
160 |
"define the action to be performed when no is pressed" |
|
161 |
||
162 |
abortAction := aBlock |
|
163 |
! |
|
164 |
||
165 |
noButton |
|
166 |
"return the no-button" |
|
167 |
||
168 |
^ abortButton |
|
169 |
! |
|
170 |
||
253
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
171 |
noLabel:aString |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
172 |
"define the label of the no-button" |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
173 |
|
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
174 |
self noText:aString. |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
175 |
|
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
176 |
"Created: 13.12.1995 / 16:21:57 / cg" |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
177 |
! |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
178 |
|
197 | 179 |
noText:aString |
180 |
"define the label of the no-button" |
|
181 |
||
182 |
aString ~= abortButton label ifTrue:[ |
|
183 |
abortButton label:aString. |
|
184 |
abortButton resize. |
|
185 |
shown ifTrue:[self resize] |
|
186 |
] |
|
187 |
! |
|
188 |
||
189 |
okText:yesString noText:noString |
|
190 |
"define the labels of both buttons - same as yesText:noText" |
|
191 |
||
192 |
^ self yesText:yesString noText:noString |
|
193 |
! |
|
194 |
||
195 |
title:aString yesAction:yesBlock noAction:noBlock |
|
196 |
"define title and actions" |
|
59 | 197 |
|
197 | 198 |
self title:aString. |
199 |
okAction := yesBlock. |
|
200 |
abortAction := noBlock |
|
201 |
! |
|
202 |
||
203 |
title:aString yesText:yesString noText:noString |
|
204 |
"define title and button labels" |
|
205 |
||
206 |
self title:aString. |
|
207 |
self yesText:yesString noText:noString |
|
208 |
! |
|
209 |
||
210 |
yesAction:aBlock |
|
211 |
"define the action to be performed when yes is pressed" |
|
212 |
||
213 |
okAction := aBlock |
|
214 |
! |
|
215 |
||
216 |
yesAction:yesBlock noAction:noBlock |
|
217 |
"define both actions" |
|
218 |
||
219 |
okAction := yesBlock. |
|
220 |
abortAction := noBlock |
|
221 |
! |
|
222 |
||
223 |
yesButton |
|
224 |
"return the yes-button" |
|
225 |
||
226 |
^ okButton |
|
227 |
! |
|
228 |
||
253
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
229 |
yesLabel:aString |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
230 |
"define the label of the yes-button" |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
231 |
|
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
232 |
self yesText:aString. |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
233 |
|
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
234 |
"Created: 13.12.1995 / 16:22:05 / cg" |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
235 |
! |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
236 |
|
197 | 237 |
yesText:aString |
238 |
"define the label of the yes-button" |
|
239 |
||
240 |
self okText:aString |
|
241 |
! |
|
242 |
||
243 |
yesText:yesString noText:noString |
|
244 |
"define the labels of both buttons" |
|
245 |
||
246 |
((yesString ~= okButton label) or:[noString ~= abortButton label]) ifTrue:[ |
|
247 |
okButton label:yesString. |
|
248 |
abortButton label:noString. |
|
249 |
okButton resize. |
|
250 |
abortButton resize. |
|
251 |
shown ifTrue:[self resize] |
|
252 |
] |
|
59 | 253 |
! ! |
254 |
||
0 | 255 |
!YesNoBox methodsFor:'initialization'! |
256 |
||
197 | 257 |
focusSequence |
258 |
^ Array with:abortButton with:okButton |
|
259 |
! |
|
260 |
||
0 | 261 |
initialize |
262 |
super initialize. |
|
263 |
||
150 | 264 |
label := resources string:'Choose'. |
79 | 265 |
|
130 | 266 |
buttonPanel horizontalLayout:#fitSpace. "/ looks better; should it come from the StyleSheet ? |
63 | 267 |
|
59 | 268 |
textLabel label:'please Confirm'. |
162 | 269 |
okButton logo:(resources at:'yes'). |
0 | 270 |
|
162 | 271 |
abortButton := Button abortButton. |
272 |
buttonPanel addSubView:abortButton before:okButton. |
|
273 |
abortButton logo:(resources at:'no'). |
|
274 |
abortButton height:(okButton height). |
|
275 |
abortButton model:self; change:#noPressed |
|
197 | 276 |
! ! |
277 |
||
278 |
!YesNoBox methodsFor:'queries'! |
|
279 |
||
280 |
preferredExtent |
|
281 |
|w h max| |
|
79 | 282 |
|
197 | 283 |
" |
284 |
make the two buttons of equal size |
|
285 |
" |
|
286 |
max := okButton preferredExtent x max:abortButton preferredExtent x. |
|
287 |
okButton width:max. |
|
288 |
abortButton width:max. |
|
289 |
w := (formLabel width + textLabel width) max:max * 2. |
|
290 |
w := w + (3 * ViewSpacing) + (okButton borderWidth + abortButton borderWidth * 2). |
|
291 |
h := ViewSpacing |
|
292 |
+ ((formLabel height) max:(textLabel height)) |
|
293 |
+ (ViewSpacing * 3) |
|
294 |
+ okButton heightIncludingBorder |
|
295 |
+ ViewSpacing. |
|
296 |
||
297 |
^ (w @ h). |
|
59 | 298 |
! ! |
38 | 299 |
|
59 | 300 |
!YesNoBox methodsFor:'startup'! |
301 |
||
302 |
confirm |
|
303 |
"open the receiver and return true for yes, false for no. |
|
304 |
This is an easier interface to use, since no action blocks |
|
305 |
have to be defined. The title is used as previously defined." |
|
38 | 306 |
|
59 | 307 |
self yesAction:[^ true] noAction:[^ false]. |
63 | 308 |
self showAtPointer. |
309 |
self yesAction:nil noAction:nil. "/ clear actions for earlier release of context |
|
38 | 310 |
|
59 | 311 |
" |
312 |
YesNoBox new confirm |
|
313 |
" |
|
0 | 314 |
! |
315 |
||
59 | 316 |
confirm:aString |
317 |
"open the receiver and return true for yes, false for no. |
|
318 |
This is an easier interface to use, since no action blocks |
|
319 |
have to be defined." |
|
38 | 320 |
|
59 | 321 |
self title:aString. |
322 |
^ self confirm |
|
323 |
||
324 |
" |
|
325 |
YesNoBox new confirm:'really ?' |
|
253
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
326 |
YesNoBox confirm:'really ?' |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
327 |
self confirm:'really ?' |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
328 |
|
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
329 |
for ST-80 compatibility, you should use Dialogs confirm |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
330 |
(which simply forwards the request to the YesNoBox anyway): |
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
331 |
|
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
332 |
Dialog confirm:'really ?' |
59 | 333 |
" |
253
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
334 |
|
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
335 |
"Modified: 13.12.1995 / 16:20:01 / cg" |
0 | 336 |
! ! |
337 |
||
338 |
!YesNoBox methodsFor:'user interaction'! |
|
339 |
||
340 |
noPressed |
|
341 |
"user pressed the no-button; |
|
342 |
hide myself and evaluate the action" |
|
343 |
||
162 | 344 |
abortButton turnOffWithoutRedraw. |
345 |
self hideAndEvaluate:abortAction |
|
0 | 346 |
! ! |
347 |
||
243 | 348 |
!YesNoBox class methodsFor:'documentation'! |
349 |
||
350 |
version |
|
253
f071ff350cd5
added confirm with button-labels
Claus Gittinger <cg@exept.de>
parents:
243
diff
changeset
|
351 |
^ '$Header: /cvs/stx/stx/libwidg/YesNoBox.st,v 1.22 1995-12-13 16:30:35 cg Exp $' |
243 | 352 |
! ! |