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