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 / Draw / Sources / DrwDDCmd.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  15.7 KB  |  499 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                DrwDDCmd.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Mary Boetcher
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef DRWDDCMD_H
  15. #include "DrwDDCmd.h"
  16. #endif
  17.  
  18. #ifndef DRAWCONT_H
  19. #include "DrawCont.h"
  20. #endif
  21.  
  22. #ifndef DRAWSEL_H
  23. #include "DrawSel.h"
  24. #endif
  25.  
  26. #ifndef BASESHP_H
  27. #include "BaseShp.h"
  28. #endif
  29.  
  30. #ifndef DRAWPART_H
  31. #include "DrawPart.h"
  32. #endif
  33.  
  34. #ifndef DRAWCLIP_H
  35. #include "DrawClip.h"
  36. #endif
  37.  
  38. #ifndef DRAWLINK_H
  39. #include "DrawLink.h"
  40. #endif
  41.  
  42. // ----- FrameWork Includes -----
  43.  
  44. #ifndef FWFRAME_H
  45. #include "FWFrame.h"
  46. #endif
  47.  
  48. #ifndef FWPRESEN_H
  49. #include "FWPresen.h"
  50. #endif
  51.  
  52. #ifndef FWSESION_H
  53. #include "FWSesion.h"
  54. #endif
  55.  
  56. //========================================================================================
  57. // RunTime Info
  58. //========================================================================================
  59.  
  60. #ifdef FW_BUILD_MAC
  61. #pragma segment odfdrawcommand
  62. #endif
  63.  
  64. FW_DEFINE_AUTO(CDrawDragCommand)
  65. FW_DEFINE_AUTO(CDrawDropCommand)
  66.  
  67. //========================================================================================
  68. // class CDrawDragCommand
  69. //========================================================================================
  70.  
  71. //----------------------------------------------------------------------------------------
  72. //    CDrawDragCommand constructor
  73. //----------------------------------------------------------------------------------------
  74. CDrawDragCommand::CDrawDragCommand(Environment* ev,
  75.                                    CDrawPart* part,
  76.                                    FW_CFrame* frame,
  77.                                    CDrawSelection* selection,
  78.                                    FW_Boolean canUndo)
  79.     : FW_CDragCommand(ev, frame, canUndo),
  80.         fDrawPart(part),
  81.         fDrawSelection(selection),
  82.         fDraggedContent(NULL),
  83.         fLinkSources(NULL)
  84. {
  85.     FW_END_CONSTRUCTOR
  86. }
  87.  
  88. //----------------------------------------------------------------------------------------
  89. //    CDrawDragCommand destructor
  90. //----------------------------------------------------------------------------------------
  91.  
  92. CDrawDragCommand::~CDrawDragCommand()
  93. {
  94.     FW_START_DESTRUCTOR
  95.     delete fDraggedContent;        // Will call remove all if necessary
  96.     fDraggedContent = NULL;
  97.     
  98.     delete fLinkSources;
  99.     fLinkSources = NULL;
  100. }
  101.  
  102. //----------------------------------------------------------------------------------------
  103. //    CDrawDragCommand::SaveUndoState
  104. //----------------------------------------------------------------------------------------
  105.  
  106. void CDrawDragCommand::SaveUndoState(Environment* ev)    // Override
  107. {
  108.     FW_ASSERT(fDraggedContent == NULL);    
  109.     fDraggedContent = FW_NEW(CDrawUndoContent, (ev, fDrawPart, fDrawSelection));
  110.     
  111.     fLinkSources = fDrawSelection->GetSelectedLinkSources();
  112. }
  113.  
  114. //----------------------------------------------------------------------------------------
  115. //    CDrawDragCommand::UndoIt
  116. //----------------------------------------------------------------------------------------
  117.  
  118. void CDrawDragCommand::UndoIt(Environment* ev)    // Override
  119. {
  120.     // ----- Add the dragged shapes back into the part
  121.     CDrawContentShapeIterator ite(fDraggedContent);
  122.     for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  123.     {
  124.         shape->RestoreShape(ev);
  125.     }
  126.  
  127.     fDraggedContent->RestoreLinks(ev);
  128.     
  129.     // ----- Add the saved shapes to the selection
  130.     fDrawSelection->SelectContent(ev, fDraggedContent);
  131.     
  132.     // ----- Invalidate 
  133.     GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. //    CDrawDragCommand::RedoIt
  138. //----------------------------------------------------------------------------------------
  139.  
  140. void CDrawDragCommand::RedoIt(Environment* ev)    // Override
  141. {
  142.     // Select saved shapes
  143.     fDrawSelection->SelectContent(ev, fDraggedContent);
  144.  
  145.     fDrawSelection->ClearSelection(ev); // clear them, again
  146.     
  147.     fDraggedContent->RemoveFromLinks(ev);
  148. }
  149.  
  150. //----------------------------------------------------------------------------------------
  151. //    CDrawDragCommand::FreeUndoState
  152. //----------------------------------------------------------------------------------------
  153.  
  154. void CDrawDragCommand::FreeUndoState(Environment *ev)    // Override
  155. {
  156.     // Delete link sources left empty because all their shapes were removed
  157.     fDraggedContent->DeleteEmptyLinkSources(ev);
  158.  
  159.     // Delete undo data
  160.     fDraggedContent->DeleteSavedShapesAndLinks(ev);
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. //    CDrawDragCommand::PropagateChanges
  165. //----------------------------------------------------------------------------------------
  166.  
  167. void  CDrawDragCommand::PropagateChanges(Environment* ev,  ODUpdateID id )
  168. {
  169.     // Propagate changes should only be called when content was removed from the part.
  170.     FW_ASSERT(IsDragMoveToAnotherPart);
  171.     
  172.     if (fLinkSources)
  173.     {
  174.         //--- Update affected links ---
  175.         id = FW_CSession::UniqueUpdateID(ev);
  176.         CDrawPublishLinkCollectionIterator iter(fLinkSources);
  177.         for (CDrawPublishLink* link = iter.First(); iter.IsNotComplete(); link = iter.Next())
  178.         {
  179.             link->ContentUpdated(ev, id);
  180.         }
  181.     }
  182.     
  183.     FW_CDragCommand::PropagateChanges(ev, id);
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. //    CDrawDragCommand::DoIt
  188. //----------------------------------------------------------------------------------------
  189.  
  190. void  CDrawDragCommand::DoIt(Environment* ev)
  191. {
  192.     FW_CDragCommand::DoIt(ev);
  193.     
  194.     if (fDraggedContent)
  195.         fDraggedContent->RemoveFromLinks(ev);
  196. }
  197.  
  198. //========================================================================================
  199. // class CDrawDropCommand
  200. //========================================================================================
  201.  
  202. //----------------------------------------------------------------------------------------
  203. //    CDrawDropCommand constructor
  204. //----------------------------------------------------------------------------------------
  205. CDrawDropCommand::CDrawDropCommand(Environment* ev,
  206.                                    CDrawPart* part,
  207.                                    FW_CFrame* frame,
  208.                                    ODDragItemIterator* dropInfo, 
  209.                                    ODFacet* facet, 
  210.                                    const FW_CPoint& windowPoint,
  211.                                    FW_Boolean canUndo)
  212.     : FW_CDropCommand(ev, frame, dropInfo, facet, windowPoint, canUndo),
  213.         fDrawPart(part),
  214.         fSavedLink(NULL),
  215.         fDropDelta(FW_kZeroPoint),
  216.         fDroppedContent(NULL),
  217.         fDrawSelection((CDrawSelection*)GetPresentation(ev)->GetSelection(ev))
  218. {
  219.     FW_END_CONSTRUCTOR
  220. }
  221.  
  222. //----------------------------------------------------------------------------------------
  223. //    CDrawDropCommand destructor
  224. //----------------------------------------------------------------------------------------
  225.  
  226. CDrawDropCommand::~CDrawDropCommand()
  227. {
  228.     FW_START_DESTRUCTOR
  229.     delete fDroppedContent;
  230. }
  231.  
  232. //----------------------------------------------------------------------------------------
  233. //    CDrawDropCommand::DoDrop
  234. //----------------------------------------------------------------------------------------
  235. FW_Boolean CDrawDropCommand::DoDrop(Environment* ev, 
  236.                                 ODStorageUnit* dropSU, 
  237.                                 const FW_CPoint& mouseDownOffset, 
  238.                                 const FW_CPoint& dropPoint,
  239.                                 FW_Boolean isDropMove,
  240.                                 short itemNumber)
  241. {
  242.     if (itemNumber == 1)
  243.         fDrawSelection->CloseSelection(ev);
  244.     
  245.     FW_Boolean result = FW_CDropCommand::DoDrop(ev, dropSU, mouseDownOffset, dropPoint, isDropMove, itemNumber);
  246.     
  247.     if (result)
  248.     {    
  249.         fDrawPart->SetTool(ev, kSelectTool);
  250.         
  251.         fDrawSelection->CalcCache(ev);
  252.         
  253.         FW_CPoint newPosition(dropPoint.x - mouseDownOffset.x, dropPoint.y - mouseDownOffset.y);
  254.         
  255.         FW_CRect box = fDrawSelection->GetDragRect();
  256.  
  257.         fDrawSelection->OffsetSelection(ev, newPosition.x - box.left, newPosition.y - box.top);
  258.         
  259.         ODShape* updateShape = fDrawSelection->GetUpdateShape();
  260.         
  261.         // ----- Calculate clip -----
  262.         CDrawFacetClipper facetClipper(fDrawPart);
  263.         facetClipper.Clip(ev, fDrawSelection->GetPresentation(ev), updateShape);    
  264.  
  265.         // ----- Invalidate -----
  266.         GetPresentation(ev)->Invalidate(ev, updateShape);
  267.         
  268.         // ----- If I am not the active part I should not have a selection -----
  269.         if (!fDrawPart->HasSelectionFocus(ev))
  270.             fDrawSelection->CloseSelection(ev);
  271.     }
  272.     
  273.     return result;
  274. }
  275.  
  276. //----------------------------------------------------------------------------------------
  277. //    CDrawDropCommand::DoDroppedInSameFrame
  278. //----------------------------------------------------------------------------------------
  279.  
  280. FW_Boolean CDrawDropCommand::DoDroppedInSameFrame(Environment* ev, 
  281.                                               ODStorageUnit* dropSU, 
  282.                                               const FW_CPoint& mouseDownOffset, 
  283.                                               const FW_CPoint& dropPoint)
  284. {
  285.     FW_UNUSED(dropSU);
  286.     
  287.     FW_CPoint newPosition(dropPoint.x - mouseDownOffset.x, dropPoint.y - mouseDownOffset.y);
  288.  
  289.     FW_CRect box = fDrawSelection->GetDragRect();
  290.  
  291.     fDropDelta.Set(newPosition.x - box.left, newPosition.y - box.top);
  292.     
  293.     this->OffsetSelection(ev, fDropDelta);
  294.  
  295.     return TRUE;
  296. }
  297.  
  298. //----------------------------------------------------------------------------------------
  299. //    CDrawDropCommand::SaveRedoState
  300. //----------------------------------------------------------------------------------------
  301.  
  302. void CDrawDropCommand::SaveRedoState(Environment* ev)    // Override
  303. {
  304.     if (fSavedLink == NULL)    // if a link was created, don't need to save anything
  305.     {
  306.         // Save shapes that were just dropped
  307.         FW_ASSERT(fDroppedContent == NULL);
  308.         fDroppedContent = FW_NEW(CDrawContent, (ev, fDrawPart, fDrawSelection->GetDrawContent(ev)));
  309.     }
  310. }
  311.  
  312. //----------------------------------------------------------------------------------------
  313. //    CDrawDropCommand::UndoIt
  314. //----------------------------------------------------------------------------------------
  315. void CDrawDropCommand::UndoIt(Environment* ev)    // Override
  316. {
  317.     if (fSavedLink)
  318.     {
  319.         fSavedLink->SelectShapes(ev);            // set the selection's update shape
  320.         GetDrawLinkManager(ev)->UndoPasteAs(ev, fSavedLink);
  321.         GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  322.     }
  323.     else if (this->IsDragMoveInSameFrame(ev))    // it's a drag-move
  324.     {
  325.         // select dropped shapes and move them back to the original position
  326.         fDrawSelection->SelectContent(ev, fDroppedContent);
  327.         this->OffsetSelection(ev, -fDropDelta);
  328.     }
  329.     else
  330.     {
  331.         // select dropped shapes and remove them from the document
  332.         fDrawSelection->SelectContent(ev, fDroppedContent);
  333.         fDrawSelection->ClearSelection(ev);
  334.     }
  335. }
  336.  
  337. //----------------------------------------------------------------------------------------
  338. //    CDrawDropCommand::RedoIt
  339. //----------------------------------------------------------------------------------------
  340. void CDrawDropCommand::RedoIt(Environment* ev)    // Override
  341. {
  342.     if (fSavedLink)
  343.     {
  344.         GetDrawLinkManager(ev)->RedoPasteAs(ev, fSavedLink);
  345.         fSavedLink->SelectShapes(ev);
  346.         GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  347.     }
  348.     else if (this->IsDragMoveInSameFrame(ev))    // it's a drag-move
  349.     {
  350.         // select dropped shapes and move them to new position
  351.         fDrawSelection->SelectContent(ev, fDroppedContent);
  352.         this->OffsetSelection(ev, fDropDelta);
  353.     }
  354.     else    // dropped shapes are copies
  355.     {
  356.         // add dropped shapes back into document
  357.         this->RestoreDroppedShapes(ev);
  358.         GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  359.     }
  360. }
  361.  
  362. //----------------------------------------------------------------------------------------
  363. //    CDrawDropCommand::RestoreDroppedShapes
  364. //----------------------------------------------------------------------------------------
  365. void CDrawDropCommand::RestoreDroppedShapes(Environment* ev)
  366. {
  367.     // Add the dropped shapes back into the part
  368.     CDrawContentShapeIterator ite(fDroppedContent);
  369.     for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  370.     {
  371.         shape->RestoreShape(ev);
  372.     }
  373.  
  374.     // Add the saved shapes to the selection
  375.     fDrawSelection->SelectContent(ev, fDroppedContent);
  376. }
  377.  
  378. //----------------------------------------------------------------------------------------
  379. //    CDrawDropCommand::OffsetSelection
  380. //----------------------------------------------------------------------------------------
  381.  
  382. void CDrawDropCommand::OffsetSelection(Environment* ev, const FW_CPoint& delta)
  383. {
  384.     if (delta != FW_kZeroPoint)
  385.     {
  386.         GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  387.         fDrawSelection->OffsetSelection(ev, delta.x, delta.y);
  388.         GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  389.  
  390.         // Adjust offsets of linked shapes
  391.         CDrawContentShapeIterator it(fDrawSelection->GetDrawContent(ev));
  392.         for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  393.         {
  394.             if (shape->GetSubscribeLink() != NULL)
  395.             {
  396.                 shape->GetSubscribeLink()->AdjustUpdateOffset(ev, delta);
  397.             }
  398.         }
  399.     }
  400. }
  401.  
  402. //----------------------------------------------------------------------------------------
  403. //    CDrawDropCommand::FreeRedoState
  404. //----------------------------------------------------------------------------------------
  405.  
  406. void CDrawDropCommand::FreeRedoState(Environment* ev)    // Override
  407. {
  408.     if (!fSavedLink)  // created link deletes it's own shapes when it's deleted in FW_CPrivCreateLinkCommand::CommitUndone
  409.     {
  410.         //-- Delete the the dropped shapes
  411.         CBaseShape* shape;
  412.         while ((shape = fDroppedContent->GetFirstShape()) != NULL)
  413.         {
  414.             fDroppedContent->RemoveShape(ev, shape);
  415.             delete shape;
  416.         }
  417.     }
  418. }
  419.  
  420. //----------------------------------------------------------------------------------------
  421. //    CDrawDropCommand::GetDrawLinkManager
  422. //----------------------------------------------------------------------------------------
  423.  
  424. CDrawLinkManager* CDrawDropCommand::GetDrawLinkManager(Environment* ev) const
  425. {
  426.     return (CDrawLinkManager*) fDrawPart->GetLinkManager(ev);
  427. }
  428.  
  429. //----------------------------------------------------------------------------------------
  430. //    CDrawDropCommand::DoDroppedPasteAs
  431. //----------------------------------------------------------------------------------------
  432.  
  433. void CDrawDropCommand::DoDroppedPasteAs(Environment* ev, 
  434.                                     const FW_CPoint& mouseDownOffset, 
  435.                                     const FW_CPoint& dropPoint)
  436. {
  437.     FW_CPoint newPosition(dropPoint.x - mouseDownOffset.x, dropPoint.y - mouseDownOffset.y);
  438.     fDrawSelection->CalcCache(ev);
  439.     FW_CRect box = fDrawSelection->GetDragRect();
  440.     fDropDelta.Set(newPosition.x - box.left, newPosition.y - box.top);
  441.  
  442.     fSavedLink = (CDrawSubscribeLink*) this->GetNewLink(ev);
  443.  
  444.     if (fSavedLink)
  445.     {
  446.         if (!fSavedLink->IsEstablished(ev))    // cross-doc link not yet established
  447.             fDropDelta = FW_kZeroPoint;        // puts linked shapes in same position as original
  448.  
  449.         // ----- Save information about newly-created link
  450.         fSavedLink->SetUpdateOffset(ev, fDropDelta);
  451.     
  452.         if (fDropDelta != FW_kZeroPoint)
  453.         {
  454.             // invalidate and clear the current selection
  455.             GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  456.             fDrawSelection->CloseSelection(ev);    // empty the selection
  457.     
  458.             // Select the newly-subscribed shapes
  459.             fSavedLink->SelectShapes(ev);
  460.     
  461.             // offset the dropped shapes
  462.             fDrawSelection->OffsetSelection(ev, fDropDelta.x, fDropDelta.y);
  463.             GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  464.         }
  465.     }
  466.     else    // no link created - must be Embed As
  467.     {
  468.         fDrawPart->SetTool(ev, kSelectTool);
  469.         
  470.         fDrawSelection->OffsetSelection(ev, fDropDelta.x, fDropDelta.y);
  471.         ODShape* updateShape = fDrawSelection->GetUpdateShape();
  472.         
  473.         // ----- Calculate clip -----
  474.         CDrawFacetClipper facetClipper(fDrawPart);
  475.         facetClipper.Clip(ev, fDrawSelection->GetPresentation(ev), updateShape);    
  476.  
  477.         // ----- Invalidate -----
  478.         GetPresentation(ev)->Invalidate(ev, updateShape);
  479.     }
  480. }
  481.  
  482. //----------------------------------------------------------------------------------------
  483. //    CDrawDropCommand::PropagateChanges
  484. //----------------------------------------------------------------------------------------
  485.  
  486. void CDrawDropCommand::PropagateChanges(Environment* ev, ODUpdateID id )
  487. {
  488.  
  489.     if (this->IsDragMoveInSameFrame(ev))
  490.     {
  491.         if (id == kODUnknownUpdate)
  492.             id = FW_CSession::UniqueUpdateID(ev);
  493.             
  494.         fDrawSelection->SelectionChanged(ev, id);
  495.     }
  496.  
  497.     FW_CDropCommand::PropagateChanges(ev, id);
  498. }
  499.