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