ImageView.st
changeset 2158 c4389029e303
parent 2099 64402b3555eb
child 2160 4c15f05f8bc6
--- a/ImageView.st	Thu Aug 22 18:55:47 2002 +0200
+++ b/ImageView.st	Fri Aug 23 10:48:59 2002 +0200
@@ -13,7 +13,7 @@
 "{ Package: 'stx:libwidg2' }"
 
 View subclass:#ImageView
-	instanceVariableNames:'image adjust imageChannel tileMode tileOffset'
+	instanceVariableNames:'image magnifiedImage adjust imageChannel tileMode tileOffset'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Views-Misc'
@@ -55,6 +55,15 @@
         ImageView openOnImage:(Image fromFile:'bitmaps/gifImages/garfield.gif')
         ImageView openOnImage:(Image fromFile:'bitmaps/SBrowser.xbm')
 
+    adjust:     
+        controls how images are displayed;
+        can be one of:
+            #topLeft    - image is dislayed as usual
+            #center     - image is shown centered
+            #fitBig     - big images are shrunk to make it fit the view
+            #fitSmall   - small images are magnified to make it fit the view,
+            #fit        - all images are magnified to fit the view
+
     [author:]
         Claus Gittinger
 
@@ -186,9 +195,18 @@
 
 adjust:layoutSymbol
     "set the adjust (how the image is displayed);
-     currently, only support #topLeft and #center"
+     currently, only support #topLeft, #center, #fitBig, #fitSmall and #fit"
+
+    adjust := layoutSymbol.
+
+    magnifiedImage := nil.
 
-    adjust := layoutSymbol
+    self shown ifTrue:[
+        image notNil ifTrue:[
+            self invalidate.
+            self contentsChanged.
+        ]
+    ].
 !
 
 image
@@ -213,23 +231,24 @@
     oldSize := image ifNil:[0@0] ifNotNil:[image extent].
 
     image := anImage.
+    self generateMagnifiedImage.
 
-    newSize := image ifNil:[0@0] ifNotNil:[image extent].
+    newSize := image ifNil:[0@0] ifNotNil:[(magnifiedImage ? image) extent].
     oldSize ~= newSize ifTrue:[
         self contentsChanged.
     ].
 
     shown ifTrue:[
-        self withWaitCursorDo:[
-            (image notNil and: [image device ~~ device]) ifTrue:[
-                devImage := image onDevice:device.
-                devImage ~~ image ifTrue:[
-                    image := devImage.
-                ].
-            ].
+"/        self withWaitCursorDo:[
+"/            (image notNil and: [image device ~~ device]) ifTrue:[
+"/                devImage := image onDevice:device.
+"/                devImage ~~ image ifTrue:[
+"/                    image := devImage.
+"/                ].
+"/            ].
             self clear.
             self invalidate
-        ].
+"/        ].
     ].
     self changed:#image.
 
@@ -280,12 +299,48 @@
 
 !ImageView methodsFor:'drawing'!
 
+generateMagnifiedImage
+    |doFit innerWidth innerHeight imgWidth imgHeight|
+
+    magnifiedImage := nil.
+    doFit := false.
+
+    innerWidth := self innerWidth.
+    innerHeight := self innerHeight.
+
+    imgWidth := image width.
+    imgHeight := image height.
+
+    tileMode ~~ true ifTrue:[
+        ((imgWidth > innerWidth)
+        or:[imgHeight > innerHeight]) ifTrue:[
+            ((adjust == #fit) or:[adjust == #fitBig]) ifTrue:[
+                doFit := true
+            ].
+        ] ifFalse:[
+            ((imgWidth < innerWidth)
+            or:[imgHeight < innerHeight]) ifTrue:[
+                ((adjust == #fit) or:[adjust == #fitSmall]) ifTrue:[
+                    doFit := true
+                ].
+            ]
+        ].
+    ].
+    doFit ifTrue:[
+        magnifiedImage := image magnifiedPreservingRatioTo:(innerWidth @ innerHeight).
+        self contentsChanged.
+    ].
+!
+
 redrawX:x y:y width:w height:h
-    |xI yI depth imgWidth imgHeight right bott devImage|
+    |xI yI depth shownImage imgWidth imgHeight right bott|
 
     image notNil ifTrue:[
-        imgWidth := image width.
-        imgHeight := image height.
+        self generateMagnifiedImage.
+
+        shownImage := magnifiedImage ? image.
+        imgWidth := shownImage width.
+        imgHeight := shownImage height.
 
         adjust == #center ifTrue:[
             xI := (width - (margin * 2) - imgWidth) // 2.
@@ -294,20 +349,9 @@
             xI := yI := margin
         ].
 
-        ((depth := image depth) == 1) ifTrue:[
-            self paint:(image colorFromValue:1)
-                    on:(image colorFromValue:0).
-        ].
-
-        Object errorSignal handle:[:ex |
-            Transcript showCR:'cannot convert image: ', ex description.
-            ^ self.
-        ] do:[
-            devImage := image onDevice:device.
-            devImage ~~ image ifTrue:[
-                image := devImage.
-                self changed:#image.
-            ].
+        ((depth := shownImage depth) == 1) ifTrue:[
+            self paint:(shownImage colorFromValue:1)
+                    on:(shownImage colorFromValue:0).
         ].
 
         tileMode == true ifTrue:[
@@ -328,11 +372,11 @@
             ].
         ] ifFalse:[
             (depth == 1
-            and:[image mask isNil]) ifFalse:[
+            and:[shownImage mask isNil]) ifFalse:[
                 self clearRectangleX:x y:y width:w height:h.
-                self displayForm:image x:xI y:yI 
+                self displayForm:shownImage x:xI y:yI 
             ] ifTrue:[
-                self displayOpaqueForm:image x:xI y:yI 
+                self displayOpaqueForm:shownImage x:xI y:yI 
             ].
 
             "/ right of image ?
@@ -358,6 +402,17 @@
     "Modified: / 12.8.1998 / 14:02:28 / cg"
 ! !
 
+!ImageView methodsFor:'event handling'!
+
+sizeChanged:how
+    magnifiedImage notNil ifTrue:[
+        self clear.
+        self invalidate.
+        magnifiedImage := nil.
+    ].
+    super sizeChanged:how
+! !
+
 !ImageView methodsFor:'initialize / release'!
 
 destroy
@@ -371,18 +426,18 @@
     "return the images height - scrollbar needs this info"
 
     image isNil ifTrue:[^ 0].
-    ^ image height
+    ^ (magnifiedImage ? image) height
 !
 
 widthOfContents
     "return the images width - scrollbar needs this info"
 
     image isNil ifTrue:[^ 0].
-    ^ image width
+    ^ (magnifiedImage ? image) width
 ! !
 
 !ImageView class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/ImageView.st,v 1.47 2002-05-06 07:31:53 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/ImageView.st,v 1.48 2002-08-23 08:48:59 cg Exp $'
 ! !