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