LayoutOrigin.st
author claus
Sat, 09 Sep 1995 04:30:16 +0200
changeset 100 0300e64bb883
parent 96 948318b2fbd4
child 114 e577a2f332d0
permissions -rw-r--r--
.

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

'From Smalltalk/X, Version:2.10.5 on 15-apr-1995 at 12:14:46 pm'!

Layout subclass:#LayoutOrigin
	 instanceVariableNames:'leftFraction topFraction leftOffset
		topOffset'
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Graphics-Geometry'
!

!LayoutOrigin 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. It provides the same functionality as a relative origin
    combined with insets.
    A layoutOrigin controls the origin of a subcomponent, given a fractional
    component and an offset component.

    See also:
	LayoutFrame AlignmentOrigin 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


    like above, but adds an additional offset:
    (i.e. center of component at:0.5@0.5 OFFSET by 10@20):

	|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;
				leftOffset:10;
				topOffset:20).

	top open
"
!

version
"
$Header: /cvs/stx/stx/libview2/LayoutOrigin.st,v 1.7 1995-09-09 02:29:45 claus Exp $
"
! !

!LayoutOrigin class methodsFor:'instance creation'!

fractionalFromPoint:aPoint
    "given a point, create a layoutOrigin representing the same 
     relative origin."

    ^ (self new) leftFraction:aPoint x topFraction:aPoint y

    "
     LayoutOrigin fractionalFromPoint:0.5@0.5
    "
!

offsetFromPoint:aPoint
    "given a point, create a layoutOrigin representing the same 
     absolute (pixel) origin."

    ^ self new leftOffset:aPoint x topOffset:aPoint y

    "
     LayoutOrigin offsetFromPoint:100@100
    "
!

fromPoint:aPoint
    "return a new LayoutOrigin from  aPoint.
     If the coordinates are between 0 and 1, take
     them as fractional parts (relative to superview).
     Otherwise, treat them as absolute offsets."

    |x y layout|

    x := aPoint x.
    y := aPoint y.
    layout := self new.
    ((x between:0 and:1)
    and:[y between:0 and:1]) ifTrue:[
	layout leftFraction:x topFraction:y
    ] ifFalse:[
	layout leftOffset:x topOffset:y
    ].
    ^ layout

    "
     LayoutOrigin fromPoint:100@100
     LayoutOrigin fromPoint:0.5@0.5
     LayoutOrigin fromPoint:0.5@100
    "
! !

!LayoutOrigin methodsFor:'printing & storing'!

displayString
    ^ (self class name) , '(' 
	, 'l: ' , leftFraction displayString
	, '+' , leftOffset displayString
	, ' t: ' , topFraction displayString
	, '+' , topOffset displayString
	, ')'
! !

!LayoutOrigin methodsFor:'accessing'!

leftFraction:something
    "set leftFraction"

    leftFraction := something.
!

leftFraction
    "return leftFraction"

    ^ leftFraction
!

leftOffset:something
    "set leftOffset"

    leftOffset := something.
!

leftOffset
    "return leftOffset"

    ^ leftOffset
!

leftFraction:something offset:o
    "set leftFraction and offset"

    leftFraction := something.
    leftOffset := o
!

topFraction:something
    "set topFraction"

    topFraction := something.
!

topFraction
    "return topFraction"

    ^ topFraction
!

topOffset:something
    "set topOffset"

    topOffset := something.
!

topOffset
    "return topOffset"

    ^ topOffset
!

topFraction:something offset:o
    "set topFraction and offset"

    topFraction := something.
    topOffset := o
!

leftFraction:newLeft topFraction:newTop 
    "set leftFraction and topFraction"

    leftFraction := newLeft.
    topFraction := newTop.
!

leftOffset:newLeft topOffset:newTop 
    "set leftOffset and topOffset"

    leftOffset := newLeft.
    topOffset := newTop.
! !

!LayoutOrigin 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)
    ].
    ^ Rectangle origin:x@y extent:prefRect extent

    "
     |superRect lO|

     superRect := 0@0 corner:100@100.
     lO := (LayoutOrigin new).
     lO leftFraction:0.5;
	topFraction:0.5.
     lO rectangleRelativeTo:superRect preferred:(0@0 corner:30@30)
    "
!

origin
    ^ leftFraction asFloat @ topFraction asFloat
! !

!LayoutOrigin methodsFor:'initialization'!

initialize
    leftOffset := topOffset := 0.
    leftFraction := topFraction := 0.
! !

!LayoutOrigin methodsFor:'converting'!

literalArrayEncoding
    "encode myself as an array.
     The encoding is: 
	(#LayoutOrigin orgOffsX relOrgX orgOffsY relOrgY)
    "

    ^ Array
	with:self class name asSymbol
	with:leftOffset
	with:leftFraction
	with:topOffset
	with:topFraction

    "
      LayoutOrigin new fromLiteralArrayEncoding:#(#LayoutOrigin 70 0 2 0)
      (LayoutOrigin new leftOffset:10; leftFraction:0.2;
		       topOffset:20; topFraction:0.4) literalArrayEncoding 
    "

    "Modified: 1.9.1995 / 02:43:48 / claus"
!

fromLiteralArrayEncoding:encoding
    "read my values from an encoding.
     The encoding is supposed to be of the form: 
	(#LayoutOrigin orgOffsX relOrgX orgOffsY relOrgY)
     This is the reverse operation to #literalArrayEncoding."

    leftOffset := encoding at:2.
    leftFraction := encoding at:3.
    topOffset := encoding at:4.
    topFraction := encoding at:5.


    "
      LayoutOrigin new fromLiteralArrayEncoding:#(#LayoutOrigin 70 0 2 0)
    "
! !