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 |
|
125
|
13 |
'From Smalltalk/X, Version:2.10.5 on 9-may-1995 at 12:09:38 pm'!
|
105
|
14 |
|
0
|
15 |
EditTextView subclass:#EditField
|
125
|
16 |
instanceVariableNames:'leaveAction enabled enableAction crAction tabAction converter
|
127
|
17 |
leaveKeys immediateAccept acceptOnLeave acceptOnReturn
|
|
18 |
lengthLimit'
|
105
|
19 |
classVariableNames:'DefaultForegroundColor DefaultBackgroundColor
|
127
|
20 |
DefaultSelectionForegroundColor DefaultSelectionBackgroundColor
|
|
21 |
DefaultFont'
|
105
|
22 |
poolDictionaries:''
|
|
23 |
category:'Views-Text'
|
0
|
24 |
!
|
|
25 |
|
|
26 |
EditField comment:'
|
5
|
27 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
59
|
28 |
All Rights Reserved
|
0
|
29 |
|
130
|
30 |
$Header: /cvs/stx/stx/libwidg/EditField.st,v 1.24 1995-06-06 04:13:12 claus Exp $
|
19
|
31 |
'!
|
|
32 |
|
|
33 |
!EditField class methodsFor:'documentation'!
|
|
34 |
|
38
|
35 |
copyright
|
|
36 |
"
|
|
37 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
59
|
38 |
All Rights Reserved
|
38
|
39 |
|
|
40 |
This software is furnished under a license and may be used
|
|
41 |
only in accordance with the terms of that license and with the
|
|
42 |
inclusion of the above copyright notice. This software may not
|
|
43 |
be provided or otherwise made available to, or used by, any
|
|
44 |
other person. No title to or ownership of the software is
|
|
45 |
hereby transferred.
|
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
version
|
|
50 |
"
|
130
|
51 |
$Header: /cvs/stx/stx/libwidg/EditField.st,v 1.24 1995-06-06 04:13:12 claus Exp $
|
38
|
52 |
"
|
|
53 |
!
|
|
54 |
|
19
|
55 |
documentation
|
|
56 |
"
|
38
|
57 |
an editable text-field. Realized by using an EditTextView,
|
|
58 |
and forcing its size to 1 line - disabling cursor movement
|
|
59 |
in the vertical direction.
|
121
|
60 |
|
|
61 |
Instance variables:
|
|
62 |
|
|
63 |
leaveAction <Block | nil> if non-nil, this is evaluated with
|
|
64 |
the key (#Return, #CursorUp etc.) when
|
|
65 |
the field is left via keyboard keys.
|
|
66 |
(fieldGroups use this to decide which
|
|
67 |
field has to be enabled next)
|
|
68 |
|
|
69 |
enabled <Boolean> if false, input is ignored.
|
|
70 |
|
125
|
71 |
enableAction <Block | nil> action performed if the field is
|
|
72 |
enabled via an explicit click.
|
|
73 |
(this is used by the group to
|
|
74 |
set the active field to the clicked upon field)
|
121
|
75 |
|
|
76 |
crAction <Block | nil> if non-nil, keyboard input of a cr are not
|
|
77 |
handled specially, instead this block is evaluated
|
|
78 |
(however, this block can perform additional checks and send
|
|
79 |
a #accept then)
|
|
80 |
|
|
81 |
tabAction <Block | nil> if non-nil, keyboard input of a tab character
|
|
82 |
is not entered into the text, instead this block
|
|
83 |
is evaluated.
|
|
84 |
|
|
85 |
converter <PrintConverter | nil> if non-nil, this is supposed to convert between
|
|
86 |
the object and its printed representation.
|
|
87 |
Defaults to nil i.e. assume that strings are edited.
|
|
88 |
|
|
89 |
leaveKeys <Collection> keys which are interpreted as 'leving the field'
|
122
|
90 |
|
125
|
91 |
immediateAccept <Boolean> if true, every change of the text is immediately
|
|
92 |
forwarded to the model/acceptBlock. If false,
|
|
93 |
the changed value is only stored in the model
|
|
94 |
if the field is left or accepted.
|
|
95 |
Default is false.
|
122
|
96 |
|
|
97 |
acceptOnLeave <Boolean> if true, leaving the field (via cursor keys)
|
|
98 |
automatically accepts the value into the model.
|
130
|
99 |
Default is true.
|
122
|
100 |
|
|
101 |
acceptOnReturn <Boolean> if true, leaving the field via return
|
|
102 |
automatically accepts the value into the model.
|
130
|
103 |
Default is true.
|
19
|
104 |
"
|
120
|
105 |
!
|
|
106 |
|
|
107 |
examples
|
|
108 |
"
|
122
|
109 |
see more examples in EnterFieldGroup>>examples.
|
|
110 |
|
|
111 |
|
|
112 |
basic field in a view:
|
120
|
113 |
|
|
114 |
|top field|
|
|
115 |
|
|
116 |
top := StandardSystemView new.
|
|
117 |
top extent:200@100.
|
|
118 |
|
|
119 |
field := EditField origin:0.0@0.0 in:top.
|
|
120 |
field width:1.0. 'let its height as-is'.
|
|
121 |
|
|
122 |
top open
|
|
123 |
|
122
|
124 |
|
125
|
125 |
forward input in topView to the field:
|
|
126 |
(currently, the field does not know this - therefore,
|
|
127 |
its been told here ... this may change)
|
122
|
128 |
|
|
129 |
|top field|
|
|
130 |
|
|
131 |
top := StandardSystemView new.
|
|
132 |
top extent:200@100.
|
|
133 |
|
|
134 |
field := EditField origin:0.0@0.0 in:top.
|
|
135 |
field width:1.0. 'let its height as-is'.
|
|
136 |
|
|
137 |
top delegate:(KeyboardForwarder toView:field).
|
125
|
138 |
field hasKeyboardFocus:true.
|
122
|
139 |
top open
|
|
140 |
|
|
141 |
|
125
|
142 |
to make it look better: set some inset:
|
120
|
143 |
|
|
144 |
|top field|
|
|
145 |
|
|
146 |
top := StandardSystemView new.
|
|
147 |
top extent:200@100.
|
|
148 |
|
|
149 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
150 |
field width:1.0. 'let its height as-is'.
|
|
151 |
field leftInset:ViewSpacing;
|
|
152 |
rightInset:ViewSpacing.
|
|
153 |
|
|
154 |
top open
|
|
155 |
|
122
|
156 |
|
120
|
157 |
give it an initial contents:
|
|
158 |
|
|
159 |
|top field|
|
|
160 |
|
|
161 |
top := StandardSystemView new.
|
|
162 |
top extent:200@100.
|
|
163 |
|
|
164 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
165 |
field width:1.0.
|
|
166 |
field leftInset:ViewSpacing;
|
|
167 |
rightInset:ViewSpacing.
|
121
|
168 |
field editValue:'hello world'.
|
120
|
169 |
|
|
170 |
top open
|
|
171 |
|
122
|
172 |
|
125
|
173 |
have it preselected:
|
120
|
174 |
|
|
175 |
|top field|
|
|
176 |
|
|
177 |
top := StandardSystemView new.
|
|
178 |
top extent:200@100.
|
|
179 |
|
|
180 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
181 |
field width:1.0.
|
|
182 |
field leftInset:ViewSpacing;
|
|
183 |
rightInset:ViewSpacing.
|
121
|
184 |
field editValue:'hello world' selected:true.
|
120
|
185 |
|
|
186 |
top open
|
|
187 |
|
122
|
188 |
|
120
|
189 |
have part of it preselected:
|
|
190 |
|
|
191 |
|top field|
|
|
192 |
|
|
193 |
top := StandardSystemView new.
|
|
194 |
top extent:200@100.
|
|
195 |
|
|
196 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
197 |
field width:1.0.
|
|
198 |
field leftInset:ViewSpacing;
|
|
199 |
rightInset:ViewSpacing.
|
121
|
200 |
field editValue:'hello world';
|
120
|
201 |
selectFromCharacterPosition:1 to:5.
|
|
202 |
|
|
203 |
top open
|
|
204 |
|
122
|
205 |
|
125
|
206 |
set a size limit:
|
|
207 |
|
|
208 |
|top field|
|
|
209 |
|
|
210 |
top := StandardSystemView new.
|
|
211 |
top extent:200@100.
|
|
212 |
|
|
213 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
214 |
field width:1.0.
|
|
215 |
field leftInset:ViewSpacing;
|
|
216 |
rightInset:ViewSpacing.
|
|
217 |
field editValue:'hello';
|
|
218 |
maxChars:8.
|
|
219 |
|
|
220 |
top open
|
|
221 |
|
|
222 |
|
121
|
223 |
use a converter:
|
125
|
224 |
- numbers (default to 0):
|
121
|
225 |
|
|
226 |
|top field|
|
|
227 |
|
|
228 |
top := StandardSystemView new.
|
|
229 |
top extent:200@100.
|
|
230 |
|
|
231 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
232 |
field width:1.0.
|
|
233 |
field leftInset:ViewSpacing;
|
|
234 |
rightInset:ViewSpacing.
|
|
235 |
|
|
236 |
field converter:(PrintConverter new initForNumber).
|
|
237 |
field editValue:1234.
|
|
238 |
field acceptAction:[:value | Transcript showCr:value].
|
|
239 |
field crAction:[field accept. top destroy].
|
|
240 |
top open.
|
|
241 |
|
|
242 |
- dates:
|
|
243 |
|
|
244 |
|top field|
|
|
245 |
|
|
246 |
top := StandardSystemView new.
|
|
247 |
top extent:200@100.
|
|
248 |
|
|
249 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
250 |
field width:1.0.
|
|
251 |
field leftInset:ViewSpacing;
|
|
252 |
rightInset:ViewSpacing.
|
|
253 |
|
|
254 |
field converter:(PrintConverter new initForDate).
|
|
255 |
field editValue:Date today.
|
125
|
256 |
field acceptAction:[:value | Transcript showCr:value class name , ' ' , value printString].
|
121
|
257 |
field crAction:[field accept. top destroy].
|
|
258 |
top open.
|
|
259 |
|
122
|
260 |
|
125
|
261 |
setting immediateAccept, makes the field update with every key:
|
|
262 |
|
|
263 |
- immediate accept numbers, defaulting to nil:
|
121
|
264 |
|
|
265 |
|top field|
|
|
266 |
|
|
267 |
top := StandardSystemView new.
|
|
268 |
top extent:200@100.
|
|
269 |
|
|
270 |
field := EditField origin:0.0@ViewSpacing in:top.
|
|
271 |
field width:1.0.
|
|
272 |
field leftInset:ViewSpacing;
|
|
273 |
rightInset:ViewSpacing.
|
|
274 |
|
125
|
275 |
field converter:(PrintConverter new initForNumberOrNil).
|
|
276 |
field immediateAccept:true.
|
121
|
277 |
field editValue:1234.
|
|
278 |
field acceptAction:[:value | Transcript showCr:value].
|
|
279 |
field crAction:[field accept. top destroy].
|
|
280 |
top open.
|
|
281 |
|
122
|
282 |
|
127
|
283 |
|
|
284 |
|
120
|
285 |
use a model:
|
|
286 |
(see changing model value in inspector when return is pressed in the field)
|
|
287 |
|
|
288 |
|top field model|
|
|
289 |
|
|
290 |
model := 'hello world' asValue.
|
|
291 |
|
|
292 |
top := StandardSystemView new.
|
|
293 |
top extent:200@100.
|
|
294 |
|
|
295 |
field := EditField origin:0.0@ViewSpacing in:top.
|
121
|
296 |
field width:1.0.
|
120
|
297 |
field leftInset:ViewSpacing;
|
|
298 |
rightInset:ViewSpacing.
|
|
299 |
field model:model.
|
128
|
300 |
field acceptOnReturn:true.
|
120
|
301 |
|
|
302 |
top open.
|
121
|
303 |
model inspect.
|
|
304 |
|
122
|
305 |
|
127
|
306 |
two views on the same model (each accepts on return):
|
121
|
307 |
|
|
308 |
|top1 top2 field1 field2 model|
|
|
309 |
|
|
310 |
model := 'hello world' asValue.
|
|
311 |
|
|
312 |
top1 := StandardSystemView new.
|
|
313 |
top1 extent:200@100.
|
|
314 |
field1 := EditField origin:0.0@ViewSpacing in:top1.
|
|
315 |
field1 width:1.0.
|
|
316 |
field1 leftInset:ViewSpacing;
|
|
317 |
rightInset:ViewSpacing.
|
|
318 |
field1 model:model.
|
128
|
319 |
field1 acceptOnReturn:true.
|
121
|
320 |
top1 open.
|
|
321 |
|
|
322 |
top2 := StandardSystemView new.
|
|
323 |
top2 extent:200@100.
|
|
324 |
field2 := EditField origin:0.0@ViewSpacing in:top2.
|
|
325 |
field2 width:1.0.
|
|
326 |
field2 leftInset:ViewSpacing;
|
|
327 |
rightInset:ViewSpacing.
|
|
328 |
field2 model:model.
|
128
|
329 |
field2 acceptOnReturn:true.
|
121
|
330 |
top2 open.
|
|
331 |
|
127
|
332 |
two views on the same model (no accept on return):
|
|
333 |
|
|
334 |
|top1 top2 field1 field2 model|
|
|
335 |
|
|
336 |
model := 'hello world' asValue.
|
|
337 |
|
|
338 |
top1 := StandardSystemView new.
|
|
339 |
top1 extent:200@100.
|
|
340 |
field1 := EditField origin:0.0@ViewSpacing in:top1.
|
|
341 |
field1 width:1.0.
|
|
342 |
field1 leftInset:ViewSpacing;
|
|
343 |
rightInset:ViewSpacing.
|
|
344 |
field1 model:model; acceptOnReturn:false.
|
|
345 |
top1 open.
|
|
346 |
|
|
347 |
top2 := StandardSystemView new.
|
|
348 |
top2 extent:200@100.
|
|
349 |
field2 := EditField origin:0.0@ViewSpacing in:top2.
|
|
350 |
field2 width:1.0.
|
|
351 |
field2 leftInset:ViewSpacing;
|
|
352 |
rightInset:ViewSpacing.
|
|
353 |
field2 model:model; acceptOnReturn:false.
|
|
354 |
top2 open.
|
|
355 |
|
125
|
356 |
with immediate accept:
|
|
357 |
|
|
358 |
|top1 top2 field1 field2 model|
|
|
359 |
|
|
360 |
model := 'hello world' asValue.
|
|
361 |
|
|
362 |
top1 := StandardSystemView new.
|
|
363 |
top1 extent:200@100.
|
|
364 |
field1 := EditField origin:0.0@ViewSpacing in:top1.
|
|
365 |
field1 width:1.0.
|
|
366 |
field1 leftInset:ViewSpacing; rightInset:ViewSpacing.
|
|
367 |
field1 model:model; immediateAccept:true.
|
|
368 |
top1 open.
|
|
369 |
|
|
370 |
top2 := StandardSystemView new.
|
|
371 |
top2 extent:200@100.
|
|
372 |
field2 := EditField origin:0.0@ViewSpacing in:top2.
|
|
373 |
field2 width:1.0.
|
|
374 |
field2 leftInset:ViewSpacing; rightInset:ViewSpacing.
|
|
375 |
field2 model:model; immediateAccept:true.
|
|
376 |
top2 open.
|
122
|
377 |
|
121
|
378 |
just an example; a checkBox and an editField on the same model:
|
|
379 |
|
|
380 |
|top1 top2 field1 box model|
|
|
381 |
|
|
382 |
model := false asValue.
|
|
383 |
|
|
384 |
top1 := StandardSystemView new.
|
|
385 |
top1 extent:200@100.
|
|
386 |
field1 := EditField origin:0.0@ViewSpacing in:top1.
|
|
387 |
field1 width:1.0.
|
|
388 |
field1 leftInset:ViewSpacing;
|
|
389 |
rightInset:ViewSpacing.
|
|
390 |
field1 converter:(PrintConverter new initForYesNo).
|
|
391 |
field1 model:model.
|
|
392 |
top1 open.
|
|
393 |
|
|
394 |
top2 := StandardSystemView new.
|
|
395 |
top2 extent:200@100.
|
|
396 |
box := CheckBox on:model.
|
|
397 |
box label:'on/off'.
|
|
398 |
top2 add:box.
|
|
399 |
top2 open.
|
|
400 |
|
120
|
401 |
model inspect.
|
125
|
402 |
|
|
403 |
|
|
404 |
connecting fields:
|
|
405 |
update field2 wehenever field1 is changed.
|
|
406 |
(normally, the processing below (xChanged) is done in your application
|
|
407 |
class, or in a complex model. For the demonstration below, we use
|
|
408 |
a Plug to simulate the protocol.)
|
|
409 |
|
|
410 |
|application top field1 field2 value1 value2|
|
|
411 |
|
|
412 |
application := Plug new.
|
|
413 |
application respondTo:#value1Changed
|
|
414 |
with:[value2 value:(value1 value isNil ifTrue:[nil]
|
|
415 |
ifFalse:[value1 value squared])].
|
|
416 |
|
|
417 |
value1 := 1 asValue.
|
|
418 |
value2 := 1 asValue.
|
|
419 |
|
|
420 |
top := Dialog new.
|
|
421 |
top extent:200@200.
|
|
422 |
|
|
423 |
(top addTextLabel:'some number:') layout:#left.
|
|
424 |
top addVerticalSpace.
|
|
425 |
|
|
426 |
(top addInputFieldOn:value1 tabable:false)
|
|
427 |
converter:(PrintConverter new initForNumberOrNil);
|
|
428 |
immediateAccept:true.
|
|
429 |
top addVerticalSpace.
|
|
430 |
|
|
431 |
(top addTextLabel:'squared:') layout:#left.
|
|
432 |
top addVerticalSpace.
|
|
433 |
(top addInputFieldOn:value2 tabable:false)
|
|
434 |
converter:(PrintConverter new initForNumberOrNil).
|
|
435 |
|
|
436 |
value1 onChangeSend:#value1Changed to:application.
|
|
437 |
|
|
438 |
top openModeless.
|
|
439 |
|
|
440 |
|
|
441 |
two-way connect:
|
|
442 |
each field updates the other (notice, that we have to turn off
|
|
443 |
onChange: notification, to avoid an endless notification cycle)
|
|
444 |
|
|
445 |
|application top field1 field2 value1 value2|
|
|
446 |
|
|
447 |
application := Plug new.
|
|
448 |
application respondTo:#value1Changed
|
|
449 |
with:[value2 retractInterrestFor:application.
|
|
450 |
value2 value:(value1 value isNil ifTrue:[nil]
|
|
451 |
ifFalse:[value1 value squared]).
|
|
452 |
value2 onChangeSend:#value2Changed to:application.
|
|
453 |
].
|
|
454 |
application respondTo:#value2Changed
|
|
455 |
with:[value1 retractInterrestFor:application.
|
|
456 |
value1 value:(value2 value isNil ifTrue:[nil]
|
|
457 |
ifFalse:[value2 value sqrt]).
|
|
458 |
value1 onChangeSend:#value1Changed to:application.
|
|
459 |
].
|
|
460 |
|
|
461 |
value1 := 1 asValue.
|
|
462 |
value2 := 1 asValue.
|
|
463 |
|
|
464 |
top := Dialog new.
|
|
465 |
top extent:200@200.
|
|
466 |
|
|
467 |
(top addTextLabel:'some number:') layout:#left.
|
|
468 |
top addVerticalSpace.
|
|
469 |
|
|
470 |
(top addInputFieldOn:value1 tabable:false)
|
|
471 |
converter:(PrintConverter new initForNumberOrNil);
|
|
472 |
immediateAccept:true.
|
|
473 |
top addVerticalSpace.
|
|
474 |
|
|
475 |
(top addTextLabel:'squared:') layout:#left.
|
|
476 |
top addVerticalSpace.
|
|
477 |
(top addInputFieldOn:value2 tabable:false)
|
127
|
478 |
converter:(PrintConverter new initForNumberOrNil);
|
|
479 |
immediateAccept:true.
|
125
|
480 |
|
|
481 |
value1 onChangeSend:#value1Changed to:application.
|
|
482 |
value2 onChangeSend:#value2Changed to:application.
|
|
483 |
|
|
484 |
top openModeless.
|
120
|
485 |
"
|
19
|
486 |
! !
|
0
|
487 |
|
|
488 |
!EditField class methodsFor:'defaults'!
|
|
489 |
|
125
|
490 |
defaultLeaveKeys
|
128
|
491 |
"return the set of keys which are taken as leave-keys.
|
|
492 |
If the field is in an enterFieldGroup, all leave keys will be
|
|
493 |
forwarded to the group and possible step to the next/previous field.
|
|
494 |
Also, if acceptOnLeave is true, leave keys will store the current
|
|
495 |
value into their model (if any)"
|
|
496 |
|
125
|
497 |
^ #(Return CursorUp CursorDown Next Previous Accept)
|
|
498 |
!
|
|
499 |
|
59
|
500 |
updateStyleCache
|
60
|
501 |
DefaultForegroundColor := StyleSheet colorAt:'editFieldForegroundColor' default:Black.
|
|
502 |
DefaultBackgroundColor := StyleSheet colorAt:'editFieldBackgroundColor' default:White.
|
|
503 |
DefaultSelectionForegroundColor := StyleSheet colorAt:'editFieldSelectionForegroundColor' default:DefaultBackgroundColor.
|
|
504 |
DefaultSelectionBackgroundColor := StyleSheet colorAt:'editFieldSelectionBackgroundColor' default:DefaultForegroundColor.
|
|
505 |
DefaultFont := StyleSheet fontAt:'editFieldFont' default:nil.
|
59
|
506 |
|
|
507 |
"
|
60
|
508 |
self updateStyleCache
|
59
|
509 |
"
|
105
|
510 |
!
|
|
511 |
|
|
512 |
defaultNumberOfLines
|
|
513 |
"the number of lines in the field"
|
|
514 |
|
|
515 |
^ 1
|
0
|
516 |
! !
|
|
517 |
|
125
|
518 |
!EditField methodsFor:'private'!
|
|
519 |
|
|
520 |
textChanged
|
|
521 |
"this is sent by mySelf (somewhere in a superclass) whenever
|
|
522 |
my contents has changed.
|
|
523 |
A good place to add immediateAccept functionality and check for the
|
|
524 |
lengthLimit."
|
|
525 |
|
|
526 |
|string|
|
|
527 |
|
|
528 |
super textChanged.
|
|
529 |
string := self contents.
|
|
530 |
lengthLimit notNil ifTrue:[
|
|
531 |
string size > lengthLimit ifTrue:[
|
|
532 |
self contents:(string := string copyTo:lengthLimit).
|
|
533 |
self flash.
|
|
534 |
]
|
|
535 |
].
|
|
536 |
immediateAccept ifTrue:[
|
|
537 |
self accept
|
|
538 |
]
|
|
539 |
!
|
|
540 |
|
|
541 |
getListFromModel
|
|
542 |
"redefined to aquire the text via the aspectMsg - not the listMsg,
|
|
543 |
and to ignore updates resulting from my own change."
|
|
544 |
|
|
545 |
"
|
|
546 |
ignore updates from my own change
|
|
547 |
"
|
|
548 |
lockUpdates ifTrue:[
|
|
549 |
lockUpdates := false.
|
|
550 |
^ self
|
|
551 |
].
|
|
552 |
|
|
553 |
(model notNil and:[aspectMsg notNil]) ifTrue:[
|
|
554 |
self editValue:(model perform:aspectMsg).
|
|
555 |
]
|
|
556 |
!
|
|
557 |
|
127
|
558 |
argForChangeMessage
|
|
559 |
"redefined to send use converted value (if I have one)"
|
|
560 |
|
|
561 |
^ self editValue
|
|
562 |
!
|
|
563 |
|
125
|
564 |
startAutoScrollUp:y
|
|
565 |
"no vertical scrolling in editfields"
|
|
566 |
|
|
567 |
^ self
|
|
568 |
!
|
|
569 |
|
|
570 |
startAutoScrollDown:y
|
|
571 |
"no vertical scrolling in editfields"
|
|
572 |
|
|
573 |
^ self
|
|
574 |
! !
|
|
575 |
|
|
576 |
!EditField methodsFor:'accessing'!
|
|
577 |
|
|
578 |
contents
|
|
579 |
"return contents as a string
|
|
580 |
- redefined since EditFields hold only one line of text.
|
|
581 |
In your application, please use #editValue;
|
|
582 |
it uses a converter (if any) and is compatible to ST-80."
|
|
583 |
|
|
584 |
list isNil ifTrue:[^ ''].
|
|
585 |
(list size == 0) ifTrue:[^ ''].
|
|
586 |
^ list at:1
|
|
587 |
!
|
|
588 |
|
|
589 |
leaveAction:aBlock
|
|
590 |
"define an action to be evaluated when field is left by return key"
|
|
591 |
|
|
592 |
leaveAction := aBlock
|
|
593 |
!
|
|
594 |
|
|
595 |
list:someText
|
|
596 |
"redefined to force text to 1 line, and notify dependents
|
|
597 |
of any changed extent-wishes."
|
|
598 |
|
|
599 |
|l oldWidth|
|
|
600 |
|
|
601 |
l := someText.
|
|
602 |
l size > 1 ifTrue:[
|
|
603 |
l := OrderedCollection with:(l at:1)
|
|
604 |
].
|
|
605 |
oldWidth := self widthOfContents.
|
|
606 |
super list:l.
|
|
607 |
self widthOfContents ~~ oldWidth ifTrue:[
|
130
|
608 |
self changed:#preferredExtent
|
125
|
609 |
]
|
|
610 |
!
|
|
611 |
|
|
612 |
contents:someText
|
|
613 |
"set the contents from a string
|
|
614 |
- redefined to place the cursor to the end.
|
|
615 |
In your application, please use #editValue:;
|
|
616 |
it uses a converter (if any) and is compatible to ST-80."
|
|
617 |
|
|
618 |
super contents:someText.
|
|
619 |
self cursorCol:(someText size + 1).
|
|
620 |
!
|
|
621 |
|
|
622 |
enable
|
|
623 |
"enable the field; show cursor and allow input"
|
|
624 |
|
|
625 |
enabled ifFalse:[
|
|
626 |
"/ enableAction notNil ifTrue:[
|
|
627 |
"/ enableAction value
|
|
628 |
"/ ].
|
|
629 |
enabled := true.
|
|
630 |
super showCursor
|
|
631 |
]
|
|
632 |
!
|
|
633 |
|
|
634 |
immediateAccept:aBoolean
|
|
635 |
"set/clear the immediateAccept flag. The default is false."
|
|
636 |
|
|
637 |
immediateAccept := aBoolean
|
|
638 |
!
|
|
639 |
|
|
640 |
editValue
|
|
641 |
"if the field edits a string, this is a name alias for #contents.
|
|
642 |
Otherwise, if there is a converter, return the edited string
|
|
643 |
converted to an appropriate object."
|
|
644 |
|
|
645 |
|string|
|
|
646 |
|
|
647 |
string := self contents.
|
|
648 |
converter isNil ifTrue:[^ string].
|
|
649 |
string isNil ifTrue:[string := ''].
|
|
650 |
^ converter readValueFrom:string
|
|
651 |
!
|
0
|
652 |
|
125
|
653 |
leaveKeys:aCollectionOfKeySymbols
|
|
654 |
"define the set of keys which are interpreted as leaveKeys.
|
|
655 |
I.e. those that make the field inactive and accept (if acceptOnLeave is true).
|
|
656 |
The default is a set of #CursorUp, #CursorDown, #Next, #Prior and #Return."
|
|
657 |
|
|
658 |
leaveKeys := aCollectionOfKeySymbols
|
|
659 |
!
|
|
660 |
|
|
661 |
initialText:aString selected:aBoolean
|
|
662 |
"set the initialText and select it if aBoolean is true"
|
|
663 |
|
|
664 |
|len s|
|
|
665 |
|
|
666 |
leftOffset := 0.
|
|
667 |
aString isNil ifTrue:[
|
|
668 |
s := nil
|
|
669 |
] ifFalse:[
|
|
670 |
s := aString asString
|
|
671 |
].
|
|
672 |
self contents:s.
|
|
673 |
aBoolean ifTrue:[
|
|
674 |
(len := s size) ~~ 0 ifTrue:[
|
|
675 |
self selectFromLine:1 col:1 toLine:1 col:len
|
|
676 |
]
|
|
677 |
]
|
|
678 |
!
|
|
679 |
|
|
680 |
crAction:aBlock
|
|
681 |
"define an action to be evaluated when the return key is pressed."
|
|
682 |
|
|
683 |
crAction := aBlock
|
|
684 |
!
|
|
685 |
|
|
686 |
tabAction:aBlock
|
|
687 |
"define an action to be evaluated when the tabulator key is pressed."
|
|
688 |
|
|
689 |
tabAction := aBlock
|
|
690 |
!
|
|
691 |
|
|
692 |
acceptOnReturn:aBoolean
|
128
|
693 |
"set/clear the acceptOnReturn flag. The default is false."
|
125
|
694 |
|
|
695 |
acceptOnReturn := aBoolean
|
|
696 |
!
|
|
697 |
|
|
698 |
initialText:aString
|
|
699 |
"set the initialText and select it"
|
|
700 |
|
|
701 |
self initialText:aString selected:true
|
|
702 |
!
|
|
703 |
|
|
704 |
editValue:aStringOrObject
|
|
705 |
"set the contents. If there is a converter, use it to convert
|
|
706 |
the object into a printed representation.
|
|
707 |
Otherwise, the argument is supposed to be a string like object,
|
|
708 |
and used directly (i.e. this is equivalent to sending #contents:)."
|
|
709 |
|
|
710 |
self editValue:aStringOrObject selected:false
|
0
|
711 |
!
|
|
712 |
|
125
|
713 |
disable
|
|
714 |
"disable the field; hide cursor and ignore input"
|
|
715 |
|
|
716 |
enabled ifTrue:[
|
|
717 |
enabled := false.
|
|
718 |
self hideCursor
|
|
719 |
]
|
|
720 |
!
|
|
721 |
|
|
722 |
editValue:aStringOrObject selected:aBoolean
|
|
723 |
"set the contents. If there is a converter, use it to convert
|
|
724 |
the object into a printed representation.
|
|
725 |
Otherwise, the argument is supposed to be a string like object,
|
|
726 |
and used directly (i.e. this is equivalent to sending #contents:)."
|
|
727 |
|
|
728 |
|string|
|
|
729 |
|
|
730 |
converter notNil ifTrue:[
|
|
731 |
string := converter printStringFor:aStringOrObject
|
|
732 |
] ifFalse:[
|
|
733 |
string := aStringOrObject.
|
|
734 |
].
|
|
735 |
self contents:string.
|
|
736 |
aBoolean ifTrue:[
|
|
737 |
self selectFromLine:1 col:1 toLine:1 col:string size
|
|
738 |
]
|
|
739 |
!
|
|
740 |
|
|
741 |
acceptOnLeave:aBoolean
|
|
742 |
"set/clear the acceptOnLeave flag. The default is false."
|
|
743 |
|
|
744 |
acceptOnLeave := aBoolean
|
|
745 |
!
|
|
746 |
|
|
747 |
converter:aConverter
|
|
748 |
"set the converter. If non-nil,
|
|
749 |
the converter is applied to the text to convert from the string
|
|
750 |
representation to the actual object value and vice versa.
|
|
751 |
The default converter is nil, meaning no-conversion
|
|
752 |
(i.e. the edited object is the string itself."
|
|
753 |
|
|
754 |
converter := aConverter
|
|
755 |
!
|
|
756 |
|
|
757 |
enableAction:aBlock
|
|
758 |
"define an action to be evaluated when enabled by clicking upon"
|
|
759 |
|
|
760 |
enableAction := aBlock
|
|
761 |
!
|
|
762 |
|
|
763 |
converter
|
|
764 |
"return the converter (if any)."
|
|
765 |
|
|
766 |
^ converter
|
|
767 |
!
|
|
768 |
|
|
769 |
maxChars:aNumberOrNil
|
|
770 |
"set the maximum number of characters that are allowed in
|
|
771 |
the field. Additional input will be ignored by the field.
|
|
772 |
A limit of nil means: unlimited. This is the default.
|
|
773 |
This method has been renamed from #lengthLimit: for ST-80
|
|
774 |
compatibility."
|
|
775 |
|
|
776 |
lengthLimit := aNumberOrNil
|
|
777 |
!
|
|
778 |
|
|
779 |
stringValue
|
|
780 |
"alias for #contents - for ST-80 compatibility"
|
|
781 |
|
|
782 |
^ self contents
|
|
783 |
! !
|
|
784 |
|
|
785 |
!EditField methodsFor:'initialization'!
|
|
786 |
|
0
|
787 |
initStyle
|
|
788 |
super initStyle.
|
|
789 |
|
59
|
790 |
DefaultBackgroundColor notNil ifTrue:[
|
|
791 |
bgColor := DefaultBackgroundColor on:device.
|
|
792 |
self viewBackground:bgColor.
|
0
|
793 |
].
|
59
|
794 |
fgColor := DefaultForegroundColor.
|
|
795 |
selectionFgColor := DefaultSelectionForegroundColor.
|
|
796 |
selectionBgColor := DefaultSelectionBackgroundColor.
|
0
|
797 |
|
59
|
798 |
DefaultFont notNil ifTrue:[
|
|
799 |
font := DefaultFont on:device
|
0
|
800 |
]
|
|
801 |
!
|
|
802 |
|
125
|
803 |
initialize
|
|
804 |
super initialize.
|
|
805 |
self height:(font height + font descent + (topMargin * 2)).
|
|
806 |
enabled := true.
|
|
807 |
fixedSize := true.
|
|
808 |
nFullLinesShown := 1.
|
|
809 |
nLinesShown := 1.
|
130
|
810 |
immediateAccept := false.
|
|
811 |
"/ acceptOnLeave := false.
|
|
812 |
"/ acceptOnReturn := false.
|
|
813 |
acceptOnLeave := acceptOnReturn := true.
|
125
|
814 |
cursorShown := true.
|
|
815 |
leaveKeys := self class defaultLeaveKeys.
|
|
816 |
!
|
|
817 |
|
97
|
818 |
editMenu
|
|
819 |
|labels selectors m|
|
0
|
820 |
|
97
|
821 |
labels := #(
|
|
822 |
'copy'
|
|
823 |
'cut'
|
|
824 |
'paste'
|
19
|
825 |
"
|
97
|
826 |
'replace'
|
19
|
827 |
"
|
97
|
828 |
'-'
|
|
829 |
'accept'
|
|
830 |
).
|
0
|
831 |
|
97
|
832 |
selectors := #(
|
|
833 |
copySelection
|
|
834 |
cut
|
|
835 |
paste
|
|
836 |
"
|
|
837 |
replace
|
19
|
838 |
"
|
97
|
839 |
nil
|
|
840 |
accept
|
|
841 |
).
|
|
842 |
|
|
843 |
m := PopUpMenu
|
|
844 |
labels:(resources array:labels)
|
|
845 |
selectors:selectors.
|
|
846 |
|
|
847 |
self hasSelection ifFalse:[
|
|
848 |
m disable:#copySelection.
|
|
849 |
m disable:#cut
|
|
850 |
].
|
|
851 |
|
|
852 |
^ m
|
0
|
853 |
! !
|
|
854 |
|
|
855 |
!EditField methodsFor:'realization'!
|
|
856 |
|
|
857 |
realize
|
|
858 |
"scroll back to beginning when realized"
|
|
859 |
leftOffset := 0.
|
|
860 |
super realize
|
|
861 |
! !
|
|
862 |
|
38
|
863 |
!EditField methodsFor:'queries'!
|
|
864 |
|
130
|
865 |
preferredExtent
|
|
866 |
"return the preferred extent of this view.
|
38
|
867 |
That is the width of the string plus some extra,
|
|
868 |
but not wider than half of the screen"
|
|
869 |
|
|
870 |
|string w|
|
|
871 |
|
|
872 |
string := self contents.
|
|
873 |
(string isNil or:[string isBlank]) ifTrue:[
|
130
|
874 |
string := ' ' "/ just any string is ok ^ super preferredExtent
|
38
|
875 |
].
|
|
876 |
w := (((font on:device) widthOf:string) * 1.5) rounded.
|
|
877 |
w := w min:(device width // 2).
|
|
878 |
^ w @ self height
|
|
879 |
! !
|
|
880 |
|
0
|
881 |
!EditField methodsFor:'cursor drawing'!
|
|
882 |
|
|
883 |
showCursor
|
|
884 |
"make cursor visible if currently invisible - but only if this
|
|
885 |
EditField is enabled"
|
|
886 |
|
|
887 |
enabled ifTrue:[super showCursor]
|
|
888 |
! !
|
|
889 |
|
|
890 |
!EditField methodsFor:'cursor movement'!
|
|
891 |
|
125
|
892 |
cursorCol:col
|
|
893 |
"redefined to lock the cursor at the end, if I have a lngthLimit"
|
|
894 |
|
|
895 |
|c sz|
|
|
896 |
|
|
897 |
c := col.
|
|
898 |
lengthLimit notNil ifTrue:[
|
|
899 |
sz := lengthLimit.
|
|
900 |
c > sz ifTrue:[
|
|
901 |
c := sz+1.
|
|
902 |
]
|
|
903 |
].
|
|
904 |
super cursorCol:c
|
|
905 |
!
|
|
906 |
|
0
|
907 |
cursorLine:line col:col
|
38
|
908 |
"catch cursor movement"
|
|
909 |
|
|
910 |
super cursorLine:1 col:col
|
0
|
911 |
!
|
|
912 |
|
|
913 |
cursorDown
|
|
914 |
"catch cursor movement"
|
|
915 |
|
|
916 |
(cursorVisibleLine == nLinesShown) ifFalse:[
|
59
|
917 |
super cursorDown
|
0
|
918 |
]
|
|
919 |
! !
|
|
920 |
|
70
|
921 |
!EditField methodsFor:'event handling'!
|
19
|
922 |
|
125
|
923 |
keyPress:key x:x y:y
|
|
924 |
"if keyHandler is defined, pass input; otherwise check for leave
|
|
925 |
keys"
|
|
926 |
|
|
927 |
|leave xCol newOffset oldWidth newWidth|
|
|
928 |
|
|
929 |
enabled ifFalse:[
|
127
|
930 |
^ self
|
125
|
931 |
].
|
|
932 |
|
|
933 |
(key == #DeleteLine) ifTrue:[
|
127
|
934 |
Smalltalk at:#CopyBuffer put:(self contents).
|
|
935 |
self contents:''. ^ self
|
125
|
936 |
].
|
19
|
937 |
|
125
|
938 |
(key == #Tab) ifTrue:[
|
127
|
939 |
tabAction notNil ifTrue:[tabAction value. ^ self].
|
125
|
940 |
].
|
|
941 |
(key == #Return) ifTrue:[
|
127
|
942 |
crAction notNil ifTrue:[crAction value. ^ self].
|
125
|
943 |
].
|
|
944 |
leave := leaveKeys includes:key.
|
|
945 |
leave ifTrue:[
|
127
|
946 |
leaveAction notNil ifTrue:[
|
|
947 |
leaveAction value:key
|
|
948 |
].
|
121
|
949 |
|
127
|
950 |
((key == #Return and:[acceptOnReturn])
|
|
951 |
or:[key ~~ #Return and:[acceptOnLeave]]) ifTrue:[
|
|
952 |
self accept.
|
|
953 |
].
|
125
|
954 |
|
127
|
955 |
x >= 0 ifTrue:[
|
|
956 |
"
|
|
957 |
let superview know about the leave ...
|
|
958 |
This is a temporary kludge for the tableWidget -
|
|
959 |
it is no clean coding style. Should make the tableWidget
|
|
960 |
a proper model and handle it via the changed mechanism ....
|
|
961 |
"
|
|
962 |
(superView notNil and:[superView canHandle:key from:self]) ifTrue:[
|
|
963 |
superView keyPress:key x:x y:y.
|
|
964 |
].
|
|
965 |
].
|
|
966 |
^ self
|
19
|
967 |
].
|
121
|
968 |
|
125
|
969 |
"
|
|
970 |
ignore some keys (if not a leaveKey) ...
|
|
971 |
"
|
|
972 |
(key == #Find) ifTrue:[^self].
|
|
973 |
(key == #FindNext) ifTrue:[^self].
|
|
974 |
(key == #FindPrev) ifTrue:[^self].
|
|
975 |
(key == #GotoLine) ifTrue:[^self].
|
|
976 |
|
|
977 |
"
|
|
978 |
a normal key - let superclass's method insert it
|
|
979 |
"
|
|
980 |
oldWidth := self widthOfContents.
|
|
981 |
super keyPress:key x:x y:y.
|
|
982 |
|
|
983 |
"
|
|
984 |
for end-of-text, also move to end-of-line
|
|
985 |
"
|
|
986 |
key == #EndOfText ifTrue:[
|
127
|
987 |
super keyPress:#EndOfLine x:x y:y.
|
125
|
988 |
].
|
|
989 |
newWidth := self widthOfContents.
|
|
990 |
|
|
991 |
"
|
|
992 |
should (& can) we resize ?
|
|
993 |
"
|
|
994 |
xCol := (self xOfCol:cursorCol inVisibleLine:cursorLine) - leftOffset.
|
|
995 |
(xCol > (width * (5/6))) ifTrue:[
|
130
|
996 |
self changed:#preferredExtent
|
125
|
997 |
] ifFalse:[
|
127
|
998 |
newWidth < (width * (1/6)) ifTrue:[
|
130
|
999 |
self changed:#preferredExtent
|
127
|
1000 |
]
|
125
|
1001 |
].
|
|
1002 |
|
|
1003 |
"
|
|
1004 |
did someone react ?
|
|
1005 |
(if not, we scroll horizontally)
|
|
1006 |
"
|
|
1007 |
xCol := (self xOfCol:cursorCol inVisibleLine:cursorLine) - leftOffset.
|
|
1008 |
(xCol > (width * (5/6))) ifTrue:[
|
127
|
1009 |
newOffset := leftOffset + (width // 2).
|
125
|
1010 |
] ifFalse:[
|
127
|
1011 |
(xCol < (width * (1/6))) ifTrue:[
|
|
1012 |
newOffset := 0 max: leftOffset - (width // 2).
|
|
1013 |
] ifFalse:[
|
|
1014 |
newOffset := leftOffset
|
|
1015 |
]
|
125
|
1016 |
].
|
|
1017 |
newOffset ~~ leftOffset ifTrue:[
|
127
|
1018 |
leftOffset := newOffset.
|
|
1019 |
self clear.
|
|
1020 |
self redraw
|
125
|
1021 |
]
|
19
|
1022 |
!
|
0
|
1023 |
|
|
1024 |
buttonPress:button x:x y:y
|
|
1025 |
"enable myself on mouse click"
|
|
1026 |
|
|
1027 |
enabled ifFalse:[
|
59
|
1028 |
enabled := true.
|
|
1029 |
super buttonPress:button x:x y:y.
|
|
1030 |
enableAction notNil ifTrue:[
|
|
1031 |
enableAction value
|
|
1032 |
]
|
0
|
1033 |
] ifTrue:[
|
59
|
1034 |
super buttonPress:button x:x y:y
|
0
|
1035 |
]
|
|
1036 |
!
|
|
1037 |
|
125
|
1038 |
focusIn
|
|
1039 |
"got the explicit focus"
|
0
|
1040 |
|
|
1041 |
enabled ifFalse:[
|
125
|
1042 |
enabled := true.
|
|
1043 |
super focusIn.
|
|
1044 |
enableAction notNil ifTrue:[
|
|
1045 |
enableAction value
|
|
1046 |
]
|
|
1047 |
] ifTrue:[
|
|
1048 |
super focusIn
|
0
|
1049 |
].
|
105
|
1050 |
!
|
|
1051 |
|
|
1052 |
canHandle:aKey
|
|
1053 |
"return true, if the receiver would like to handle aKey
|
|
1054 |
(usually from another view, when the receiver is part of
|
|
1055 |
a more complex dialog box).
|
|
1056 |
We do return true here, since the editfield will handle
|
125
|
1057 |
all keys.
|
|
1058 |
OBSOLETE: dont use this anymore - its a leftover for the tableWidget"
|
105
|
1059 |
|
|
1060 |
^ true
|
0
|
1061 |
! !
|
125
|
1062 |
|
|
1063 |
!EditField methodsFor:'editing'!
|
|
1064 |
|
|
1065 |
paste:someText
|
|
1066 |
"redefined to force text to 1 line"
|
|
1067 |
|
|
1068 |
super paste:someText.
|
|
1069 |
list size > 1 ifTrue:[
|
|
1070 |
self deleteFromLine:2 toLine:(list size)
|
|
1071 |
]
|
|
1072 |
! !
|