author | Claus Gittinger <cg@exept.de> |
Wed, 08 Sep 1999 17:46:41 +0200 | |
changeset 1508 | 9c54f2e53da1 |
parent 1468 | d3ad5b4b3b80 |
child 1533 | 609fce5df829 |
permissions | -rw-r--r-- |
1431 | 1 |
" |
2 |
COPYRIGHT (c) 1999 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 |
||
13 |
||
1390 | 14 |
ListModelView subclass:#SelectionInListModelView |
15 |
instanceVariableNames:'selection multipleSelectOk actionBlock doubleClickActionBlock |
|
16 |
clickLine highlightMode useIndex hilightFgColor hilightBgColor |
|
17 |
hilightLevel hilightFrameColor hilightStyle dragAccessPoint |
|
18 |
dropTarget dropSource' |
|
19 |
classVariableNames:'DefaultHilightStyle DefaultHilightBackgroundColor |
|
20 |
DefaultHilightForegroundColor DefaultHilightLevel |
|
21 |
DefaultHilightFrameColor' |
|
22 |
poolDictionaries:'' |
|
1430 | 23 |
category:'Views-Lists' |
1390 | 24 |
! |
25 |
||
26 |
!SelectionInListModelView class methodsFor:'documentation'! |
|
27 |
||
1431 | 28 |
copyright |
29 |
" |
|
30 |
COPYRIGHT (c) 1999 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 |
||
1390 | 43 |
documentation |
44 |
" |
|
45 |
SelectionInListModelView is mostly like SelectionInListView, |
|
46 |
but derives from the ListModelView and thus the list is kept |
|
47 |
by the model. |
|
48 |
||
49 |
[Instance variables:] |
|
50 |
selection <misc> the current selection. nil, a number or collection of numbers |
|
51 |
multipleSelectOk <Boolean> allow/disallow multiple selections( default:false ) |
|
52 |
actionBlock <Block> action evaluated on single click |
|
53 |
doubleClickActionBlock <Block> action evaluated on double click |
|
54 |
clickPosition <Point> internal use |
|
55 |
highlightMode <Symbol> how to draw the selection |
|
56 |
useIndex <Boolean> representation of the model selection |
|
57 |
hilightFgColor <Color> |
|
58 |
hilightBgColor <Color> how highlighted items are drawn |
|
59 |
hilightLevel <Integer> level to draw selections (i.e. for 3D effect) |
|
60 |
hilightFrameColor <Color> rectangle around highlighted items |
|
61 |
hilightStyle <Boolean> actions on widget are enabled/disabled |
|
62 |
||
63 |
[author:] |
|
64 |
Claus Atzkern |
|
65 |
||
66 |
[see also:] |
|
67 |
||
68 |
ListModelView |
|
69 |
HierarchicalListView |
|
70 |
" |
|
71 |
||
72 |
||
73 |
! |
|
74 |
||
75 |
examples |
|
76 |
" |
|
77 |
[exBegin] |
|
78 |
|top list view| |
|
79 |
||
80 |
list := List new. |
|
81 |
||
82 |
1 to:100 do:[:i| list add:('element: ', i printString) ]. |
|
83 |
top := StandardSystemView new; extent:300@300. |
|
84 |
view := ScrollableView for:SelectionInListModelView miniScroller:true |
|
85 |
origin:0.0@0.0 corner:1.0@1.0 in:top. |
|
86 |
view list:list. |
|
87 |
top open. |
|
88 |
[exEnd] |
|
89 |
||
90 |
||
91 |
[exBegin] |
|
92 |
|top list view item| |
|
93 |
||
94 |
list := HierarchicalList new. |
|
95 |
item := HierarchicalItem::Example labeled:'Test'. |
|
96 |
item expand. |
|
97 |
list showRoot:false. |
|
98 |
list root:item. |
|
99 |
||
100 |
top := StandardSystemView new; extent:300@300. |
|
101 |
view := ScrollableView for:SelectionInListModelView miniScroller:true |
|
102 |
origin:0.0@0.0 corner:1.0@1.0 in:top. |
|
103 |
||
104 |
view list:list. |
|
105 |
view doubleClickAction:[:i| (list at:i) toggleExpand ]. |
|
106 |
top open. |
|
107 |
[exEnd] |
|
108 |
||
109 |
" |
|
110 |
||
111 |
! ! |
|
112 |
||
1409 | 113 |
!SelectionInListModelView class methodsFor:'initialization'! |
114 |
||
115 |
initialize |
|
116 |
"setup the signals |
|
117 |
" |
|
118 |
super initialize. |
|
119 |
||
120 |
DefaultHilightLevel isNil ifTrue:[ |
|
121 |
self updateStyleCache |
|
122 |
]. |
|
123 |
||
124 |
||
125 |
! ! |
|
126 |
||
1390 | 127 |
!SelectionInListModelView class methodsFor:'defaults'! |
128 |
||
129 |
updateStyleCache |
|
130 |
"extract values from the styleSheet and cache them in class variables" |
|
131 |
||
132 |
<resource: #style ( |
|
133 |
#'selection.hilightForegroundColor' #'selection.hilightBackgroundColor' |
|
134 |
#'selection.hilightFrameColor' #'selection.hilightLevel' |
|
135 |
#'selection.foregroundColor' #'selection.backgroundColor' |
|
136 |
#'selection.shadowColor' #'selection.lightColor' |
|
137 |
#'selection.font' #'selection.hilightStyle' |
|
138 |
#'text.foregroundColor' |
|
139 |
)> |
|
140 |
||
141 |
DefaultHilightForegroundColor := StyleSheet colorAt:'selection.hilightForegroundColor'. |
|
142 |
DefaultHilightBackgroundColor := StyleSheet colorAt:'selection.hilightBackgroundColor'. |
|
143 |
DefaultHilightFrameColor := StyleSheet colorAt:'selection.hilightFrameColor'. |
|
144 |
DefaultHilightLevel := StyleSheet at:'selection.hilightLevel' default:0. |
|
145 |
DefaultHilightStyle := StyleSheet at:'selection.hilightStyle' default:(StyleSheet name). |
|
146 |
DefaultForegroundColor := StyleSheet colorAt:'selection.foregroundColor'. |
|
147 |
DefaultBackgroundColor := StyleSheet colorAt:'selection.backgroundColor'. |
|
148 |
DefaultShadowColor := StyleSheet colorAt:'selection.shadowColor'. |
|
149 |
DefaultLightColor := StyleSheet colorAt:'selection.lightColor'. |
|
150 |
||
151 |
DefaultForegroundColor isNil ifTrue:[ |
|
152 |
DefaultForegroundColor := StyleSheet colorAt:'text.foregroundColor' default:Black |
|
153 |
]. |
|
154 |
" |
|
155 |
self updateStyleCache |
|
156 |
" |
|
157 |
||
158 |
||
159 |
! ! |
|
160 |
||
161 |
!SelectionInListModelView methodsFor:'accessing'! |
|
162 |
||
163 |
list:aList |
|
164 |
"get the status of <showRoot> from the list |
|
165 |
" |
|
166 |
selection notNil ifTrue:[ |
|
167 |
selection := nil. |
|
168 |
self selectionChanged. |
|
169 |
]. |
|
170 |
^ super list:aList |
|
171 |
! ! |
|
172 |
||
173 |
!SelectionInListModelView methodsFor:'accessing behavior'! |
|
174 |
||
175 |
highlightMode |
|
176 |
"get the mode how to draw a selected line: |
|
177 |
#line draw whole line selected |
|
178 |
#label draw label selected |
|
179 |
" |
|
180 |
^ highlightMode |
|
181 |
||
182 |
||
183 |
! |
|
184 |
||
185 |
highlightMode:aMode |
|
186 |
"set the mode how to draw a selected line: |
|
187 |
#line draw whole line selected |
|
188 |
#label draw label selected |
|
189 |
" |
|
190 |
(aMode ~~ highlightMode and:[(aMode == #label or:[aMode == #line])]) ifTrue:[ |
|
191 |
highlightMode := aMode. |
|
192 |
||
193 |
shown ifTrue:[ |
|
194 |
self selectionDo:[:i|self redrawSelectionAt:i] |
|
195 |
] |
|
196 |
] |
|
197 |
||
198 |
! |
|
199 |
||
200 |
multipleSelectOk |
|
201 |
"allow/disallow multiple selections; the default is false |
|
202 |
" |
|
203 |
^ multipleSelectOk |
|
204 |
||
205 |
||
206 |
! |
|
207 |
||
208 |
multipleSelectOk:aState |
|
209 |
"allow/disallow multiple selections; the default is false |
|
210 |
" |
|
211 |
aState == multipleSelectOk ifFalse:[ |
|
212 |
multipleSelectOk := aState. |
|
213 |
selection notNil ifTrue:[ |
|
214 |
multipleSelectOk ifTrue:[ |
|
215 |
selection := Array with:selection |
|
216 |
] ifFalse:[ |
|
217 |
selection size ~~ 1 ifTrue:[ |
|
218 |
selection := nil. |
|
219 |
self invalidate. |
|
220 |
self selectionChanged |
|
221 |
] ifFalse:[ |
|
222 |
selection := selection at:1 |
|
223 |
] |
|
224 |
] |
|
225 |
] |
|
226 |
] |
|
227 |
||
228 |
! |
|
229 |
||
230 |
useIndex |
|
231 |
"set/clear the useIndex flag. |
|
232 |
the selection writen to the model are the indices into the list |
|
233 |
or the elements selected. |
|
234 |
" |
|
235 |
^ useIndex |
|
236 |
||
237 |
||
238 |
! |
|
239 |
||
240 |
useIndex:aBoolean |
|
241 |
"set/clear the useIndex flag. |
|
242 |
the selection writen to the model are the indices into the list |
|
243 |
or the elements selected. |
|
244 |
" |
|
245 |
useIndex := aBoolean ? true |
|
246 |
||
247 |
||
248 |
! ! |
|
249 |
||
250 |
!SelectionInListModelView methodsFor:'actions'! |
|
251 |
||
1423 | 252 |
action |
253 |
"get the action block to be performed on select |
|
254 |
" |
|
255 |
^ actionBlock |
|
256 |
! |
|
257 |
||
1390 | 258 |
action:aOneArgAction |
259 |
"set the action block to be performed on select |
|
260 |
" |
|
261 |
actionBlock := aOneArgAction |
|
262 |
||
263 |
||
264 |
! |
|
265 |
||
1423 | 266 |
doubleClickAction |
267 |
"get the action block to be performed on doubleclick |
|
268 |
" |
|
269 |
^ doubleClickActionBlock |
|
270 |
||
271 |
! |
|
272 |
||
1390 | 273 |
doubleClickAction:aOneArgAction |
274 |
"set the action block to be performed on doubleclick |
|
275 |
" |
|
276 |
doubleClickActionBlock := aOneArgAction |
|
277 |
||
278 |
! ! |
|
279 |
||
280 |
!SelectionInListModelView methodsFor:'change & update'! |
|
281 |
||
282 |
argForChangeMessage |
|
283 |
"return the argument for a selectionChange; |
|
284 |
depending on the setting of useIndex, this is either the numeric |
|
285 |
index of the selection or the value (i.e. the string) |
|
286 |
" |
|
287 |
useIndex ifFalse:[ |
|
288 |
^ self selectionValue |
|
289 |
]. |
|
290 |
selection isNil ifTrue:[ |
|
291 |
^ multipleSelectOk ifTrue:[#()] ifFalse:[0] |
|
292 |
]. |
|
293 |
^ multipleSelectOk ifTrue:[selection copy] ifFalse:[selection] |
|
294 |
! |
|
295 |
||
296 |
getSelectionFromModel |
|
297 |
"get selection from model; returns a selection or nil |
|
298 |
" |
|
299 |
|value newSel| |
|
300 |
||
301 |
( model isNil |
|
302 |
or:[(value := model value) isNil |
|
303 |
or:[value == 0]] |
|
304 |
) ifTrue:[ |
|
305 |
^ nil |
|
306 |
]. |
|
307 |
||
308 |
multipleSelectOk ifFalse:[ |
|
309 |
useIndex ifFalse:[ |
|
310 |
(value := self identityIndexOf:value) == 0 ifTrue:[ |
|
311 |
^ nil |
|
312 |
] |
|
313 |
]. |
|
314 |
^ value |
|
315 |
]. |
|
316 |
||
317 |
"/ MULTI SELECT |
|
318 |
||
319 |
value isEmpty ifTrue:[^ nil]. |
|
320 |
useIndex ifTrue:[^ value]. |
|
321 |
||
322 |
newSel := OrderedCollection new. |
|
323 |
||
324 |
value do:[:el||index| |
|
325 |
(index := self identityIndexOf:el) ~~ 0 ifTrue:[ |
|
326 |
newSel add:index |
|
327 |
] |
|
328 |
]. |
|
329 |
||
330 |
^ newSel notEmpty ifTrue:[newSel] ifFalse:[nil] |
|
331 |
! |
|
332 |
||
333 |
listSizeChanged:aLnNr nLines:aDeltaLines |
|
334 |
"update selection |
|
335 |
" |
|
336 |
|newSel noChg size changed| |
|
337 |
||
338 |
super listSizeChanged:aLnNr nLines:aDeltaLines. |
|
339 |
||
340 |
selection isNil ifTrue:[^ self]. |
|
341 |
||
342 |
list size == 0 ifTrue:[ |
|
343 |
^ self deselectWithoutRedraw |
|
344 |
]. |
|
345 |
multipleSelectOk ifFalse:[ |
|
346 |
selection >= aLnNr ifTrue:[ |
|
347 |
selection := selection + aDeltaLines. |
|
348 |
||
349 |
(aDeltaLines < 0 and:[selection < aLnNr]) ifTrue:[ |
|
350 |
self deselectWithoutRedraw |
|
351 |
] ifFalse:[ |
|
352 |
(model notNil and:[useIndex]) ifTrue:[ |
|
353 |
model setValue:selection |
|
354 |
] |
|
355 |
] |
|
356 |
]. |
|
357 |
^ self |
|
358 |
]. |
|
359 |
||
360 |
size := selection size. |
|
361 |
changed := false. |
|
362 |
||
363 |
aDeltaLines < 0 ifFalse:[ |
|
364 |
1 to:size do:[:anIndex| |
|
365 |
newSel := selection at:anIndex. |
|
366 |
||
367 |
newSel >= aLnNr ifTrue:[ |
|
368 |
changed := true. |
|
369 |
selection at:anIndex put:(newSel + aDeltaLines) |
|
370 |
] |
|
371 |
]. |
|
372 |
(changed and:[useIndex and:[model notNil]]) ifTrue:[ |
|
373 |
model setValue:(selection copy) |
|
374 |
]. |
|
375 |
^ self |
|
376 |
]. |
|
377 |
noChg := 0. |
|
378 |
||
379 |
1 to:size do:[:anIndex| |
|
380 |
newSel := selection at:anIndex. |
|
381 |
||
382 |
newSel >= aLnNr ifTrue:[ |
|
383 |
newSel := newSel + aDeltaLines. |
|
384 |
changed := true. |
|
385 |
||
386 |
newSel < aLnNr ifTrue:[ |
|
387 |
noChg := noChg + 1. |
|
388 |
newSel := 0. |
|
389 |
]. |
|
390 |
selection at:anIndex put:newSel |
|
391 |
] |
|
392 |
]. |
|
393 |
||
394 |
noChg ~~ 0 ifTrue:[ |
|
395 |
noChg == size ifTrue:[ |
|
396 |
self deselectWithoutRedraw |
|
397 |
] ifFalse:[ |
|
398 |
selection := selection select:[:i| i ~~ 0]. |
|
399 |
self selectionChanged |
|
400 |
] |
|
401 |
] ifFalse:[ |
|
402 |
(changed and:[useIndex and:[model notNil]]) ifTrue:[ |
|
403 |
model setValue:(selection copy) |
|
404 |
] |
|
405 |
] |
|
406 |
||
407 |
! |
|
408 |
||
409 |
selectionChanged |
|
410 |
"selection has changed. Call actionblock and/or send changeMessage if defined |
|
411 |
" |
|
412 |
|value arg| |
|
413 |
||
414 |
(model isNil and:[actionBlock isNil]) ifTrue:[ |
|
415 |
^ self |
|
416 |
]. |
|
417 |
||
418 |
arg := self argForChangeMessage. |
|
419 |
||
420 |
model notNil ifTrue:[ |
|
421 |
model removeDependent:self. |
|
422 |
self sendChangeMessage:#value: with:arg. |
|
423 |
model addDependent:self. |
|
424 |
]. |
|
425 |
||
426 |
actionBlock notNil ifTrue:[ |
|
427 |
(actionBlock numArgs) == 1 ifTrue:[ |
|
428 |
actionBlock value:arg |
|
429 |
] ifFalse:[ |
|
430 |
actionBlock value |
|
431 |
] |
|
432 |
]. |
|
433 |
||
434 |
! |
|
435 |
||
436 |
update:something with:aParameter from:changedObject |
|
437 |
"one of my models changed |
|
438 |
" |
|
439 |
|newSelection| |
|
440 |
||
441 |
changedObject == model ifTrue:[ |
|
442 |
newSelection := self getSelectionFromModel. |
|
443 |
||
444 |
newSelection ~= selection ifTrue:[ |
|
1468 | 445 |
self pushEvent:#setSelection: with:newSelection |
1390 | 446 |
] |
447 |
] ifFalse:[ |
|
448 |
super update:something with:aParameter from:changedObject |
|
449 |
]. |
|
450 |
! ! |
|
451 |
||
452 |
!SelectionInListModelView methodsFor:'drag & drop'! |
|
453 |
||
454 |
canDrag |
|
455 |
"returns true if dragging is enabled |
|
456 |
" |
|
457 |
^ dropSource notNil |
|
458 |
||
459 |
! |
|
460 |
||
461 |
dropSource |
|
462 |
"returns the dropSource or nil |
|
463 |
" |
|
464 |
^ dropSource |
|
465 |
||
466 |
! |
|
467 |
||
468 |
dropSource:aDropSourceOrNil |
|
469 |
"set the dropSource or nil |
|
470 |
" |
|
471 |
dropSource := aDropSourceOrNil. |
|
472 |
||
473 |
! |
|
474 |
||
475 |
dropTarget |
|
476 |
"returns the dropTarget or nil |
|
477 |
" |
|
478 |
^ dropTarget |
|
479 |
||
480 |
||
481 |
! |
|
482 |
||
483 |
dropTarget:aDropTragetOrNil |
|
484 |
"set the dropTarget or nil |
|
485 |
" |
|
486 |
dropTarget := aDropTragetOrNil. |
|
487 |
||
488 |
! |
|
489 |
||
490 |
startDragAt:aPoint |
|
491 |
"start drag at a point |
|
492 |
" |
|
493 |
dropSource notNil ifTrue:[ |
|
494 |
dropSource startDragSelector notNil ifTrue:[ |
|
495 |
dropSource startDragIn:self at:aPoint |
|
496 |
] ifFalse:[ |
|
497 |
DragAndDropManager new startDragFrom:self |
|
498 |
dropSource:dropSource |
|
499 |
offset:#center |
|
500 |
] |
|
501 |
] |
|
502 |
||
503 |
! ! |
|
504 |
||
505 |
!SelectionInListModelView methodsFor:'drawing'! |
|
506 |
||
1399 | 507 |
drawFrom:start to:stop x:xL y:yT toX:xR |
1390 | 508 |
"draw the lines between start to stop without clearing the background |
509 |
" |
|
510 |
|selY selH |
|
1399 | 511 |
w "{ Class:SmallInteger }" |
1390 | 512 |
y0 "{ Class:SmallInteger }" |
513 |
y1 "{ Class:SmallInteger }" |
|
514 |
hg "{ Class:SmallInteger }" |
|
515 |
| |
|
1399 | 516 |
w := xR - xL. |
517 |
||
1390 | 518 |
(highlightMode == #line and:[selection notNil]) ifTrue:[ |
519 |
"/ redraw the background for all selected lines in the invalid range |
|
520 |
||
521 |
self selectionDo:[:lnNr| |
|
522 |
(lnNr between:start and:stop) ifTrue:[ |
|
523 |
selY isNil ifTrue:[ |
|
524 |
selY := OrderedCollection new. |
|
525 |
selH := OrderedCollection new. |
|
526 |
self paint:hilightBgColor. |
|
527 |
]. |
|
528 |
y0 := self yVisibleOfLine:lnNr. |
|
529 |
y1 := self yVisibleOfLine:(lnNr + 1). |
|
530 |
hg := y1 - y0. |
|
531 |
selY add:y0. |
|
532 |
selH add:hg. |
|
1399 | 533 |
self fillRectangleX:xL y:y0 width:w height:hg. |
1390 | 534 |
] |
535 |
] |
|
536 |
]. |
|
1399 | 537 |
self drawElementsFrom:start to:stop x:xL y:yT toX:xR. |
1390 | 538 |
|
539 |
"/ draw selection frames |
|
540 |
selY notNil ifTrue:[ |
|
541 |
1 to:selY size do:[:i| |
|
1399 | 542 |
self drawSelectionFrameAtX:xL y:(selY at:i) width:w h:(selH at:i) |
1390 | 543 |
] |
544 |
]. |
|
545 |
||
546 |
||
547 |
||
548 |
! |
|
549 |
||
1399 | 550 |
drawLabelAt:anIndex x:xL y:yT toX:xR y:yB |
1390 | 551 |
"draw the label at position x/y without clearing the background |
552 |
" |
|
553 |
|label item |
|
554 |
w "{ Class:SmallInteger }" |
|
1399 | 555 |
h "{ Class:SmallInteger }" |
1390 | 556 |
x0 "{ Class:SmallInteger }" |
557 |
| |
|
1508
9c54f2e53da1
check whether #at:ifAbsent returns nil.
Claus Gittinger <cg@exept.de>
parents:
1468
diff
changeset
|
558 |
item := list at:anIndex ifAbsent:nil. |
9c54f2e53da1
check whether #at:ifAbsent returns nil.
Claus Gittinger <cg@exept.de>
parents:
1468
diff
changeset
|
559 |
item isNil ifTrue:[^ self]. |
1390 | 560 |
|
561 |
(self isInSelection:anIndex) ifTrue:[ |
|
562 |
highlightMode == #label ifTrue:[ |
|
563 |
w := (item widthOn:self) + textStartLeft. |
|
1399 | 564 |
h := yB - yT. |
565 |
x0 := xL - (textStartLeft // 2). |
|
1390 | 566 |
self paint:hilightBgColor. |
1399 | 567 |
self fillRectangleX:x0 y:yT width:w height:h. |
568 |
self drawSelectionFrameAtX:x0 y:yT width:w h:h. |
|
1390 | 569 |
]. |
570 |
self paint:hilightFgColor on:hilightBgColor |
|
571 |
] ifFalse:[ |
|
572 |
self paint:fgColor on:bgColor. |
|
573 |
]. |
|
1399 | 574 |
self displayElement:item x:xL y:yT toX:xR y:yB. |
1390 | 575 |
|
576 |
||
577 |
! |
|
578 |
||
579 |
drawSelectionFrameAtX:x0 y:y0 width:w h:h |
|
580 |
"redraw selection frame for a line |
|
581 |
" |
|
582 |
| |
|
583 |
x1 "{ Class: SmallInteger }" |
|
584 |
x "{ Class: SmallInteger }" |
|
585 |
y "{ Class: SmallInteger }" |
|
586 |
| |
|
587 |
x1 := x0 + w. |
|
588 |
||
589 |
hilightFrameColor notNil ifTrue:[ |
|
590 |
hilightLevel == 0 ifTrue:[ |
|
591 |
self paint:hilightFrameColor. |
|
592 |
||
593 |
highlightMode == #line ifTrue:[ |
|
594 |
self displayLineFromX:x0 y:y0 toX:x1 y:y0. |
|
595 |
y := y0 + h - 1. |
|
596 |
self displayLineFromX:x0 y:y toX:x1 y:y. |
|
597 |
] ifFalse:[ |
|
598 |
self displayRectangleX:x0 y:y0 width:w height:h |
|
599 |
]. |
|
600 |
^ self. |
|
601 |
] |
|
602 |
] ifFalse:[ |
|
603 |
hilightStyle == #motif ifTrue:[ |
|
604 |
self paint:bgColor. |
|
605 |
y := y0 + 1. |
|
606 |
highlightMode == #line ifTrue:[ |
|
607 |
self displayLineFromX:x0 y:y toX:x1 y:y. |
|
608 |
y := y0 + h - 2. |
|
609 |
self displayLineFromX:x0 y:y toX:x1 y:y. |
|
610 |
] ifFalse:[ |
|
611 |
self displayRectangleX:x0 + 1 y:y width:w - 2 height:h - 2 |
|
612 |
] |
|
613 |
] |
|
614 |
]. |
|
615 |
||
616 |
hilightLevel ~~ 0 ifTrue:[ |
|
617 |
"/ draw edge |
|
618 |
highlightMode == #line ifTrue:[ |
|
619 |
x := margin. |
|
620 |
x1 := width - x - x. |
|
621 |
] ifFalse:[ |
|
622 |
x := x0. |
|
623 |
x1 := w. |
|
624 |
]. |
|
625 |
self drawEdgesForX:x y:y0 width:x1 height:h level:hilightLevel. |
|
626 |
] |
|
627 |
||
628 |
! |
|
629 |
||
630 |
redrawSelectionAt:anIndex |
|
631 |
"called to redraw a line caused by a change of the selection |
|
632 |
" |
|
1421 | 633 |
| |
1390 | 634 |
h "{ Class:SmallInteger }" |
635 |
y0 "{ Class:SmallInteger }" |
|
636 |
y1 "{ Class:SmallInteger }" |
|
637 |
| |
|
1421 | 638 |
shown ifTrue:[ |
639 |
y0 := (self yVisibleOfLine:anIndex) max:margin. |
|
640 |
y1 := (self yVisibleOfLine:(anIndex + 1)) min:(height - margin). |
|
1390 | 641 |
|
1421 | 642 |
(h := y1 - y0) > 0 ifTrue:[ |
1399 | 643 |
self redrawX:margin y:y0 width:(self innerWidth) height:h |
1390 | 644 |
] |
645 |
] |
|
646 |
! ! |
|
647 |
||
648 |
!SelectionInListModelView methodsFor:'event handling'! |
|
649 |
||
650 |
buttonMotion:buttonMask x:x y:y |
|
651 |
"mouse-move while button was pressed - handle selection changes |
|
652 |
" |
|
653 |
|sensor idx p cY oY lnNr h| |
|
654 |
||
655 |
(enabled and:[selection notNil]) ifFalse:[^ self]. |
|
656 |
||
657 |
dragAccessPoint notNil ifTrue:[ |
|
658 |
p := x @ y. |
|
659 |
||
660 |
(dragAccessPoint dist:p) > 5.0 ifTrue:[ |
|
661 |
dragAccessPoint := nil. |
|
662 |
self startDragAt:p |
|
663 |
]. |
|
664 |
^ self |
|
665 |
]. |
|
666 |
||
667 |
(multipleSelectOk and:[self sensor leftButtonPressed]) ifFalse:[ |
|
668 |
^ self |
|
669 |
]. |
|
670 |
||
671 |
clickLine isNil ifTrue:[ |
|
672 |
^ self |
|
673 |
]. |
|
674 |
cY := self yVisibleOfLine:clickLine. |
|
675 |
oY := cY. |
|
676 |
||
677 |
y < cY ifTrue:[ |
|
678 |
(lnNr := clickLine - 1) == 0 ifTrue:[^ self]. |
|
679 |
cY := self yVisibleOfLine:lnNr. |
|
680 |
h := oY - cY. |
|
681 |
] ifFalse:[ |
|
682 |
( (lnNr := clickLine + 1) > list size |
|
683 |
or:[(cY := self yVisibleOfLine:lnNr) > y] |
|
684 |
) ifTrue:[ |
|
685 |
^ self |
|
686 |
]. |
|
687 |
h := cY - oY. |
|
688 |
]. |
|
689 |
selection := selection asOrderedCollection. |
|
690 |
||
691 |
(selection removeIdentical:lnNr ifAbsent:nil) isNil ifTrue:[ |
|
692 |
selection add:lnNr |
|
693 |
]. |
|
694 |
clickLine := lnNr. |
|
695 |
||
696 |
(cY between:margin and:(height - h)) ifTrue:[ |
|
697 |
self redrawSelectionAt:lnNr |
|
698 |
] ifFalse:[ |
|
699 |
self scrollToLine:lnNr. |
|
700 |
]. |
|
701 |
self selectionChanged |
|
702 |
||
703 |
! |
|
704 |
||
705 |
buttonMultiPress:button x:x y:y |
|
706 |
"button was pressed multiple - handle a doubleClick action |
|
707 |
" |
|
708 |
clickLine := nil. |
|
709 |
dragAccessPoint := nil. |
|
710 |
||
711 |
enabled ifFalse:[^ self]. |
|
712 |
||
713 |
((button == 1) or:[button == #select]) ifFalse:[ |
|
714 |
^ super buttonMultiPress:button x:x y:y |
|
715 |
]. |
|
716 |
self doubleClicked |
|
717 |
! |
|
718 |
||
719 |
buttonPress:button x:x y:y |
|
720 |
"a button was pressed - handle selection here |
|
721 |
" |
|
722 |
|lnNr sensor start step list changed| |
|
723 |
||
724 |
clickLine := nil. |
|
725 |
dragAccessPoint := nil. |
|
726 |
||
727 |
enabled ifFalse:[^ self]. |
|
728 |
||
729 |
(button == 1 or:[button == #select]) ifFalse:[ |
|
730 |
^ super buttonPress:button x:x y:y |
|
731 |
]. |
|
732 |
||
733 |
(lnNr := self yVisibleToLineNr:y) isNil ifTrue:[ |
|
734 |
^ self |
|
735 |
]. |
|
736 |
clickLine := lnNr. |
|
737 |
||
738 |
(multipleSelectOk and:[(sensor := self sensor) notNil]) ifTrue:[ |
|
739 |
sensor ctrlDown ifTrue:[ |
|
740 |
(self isInSelection:lnNr) ifTrue:[self removeFromSelection:lnNr] |
|
741 |
ifFalse:[self addToSelection:lnNr]. |
|
742 |
^ self selectionChanged |
|
743 |
||
744 |
]. |
|
745 |
(selection notNil and:[sensor shiftDown]) ifTrue:[ |
|
746 |
start := selection at:1. |
|
747 |
step := lnNr < start ifTrue:[-1] ifFalse:[1]. |
|
748 |
list := selection. |
|
749 |
selection := OrderedCollection new. |
|
750 |
changed := false. |
|
751 |
||
752 |
start to:lnNr by:step do:[:i| |
|
753 |
selection add:i. |
|
754 |
(list identityIndexOf:i) == 0 ifTrue:[ |
|
755 |
changed := true. |
|
756 |
self redrawSelectionAt:i "/ redraw selected |
|
757 |
] |
|
758 |
]. |
|
759 |
list do:[:i| |
|
760 |
(selection identityIndexOf:i) == 0 ifTrue:[ |
|
761 |
changed := true. |
|
762 |
self redrawSelectionAt:i "/ redraw unselected |
|
763 |
]. |
|
764 |
]. |
|
765 |
changed ifTrue:[ |
|
766 |
self selectionChanged |
|
767 |
]. |
|
768 |
^ self |
|
769 |
] |
|
770 |
]. |
|
771 |
||
772 |
(self canDrag and:[self isInSelection:lnNr]) ifTrue:[ |
|
773 |
dragAccessPoint := x @ y |
|
774 |
] ifFalse:[ |
|
775 |
self selectedIndex ~~ lnNr ifTrue:[ |
|
776 |
self selectWithoutScroll:lnNr. |
|
777 |
self selectionChanged |
|
778 |
] |
|
779 |
] |
|
780 |
! |
|
781 |
||
782 |
buttonRelease:button x:x y:y |
|
783 |
"a button was released |
|
784 |
" |
|
785 |
enabled ifTrue:[ |
|
786 |
(dragAccessPoint notNil and:[clickLine notNil]) ifTrue:[ |
|
787 |
self selectedIndex ~~ clickLine ifTrue:[ |
|
788 |
self selectWithoutScroll:clickLine. |
|
789 |
self selectionChanged |
|
790 |
] |
|
791 |
] |
|
792 |
]. |
|
793 |
clickLine := nil. |
|
794 |
dragAccessPoint := nil. |
|
795 |
||
796 |
||
797 |
! |
|
798 |
||
799 |
characterPress:aKey x:x y:y |
|
800 |
" a character is pressed - lookup and change selection |
|
801 |
" |
|
802 |
|lnNr size idx sensor stp to1 fr2| |
|
803 |
||
804 |
(enabled and:[(size := self size) > 1]) ifFalse:[ |
|
805 |
^ self |
|
806 |
]. |
|
807 |
lnNr := self firstInSelection ? 0. |
|
808 |
||
809 |
((sensor := self sensor) notNil and:[sensor shiftDown]) ifTrue:[ |
|
810 |
stp := -1. "/ search backward |
|
811 |
to1 := 1. |
|
812 |
fr2 := size. |
|
813 |
] ifFalse:[ |
|
814 |
stp := 1. "/ search forward |
|
815 |
to1 := size. |
|
816 |
fr2 := 1. |
|
817 |
]. |
|
818 |
||
819 |
idx := self findLineFrom:lnNr+stp to:to1 by:stp startingWithCharacter:aKey. |
|
820 |
||
821 |
idx == 0 ifTrue:[ |
|
822 |
idx := self findLineFrom:fr2 to:lnNr-stp by:stp startingWithCharacter:aKey |
|
823 |
]. |
|
824 |
idx ~~ 0 ifTrue:[^ self selection:idx] |
|
825 |
! |
|
826 |
||
827 |
doubleClicked |
|
828 |
"handle a double click |
|
829 |
" |
|
830 |
(doubleClickActionBlock notNil and:[self numberOfSelections == 1]) ifTrue:[ |
|
831 |
(doubleClickActionBlock numArgs == 1) ifTrue:[ |
|
832 |
doubleClickActionBlock value:(self selectedIndex) |
|
833 |
] ifFalse:[ |
|
834 |
doubleClickActionBlock value |
|
835 |
] |
|
836 |
] |
|
837 |
||
838 |
! |
|
839 |
||
840 |
findLineFrom:aStart to:aStop by:aStep startingWithCharacter:aCharacter |
|
841 |
"find a line starting with a character |
|
842 |
" |
|
843 |
|item char lbl cmp |
|
844 |
size "{ Class:SmallInteger }" |
|
845 |
start "{ Class:SmallInteger }" |
|
846 |
stop "{ Class:SmallInteger }" |
|
847 |
| |
|
848 |
(size := list size) ~~ 0 ifTrue:[ |
|
849 |
aStep > 0 ifTrue:[ |
|
850 |
aStart > aStop ifTrue:[^ 0]. |
|
851 |
] ifFalse:[ |
|
852 |
(aStep == 0 or:[aStop > aStart]) ifTrue:[^ 0] |
|
853 |
]. |
|
854 |
||
855 |
start := aStart < 0 ifTrue:[1] ifFalse:[aStart min:size]. |
|
856 |
stop := aStop < 0 ifTrue:[1] ifFalse:[aStop min:size]. |
|
857 |
char := aCharacter asUppercase. |
|
858 |
||
859 |
start to:stop by:aStep do:[:anIndex| |
|
1508
9c54f2e53da1
check whether #at:ifAbsent returns nil.
Claus Gittinger <cg@exept.de>
parents:
1468
diff
changeset
|
860 |
item := list at:anIndex ifAbsent:nil. |
9c54f2e53da1
check whether #at:ifAbsent returns nil.
Claus Gittinger <cg@exept.de>
parents:
1468
diff
changeset
|
861 |
item isNil ifTrue:[^ 0]. "/ list changed |
9c54f2e53da1
check whether #at:ifAbsent returns nil.
Claus Gittinger <cg@exept.de>
parents:
1468
diff
changeset
|
862 |
|
9c54f2e53da1
check whether #at:ifAbsent returns nil.
Claus Gittinger <cg@exept.de>
parents:
1468
diff
changeset
|
863 |
lbl := item perform:#string ifNotUnderstood:nil. |
1390 | 864 |
|
865 |
lbl notNil ifTrue:[ |
|
866 |
cmp := lbl string at:1 ifAbsent:nil. |
|
867 |
||
868 |
cmp notNil ifTrue:[ |
|
869 |
(char == cmp or:[char == cmp asUppercase]) ifTrue:[ |
|
870 |
^ anIndex |
|
871 |
] |
|
872 |
] |
|
873 |
] |
|
874 |
] |
|
875 |
]. |
|
876 |
^ 0 |
|
877 |
||
878 |
||
879 |
! |
|
880 |
||
881 |
keyPress:aKey x:x y:y |
|
882 |
"a key was pressed - handle page-keys here |
|
883 |
" |
|
884 |
<resource: #keyboard( #Return #CursorUp #CursorDown )> |
|
885 |
||
886 |
|sensor n size lineNr| |
|
887 |
||
888 |
enabled ifFalse:[ |
|
889 |
^ super keyPress:aKey x:x y:y |
|
890 |
]. |
|
891 |
aKey == #Return ifTrue:[ |
|
892 |
self numberOfSelections == 1 ifTrue:[self doubleClicked]. |
|
893 |
^ self |
|
894 |
]. |
|
895 |
||
896 |
aKey isCharacter ifTrue:[ |
|
897 |
^ self characterPress:aKey x:x y:y |
|
898 |
]. |
|
899 |
||
900 |
(aKey == #CursorUp or:[aKey == #CursorDown]) ifFalse:[ |
|
901 |
^ super keyPress:aKey x:x y:y |
|
902 |
]. |
|
903 |
||
904 |
(size := self size) == 0 ifTrue:[ |
|
905 |
^ self |
|
906 |
]. |
|
907 |
||
908 |
lineNr := self selectedIndex. |
|
909 |
sensor := self sensor. |
|
910 |
||
911 |
sensor notNil ifTrue:[ |
|
912 |
n := (1 + (sensor compressKeyPressEventsWithKey:aKey)) \\ size. |
|
913 |
n == 0 ifTrue:[^ self]. |
|
914 |
] ifFalse:[ |
|
915 |
n := 1 |
|
916 |
]. |
|
917 |
||
918 |
aKey == #CursorUp ifTrue:[ |
|
919 |
lineNr == 0 ifTrue:[lineNr := size + 1]. |
|
920 |
(n := lineNr - n) <= 0 ifTrue:[n := size + n] |
|
921 |
] ifFalse:[ |
|
922 |
(n := lineNr + n) > size ifTrue:[n := n - size] |
|
923 |
]. |
|
924 |
self selection:n |
|
925 |
! ! |
|
926 |
||
1421 | 927 |
!SelectionInListModelView methodsFor:'focus handling'! |
928 |
||
929 |
wantsFocusWithPointerEnter |
|
930 |
"return true, if I want the focus when |
|
931 |
the mouse pointer enters" |
|
932 |
||
933 |
|pref| |
|
934 |
||
935 |
pref := UserPreferences current focusFollowsMouse. |
|
936 |
(pref ~~ false |
|
937 |
and:[(styleSheet at:#'selection.requestFocusOnPointerEnter' default:true) |
|
938 |
]) ifTrue:[ |
|
939 |
^ true |
|
940 |
]. |
|
941 |
^ false |
|
942 |
||
943 |
||
944 |
! ! |
|
945 |
||
1390 | 946 |
!SelectionInListModelView methodsFor:'initialize / release'! |
947 |
||
948 |
fetchResources |
|
949 |
"fetch device colors and ..., to avoid reallocation at redraw time; |
|
950 |
*** called after a create or snapin to fetch all device resources |
|
951 |
" |
|
952 |
||
953 |
super fetchResources. |
|
954 |
||
955 |
hilightFgColor := self colorOnDevice:hilightFgColor. |
|
956 |
hilightBgColor := self colorOnDevice:hilightBgColor. |
|
957 |
hilightFrameColor := self colorOnDevice:hilightFrameColor. |
|
958 |
! |
|
959 |
||
960 |
initStyle |
|
961 |
"setup viewStyle specifics |
|
962 |
" |
|
963 |
|h| |
|
964 |
||
965 |
super initStyle. |
|
966 |
||
967 |
hilightFrameColor := nil. |
|
968 |
hilightLevel := 0. |
|
969 |
hilightStyle := DefaultHilightStyle. |
|
970 |
highlightMode := #line. |
|
971 |
textStartLeft := 4. |
|
972 |
||
973 |
device hasGrayscales ifTrue:[ |
|
974 |
" |
|
975 |
must get rid of these hard codings |
|
976 |
" |
|
977 |
(hilightStyle == #next) ifTrue:[ |
|
978 |
hilightFgColor := fgColor. |
|
979 |
hilightBgColor := White. |
|
980 |
hilightFrameColor := fgColor |
|
981 |
] ifFalse:[ |
|
982 |
(hilightStyle == #motif) ifTrue:[ |
|
983 |
fgColor := White. |
|
984 |
bgColor := Grey. |
|
985 |
viewBackground := bgColor. |
|
986 |
hilightFgColor := bgColor. |
|
987 |
hilightBgColor := fgColor. |
|
988 |
] ifFalse:[ |
|
989 |
(hilightStyle == #openwin) ifTrue:[ |
|
990 |
hilightFgColor := fgColor. |
|
991 |
hilightBgColor := Color grey. |
|
992 |
] |
|
993 |
] |
|
994 |
] |
|
995 |
]. |
|
996 |
||
997 |
hilightFgColor isNil ifTrue:[ |
|
998 |
hilightFgColor := bgColor. |
|
999 |
]. |
|
1000 |
hilightBgColor isNil ifTrue:[ |
|
1001 |
hilightBgColor := fgColor. |
|
1002 |
]. |
|
1003 |
DefaultForegroundColor notNil ifTrue:[ |
|
1004 |
fgColor := DefaultForegroundColor |
|
1005 |
]. |
|
1006 |
DefaultBackgroundColor notNil ifTrue:[ |
|
1007 |
bgColor := viewBackground := DefaultBackgroundColor |
|
1008 |
]. |
|
1009 |
||
1010 |
DefaultHilightForegroundColor notNil ifTrue:[ |
|
1011 |
hilightFgColor := DefaultHilightForegroundColor |
|
1012 |
]. |
|
1013 |
DefaultHilightBackgroundColor notNil ifTrue:[ |
|
1014 |
hilightBgColor := DefaultHilightBackgroundColor |
|
1015 |
]. |
|
1016 |
DefaultHilightFrameColor notNil ifTrue:[ |
|
1017 |
hilightFrameColor := DefaultHilightFrameColor |
|
1018 |
]. |
|
1019 |
DefaultHilightLevel notNil ifTrue:[ |
|
1020 |
hilightLevel := DefaultHilightLevel |
|
1021 |
]. |
|
1022 |
lineSpacing := 2 * (hilightLevel abs). |
|
1023 |
||
1024 |
(hilightStyle == #motif) ifTrue:[ |
|
1025 |
lineSpacing := lineSpacing max:6. |
|
1026 |
] ifFalse:[ |
|
1027 |
lineSpacing := lineSpacing max:4. |
|
1028 |
]. |
|
1029 |
hilightFgColor isNil ifTrue:[ |
|
1030 |
hilightFgColor := bgColor. |
|
1031 |
hilightBgColor := fgColor |
|
1032 |
]. |
|
1033 |
! |
|
1034 |
||
1035 |
initialize |
|
1036 |
"setup default attributes/behavior |
|
1037 |
" |
|
1038 |
super initialize. |
|
1039 |
||
1040 |
multipleSelectOk := false. |
|
1041 |
useIndex := true. |
|
1042 |
||
1043 |
! |
|
1044 |
||
1045 |
realize |
|
1046 |
"get selection from model; scroll to selection |
|
1047 |
" |
|
1048 |
selection := self getSelectionFromModel. |
|
1049 |
||
1050 |
super realize. |
|
1051 |
||
1052 |
selection notNil ifTrue:[ |
|
1053 |
useIndex ifTrue:[selection := selection copy]. |
|
1054 |
self makeSelectionVisible. |
|
1055 |
] |
|
1056 |
! ! |
|
1057 |
||
1058 |
!SelectionInListModelView methodsFor:'protocol'! |
|
1059 |
||
1399 | 1060 |
drawElementsFrom:start to:stop x:xL y:yT toX:xR |
1390 | 1061 |
"draw the items between start to stop without clearing the background |
1062 |
" |
|
1063 |
|y0 "{ Class:SmallInteger }" |
|
1064 |
y1 "{ Class:SmallInteger }" |
|
1065 |
x0 "{ Class:SmallInteger }" |
|
1066 |
| |
|
1067 |
x0 := textStartLeft - viewOrigin x. |
|
1399 | 1068 |
y1 := yT. |
1390 | 1069 |
|
1070 |
start to:stop do:[:i| |
|
1071 |
y0 := y1. |
|
1072 |
y1 := self yVisibleOfLine:(i + 1). |
|
1399 | 1073 |
self drawLabelAt:i x:x0 y:y0 toX:xR y:y1 |
1390 | 1074 |
]. |
1075 |
||
1076 |
||
1077 |
! |
|
1078 |
||
1458 | 1079 |
lostSynchronisation |
1080 |
"called when the changes derived from the model are faster than the handling |
|
1081 |
" |
|
1082 |
|oldSelect| |
|
1083 |
||
1084 |
oldSelect := selection. |
|
1085 |
selection := nil. |
|
1086 |
||
1087 |
super lostSynchronisation. |
|
1088 |
||
1089 |
oldSelect notNil ifTrue:[ |
|
1090 |
self selectionChanged |
|
1091 |
] |
|
1092 |
||
1093 |
! |
|
1094 |
||
1390 | 1095 |
redrawLabelFromItem:anItem atY:y h:h |
1096 |
"called to redraw a label caused by a selection changed |
|
1097 |
" |
|
1098 |
| |
|
1099 |
x0 "{ Class:SmallInteger }" |
|
1100 |
x1 "{ Class:SmallInteger }" |
|
1101 |
| |
|
1102 |
x0 := textStartLeft // 2 - viewOrigin x. |
|
1103 |
x1 := x0 + textStartLeft + (anItem widthOn:self). |
|
1104 |
x0 := x0 max:margin. |
|
1105 |
x1 := x1 min:(self innerWidth). |
|
1106 |
||
1107 |
x1 > x0 ifTrue:[ |
|
1399 | 1108 |
self redrawX:x0 y:y width:(x1 - x0) height:h. |
1390 | 1109 |
] |
1110 |
! ! |
|
1111 |
||
1112 |
!SelectionInListModelView methodsFor:'selection'! |
|
1113 |
||
1114 |
deselect |
|
1115 |
"clear selection |
|
1116 |
" |
|
1117 |
self selection:nil |
|
1118 |
||
1119 |
! |
|
1120 |
||
1121 |
firstInSelection |
|
1122 |
"returns line number of first element selected or nil |
|
1123 |
" |
|
1124 |
|lineNr| |
|
1125 |
||
1126 |
selection notNil ifTrue:[ |
|
1127 |
^ multipleSelectOk ifTrue:[selection at:1] ifFalse:[selection] |
|
1128 |
]. |
|
1129 |
^ nil |
|
1130 |
||
1131 |
||
1132 |
! |
|
1133 |
||
1134 |
hasSelection |
|
1135 |
"returns true if a selection exists |
|
1136 |
" |
|
1137 |
^ selection notNil |
|
1138 |
||
1139 |
! |
|
1140 |
||
1141 |
isInSelection:aNumber |
|
1142 |
"return true, if line, aNumber is in the selection |
|
1143 |
" |
|
1144 |
selection isNil ifTrue:[^ false]. |
|
1145 |
||
1146 |
^ multipleSelectOk ifFalse:[aNumber == selection] |
|
1147 |
ifTrue:[selection includes:aNumber] |
|
1148 |
! |
|
1149 |
||
1150 |
lastInSelection |
|
1151 |
"returns line number of last element selected or nil |
|
1152 |
" |
|
1153 |
|lineNr| |
|
1154 |
||
1155 |
selection notNil ifTrue:[ |
|
1156 |
^ multipleSelectOk ifTrue:[selection last] ifFalse:[selection] |
|
1157 |
]. |
|
1158 |
^ nil |
|
1159 |
||
1160 |
||
1161 |
! |
|
1162 |
||
1163 |
numberOfSelections |
|
1164 |
"return the number of selected items |
|
1165 |
" |
|
1166 |
selection isNil ifTrue:[^ 0]. |
|
1167 |
^ multipleSelectOk ifFalse:[1] ifTrue:[selection size] |
|
1168 |
||
1169 |
||
1170 |
! |
|
1171 |
||
1172 |
selectElement:anElement |
|
1173 |
"select the element. Scroll to make the new selection visible. |
|
1174 |
Model and/or actionBlock notification IS done. |
|
1175 |
" |
|
1176 |
|index| |
|
1177 |
||
1178 |
(index := self identityIndexOf:anElement) ~~ 0 ifTrue:[ |
|
1179 |
self selection:index |
|
1180 |
] |
|
1181 |
||
1182 |
! |
|
1183 |
||
1184 |
selectedElement |
|
1185 |
"return the single selected item or nil |
|
1186 |
" |
|
1187 |
|index| |
|
1188 |
||
1189 |
index := self selectedIndex. |
|
1190 |
^ index ~~ 0 ifTrue:[self at:index ifAbsent:nil] ifFalse:[nil] |
|
1191 |
||
1192 |
! |
|
1193 |
||
1194 |
selectedIndex |
|
1195 |
"returns the index of the selected line or 0. If more |
|
1196 |
lines are selected, 0 is returned |
|
1197 |
" |
|
1198 |
selection notNil ifTrue:[ |
|
1199 |
multipleSelectOk ifFalse:[^ selection]. |
|
1200 |
selection size == 1 ifTrue:[^ selection at:1] |
|
1201 |
]. |
|
1202 |
^ 0 |
|
1203 |
! |
|
1204 |
||
1205 |
selection |
|
1206 |
"return the selection index or collection of indices |
|
1207 |
in case of multiple selection enabled |
|
1208 |
" |
|
1209 |
^ selection |
|
1210 |
||
1211 |
! |
|
1212 |
||
1213 |
selection:aNumberOrNil |
|
1214 |
"select line, aNumber or deselect if argument is nil; |
|
1215 |
scroll to make the selected line visible. |
|
1216 |
The model and/or actionBlock IS notified. |
|
1217 |
" |
|
1218 |
|oldSelection| |
|
1219 |
||
1220 |
oldSelection := selection. |
|
1221 |
self setSelection:aNumberOrNil. |
|
1222 |
||
1223 |
selection ~= oldSelection ifTrue:[ |
|
1224 |
self selectionChanged |
|
1225 |
] |
|
1226 |
||
1227 |
||
1228 |
! |
|
1229 |
||
1230 |
selectionDo:aBlock |
|
1231 |
"perform aBlock for each nr in the selection. |
|
1232 |
For single selection, it is called once for the items nr. |
|
1233 |
For multiple selections, it is called for each. |
|
1234 |
" |
|
1235 |
selection notNil ifTrue:[ |
|
1236 |
multipleSelectOk ifTrue:[ |
|
1237 |
selection do:aBlock |
|
1238 |
] ifFalse:[ |
|
1239 |
aBlock value:selection |
|
1240 |
]. |
|
1241 |
]. |
|
1242 |
||
1243 |
||
1244 |
! |
|
1245 |
||
1246 |
selectionValue |
|
1247 |
"return the selection value. For multiple selections a collection |
|
1248 |
containing the elements is returned. Otherwise the selected element |
|
1249 |
" |
|
1250 |
selection isNil ifTrue:[ |
|
1251 |
^ multipleSelectOk ifTrue:[#()] ifFalse:[nil] |
|
1252 |
]. |
|
1253 |
multipleSelectOk ifTrue:[ |
|
1254 |
^ selection collect:[:nr | list at:nr ifAbsent:nil ] |
|
1255 |
]. |
|
1256 |
^ list at:selection ifAbsent:nil |
|
1257 |
! |
|
1258 |
||
1259 |
setSelection:aNumberOrNilOrCollection |
|
1260 |
"select line, aNumber or deselect if argument is nil; |
|
1261 |
scroll to make the selected line visible. |
|
1262 |
*** No model and/or actionBlock notification is done here. |
|
1263 |
" |
|
1264 |
self selectWithoutScroll:aNumberOrNilOrCollection. |
|
1265 |
selection notNil ifTrue:[ |
|
1266 |
self makeSelectionVisible |
|
1267 |
] |
|
1268 |
||
1269 |
! |
|
1270 |
||
1271 |
toggleSelection:aNumber |
|
1272 |
"toggle selection-state of entry, aNumber. |
|
1273 |
*** No model and/or actionBlock notification is done here. |
|
1274 |
" |
|
1275 |
aNumber notNil ifTrue:[ |
|
1276 |
(self isInSelection:aNumber) ifTrue:[ |
|
1277 |
self removeFromSelection:aNumber |
|
1278 |
] ifFalse:[ |
|
1279 |
self addToSelection:aNumber |
|
1280 |
] |
|
1281 |
] |
|
1282 |
! ! |
|
1283 |
||
1284 |
!SelectionInListModelView methodsFor:'selection private'! |
|
1285 |
||
1286 |
addToSelection:aNumber |
|
1287 |
"add a number to the selection. No scrolling is done. |
|
1288 |
*** No model and/or actionBlock notification is done here. |
|
1289 |
" |
|
1290 |
|newSelect oldSelect| |
|
1291 |
||
1292 |
(aNumber notNil and:[aNumber between:1 and:(self size)]) ifFalse:[ |
|
1293 |
^ self |
|
1294 |
]. |
|
1295 |
multipleSelectOk ifFalse:[ |
|
1296 |
oldSelect == selection ifTrue:[^ self]. |
|
1297 |
oldSelect := selection. |
|
1298 |
selection := aNumber. |
|
1299 |
oldSelect notNil ifTrue:[self redrawSelectionAt:oldSelect]. |
|
1300 |
] ifTrue:[ |
|
1301 |
selection isNil ifTrue:[ |
|
1302 |
selection := Array with:aNumber. |
|
1303 |
] ifFalse:[ |
|
1304 |
(selection includes:aNumber) ifTrue:[^ self]. |
|
1305 |
selection := selection copyWith:aNumber. |
|
1306 |
]. |
|
1307 |
]. |
|
1308 |
self redrawSelectionAt:aNumber. |
|
1309 |
! |
|
1310 |
||
1311 |
deselectWithoutRedraw |
|
1312 |
"set selection without redraw, scrolling. |
|
1313 |
The model and/or actionBlock IS notified. |
|
1314 |
" |
|
1315 |
selection notNil ifTrue:[ |
|
1316 |
selection := nil. |
|
1317 |
self selectionChanged |
|
1318 |
] |
|
1319 |
! |
|
1320 |
||
1321 |
makeSelectionVisible |
|
1322 |
"scroll to make the selection line visible |
|
1323 |
" |
|
1324 |
|lineNr| |
|
1325 |
||
1326 |
(lineNr := self firstInSelection) notNil ifTrue:[ |
|
1327 |
self scrollToLine:lineNr |
|
1328 |
] |
|
1329 |
||
1330 |
||
1331 |
! |
|
1332 |
||
1333 |
removeFromSelection:aNumber |
|
1334 |
"remove aNumber from the selection and redraw line; |
|
1335 |
*** No model and/or actionBlock notification is done here. |
|
1336 |
" |
|
1337 |
selection notNil ifTrue:[ |
|
1338 |
multipleSelectOk ifTrue:[ |
|
1339 |
(selection includes:aNumber) ifTrue:[ |
|
1340 |
selection size == 1 ifTrue:[ |
|
1341 |
selection := nil |
|
1342 |
] ifFalse:[ |
|
1343 |
selection := selection copyWithout:aNumber |
|
1344 |
]. |
|
1345 |
self redrawSelectionAt:aNumber |
|
1346 |
] |
|
1347 |
] ifFalse:[ |
|
1348 |
aNumber == selection ifFalse:[ |
|
1349 |
selection := nil. |
|
1350 |
self redrawSelectionAt:aNumber |
|
1351 |
] |
|
1352 |
] |
|
1353 |
] |
|
1354 |
||
1355 |
! |
|
1356 |
||
1357 |
selectWithoutScroll:aNumberOrNilOrCollection |
|
1358 |
"select line, aNumber or deselect if argument is nil; |
|
1359 |
scroll to make the selected line visible. |
|
1360 |
*** No model and/or actionBlock notification is done here. |
|
1361 |
" |
|
1362 |
|oldSelect| |
|
1363 |
||
1364 |
oldSelect := selection. |
|
1365 |
selection := self validateSelection:aNumberOrNilOrCollection. |
|
1366 |
||
1367 |
(shown and:[selection ~= oldSelect]) ifFalse:[ |
|
1368 |
^ self |
|
1369 |
]. |
|
1370 |
||
1371 |
multipleSelectOk ifFalse:[ |
|
1372 |
oldSelect notNil ifTrue:[self redrawSelectionAt:oldSelect]. |
|
1373 |
selection notNil ifTrue:[self redrawSelectionAt:selection]. |
|
1374 |
] ifTrue:[ |
|
1375 |
selection isNil ifTrue:[ |
|
1376 |
oldSelect do:[:i|self redrawSelectionAt:i]. |
|
1377 |
] ifFalse:[ |
|
1378 |
oldSelect isNil ifTrue:[ |
|
1379 |
selection do:[:i|self redrawSelectionAt:i]. |
|
1380 |
] ifFalse:[ |
|
1381 |
selection do:[:i| |
|
1382 |
(oldSelect includes:i) ifFalse:[self redrawSelectionAt:i] |
|
1383 |
]. |
|
1384 |
oldSelect do:[:i| |
|
1385 |
(selection includes:i) ifFalse:[self redrawSelectionAt:i] |
|
1386 |
]. |
|
1387 |
] |
|
1388 |
] |
|
1389 |
]. |
|
1390 |
||
1391 |
||
1392 |
||
1393 |
! |
|
1394 |
||
1395 |
selectionWithoutRedraw:aSelection |
|
1396 |
"set selection without redraw, scrolling. |
|
1397 |
The model and/or actionBlock IS notified. |
|
1398 |
" |
|
1399 |
selection ~= aSelection ifTrue:[ |
|
1400 |
selection := aSelection. |
|
1401 |
self selectionChanged |
|
1402 |
] |
|
1403 |
! |
|
1404 |
||
1405 |
validateSelection:aNumberOrCollection |
|
1406 |
"validate the selection; returns a valid selection or nil |
|
1407 |
" |
|
1408 |
|sz newSelection| |
|
1409 |
||
1410 |
(aNumberOrCollection notNil and:[aNumberOrCollection ~~ 0]) ifTrue:[ |
|
1411 |
sz := self size. |
|
1412 |
||
1413 |
aNumberOrCollection isCollection ifFalse:[ |
|
1414 |
(aNumberOrCollection between:1 and:sz) ifTrue:[ |
|
1415 |
^ multipleSelectOk ifFalse:[aNumberOrCollection ] |
|
1416 |
ifTrue:[Array with:aNumberOrCollection] |
|
1417 |
] |
|
1418 |
] ifTrue:[ |
|
1419 |
(aNumberOrCollection notNil and:[multipleSelectOk]) ifTrue:[ |
|
1420 |
newSelection := OrderedCollection new. |
|
1421 |
||
1422 |
aNumberOrCollection do:[:anIndex| |
|
1423 |
(anIndex between:1 and:sz) ifFalse:[^ nil]. |
|
1424 |
newSelection add:anIndex. |
|
1425 |
]. |
|
1426 |
^ newSelection |
|
1427 |
] |
|
1428 |
] |
|
1429 |
]. |
|
1430 |
^ nil |
|
1431 |
||
1432 |
||
1433 |
! ! |
|
1434 |
||
1435 |
!SelectionInListModelView class methodsFor:'documentation'! |
|
1436 |
||
1437 |
version |
|
1508
9c54f2e53da1
check whether #at:ifAbsent returns nil.
Claus Gittinger <cg@exept.de>
parents:
1468
diff
changeset
|
1438 |
^ '$Header: /cvs/stx/stx/libwidg2/SelectionInListModelView.st,v 1.12 1999-09-08 15:46:27 cg Exp $' |
1390 | 1439 |
! ! |
1409 | 1440 |
SelectionInListModelView initialize! |