0
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1990 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 |
ModalBox subclass:#EnterBox
|
|
14 |
instanceVariableNames:'labelField enterField buttonPanel
|
59
|
15 |
okButton abortButton
|
|
16 |
okAction abortAction'
|
0
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
38
|
19 |
category:'Views-DialogBoxes'
|
0
|
20 |
!
|
|
21 |
|
|
22 |
EnterBox comment:'
|
5
|
23 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
59
|
24 |
All Rights Reserved
|
0
|
25 |
|
73
|
26 |
$Header: /cvs/stx/stx/libwidg/EnterBox.st,v 1.13 1994-12-28 14:44:02 claus Exp $
|
0
|
27 |
'!
|
|
28 |
|
3
|
29 |
!EnterBox class methodsFor:'documentation'!
|
|
30 |
|
38
|
31 |
copyright
|
|
32 |
"
|
|
33 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
59
|
34 |
All Rights Reserved
|
38
|
35 |
|
|
36 |
This software is furnished under a license and may be used
|
|
37 |
only in accordance with the terms of that license and with the
|
|
38 |
inclusion of the above copyright notice. This software may not
|
|
39 |
be provided or otherwise made available to, or used by, any
|
|
40 |
other person. No title to or ownership of the software is
|
|
41 |
hereby transferred.
|
|
42 |
"
|
|
43 |
!
|
|
44 |
|
|
45 |
version
|
|
46 |
"
|
73
|
47 |
$Header: /cvs/stx/stx/libwidg/EnterBox.st,v 1.13 1994-12-28 14:44:02 claus Exp $
|
38
|
48 |
"
|
|
49 |
!
|
|
50 |
|
3
|
51 |
documentation
|
|
52 |
"
|
38
|
53 |
this class implements a pop-up box to enter some string
|
|
54 |
with 2 buttons; a cancel button, and a trigger-action button.
|
|
55 |
The boxes title can be changed using:
|
3
|
56 |
|
59
|
57 |
aBox title:'some string'
|
38
|
58 |
|
|
59 |
The two button-labels default to 'abort' and 'ok';
|
|
60 |
they can be changed using:
|
3
|
61 |
|
59
|
62 |
aBox okText:'someString'
|
|
63 |
aBox abortText:'someString'
|
38
|
64 |
|
|
65 |
The initial text in the enterfield can be set using:
|
3
|
66 |
|
59
|
67 |
aBox initialText:'someString'
|
3
|
68 |
|
38
|
69 |
when the ok-button is pressed, an action is performed, which is
|
|
70 |
set using:
|
3
|
71 |
|
59
|
72 |
aBox action:[ ... ]
|
3
|
73 |
|
38
|
74 |
the abort-action defaults to no-action, but can also be set.
|
|
75 |
The box can be opened modal (i.e. the currently active view will
|
|
76 |
be suspended) or modeless. The default is modal (i.e. sending #open
|
|
77 |
is equivalent to #openModal).
|
62
|
78 |
"
|
|
79 |
!
|
3
|
80 |
|
62
|
81 |
examples
|
|
82 |
"
|
|
83 |
examples:
|
59
|
84 |
|box|
|
|
85 |
|
|
86 |
box := EnterBox new.
|
|
87 |
box title:'your name please:'.
|
|
88 |
box action:[:arg | Transcript showCr:'entered: ' , arg printString].
|
|
89 |
box open
|
|
90 |
|
|
91 |
|box|
|
8
|
92 |
|
59
|
93 |
box := EnterBox new.
|
|
94 |
box title:'your name please:'.
|
|
95 |
box action:[:arg | Transcript showCr:'entered: ' , arg printString].
|
|
96 |
box openModeless
|
|
97 |
|
62
|
98 |
for easier instance creation, there are also some combination
|
|
99 |
methods for instance creation:
|
|
100 |
|
|
101 |
|box|
|
|
102 |
|
|
103 |
box := EnterBox
|
|
104 |
title:'your name please:'
|
|
105 |
action:[:arg | Transcript showCr:'entered: ' , arg printString].
|
|
106 |
box openModeless
|
|
107 |
|
59
|
108 |
If the box is needed to ask for a simple string, you can also use the
|
|
109 |
#request method, to bring up a box, let it ask for something and return
|
|
110 |
the entered string. This method will return nil, if the box was
|
|
111 |
closed with the 'abort' button.
|
|
112 |
Example:
|
8
|
113 |
|
59
|
114 |
|box string|
|
|
115 |
|
60
|
116 |
box := EnterBox request:'input some string:'.
|
59
|
117 |
string isNil ifTrue:[
|
|
118 |
Transcript showCr:'no input'
|
|
119 |
] ifFalse:[
|
|
120 |
Transcript showCr:('the enetered string was: ' , string)
|
|
121 |
]
|
38
|
122 |
|
59
|
123 |
of course, this can be written shorter as:
|
|
124 |
|
|
125 |
|string|
|
|
126 |
|
60
|
127 |
string := EnterBox request:'input some string:'.
|
59
|
128 |
string isNil ifTrue:[
|
|
129 |
Transcript showCr:'no input'
|
|
130 |
] ifFalse:[
|
|
131 |
Transcript showCr:('the enetered string was: ' , string)
|
|
132 |
]
|
3
|
133 |
"
|
|
134 |
! !
|
|
135 |
|
0
|
136 |
!EnterBox class methodsFor:'defaults'!
|
|
137 |
|
|
138 |
defaultExtent
|
|
139 |
^ (Display pixelPerMillimeter * (60 @ 30)) rounded
|
43
|
140 |
!
|
|
141 |
|
|
142 |
minExtent
|
|
143 |
^ self defaultExtent
|
0
|
144 |
! !
|
|
145 |
|
59
|
146 |
!EnterBox class methodsFor:'easy startup '!
|
|
147 |
|
|
148 |
request:aTitle
|
60
|
149 |
"create and show an enterBox asking for aTitle.
|
|
150 |
Return the enterred string or nil (if abort was pressed).
|
|
151 |
The string may be empty, in case return was pressed immediately."
|
|
152 |
|
59
|
153 |
^ self new request:aTitle
|
60
|
154 |
|
|
155 |
"
|
|
156 |
EnterBox request:'enter a string'
|
|
157 |
"
|
59
|
158 |
! !
|
|
159 |
|
0
|
160 |
!EnterBox class methodsFor:'instance creation'!
|
|
161 |
|
|
162 |
action:aBlock
|
|
163 |
"create and return a new EnterBox
|
|
164 |
which will evaluate aBlock when 'ok' is pressed"
|
|
165 |
|
|
166 |
^ (self new) action:aBlock
|
|
167 |
|
|
168 |
"(EnterBox action:[:string | Transcript showCr:string]) showAtPointer"
|
|
169 |
!
|
|
170 |
|
62
|
171 |
title:titleString
|
|
172 |
"create and return a new EnterBox with title aString"
|
|
173 |
|
|
174 |
^ self new title:titleString
|
|
175 |
!
|
|
176 |
|
0
|
177 |
title:titleString action:aBlock
|
|
178 |
"create and return a new EnterBox with title aString,
|
|
179 |
which will evaluate aBlock when 'ok' is pressed"
|
|
180 |
|
|
181 |
^ ((self new) title:titleString) action:aBlock
|
|
182 |
!
|
|
183 |
|
|
184 |
title:titleString okText:okText abortText:abortText action:aBlock
|
|
185 |
"create and return a new EnterBox with title aString, and buttons showing
|
|
186 |
okText and abortText; it will evaluate aBlock when 'ok' is pressed"
|
|
187 |
|
|
188 |
^ ((self new) title:titleString
|
59
|
189 |
okText:okText
|
|
190 |
abortText:abortText) action:aBlock
|
0
|
191 |
! !
|
|
192 |
|
|
193 |
!EnterBox methodsFor:'initialization'!
|
|
194 |
|
|
195 |
initialize
|
73
|
196 |
|space2 innerWidth bw2 bh|
|
0
|
197 |
|
|
198 |
super initialize.
|
|
199 |
|
73
|
200 |
label := 'Enter'.
|
|
201 |
|
0
|
202 |
space2 := 2 * ViewSpacing.
|
73
|
203 |
bw2 := borderWidth * 2.
|
|
204 |
innerWidth := width - space2.
|
0
|
205 |
|
|
206 |
labelField := Label in:self.
|
73
|
207 |
labelField label:'';
|
|
208 |
borderWidth:0;
|
|
209 |
adjust:#center.
|
0
|
210 |
|
|
211 |
labelField origin:(ViewSpacing @ ViewSpacing)
|
59
|
212 |
extent:(innerWidth @ labelField height).
|
0
|
213 |
|
38
|
214 |
self createEnterField.
|
0
|
215 |
enterField origin:[ViewSpacing @ (space2 + labelField height)]
|
59
|
216 |
extent:[(width - space2 - (enterField borderWidth * 2) - margin) @ enterField height].
|
0
|
217 |
enterField leaveAction:[:key | self okPressed].
|
59
|
218 |
enterField addDependent:self. "to get preferedExtent-changes"
|
0
|
219 |
|
73
|
220 |
bh := font height * 2 + bw2.
|
0
|
221 |
buttonPanel := HorizontalPanelView in:self.
|
73
|
222 |
buttonPanel origin:[ViewSpacing @ (height - bh - ViewSpacing)]
|
|
223 |
extent:[(width - space2) @ bh].
|
0
|
224 |
|
73
|
225 |
buttonPanel layout:#fit; borderWidth:0.
|
0
|
226 |
|
59
|
227 |
abortButton := Button abortButtonIn:buttonPanel.
|
|
228 |
abortButton action:[
|
|
229 |
abortButton turnOffWithoutRedraw.
|
|
230 |
self abortPressed
|
|
231 |
].
|
0
|
232 |
|
59
|
233 |
okButton := Button okButtonIn:buttonPanel.
|
|
234 |
okButton action:[
|
|
235 |
okButton turnOffWithoutRedraw.
|
|
236 |
self okPressed
|
|
237 |
].
|
0
|
238 |
|
59
|
239 |
"
|
|
240 |
forward keyboard input to the enterfield
|
|
241 |
"
|
0
|
242 |
self keyboardHandler:enterField
|
|
243 |
!
|
|
244 |
|
38
|
245 |
createEnterField
|
73
|
246 |
"this has been extracted from the initialize method
|
|
247 |
to allow redefinition in subclasses. (FilenameEnterBox for example)"
|
38
|
248 |
|
|
249 |
enterField := EditField in:self.
|
0
|
250 |
!
|
|
251 |
|
|
252 |
reAdjustGeometry
|
|
253 |
"sent late in snapin processing - gives me a chance
|
|
254 |
to resize for new font dimensions"
|
|
255 |
|
|
256 |
super reAdjustGeometry.
|
|
257 |
labelField resize.
|
|
258 |
okButton resize.
|
|
259 |
abortButton resize.
|
|
260 |
self resize
|
|
261 |
! !
|
|
262 |
|
7
|
263 |
!EnterBox methodsFor:'realization'!
|
|
264 |
|
|
265 |
positionOffset
|
|
266 |
"return the delta, by which the box should be displayed
|
|
267 |
from the mouse pointer. Value returned here makes
|
|
268 |
okButton appear under the cursor"
|
|
269 |
|
60
|
270 |
buttonPanel setChildPositionsIfChanged.
|
7
|
271 |
^ (okButton originRelativeTo:self) + (okButton extent // 2)
|
|
272 |
! !
|
|
273 |
|
59
|
274 |
!EnterBox methodsFor:'startup'!
|
|
275 |
|
|
276 |
request
|
|
277 |
"open the box and return the entered string or nil, if
|
|
278 |
abort was pressed"
|
|
279 |
|
|
280 |
self action:[:string | ^ string].
|
|
281 |
self open.
|
|
282 |
^ nil
|
|
283 |
!
|
|
284 |
|
|
285 |
request:title
|
|
286 |
"set the title, open the box and return the entered string or nil, if
|
|
287 |
abort was pressed"
|
|
288 |
|
|
289 |
self title:title.
|
|
290 |
^ self request
|
|
291 |
! !
|
|
292 |
|
62
|
293 |
!EnterBox methodsFor:'queries'!
|
0
|
294 |
|
59
|
295 |
preferedExtent
|
73
|
296 |
|wWanted hWanted wPanel vs2 min panelPref|
|
59
|
297 |
|
|
298 |
vs2 := ViewSpacing * 2.
|
73
|
299 |
panelPref := buttonPanel preferedExtent.
|
59
|
300 |
wWanted := (labelField widthIncludingBorder max:enterField preferedExtent x) + vs2.
|
73
|
301 |
wPanel := panelPref x + vs2.
|
59
|
302 |
wPanel > wWanted ifTrue:[
|
|
303 |
wWanted := wPanel
|
|
304 |
].
|
|
305 |
hWanted := vs2 + labelField height + enterField height +
|
73
|
306 |
(ViewSpacing * 6) + panelPref y + ViewSpacing.
|
59
|
307 |
|
|
308 |
min := self class minExtent.
|
|
309 |
wWanted < min x ifTrue:[
|
|
310 |
wWanted := min x
|
|
311 |
].
|
|
312 |
hWanted < min y ifTrue:[
|
|
313 |
hWanted := min y
|
|
314 |
].
|
|
315 |
^ wWanted @ hWanted
|
0
|
316 |
! !
|
|
317 |
|
|
318 |
!EnterBox methodsFor:'accessing'!
|
|
319 |
|
|
320 |
title:aString
|
|
321 |
"set the title to be displayed at top of enterBox"
|
|
322 |
|
3
|
323 |
|oldSize|
|
|
324 |
|
8
|
325 |
aString ~= labelField label ifTrue:[
|
59
|
326 |
oldSize := labelField extent.
|
|
327 |
labelField label:aString.
|
|
328 |
labelField resize.
|
3
|
329 |
|
59
|
330 |
labelField extent ~= oldSize ifTrue:[
|
|
331 |
self resize
|
|
332 |
]
|
3
|
333 |
]
|
0
|
334 |
!
|
|
335 |
|
|
336 |
title:titleString okText:okString abortText:abortString
|
|
337 |
"set title and texts in the buttons"
|
|
338 |
|
8
|
339 |
(titleString ~= labelField label
|
|
340 |
or:[okString ~= okButton label
|
|
341 |
or:[abortString ~= abortButton label]]) ifTrue:[
|
59
|
342 |
okButton label:okString.
|
|
343 |
okButton resize.
|
|
344 |
abortButton label:abortString.
|
|
345 |
abortButton resize.
|
|
346 |
labelField label:titleString.
|
|
347 |
labelField resize.
|
|
348 |
self resize.
|
8
|
349 |
]
|
0
|
350 |
!
|
|
351 |
|
|
352 |
title:titleString okText:okString
|
|
353 |
"set title and text in okbutton"
|
|
354 |
|
8
|
355 |
(titleString ~= labelField label or:[okString ~= okButton label]) ifTrue:[
|
59
|
356 |
okButton label:okString.
|
|
357 |
okButton resize.
|
|
358 |
labelField label:titleString.
|
|
359 |
labelField resize.
|
|
360 |
self resize.
|
8
|
361 |
]
|
0
|
362 |
!
|
|
363 |
|
|
364 |
okText:aString
|
|
365 |
"set the text to be displayed in the ok-button"
|
|
366 |
|
3
|
367 |
|oldSize|
|
|
368 |
|
8
|
369 |
aString ~= okButton label ifTrue:[
|
59
|
370 |
oldSize := okButton extent.
|
|
371 |
okButton label:aString.
|
|
372 |
okButton resize.
|
|
373 |
okButton extent ~= oldSize ifTrue:[
|
|
374 |
self resize
|
|
375 |
]
|
3
|
376 |
]
|
0
|
377 |
!
|
|
378 |
|
|
379 |
abortText:aString
|
|
380 |
"set the text to be displayed in the abort-button"
|
|
381 |
|
3
|
382 |
|oldSize|
|
|
383 |
|
8
|
384 |
aString ~= abortButton label ifTrue:[
|
59
|
385 |
oldSize := abortButton extent.
|
|
386 |
abortButton label:aString.
|
|
387 |
abortButton resize.
|
|
388 |
abortButton extent ~= oldSize ifTrue:[
|
|
389 |
self resize
|
|
390 |
]
|
3
|
391 |
]
|
0
|
392 |
!
|
|
393 |
|
|
394 |
okText:okString abortText:abortString
|
|
395 |
"set both texts displayed in the buttons"
|
|
396 |
|
8
|
397 |
(abortString ~= abortButton label
|
|
398 |
or:[okString ~= okButton label]) ifTrue:[
|
59
|
399 |
okButton label:okString.
|
|
400 |
abortButton label:abortString.
|
|
401 |
okButton resize.
|
|
402 |
abortButton resize.
|
|
403 |
self resize
|
8
|
404 |
]
|
0
|
405 |
!
|
|
406 |
|
|
407 |
contents
|
|
408 |
"return my contents"
|
|
409 |
|
|
410 |
^ enterField contents
|
|
411 |
!
|
|
412 |
|
|
413 |
initialText:aString
|
|
414 |
"define the initial text in the enterfield. all will be selected initially"
|
|
415 |
|
|
416 |
enterField initialText:aString
|
|
417 |
!
|
|
418 |
|
|
419 |
initialText:aString selectFrom:start to:stop
|
|
420 |
"define the initial text in the enterfield, and the part to be selected"
|
|
421 |
|
|
422 |
enterField initialText:aString.
|
|
423 |
enterField selectFromLine:1 col:start toLine:1 col:stop
|
|
424 |
!
|
|
425 |
|
|
426 |
action:aBlock
|
|
427 |
"set the action to be performed when user presses ok-button;
|
|
428 |
aBlock must be nil or a block with one argument "
|
|
429 |
|
|
430 |
okAction := aBlock
|
|
431 |
!
|
|
432 |
|
38
|
433 |
okAction:aBlock
|
|
434 |
"same as action - for your convenience"
|
|
435 |
|
|
436 |
okAction := aBlock
|
|
437 |
!
|
|
438 |
|
0
|
439 |
abortAction:aBlock
|
|
440 |
"set the action to be performed when user presses abort-button;
|
|
441 |
aBlock must be nil or a block with no arguments"
|
|
442 |
|
|
443 |
abortAction := aBlock
|
|
444 |
! !
|
|
445 |
|
38
|
446 |
!EnterBox methodsFor:'dependencies'!
|
|
447 |
|
59
|
448 |
update:something with:someArgument from:changedObject
|
38
|
449 |
"sent if my enterbox thinks it needs more real-estate ..."
|
|
450 |
|
59
|
451 |
changedObject == enterField ifTrue:[
|
|
452 |
something == #preferedExtent ifTrue:[
|
|
453 |
self resize
|
|
454 |
]
|
38
|
455 |
]
|
|
456 |
! !
|
|
457 |
|
0
|
458 |
!EnterBox methodsFor:'user interaction'!
|
|
459 |
|
|
460 |
hideAndEvaluate:aBlock
|
|
461 |
"common processing for all ok-actions (see subclasses);
|
|
462 |
shut down box, fetch entered string and evaluate the action with it"
|
|
463 |
|
|
464 |
|string|
|
|
465 |
|
|
466 |
self hide.
|
|
467 |
aBlock notNil ifTrue:[
|
59
|
468 |
string := self contents.
|
|
469 |
string isNil ifTrue:[
|
|
470 |
string := ''
|
|
471 |
] ifFalse:[
|
|
472 |
string := string withoutSeparators
|
|
473 |
].
|
|
474 |
aBlock value:string
|
0
|
475 |
]
|
|
476 |
!
|
|
477 |
|
|
478 |
okPressed
|
|
479 |
"user pressed ok button - hide myself and evaluate okAction"
|
|
480 |
|
|
481 |
self hideAndEvaluate:okAction
|
|
482 |
!
|
|
483 |
|
|
484 |
abortPressed
|
|
485 |
"user pressed abort button - hide myself and evaluate okAction"
|
|
486 |
|
|
487 |
self hideAndEvaluate:abortAction
|
|
488 |
! !
|