home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / FWShpLst.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  14.5 KB  |  517 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWShpLst.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9. //    The top shape is the first one in the list. The bottom shape is the last one. 
  10. //    FW_CShapeListShape draws the shape from the last (bottom) to the first (top)
  11.  
  12. #include "FWOS.hpp"
  13.  
  14. #ifndef FWSHPLST_H
  15. #include "FWShpLst.h"
  16. #endif
  17.  
  18. #ifndef FWSHAPE_H
  19. #include "FWShape.h"
  20. #endif
  21.  
  22. // ----- Foundation Includes -----
  23.  
  24. #ifndef FWSTREAM_H
  25. #include "FWStream.h"
  26. #endif
  27.  
  28. //========================================================================================
  29. //    RunTime Info
  30. //========================================================================================
  31.  
  32. #ifdef FW_BUILD_MAC
  33. #pragma segment FWGraphx_ShapeList
  34. #endif
  35.  
  36. //========================================================================================
  37. //    class FW_CShapeListRep
  38. //========================================================================================
  39.  
  40. #ifdef FW_USE_TEMPLATE_PRAGMAS
  41. #pragma template_access public
  42. #pragma template FW_TOrderedCollection<FW_CShape>
  43. #endif
  44.  
  45. FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollection, FW_CShape)
  46. FW_DEFINE_AUTO(FW_CShapeListIterator)
  47. FW_DEFINE_AUTO(FW_CShapeListRep)
  48.  
  49. //----------------------------------------------------------------------------------------
  50. // FW_CShapeListRep::FW_CShapeListRep
  51. //----------------------------------------------------------------------------------------
  52.  
  53. FW_CShapeListRep::FW_CShapeListRep() :
  54.     fShapeList(NULL),
  55.     fValidAnchorPoint(FALSE)
  56. {
  57.     fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
  58.     
  59.     FW_END_CONSTRUCTOR
  60. }
  61.  
  62. //----------------------------------------------------------------------------------------
  63. // FW_CShapeListRep::FW_CShapeListRep
  64. //----------------------------------------------------------------------------------------
  65.  
  66. FW_CShapeListRep::FW_CShapeListRep(const FW_CShapeListRep& other) :
  67.     fShapeList(NULL),
  68.     fValidAnchorPoint(FALSE)
  69. {
  70.     fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
  71.  
  72.     FW_TRY
  73.     {
  74.         FW_CShapeListIterator ite(other);
  75.         for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  76.             AdoptAtBottom(shape->Copy());
  77.     }
  78.     FW_CATCH_BEGIN
  79.     FW_CATCH_EVERYTHING()
  80.     {
  81.         PrivFree();
  82.         FW_THROW_SAME();
  83.     }
  84.     FW_CATCH_END
  85.     
  86.     FW_END_CONSTRUCTOR
  87. }
  88.  
  89. //----------------------------------------------------------------------------------------
  90. // FW_CShapeListRep::FW_CShapeListRep
  91. //----------------------------------------------------------------------------------------
  92.  
  93. FW_CShapeListRep::FW_CShapeListRep(FW_CReadableStream& stream) :
  94.     fShapeList(NULL),
  95.     fValidAnchorPoint(FALSE)
  96. {
  97.     fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
  98.     
  99.     FW_TRY
  100.     {
  101.         unsigned long count;
  102.         stream >> count;
  103.         
  104.         while(count -- != 0)
  105.         {
  106.             FW_CShape* shape;
  107.             FW_READ_DYNAMIC_OBJECT(stream, &shape, FW_CShape);
  108.  
  109.             AdoptAtBottom(shape);
  110.         }
  111.     }
  112.     FW_CATCH_BEGIN
  113.     FW_CATCH_EVERYTHING()
  114.     {
  115.         PrivFree();
  116.         FW_THROW_SAME();
  117.     }
  118.     FW_CATCH_END
  119.  
  120.     FW_END_CONSTRUCTOR
  121. }
  122.  
  123. //----------------------------------------------------------------------------------------
  124. // FW_CShapeListRep::~FW_CShapeListRep
  125. //----------------------------------------------------------------------------------------
  126.  
  127. FW_CShapeListRep::~FW_CShapeListRep()
  128. {
  129.     FW_START_DESTRUCTOR
  130.  
  131.     PrivFree();
  132. }
  133.  
  134. //----------------------------------------------------------------------------------------
  135. // FW_CShapeListRep::DeleteAll
  136. //----------------------------------------------------------------------------------------
  137.  
  138. void FW_CShapeListRep::DeleteAll()
  139. {
  140.     if (fShapeList)
  141.     {
  142.         FW_CShape* shape;
  143.         while ((shape = fShapeList->First()) != NULL)
  144.         {
  145.             Remove(shape);
  146.             delete shape;        
  147.         }
  148.     }
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // FW_CShapeListRep::PrivFree
  153. //----------------------------------------------------------------------------------------
  154.  
  155. void FW_CShapeListRep::PrivFree()
  156. {
  157.     DeleteAll();
  158.     delete fShapeList;
  159.     fShapeList = NULL;
  160. }
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // FW_CShapeListRep::GetCount
  164. //----------------------------------------------------------------------------------------
  165.  
  166. unsigned long FW_CShapeListRep::GetCount() const
  167. {
  168.     return fShapeList->Count();
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // FW_CShapeListRep::Purge
  173. //----------------------------------------------------------------------------------------
  174.  
  175. void FW_CShapeListRep::Purge()
  176. {
  177.     FW_CShapeListIterator ite(*this);
  178.     for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  179.     {
  180.         shape->Purge();
  181.     }
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. // FW_CShapeListRep::AdoptAtTop
  186. //----------------------------------------------------------------------------------------
  187.  
  188. void FW_CShapeListRep::AdoptAtTop(FW_CShape* shape)
  189. {
  190.     fShapeList->AddFirst(shape);
  191.     fValidAnchorPoint = FALSE;
  192. }
  193.  
  194. //----------------------------------------------------------------------------------------
  195. // FW_CShapeListRep::AdoptAtBottom
  196. //----------------------------------------------------------------------------------------
  197.  
  198. void FW_CShapeListRep::AdoptAtBottom(FW_CShape* shape)
  199. {
  200.     fShapeList->AddLast(shape);
  201.     fValidAnchorPoint = FALSE;
  202. }
  203.  
  204. //----------------------------------------------------------------------------------------
  205. // FW_CShapeListRep::AdoptBelow
  206. //----------------------------------------------------------------------------------------
  207.  
  208. void FW_CShapeListRep::AdoptBelow(FW_CShape* shapeToAdd, FW_CShape* belowWhich)
  209. {
  210.     fShapeList->AddAfter(belowWhich, shapeToAdd);
  211.     fValidAnchorPoint = FALSE;
  212. }
  213.  
  214. //----------------------------------------------------------------------------------------
  215. // FW_CShapeListRep::AdoptAbove
  216. //----------------------------------------------------------------------------------------
  217.  
  218. void FW_CShapeListRep::AdoptAbove(FW_CShape* shapeToAdd, FW_CShape* aboveWhich)
  219. {
  220.     fShapeList->AddBefore(aboveWhich, shapeToAdd);
  221.     fValidAnchorPoint = FALSE;
  222. }
  223.  
  224. //----------------------------------------------------------------------------------------
  225. // FW_CShapeListRep::Contains
  226. //----------------------------------------------------------------------------------------
  227.  
  228. FW_Boolean FW_CShapeListRep::Contains(FW_CShape* shape) const
  229. {    
  230.     return fShapeList->Contains(shape);
  231. }
  232.  
  233. //----------------------------------------------------------------------------------------
  234. // FW_CShapeListRep::Remove
  235. //----------------------------------------------------------------------------------------
  236.  
  237. void FW_CShapeListRep::Remove(FW_CShape* shape)
  238. {
  239.     fShapeList->Remove(shape);
  240.     fValidAnchorPoint = FALSE;
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. // FW_CShapeListRep::RemoveAll
  245. //----------------------------------------------------------------------------------------
  246.  
  247. void FW_CShapeListRep::RemoveAll()
  248. {
  249.     while (GetCount() != 0)
  250.         this->Remove(fShapeList->First());
  251. }
  252.  
  253. //----------------------------------------------------------------------------------------
  254. // FW_CShapeListRep::RemoveTop
  255. //----------------------------------------------------------------------------------------
  256.  
  257. FW_CShape* FW_CShapeListRep::RemoveTop()
  258. {
  259.     FW_ASSERT(GetCount() != 0);
  260.     fValidAnchorPoint = FALSE;
  261.     return fShapeList->RemoveFirst();
  262. }
  263.  
  264. //----------------------------------------------------------------------------------------
  265. // FW_CShapeListRep::RemoveBottom
  266. //----------------------------------------------------------------------------------------
  267.  
  268. FW_CShape* FW_CShapeListRep::RemoveBottom()
  269. {
  270.     FW_ASSERT(GetCount() != 0);
  271.     fValidAnchorPoint = FALSE;
  272.     return fShapeList->RemoveLast();
  273. }
  274.  
  275. //----------------------------------------------------------------------------------------
  276. // FW_CShapeListRep::MoveUp
  277. //----------------------------------------------------------------------------------------
  278.  
  279. FW_Boolean FW_CShapeListRep::MoveUp(FW_CShape* shape)
  280. {
  281.     FW_CShape* before = fShapeList->Before(shape);
  282.     if (before != NULL)
  283.     {
  284.         fShapeList->Remove(shape);
  285.         fShapeList->AddBefore(before, shape);
  286.         return TRUE;
  287.     }
  288.  
  289.     return FALSE;
  290. }
  291.  
  292. //----------------------------------------------------------------------------------------
  293. // FW_CShapeListRep::MoveToTop
  294. //----------------------------------------------------------------------------------------
  295.  
  296. FW_Boolean FW_CShapeListRep::MoveToTop(FW_CShape* shape)
  297. {
  298.     if (fShapeList->Before(shape) != NULL)
  299.     {
  300.         fShapeList->Remove(shape);
  301.         fShapeList->AddFirst(shape);
  302.         return TRUE;
  303.     }
  304.  
  305.     return FALSE;
  306. }
  307.  
  308. //----------------------------------------------------------------------------------------
  309. // FW_CShapeListRep::MoveDown
  310. //----------------------------------------------------------------------------------------
  311.  
  312. FW_Boolean FW_CShapeListRep::MoveDown(FW_CShape* shape)
  313. {
  314.     FW_CShape* after = fShapeList->After(shape);
  315.     if (after != NULL)
  316.     {
  317.         fShapeList->Remove(shape);
  318.         fShapeList->AddAfter(after, shape);
  319.         return TRUE;
  320.     }
  321.  
  322.     return FALSE;
  323. }
  324.  
  325. //----------------------------------------------------------------------------------------
  326. // FW_CShapeListRep::MoveToBottom
  327. //----------------------------------------------------------------------------------------
  328.  
  329. FW_Boolean FW_CShapeListRep::MoveToBottom(FW_CShape* shape)
  330. {
  331.     if (fShapeList->After(shape) != NULL)
  332.     {
  333.         fShapeList->Remove(shape);
  334.         fShapeList->AddLast(shape);
  335.         return TRUE;
  336.     }
  337.  
  338.     return FALSE;
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------
  342. // FW_CShapeListRep::Write
  343. //----------------------------------------------------------------------------------------
  344.  
  345. void FW_CShapeListRep::Write(FW_CWritableStream& stream) const
  346. {
  347.     unsigned long count = this->GetCount();
  348.     stream << count;
  349.     
  350.     FW_CShapeListIterator ite(*this);
  351.     for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  352.         FW_WRITE_DYNAMIC_OBJECT(stream, shape, FW_CShape);
  353. }
  354.  
  355. //----------------------------------------------------------------------------------------
  356. // FW_CShapeListRep::IsEqual
  357. //----------------------------------------------------------------------------------------
  358.  
  359. FW_Boolean FW_CShapeListRep::IsEqual(const FW_CShapeListRep* rep) const
  360. {
  361.     if (rep == this)
  362.         return TRUE;
  363.     
  364.     unsigned long count = this->GetCount();
  365.         
  366.     if(count == rep->GetCount())
  367.     {
  368.         FW_CShapeListIterator iteThis(*this);
  369.         FW_CShapeListIterator iteThat(*rep);
  370.         
  371.         FW_CShape* shapeThis = iteThis.First();
  372.         FW_CShape* shapeThat = iteThat.First();
  373.         
  374.         for (unsigned long i = 0; i< count; ++ i)
  375.         {
  376.             if(shapeThis != shapeThat)
  377.                 return FALSE;
  378.             
  379.             shapeThis = iteThis.Next();
  380.             shapeThat = iteThat.Next();    
  381.         }
  382.         return TRUE;
  383.     }
  384.  
  385.     return FALSE;
  386. }
  387.  
  388. //----------------------------------------------------------------------------------------
  389. //    FW_CShapeListRep::GetAnchorPoint
  390. //----------------------------------------------------------------------------------------
  391.  
  392. FW_CPoint FW_CShapeListRep::GetAnchorPoint() const
  393. {
  394.     if (fValidAnchorPoint)
  395.     {
  396.         return fAnchorPoint;
  397.     }
  398.     else
  399.     {
  400.         FW_Boolean first = TRUE;
  401.         FW_CPoint anchor, anAnchor;
  402.     
  403.         FW_CShapeListIterator ite(*this);
  404.         for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  405.         {
  406.             anAnchor = shape->GetAnchorPoint();
  407.             if (first)
  408.                 anchor = anAnchor;
  409.             else
  410.             {
  411.                 anchor.x = FW_Minimum(anchor.x, anAnchor.x);
  412.                 anchor.y = FW_Minimum(anchor.y, anAnchor.y);
  413.             }
  414.             first = FALSE;
  415.         }    
  416.     
  417.         ((FW_CShapeListRep*)this)->fAnchorPoint = anchor;
  418.         ((FW_CShapeListRep*)this)->fValidAnchorPoint = TRUE;
  419.         
  420.         return anchor;
  421.     }
  422. }
  423.  
  424. //========================================================================================
  425. //    class FW_PShapeList
  426. //========================================================================================
  427.  
  428. FW_DEFINE_AUTO_TEMPLATE(FW_TCountedPtr, FW_CShapeListRep)
  429.  
  430. #ifdef FW_USE_TEMPLATE_PRAGMAS
  431.  
  432. #pragma template_access public
  433. #pragma template FW_TCountedPtr<FW_CShapeListRep>
  434.  
  435. #else
  436.  
  437. template class FW_TCountedPtr<FW_CShapeListRep>;
  438.  
  439. #endif
  440.  
  441. //----------------------------------------------------------------------------------------
  442. // FW_PShapeList::FW_PShapeList
  443. //----------------------------------------------------------------------------------------
  444.  
  445. FW_PShapeList::FW_PShapeList() :
  446.     FW_TCountedPtr<FW_CShapeListRep>(FW_NEW(FW_CShapeListRep, ()))
  447. {
  448. }
  449.  
  450. //----------------------------------------------------------------------------------------
  451. // FW_PShapeList::FW_PShapeList
  452. //----------------------------------------------------------------------------------------
  453.  
  454. FW_PShapeList::FW_PShapeList(FW_CReadableStream& stream) :
  455.     FW_TCountedPtr<FW_CShapeListRep>(FW_NEW(FW_CShapeListRep, (stream)))
  456. {
  457. }
  458.  
  459. //----------------------------------------------------------------------------------------
  460. // FW_PShapeList::~FW_PShapeList
  461. //----------------------------------------------------------------------------------------
  462.  
  463. FW_PShapeList::~FW_PShapeList()
  464. {
  465. }
  466.  
  467. //----------------------------------------------------------------------------------------
  468. // FW_PShapeList::FW_PShapeList
  469. //----------------------------------------------------------------------------------------
  470.  
  471. FW_PShapeList::FW_PShapeList(const FW_PShapeList& other)
  472. {
  473.     SetRep(other.fRep);
  474. }
  475.  
  476. //----------------------------------------------------------------------------------------
  477. // FW_PShapeList::operator=
  478. //----------------------------------------------------------------------------------------
  479.  
  480. FW_PShapeList& FW_PShapeList::operator=(const FW_PShapeList& other)
  481. {
  482.     FW_TCountedPtr<FW_CShapeListRep>::operator=(other);
  483.     return *this;
  484. }
  485.  
  486. //----------------------------------------------------------------------------------------
  487. // FW_PShapeList::Copy
  488. //----------------------------------------------------------------------------------------
  489.  
  490. FW_PShapeList FW_PShapeList::Copy() const
  491. {
  492.     FW_PShapeList list;
  493.     list.SetRep(FW_NEW(FW_CShapeListRep, (*fRep)));
  494.     return list;
  495. }
  496.  
  497. //----------------------------------------------------------------------------------------
  498. //    operator <<
  499. //----------------------------------------------------------------------------------------
  500.  
  501. FW_CWritableStream& operator << (FW_CWritableStream& stream, const FW_PShapeList& list)
  502. {
  503.     list.fRep->Write(stream);
  504.     return stream;
  505. }
  506.  
  507. //----------------------------------------------------------------------------------------
  508. //    operator >>
  509. //----------------------------------------------------------------------------------------
  510.  
  511. FW_CReadableStream& operator >> (FW_CReadableStream& stream, FW_PShapeList& list)
  512. {
  513.     list.SetRep(FW_NEW(FW_CShapeListRep, (stream)));
  514.     return stream;
  515. }
  516.  
  517.