AlignmentOrigin.st
author claus
Tue, 29 Aug 1995 18:19:30 +0200
changeset 94 8888ddd11323
parent 93 f2389560b8eb
child 96 948318b2fbd4
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1995 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.
"

LayoutOrigin subclass:#AlignmentOrigin
	 instanceVariableNames:'leftAlignmentFraction topAlignmentFraction'
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Graphics-Geometry'
!

!AlignmentOrigin class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1995 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 class is provided to make porting of existing ST-80 applications
    easier. Instances can be used to control the geometry of a subview, within
    its superview. Like a LayoutOrigin, it controls the components origin
    via a relative part plus offset. However, in contrast to LayoutOrigin
    (in which the top-left corner is specified by fraction+offset, here any
    reference point within the component is positioned.
    The reference point itself is specified as fraction of the components size.

    See also:
	LayoutOrigin LayoutFrame Layout

    Notice: this class was implemented using protocol information
    from alpha testers - it may not be complete or compatible to
    the corresponding ST-80 class. If you encounter any incompatibilities,
    please forward a note to the ST/X team.
"
!

examples
"
    using a LayoutOrigin, to control the top-left origins position of
    a component (i.e. origin at:0.5@0.5):

	|top button|

	top := StandardSystemView new.
	top extent:300@300.

	button := Button label:'component'.
	top add:button in:(LayoutOrigin new
				leftFraction:0.5;
				topFraction:0.5).

	top open


    using an AlignmentOrigin, to control the centers position of
    a component (i.e. center of component at:0.5@0.5):

	|top button|

	top := StandardSystemView new.
	top extent:300@300.

	button := Button label:'component'.
	top add:button in:(AlignmentOrigin new
				leftFraction:0.5;
				topFraction:0.5;
				leftAlignmentFraction:0.5;
				topAlignmentFraction:0.5).

	top open


    using an AlignmentOrigin, to control the bottom-right position of
    a component (i.e. bottom-right of component at:0.5@0.5):

	|top button|

	top := StandardSystemView new.
	top extent:300@300.

	button := Button label:'component'.
	top add:button in:(AlignmentOrigin new
				leftFraction:0.5;
				topFraction:0.5;
				leftAlignmentFraction:1.0;
				topAlignmentFraction:1.0).

	top open
"
!

version
"
$Header: /cvs/stx/stx/libview2/AlignmentOrigin.st,v 1.4 1995-08-29 16:19:30 claus Exp $
"
! !

!AlignmentOrigin methodsFor:'accessing'!

leftAlignmentFraction:something
    "set leftAlignmentFraction"

    leftAlignmentFraction := something.
!

leftAlignmentFraction
    "return leftAlignmentFraction"

    ^ leftAlignmentFraction
!

topAlignmentFraction:something
    "set topAlignmentFraction"

    topAlignmentFraction := something.
!

topAlignmentFraction
    "return topAlignmentFraction"

    ^ topFraction
! !

!AlignmentOrigin methodsFor:'printing & storing'!

displayString
    ^ 'LayoutOrigin(' 
	, 'l: ' , leftFraction displayString
	, '+' , leftOffset displayString
	, ' t: ' , topFraction displayString
	, '+' , topOffset displayString
	, ' a: ' , leftAlignmentFraction displayString
	, '@' , topAlignmentFraction displayString
	, ')'
! !


!AlignmentOrigin methodsFor:'queries'!

rectangleRelativeTo:superRectangle preferred:prefRect
    |x y|

    leftOffset isNil ifTrue:[
	x := 0
    ] ifFalse:[
	x := leftOffset
    ].
    topOffset isNil ifTrue:[
	y := 0
    ] ifFalse:[
	y := topOffset
    ].
    leftFraction notNil ifTrue:[
	x := x + (superRectangle width * leftFraction)
    ].
    topFraction notNil ifTrue:[
	y := y + (superRectangle height * topFraction)
    ].
    leftAlignmentFraction ~~ 0 ifTrue:[
	x := x - (prefRect width * leftAlignmentFraction)
    ].
    topAlignmentFraction ~~ 0 ifTrue:[
	y := y - (prefRect height * topAlignmentFraction)
    ].
    ^ Rectangle origin:x@y extent:prefRect extent

    "
     |superRect aO|

     superRect := 0@0 corner:100@100.
     aO := (AlignmentOrigin new).
     aO leftFraction:0.5;
	topFraction:0.5;
	leftAlignmentFraction:0.5;
	topAlignmentFraction:0.5.
     aO rectangleRelativeTo:superRect preferred:(0@0 corner:30@30) 
    "
! !

!AlignmentOrigin methodsFor:'initialization'!

initialize
    super initialize.
    leftAlignmentFraction := topAlignmentFraction := 0.
! !