76
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
|
|
14 |
'From Smalltalk/X, Version:2.10.4 on 28-dec-1994 at 1:38:02 pm'!
|
|
15 |
|
|
16 |
ModalBox subclass:#DialogBox
|
|
17 |
instanceVariableNames:'buttonPanel okButton okAction abortButton abortAction
|
|
18 |
acceptReturnAsOK'
|
|
19 |
classVariableNames:''
|
|
20 |
poolDictionaries:''
|
|
21 |
category:'Views-DialogBoxes'
|
|
22 |
!
|
|
23 |
|
|
24 |
!DialogBox class methodsFor:'documentation'!
|
|
25 |
|
|
26 |
version
|
|
27 |
"
|
112
|
28 |
$Header: /cvs/stx/stx/libwidg/DialogBox.st,v 1.7 1995-03-26 20:15:10 claus Exp $
|
76
|
29 |
"
|
|
30 |
!
|
|
31 |
|
|
32 |
documentation
|
|
33 |
"
|
|
34 |
this class implements the common behavior of dialogboxes.
|
|
35 |
|
|
36 |
DialogBox is an (abstract) superclass of many other boxes - see InfoBox,
|
|
37 |
WarningBox, YesNoBox etc. for concrete examples.
|
|
38 |
Most of them simply add buttons or other elements.
|
|
39 |
|
|
40 |
instance variables:
|
|
41 |
|
|
42 |
buttonPanel <PanelView> contains the button(s)
|
|
43 |
|
|
44 |
okButton <Button> the ok-Button
|
|
45 |
|
|
46 |
okAction <Block> the action to be performed when ok is pressed,
|
|
47 |
or return is pressed.
|
|
48 |
|
|
49 |
acceptReturnAsOK <Boolean> if true, pressing the return-key counts
|
|
50 |
as if ok was pressed. Default is true.
|
|
51 |
|
|
52 |
abortButton <Button> the cancel-Button
|
|
53 |
|
|
54 |
abortAction <Block> the action to be performed when cancel is
|
|
55 |
pressed.
|
100
|
56 |
|
|
57 |
For compatibility with ST-80, this class is also available under
|
|
58 |
the global names DialogView and Dialog (see patches file).
|
76
|
59 |
"
|
|
60 |
!
|
|
61 |
|
|
62 |
examples
|
|
63 |
"
|
91
|
64 |
since DialogBox is abstract and meant as a base for InfoBox, YesNoBox etc,
|
|
65 |
the following examples are somewhat artificial ...
|
|
66 |
|
|
67 |
|
76
|
68 |
DialogBox new open
|
|
69 |
|
91
|
70 |
DialogBox new addOkButton; open
|
76
|
71 |
|
|
72 |
DialogBox new addAbortButton; addOkButton; open
|
|
73 |
"
|
|
74 |
!
|
|
75 |
|
|
76 |
copyright
|
|
77 |
"
|
|
78 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
79 |
All Rights Reserved
|
|
80 |
|
|
81 |
This software is furnished under a license and may be used
|
|
82 |
only in accordance with the terms of that license and with the
|
|
83 |
inclusion of the above copyright notice. This software may not
|
|
84 |
be provided or otherwise made available to, or used by, any
|
|
85 |
other person. No title to or ownership of the software is
|
|
86 |
hereby transferred.
|
|
87 |
"
|
|
88 |
! !
|
|
89 |
|
100
|
90 |
!DialogBox class methodsFor:'startup'!
|
|
91 |
|
|
92 |
request:aString
|
|
93 |
"launch a Dialog, which allows user to enter something.
|
|
94 |
Return the entered string (may be empty string)
|
|
95 |
or nil (if cancel was pressed)"
|
|
96 |
|
|
97 |
^ self
|
|
98 |
request:aString
|
|
99 |
displayAt:nil
|
|
100 |
centered:true
|
|
101 |
action:nil
|
|
102 |
initialAnswer:''
|
|
103 |
|
|
104 |
"
|
|
105 |
Dialog
|
|
106 |
request:'enter a string:'
|
|
107 |
"
|
|
108 |
!
|
|
109 |
|
|
110 |
request:aString initialAnswer:initial
|
|
111 |
"launch a Dialog, which allows user to enter something.
|
|
112 |
Return the entered string (may be empty string) or nil (if cancel was pressed)"
|
|
113 |
|
|
114 |
^ self
|
|
115 |
request:aString
|
|
116 |
displayAt:nil
|
|
117 |
centered:true
|
|
118 |
action:nil
|
|
119 |
initialAnswer:initial
|
|
120 |
|
|
121 |
"
|
|
122 |
Dialog
|
|
123 |
request:'enter a string:'
|
|
124 |
initialAnswer:'the default'
|
|
125 |
"
|
|
126 |
!
|
|
127 |
|
|
128 |
request:aString displayAt:aPoint initialAnswer:initial
|
|
129 |
"launch a Dialog, which allows user to enter something.
|
|
130 |
Return the entered string (may be empty string) or nil (if cancel was pressed)"
|
|
131 |
|
|
132 |
^ self
|
|
133 |
request:aString
|
|
134 |
displayAt:aPoint
|
|
135 |
centered:true
|
|
136 |
action:nil
|
|
137 |
initialAnswer:initial
|
|
138 |
|
|
139 |
"
|
|
140 |
Dialog
|
|
141 |
request:'enter a string:'
|
|
142 |
displayAt:(50 @ 50)
|
|
143 |
initialAnswer:'the default'
|
|
144 |
"
|
|
145 |
!
|
|
146 |
|
|
147 |
request:aString displayAt:aPoint centered:centered action:resultAction initialAnswer:initial
|
|
148 |
"launch a Dialog, which allows user to enter a string.
|
|
149 |
Return the string (may be empty string) or nil (if cancel was pressed)"
|
|
150 |
|
|
151 |
|box|
|
|
152 |
|
|
153 |
box := EnterBox title:aString.
|
|
154 |
box initialText:initial.
|
|
155 |
resultAction isNil ifTrue:[
|
|
156 |
box action:[:val | ^ val]
|
|
157 |
] ifFalse:[
|
|
158 |
box action:[:val | ^ resultAction value:val]
|
|
159 |
].
|
|
160 |
aPoint notNil ifTrue:[
|
|
161 |
box showAt:aPoint
|
|
162 |
] ifFalse:[
|
|
163 |
box showAtPointer
|
|
164 |
].
|
|
165 |
^ nil
|
|
166 |
|
|
167 |
"
|
|
168 |
Dialog request:'enter a string:'
|
|
169 |
displayAt:0@0
|
|
170 |
centered:true
|
|
171 |
action:[:result | result printNewline]
|
|
172 |
initialAnswer:'the default'
|
|
173 |
"
|
|
174 |
!
|
|
175 |
|
|
176 |
confirm:aString
|
|
177 |
"launch a Dialog, which allows user to enter yes or no.
|
|
178 |
return true for yes, false for no"
|
|
179 |
|
|
180 |
|box answer|
|
|
181 |
|
|
182 |
box := YesNoBox title:aString.
|
|
183 |
box yesAction:[answer := true] noAction:[answer := false].
|
|
184 |
box showAtPointer.
|
|
185 |
box yesAction:nil noAction:nil.
|
|
186 |
^ answer
|
|
187 |
|
|
188 |
"
|
|
189 |
Dialog confirm:'really ?'
|
|
190 |
"
|
|
191 |
!
|
|
192 |
|
|
193 |
confirmWithCancel:aString
|
|
194 |
"launch a Dialog, which allows user to enter yes, no and cancel.
|
|
195 |
return true for 'yes', false for 'no', nil for 'cancel'"
|
|
196 |
|
|
197 |
|box answer|
|
|
198 |
|
|
199 |
box := OptionBox title:aString numberOfOptions:3.
|
|
200 |
box buttonTitles:(self classResources array:
|
|
201 |
#('cancel'
|
|
202 |
'no'
|
|
203 |
'yes')
|
|
204 |
)
|
|
205 |
actions:(Array with:[answer := nil]
|
|
206 |
with:[answer := false]
|
|
207 |
with:[answer := true]
|
|
208 |
).
|
|
209 |
box showAtPointer.
|
|
210 |
box actions:nil.
|
|
211 |
^ answer
|
|
212 |
|
|
213 |
"
|
|
214 |
Dialog confirmWithCancel:'really ?' nil
|
|
215 |
"
|
|
216 |
!
|
|
217 |
|
|
218 |
information:aString
|
|
219 |
"launch a Dialog to tell user something"
|
|
220 |
|
|
221 |
(InfoBox title:aString) showAtPointer
|
|
222 |
|
|
223 |
"
|
|
224 |
Dialog information:'help'
|
|
225 |
"
|
|
226 |
!
|
|
227 |
|
|
228 |
warn:aString
|
|
229 |
"launch a Dialog to warn user"
|
|
230 |
|
|
231 |
(WarningBox title:aString) showAtPointer
|
|
232 |
|
|
233 |
"
|
|
234 |
Dialog warn:'help'
|
|
235 |
"
|
|
236 |
!
|
|
237 |
|
|
238 |
requestFileName:titleString default:defaultName
|
|
239 |
"launch a Dialog, which allows user to enter a filename.
|
|
240 |
The box will not allow pressing 'ok' without an entered string.
|
|
241 |
Return the pathname string consisting of the full pathname of the filename,
|
|
242 |
or an empty string (if cancel was pressed)."
|
|
243 |
|
|
244 |
^ self requestFileName:titleString
|
|
245 |
default:defaultName
|
|
246 |
version:nil
|
|
247 |
ifFail:''
|
|
248 |
|
|
249 |
"
|
|
250 |
Dialog requestFileName:'enter a fileName:' default:''
|
|
251 |
Dialog requestFileName:'enter a fileName:' default:'Makefile.bak'
|
|
252 |
"
|
|
253 |
!
|
|
254 |
|
|
255 |
requestFileName:titleString default:defaultName version:versionSymbol
|
|
256 |
"launch a Dialog, which allows user to enter a filename.
|
|
257 |
The box will not allow pressing 'ok' without an entered string.
|
|
258 |
Return the pathname string or the empty string if cancel was pressed.
|
|
259 |
The version argument is ignored on Unix, but may at some time in the
|
|
260 |
future be used on systems like VMS (which support file versioning)."
|
|
261 |
|
|
262 |
^ self requestFileName:titleString
|
|
263 |
default:defaultName
|
|
264 |
version:versionSymbol
|
|
265 |
ifFail:''
|
|
266 |
|
|
267 |
"
|
|
268 |
Dialog requestFileName:'enter a fileName:'
|
|
269 |
default:''
|
|
270 |
version:nil
|
|
271 |
"
|
|
272 |
!
|
|
273 |
|
|
274 |
requestFileName:titleString default:defaultName version:versionSymbol ifFail:failBlock
|
|
275 |
"launch a Dialog, which allows user to enter a filename.
|
|
276 |
The box will not allow pressing 'ok' without an entered string.
|
|
277 |
Return the string or the value of failBlock if cancel was pressed.
|
|
278 |
The version argument is ignored on Unix, but may at some time in the
|
|
279 |
future be used on systems like VMS (which support file versioning)."
|
|
280 |
|
|
281 |
|box|
|
|
282 |
|
|
283 |
box := FileSelectionBox title:titleString.
|
|
284 |
box initialText:defaultName.
|
|
285 |
box action:[:name | ^ name].
|
|
286 |
box showAtPointer.
|
|
287 |
box action:nil.
|
|
288 |
^ failBlock value
|
|
289 |
|
|
290 |
"
|
|
291 |
Dialog requestFileName:'enter a fileName:'
|
|
292 |
default:''
|
|
293 |
version:nil
|
|
294 |
ifFail:['none']
|
|
295 |
"
|
|
296 |
!
|
|
297 |
|
|
298 |
requestFileName:titleString default:defaultName ok:okText abort:abortText pattern:pattern
|
|
299 |
"launch a Dialog, which allows user to enter a filename.
|
|
300 |
The box will show okText in its okButton, abortText in the abortButton.
|
|
301 |
The matchPattern is set to pattern initially.
|
|
302 |
Return the string or nil if cancel was pressed."
|
|
303 |
|
|
304 |
|box|
|
|
305 |
|
|
306 |
box := FileSelectionBox
|
|
307 |
title:titleString
|
|
308 |
okText:okText
|
|
309 |
abortText:abortText
|
|
310 |
action:[:fileName | ^ fileName].
|
|
311 |
box pattern:pattern.
|
|
312 |
box initialText:defaultName.
|
|
313 |
box showAtPointer.
|
|
314 |
box action:nil.
|
|
315 |
^ nil
|
|
316 |
|
|
317 |
"
|
|
318 |
Dialog requestFileName:'enter a fileName:'
|
|
319 |
default:''
|
|
320 |
ok:'yeah'
|
|
321 |
abort:'oh, no'
|
|
322 |
pattern:'*.st'
|
|
323 |
"
|
|
324 |
! !
|
|
325 |
|
76
|
326 |
!DialogBox methodsFor:'user actions'!
|
|
327 |
|
|
328 |
okPressed
|
|
329 |
"user pressed ok-button; make myself invisible and if an action was
|
|
330 |
specified do it"
|
|
331 |
|
95
|
332 |
okButton turnOffWithoutRedraw.
|
76
|
333 |
self hideAndEvaluate:okAction
|
|
334 |
!
|
|
335 |
|
|
336 |
abortPressed
|
|
337 |
"user pressed abort button - hide myself and evaluate okAction"
|
|
338 |
|
95
|
339 |
abortButton turnOffWithoutRedraw.
|
76
|
340 |
self hideAndEvaluate:abortAction
|
|
341 |
!
|
|
342 |
|
|
343 |
keyPress:aKey x:x y:y
|
|
344 |
"return-key dublicates ok-function if acceptReturnAsOK is true"
|
|
345 |
|
|
346 |
acceptReturnAsOK ifTrue:[
|
|
347 |
(aKey == #Return) ifTrue:[^ self okPressed]
|
|
348 |
].
|
|
349 |
super keyPress:aKey x:x y:y
|
|
350 |
! !
|
|
351 |
|
|
352 |
!DialogBox methodsFor:'queries'!
|
|
353 |
|
|
354 |
positionOffset
|
|
355 |
"return the delta, by which the box should be displayed
|
|
356 |
from the mouse pointer. Value returned here makes center of
|
|
357 |
okButton appear under the cursor"
|
|
358 |
|
|
359 |
okButton isNil ifTrue:[
|
91
|
360 |
^ super positionOffset
|
76
|
361 |
].
|
|
362 |
buttonPanel setChildPositionsIfChanged.
|
|
363 |
^ (okButton originRelativeTo:self) + (okButton extent // 2)
|
|
364 |
!
|
|
365 |
|
|
366 |
preferedExtent
|
|
367 |
"return my prefered extent"
|
|
368 |
|
97
|
369 |
|w h p|
|
76
|
370 |
|
|
371 |
okButton isNil ifTrue:[
|
|
372 |
^ super preferedExtent
|
|
373 |
].
|
97
|
374 |
p := buttonPanel preferedExtent.
|
|
375 |
w := p x.
|
76
|
376 |
h := ViewSpacing
|
97
|
377 |
+ "okButton preferedExtent" p y
|
76
|
378 |
+ ViewSpacing.
|
|
379 |
|
|
380 |
^ w @ h
|
|
381 |
! !
|
|
382 |
|
|
383 |
!DialogBox methodsFor:'initialization'!
|
|
384 |
|
|
385 |
initialize
|
|
386 |
|mm|
|
|
387 |
|
|
388 |
super initialize.
|
|
389 |
|
|
390 |
label := 'Dialog'.
|
|
391 |
|
|
392 |
mm := ViewSpacing.
|
|
393 |
|
|
394 |
acceptReturnAsOK := true.
|
|
395 |
|
|
396 |
buttonPanel := HorizontalPanelView in:self.
|
|
397 |
buttonPanel origin:(0.0 @ 1.0) corner:(1.0 @ 1.0).
|
|
398 |
buttonPanel bottomInset:mm;
|
|
399 |
topInset:(font height + mm * 2) negated.
|
|
400 |
buttonPanel borderWidth:0.
|
|
401 |
buttonPanel layout:#spread.
|
|
402 |
|
|
403 |
"
|
|
404 |
|b|
|
|
405 |
b := DialogBox new.
|
|
406 |
b addAbortButton; addOkButton; showAtPointer
|
|
407 |
"
|
|
408 |
"
|
|
409 |
|b|
|
|
410 |
b := DialogBox new.
|
|
411 |
b addOkButton; showAtPointer
|
|
412 |
"
|
|
413 |
!
|
|
414 |
|
|
415 |
addOkButton
|
|
416 |
"create an okButton - to be sent from redefined initialize
|
|
417 |
methods in subclasses."
|
|
418 |
|
|
419 |
okButton := Button okButtonIn:buttonPanel.
|
95
|
420 |
okButton model:self; change:#okPressed.
|
76
|
421 |
okButton isReturnButton:acceptReturnAsOK.
|
110
|
422 |
buttonPanel subViews size > 1 ifTrue:[
|
|
423 |
buttonPanel layout:#fitSpace.
|
|
424 |
].
|
76
|
425 |
!
|
|
426 |
|
|
427 |
addAbortButton
|
|
428 |
"create an abortButton - to be sent from redefined initialize
|
|
429 |
methods in subclasses."
|
|
430 |
|
110
|
431 |
abortButton := Button abortButtonIn:buttonPanel.
|
|
432 |
abortButton model:self; change:#abortPressed.
|
|
433 |
buttonPanel subViews size > 1 ifTrue:[
|
76
|
434 |
buttonPanel layout:#fitSpace.
|
|
435 |
].
|
|
436 |
!
|
|
437 |
|
|
438 |
reAdjustGeometry
|
|
439 |
"sent late in snapin processing - gives me a chance
|
|
440 |
to resize for changed font dimensions."
|
|
441 |
|
|
442 |
super reAdjustGeometry.
|
|
443 |
okButton notNil ifTrue:[okButton resize].
|
|
444 |
abortButton notNil ifTrue:[abortButton resize].
|
|
445 |
self resize
|
112
|
446 |
!
|
|
447 |
|
|
448 |
focusSequence
|
|
449 |
^ buttonPanel subViews
|
76
|
450 |
! !
|
|
451 |
|
|
452 |
!DialogBox methodsFor:'private'!
|
|
453 |
|
|
454 |
hideAndEvaluate:aBlock
|
|
455 |
"make myself invisible and evaluate aBlock"
|
|
456 |
|
|
457 |
self hide.
|
|
458 |
aBlock notNil ifTrue:[aBlock value]
|
|
459 |
! !
|
|
460 |
|
|
461 |
!DialogBox methodsFor:'accessing'!
|
|
462 |
|
|
463 |
acceptReturnAsOK:aBoolean
|
|
464 |
"turn on/off interpretation of return-key as ok.
|
|
465 |
Default is on"
|
|
466 |
|
|
467 |
acceptReturnAsOK := aBoolean.
|
|
468 |
okButton notNil ifTrue:[
|
|
469 |
okButton isReturnButton:aBoolean.
|
|
470 |
]
|
|
471 |
!
|
|
472 |
|
|
473 |
okAction:aBlock
|
|
474 |
"define the action to be performed when ok is pressed"
|
|
475 |
|
|
476 |
okAction := aBlock
|
|
477 |
!
|
|
478 |
|
|
479 |
action:aBlock
|
|
480 |
"set the action to be performed when user presses ok-button;
|
|
481 |
aBlock must be nil or a block. This method simply
|
|
482 |
reuses okAction: and has been added for a consistent action-setting
|
|
483 |
protocol."
|
|
484 |
|
|
485 |
self okAction:aBlock
|
|
486 |
!
|
|
487 |
|
|
488 |
okButton
|
|
489 |
"return the okButton"
|
|
490 |
|
|
491 |
^ okButton
|
|
492 |
!
|
|
493 |
|
|
494 |
okText:aString
|
|
495 |
"define the text in the ok-button"
|
|
496 |
|
|
497 |
|oldSize|
|
|
498 |
|
|
499 |
aString ~= okButton label ifTrue:[
|
|
500 |
oldSize := okButton extent.
|
|
501 |
okButton label:aString.
|
|
502 |
okButton resize.
|
|
503 |
okButton extent ~= oldSize ifTrue:[
|
97
|
504 |
shown ifTrue:[self resize]
|
76
|
505 |
]
|
|
506 |
]
|
|
507 |
!
|
|
508 |
|
|
509 |
abortAction:aBlock
|
|
510 |
"define the action to be performed when abort is pressed"
|
|
511 |
|
|
512 |
abortAction := aBlock
|
|
513 |
!
|
|
514 |
|
|
515 |
abortButton
|
|
516 |
"return the abortButton"
|
|
517 |
|
|
518 |
^ abortButton
|
|
519 |
!
|
|
520 |
|
|
521 |
abortText:aString
|
|
522 |
"define the label in the abort-button"
|
|
523 |
|
|
524 |
|oldSize|
|
|
525 |
|
|
526 |
aString ~= abortButton label ifTrue:[
|
|
527 |
oldSize := abortButton extent.
|
|
528 |
abortButton label:aString.
|
|
529 |
abortButton resize.
|
|
530 |
abortButton extent ~= oldSize ifTrue:[
|
97
|
531 |
shown ifTrue:[self resize]
|
76
|
532 |
]
|
|
533 |
]
|
|
534 |
!
|
|
535 |
|
|
536 |
okText:okString abortText:abortString
|
|
537 |
"set both texts displayed in the buttons"
|
|
538 |
|
|
539 |
(abortString ~= abortButton label
|
|
540 |
or:[okString ~= okButton label]) ifTrue:[
|
|
541 |
okButton label:okString.
|
|
542 |
abortButton label:abortString.
|
|
543 |
okButton resize.
|
|
544 |
abortButton resize.
|
97
|
545 |
shown ifTrue:[self resize]
|
76
|
546 |
]
|
|
547 |
! !
|