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
|
197
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:'RequestBitmap'
|
|
16 |
poolDictionaries:''
|
|
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 |
|
|
171 |
noText:aString
|
|
172 |
"define the label of the no-button"
|
|
173 |
|
|
174 |
aString ~= abortButton label ifTrue:[
|
|
175 |
abortButton label:aString.
|
|
176 |
abortButton resize.
|
|
177 |
shown ifTrue:[self resize]
|
|
178 |
]
|
|
179 |
!
|
|
180 |
|
|
181 |
okText:yesString noText:noString
|
|
182 |
"define the labels of both buttons - same as yesText:noText"
|
|
183 |
|
|
184 |
^ self yesText:yesString noText:noString
|
|
185 |
!
|
|
186 |
|
|
187 |
title:aString yesAction:yesBlock noAction:noBlock
|
|
188 |
"define title and actions"
|
59
|
189 |
|
197
|
190 |
self title:aString.
|
|
191 |
okAction := yesBlock.
|
|
192 |
abortAction := noBlock
|
|
193 |
!
|
|
194 |
|
|
195 |
title:aString yesText:yesString noText:noString
|
|
196 |
"define title and button labels"
|
|
197 |
|
|
198 |
self title:aString.
|
|
199 |
self yesText:yesString noText:noString
|
|
200 |
!
|
|
201 |
|
|
202 |
yesAction:aBlock
|
|
203 |
"define the action to be performed when yes is pressed"
|
|
204 |
|
|
205 |
okAction := aBlock
|
|
206 |
!
|
|
207 |
|
|
208 |
yesAction:yesBlock noAction:noBlock
|
|
209 |
"define both actions"
|
|
210 |
|
|
211 |
okAction := yesBlock.
|
|
212 |
abortAction := noBlock
|
|
213 |
!
|
|
214 |
|
|
215 |
yesButton
|
|
216 |
"return the yes-button"
|
|
217 |
|
|
218 |
^ okButton
|
|
219 |
!
|
|
220 |
|
|
221 |
yesText:aString
|
|
222 |
"define the label of the yes-button"
|
|
223 |
|
|
224 |
self okText:aString
|
|
225 |
!
|
|
226 |
|
|
227 |
yesText:yesString noText:noString
|
|
228 |
"define the labels of both buttons"
|
|
229 |
|
|
230 |
((yesString ~= okButton label) or:[noString ~= abortButton label]) ifTrue:[
|
|
231 |
okButton label:yesString.
|
|
232 |
abortButton label:noString.
|
|
233 |
okButton resize.
|
|
234 |
abortButton resize.
|
|
235 |
shown ifTrue:[self resize]
|
|
236 |
]
|
59
|
237 |
! !
|
|
238 |
|
0
|
239 |
!YesNoBox methodsFor:'initialization'!
|
|
240 |
|
197
|
241 |
focusSequence
|
|
242 |
^ Array with:abortButton with:okButton
|
|
243 |
!
|
|
244 |
|
0
|
245 |
initialize
|
|
246 |
super initialize.
|
|
247 |
|
150
|
248 |
label := resources string:'Choose'.
|
79
|
249 |
|
130
|
250 |
buttonPanel horizontalLayout:#fitSpace. "/ looks better; should it come from the StyleSheet ?
|
63
|
251 |
|
59
|
252 |
textLabel label:'please Confirm'.
|
162
|
253 |
okButton logo:(resources at:'yes').
|
0
|
254 |
|
162
|
255 |
abortButton := Button abortButton.
|
|
256 |
buttonPanel addSubView:abortButton before:okButton.
|
|
257 |
abortButton logo:(resources at:'no').
|
|
258 |
abortButton height:(okButton height).
|
|
259 |
abortButton model:self; change:#noPressed
|
197
|
260 |
! !
|
|
261 |
|
|
262 |
!YesNoBox methodsFor:'queries'!
|
|
263 |
|
|
264 |
preferredExtent
|
|
265 |
|w h max|
|
79
|
266 |
|
197
|
267 |
"
|
|
268 |
make the two buttons of equal size
|
|
269 |
"
|
|
270 |
max := okButton preferredExtent x max:abortButton preferredExtent x.
|
|
271 |
okButton width:max.
|
|
272 |
abortButton width:max.
|
|
273 |
w := (formLabel width + textLabel width) max:max * 2.
|
|
274 |
w := w + (3 * ViewSpacing) + (okButton borderWidth + abortButton borderWidth * 2).
|
|
275 |
h := ViewSpacing
|
|
276 |
+ ((formLabel height) max:(textLabel height))
|
|
277 |
+ (ViewSpacing * 3)
|
|
278 |
+ okButton heightIncludingBorder
|
|
279 |
+ ViewSpacing.
|
|
280 |
|
|
281 |
^ (w @ h).
|
59
|
282 |
! !
|
38
|
283 |
|
59
|
284 |
!YesNoBox methodsFor:'startup'!
|
|
285 |
|
|
286 |
confirm
|
|
287 |
"open the receiver and return true for yes, false for no.
|
|
288 |
This is an easier interface to use, since no action blocks
|
|
289 |
have to be defined. The title is used as previously defined."
|
38
|
290 |
|
59
|
291 |
self yesAction:[^ true] noAction:[^ false].
|
63
|
292 |
self showAtPointer.
|
|
293 |
self yesAction:nil noAction:nil. "/ clear actions for earlier release of context
|
38
|
294 |
|
59
|
295 |
"
|
|
296 |
YesNoBox new confirm
|
|
297 |
"
|
0
|
298 |
!
|
|
299 |
|
59
|
300 |
confirm:aString
|
|
301 |
"open the receiver and return true for yes, false for no.
|
|
302 |
This is an easier interface to use, since no action blocks
|
|
303 |
have to be defined."
|
38
|
304 |
|
59
|
305 |
self title:aString.
|
|
306 |
^ self confirm
|
|
307 |
|
|
308 |
"
|
|
309 |
YesNoBox new confirm:'really ?'
|
|
310 |
"
|
0
|
311 |
! !
|
|
312 |
|
|
313 |
!YesNoBox methodsFor:'user interaction'!
|
|
314 |
|
|
315 |
noPressed
|
|
316 |
"user pressed the no-button;
|
|
317 |
hide myself and evaluate the action"
|
|
318 |
|
162
|
319 |
abortButton turnOffWithoutRedraw.
|
|
320 |
self hideAndEvaluate:abortAction
|
0
|
321 |
! !
|
|
322 |
|
243
|
323 |
!YesNoBox class methodsFor:'documentation'!
|
|
324 |
|
|
325 |
version
|
|
326 |
^ '$Header: /cvs/stx/stx/libwidg/YesNoBox.st,v 1.21 1995-12-07 22:24:08 cg Exp $'
|
|
327 |
! !
|