home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 14.5 KB | 517 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWShpLst.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
- // The top shape is the first one in the list. The bottom shape is the last one.
- // FW_CShapeListShape draws the shape from the last (bottom) to the first (top)
-
- #include "FWOS.hpp"
-
- #ifndef FWSHPLST_H
- #include "FWShpLst.h"
- #endif
-
- #ifndef FWSHAPE_H
- #include "FWShape.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphx_ShapeList
- #endif
-
- //========================================================================================
- // class FW_CShapeListRep
- //========================================================================================
-
- #ifdef FW_USE_TEMPLATE_PRAGMAS
- #pragma template_access public
- #pragma template FW_TOrderedCollection<FW_CShape>
- #endif
-
- FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollection, FW_CShape)
- FW_DEFINE_AUTO(FW_CShapeListIterator)
- FW_DEFINE_AUTO(FW_CShapeListRep)
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::FW_CShapeListRep
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListRep::FW_CShapeListRep() :
- fShapeList(NULL),
- fValidAnchorPoint(FALSE)
- {
- fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::FW_CShapeListRep
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListRep::FW_CShapeListRep(const FW_CShapeListRep& other) :
- fShapeList(NULL),
- fValidAnchorPoint(FALSE)
- {
- fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
-
- FW_TRY
- {
- FW_CShapeListIterator ite(other);
- for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
- AdoptAtBottom(shape->Copy());
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- PrivFree();
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::FW_CShapeListRep
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListRep::FW_CShapeListRep(FW_CReadableStream& stream) :
- fShapeList(NULL),
- fValidAnchorPoint(FALSE)
- {
- fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
-
- FW_TRY
- {
- unsigned long count;
- stream >> count;
-
- while(count -- != 0)
- {
- FW_CShape* shape;
- FW_READ_DYNAMIC_OBJECT(stream, &shape, FW_CShape);
-
- AdoptAtBottom(shape);
- }
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- PrivFree();
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::~FW_CShapeListRep
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListRep::~FW_CShapeListRep()
- {
- FW_START_DESTRUCTOR
-
- PrivFree();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::DeleteAll
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::DeleteAll()
- {
- if (fShapeList)
- {
- FW_CShape* shape;
- while ((shape = fShapeList->First()) != NULL)
- {
- Remove(shape);
- delete shape;
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::PrivFree
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::PrivFree()
- {
- DeleteAll();
- delete fShapeList;
- fShapeList = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::GetCount
- //----------------------------------------------------------------------------------------
-
- unsigned long FW_CShapeListRep::GetCount() const
- {
- return fShapeList->Count();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Purge
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::Purge()
- {
- FW_CShapeListIterator ite(*this);
- for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
- {
- shape->Purge();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::AdoptAtTop
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::AdoptAtTop(FW_CShape* shape)
- {
- fShapeList->AddFirst(shape);
- fValidAnchorPoint = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::AdoptAtBottom
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::AdoptAtBottom(FW_CShape* shape)
- {
- fShapeList->AddLast(shape);
- fValidAnchorPoint = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::AdoptBelow
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::AdoptBelow(FW_CShape* shapeToAdd, FW_CShape* belowWhich)
- {
- fShapeList->AddAfter(belowWhich, shapeToAdd);
- fValidAnchorPoint = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::AdoptAbove
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::AdoptAbove(FW_CShape* shapeToAdd, FW_CShape* aboveWhich)
- {
- fShapeList->AddBefore(aboveWhich, shapeToAdd);
- fValidAnchorPoint = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Contains
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::Contains(FW_CShape* shape) const
- {
- return fShapeList->Contains(shape);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Remove
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::Remove(FW_CShape* shape)
- {
- fShapeList->Remove(shape);
- fValidAnchorPoint = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::RemoveAll
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::RemoveAll()
- {
- while (GetCount() != 0)
- this->Remove(fShapeList->First());
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::RemoveTop
- //----------------------------------------------------------------------------------------
-
- FW_CShape* FW_CShapeListRep::RemoveTop()
- {
- FW_ASSERT(GetCount() != 0);
- fValidAnchorPoint = FALSE;
- return fShapeList->RemoveFirst();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::RemoveBottom
- //----------------------------------------------------------------------------------------
-
- FW_CShape* FW_CShapeListRep::RemoveBottom()
- {
- FW_ASSERT(GetCount() != 0);
- fValidAnchorPoint = FALSE;
- return fShapeList->RemoveLast();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::MoveUp
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::MoveUp(FW_CShape* shape)
- {
- FW_CShape* before = fShapeList->Before(shape);
- if (before != NULL)
- {
- fShapeList->Remove(shape);
- fShapeList->AddBefore(before, shape);
- return TRUE;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::MoveToTop
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::MoveToTop(FW_CShape* shape)
- {
- if (fShapeList->Before(shape) != NULL)
- {
- fShapeList->Remove(shape);
- fShapeList->AddFirst(shape);
- return TRUE;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::MoveDown
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::MoveDown(FW_CShape* shape)
- {
- FW_CShape* after = fShapeList->After(shape);
- if (after != NULL)
- {
- fShapeList->Remove(shape);
- fShapeList->AddAfter(after, shape);
- return TRUE;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::MoveToBottom
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::MoveToBottom(FW_CShape* shape)
- {
- if (fShapeList->After(shape) != NULL)
- {
- fShapeList->Remove(shape);
- fShapeList->AddLast(shape);
- return TRUE;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Write
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::Write(FW_CWritableStream& stream) const
- {
- unsigned long count = this->GetCount();
- stream << count;
-
- FW_CShapeListIterator ite(*this);
- for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
- FW_WRITE_DYNAMIC_OBJECT(stream, shape, FW_CShape);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::IsEqual
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::IsEqual(const FW_CShapeListRep* rep) const
- {
- if (rep == this)
- return TRUE;
-
- unsigned long count = this->GetCount();
-
- if(count == rep->GetCount())
- {
- FW_CShapeListIterator iteThis(*this);
- FW_CShapeListIterator iteThat(*rep);
-
- FW_CShape* shapeThis = iteThis.First();
- FW_CShape* shapeThat = iteThat.First();
-
- for (unsigned long i = 0; i< count; ++ i)
- {
- if(shapeThis != shapeThat)
- return FALSE;
-
- shapeThis = iteThis.Next();
- shapeThat = iteThat.Next();
- }
- return TRUE;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::GetAnchorPoint
- //----------------------------------------------------------------------------------------
-
- FW_CPoint FW_CShapeListRep::GetAnchorPoint() const
- {
- if (fValidAnchorPoint)
- {
- return fAnchorPoint;
- }
- else
- {
- FW_Boolean first = TRUE;
- FW_CPoint anchor, anAnchor;
-
- FW_CShapeListIterator ite(*this);
- for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
- {
- anAnchor = shape->GetAnchorPoint();
- if (first)
- anchor = anAnchor;
- else
- {
- anchor.x = FW_Minimum(anchor.x, anAnchor.x);
- anchor.y = FW_Minimum(anchor.y, anAnchor.y);
- }
- first = FALSE;
- }
-
- ((FW_CShapeListRep*)this)->fAnchorPoint = anchor;
- ((FW_CShapeListRep*)this)->fValidAnchorPoint = TRUE;
-
- return anchor;
- }
- }
-
- //========================================================================================
- // class FW_PShapeList
- //========================================================================================
-
- FW_DEFINE_AUTO_TEMPLATE(FW_TCountedPtr, FW_CShapeListRep)
-
- #ifdef FW_USE_TEMPLATE_PRAGMAS
-
- #pragma template_access public
- #pragma template FW_TCountedPtr<FW_CShapeListRep>
-
- #else
-
- template class FW_TCountedPtr<FW_CShapeListRep>;
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::FW_PShapeList
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList::FW_PShapeList() :
- FW_TCountedPtr<FW_CShapeListRep>(FW_NEW(FW_CShapeListRep, ()))
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::FW_PShapeList
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList::FW_PShapeList(FW_CReadableStream& stream) :
- FW_TCountedPtr<FW_CShapeListRep>(FW_NEW(FW_CShapeListRep, (stream)))
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::~FW_PShapeList
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList::~FW_PShapeList()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::FW_PShapeList
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList::FW_PShapeList(const FW_PShapeList& other)
- {
- SetRep(other.fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::operator=
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList& FW_PShapeList::operator=(const FW_PShapeList& other)
- {
- FW_TCountedPtr<FW_CShapeListRep>::operator=(other);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::Copy
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList FW_PShapeList::Copy() const
- {
- FW_PShapeList list;
- list.SetRep(FW_NEW(FW_CShapeListRep, (*fRep)));
- return list;
- }
-
- //----------------------------------------------------------------------------------------
- // operator <<
- //----------------------------------------------------------------------------------------
-
- FW_CWritableStream& operator << (FW_CWritableStream& stream, const FW_PShapeList& list)
- {
- list.fRep->Write(stream);
- return stream;
- }
-
- //----------------------------------------------------------------------------------------
- // operator >>
- //----------------------------------------------------------------------------------------
-
- FW_CReadableStream& operator >> (FW_CReadableStream& stream, FW_PShapeList& list)
- {
- list.SetRep(FW_NEW(FW_CShapeListRep, (stream)));
- return stream;
- }
-
-