center label, icon and image in the display routine
authortz
Wed, 04 Feb 1998 18:52:06 +0100
changeset 726 4b3a40124f6d
parent 725 0e051202859d
child 727 3ba54333342a
center label, icon and image in the display routine
LabelAndIcon.st
--- a/LabelAndIcon.st	Wed Feb 04 00:17:51 1998 +0100
+++ b/LabelAndIcon.st	Wed Feb 04 18:52:06 1998 +0100
@@ -184,14 +184,17 @@
 !LabelAndIcon class methodsFor:'instance creation'!
 
 form:aForm image:anImage
+
     ^self new form:aForm image:anImage string:''
 !
 
 form:aForm image:anImage string:aString
+
     ^self new form:aForm image:anImage string:aString
 !
 
 icon:anIcon string:aString
+
     ^ self new icon:anIcon string:aString
 
     "Created: 12.5.1996 / 20:00:58 / cg"
@@ -285,86 +288,42 @@
 displayOn:aGC x:x y:y opaque:opaque
     "display the receiver on a GC"
 
-    |yOffs yGap font fH iH fA yI xOffs|
+    |y1 x1 iconHeight imageHeight stringHeight maxHeight gapY asc|
+
+    gapY := (aGC device pixelPerMillimeter x) rounded.
+    asc  := (aGC font on:aGC device) ascent.
 
-    font := aGC font on:aGC device.
-    fH := font height.
-    icon isNil ifTrue:[
-        iH := 0.
-        yGap := 0.
-        xOffs := 0.
-    ] ifFalse:[
-        yGap := (aGC device pixelPerMillimeter x rounded).
-        iH := icon height + yGap.
-        xOffs := icon width + gap.
-    ].
-    fA := font ascent.
+    iconHeight   := icon notNil     ifTrue:[icon   height]       ifFalse:[0].
+    imageHeight  := image notNil    ifTrue:[image  height]       ifFalse:[0].
+    stringHeight := string size > 0 ifTrue:[string heightOn:aGC] ifFalse:[0].
 
-    yOffs := 0.
+    maxHeight := ((iconHeight max: imageHeight) + gapY) max: stringHeight.
+    x1 := x.
 
     icon notNil ifTrue:[
-
-"/ #ifdef OLD
-"/        iH <= fA ifTrue:[
-"/            "/ place the icon on the baseLine
-"/            yI := y-fA.
-"/ #else NEW
+        y1 := y - asc + (maxHeight - iconHeight + 1 // 2).
+        icon := icon onDevice:aGC device.
 
-        iH <= fH ifTrue:[
-            "/ place the icon centered
-            yI := (fH-iH)//2 + y - fA + (yGap).
-"/ #endif NEW
-        ] ifFalse:[
-            iH := iH - yGap. yGap := 0. 
-            iH <= fH ifTrue:[
-                "/ place the icon centered - no gap
-                yI := (fH-iH)//2 + y - fA + (yGap).
-            ] ifFalse:[
-                yI := y-(fA+(font descent // 2)).
-                yOffs := (iH - fH) // 2
-            ]
-        ].
+        (opaque and:[icon mask isNil]) ifTrue:[aGC displayOpaqueForm:icon x:x1 y:y1]
+                                      ifFalse:[aGC displayForm:icon x:x1 y:y1].
 
-        icon := icon onDevice:aGC device.
-        (opaque 
-        and:[icon mask isNil]) ifTrue:[
-            aGC displayOpaqueForm:icon x:x y:yI.
-        ] ifFalse:[
-            aGC displayForm:icon x:x y:yI. 
-        ].
+        x1 := x1 + icon width + gap
     ].
 
     image notNil ifTrue:[
-        iH := image height + yGap.
-        iH <= fH ifTrue:[
-            "/ place the icon centered
-            yI := (fH-iH)//2 + y - fA + (yGap).
-        ] ifFalse:[
-            iH := iH - yGap. yGap := 0. 
-            iH <= fH ifTrue:[
-                "/ place the icon centered - no gap
-                yI := (fH-iH)//2 + y - fA + (yGap).
-            ] ifFalse:[
-                yI := y-(fA+(font descent // 2)).
-                yOffs := (iH - fH) // 2
-            ]
-        ].
+        y1 := y - asc + (maxHeight - imageHeight + 1 // 2).
+        image := image onDevice:aGC device.
+
+        (opaque and:[image mask isNil]) ifTrue:[aGC displayOpaqueForm:image x:x1 y:y1]
+                                       ifFalse:[aGC displayForm:image x:x1 y:y1].
 
-        image := image onDevice:aGC device.
-        (opaque 
-        and:[image mask isNil]) ifTrue:[
-            aGC displayOpaqueForm:image x:x + xOffs y:yI.
-        ] ifFalse:[
-            aGC displayForm:image x:x + xOffs y:yI. 
-        ].  
-        xOffs := xOffs + image width + gap.
+        x1 := x1 + image width + gap
     ].
-    string 
-        displayOn:aGC 
-        x:x+xOffs
-        y:y+yOffs.
 
-    "Modified: / 29.10.1997 / 03:33:29 / cg"
+    stringHeight ~~ 0 ifTrue:[
+        y1 := y + (maxHeight - stringHeight + 1 // 2).
+        string displayOn:aGC x:x1 y:y1
+    ]    
 ! !
 
 !LabelAndIcon methodsFor:'initialization'!
@@ -397,7 +356,7 @@
     ] ifFalse:[
         stringHeight := 0   
     ].
-    ^ (iconHeight max: imageHeight) max: stringHeight
+    ^ ((iconHeight max: imageHeight) max: stringHeight)
 !
 
 widthOn:aGC
@@ -422,5 +381,5 @@
 !LabelAndIcon class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/LabelAndIcon.st,v 1.19 1998-02-03 17:46:39 tz Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/LabelAndIcon.st,v 1.20 1998-02-04 17:52:06 tz Exp $'
 ! !