home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Embed / Sources / Command.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  15.9 KB  |  515 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Command.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                M.Boetcher
  7. //
  8. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "Embed.hpp"
  13.  
  14. #ifndef COMMAND_H
  15. #include "Command.h"
  16. #endif
  17.  
  18. #ifndef SELECT_H
  19. #include "Select.h"
  20. #endif
  21.  
  22. #ifndef PROXY_H
  23. #include "Proxy.h"
  24. #endif
  25.  
  26. #ifndef PART_H
  27. #include "Part.h"
  28. #endif
  29.  
  30. #ifndef FRAME_H
  31. #include "Frame.h"
  32. #endif
  33.  
  34. #ifndef CONTENT_H
  35. #include "Content.h"
  36. #endif
  37.  
  38. // ----- Framework Includes -----
  39.  
  40. #ifndef FWPRESEN_H
  41. #include "FWPresen.h"
  42. #endif
  43.  
  44. // ----- OS Includes -----
  45.  
  46. #ifndef FWORDCOL_H
  47. #include "FWOrdCol.h"
  48. #endif
  49.  
  50. // ----- OpenDoc Includes -----
  51.  
  52. #ifndef SOM_Module_OpenDoc_Commands_defined
  53. #include <CmdDefs.xh>
  54. #endif
  55.  
  56. //========================================================================================
  57. // RunTime Info
  58. //========================================================================================
  59.  
  60. #ifdef FW_BUILD_MAC
  61. #pragma segment opfEmbed
  62. #endif
  63.  
  64. FW_DEFINE_AUTO(CEmbedEditCommand)
  65. FW_DEFINE_AUTO(CEmbedInsertCommand)
  66. FW_DEFINE_AUTO(CEmbedDropCommand)
  67. FW_DEFINE_AUTO(CEmbedDragCommand)
  68.  
  69. //========================================================================================
  70. // CEmbedEditCommand
  71. //========================================================================================
  72.  
  73. //----------------------------------------------------------------------------------------
  74. //    CEmbedEditCommand constructor
  75. //----------------------------------------------------------------------------------------
  76.  
  77. CEmbedEditCommand::CEmbedEditCommand(Environment *ev, 
  78.                                      ODCommandID commandID,
  79.                                      CEmbedContent* content, 
  80.                                      FW_CFrame* frame, 
  81.                                      FW_CSelection* selection,
  82.                                      FW_Boolean canUndo) :
  83.     FW_CClipboardCommand(ev, commandID, frame, canUndo),
  84.         fEmbedContent(content),
  85.         fPastedProxy(NULL),
  86.         fOldProxy(NULL)
  87. {
  88. }
  89.  
  90. //----------------------------------------------------------------------------------------
  91. //    CEmbedEditCommand destructor
  92. //----------------------------------------------------------------------------------------
  93.  
  94. CEmbedEditCommand::~CEmbedEditCommand()
  95. {
  96. }
  97.  
  98. //----------------------------------------------------------------------------------------
  99. //    CEmbedEditCommand::SaveUndoState
  100. //----------------------------------------------------------------------------------------
  101. void CEmbedEditCommand::SaveUndoState(Environment* ev)    // Override
  102. {
  103.     ODCommandID commandID = GetCommandID(ev);
  104.     
  105.     if (commandID == kODCommandCut || commandID == kODCommandClear)
  106.         fPastedProxy = fEmbedContent->GetProxy();
  107.     else if (commandID == kODCommandPaste)
  108.         fOldProxy = fEmbedContent->GetProxy();
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. //    CEmbedEditCommand::FreeUndoState
  113. //----------------------------------------------------------------------------------------
  114. void CEmbedEditCommand::FreeUndoState(Environment* ev)    // Override
  115. {
  116.     ODCommandID commandID = GetCommandID(ev);
  117.     
  118.     if (commandID == kODCommandCut || commandID == kODCommandClear)
  119.     {
  120.         delete fPastedProxy;
  121.     }
  122.     else if (commandID == kODCommandPaste)
  123.     {
  124.         // Delete the original proxy
  125.         delete fOldProxy;
  126.     }
  127. }
  128.  
  129. //----------------------------------------------------------------------------------------
  130. //    CEmbedEditCommand::SaveRedoState
  131. //----------------------------------------------------------------------------------------
  132. void CEmbedEditCommand::SaveRedoState(Environment *ev)    // Override
  133. {
  134.     if (GetCommandID(ev) == kODCommandPaste) 
  135.         fPastedProxy = fEmbedContent->GetProxy();
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. //    CEmbedEditCommand::FreeRedoState
  140. //----------------------------------------------------------------------------------------
  141. void CEmbedEditCommand::FreeRedoState(Environment* ev)    // Override
  142. {
  143.     if (GetCommandID(ev) == kODCommandPaste)
  144.     {
  145.         // Delete the pasted proxy
  146.         delete fPastedProxy;
  147.     }
  148. }
  149.  
  150. //----------------------------------------------------------------------------------------
  151. //    CEmbedEditCommand::UndoIt
  152. //----------------------------------------------------------------------------------------
  153. void CEmbedEditCommand::UndoIt(Environment *ev)    // Override
  154. {
  155.     FW_CClipboardCommand::UndoIt(ev);
  156.  
  157.     switch (GetCommandID(ev))
  158.     {
  159.         case kODCommandCut:
  160.         case kODCommandClear:
  161.             this->RestorePart(ev);
  162.             break;
  163.  
  164.         case kODCommandPaste:
  165.             this->RemovePart(ev);
  166.             this->RestoreOldPart(ev);
  167.             break;
  168.     }    
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. //    CEmbedEditCommand::RedoIt
  173. //----------------------------------------------------------------------------------------
  174. void CEmbedEditCommand::RedoIt(Environment *ev)    // Override
  175. {
  176.     FW_CClipboardCommand::RedoIt(ev);
  177.  
  178.     switch (GetCommandID(ev))
  179.     {
  180.         case kODCommandCut:
  181.         case kODCommandClear:
  182.             this->RemovePart(ev);
  183.             break;
  184.  
  185.         case kODCommandPaste:
  186.             this->RemovePart(ev);
  187.             this->RestorePart(ev);
  188.             break;
  189.     }    
  190. }
  191.  
  192. //----------------------------------------------------------------------------------------
  193. //    CEmbedEditCommand::RestorePart
  194. //----------------------------------------------------------------------------------------
  195. void CEmbedEditCommand::RestorePart(Environment *ev)    // Override
  196. {
  197.     fEmbedContent->SetProxy(fPastedProxy);
  198.     fPastedProxy->AttachEmbeddedFrames(ev);
  199.         
  200.     GetPresentation(ev)->Invalidate(ev);
  201. }
  202.  
  203. //----------------------------------------------------------------------------------------
  204. //    CEmbedEditCommand::RemovePart
  205. //----------------------------------------------------------------------------------------
  206. void CEmbedEditCommand::RemovePart(Environment *ev)    // Override
  207. {
  208.     // Clear the selection, which includes detaching the proxy frames.
  209.     fSelection->ClearSelection(ev);
  210. }
  211.  
  212. //----------------------------------------------------------------------------------------
  213. //    CEmbedEditCommand::RestoreOldPart
  214. //----------------------------------------------------------------------------------------
  215. void CEmbedEditCommand::RestoreOldPart(Environment* ev)
  216. {
  217.     // restore the previous part, if any
  218.     if (fOldProxy)
  219.     {
  220.         fEmbedContent->SetProxy(fOldProxy);
  221.         if (fOldProxy)
  222.             fOldProxy->AttachEmbeddedFrames(ev);
  223.  
  224.         // Force a redraw
  225.         GetPresentation(ev)->Invalidate(ev);
  226.     }
  227. }
  228.  
  229. //========================================================================================
  230. //    class CEmbedInsertCommand
  231. //========================================================================================
  232.  
  233. //----------------------------------------------------------------------------------------
  234. //    CEmbedInsertCommand constructor
  235. //----------------------------------------------------------------------------------------
  236.  
  237. CEmbedInsertCommand::CEmbedInsertCommand(Environment* ev,
  238.                                          FW_CEmbeddingFrame* frame,
  239.                                          const FW_PFileSpecification& fileSpec,
  240.                                          CEmbedContent* content)
  241. :    FW_CInsertCommand(ev, frame, fileSpec, FW_kCanUndo),
  242.     fPartContent(content),
  243.     fEmbedSelection(NULL),
  244.     fInsertedProxy(NULL),
  245.     fOldProxy(NULL)
  246. {
  247.     fEmbedSelection = (CEmbedSelection*) frame->GetPresentation(ev)->GetSelection(ev);
  248.     FW_END_CONSTRUCTOR
  249. }
  250.  
  251. //----------------------------------------------------------------------------------------
  252. //    CEmbedInsertCommand destructor
  253. //----------------------------------------------------------------------------------------
  254.  
  255. CEmbedInsertCommand::~CEmbedInsertCommand()
  256. {
  257. }
  258.  
  259. //---------------------------------------------------------------------------------------
  260. //    CEmbedInsertCommand::UndoIt
  261. //---------------------------------------------------------------------------------------
  262. void CEmbedInsertCommand::UndoIt(Environment* ev)
  263. {
  264.     // remove the inserted part
  265.     fEmbedSelection->ClearSelection(ev);
  266.  
  267.     // restore the previous part, if any
  268.     fPartContent->SetProxy(fOldProxy);
  269.     if (fOldProxy)
  270.         fOldProxy->AttachEmbeddedFrames(ev);
  271.  
  272.     // Force a redraw
  273.     GetPresentation(ev)->Invalidate(ev);
  274. }
  275.  
  276. //---------------------------------------------------------------------------------------
  277. //    CEmbedInsertCommand::RedoIt
  278. //---------------------------------------------------------------------------------------
  279. void CEmbedInsertCommand::RedoIt(Environment* ev)
  280. {
  281.     // remove the original embedded part
  282.     fEmbedSelection->ClearSelection(ev);
  283.  
  284.     // restore the inserted part
  285.     fPartContent->SetProxy(fInsertedProxy);
  286.     fInsertedProxy->AttachEmbeddedFrames(ev);
  287.  
  288.     // Force a redraw
  289.     GetPresentation(ev)->Invalidate(ev);
  290. }
  291.  
  292. //---------------------------------------------------------------------------------------
  293. //    CEmbedInsertCommand::CommitDone
  294. //---------------------------------------------------------------------------------------
  295. void CEmbedInsertCommand::CommitDone(Environment* ev)
  296. {
  297.     if (fOldProxy)
  298.     {
  299.         // Delete the original proxy
  300.         delete fOldProxy;
  301.         fOldProxy = NULL;
  302.     }
  303. }
  304.  
  305. //---------------------------------------------------------------------------------------
  306. //    CEmbedInsertCommand::CommitUndone
  307. //---------------------------------------------------------------------------------------
  308. void CEmbedInsertCommand::CommitUndone(Environment* ev)
  309. {
  310.     // Delete the inserted proxy
  311.     delete fInsertedProxy;
  312. }
  313.  
  314. //---------------------------------------------------------------------------------------
  315. //    CEmbedInsertCommand::SaveUndoState
  316. //---------------------------------------------------------------------------------------
  317. void CEmbedInsertCommand::SaveUndoState(Environment* ev)
  318. {
  319.     // Save the proxy for the part that's currently embedded
  320.     fOldProxy = fPartContent->GetProxy();
  321. }
  322.  
  323. //---------------------------------------------------------------------------------------
  324. //    CEmbedInsertCommand::SaveRedoState
  325. //---------------------------------------------------------------------------------------
  326. void CEmbedInsertCommand::SaveRedoState(Environment* ev)
  327. {
  328.     // Save the proxy for the part that was just inserted
  329.     fInsertedProxy = fPartContent->GetProxy();
  330. }
  331.  
  332. //========================================================================================
  333. //    class CEmbedDropCommand
  334. //========================================================================================
  335.  
  336. //----------------------------------------------------------------------------------------
  337. //    CEmbedDropCommand constructor
  338. //----------------------------------------------------------------------------------------
  339.  
  340. CEmbedDropCommand::CEmbedDropCommand(Environment* ev,
  341.                                      CEmbedContent* content,
  342.                                      FW_CFrame* frame,
  343.                                      ODDragItemIterator* dropInfo, 
  344.                                      ODFacet* odFacet,
  345.                                      const FW_CPoint& dropPoint) : 
  346.     FW_CDropCommand(ev, frame, dropInfo, odFacet, dropPoint, FW_kCanUndo),
  347.     fEmbedContent(content),
  348.     fDroppedProxy(NULL),
  349.     fOldProxy(NULL)
  350. {
  351.     fEmbedSelection = (CEmbedSelection*) frame->GetPresentation(ev)->GetSelection(ev);
  352.     this->SetMenuStrings(ev, "Undo Drop", "Redo Drop");
  353.  
  354.     FW_END_CONSTRUCTOR
  355. }
  356.  
  357. //----------------------------------------------------------------------------------------
  358. //    CEmbedDropCommand destructor
  359. //----------------------------------------------------------------------------------------
  360.  
  361. CEmbedDropCommand::~CEmbedDropCommand()
  362. {
  363. }
  364.  
  365. //---------------------------------------------------------------------------------------
  366. //    CEmbedDropCommand::UndoIt
  367. //---------------------------------------------------------------------------------------
  368. void CEmbedDropCommand::UndoIt(Environment* ev)
  369. {
  370.     FW_ASSERT(fDroppedProxy);
  371.  
  372.     // remove the dropped part
  373.     fEmbedSelection->ClearSelection(ev);
  374.  
  375.     // restore the previous part, if any
  376.     fEmbedContent->SetProxy(fOldProxy);
  377.     if (fOldProxy)
  378.         fOldProxy->AttachEmbeddedFrames(ev);
  379.  
  380.     // Force a redraw
  381.     GetPresentation(ev)->Invalidate(ev);
  382. }
  383.  
  384. //---------------------------------------------------------------------------------------
  385. //    CEmbedDropCommand::RedoIt
  386. //---------------------------------------------------------------------------------------
  387. void CEmbedDropCommand::RedoIt(Environment* ev)
  388. {
  389.     FW_ASSERT(fDroppedProxy);
  390.  
  391.     // remove the original embedded part
  392.     fEmbedSelection->ClearSelection(ev);
  393.  
  394.     // restore the dropped part
  395.     fEmbedContent->SetProxy(fDroppedProxy);
  396.     fDroppedProxy->AttachEmbeddedFrames(ev);
  397.  
  398.     // Force a redraw
  399.     GetPresentation(ev)->Invalidate(ev);
  400. }
  401.  
  402. //----------------------------------------------------------------------------------------
  403. //    CEmbedDropCommand::SaveUndoState
  404. //----------------------------------------------------------------------------------------
  405. void CEmbedDropCommand::SaveUndoState(Environment *ev)
  406. {
  407.     // Save the proxy for the part that's currently embedded
  408.     fOldProxy = fEmbedContent->GetProxy();
  409. }
  410.  
  411. //---------------------------------------------------------------------------------------
  412. //    CEmbedDropCommand::SaveRedoState
  413. //---------------------------------------------------------------------------------------
  414. void CEmbedDropCommand::SaveRedoState(Environment *ev)
  415. {
  416.     // Save the proxy for the part that was just dropped
  417.     fDroppedProxy = fEmbedContent->GetProxy();
  418. }
  419.  
  420. //---------------------------------------------------------------------------------------
  421. //    CEmbedDropCommand::CommitDone
  422. //---------------------------------------------------------------------------------------
  423. void CEmbedDropCommand::CommitDone(Environment* ev)
  424. {
  425.     if (fOldProxy)
  426.     {
  427.         // Delete the original proxy
  428.         delete fOldProxy;
  429.         fOldProxy = NULL;
  430.     }
  431. }
  432.  
  433. //---------------------------------------------------------------------------------------
  434. //    CEmbedDropCommand::CommitUndone
  435. //---------------------------------------------------------------------------------------
  436. void CEmbedDropCommand::CommitUndone(Environment* ev)
  437. {
  438.     // Delete the dropped proxy
  439.     delete fDroppedProxy;
  440. }
  441.  
  442. //========================================================================================
  443. //    class CEmbedDragCommand
  444. //========================================================================================
  445.  
  446. //----------------------------------------------------------------------------------------
  447. //    CEmbedDragCommand constructor
  448. //----------------------------------------------------------------------------------------
  449.  
  450. CEmbedDragCommand::CEmbedDragCommand(Environment* ev,
  451.                                     CEmbedContent* content,
  452.                                     FW_CFrame* frame,
  453.                                     CEmbedSelection* selection) : 
  454.     FW_CDragCommand(ev, frame, FW_kCanUndo),
  455.     fEmbedContent(content),
  456.     fEmbedSelection(selection),
  457.     fSavedProxy(NULL),
  458.     fDraggedProxy(NULL)
  459. {
  460.     fDraggedProxy = content->GetProxy();
  461. }
  462.  
  463. //----------------------------------------------------------------------------------------
  464. //    CEmbedDragCommand destructor
  465. //----------------------------------------------------------------------------------------
  466.  
  467. CEmbedDragCommand::~CEmbedDragCommand()
  468. {
  469. }
  470.  
  471. //---------------------------------------------------------------------------------------
  472. //    CEmbedDragCommand::UndoIt
  473. //---------------------------------------------------------------------------------------
  474. void CEmbedDragCommand::UndoIt(Environment* ev)
  475. {
  476.     FW_ASSERT(fSavedProxy);
  477.  
  478.     // Restore saved proxy to the table
  479.     fSavedProxy->AttachEmbeddedFrames(ev);
  480.     fEmbedContent->SetProxy(fSavedProxy);
  481. }
  482.  
  483. //---------------------------------------------------------------------------------------
  484. //    CEmbedDragCommand::RedoIt
  485. //---------------------------------------------------------------------------------------
  486. void CEmbedDragCommand::RedoIt(Environment* ev)
  487. {
  488.     fEmbedSelection->ClearSelection(ev);
  489. }
  490.  
  491. //---------------------------------------------------------------------------------------
  492. //    CEmbedDragCommand::SaveUndoState
  493. //---------------------------------------------------------------------------------------
  494. void CEmbedDragCommand::SaveUndoState(Environment* ev)
  495. {
  496. FW_UNUSED(ev);
  497.  
  498.     // Save the proxy for the part that was dragged
  499.     fSavedProxy = fDraggedProxy;
  500. }
  501.  
  502. //---------------------------------------------------------------------------------------
  503. //    CEmbedDragCommand::FreeUndoState
  504. //---------------------------------------------------------------------------------------
  505. void CEmbedDragCommand::FreeUndoState(Environment* ev)
  506. {
  507. FW_UNUSED(ev);
  508.  
  509. //    fSavedProxy = NULL;
  510.     FW_ASSERT(fSavedProxy);
  511.     delete fSavedProxy;
  512. }
  513.  
  514.  
  515.