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:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1990 by Claus Gittinger
|
0
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
an editable text-field. Realized by using an EditTextView,
|
|
26 |
and forcing its size to 1 line - disabling cursor movement
|
|
27 |
in the vertical direction.
|
|
28 |
|
7
|
29 |
$Header: /cvs/stx/stx/libwidg/EditField.st,v 1.4 1993-12-11 01:43:34 claus Exp $
|
0
|
30 |
written jan-90 by claus
|
|
31 |
'!
|
|
32 |
|
|
33 |
!EditField class methodsFor:'defaults'!
|
|
34 |
|
|
35 |
defaultNumberOfLines
|
|
36 |
"the number of lines in the field"
|
|
37 |
|
|
38 |
^ 1
|
|
39 |
! !
|
|
40 |
|
|
41 |
!EditField methodsFor:'initialization'!
|
|
42 |
|
|
43 |
initialize
|
|
44 |
super initialize.
|
|
45 |
self height:(font height + font descent + (topMargin * 2)).
|
|
46 |
enabled := true.
|
|
47 |
fixedSize := true.
|
|
48 |
nFullLinesShown := 1.
|
|
49 |
nLinesShown := 1.
|
|
50 |
!
|
|
51 |
|
|
52 |
initStyle
|
|
53 |
|myBgColor myFont|
|
|
54 |
|
|
55 |
super initStyle.
|
|
56 |
|
3
|
57 |
myBgColor := resources name:'BACKGROUND' default:nil.
|
0
|
58 |
myBgColor notNil ifTrue:[
|
|
59 |
bgColor := myBgColor on:device.
|
|
60 |
self viewBackground:bgColor.
|
|
61 |
selectionFgColor := fgColor on:device.
|
|
62 |
selectionBgColor := White on:device
|
|
63 |
].
|
|
64 |
|
3
|
65 |
myFont := resources name:'FONT' default:nil.
|
0
|
66 |
myFont notNil ifTrue:[
|
|
67 |
font := myFont
|
|
68 |
]
|
|
69 |
!
|
|
70 |
|
|
71 |
initializeMiddleButtonMenu
|
|
72 |
|labels|
|
|
73 |
|
|
74 |
labels := resources array:#(
|
|
75 |
'copy'
|
|
76 |
'cut'
|
|
77 |
'paste'
|
|
78 |
'replace').
|
|
79 |
|
|
80 |
self middleButtonMenu:(PopUpMenu
|
|
81 |
labels:labels
|
|
82 |
selectors:#(
|
|
83 |
copySelection
|
|
84 |
cut
|
|
85 |
paste
|
|
86 |
replace)
|
|
87 |
receiver:self
|
|
88 |
for:self)
|
|
89 |
! !
|
|
90 |
|
|
91 |
!EditField methodsFor:'realization'!
|
|
92 |
|
|
93 |
realize
|
|
94 |
"scroll back to beginning when realized"
|
|
95 |
leftOffset := 0.
|
|
96 |
super realize
|
|
97 |
! !
|
|
98 |
|
|
99 |
!EditField methodsFor:'private'!
|
|
100 |
|
|
101 |
startScrollUp:y
|
|
102 |
"no scrolling in editfields"
|
|
103 |
|
|
104 |
^ self
|
|
105 |
!
|
|
106 |
|
|
107 |
startScrollDown:y
|
|
108 |
"no scrolling in editfields"
|
|
109 |
|
|
110 |
^ self
|
|
111 |
! !
|
|
112 |
|
|
113 |
!EditField methodsFor:'accessing'!
|
|
114 |
|
|
115 |
contents
|
|
116 |
"return contents as a string
|
|
117 |
- redefined since EditFields hold only one line of text"
|
|
118 |
|
|
119 |
list isNil ifTrue:[^ ''].
|
|
120 |
(list size == 0) ifTrue:[^ ''].
|
|
121 |
^ list at:1
|
|
122 |
!
|
|
123 |
|
|
124 |
enable
|
|
125 |
"enable the field; show cursor and allow input"
|
|
126 |
|
|
127 |
enabled ifFalse:[
|
|
128 |
enableAction notNil ifTrue:[
|
|
129 |
enableAction value
|
|
130 |
].
|
|
131 |
enabled := true.
|
|
132 |
super showCursor
|
|
133 |
]
|
|
134 |
!
|
|
135 |
|
|
136 |
disable
|
|
137 |
"disable the field; hide cursor and ignore input"
|
|
138 |
|
|
139 |
enabled ifTrue:[
|
|
140 |
enabled := false.
|
|
141 |
self hideCursor
|
|
142 |
]
|
|
143 |
!
|
|
144 |
|
|
145 |
enableAction:aBlock
|
|
146 |
"define an action to be evaluated when enabled by clicking upon"
|
|
147 |
|
|
148 |
enableAction := aBlock
|
|
149 |
!
|
|
150 |
|
|
151 |
leaveAction:aBlock
|
|
152 |
"define an action to be evaluated when field is left by return key"
|
|
153 |
|
|
154 |
leaveAction := aBlock
|
|
155 |
!
|
|
156 |
|
|
157 |
initialText:aString
|
|
158 |
"set the initialText"
|
|
159 |
|
|
160 |
leftOffset := 0.
|
|
161 |
self contents:aString.
|
7
|
162 |
aString size ~~ 0 ifTrue:[
|
|
163 |
self selectFromLine:1 col:1 toLine:1 col:(aString size)
|
|
164 |
]
|
0
|
165 |
! !
|
|
166 |
|
|
167 |
!EditField methodsFor:'cursor drawing'!
|
|
168 |
|
|
169 |
showCursor
|
|
170 |
"make cursor visible if currently invisible - but only if this
|
|
171 |
EditField is enabled"
|
|
172 |
|
|
173 |
enabled ifTrue:[super showCursor]
|
|
174 |
! !
|
|
175 |
|
|
176 |
!EditField methodsFor:'cursor movement'!
|
|
177 |
|
|
178 |
cursorLine:line col:col
|
|
179 |
((line >= 1) and:[line <= nLinesShown]) ifTrue:[
|
|
180 |
super cursorLine:line col:col
|
|
181 |
]
|
|
182 |
"ignore"
|
|
183 |
!
|
|
184 |
|
|
185 |
cursorDown
|
|
186 |
"catch cursor movement"
|
|
187 |
|
|
188 |
(cursorVisibleLine == nLinesShown) ifFalse:[
|
|
189 |
super cursorDown
|
|
190 |
]
|
|
191 |
! !
|
|
192 |
|
|
193 |
!EditField methodsFor:'event processing'!
|
|
194 |
|
|
195 |
buttonPress:button x:x y:y
|
|
196 |
"enable myself on mouse click"
|
|
197 |
|
|
198 |
enabled ifFalse:[
|
|
199 |
enabled := true.
|
|
200 |
super buttonPress:button x:x y:y.
|
|
201 |
enableAction notNil ifTrue:[
|
|
202 |
enableAction value
|
|
203 |
]
|
|
204 |
] ifTrue:[
|
|
205 |
super buttonPress:button x:x y:y
|
|
206 |
]
|
|
207 |
!
|
|
208 |
|
|
209 |
canHandle:aKey
|
|
210 |
"return true, if the receiver would like to handle aKey
|
|
211 |
(usually from another view, when the receiver is part of
|
|
212 |
a more complex dialog box).
|
|
213 |
We do return true here, since the editfield will handle
|
|
214 |
all keys."
|
|
215 |
|
|
216 |
^ true
|
|
217 |
!
|
|
218 |
|
|
219 |
keyPress:key x:x y:y
|
|
220 |
"if keyHandler is defined, pass input; otherwise check for leave
|
|
221 |
keys"
|
|
222 |
|
3
|
223 |
|leave xCol|
|
0
|
224 |
|
|
225 |
enabled ifFalse:[
|
|
226 |
(keyboardHandler notNil
|
|
227 |
and:[keyboardHandler canHandle:key]) ifTrue:[
|
|
228 |
(keyboardHandler == self) ifTrue:[
|
|
229 |
self error:'invalid keyhandler'.
|
|
230 |
^ self
|
|
231 |
].
|
|
232 |
keyboardHandler keyPress:key x:x y:y
|
|
233 |
].
|
|
234 |
^ self
|
|
235 |
].
|
|
236 |
|
|
237 |
(key == #DeleteLine) ifTrue:[
|
|
238 |
Smalltalk at:#CopyBuffer put:(self contents).
|
|
239 |
self contents:''. ^ self
|
|
240 |
].
|
|
241 |
|
|
242 |
leave := false.
|
|
243 |
(key == #Return) ifTrue:[leave := true].
|
|
244 |
((key == #CursorDown) or:[key == #Next]) ifTrue:[leave := true].
|
|
245 |
((key == #CursorUp) or:[key == #Prior]) ifTrue:[leave := true].
|
|
246 |
|
|
247 |
leave ifTrue:[
|
|
248 |
leaveAction notNil ifTrue:[
|
|
249 |
leaveAction value:key
|
|
250 |
].
|
|
251 |
^ self
|
|
252 |
].
|
|
253 |
super keyPress:key x:x y:y.
|
3
|
254 |
xCol := (self xOfCol:cursorCol inLine:cursorLine) - leftOffset.
|
|
255 |
(xCol > (width * (5/6))) ifTrue:[
|
0
|
256 |
leftOffset := leftOffset + (width // 2).
|
|
257 |
self clear.
|
|
258 |
self redraw
|
|
259 |
] ifFalse:[
|
3
|
260 |
(xCol < (width * (1/6))) ifTrue:[
|
0
|
261 |
leftOffset := 0 max: leftOffset - (width // 2).
|
|
262 |
self clear.
|
|
263 |
self redraw
|
|
264 |
]
|
|
265 |
]
|
|
266 |
! !
|