AlignmentOrigin.st
author Claus Gittinger <cg@exept.de>
Wed, 08 May 1996 21:54:36 +0200
changeset 244 74c365cb546a
parent 243 dfbfd389ec48
child 379 62b71a8294a3
permissions -rw-r--r--
checkin from browser

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

    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 
        describing the incompatibility verbal (i.e. no code) to the ST/X team.

    [author:]
        Claus Gittinger

    [See also:]
        LayoutOrigin LayoutFrame Layout
        View
"
!

examples
"
    Although the examples below use a button as component,
    they work of course with any type of subview ....

    using a LayoutOrigin, to control the top-left origins position of
    a component (i.e. origin at:0.5@0.5):
                                                                        [exBegin]
        |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
                                                                        [exEnd]


    using an AlignmentOrigin, to control the centers position of
    a component (i.e. center of component at:0.5@0.5):
                                                                        [exBegin]
        |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
                                                                        [exEnd]


    using an AlignmentOrigin, to control the bottom-right position of
    a component (i.e. bottom-right of component at:0.5@0.5):
                                                                        [exBegin]
        |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
                                                                        [exEnd]


    four buttons around relative 0.75@0.75:
                                                                        [exBegin]
        |top button|

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

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

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

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

        button := Button label:'button4'.
        top add:button in:(AlignmentOrigin new
                                leftFraction:0.75;
                                topFraction:0.75;
                                leftAlignmentFraction:0.0;
                                topAlignmentFraction:0.0).
        top open
                                                                        [exEnd]
"
! !

!AlignmentOrigin methodsFor:'accessing'!

leftAlignmentFraction
    "return leftAlignmentFraction"

    ^ leftAlignmentFraction
!

leftAlignmentFraction:something
    "set leftAlignmentFraction"

    leftAlignmentFraction := something.
!

topAlignmentFraction
    "return topAlignmentFraction"

    ^ topFraction
!

topAlignmentFraction:something
    "set topAlignmentFraction"

    topAlignmentFraction := something.
! !

!AlignmentOrigin methodsFor:'converting'!

fromLiteralArrayEncoding:encoding
    "read my values from an encoding.
     The encoding is supposed to be of the form: 
	(AlignmentOrigin orgOffsX relOrgX orgOffsY relOrgY leftAlignFract topAlignFract)"

    leftOffset := encoding at:2.
    leftFraction := encoding at:3.
    topOffset := encoding at:4.
    topFraction := encoding at:5.
    leftAlignmentFraction := encoding at:6.
    topAlignmentFraction := encoding at:7.


    "
      AlignmentOrigin new fromLiteralArrayEncoding:#(#AlignmentOrigin 70 0 2 0 0.5 0.25)
    "

    "Modified: 1.9.1995 / 02:23:53 / claus"
!

literalArrayEncoding
    "encode myself as an array, from which a copy of the receiver
     can be reconstructed with #decodeAsLiteralArray.
     The encoding is: 
        (#AlignmentOrigin orgOffsX relOrgX orgOffsY relOrgY leftAlignFract topAlignFract)"

    ^ super literalArrayEncoding
      , (Array
            with:leftAlignmentFraction
            with:topAlignmentFraction)

    "Modified: 1.9.1995 / 02:43:35 / claus"
    "Modified: 22.4.1996 / 12:59:56 / cg"
! !

!AlignmentOrigin methodsFor:'initialization'!

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

!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
    "compute the rectangle represented by the receiver,
     given the superViews rectangle and the views preferredExtent."

    |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 left:x top: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) 
    "

    "Modified: 8.5.1996 / 20:57:09 / cg"
! !

!AlignmentOrigin class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/AlignmentOrigin.st,v 1.15 1996-05-08 19:54:36 cg Exp $'
! !