ImageView.st
author Claus Gittinger <cg@exept.de>
Sun, 02 Nov 1997 18:36:44 +0100
changeset 599 66c4da764a9e
parent 430 574a94732ece
child 635 915077fd17ab
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

View subclass:#ImageView
	instanceVariableNames:'image adjust'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-Misc'
!

!ImageView class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    This View knows how to display an image (or form).

    You can display an image with:

        ImageView openOn:anImageFileName
    or:
        ImageView openOnImage:anImage
    or:
        ImageView new image:anImage

    i.e.

        ImageView openOn:'bitmaps/gifImages/garfield.gif'
        ImageView openOn:'bitmaps/SBrowser.xbm'

        ImageView openOnImage:(Image fromFile:'bitmaps/gifImages/garfield.gif')
        ImageView openOnImage:(Image fromFile:'bitmaps/SBrowser.xbm')

    [author:]
        Claus Gittinger

    [see also:]
        Image Form
"
! !

!ImageView class methodsFor:'startup'!

openOn:aFileName
    "startup an image viewer on an image read from a file"

    |imageView img e|

    img := Image fromFile:aFileName.
    img isNil ifTrue:[
        aFileName asFilename exists ifTrue:[
            e := 'unknown/unsupported image format'
        ] ifFalse:[
            e := 'no such image'.
        ].
        self warn:e.
        ^ nil
    ].
    imageView := self openOnImage:img.

    imageView topView label:aFileName iconLabel:(aFileName asFilename baseName).

    ^ imageView

    "
     ImageView openOn:'bitmaps/gifImages/garfield.gif'
     ImageView openOn:'bitmaps/xpmBitmaps/BOOK.xpm'
    "

    "Modified: / 31.10.1997 / 16:17:52 / cg"
!

openOnImage:anImage
    "startup an image viewer on an image"

    |top v imageView icnW icnH iconView magX magY mag lbl|

    anImage isImage ifTrue:[
        lbl := 'an Image'
    ] ifFalse:[
        lbl := 'a Form'
    ].
    top := StandardSystemView label:lbl.

    v := HVScrollableView for:self in:top.
    v origin:0@0 extent:1.0@1.0. 
    imageView := v scrolledView.

    anImage notNil ifTrue:[
        imageView image:anImage.

        "define an icon view showing a little version of image.
         Since some window managers cannot handle this correctly (twm),
         this is only done when running on an IRIS"

        (true "(OperatingSystem getSystemType = 'iris')" 
        and:[StyleSheet name == #iris]) ifTrue:[
            iconView := ImageView new.

            "for now; should somehow get access to preferred iconview extent ..."
            icnW := 86.
            icnH := 68.

            ((anImage width <= icnW) and:[anImage height <= icnH]) ifTrue:[
                iconView extent:(anImage width @ anImage height).
                mag := 1 @ 1
            ] ifFalse:[
                magX := icnW / anImage width.
                magY := icnH / anImage height.

                "scale image"
"
                mag := magX @ magY.
"
                "preserve ratio"
" 
                mag := (magX min:magY) asPoint.
" 
" "
                mag := (magX max:magY) asPoint.
" "

                iconView extent:((anImage width @ anImage height) * mag) rounded.
            ].

            top iconView:iconView.
        ].
    ].

    top open.

    iconView notNil ifTrue:[
        top windowGroup addView:iconView.
        [ 
            iconView image:(anImage magnifyBy:mag).
        ] forkAt:4
    ].
    ^ imageView

    "
     ImageView openOnImage:(Image fromFile:'bitmaps/gifImages/garfield.gif')
     ImageView openOnImage:(Image fromFile:'bitmaps/SBrowser.xbm')
    "

    "Modified: / 31.10.1997 / 16:37:36 / cg"
! !

!ImageView methodsFor:'accessing'!

adjust:layoutSymbol
    "set the adjust (how the image is displayed);
     currently, only support #topLeft and #center"

    adjust := layoutSymbol
!

image
    "return the image"

    ^ image
!

image:anImage
    "set the image - show a wait cursor, since image dithering may take a while"

    image := anImage.
    anImage notNil ifTrue:[
        self cursor:Cursor wait.
        shown ifTrue:[
            self clear.
            self invalidate
        ].
        self contentsChanged.
        self cursor:(Cursor normal).
    ].

    "
     ImageView new realize image:(Image fromFile:'bitmaps/claus.gif')

     |f|
     f := Image fromFile:'bitmaps/SBrowser.xbm'.
     f colorMap:(Array with:Color red with:Color yellow).
     ImageView new realize image:f
    "

    "Modified: 17.3.1997 / 12:13:30 / cg"
! !

!ImageView methodsFor:'drawing'!

redrawX:x y:y width:w height:h
    |xI yI depth ext|

    image notNil ifTrue:[
        adjust == #center ifTrue:[
            ext := image extent.
            xI := (width - (margin * 2) - (ext x)) // 2.
            yI := (height - (margin * 2) - (ext y)) // 2.
        ] ifFalse:[
            xI := yI := margin
        ].

        image device ~~ device ifTrue:[
            "/ this may take some time (allocating colors & dithering

            self displayRectangleX:x y:y width:w height:h.
            self withWaitCursorDo:[
                image := image on:device.
            ]
        ].

        ((depth := image depth) == 1) ifTrue:[
            self paint:(image colorFromValue:1)
                    on:(image colorFromValue:0).
        ].

        (depth ~~ 1
        and:[image mask notNil]) ifTrue:[
            self clearRectangleX:x y:y width:w height:h.
            self displayForm:image x:xI y:yI 
        ] ifFalse:[
            self displayOpaqueForm:image x:xI y:yI 
        ].

        "/ right of image ?
        (x + w - 1) > (xI + image width) ifTrue:[
            self clearRectangleX:(xI + image width)
                               y:y
                           width:(x + w - 1 - image width - xI)
                          height:h  
        ].
        "/ below of image ?
        (y + h - 1) > (yI + image height) ifTrue:[
            self clearRectangleX:margin
                               y:(yI + image height)
                           width:w
                          height:(y + h - 1 - image height - yI)  
        ].
    ]

    "Created: 11.7.1996 / 21:02:12 / cg"
    "Modified: 23.6.1997 / 21:27:42 / cg"
! !

!ImageView methodsFor:'queries'!

heightOfContents
    "return the images height - scrollbar needs this info"

    image isNil ifTrue:[^ 0].
    ^ image height
!

widthOfContents
    "return the images width - scrollbar needs this info"

    image isNil ifTrue:[^ 0].
    ^ image width
! !

!ImageView methodsFor:'release'!

destroy
    image := nil.
    super destroy.
! !

!ImageView class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg2/ImageView.st,v 1.32 1997-11-02 17:36:44 cg Exp $'
! !