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 / Framewrk / FWPart / FWLnkCmd.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  16.1 KB  |  467 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWLnkCmd.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWLNKCMD_H
  13. #include "FWLnkCmd.h"
  14. #endif
  15.  
  16. #ifndef FWLNKMGR_H
  17. #include "FWLnkMgr.h"
  18. #endif
  19.  
  20. #ifndef FWLNKDST_H
  21. #include "FWLnkDst.h"
  22. #endif
  23.  
  24. #ifndef FWLNKSRC_H
  25. #include "FWLnkSrc.h"
  26. #endif
  27.  
  28. #ifndef FWPART_H
  29. #include "FWPart.h"
  30. #endif
  31.  
  32. // ----- OS Layer -----
  33.  
  34. #ifndef SLODFSTR_K
  35. #include "SLODFStr.k"
  36. #endif
  37.  
  38. #ifndef SLODFSTR_H
  39. #include "SLODFStr.h"
  40. #endif
  41.  
  42. #ifndef FWMENUS_K
  43. #include "FWMenus.k"
  44. #endif
  45.  
  46. // ----- OpenDoc Includes -----
  47.  
  48. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  49. #include <StdTypes.xh>
  50. #endif
  51.  
  52. #ifndef SOM_ODSession_xh
  53. #include <ODSessn.xh>
  54. #endif
  55.  
  56. #ifndef SOM_ODDraft_xh
  57. #include <Draft.xh>
  58. #endif
  59.  
  60. #ifndef SOM_ODLinkSource_xh
  61. #include <LinkSrc.xh>
  62. #endif
  63.  
  64. //========================================================================================
  65. //    Runtime Info
  66. //========================================================================================
  67.  
  68. #ifdef FW_BUILD_MAC
  69. #pragma segment odflinking
  70. #endif
  71.  
  72. FW_DEFINE_AUTO(FW_CPrivCreateLinkSourceCommand)
  73. FW_DEFINE_AUTO(FW_CPrivCreateLinkCommand)
  74.  
  75. //========================================================================================
  76. // FW_CPrivCreateLinkSourceCommand class
  77. //========================================================================================
  78.  
  79. //----------------------------------------------------------------------------------------
  80. // FW_CPrivCreateLinkSourceCommand constructor
  81. //----------------------------------------------------------------------------------------
  82. FW_CPrivCreateLinkSourceCommand::FW_CPrivCreateLinkSourceCommand(Environment* ev,
  83.                                              FW_CFrame* frame,
  84.                                              FW_CLinkManager* linkMgr,
  85.                                              FW_CLinkSource* pendingLink) 
  86. :    FW_CCommand(ev, FW_kCreateLinkSourceCommand, frame, FW_kCanUndo),
  87.     fLinkMgr(linkMgr),
  88.     fLinkSource(pendingLink)
  89. {
  90.     // This command's Undo strings don't matter, because this transaction is always 
  91.     // bracketted by kODBegin/EndAction transactions.
  92.     this->SetMenuStrings(ev, "Undo", "Redo");
  93.  
  94.     FW_END_CONSTRUCTOR
  95. }
  96.  
  97. //----------------------------------------------------------------------------------------
  98. // FW_CPrivCreateLinkSourceCommand destructor
  99. //----------------------------------------------------------------------------------------
  100. FW_CPrivCreateLinkSourceCommand::~FW_CPrivCreateLinkSourceCommand()
  101. {
  102.     FW_START_DESTRUCTOR
  103. }
  104.  
  105. //----------------------------------------------------------------------------------------
  106. // FW_CPrivCreateLinkSourceCommand::CommitUndone
  107. //----------------------------------------------------------------------------------------
  108. void FW_CPrivCreateLinkSourceCommand::CommitUndone(Environment*)
  109. {
  110.     if (fLinkSource)
  111.     {
  112.         delete fLinkSource;
  113.         fLinkSource = NULL;
  114.     }
  115. }
  116.  
  117. //----------------------------------------------------------------------------------------
  118. // FW_CPrivCreateLinkSourceCommand::DoIt
  119. //----------------------------------------------------------------------------------------
  120. void FW_CPrivCreateLinkSourceCommand::DoIt(Environment* ev)
  121. {
  122.     if (fLinkSource)
  123.     {
  124.         FW_CPart* part = GetPart(ev);
  125.         ODLinkSource* odLinkSource = part->GetDraft(ev)->CreateLinkSource(ev, part->GetODPart(ev));
  126.         fLinkSource->SetODLinkSource(ev, odLinkSource);
  127.     
  128.         //--- Establish the link ---
  129.         fLinkMgr->AddToSourceLinkList(ev, fLinkSource);
  130.         fLinkSource->PrivLinkEstablished(ev);
  131.     }
  132.     else
  133.     {
  134.         // if nothing comes of this, then it won't cause any change!
  135.         this->SetCausesChange(ev, false);
  136.     }
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // FW_CPrivCreateLinkSourceCommand::GetODLinkSource
  141. //----------------------------------------------------------------------------------------
  142. ODLinkSource* FW_CPrivCreateLinkSourceCommand::GetODLinkSource(Environment* ev)
  143. {
  144.     if (fLinkSource)
  145.         return fLinkSource->GetODLinkSource(ev);
  146.     return NULL;
  147. }
  148.  
  149. //----------------------------------------------------------------------------------------
  150. // FW_CPrivCreateLinkSourceCommand::UndoIt
  151. //----------------------------------------------------------------------------------------
  152. void FW_CPrivCreateLinkSourceCommand::UndoIt(Environment* ev)
  153. {
  154.     if (fLinkSource)
  155.         fLinkMgr->BreakSourceLink(ev, fLinkSource);        // calls RemoveFromSourceLinkList
  156. }
  157.  
  158. //----------------------------------------------------------------------------------------
  159. // FW_CPrivCreateLinkSourceCommand::RedoIt
  160. //----------------------------------------------------------------------------------------
  161. void FW_CPrivCreateLinkSourceCommand::RedoIt(Environment* ev)
  162. {
  163.     //--- Re-establish the link ---
  164.     if (fLinkSource)
  165.         fLinkMgr->RestoreSourceLink(ev, fLinkSource);    // calls AddToSourceLinkList
  166. }
  167.  
  168. //----------------------------------------------------------------------------------------
  169. // FW_CPrivCreateLinkSourceCommand::PropagateChanges
  170. //----------------------------------------------------------------------------------------
  171. void  FW_CPrivCreateLinkSourceCommand::PropagateChanges(Environment* ev,  ODUpdateID id )
  172. {
  173. FW_UNUSED(id);
  174.  
  175.     // just creating or removing link source should not propagate out the frame hierarchy, because
  176.     // link updates only deal with content, not with contained links. The link source
  177.     // contains existing, unchanged content.
  178.     
  179.     // It is a change to the document, so just dirty stuff
  180.     GetPart(ev)->Changed(ev);
  181. }
  182.  
  183.  
  184. //----------------------------------------------------------------------------------------
  185. // FW_CPrivCreateLinkSourceCommand::SetLinkSource
  186. //----------------------------------------------------------------------------------------
  187. void FW_CPrivCreateLinkSourceCommand::SetLinkSource(Environment* ev, FW_CLinkSource* linkSrc)
  188. {
  189.     FW_ASSERT(fLinkSource == NULL);
  190.     FW_ASSERT(linkSrc != NULL);
  191.     
  192.     fLinkSource = linkSrc;
  193.     
  194.     // everything was suppressed during execute. It just placed an action in the undo history
  195.     this->DoIt(ev);  
  196.     this->SetCausesChange(ev, true);
  197.     this->PropagateChanges(ev);
  198. }
  199.  
  200.  
  201. //========================================================================================
  202. // FW_CPrivCreateLinkCommand class
  203. //========================================================================================
  204.  
  205. //----------------------------------------------------------------------------------------
  206. // FW_CPrivCreateLinkCommand constructor
  207. //----------------------------------------------------------------------------------------
  208. FW_CPrivCreateLinkCommand::FW_CPrivCreateLinkCommand(Environment* ev,
  209.                                              FW_CFrame* frame,
  210.                                              FW_CLinkManager* linkMgr,
  211.                                              FW_Boolean fromClipboard) 
  212. :    FW_CCommand(ev, FW_kCreateLinkCommand, frame, FW_kCanUndo),
  213.     fLinkMgr(linkMgr),
  214.     fLink(NULL)
  215. {
  216.     if (fromClipboard)
  217.     {
  218.         SetActionType(ev, kODBeginAction);
  219.         // ODPG says to use kODEndAction, but my PasteAs command adds its Undo action
  220.         // _after_ the link has been created, so I have to reverse the order of transactions
  221.  
  222.         // Set Undo strings to the ones used for the Paste As command
  223.         FW_CString undoString;
  224.         FW_CString redoString;
  225.         ::FW_PrivLoadUndoStrings(ev, FW_kUndoPasteAsMsg, undoString, redoString);
  226.         SetMenuStrings(ev, undoString, redoString);
  227.     }
  228.     else
  229.     {
  230.         // For Drag&Drop use the default action type, kODSingleAction.
  231.         // The Undo strings don't matter, because this transaction will be 
  232.         // bracketted by kODBegin/EndAction transactions.
  233.         this->SetMenuStrings(ev, "", "");
  234.     }
  235.  
  236.     FW_END_CONSTRUCTOR
  237. }
  238.  
  239. //----------------------------------------------------------------------------------------
  240. // FW_CPrivCreateLinkCommand destructor
  241. //----------------------------------------------------------------------------------------
  242. FW_CPrivCreateLinkCommand::~FW_CPrivCreateLinkCommand()
  243. {
  244.     FW_START_DESTRUCTOR
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------
  248. // FW_CPrivCreateLinkCommand::CommitUndone
  249. //----------------------------------------------------------------------------------------
  250. void FW_CPrivCreateLinkCommand::CommitUndone(Environment*)
  251. {
  252.     // The link creation was Undone
  253.     delete fLink;
  254.     fLink = NULL;
  255. }
  256.  
  257. //----------------------------------------------------------------------------------------
  258. // FW_CPrivCreateLinkCommand::DoIt
  259. //----------------------------------------------------------------------------------------
  260. void FW_CPrivCreateLinkCommand::DoIt(Environment* ev)
  261. {
  262. FW_UNUSED(ev);
  263.     // The main purpose of this command is to add a transaction to the Undo history
  264.     FW_ASSERT(GetCanUndo(ev));
  265.     // Nothing else to do
  266. }
  267.  
  268. //----------------------------------------------------------------------------------------
  269. // FW_CPrivCreateLinkCommand::UndoIt
  270. //----------------------------------------------------------------------------------------
  271. void FW_CPrivCreateLinkCommand::UndoIt(Environment* ev)
  272. {
  273.     //--- Remove the link ---
  274.     FW_ASSERT(fLink);
  275.     
  276.     // It's not necessary to BreakDestinationLink.  That involves changing any embedded
  277.     // frame's link status, but they've been or are being removed, and will, if this is
  278.     // redone, be reinstated with the same link status.  Besides that,
  279.     // ODFrame::ChangeLinkStatus crashes when called on a detatched frame!
  280.     
  281.     
  282.     // just unregister the link and remove it from the part.
  283.     fLink->Unregister(ev);
  284.     fLinkMgr->RemoveFromDestLinkList(ev, fLink);
  285. }
  286.  
  287. //----------------------------------------------------------------------------------------
  288. // FW_CPrivCreateLinkCommand::RedoIt
  289. //----------------------------------------------------------------------------------------
  290. void FW_CPrivCreateLinkCommand::RedoIt(Environment* ev)
  291. {
  292.     //--- Restore the link ---
  293.     FW_ASSERT(fLink);
  294.     
  295.     // add link to back to linkmgr, then register it.  It won't be able to get any resultant
  296.     // update if it's not added back first
  297.     
  298.     fLinkMgr->AddToDestLinkList(ev, fLink);
  299.     fLink->Register(ev, GetPart(ev));
  300. }
  301.  
  302. //========================================================================================
  303. // FW_CBreakLinkCommand class
  304. //========================================================================================
  305.  
  306. FW_DEFINE_AUTO(FW_CBreakLinkCommand)
  307.  
  308. //----------------------------------------------------------------------------------------
  309. // FW_CBreakLinkCommand constructor
  310. //----------------------------------------------------------------------------------------
  311. FW_CBreakLinkCommand::FW_CBreakLinkCommand(Environment* ev,
  312.                             FW_CFrame* frame,
  313.                             FW_CLinkDestination* link,
  314.                             FW_CLinkManager* linkMgr)
  315. :    FW_CCommand(ev, FW_kBreakLinkCommand, frame, FW_kCanUndo),
  316.     fLink(link),
  317.     fLinkMgr(linkMgr)
  318. {
  319.     FW_CString undoString;
  320.     FW_CString redoString;
  321.     
  322.     ::FW_PrivLoadUndoStrings(ev, FW_kUndoBreakLinkMsg, undoString, redoString);
  323.     SetMenuStrings(ev, undoString, redoString);
  324.  
  325.     FW_END_CONSTRUCTOR
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. // FW_CBreakLinkCommand destructor
  330. //----------------------------------------------------------------------------------------
  331. FW_CBreakLinkCommand::~FW_CBreakLinkCommand()
  332. {
  333.     FW_START_DESTRUCTOR
  334. }
  335.  
  336. //----------------------------------------------------------------------------------------
  337. // FW_CBreakLinkCommand::CommitDone
  338. //----------------------------------------------------------------------------------------
  339. void FW_CBreakLinkCommand::CommitDone(Environment* ev)
  340. {
  341. FW_UNUSED(ev);
  342.     // Delete the saved link destination only if the command was not undone
  343.     delete fLink;
  344. }
  345.  
  346. //----------------------------------------------------------------------------------------
  347. // FW_CBreakLinkCommand::DoIt
  348. //----------------------------------------------------------------------------------------
  349. void FW_CBreakLinkCommand::DoIt(Environment* ev)
  350. {
  351.     //-- Break the link --
  352.     fLinkMgr->BreakDestinationLink(ev, fLink);
  353. }
  354.  
  355. //----------------------------------------------------------------------------------------
  356. // FW_CBreakLinkCommand::UndoIt
  357. //----------------------------------------------------------------------------------------
  358. void FW_CBreakLinkCommand::UndoIt(Environment* ev)
  359. {
  360.     //-- Restore the broken link --
  361.     fLinkMgr->RestoreDestinationLink(ev, fLink);
  362.     GetPart(ev)->Changed(ev);
  363. }
  364.  
  365. //----------------------------------------------------------------------------------------
  366. // FW_CBreakLinkCommand::RedoIt
  367. //----------------------------------------------------------------------------------------
  368. void FW_CBreakLinkCommand::RedoIt(Environment* ev)
  369. {
  370.     fLinkMgr->BreakDestinationLink(ev, fLink);
  371. }
  372.  
  373. //----------------------------------------------------------------------------------------
  374. // FW_CBreakLinkCommand::PropagateChanges
  375. //----------------------------------------------------------------------------------------
  376. void  FW_CBreakLinkCommand::PropagateChanges(Environment* ev,  ODUpdateID )
  377. {
  378.     // removing or adding a link doesn't need to propagate outwards to any
  379.     // containing publishers, because only the underlying content is actually
  380.     // linked, not the contained links themselves. So just mark the part dirty
  381.     
  382.     GetPart(ev)->Changed(ev);
  383. }
  384.  
  385. //========================================================================================
  386. // FW_CBreakLinkSourceCommand class
  387. //========================================================================================
  388.  
  389. FW_DEFINE_AUTO(FW_CBreakLinkSourceCommand)
  390.  
  391. //----------------------------------------------------------------------------------------
  392. // FW_CBreakLinkSourceCommand constructor
  393. //----------------------------------------------------------------------------------------
  394. FW_CBreakLinkSourceCommand::FW_CBreakLinkSourceCommand(Environment* ev,
  395.                                     FW_CFrame* frame,
  396.                                     FW_CLinkSource* linkSource,
  397.                                     FW_CLinkManager* linkMgr)
  398. :    FW_CCommand(ev, FW_kBreakLinkCommand, frame, FW_kCanUndo),
  399.     fLinkSource(linkSource),
  400.     fLinkMgr(linkMgr)
  401. {
  402.     FW_CString undoString;
  403.     FW_CString redoString;
  404.     
  405.     ::FW_PrivLoadUndoStrings(ev, FW_kUndoBreakLinkSourceMsg, undoString, redoString);
  406.     SetMenuStrings(ev, undoString, redoString);
  407.  
  408.     FW_END_CONSTRUCTOR
  409. }
  410.  
  411. //----------------------------------------------------------------------------------------
  412. // FW_CBreakLinkSourceCommand destructor
  413. //----------------------------------------------------------------------------------------
  414. FW_CBreakLinkSourceCommand::~FW_CBreakLinkSourceCommand()
  415. {
  416.     FW_START_DESTRUCTOR
  417. }
  418.  
  419. //----------------------------------------------------------------------------------------
  420. // FW_CBreakLinkSourceCommand::CommitDone
  421. //----------------------------------------------------------------------------------------
  422. void FW_CBreakLinkSourceCommand::CommitDone(Environment* ev)
  423. {
  424. FW_UNUSED(ev);
  425.     // Delete the saved link source only if the command was not undone
  426.     delete fLinkSource;
  427. }
  428.  
  429. //----------------------------------------------------------------------------------------
  430. // FW_CBreakLinkSourceCommand::DoIt
  431. //----------------------------------------------------------------------------------------
  432. void FW_CBreakLinkSourceCommand::DoIt(Environment* ev)
  433. {
  434.     //-- Break the link --
  435.     fLinkMgr->BreakSourceLink(ev, fLinkSource);
  436. }
  437.  
  438. //----------------------------------------------------------------------------------------
  439. // FW_CBreakLinkSourceCommand::UndoIt
  440. //----------------------------------------------------------------------------------------
  441. void FW_CBreakLinkSourceCommand::UndoIt(Environment* ev)
  442. {
  443.     //-- Restore the broken link --
  444.     fLinkMgr->RestoreSourceLink(ev, fLinkSource);
  445. }
  446.  
  447. //----------------------------------------------------------------------------------------
  448. // FW_CBreakLinkSourceCommand::RedoIt
  449. //----------------------------------------------------------------------------------------
  450. void FW_CBreakLinkSourceCommand::RedoIt(Environment* ev)
  451. {
  452.     fLinkMgr->BreakSourceLink(ev, fLinkSource);
  453. }
  454.  
  455. //----------------------------------------------------------------------------------------
  456. // FW_CBreakLinkSourceCommand::PropagateChanges
  457. //----------------------------------------------------------------------------------------
  458. void  FW_CBreakLinkSourceCommand::PropagateChanges(Environment* ev,  ODUpdateID)
  459. {
  460.     // removing or adding a link doesn't need to propagate outwards to any
  461.     // containing publishers, because only the underlying content is actually
  462.     // linked, not the contained links themselves. So just mark the part dirty
  463.     
  464.     GetPart(ev)->Changed(ev);
  465. }
  466.  
  467.