ImageView.st
author claus
Tue, 27 Jun 1995 04:25:33 +0200
changeset 63 e560d8279473
parent 62 378b60ba1200
child 64 10910b8b003a
permissions -rw-r--r--
.

"
 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.
"

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

ImageView comment:'
COPYRIGHT (c) 1993 by Claus Gittinger
	      All Rights Reserved

$Header: /cvs/stx/stx/libwidg2/ImageView.st,v 1.11 1995-06-27 02:25:10 claus Exp $
'!

!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/garfield.gif'
	ImageView openOn:'bitmaps/SBrowser.xbm'

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

!ImageView class methodsFor:'startup'!

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

    |top img|

    img := Image fromFile:aFileName.
    img isNil ifTrue:[
	self error:'no such image'.
	^ nil
    ].
    top := self openOnImage:img.

    top label:aFileName.
    top iconLabel:aFileName asFilename baseName.

    ^ top

    "
     ImageView openOn:'bitmaps/garfield.gif'
     ImageView openOn:'/phys/porty/tmp/img.pcx'
    "
!

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

    |top v imageView img icnW icnH iconView magX magY mag wg 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 := self 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
    ].
    ^ top

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

!ImageView methodsFor:'release'!

destroy
    image := nil.
    super destroy.
! !

!ImageView methodsFor:'drawing'!

redraw
    |clrMap|

    image notNil ifTrue:[
	image := image on:device.
	self clear.
	(image depth == 1) ifTrue:[
	    clrMap := image colorMap.
	    clrMap notNil ifTrue:[
		self paint:(clrMap at:2) on:(clrMap at:1).
	    ] ifFalse:[
		self paint:Color black on:Color white.
	    ]
	].
	self displayOpaqueForm:image x:0 y:0
    ]
! !

!ImageView methodsFor:'accessing'!

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 redraw
	].
	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
    "
!

image
    "return the image"

    ^ image
! !

!ImageView methodsFor:'queries'!

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

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

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

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