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