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