home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 24.2 KB | 798 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWRect.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWRECT_H
- #include "FWRect.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWFXMATH_H
- #include "FWFxMath.h"
- #endif
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwodmisc_rect
- #endif
-
- //========================================================================================
- // struct FW_CPlatformRect
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPlatformRect::FW_CPlatformRect
- //----------------------------------------------------------------------------------------
-
- FW_CPlatformRect::FW_CPlatformRect(const FW_SRect& rect)
- {
- left = FW_FixedToInt(rect.left);
- top = FW_FixedToInt(rect.top);
- right = FW_FixedToInt(rect.right);
- bottom = FW_FixedToInt(rect.bottom);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPlatformRect::operator =
- //----------------------------------------------------------------------------------------
-
- FW_CPlatformRect& FW_CPlatformRect::operator =(const FW_SRect& rect)
- {
- left = FW_FixedToInt(rect.left);
- top = FW_FixedToInt(rect.top);
- right = FW_FixedToInt(rect.right);
- bottom = FW_FixedToInt(rect.bottom);
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPlatformRect::operator==
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPlatformRect::operator==(const FW_CPlatformRect& rect) const
- {
- return
- left == rect.left &&
- top == rect.top &&
- right == rect.right &&
- bottom == rect.bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPlatformRect::operator!=
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPlatformRect::operator!=(const FW_CPlatformRect& rect) const
- {
- return
- left != rect.left ||
- top != rect.top ||
- right != rect.right ||
- bottom != rect.bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPlatformRect::TopRight
- //----------------------------------------------------------------------------------------
-
- FW_CPlatformPoint FW_CPlatformRect::TopRight() const
- {
- FW_CPlatformPoint point(right, top);
- return point;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPlatformRect::BotLeft
- //----------------------------------------------------------------------------------------
-
- FW_CPlatformPoint FW_CPlatformRect::BotLeft() const
- {
- FW_CPlatformPoint point(left, bottom);
- return point;
- }
-
- //========================================================================================
- // class FW_CRect
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect()
- {
- left = top = right = bottom = FW_IntToFixed(0);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(FW_Fixed l, FW_Fixed t, FW_Fixed r, FW_Fixed b)
- {
- left = l;
- top = t;
- right = r;
- bottom = b;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_SPoint &topLeft, const FW_SPoint &botRight)
- {
- left = topLeft.x; right = botRight.x;
- top = topLeft.y; bottom = botRight.y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_SPoint &startPoint, const FW_SPoint &endPoint, FW_Fixed penSize)
- {
- left = startPoint.x;
- right = endPoint.x;
- top = startPoint.y;
- bottom= endPoint.y;
-
- Sort();
-
- right -= penSize;
- bottom -= penSize;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_SPoint &topLeft, FW_Fixed width, FW_Fixed height )
- {
- left = topLeft.x; right = left + width;
- top = topLeft.y; bottom= top + height;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const ODRect& odRect)
- {
- left = FW_ODFixedToFixed(odRect.left);
- top = FW_ODFixedToFixed(odRect.top);
- right = FW_ODFixedToFixed(odRect.right);
- bottom = FW_ODFixedToFixed(odRect.bottom);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CPoint
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(FW_CReadableStream& stream)
- {
- stream
- >> left
- >> top
- >> right
- >> bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect:: operator=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect:: operator=(const ODRect& odRect)
- {
- left = FW_ODFixedToFixed(odRect.left); right = FW_ODFixedToFixed(odRect.right);
- top = FW_ODFixedToFixed(odRect.top); bottom= FW_ODFixedToFixed(odRect.bottom);
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_PlatformRect& plfmRect)
- {
- left = FW_IntToFixed(plfmRect.left);
- top = FW_IntToFixed(plfmRect.top);
- right = FW_IntToFixed(plfmRect.right);
- bottom = FW_IntToFixed(plfmRect.bottom);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_CRect& other)
- {
- left = other.left;
- top = other.top;
- right = other.right;
- bottom = other.bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_SRect& sRect)
- {
- left = sRect.left;
- top = sRect.top;
- right = sRect.right;
- bottom = sRect.bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect::operator=(const FW_PlatformRect& plfmRect)
- {
- left = FW_IntToFixed(plfmRect.left); right = FW_IntToFixed(plfmRect.right);
- top = FW_IntToFixed(plfmRect.top); bottom= FW_IntToFixed(plfmRect.bottom);
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect:: operator=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect:: operator=(const FW_CRect& other)
- {
- left = other.left; right = other.right;
- top = other.top; bottom= other.bottom;
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect:: operator=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect:: operator=(const FW_SRect& rect)
- {
- left = rect.left; right = rect.right;
- top = rect.top; bottom= rect.bottom;
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Set
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Set(FW_Fixed l, FW_Fixed t, FW_Fixed r, FW_Fixed b)
- {
- left = l; right = r;
- top = t; bottom= b;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Set
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Set(const FW_SPoint &topLeft, FW_Fixed width, FW_Fixed height)
- {
- left = topLeft.x; right = left+width;
- top = topLeft.y; bottom= top +height;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Set
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Set(const FW_SPoint &topLeft, const FW_SPoint &botRight)
- {
- left = topLeft.x; right = botRight.x;
- top = topLeft.y; bottom= botRight.y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::SetInt
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::SetInt(short l, short t, short r, short b)
- {
- left = FW_IntToFixed(l); right = FW_IntToFixed(r);
- top = FW_IntToFixed(t); bottom= FW_IntToFixed(b);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Offset
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Offset(FW_Fixed x, FW_Fixed y)
- {
- left += x; right += x;
- top += y; bottom+= y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Offset
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Offset(const FW_SPoint &pt)
- {
- left += pt.x; right += pt.x;
- top += pt.y; bottom+= pt.y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Place
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Place(FW_Fixed x, FW_Fixed y)
- {
- right += x - left;
- left = x;
- bottom += y - top;
- top = y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Place
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Place(const FW_SPoint &pt)
- {
- right += pt.x - left;
- left = pt.x;
- bottom += pt.y - top;
- top = pt.y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::PlaceInCenterOf
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::PlaceInCenterOf(const FW_SRect& otherRect)
- {
- FW_Fixed newTop = FW_Half(otherRect.bottom + otherRect.top - bottom + top);
- bottom = newTop + bottom - top;
- top = newTop;
-
- FW_Fixed newLeft = FW_Half(otherRect.right + otherRect.left - right + left);
- right = newLeft + right - left;
- left = newLeft;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Center
- //----------------------------------------------------------------------------------------
-
- FW_CPoint FW_CRect::Center() const
- {
- FW_CPoint center(FW_Half(left + right), FW_Half(top + bottom));
- return center;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Inset
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Inset(FW_Fixed x, FW_Fixed y )
- {
- left += x;
- right -= x;
- top += y;
- bottom -= y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Clear
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Clear( )
- {
- left = right = top = bottom = FW_IntToFixed(0);
- }
-
- //----------------------------------------------------------------------------------------
- // operator|= FW_CPoint
- //----------------------------------------------------------------------------------------
-
- void FW_CRect:: operator|= (const FW_SPoint &pt)
- {
- left = FW_Minimum(left,pt.x);
- right = FW_Maximum(right,pt.x);
- top = FW_Minimum(top,pt.y);
- bottom = FW_Maximum(bottom,pt.y);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::AsPlatformRect
- //----------------------------------------------------------------------------------------
-
- FW_CPlatformRect FW_CRect::AsPlatformRect() const
- {
- return FW_CPlatformRect(
- FW_FixedToInt(left),
- FW_FixedToInt(top),
- FW_FixedToInt(right),
- FW_FixedToInt(bottom));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::IsEmpty
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::IsEmpty() const
- {
- return right<=left || bottom<=top;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Contains
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::Contains(const FW_SPoint &pt) const
- {
- return left<=pt.x && pt.x<right
- && top <=pt.y && pt.y<bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Contains
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::Contains( const FW_SRect &r) const
- {
- return left<=r.left && r.right<=right
- && top <=r.top && r.bottom<=bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator==
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::operator==(const FW_SRect &sRect) const
- {
- return (left == sRect.left && top == sRect.top && right == sRect.right && bottom == sRect.bottom);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::IsIntersecting
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::IsIntersecting(const FW_SRect &r) const
- {
- return FW_Maximum(left,r.left) < FW_Minimum(right,r.right)
- && FW_Maximum(top,r.top) < FW_Minimum(bottom,r.bottom);
- }
-
- static inline void FW_PrivAdjustIfEmpty(FW_CRect &r)
- {
- // [BRP] The IsEmpty test below was in Bedrock and ODF R1
- // However, the caller should be responsible for testing
- // the IsEmpty status of the rect.
- // This particular case fixes a bug
- // found in ODFDraw where rectangle selection of
- // vertical and horizontal lines would fail.
- // Here, we allow the case where the intersection has zero-width or height
-
- // if (IsEmpty())
- if(r.left > r.right || r.top > r.bottom)
- r.SetInt(0,0,0,0);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Intersection
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Intersection(const FW_SRect &r)
- {
- left = FW_Maximum(left, r.left);
- top = FW_Maximum(top, r.top);
- right = FW_Minimum(right, r.right);
- bottom = FW_Minimum(bottom, r.bottom);
-
- FW_PrivAdjustIfEmpty(*this);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Intersection
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Intersection(const FW_SRect &r1, const FW_SRect &r2)
- {
- left = FW_Maximum(r1.left, r2.left);
- top = FW_Maximum(r1.top, r2.top);
- right = FW_Minimum(r1.right, r2.right);
- bottom = FW_Minimum(r1.bottom, r2.bottom);
-
- FW_PrivAdjustIfEmpty(*this);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Union
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Union(const FW_SRect &rect)
- {
- left = FW_Minimum(left, rect.left);
- right= FW_Maximum(right, rect.right);
- top = FW_Minimum(top, rect.top);
- bottom=FW_Maximum(bottom, rect.bottom);
-
- FW_PrivAdjustIfEmpty(*this);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Union
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Union(const FW_SRect &r1, const FW_SRect &r2)
- {
- left = FW_Minimum(r1.left, r2.left);
- right= FW_Maximum(r1.right, r2.right);
- top = FW_Minimum(r1.top, r2.top);
- bottom=FW_Maximum(r1.bottom, r2.bottom);
-
- FW_PrivAdjustIfEmpty(*this);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Sort
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Sort()
- {
- if (left > right)
- {
- FW_Fixed temp = left;
- left = right;
- right = temp;
- }
- if (top > bottom)
- {
- FW_Fixed temp = top;
- top = bottom;
- bottom = temp;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Map
- //----------------------------------------------------------------------------------------
- // The result may not be sorted. You might have to call Sort.
-
- void FW_CRect::Map(const FW_SRect& srcRect, const FW_SRect& dstRect)
- {
- ((FW_CPoint*)&left)->Map(srcRect, dstRect);
- ((FW_CPoint*)&right)->Map(srcRect, dstRect);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::TopRight
- //----------------------------------------------------------------------------------------
-
- FW_CPoint FW_CRect::TopRight() const
- {
- FW_CPoint point(right, top);
- return point;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::BotLeft
- //----------------------------------------------------------------------------------------
-
- FW_CPoint FW_CRect::BotLeft() const
- {
- FW_CPoint point(left, bottom);
- return point;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_Transform
- //----------------------------------------------------------------------------------------
-
- void FW_Transform(Environment* ev, FW_SRect& rect, ODTransform* transform)
- {
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &rect) + 0) =
- #endif
- transform->TransformPoint(ev, ((ODPoint*) &rect) + 0);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &rect) + 1) =
- #endif
- transform->TransformPoint(ev, ((ODPoint*) &rect) + 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_InverseTransform
- //----------------------------------------------------------------------------------------
-
- void FW_InverseTransform(Environment* ev, FW_SRect& rect, ODTransform* transform)
- {
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &rect) + 0) =
- #endif
- transform->InvertPoint(ev, ((ODPoint*) &rect) + 0);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &rect) + 1) =
- #endif
- transform->InvertPoint(ev, ((ODPoint*) &rect) + 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TransformCopy
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_TransformCopy(Environment* ev, const FW_SRect& rect, ODTransform* transform)
- {
- FW_CRect r(rect);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &r) + 0) =
- #endif
- transform->TransformPoint(ev, ((ODPoint*) &r) + 0);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &r) + 1) =
- #endif
- transform->TransformPoint(ev, ((ODPoint*) &r) + 1);
-
- return r;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_InverseTransformCopy
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_InverseTransformCopy(Environment* ev, const FW_SRect& rect, ODTransform* transform)
- {
- FW_CRect r(rect);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &r) + 0) =
- #endif
- transform->InvertPoint(ev, ((ODPoint*) &r) + 0);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &r) + 1) =
- #endif
- transform->InvertPoint(ev, ((ODPoint*) &r) + 1);
-
- return r;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator[]
- //----------------------------------------------------------------------------------------
-
- FW_CPoint& FW_CRect::operator[](FW_PointSelector sel)
- {
- return (sel == FW_kTopLeft) ? *((FW_CPoint *) &left) : *((FW_CPoint *) &right);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator[]
- //----------------------------------------------------------------------------------------
-
- const FW_CPoint& FW_CRect::operator[](FW_PointSelector sel) const
- {
- return (sel == FW_kTopLeft) ? *((FW_CPoint *) &left) : *((FW_CPoint *) &right);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Size
- //----------------------------------------------------------------------------------------
-
- FW_CPoint FW_CRect::Size() const
- {
- return FW_CPoint(right - left, bottom - top);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Length
- //----------------------------------------------------------------------------------------
-
- FW_Fixed FW_CRect::Length(FW_XYSelector selector) const
- {
- return BotRight()[selector] - TopLeft()[selector];
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator+=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect::operator+=(const FW_SPoint& pt)
- {
- left += pt.x;
- top += pt.y;
- right += pt.x;
- bottom += pt.y;
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator+
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CRect::operator+(const FW_SPoint& pt) const
- {
- return FW_CRect(
- left + pt.x,
- top + pt.y,
- right + pt.x,
- bottom + pt.y);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator-=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect::operator-=(const FW_SPoint& pt)
- {
- left -= pt.x;
- top -= pt.y;
- right -= pt.x;
- bottom -= pt.y;
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator-
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CRect::operator-(const FW_SPoint& pt) const
- {
- return FW_CRect(
- left - pt.x,
- top - pt.y,
- right - pt.x,
- bottom - pt.y);
- }
-
- //----------------------------------------------------------------------------------------
- // operator<<
- //----------------------------------------------------------------------------------------
-
- FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_SRect& rect)
- {
- return stream
- << rect.left
- << rect.top
- << rect.right
- << rect.bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // operator>>
- //----------------------------------------------------------------------------------------
-
- FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_SRect& rect)
- {
- return stream
- >> rect.left
- >> rect.top
- >> rect.right
- >> rect.bottom;
- }
-
-