SelectionInListModelView.st
changeset 1554 c45cac3c4d31
parent 1547 f72d75aaec14
child 1557 e3feb8bc17b0
--- a/SelectionInListModelView.st	Wed Sep 22 11:35:19 1999 +0200
+++ b/SelectionInListModelView.st	Wed Sep 22 12:08:52 1999 +0200
@@ -261,13 +261,16 @@
      depending on the setting of useIndex, this is either the numeric
      index of the selection or the value (i.e. the string)
     "
-    useIndex ifFalse:[
-        ^ self selectionValue
+    selection isNil ifTrue:[
+        multipleSelectOk ifTrue:[^ #()].
+        ^ useIndex ifTrue:[0] ifFalse:[nil]
     ].
-    selection isNil ifTrue:[
-        ^ multipleSelectOk ifTrue:[#()] ifFalse:[0]
+    useIndex ifTrue:[^ selection].
+
+    multipleSelectOk ifFalse:[
+        ^ list at:selection ifAbsent:nil
     ].
-  ^ multipleSelectOk ifTrue:[selection copy] ifFalse:[selection]
+    ^ selection collect:[:nr| list at:nr ifAbsent:nil ]
 !
 
 getSelectionFromModel
@@ -275,6 +278,11 @@
     "
     |value nsel|
 
+    model isNil ifTrue:[
+        ^ nil
+    ].
+    self syncFlushEvent:#setSelection:.
+
     ((value := model value) isNil or:[value == 0]) ifTrue:[
         ^ nil
     ].
@@ -389,7 +397,7 @@
 selectionChanged
     "selection has changed. Call actionblock and/or send changeMessage if defined
     "
-    |value arg|
+    |arg|
 
     (model isNil and:[actionBlock isNil]) ifTrue:[
         ^ self
@@ -414,19 +422,25 @@
 !
 
 update:something with:aParameter from:changedObject
-    "one of my models changed
+    "check wether my model changed
     "
     |newSelection|
 
     changedObject == model ifTrue:[
-        newSelection := self getSelectionFromModel.
-
-        newSelection ~= selection ifTrue:[
-            self pushEvent:#setSelection: with:newSelection
-        ]
+        self syncPushEvent:#updateFromModel
     ] ifFalse:[
         super update:something with:aParameter from:changedObject
     ].
+
+!
+
+updateFromModel
+    "update selection from the model
+    "
+    model notNil ifTrue:[
+        self syncFlushEvent:#updateFromModel.
+        self setSelection:(self getSelectionFromModel).
+    ]
 ! !
 
 !SelectionInListModelView methodsFor:'drag & drop'!
@@ -487,62 +501,25 @@
 drawFrom:start to:stop x:x y:y w:w
     "draw the lines between start to stop without clearing the background
     "
-    |selY selH
-     y0       "{ Class:SmallInteger }"
-     y1       "{ Class:SmallInteger }"
-     hg       "{ Class:SmallInteger }"
-    |
-    highlightMode == #line ifTrue:[
-        "/ redraw the background for all selected lines in the invalid range
-
-        self selectionDo:[:lnNr|
-            (lnNr between:start and:stop) ifTrue:[
-                selY isNil ifTrue:[
-                    selY := OrderedCollection new.
-                    selH := OrderedCollection new.
-                    self paint:hilightBgColor.
-                ].
-                y0 := self yVisibleOfLine:lnNr.
-                y1 := self yVisibleOfLine:(lnNr + 1).
-                hg := y1 - y0.
-                selY add:y0.
-                selH add:hg.
-                self fillRectangleX:x y:y0 width:w height:hg.
-            ]
+    self selectionDo:[:lnNr|
+        (lnNr between:start and:stop) ifTrue:[
+            self drawSelectionFrameAt:lnNr x:x w:w
         ]
     ].
-
     self drawElementsFrom:start to:stop x:x y:y w:w.
 
-    "/ draw selection frames
-    selY notNil ifTrue:[
-        1 to:selY size do:[:i|
-            self drawSelectionFrameAtX:x y:(selY at:i) width:w h:(selH at:i)
-        ]
-    ].
-
-
 
 !
 
 drawLabelAt:anIndex x:x y:y h:h
     "draw the label at position x/y without clearing the background
     "
-    |label item
-     w  "{ Class:SmallInteger }"
-     x0 "{ Class:SmallInteger }"
-    |
+    |item|
+
     item := list at:anIndex ifAbsent:nil.
     item isNil ifTrue:[^ self].
 
     (self isInSelection:anIndex) ifTrue:[
-        highlightMode == #label ifTrue:[
-            w  := (item widthOn:self) + textStartLeft.
-            x0 := x - (textStartLeft // 2).
-            self paint:hilightBgColor.
-            self fillRectangleX:x0 y:y width:w height:h.
-            self drawSelectionFrameAtX:x0 y:y width:w h:h.
-        ].
         self paint:hilightFgColor on:hilightBgColor
     ] ifFalse:[
         self paint:fgColor on:bgColor.
@@ -552,15 +529,46 @@
 
 !
 
-drawSelectionFrameAtX:x0 y:y0 width:w h:h
-    "redraw selection frame for a line
+drawSelectionFrameAt:lnNr x:x w:w
+    "draw the background and foreground of the selection frame
+     at a lineNr.
     "
-    |
+    |item
+     x0 "{ Class:SmallInteger }"
      x1 "{ Class: SmallInteger }"
-     x  "{ Class: SmallInteger }" 
-     y  "{ Class: SmallInteger }" 
+     y0 "{ Class:SmallInteger }"
+     y1 "{ Class:SmallInteger }"
+     hL "{ Class:SmallInteger }"
+     wL "{ Class:SmallInteger }"
     |
-    x1 := x0 + w.
+    (item := list at:lnNr ifAbsent:nil) isNil ifTrue:[
+        ^ self
+    ].
+
+    "/ CLEAR THE BACKGROUND
+
+    y0 := self yVisibleOfLine:lnNr.
+    y1 := self yVisibleOfLine:(lnNr + 1).
+    hL := y1 - y0.
+
+    highlightMode == #line ifTrue:[
+        x0 := x.
+        x1 := x0 + w.
+    ] ifFalse:[
+        highlightMode == #label ifFalse:[^ self].
+        x0 := (self xVisibleOfItem:item) - (textStartLeft // 2).
+        x1 := x0 + (item widthOn:self) + textStartLeft.
+    ].
+    x0 := x0 max:(x - 1).
+    x1 := x1 min:width.
+    wL := x1 - x0.
+
+    wL > 0 ifFalse:[^ self].
+
+    self paint:hilightBgColor.
+    self fillRectangleX:x0 y:y0 width:wL height:hL.
+
+    "/ DRAW THE FRAME
 
     hilightFrameColor notNil ifTrue:[
         hilightLevel == 0 ifTrue:[
@@ -568,23 +576,23 @@
 
             highlightMode == #line ifTrue:[
                 self displayLineFromX:x0 y:y0 toX:x1 y:y0.
-                y := y0 + h - 1.
-                self displayLineFromX:x0 y:y toX:x1 y:y.
+                y1 := y0 + hL - 1.
+                self displayLineFromX:x0 y:y1 toX:x1 y:y1.
             ] ifFalse:[
-                self displayRectangleX:x0 y:y0 width:w height:h
+                self displayRectangleX:x0 y:y0 width:wL height:hL
             ].
             ^ self.
         ]
     ] ifFalse:[
         hilightStyle == #motif ifTrue:[
             self paint:bgColor.
-            y := y0 + 1.
+            y1 := y0 + 1.
             highlightMode == #line ifTrue:[
-                self displayLineFromX:x0 y:y toX:x1 y:y.
-                y := y0 + h - 2.
-                self displayLineFromX:x0 y:y toX:x1 y:y.
+                self displayLineFromX:x0 y:y1 toX:x1 y:y1.
+                y1 := y0 + hL - 2.
+                self displayLineFromX:x0 y:y1 toX:x1 y:y1.
             ] ifFalse:[
-                self displayRectangleX:x0 + 1 y:y width:w - 2 height:h - 2
+                self displayRectangleX:x0 + 1 y:y1 width:wL - 2 height:hL - 2
             ]
         ]
     ].
@@ -592,33 +600,18 @@
     hilightLevel ~~ 0 ifTrue:[
         "/ draw edge
         highlightMode == #line ifTrue:[
-            x  := margin.
-            x1 := width - x - x.
-        ] ifFalse:[
-            x  := x0.
-            x1 := w.
+            x0 := margin.
+            wL := width - x0 - x0.
         ].
-        self drawEdgesForX:x y:y0 width:x1 height:h level:hilightLevel.
+        self drawEdgesForX:x0 y:y0 width:wL height:hL level:hilightLevel.
     ]
 
 !
 
-redrawSelectionAt:anIndex
+redrawSelectionAt:aLineNr
     "called to redraw a line caused by a change of the selection
     "
-    |
-     h  "{ Class:SmallInteger }"
-     y0 "{ Class:SmallInteger }"
-     y1 "{ Class:SmallInteger }"
-    |
-    shown ifTrue:[
-        y0 := (self yVisibleOfLine:anIndex)       max:margin.
-        y1 := (self yVisibleOfLine:(anIndex + 1)) min:(height - margin).
-
-        (h := y1 - y0) > 0 ifTrue:[
-            self redrawX:margin y:y0 width:(self innerWidth) height:h
-        ]
-    ]
+    self redrawLineAt:aLineNr.
 ! !
 
 !SelectionInListModelView methodsFor:'event handling'!
@@ -1001,7 +994,7 @@
 realize
     "get selection from model; scroll to selection
     "
-    selection := self getSelectionFromModel.
+    self updateFromModel.
     super realize.
     self makeSelectionVisible.
 
@@ -1033,6 +1026,14 @@
     "
     self deselectWithoutRedraw.
     super lostSynchronisation.
+!
+
+xVisibleOfItem:anItem
+    "returns the visible x of the labeled text
+    "
+    ^ textStartLeft - viewOrigin x
+
+
 ! !
 
 !SelectionInListModelView methodsFor:'selection'!
@@ -1144,19 +1145,6 @@
 
 !
 
-selectionValue
-    "return the selection value. For multiple selections a collection
-     containing the elements is returned. Otherwise the selected element
-    "
-    selection isNil ifTrue:[
-        ^ multipleSelectOk ifTrue:[#()] ifFalse:[nil]
-    ].
-    multipleSelectOk ifTrue:[
-         ^ selection collect:[:nr | list at:nr ifAbsent:nil ]
-    ].
-    ^ list at:selection ifAbsent:nil
-!
-
 setSelection:something
     "select something or deselect if the argument is nil;
      scroll to make the selected line visible.
@@ -1222,11 +1210,12 @@
     "
     |lineNr|
 
-    (lineNr := self firstInSelection) notNil ifTrue:[
-        self scrollToLine:lineNr
+    selection notNil ifTrue:[
+        (lineNr := self firstInSelection) notNil ifTrue:[
+            self scrollToLine:lineNr
+        ]
     ]
 
-
 !
 
 removeFromSelection:aNumber
@@ -1261,6 +1250,8 @@
     "
     |oldSelect|
 
+    selection = something ifTrue:[^ false].
+
     oldSelect := selection.
 
     (something isNil or:[something == 0]) ifTrue:[
@@ -1303,6 +1294,6 @@
 !SelectionInListModelView class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/SelectionInListModelView.st,v 1.17 1999-09-21 18:13:04 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/SelectionInListModelView.st,v 1.18 1999-09-22 10:08:16 cg Exp $'
 ! !
 SelectionInListModelView initialize!