FramedBox.st
author claus
Mon, 06 Feb 1995 01:53:30 +0100
changeset 77 565b052f5277
parent 70 14443a9ea4ec
child 118 3ee5ea99d0e2
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1991 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:#FramedBox
	 instanceVariableNames:'label layout fgColor showFrame frame3D'
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Views-Layout'
!

FramedBox comment:'
COPYRIGHT (c) 1991 by Claus Gittinger
	      All Rights Reserved

$Header: /cvs/stx/stx/libwidg/FramedBox.st,v 1.9 1994-11-28 21:05:00 claus Exp $
'!

!FramedBox class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1991 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.
"
!

version
"
$Header: /cvs/stx/stx/libwidg/FramedBox.st,v 1.9 1994-11-28 21:05:00 claus Exp $
"
!

documentation
"
    a frame around something. The frame may have a label, whose position
    is controlled by the layout variable, aSymbol which may be one of:
    [#topCenter #topLeft #topRight #bottomLeft #bottomCenter #bottomRight]

    Its also possible, to not show the frame but only the label, by setting
    showFrame to false.
"
! !

!FramedBox methodsFor:'private'!

redrawIfShown
    shown ifTrue:[
	self clear.
	self redraw
    ]
! !

!FramedBox methodsFor:'accessing'!

foregroundColor
    "return the frame labels foreground color"

    ^ fgColor
!

foregroundColor:aColor
    "set the frame labels foreground color"

    aColor ~= fgColor ifTrue:[
	fgColor := aColor.
	self redrawIfShown
    ]
!

frameShown
    "return true, if frame is shown;
     if false, oly the label is shown"

    ^ showFrame
!

showFrame:aBoolean
    "turn on/off showing of the frame -
     without a frame, only the label is shown at its position"

    aBoolean ~~ showFrame ifTrue:[
	showFrame := aBoolean.
	self redrawIfShown
    ]
!

label
    "return the frames labelstring"

    ^ label
!

label:aString
    "set the frames labelstring"

    (label ~= aString) ifTrue:[
	label := aString.
	self redrawIfShown
    ]
!

font:aFont
    "set the frame labelstrings font"

    (font ~= aFont) ifTrue:[
	super font:aFont.
	self redrawIfShown
    ]
!

viewRectangle
    "return the inside area - redefined to save frame from
     relative computations"

    |m2 sep|

    sep := font height.
    m2 := sep + sep "+ sep".

    showFrame ifFalse:[
	^ (0 @ sep) extent:(width @ height)
    ].
    ^ (sep @ sep) extent:((width - sep) @ (height - sep))
!

layout
    "return the current layout, which is a symbol describing
     the labels position."

    ^ layout
!

layout:aSymbol
    "define the position of the label;
     aSymbol may be: #topLeft, #topCenter, #topRight;
     #bottomLeft, #bottomCenter or #bottomRight"

    layout ~~ aSymbol ifTrue:[
	layout := aSymbol.
	self redrawIfShown
    ]
! !

!FramedBox methodsFor:'event handling'!

sizeChanged:how
    self redrawIfShown.
    super sizeChanged:how
! !

!FramedBox methodsFor:'drawing'!

drawFrame
    "redraw the frame"

    |sep halfSep w h|

    "
     if there is no label, give more real estate to the inside
    "
    label isNil ifTrue:[
	halfSep := 2
    ] ifFalse:[
	halfSep := font height // 2.
    ].
    sep := halfSep * 2.

    w := width - sep.
    h := height - sep.

    frame3D ifFalse:[
	self displayRectangleX:halfSep 
			     y:halfSep
			 width:w 
			height:h.
	^ self
    ].

    w := w + 1.
    h := h + 1.

    self paint:shadowColor.
    self displayRectangleX:halfSep-1 
			 y:halfSep-1
		     width:w 
		    height:h.

    self paint:lightColor.
    self displayRectangleX:halfSep 
			 y:halfSep
		     width:w
		    height:h
!

redraw
    "redraw the frame and name if present"

    |labelLen l x y|

    label isNil ifTrue:[
	labelLen := 0
    ] ifFalse:[
	l := ' ' , label , ' '.
	labelLen := font widthOf:l
    ].

    showFrame ifTrue:[
	self drawFrame.
    ].

    labelLen > 0 ifTrue:[
	labelLen < width ifTrue:[
	    (#(topLeft topCenter topRight) includes:layout) ifTrue:[
		y := font ascent.
	    ] ifFalse:[
		y := height - font descent.
	    ].
	    (#(topLeft bottomLeft) includes:layout) ifTrue:[
		x := font height
	    ] ifFalse:[
		(#(topRight bottomRight) includes:layout) ifTrue:[
		    x := width - labelLen - font height
		] ifFalse:[
		    x := (width - labelLen) // 2
		]
	    ].
	    self paint:fgColor on:viewBackground.
	    self displayOpaqueString:l x:x y:y
	]
    ]
! !

!FramedBox methodsFor:'initialization'!

initialize
    super initialize.
    showFrame := true
!

initStyle
    "default position is top-center, except for ms-windows, where
     the text is positioned at top-left"

    super initStyle.

    fgColor := StyleSheet at:'framedBoxForegroundColor' default:Black.
    layout := StyleSheet at:'framedBoxLabelPosition' default:#topCenter.
    frame3D := StyleSheet at:'framedBox3DFrame' default:true.
! !