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 |
EditTextView subclass:#EditField
|
|
14 |
instanceVariableNames:'leaveAction enabled enableAction'
|
59
|
15 |
classVariableNames:'DefaultForegroundColor DefaultBackgroundColor
|
|
16 |
DefaultSelectionForegroundColor DefaultSelectionBackgroundColor
|
|
17 |
DefaultFont'
|
0
|
18 |
poolDictionaries:''
|
|
19 |
category:'Views-Text'
|
|
20 |
!
|
|
21 |
|
|
22 |
EditField comment:'
|
5
|
23 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
59
|
24 |
All Rights Reserved
|
0
|
25 |
|
97
|
26 |
$Header: /cvs/stx/stx/libwidg/EditField.st,v 1.13 1995-03-06 19:28:26 claus Exp $
|
19
|
27 |
'!
|
|
28 |
|
|
29 |
!EditField 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 |
"
|
97
|
47 |
$Header: /cvs/stx/stx/libwidg/EditField.st,v 1.13 1995-03-06 19:28:26 claus Exp $
|
38
|
48 |
"
|
|
49 |
!
|
|
50 |
|
19
|
51 |
documentation
|
|
52 |
"
|
38
|
53 |
an editable text-field. Realized by using an EditTextView,
|
|
54 |
and forcing its size to 1 line - disabling cursor movement
|
|
55 |
in the vertical direction.
|
|
56 |
An action (leaveAction) is performed when the field is left
|
|
57 |
by either Return or a cursor movement, or if 'accept' is
|
|
58 |
performed from the menu.
|
19
|
59 |
"
|
|
60 |
! !
|
0
|
61 |
|
|
62 |
!EditField class methodsFor:'defaults'!
|
|
63 |
|
|
64 |
defaultNumberOfLines
|
|
65 |
"the number of lines in the field"
|
|
66 |
|
|
67 |
^ 1
|
59
|
68 |
!
|
|
69 |
|
|
70 |
updateStyleCache
|
60
|
71 |
DefaultForegroundColor := StyleSheet colorAt:'editFieldForegroundColor' default:Black.
|
|
72 |
DefaultBackgroundColor := StyleSheet colorAt:'editFieldBackgroundColor' default:White.
|
|
73 |
DefaultSelectionForegroundColor := StyleSheet colorAt:'editFieldSelectionForegroundColor' default:DefaultBackgroundColor.
|
|
74 |
DefaultSelectionBackgroundColor := StyleSheet colorAt:'editFieldSelectionBackgroundColor' default:DefaultForegroundColor.
|
|
75 |
DefaultFont := StyleSheet fontAt:'editFieldFont' default:nil.
|
59
|
76 |
|
|
77 |
"
|
60
|
78 |
self updateStyleCache
|
59
|
79 |
"
|
0
|
80 |
! !
|
|
81 |
|
|
82 |
!EditField methodsFor:'initialization'!
|
|
83 |
|
|
84 |
initialize
|
|
85 |
super initialize.
|
|
86 |
self height:(font height + font descent + (topMargin * 2)).
|
|
87 |
enabled := true.
|
|
88 |
fixedSize := true.
|
|
89 |
nFullLinesShown := 1.
|
|
90 |
nLinesShown := 1.
|
|
91 |
!
|
|
92 |
|
|
93 |
initStyle
|
|
94 |
super initStyle.
|
|
95 |
|
59
|
96 |
DefaultBackgroundColor notNil ifTrue:[
|
|
97 |
bgColor := DefaultBackgroundColor on:device.
|
|
98 |
self viewBackground:bgColor.
|
0
|
99 |
].
|
59
|
100 |
fgColor := DefaultForegroundColor.
|
|
101 |
selectionFgColor := DefaultSelectionForegroundColor.
|
|
102 |
selectionBgColor := DefaultSelectionBackgroundColor.
|
0
|
103 |
|
59
|
104 |
DefaultFont notNil ifTrue:[
|
|
105 |
font := DefaultFont on:device
|
0
|
106 |
]
|
|
107 |
!
|
|
108 |
|
97
|
109 |
editMenu
|
|
110 |
|labels selectors m|
|
0
|
111 |
|
97
|
112 |
labels := #(
|
|
113 |
'copy'
|
|
114 |
'cut'
|
|
115 |
'paste'
|
19
|
116 |
"
|
97
|
117 |
'replace'
|
19
|
118 |
"
|
97
|
119 |
'-'
|
|
120 |
'accept'
|
|
121 |
).
|
0
|
122 |
|
97
|
123 |
selectors := #(
|
|
124 |
copySelection
|
|
125 |
cut
|
|
126 |
paste
|
|
127 |
"
|
|
128 |
replace
|
19
|
129 |
"
|
97
|
130 |
nil
|
|
131 |
accept
|
|
132 |
).
|
|
133 |
|
|
134 |
m := PopUpMenu
|
|
135 |
labels:(resources array:labels)
|
|
136 |
selectors:selectors.
|
|
137 |
|
|
138 |
self hasSelection ifFalse:[
|
|
139 |
m disable:#copySelection.
|
|
140 |
m disable:#cut
|
|
141 |
].
|
|
142 |
|
|
143 |
^ m
|
0
|
144 |
! !
|
|
145 |
|
|
146 |
!EditField methodsFor:'realization'!
|
|
147 |
|
|
148 |
realize
|
|
149 |
"scroll back to beginning when realized"
|
|
150 |
leftOffset := 0.
|
|
151 |
super realize
|
|
152 |
! !
|
|
153 |
|
|
154 |
!EditField methodsFor:'private'!
|
|
155 |
|
59
|
156 |
startAutoScrollUp:y
|
|
157 |
"no vertical scrolling in editfields"
|
0
|
158 |
|
|
159 |
^ self
|
|
160 |
!
|
|
161 |
|
59
|
162 |
startAutoScrollDown:y
|
|
163 |
"no vertical scrolling in editfields"
|
0
|
164 |
|
|
165 |
^ self
|
|
166 |
! !
|
|
167 |
|
38
|
168 |
!EditField methodsFor:'queries'!
|
|
169 |
|
59
|
170 |
preferedExtent
|
|
171 |
"return the prefered extent of this view.
|
38
|
172 |
That is the width of the string plus some extra,
|
|
173 |
but not wider than half of the screen"
|
|
174 |
|
|
175 |
|string w|
|
|
176 |
|
|
177 |
string := self contents.
|
|
178 |
(string isNil or:[string isBlank]) ifTrue:[
|
59
|
179 |
string := ' ' "/ just any string is ok ^ super preferedExtent
|
38
|
180 |
].
|
|
181 |
w := (((font on:device) widthOf:string) * 1.5) rounded.
|
|
182 |
w := w min:(device width // 2).
|
|
183 |
^ w @ self height
|
|
184 |
! !
|
|
185 |
|
|
186 |
!EditField methodsFor:'editing'!
|
|
187 |
|
|
188 |
paste:someText
|
|
189 |
"redefined to force text to 1 line"
|
|
190 |
|
|
191 |
super paste:someText.
|
|
192 |
list size > 1 ifTrue:[
|
59
|
193 |
self deleteFromLine:2 toLine:(list size)
|
38
|
194 |
]
|
|
195 |
! !
|
|
196 |
|
0
|
197 |
!EditField methodsFor:'accessing'!
|
|
198 |
|
38
|
199 |
list:someText
|
|
200 |
"redefined to force text to 1 line, and notify dependents
|
|
201 |
of any changed extent-wishes."
|
|
202 |
|
|
203 |
|l oldWidth|
|
|
204 |
|
|
205 |
l := someText.
|
|
206 |
l size > 1 ifTrue:[
|
59
|
207 |
l := OrderedCollection with:(l at:1)
|
38
|
208 |
].
|
|
209 |
oldWidth := self widthOfContents.
|
|
210 |
super list:l.
|
|
211 |
self widthOfContents ~~ oldWidth ifTrue:[
|
59
|
212 |
self changed:#preferedExtent
|
38
|
213 |
]
|
|
214 |
!
|
|
215 |
|
0
|
216 |
contents
|
|
217 |
"return contents as a string
|
|
218 |
- redefined since EditFields hold only one line of text"
|
|
219 |
|
|
220 |
list isNil ifTrue:[^ ''].
|
|
221 |
(list size == 0) ifTrue:[^ ''].
|
|
222 |
^ list at:1
|
|
223 |
!
|
|
224 |
|
|
225 |
enable
|
|
226 |
"enable the field; show cursor and allow input"
|
|
227 |
|
|
228 |
enabled ifFalse:[
|
59
|
229 |
enableAction notNil ifTrue:[
|
|
230 |
enableAction value
|
|
231 |
].
|
|
232 |
enabled := true.
|
|
233 |
super showCursor
|
0
|
234 |
]
|
|
235 |
!
|
|
236 |
|
|
237 |
disable
|
|
238 |
"disable the field; hide cursor and ignore input"
|
|
239 |
|
|
240 |
enabled ifTrue:[
|
59
|
241 |
enabled := false.
|
|
242 |
self hideCursor
|
0
|
243 |
]
|
|
244 |
!
|
|
245 |
|
|
246 |
enableAction:aBlock
|
|
247 |
"define an action to be evaluated when enabled by clicking upon"
|
|
248 |
|
|
249 |
enableAction := aBlock
|
|
250 |
!
|
|
251 |
|
|
252 |
leaveAction:aBlock
|
|
253 |
"define an action to be evaluated when field is left by return key"
|
|
254 |
|
|
255 |
leaveAction := aBlock
|
|
256 |
!
|
|
257 |
|
62
|
258 |
initialText:aString selected:aBoolean
|
|
259 |
"set the initialText and select it if aBoolean is true"
|
|
260 |
|
|
261 |
|len s|
|
0
|
262 |
|
|
263 |
leftOffset := 0.
|
62
|
264 |
self contents:(s := aString asString).
|
|
265 |
aBoolean ifTrue:[
|
|
266 |
(len := s size) ~~ 0 ifTrue:[
|
|
267 |
self selectFromLine:1 col:1 toLine:1 col:len
|
|
268 |
]
|
7
|
269 |
]
|
62
|
270 |
!
|
|
271 |
|
|
272 |
initialText:aString
|
|
273 |
"set the initialText and select it"
|
|
274 |
|
|
275 |
self initialText:aString selected:true
|
0
|
276 |
! !
|
|
277 |
|
|
278 |
!EditField methodsFor:'cursor drawing'!
|
|
279 |
|
|
280 |
showCursor
|
|
281 |
"make cursor visible if currently invisible - but only if this
|
|
282 |
EditField is enabled"
|
|
283 |
|
|
284 |
enabled ifTrue:[super showCursor]
|
|
285 |
! !
|
|
286 |
|
|
287 |
!EditField methodsFor:'cursor movement'!
|
|
288 |
|
|
289 |
cursorLine:line col:col
|
38
|
290 |
"catch cursor movement"
|
|
291 |
|
|
292 |
super cursorLine:1 col:col
|
0
|
293 |
!
|
|
294 |
|
|
295 |
cursorDown
|
|
296 |
"catch cursor movement"
|
|
297 |
|
|
298 |
(cursorVisibleLine == nLinesShown) ifFalse:[
|
59
|
299 |
super cursorDown
|
0
|
300 |
]
|
|
301 |
! !
|
|
302 |
|
70
|
303 |
!EditField methodsFor:'event handling'!
|
19
|
304 |
|
|
305 |
accept
|
|
306 |
"accept the fields contents - perform the leave action as if
|
|
307 |
return was pressed."
|
|
308 |
|
|
309 |
enabled ifTrue:[
|
59
|
310 |
leaveAction notNil ifTrue:[
|
|
311 |
leaveAction value:#Return
|
|
312 |
].
|
|
313 |
"model-view behavior"
|
|
314 |
(model notNil and:[aspectSymbol notNil]) ifTrue:[
|
|
315 |
model perform:aspectSymbol with:(self contents).
|
|
316 |
].
|
19
|
317 |
].
|
|
318 |
!
|
0
|
319 |
|
|
320 |
buttonPress:button x:x y:y
|
|
321 |
"enable myself on mouse click"
|
|
322 |
|
|
323 |
enabled ifFalse:[
|
59
|
324 |
enabled := true.
|
|
325 |
super buttonPress:button x:x y:y.
|
|
326 |
enableAction notNil ifTrue:[
|
|
327 |
enableAction value
|
|
328 |
]
|
0
|
329 |
] ifTrue:[
|
59
|
330 |
super buttonPress:button x:x y:y
|
0
|
331 |
]
|
|
332 |
!
|
|
333 |
|
|
334 |
canHandle:aKey
|
|
335 |
"return true, if the receiver would like to handle aKey
|
|
336 |
(usually from another view, when the receiver is part of
|
|
337 |
a more complex dialog box).
|
|
338 |
We do return true here, since the editfield will handle
|
|
339 |
all keys."
|
|
340 |
|
|
341 |
^ true
|
|
342 |
!
|
|
343 |
|
|
344 |
keyPress:key x:x y:y
|
|
345 |
"if keyHandler is defined, pass input; otherwise check for leave
|
|
346 |
keys"
|
|
347 |
|
38
|
348 |
|leave xCol newOffset oldWidth newWidth|
|
0
|
349 |
|
|
350 |
enabled ifFalse:[
|
59
|
351 |
(keyboardHandler notNil
|
|
352 |
and:[keyboardHandler canHandle:key]) ifTrue:[
|
|
353 |
(keyboardHandler == self) ifTrue:[
|
|
354 |
self error:'invalid keyhandler'.
|
|
355 |
^ self
|
|
356 |
].
|
|
357 |
keyboardHandler keyPress:key x:x y:y
|
|
358 |
].
|
|
359 |
^ self
|
0
|
360 |
].
|
|
361 |
|
|
362 |
(key == #DeleteLine) ifTrue:[
|
59
|
363 |
Smalltalk at:#CopyBuffer put:(self contents).
|
|
364 |
self contents:''. ^ self
|
0
|
365 |
].
|
|
366 |
|
|
367 |
leave := false.
|
|
368 |
(key == #Return) ifTrue:[leave := true].
|
59
|
369 |
(key == #Accept) ifTrue:[leave := true].
|
0
|
370 |
((key == #CursorDown) or:[key == #Next]) ifTrue:[leave := true].
|
|
371 |
((key == #CursorUp) or:[key == #Prior]) ifTrue:[leave := true].
|
|
372 |
|
|
373 |
leave ifTrue:[
|
59
|
374 |
self accept.
|
|
375 |
^ self
|
0
|
376 |
].
|
38
|
377 |
|
|
378 |
oldWidth := self widthOfContents.
|
0
|
379 |
super keyPress:key x:x y:y.
|
38
|
380 |
newWidth := self widthOfContents.
|
|
381 |
|
|
382 |
"
|
|
383 |
should (& can) we resize ?
|
|
384 |
"
|
59
|
385 |
xCol := (self xOfCol:cursorCol inVisibleLine:cursorLine) - leftOffset.
|
3
|
386 |
(xCol > (width * (5/6))) ifTrue:[
|
59
|
387 |
self changed:#preferedExtent
|
38
|
388 |
] ifFalse:[
|
77
|
389 |
newWidth < (width * (1/6)) ifTrue:[
|
59
|
390 |
self changed:#preferedExtent
|
|
391 |
]
|
38
|
392 |
].
|
|
393 |
|
|
394 |
"
|
|
395 |
did someone react ?
|
|
396 |
(if not, we scroll horizontally)
|
|
397 |
"
|
59
|
398 |
xCol := (self xOfCol:cursorCol inVisibleLine:cursorLine) - leftOffset.
|
38
|
399 |
(xCol > (width * (5/6))) ifTrue:[
|
59
|
400 |
newOffset := leftOffset + (width // 2).
|
38
|
401 |
] ifFalse:[
|
59
|
402 |
(xCol < (width * (1/6))) ifTrue:[
|
|
403 |
newOffset := 0 max: leftOffset - (width // 2).
|
|
404 |
] ifFalse:[
|
|
405 |
newOffset := leftOffset
|
|
406 |
]
|
38
|
407 |
].
|
|
408 |
newOffset ~~ leftOffset ifTrue:[
|
59
|
409 |
leftOffset := newOffset.
|
|
410 |
self clear.
|
|
411 |
self redraw
|
0
|
412 |
]
|
|
413 |
! !
|