author | Claus Gittinger <cg@exept.de> |
Wed, 05 Feb 2014 20:08:38 +0100 | |
changeset 13871 | 67bf2b738bb3 |
parent 12389 | 83d4d24d847c |
child 14985 | 8c2d0c3191db |
permissions | -rw-r--r-- |
10025 | 1 |
" |
2 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
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 |
"{ Package: 'stx:libtool' }" |
|
13 |
||
14 |
EditField subclass:#EditFieldWithCompletion |
|
15 |
instanceVariableNames:'showOptions optionsHolder optionsView optionsWindow |
|
10443 | 16 |
focusEventsToIgnore hadFocus completionJob' |
10025 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
19 |
category:'Views-Text' |
|
20 |
! |
|
21 |
||
22 |
!EditFieldWithCompletion class methodsFor:'documentation'! |
|
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
documentation to be added. |
|
41 |
||
42 |
[author:] |
|
43 |
Jan Vrany (janfrog@bruxa) |
|
44 |
||
45 |
[instance variables:] |
|
46 |
||
47 |
[class variables:] |
|
48 |
||
49 |
[see also:] |
|
50 |
||
51 |
" |
|
52 |
! |
|
53 |
||
54 |
example_1 |
|
55 |
||
56 |
|top field label value| |
|
57 |
||
58 |
top := StandardSystemView new. |
|
59 |
top |
|
60 |
extent:300 @ 300; |
|
61 |
label:'Live class completion field'. |
|
62 |
value := '' asValue. |
|
63 |
field := (EditFieldWithCompletion new) |
|
64 |
origin:10 @ 135 corner:280 @ 165; |
|
65 |
entryCompletionBlock:[:content | Smalltalk classnameCompletion:content ]; |
|
66 |
model:value. |
|
67 |
label := (Label new) |
|
68 |
origin:10 @ 95 corner:280 @ 115; |
|
69 |
labelChannel:value. |
|
70 |
top |
|
71 |
add:field; |
|
72 |
add:label. |
|
73 |
top open |
|
74 |
! |
|
75 |
||
76 |
examples |
|
77 |
" |
|
78 |
Opens a LiveCompletionEditField on a class name |
|
79 |
[exBegin] |
|
80 |
LiveCompletionEditField example_1 |
|
81 |
[exEnd] |
|
82 |
" |
|
83 |
! ! |
|
84 |
||
85 |
!EditFieldWithCompletion methodsFor:'accepting'! |
|
86 |
||
87 |
accept |
|
88 |
||
89 |
self unselect. |
|
90 |
super accept. |
|
91 |
||
92 |
"Created: / 27-07-2009 / 09:38:45 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
93 |
"Modified: / 12-02-2010 / 11:56:33 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
94 |
! ! |
|
95 |
||
96 |
!EditFieldWithCompletion methodsFor:'accessing-behavior'! |
|
97 |
||
98 |
showOptions |
|
99 |
^ showOptions |
|
100 |
! |
|
101 |
||
102 |
showOptions:aBoolean |
|
103 |
showOptions := aBoolean. |
|
104 |
! ! |
|
105 |
||
106 |
!EditFieldWithCompletion methodsFor:'accessing-dimensions'! |
|
107 |
||
108 |
absoluteLeft |
|
109 |
||
110 |
| absoluteLeft view | |
|
111 |
absoluteLeft := 1. |
|
112 |
view := self. |
|
113 |
[ view notNil ] whileTrue: |
|
114 |
[absoluteLeft := absoluteLeft + view left - 1. |
|
115 |
view := view superView]. |
|
116 |
^absoluteLeft |
|
117 |
||
118 |
"Created: / 08-08-2009 / 22:30:07 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
119 |
! |
|
120 |
||
121 |
absoluteTop |
|
122 |
||
123 |
| absoluteTop view | |
|
124 |
absoluteTop := 1. |
|
125 |
view := self. |
|
126 |
[ view notNil ] whileTrue: |
|
127 |
[absoluteTop := absoluteTop + view top - 1. |
|
128 |
view := view superView]. |
|
129 |
^absoluteTop |
|
130 |
||
131 |
"Created: / 08-08-2009 / 22:30:16 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
132 |
! ! |
|
133 |
||
134 |
!EditFieldWithCompletion methodsFor:'accessing-mvc'! |
|
135 |
||
136 |
optionsHolder |
|
137 |
||
138 |
^ optionsHolder |
|
139 |
||
140 |
"Created: / 09-08-2009 / 08:14:02 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
141 |
! |
|
142 |
||
143 |
optionsHolder:aValueHolder |
|
144 |
"set the 'options' value holder (automatically generated)" |
|
145 |
||
146 |
optionsHolder := aValueHolder. |
|
147 |
||
148 |
"Created: / 09-08-2009 / 08:14:24 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
149 |
! ! |
|
150 |
||
151 |
!EditFieldWithCompletion methodsFor:'event handling'! |
|
152 |
||
153 |
completion:best options:options |
|
154 |
|newContent oldContent| |
|
155 |
" |
|
156 |
oldContent := ((self contents ? '') asString). |
|
157 |
newContent := ((best isNil or:[ best = oldContent ]) |
|
158 |
and:[ options isNilOrEmptyCollection not ]) |
|
159 |
ifTrue:[ |
|
160 |
options first |
|
161 |
""options inject:options anyOne |
|
162 |
into:[:shortest :each | |
|
163 |
shortest asString size > each asString size ifTrue:[ |
|
164 |
each |
|
165 |
] ifFalse:[ shortest ] |
|
166 |
]"" |
|
167 |
] |
|
168 |
ifFalse:[ best ]. |
|
169 |
self contents:newContent asString. |
|
170 |
self cursorCol:oldContent size + 1. |
|
171 |
oldContent size < newContent size ifTrue:[ |
|
172 |
self |
|
173 |
selectFromLine:1 |
|
174 |
col:oldContent size + 1 |
|
175 |
toLine:1 |
|
176 |
col:newContent size |
|
177 |
]. |
|
178 |
" |
|
179 |
options size > 1 ifTrue:[ |
|
180 |
optionsHolder value:options. |
|
181 |
self showOptionsWindow |
|
182 |
] |
|
183 |
||
184 |
"Created: / 08-08-2009 / 22:02:29 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
185 |
"Modified: / 09-08-2009 / 08:16:41 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
186 |
"Modified: / 13-02-2010 / 09:51:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
187 |
! |
|
188 |
||
189 |
handleNonCommandKey: char |
|
190 |
||
191 |
super handleNonCommandKey: char. |
|
192 |
self startCompletion |
|
193 |
||
194 |
"Modified: / 26-07-2009 / 17:41:22 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
195 |
! |
|
196 |
||
197 |
handlesKeyPress:key inView:aView |
|
198 |
||
12110
7231e996222a
changed: #keyPress:x:y:view:
Claus Gittinger <cg@exept.de>
parents:
10913
diff
changeset
|
199 |
<resource: #keyboard (#CursorDown #CursorUp)> |
7231e996222a
changed: #keyPress:x:y:view:
Claus Gittinger <cg@exept.de>
parents:
10913
diff
changeset
|
200 |
|
10025 | 201 |
^aView == optionsView and: |
202 |
[(#( #CursorDown #CursorUp ) includes: key) not]. |
|
203 |
||
204 |
"Created: / 09-12-2010 / 21:31:51 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
205 |
! |
|
206 |
||
207 |
hasKeyboardFocus:aBoolean |
|
208 |
||
209 |
focusEventsToIgnore > 0 ifTrue:[ |
|
210 |
focusEventsToIgnore := focusEventsToIgnore - 1. |
|
211 |
" |
|
212 |
xxx == 0 ifTrue:[ |
|
213 |
Transcript showCR:'--> taking focus'. |
|
214 |
self takeFocus. |
|
215 |
self assert: self windowGroup focusView == self. |
|
216 |
hadFocus := true. |
|
217 |
]. |
|
218 |
" |
|
219 |
^self]. |
|
220 |
||
221 |
hadFocus == aBoolean ifTrue:[ |
|
222 |
^self |
|
223 |
]. |
|
224 |
||
225 |
aBoolean |
|
226 |
ifTrue: |
|
227 |
[self selectAll] |
|
228 |
ifFalse: |
|
229 |
[self unselect. |
|
230 |
self hideOptionsWindow]. |
|
231 |
hadFocus := aBoolean. |
|
232 |
^ super hasKeyboardFocus:aBoolean |
|
233 |
||
234 |
"Created: / 08-08-2009 / 23:28:50 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
235 |
"Modified: / 09-08-2009 / 10:14:25 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
236 |
"Modified: / 09-12-2010 / 22:05:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
237 |
! |
|
238 |
||
239 |
keyPress:key x:x y:y |
|
12110
7231e996222a
changed: #keyPress:x:y:view:
Claus Gittinger <cg@exept.de>
parents:
10913
diff
changeset
|
240 |
"Forward certain events to optionsView if any" |
10025 | 241 |
|
12389
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
242 |
<resource: #keyboard (#CursorDown #CursorUp #Accept #Return #Escape #BackSpace #Delete)> |
10025 | 243 |
|
12389
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
244 |
(optionsView notNil |
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
245 |
and:[#(CursorDown CursorUp) includes:key]) ifTrue:[ |
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
246 |
optionsView keyPress:key x:x y:y. |
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
247 |
^ self. |
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
248 |
]. |
10025 | 249 |
|
250 |
key = #Accept ifTrue:[self hideOptionsWindow]. |
|
251 |
key = #Return ifTrue:[self hideOptionsWindow]. |
|
252 |
key = #Escape ifTrue:[self hideOptionsWindow]. |
|
12389
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
253 |
(key = #BackSpace or:[key = #Delete]) ifTrue:[ |
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
254 |
super keyPress:key x:x y:y. |
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
255 |
self startCompletion. |
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
256 |
^ self. |
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
257 |
]. |
10025 | 258 |
|
12389
83d4d24d847c
class: EditFieldWithCompletion
Stefan Vogel <sv@exept.de>
parents:
12110
diff
changeset
|
259 |
super keyPress:key x:x y:y |
10025 | 260 |
|
261 |
"Created: / 08-08-2009 / 22:02:13 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
262 |
"Modified: / 09-08-2009 / 14:06:43 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
263 |
"Modified: / 09-02-2010 / 20:47:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
264 |
! |
|
265 |
||
266 |
keyPress:key x:x y:y view:aView |
|
267 |
||
268 |
self keyPress: key x:x y:y |
|
269 |
||
270 |
"Created: / 09-12-2010 / 21:32:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
271 |
! ! |
|
272 |
||
273 |
!EditFieldWithCompletion methodsFor:'initialization & release'! |
|
274 |
||
275 |
destroy |
|
276 |
||
277 |
self hideOptionsWindow. |
|
278 |
super destroy. |
|
279 |
||
280 |
"Created: / 08-08-2009 / 22:16:17 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
281 |
"Modified: / 09-08-2009 / 08:50:04 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
282 |
! |
|
283 |
||
284 |
initialize |
|
285 |
||
286 |
super initialize. |
|
287 |
showOptions := true. |
|
288 |
optionsHolder := ValueHolder new. |
|
289 |
focusEventsToIgnore := 0. |
|
10443 | 290 |
completionJob := BackgroundJob |
291 |
named: 'Edit Field Completion Job' |
|
292 |
on:[self doCompletion]. |
|
10025 | 293 |
|
294 |
"Created: / 08-08-2009 / 20:24:05 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
295 |
"Modified: / 09-08-2009 / 08:17:09 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
10443 | 296 |
"Modified: / 03-08-2011 / 17:50:15 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
10025 | 297 |
! ! |
298 |
||
299 |
!EditFieldWithCompletion methodsFor:'private'! |
|
300 |
||
301 |
doCompletion |
|
302 |
|oldContents options best| |
|
303 |
||
304 |
oldContents := (self contents ? '') asString. |
|
305 |
options := self entryCompletionBlock |
|
306 |
valueWithOptionalArgument:oldContents |
|
307 |
and:self. |
|
10885 | 308 |
options isNil ifTrue:[ |
309 |
^ self |
|
310 |
]. |
|
10025 | 311 |
best := options first. |
312 |
options := options second. |
|
10885 | 313 |
(options includes:best) ifFalse:[ |
314 |
best := options |
|
315 |
detect:[:e | e asString startsWith:best asString ] |
|
316 |
ifNone:[ best ] |
|
317 |
]. |
|
318 |
options isSortedCollection ifFalse:[ |
|
13871 | 319 |
options := options asSortedCollection:[:a :b | a displayString < b displayString ] |
10885 | 320 |
]. |
10025 | 321 |
self sensor |
10885 | 322 |
pushUserEvent:#completion:options: |
323 |
for:self |
|
324 |
withArguments:(Array with:best with:options). |
|
10025 | 325 |
|
326 |
"Created: / 26-07-2009 / 17:45:09 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
327 |
"Modified: / 09-08-2009 / 02:51:44 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
10885 | 328 |
"Modified: / 18-11-2011 / 14:33:56 / cg" |
329 |
"Modified (format): / 20-11-2011 / 09:42:25 / cg" |
|
13871 | 330 |
"Modified: / 20-04-2012 / 18:20:08 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
10025 | 331 |
! |
332 |
||
333 |
hideOptionsWindow |
|
10913 | 334 |
optionsWindow notNil ifTrue:[ |
335 |
optionsWindow destroy. |
|
336 |
optionsWindow := nil. |
|
337 |
] |
|
10025 | 338 |
|
339 |
"Created: / 08-08-2009 / 23:23:45 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
340 |
"Modified: / 09-08-2009 / 09:00:00 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
10913 | 341 |
"Modified: / 29-11-2011 / 11:27:03 / cg" |
10025 | 342 |
! |
343 |
||
344 |
showOptionsWindow |
|
345 |
||
13871 | 346 |
| x y w | |
347 |
||
10025 | 348 |
showOptions ifFalse:[^ self]. |
10913 | 349 |
optionsWindow notNil ifTrue:[ ^ self ]. |
10025 | 350 |
|
351 |
optionsView := SelectionInListModelView new |
|
352 |
textStartLeft: textStartLeft - 2; |
|
353 |
listHolder: optionsHolder; |
|
354 |
action:[:value | self contents:value asString ]; |
|
10445 | 355 |
doubleClickAction:[:index | |
356 |
self contents:(optionsView at:index) asString. |
|
357 |
self hideOptionsWindow. |
|
358 |
self accept |
|
359 |
]; |
|
10025 | 360 |
useIndex: false; |
361 |
" JV: Looks good to me " |
|
362 |
highlightMode: #line; |
|
363 |
font:self font; |
|
364 |
backgroundColor:self backgroundColor; |
|
13871 | 365 |
delegate: self; |
366 |
yourself. |
|
367 |
||
368 |
x := self absoluteLeft + 5" - optionsView textStartLeft". |
|
369 |
y := self absoluteTop + self height + 1 + 5. |
|
370 |
w := (width * 2) + 0"((optionsView textStartLeft) * 2)". |
|
10025 | 371 |
|
372 |
optionsWindow := StandardSystemView new |
|
373 |
bePopUpView; |
|
374 |
beSlave; |
|
13871 | 375 |
origin:x @ y |
376 |
extent:(w min: (Screen current width - x)) @ (fontHeight * 10); |
|
377 |
yourself. |
|
10025 | 378 |
|
379 |
ScrollableView |
|
380 |
forView:optionsView |
|
381 |
hasHorizontalScrollBar:false |
|
382 |
hasVerticalScrollBar:true |
|
383 |
miniScrollerH:true |
|
384 |
miniScrollerV:false |
|
385 |
origin:0.0@0.0 |
|
386 |
corner:1.0@1.0 |
|
387 |
in:optionsWindow. |
|
388 |
||
389 |
focusEventsToIgnore := 4. |
|
390 |
optionsWindow open. |
|
391 |
||
392 |
"Created: / 09-08-2009 / 08:12:21 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
393 |
"Modified: / 09-08-2009 / 09:28:47 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
10913 | 394 |
"Modified: / 29-11-2011 / 11:27:13 / cg" |
13871 | 395 |
"Modified: / 04-04-2012 / 13:08:45 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
10025 | 396 |
! |
397 |
||
398 |
startCompletion |
|
399 |
||
10443 | 400 |
completionJob restart. |
10025 | 401 |
|
402 |
"Created: / 26-07-2009 / 17:41:41 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
10443 | 403 |
"Modified (format): / 03-08-2011 / 17:50:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
10025 | 404 |
! ! |
405 |
||
406 |
!EditFieldWithCompletion class methodsFor:'documentation'! |
|
407 |
||
408 |
version_CVS |
|
13871 | 409 |
^ '$Header: /cvs/stx/stx/libtool/EditFieldWithCompletion.st,v 1.8 2014-02-05 19:08:38 cg Exp $' |
10025 | 410 |
! |
411 |
||
412 |
version_SVN |
|
13871 | 413 |
^ '$Id: EditFieldWithCompletion.st,v 1.8 2014-02-05 19:08:38 cg Exp $' |
10025 | 414 |
! ! |
12110
7231e996222a
changed: #keyPress:x:y:view:
Claus Gittinger <cg@exept.de>
parents:
10913
diff
changeset
|
415 |