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 |
|
121
|
13 |
'From Smalltalk/X, Version:2.10.5 on 4-may-1995 at 8:46:55 am'!
|
105
|
14 |
|
0
|
15 |
EditTextView subclass:#EditField
|
122
|
16 |
instanceVariableNames:'leaveAction enabled enableAction crAction tabAction
|
|
17 |
converter acceptAction leaveKeys alwaysAccept acceptOnLeave acceptOnReturn'
|
105
|
18 |
classVariableNames:'DefaultForegroundColor DefaultBackgroundColor
|
110
|
19 |
DefaultSelectionForegroundColor DefaultSelectionBackgroundColor
|
|
20 |
DefaultFont'
|
105
|
21 |
poolDictionaries:''
|
|
22 |
category:'Views-Text'
|
0
|
23 |
!
|
|
24 |
|
|
25 |
EditField comment:'
|
5
|
26 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
59
|
27 |
All Rights Reserved
|
0
|
28 |
|
122
|
29 |
$Header: /cvs/stx/stx/libwidg/EditField.st,v 1.20 1995-05-07 00:15:56 claus Exp $
|
19
|
30 |
'!
|
|
31 |
|
|
32 |
!EditField class methodsFor:'documentation'!
|
|
33 |
|
38
|
34 |
copyright
|
|
35 |
"
|
|
36 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
59
|
37 |
All Rights Reserved
|
38
|
38 |
|
|
39 |
This software is furnished under a license and may be used
|
|
40 |
only in accordance with the terms of that license and with the
|
|
41 |
inclusion of the above copyright notice. This software may not
|
|
42 |
be provided or otherwise made available to, or used by, any
|
|
43 |
other person. No title to or ownership of the software is
|
|
44 |
hereby transferred.
|
|
45 |
"
|
|
46 |
!
|
|
47 |
|
|
48 |
version
|
|
49 |
"
|
122
|
50 |
$Header: /cvs/stx/stx/libwidg/EditField.st,v 1.20 1995-05-07 00:15:56 claus Exp $
|
38
|
51 |
"
|
|
52 |
!
|
|
53 |
|
19
|
54 |
documentation
|
|
55 |
"
|
38
|
56 |
an editable text-field. Realized by using an EditTextView,
|
|
57 |
and forcing its size to 1 line - disabling cursor movement
|
|
58 |
in the vertical direction.
|
|
59 |
An action (leaveAction) is performed when the field is left
|
|
60 |
by either Return or a cursor movement, or if 'accept' is
|
|
61 |
performed from the menu.
|
121
|
62 |
|
|
63 |
Instance variables:
|
|
64 |
|
|
65 |
leaveAction <Block | nil> if non-nil, this is evaluated with
|
|
66 |
the key (#Return, #CursorUp etc.) when
|
|
67 |
the field is left via keyboard keys.
|
|
68 |
(fieldGroups use this to decide which
|
|
69 |
field has to be enabled next)
|
|
70 |
|
|
71 |
enabled <Boolean> if false, input is ignored.
|
|
72 |
|
|
73 |
enableAction <Block | nil>
|
|
74 |
|
|
75 |
crAction <Block | nil> if non-nil, keyboard input of a cr are not
|
|
76 |
handled specially, instead this block is evaluated
|
|
77 |
(however, this block can perform additional checks and send
|
|
78 |
a #accept then)
|
|
79 |
|
|
80 |
tabAction <Block | nil> if non-nil, keyboard input of a tab character
|
|
81 |
is not entered into the text, instead this block
|
|
82 |
is evaluated.
|
|
83 |
|
|
84 |
converter <PrintConverter | nil> if non-nil, this is supposed to convert between
|
|
85 |
the object and its printed representation.
|
|
86 |
Defaults to nil i.e. assume that strings are edited.
|
|
87 |
|
|
88 |
acceptAction <Block | nil> if non-nil, this is performed in addition to
|
|
89 |
the leaveAction.
|
|
90 |
|
|
91 |
leaveKeys <Collection> keys which are interpreted as 'leving the field'
|
122
|
92 |
|
|
93 |
alwaysAccept <Boolean> if true, every change of the text is immediately
|
|
94 |
forwardd to the model/acceptBlock.
|
|
95 |
Default is false i.e. only forward changes
|
|
96 |
on accept.
|
|
97 |
|
|
98 |
acceptOnLeave <Boolean> if true, leaving the field (via cursor keys)
|
|
99 |
automatically accepts the value into the model.
|
|
100 |
Default is false.
|
|
101 |
|
|
102 |
acceptOnReturn <Boolean> if true, leaving the field via return
|
|
103 |
automatically accepts the value into the model.
|
|
104 |
Default is true.
|
19
|
105 |
"
|
120
|
106 |
!
|
|
107 |
|
|
108 |
examples
|
|
109 |
"
|
122
|
110 |
see more examples in EnterFieldGroup>>examples.
|
|
111 |
|
|
112 |
|
|
113 |
basic field in a view:
|
120
|
114 |
|
|
115 |
|top field|
|
|
116 |
|
|
117 |
top := StandardSystemView new.
|
|
118 |
top extent:200@100.
|
|
119 |
|
|
120 |
field := EditField origin:0.0@0.0 in:top.
|
|
121 |
field width:1.0. 'let its height as-is'.
|
|
122 |
|
|
123 |
top open
|
|
124 |
|
122
|
125 |
|
|
126 |
forward input in topView to field:
|
|
127 |
|
|
128 |
|top field|
|
|
129 |
|
|
130 |
top := StandardSystemView new.
|
|
131 |
top extent:200@100.
|
|
132 |
|
|
133 |
field := EditField origin:0.0@0.0 in:top.
|
|
134 |
field width:1.0. 'let its height as-is'.
|
|
135 |
|
|
136 |
top delegate:(KeyboardForwarder toView:field).
|
|
137 |
top open
|
|
138 |
|
|
139 |
|
120
|
140 |
just to make it look better: set some inset:
|
|
141 |
|
|
142 |
|top field|
|
|
143 |
|
|
144 |
top := StandardSystemView new.
|
|
145 |
top extent:200@100.
|
|
146 |
|
|
147 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
148 |
field width:1.0. 'let its height as-is'.
|
|
149 |
field leftInset:ViewSpacing;
|
|
150 |
rightInset:ViewSpacing.
|
|
151 |
|
|
152 |
top open
|
|
153 |
|
122
|
154 |
|
120
|
155 |
give it an initial contents:
|
|
156 |
|
|
157 |
|top field|
|
|
158 |
|
|
159 |
top := StandardSystemView new.
|
|
160 |
top extent:200@100.
|
|
161 |
|
|
162 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
163 |
field width:1.0.
|
|
164 |
field leftInset:ViewSpacing;
|
|
165 |
rightInset:ViewSpacing.
|
121
|
166 |
field editValue:'hello world'.
|
120
|
167 |
|
|
168 |
top open
|
|
169 |
|
122
|
170 |
|
120
|
171 |
and have it preselected:
|
|
172 |
|
|
173 |
|top field|
|
|
174 |
|
|
175 |
top := StandardSystemView new.
|
|
176 |
top extent:200@100.
|
|
177 |
|
|
178 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
179 |
field width:1.0.
|
|
180 |
field leftInset:ViewSpacing;
|
|
181 |
rightInset:ViewSpacing.
|
121
|
182 |
field editValue:'hello world' selected:true.
|
120
|
183 |
|
|
184 |
top open
|
|
185 |
|
122
|
186 |
|
120
|
187 |
have part of it preselected:
|
|
188 |
|
|
189 |
|top field|
|
|
190 |
|
|
191 |
top := StandardSystemView new.
|
|
192 |
top extent:200@100.
|
|
193 |
|
|
194 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
195 |
field width:1.0.
|
|
196 |
field leftInset:ViewSpacing;
|
|
197 |
rightInset:ViewSpacing.
|
121
|
198 |
field editValue:'hello world';
|
120
|
199 |
selectFromCharacterPosition:1 to:5.
|
|
200 |
|
|
201 |
top open
|
|
202 |
|
122
|
203 |
|
121
|
204 |
use a converter:
|
|
205 |
- numbers:
|
|
206 |
|
|
207 |
|top field|
|
|
208 |
|
|
209 |
top := StandardSystemView new.
|
|
210 |
top extent:200@100.
|
|
211 |
|
|
212 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
213 |
field width:1.0.
|
|
214 |
field leftInset:ViewSpacing;
|
|
215 |
rightInset:ViewSpacing.
|
|
216 |
|
|
217 |
field converter:(PrintConverter new initForNumber).
|
|
218 |
field editValue:1234.
|
|
219 |
field acceptAction:[:value | Transcript showCr:value].
|
|
220 |
field crAction:[field accept. top destroy].
|
|
221 |
top open.
|
|
222 |
|
|
223 |
- dates:
|
|
224 |
|
|
225 |
|top field|
|
|
226 |
|
|
227 |
top := StandardSystemView new.
|
|
228 |
top extent:200@100.
|
|
229 |
|
|
230 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
231 |
field width:1.0.
|
|
232 |
field leftInset:ViewSpacing;
|
|
233 |
rightInset:ViewSpacing.
|
|
234 |
|
|
235 |
field converter:(PrintConverter new initForDate).
|
|
236 |
field editValue:Date today.
|
|
237 |
field acceptAction:[:value | Transcript show:value class name; space; showCr:value].
|
|
238 |
field crAction:[field accept. top destroy].
|
|
239 |
top open.
|
|
240 |
|
122
|
241 |
|
121
|
242 |
setting alwaysAccept, makes the field update with every key:
|
|
243 |
- numbers:
|
|
244 |
|
|
245 |
|top field|
|
|
246 |
|
|
247 |
top := StandardSystemView new.
|
|
248 |
top extent:200@100.
|
|
249 |
|
|
250 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
251 |
field width:1.0.
|
|
252 |
field leftInset:ViewSpacing;
|
|
253 |
rightInset:ViewSpacing.
|
|
254 |
|
|
255 |
field converter:(PrintConverter new initForNumber).
|
|
256 |
field alwaysAccept:true.
|
|
257 |
field editValue:1234.
|
|
258 |
field acceptAction:[:value | Transcript showCr:value].
|
|
259 |
field crAction:[field accept. top destroy].
|
|
260 |
top open.
|
|
261 |
|
122
|
262 |
|
120
|
263 |
use a model:
|
|
264 |
(see changing model value in inspector when return is pressed in the field)
|
|
265 |
|
|
266 |
|top field model|
|
|
267 |
|
|
268 |
model := 'hello world' asValue.
|
|
269 |
|
|
270 |
top := StandardSystemView new.
|
|
271 |
top extent:200@100.
|
|
272 |
|
|
273 |
field := EditField origin:0.0@ViewSpacing in:top.
|
121
|
274 |
field width:1.0.
|
120
|
275 |
field leftInset:ViewSpacing;
|
|
276 |
rightInset:ViewSpacing.
|
|
277 |
field model:model.
|
|
278 |
|
|
279 |
top open.
|
121
|
280 |
model inspect.
|
|
281 |
|
122
|
282 |
|
121
|
283 |
two views on the same model:
|
|
284 |
|
|
285 |
|top1 top2 field1 field2 model|
|
|
286 |
|
|
287 |
model := 'hello world' asValue.
|
|
288 |
|
|
289 |
top1 := StandardSystemView new.
|
|
290 |
top1 extent:200@100.
|
|
291 |
field1 := EditField origin:0.0@ViewSpacing in:top1.
|
|
292 |
field1 width:1.0.
|
|
293 |
field1 leftInset:ViewSpacing;
|
|
294 |
rightInset:ViewSpacing.
|
|
295 |
field1 model:model.
|
|
296 |
top1 open.
|
|
297 |
|
|
298 |
top2 := StandardSystemView new.
|
|
299 |
top2 extent:200@100.
|
|
300 |
field2 := EditField origin:0.0@ViewSpacing in:top2.
|
|
301 |
field2 width:1.0.
|
|
302 |
field2 leftInset:ViewSpacing;
|
|
303 |
rightInset:ViewSpacing.
|
|
304 |
field2 model:model.
|
|
305 |
top2 open.
|
|
306 |
|
122
|
307 |
|
121
|
308 |
just an example; a checkBox and an editField on the same model:
|
|
309 |
|
|
310 |
|top1 top2 field1 box model|
|
|
311 |
|
|
312 |
model := false asValue.
|
|
313 |
|
|
314 |
top1 := StandardSystemView new.
|
|
315 |
top1 extent:200@100.
|
|
316 |
field1 := EditField origin:0.0@ViewSpacing in:top1.
|
|
317 |
field1 width:1.0.
|
|
318 |
field1 leftInset:ViewSpacing;
|
|
319 |
rightInset:ViewSpacing.
|
|
320 |
field1 converter:(PrintConverter new initForYesNo).
|
|
321 |
field1 model:model.
|
|
322 |
top1 open.
|
|
323 |
|
|
324 |
top2 := StandardSystemView new.
|
|
325 |
top2 extent:200@100.
|
|
326 |
box := CheckBox on:model.
|
|
327 |
box label:'on/off'.
|
|
328 |
top2 add:box.
|
|
329 |
top2 open.
|
|
330 |
|
120
|
331 |
model inspect.
|
|
332 |
"
|
19
|
333 |
! !
|
0
|
334 |
|
|
335 |
!EditField class methodsFor:'defaults'!
|
|
336 |
|
59
|
337 |
updateStyleCache
|
60
|
338 |
DefaultForegroundColor := StyleSheet colorAt:'editFieldForegroundColor' default:Black.
|
|
339 |
DefaultBackgroundColor := StyleSheet colorAt:'editFieldBackgroundColor' default:White.
|
|
340 |
DefaultSelectionForegroundColor := StyleSheet colorAt:'editFieldSelectionForegroundColor' default:DefaultBackgroundColor.
|
|
341 |
DefaultSelectionBackgroundColor := StyleSheet colorAt:'editFieldSelectionBackgroundColor' default:DefaultForegroundColor.
|
|
342 |
DefaultFont := StyleSheet fontAt:'editFieldFont' default:nil.
|
59
|
343 |
|
|
344 |
"
|
60
|
345 |
self updateStyleCache
|
59
|
346 |
"
|
105
|
347 |
!
|
|
348 |
|
121
|
349 |
defaultLeaveKeys
|
|
350 |
^ #(Return CursorUp CursorDown Next Previous Accept)
|
|
351 |
!
|
|
352 |
|
105
|
353 |
defaultNumberOfLines
|
|
354 |
"the number of lines in the field"
|
|
355 |
|
|
356 |
^ 1
|
0
|
357 |
! !
|
|
358 |
|
|
359 |
!EditField methodsFor:'initialization'!
|
|
360 |
|
|
361 |
initialize
|
|
362 |
super initialize.
|
|
363 |
self height:(font height + font descent + (topMargin * 2)).
|
|
364 |
enabled := true.
|
|
365 |
fixedSize := true.
|
|
366 |
nFullLinesShown := 1.
|
|
367 |
nLinesShown := 1.
|
121
|
368 |
alwaysAccept := false.
|
122
|
369 |
acceptOnLeave := false.
|
|
370 |
acceptOnReturn := true.
|
121
|
371 |
leaveKeys := self class defaultLeaveKeys.
|
|
372 |
cursorShown := true
|
0
|
373 |
!
|
|
374 |
|
|
375 |
initStyle
|
|
376 |
super initStyle.
|
|
377 |
|
59
|
378 |
DefaultBackgroundColor notNil ifTrue:[
|
|
379 |
bgColor := DefaultBackgroundColor on:device.
|
|
380 |
self viewBackground:bgColor.
|
0
|
381 |
].
|
59
|
382 |
fgColor := DefaultForegroundColor.
|
|
383 |
selectionFgColor := DefaultSelectionForegroundColor.
|
|
384 |
selectionBgColor := DefaultSelectionBackgroundColor.
|
0
|
385 |
|
59
|
386 |
DefaultFont notNil ifTrue:[
|
|
387 |
font := DefaultFont on:device
|
0
|
388 |
]
|
|
389 |
!
|
|
390 |
|
97
|
391 |
editMenu
|
|
392 |
|labels selectors m|
|
0
|
393 |
|
97
|
394 |
labels := #(
|
|
395 |
'copy'
|
|
396 |
'cut'
|
|
397 |
'paste'
|
19
|
398 |
"
|
97
|
399 |
'replace'
|
19
|
400 |
"
|
97
|
401 |
'-'
|
|
402 |
'accept'
|
|
403 |
).
|
0
|
404 |
|
97
|
405 |
selectors := #(
|
|
406 |
copySelection
|
|
407 |
cut
|
|
408 |
paste
|
|
409 |
"
|
|
410 |
replace
|
19
|
411 |
"
|
97
|
412 |
nil
|
|
413 |
accept
|
|
414 |
).
|
|
415 |
|
|
416 |
m := PopUpMenu
|
|
417 |
labels:(resources array:labels)
|
|
418 |
selectors:selectors.
|
|
419 |
|
|
420 |
self hasSelection ifFalse:[
|
|
421 |
m disable:#copySelection.
|
|
422 |
m disable:#cut
|
|
423 |
].
|
|
424 |
|
|
425 |
^ m
|
0
|
426 |
! !
|
|
427 |
|
|
428 |
!EditField methodsFor:'realization'!
|
|
429 |
|
|
430 |
realize
|
|
431 |
"scroll back to beginning when realized"
|
|
432 |
leftOffset := 0.
|
|
433 |
super realize
|
|
434 |
! !
|
|
435 |
|
|
436 |
!EditField methodsFor:'private'!
|
|
437 |
|
120
|
438 |
getListFromModel
|
|
439 |
"redefined to aquire the text via the aspectMsg - not the listMsg"
|
|
440 |
|
121
|
441 |
|savedCursorCol|
|
|
442 |
|
120
|
443 |
(model notNil and:[aspectMsg notNil]) ifTrue:[
|
121
|
444 |
"
|
|
445 |
kludge: editValue positions cursor to beginning
|
|
446 |
"
|
|
447 |
savedCursorCol := cursorCol.
|
|
448 |
self editValue:(model perform:aspectMsg).
|
|
449 |
savedCursorCol ~~ 1 ifTrue:[self cursorLine:1 col:savedCursorCol].
|
|
450 |
cursorVisibleLine := 1.
|
|
451 |
]
|
|
452 |
!
|
|
453 |
|
|
454 |
textChanged
|
|
455 |
super textChanged.
|
|
456 |
alwaysAccept ifTrue:[
|
|
457 |
self accept
|
120
|
458 |
]
|
|
459 |
!
|
|
460 |
|
59
|
461 |
startAutoScrollUp:y
|
|
462 |
"no vertical scrolling in editfields"
|
0
|
463 |
|
|
464 |
^ self
|
|
465 |
!
|
|
466 |
|
59
|
467 |
startAutoScrollDown:y
|
|
468 |
"no vertical scrolling in editfields"
|
0
|
469 |
|
|
470 |
^ self
|
|
471 |
! !
|
|
472 |
|
38
|
473 |
!EditField methodsFor:'queries'!
|
|
474 |
|
59
|
475 |
preferedExtent
|
|
476 |
"return the prefered extent of this view.
|
38
|
477 |
That is the width of the string plus some extra,
|
|
478 |
but not wider than half of the screen"
|
|
479 |
|
|
480 |
|string w|
|
|
481 |
|
|
482 |
string := self contents.
|
|
483 |
(string isNil or:[string isBlank]) ifTrue:[
|
59
|
484 |
string := ' ' "/ just any string is ok ^ super preferedExtent
|
38
|
485 |
].
|
|
486 |
w := (((font on:device) widthOf:string) * 1.5) rounded.
|
|
487 |
w := w min:(device width // 2).
|
|
488 |
^ w @ self height
|
|
489 |
! !
|
|
490 |
|
0
|
491 |
!EditField methodsFor:'accessing'!
|
|
492 |
|
38
|
493 |
list:someText
|
|
494 |
"redefined to force text to 1 line, and notify dependents
|
|
495 |
of any changed extent-wishes."
|
|
496 |
|
|
497 |
|l oldWidth|
|
|
498 |
|
|
499 |
l := someText.
|
|
500 |
l size > 1 ifTrue:[
|
59
|
501 |
l := OrderedCollection with:(l at:1)
|
38
|
502 |
].
|
|
503 |
oldWidth := self widthOfContents.
|
|
504 |
super list:l.
|
|
505 |
self widthOfContents ~~ oldWidth ifTrue:[
|
59
|
506 |
self changed:#preferedExtent
|
38
|
507 |
]
|
|
508 |
!
|
|
509 |
|
121
|
510 |
contents:someText
|
|
511 |
"set the contents from a string
|
122
|
512 |
- redefined to place the cursor to the end.
|
|
513 |
In your application, please use #editValue:;
|
|
514 |
it uses a converter (if any) and is compatible to ST-80."
|
121
|
515 |
|
|
516 |
super contents:someText.
|
|
517 |
self cursorCol:(someText size + 1).
|
|
518 |
!
|
|
519 |
|
0
|
520 |
contents
|
|
521 |
"return contents as a string
|
122
|
522 |
- redefined since EditFields hold only one line of text.
|
|
523 |
In your application, please use #editValue;
|
|
524 |
it uses a converter (if any) and is compatible to ST-80."
|
0
|
525 |
|
|
526 |
list isNil ifTrue:[^ ''].
|
|
527 |
(list size == 0) ifTrue:[^ ''].
|
|
528 |
^ list at:1
|
|
529 |
!
|
|
530 |
|
|
531 |
enable
|
|
532 |
"enable the field; show cursor and allow input"
|
|
533 |
|
|
534 |
enabled ifFalse:[
|
59
|
535 |
enableAction notNil ifTrue:[
|
|
536 |
enableAction value
|
|
537 |
].
|
|
538 |
enabled := true.
|
|
539 |
super showCursor
|
0
|
540 |
]
|
|
541 |
!
|
|
542 |
|
105
|
543 |
leaveAction:aBlock
|
|
544 |
"define an action to be evaluated when field is left by return key"
|
|
545 |
|
|
546 |
leaveAction := aBlock
|
|
547 |
!
|
|
548 |
|
122
|
549 |
leaveKeys:aCollectionOfKeySymbols
|
|
550 |
"define the set of keys which are interpreted as leaveKeys.
|
|
551 |
I.e. those that make the field inactive and accept (if acceptOnLeave is true).
|
|
552 |
The default is a set of #CursorUp, #CursorDown, #Next, #Prior and #Return."
|
|
553 |
|
|
554 |
leaveKeys := aCollectionOfKeySymbols
|
|
555 |
!
|
|
556 |
|
121
|
557 |
crAction:aBlock
|
|
558 |
"define an action to be evaluated when the return key is pressed."
|
|
559 |
|
|
560 |
crAction := aBlock
|
|
561 |
!
|
|
562 |
|
|
563 |
acceptAction:aBlock
|
|
564 |
"define an action to be evaluated when accepted."
|
|
565 |
|
|
566 |
acceptAction := aBlock
|
|
567 |
!
|
|
568 |
|
|
569 |
tabAction:aBlock
|
|
570 |
"define an action to be evaluated when the tabulator key is pressed."
|
|
571 |
|
|
572 |
tabAction := aBlock
|
|
573 |
!
|
|
574 |
|
|
575 |
alwaysAccept:aBoolean
|
122
|
576 |
"set/clear the alwaysAccept flag. The default is false."
|
121
|
577 |
|
|
578 |
alwaysAccept := aBoolean
|
|
579 |
!
|
|
580 |
|
122
|
581 |
acceptOnReturn:aBoolean
|
|
582 |
"set/clear the acceptOnReturn flag. The default is true."
|
|
583 |
|
|
584 |
acceptOnReturn := aBoolean
|
|
585 |
!
|
|
586 |
|
|
587 |
acceptOnLeave:aBoolean
|
|
588 |
"set/clear the acceptOnLeave flag. The default is false."
|
|
589 |
|
|
590 |
acceptOnLeave := aBoolean
|
|
591 |
!
|
|
592 |
|
121
|
593 |
converter
|
|
594 |
"return the converter (if any)."
|
|
595 |
|
|
596 |
^ converter
|
|
597 |
!
|
|
598 |
|
|
599 |
editValue
|
|
600 |
"if the field edits a string, this is a name alias for #contents.
|
|
601 |
Otherwise, if there is a converter, return the edited string
|
|
602 |
converted to an appropriate object."
|
|
603 |
|
|
604 |
|text|
|
|
605 |
|
|
606 |
text := self contents.
|
|
607 |
converter isNil ifTrue:[^ text].
|
|
608 |
^ converter readValueFrom:text withoutSpaces
|
|
609 |
!
|
|
610 |
|
|
611 |
editValue:aStringOrObject
|
|
612 |
"set the contents. If there is a converter, use it to convert
|
|
613 |
the object into a printed representation.
|
|
614 |
Otherwise, the argument is supposed to be a string like object,
|
|
615 |
and used directly (i.e. this is equivalent to sending #contents:)."
|
|
616 |
|
|
617 |
self editValue:aStringOrObject selected:false
|
|
618 |
!
|
|
619 |
|
|
620 |
editValue:aStringOrObject selected:aBoolean
|
|
621 |
"set the contents. If there is a converter, use it to convert
|
|
622 |
the object into a printed representation.
|
|
623 |
Otherwise, the argument is supposed to be a string like object,
|
|
624 |
and used directly (i.e. this is equivalent to sending #contents:)."
|
|
625 |
|
|
626 |
|text|
|
|
627 |
|
|
628 |
converter notNil ifTrue:[
|
|
629 |
text := converter printStringFor:aStringOrObject
|
|
630 |
] ifFalse:[
|
|
631 |
text := aStringOrObject.
|
|
632 |
].
|
|
633 |
self contents:text.
|
|
634 |
aBoolean ifTrue:[
|
|
635 |
self selectFromLine:1 col:1 toLine:1 col:text size
|
|
636 |
]
|
|
637 |
!
|
|
638 |
|
|
639 |
converter:aConverter
|
|
640 |
"set the converter. If non-nil,
|
|
641 |
the converter is applied to the text to convert from the string
|
|
642 |
representation to the actual object value and vice versa.
|
|
643 |
The default converter is nil, meaning no-conversion
|
|
644 |
(i.e. the edited object is the string itself."
|
|
645 |
|
|
646 |
converter := aConverter
|
|
647 |
!
|
|
648 |
|
|
649 |
|
0
|
650 |
disable
|
|
651 |
"disable the field; hide cursor and ignore input"
|
|
652 |
|
|
653 |
enabled ifTrue:[
|
59
|
654 |
enabled := false.
|
|
655 |
self hideCursor
|
0
|
656 |
]
|
|
657 |
!
|
|
658 |
|
|
659 |
enableAction:aBlock
|
|
660 |
"define an action to be evaluated when enabled by clicking upon"
|
|
661 |
|
|
662 |
enableAction := aBlock
|
|
663 |
!
|
|
664 |
|
62
|
665 |
initialText:aString selected:aBoolean
|
|
666 |
"set the initialText and select it if aBoolean is true"
|
|
667 |
|
|
668 |
|len s|
|
0
|
669 |
|
|
670 |
leftOffset := 0.
|
62
|
671 |
self contents:(s := aString asString).
|
|
672 |
aBoolean ifTrue:[
|
|
673 |
(len := s size) ~~ 0 ifTrue:[
|
|
674 |
self selectFromLine:1 col:1 toLine:1 col:len
|
|
675 |
]
|
7
|
676 |
]
|
62
|
677 |
!
|
|
678 |
|
|
679 |
initialText:aString
|
|
680 |
"set the initialText and select it"
|
|
681 |
|
|
682 |
self initialText:aString selected:true
|
0
|
683 |
! !
|
|
684 |
|
105
|
685 |
!EditField methodsFor:'editing'!
|
|
686 |
|
|
687 |
paste:someText
|
|
688 |
"redefined to force text to 1 line"
|
|
689 |
|
|
690 |
super paste:someText.
|
|
691 |
list size > 1 ifTrue:[
|
|
692 |
self deleteFromLine:2 toLine:(list size)
|
|
693 |
]
|
|
694 |
! !
|
|
695 |
|
0
|
696 |
!EditField methodsFor:'cursor drawing'!
|
|
697 |
|
|
698 |
showCursor
|
|
699 |
"make cursor visible if currently invisible - but only if this
|
|
700 |
EditField is enabled"
|
|
701 |
|
|
702 |
enabled ifTrue:[super showCursor]
|
|
703 |
! !
|
|
704 |
|
|
705 |
!EditField methodsFor:'cursor movement'!
|
|
706 |
|
|
707 |
cursorLine:line col:col
|
38
|
708 |
"catch cursor movement"
|
|
709 |
|
|
710 |
super cursorLine:1 col:col
|
0
|
711 |
!
|
|
712 |
|
|
713 |
cursorDown
|
|
714 |
"catch cursor movement"
|
|
715 |
|
|
716 |
(cursorVisibleLine == nLinesShown) ifFalse:[
|
59
|
717 |
super cursorDown
|
0
|
718 |
]
|
|
719 |
! !
|
|
720 |
|
70
|
721 |
!EditField methodsFor:'event handling'!
|
19
|
722 |
|
|
723 |
accept
|
|
724 |
"accept the fields contents - perform the leave action as if
|
|
725 |
return was pressed."
|
|
726 |
|
121
|
727 |
|value|
|
|
728 |
|
|
729 |
value := self editValue.
|
|
730 |
acceptAction notNil ifTrue:[
|
|
731 |
acceptAction value:value
|
19
|
732 |
].
|
121
|
733 |
|
|
734 |
"model-view behavior"
|
|
735 |
self sendChangeMessageWith:value.
|
19
|
736 |
!
|
0
|
737 |
|
|
738 |
buttonPress:button x:x y:y
|
|
739 |
"enable myself on mouse click"
|
|
740 |
|
|
741 |
enabled ifFalse:[
|
59
|
742 |
enabled := true.
|
|
743 |
super buttonPress:button x:x y:y.
|
|
744 |
enableAction notNil ifTrue:[
|
|
745 |
enableAction value
|
|
746 |
]
|
0
|
747 |
] ifTrue:[
|
59
|
748 |
super buttonPress:button x:x y:y
|
0
|
749 |
]
|
|
750 |
!
|
|
751 |
|
|
752 |
keyPress:key x:x y:y
|
|
753 |
"if keyHandler is defined, pass input; otherwise check for leave
|
|
754 |
keys"
|
|
755 |
|
38
|
756 |
|leave xCol newOffset oldWidth newWidth|
|
0
|
757 |
|
|
758 |
enabled ifFalse:[
|
110
|
759 |
^ self
|
0
|
760 |
].
|
|
761 |
|
|
762 |
(key == #DeleteLine) ifTrue:[
|
110
|
763 |
Smalltalk at:#CopyBuffer put:(self contents).
|
|
764 |
self contents:''. ^ self
|
0
|
765 |
].
|
|
766 |
|
121
|
767 |
(key == #Tab) ifTrue:[
|
|
768 |
tabAction notNil ifTrue:[tabAction value. ^ self].
|
|
769 |
].
|
|
770 |
(key == #Return) ifTrue:[
|
|
771 |
crAction notNil ifTrue:[crAction value. ^ self].
|
|
772 |
].
|
|
773 |
leave := leaveKeys includes:key.
|
|
774 |
leave ifTrue:[
|
|
775 |
leaveAction notNil ifTrue:[
|
|
776 |
leaveAction value:key
|
|
777 |
].
|
0
|
778 |
|
122
|
779 |
((key == #Return and:[acceptOnReturn])
|
|
780 |
or:[key ~~ #Return and:[acceptOnLeave]]) ifTrue:[
|
|
781 |
self accept.
|
|
782 |
].
|
|
783 |
|
113
|
784 |
x >= 0 ifTrue:[
|
|
785 |
"
|
|
786 |
let superview know about the leave ...
|
|
787 |
This is a temporary kludge for the tableWidget -
|
|
788 |
it is no clean coding style. Should make the tableWidget
|
|
789 |
a proper model and handle it via the changed mechanism ....
|
|
790 |
"
|
121
|
791 |
(superView notNil and:[superView canHandle:key from:self]) ifTrue:[
|
113
|
792 |
superView keyPress:key x:x y:y.
|
|
793 |
].
|
110
|
794 |
].
|
|
795 |
^ self
|
0
|
796 |
].
|
38
|
797 |
|
121
|
798 |
"
|
|
799 |
ignore some keys (if not a leaveKey) ...
|
|
800 |
"
|
|
801 |
(key == #Find) ifTrue:[^self].
|
|
802 |
(key == #FindNext) ifTrue:[^self].
|
|
803 |
(key == #FindPrev) ifTrue:[^self].
|
|
804 |
(key == #GotoLine) ifTrue:[^self].
|
|
805 |
|
|
806 |
"
|
|
807 |
a normal key - let superclass's method insert it
|
|
808 |
"
|
38
|
809 |
oldWidth := self widthOfContents.
|
0
|
810 |
super keyPress:key x:x y:y.
|
38
|
811 |
newWidth := self widthOfContents.
|
|
812 |
|
|
813 |
"
|
|
814 |
should (& can) we resize ?
|
|
815 |
"
|
59
|
816 |
xCol := (self xOfCol:cursorCol inVisibleLine:cursorLine) - leftOffset.
|
3
|
817 |
(xCol > (width * (5/6))) ifTrue:[
|
110
|
818 |
self changed:#preferedExtent
|
38
|
819 |
] ifFalse:[
|
110
|
820 |
newWidth < (width * (1/6)) ifTrue:[
|
|
821 |
self changed:#preferedExtent
|
|
822 |
]
|
38
|
823 |
].
|
|
824 |
|
|
825 |
"
|
|
826 |
did someone react ?
|
|
827 |
(if not, we scroll horizontally)
|
|
828 |
"
|
59
|
829 |
xCol := (self xOfCol:cursorCol inVisibleLine:cursorLine) - leftOffset.
|
38
|
830 |
(xCol > (width * (5/6))) ifTrue:[
|
110
|
831 |
newOffset := leftOffset + (width // 2).
|
38
|
832 |
] ifFalse:[
|
110
|
833 |
(xCol < (width * (1/6))) ifTrue:[
|
|
834 |
newOffset := 0 max: leftOffset - (width // 2).
|
|
835 |
] ifFalse:[
|
|
836 |
newOffset := leftOffset
|
|
837 |
]
|
38
|
838 |
].
|
|
839 |
newOffset ~~ leftOffset ifTrue:[
|
110
|
840 |
leftOffset := newOffset.
|
|
841 |
self clear.
|
|
842 |
self redraw
|
0
|
843 |
]
|
105
|
844 |
!
|
|
845 |
|
|
846 |
canHandle:aKey
|
|
847 |
"return true, if the receiver would like to handle aKey
|
|
848 |
(usually from another view, when the receiver is part of
|
|
849 |
a more complex dialog box).
|
|
850 |
We do return true here, since the editfield will handle
|
|
851 |
all keys."
|
|
852 |
|
|
853 |
^ true
|
0
|
854 |
! !
|