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 / ODF / Framewrk / FWPart / Sources / FWCmd.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  11.0 KB  |  424 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWCmd.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWCMD_H
  13. #include "FWCmd.h"
  14. #endif
  15.  
  16. // ----- Framework Includes -----
  17.  
  18. #ifndef FWPART_H
  19. #include "FWPart.h"
  20. #endif
  21.  
  22. #ifndef FWFRAME_H
  23. #include "FWFrame.h"
  24. #endif
  25.  
  26. #ifndef FWSELECT_H
  27. #include "FWSelect.h"
  28. #endif
  29.  
  30. #ifndef FWPRESEN_H
  31. #include "FWPresen.h"
  32. #endif
  33.  
  34. // ----- OS Layer -----
  35.  
  36. #ifndef FWMENU_K
  37. #include "FWMenu.k"
  38. #endif
  39.  
  40. #ifndef FWRESACC_H
  41. #include "FWResAcc.h"
  42. #endif
  43.  
  44. #ifndef FWRESTYP_H
  45. #include "FWResTyp.h"
  46. #endif
  47.  
  48. #ifndef SLODFSTR_K
  49. #include "SLODFStr.k"
  50. #endif
  51.  
  52. #ifndef SLODFSTR_H
  53. #include "SLODFStr.h"
  54. #endif
  55.  
  56. // ----- Foundation Layer -----
  57.  
  58. #ifndef FWCFMRES_H
  59. #include "FWCFMRes.h"
  60. #endif
  61.  
  62. #ifndef FWMEMMGR_H
  63. #include "FWMemMgr.h"
  64. #endif
  65.  
  66. #ifndef FWSTRS_H
  67. #include "FWStrs.h"
  68. #endif
  69.  
  70. #ifndef FWBNDSTR_H
  71. #include "FWBndStr.h"
  72. #endif
  73.  
  74. // ----- OpenDoc Includes -----
  75.  
  76. // for ODIText
  77. #ifndef _ITEXT_
  78. #include "IText.h"
  79. #endif
  80.  
  81. #ifndef SOM_Module_OpenDoc_Commands_defined
  82. #include <CmdDefs.xh>
  83. #endif
  84.  
  85. #ifndef SOM_ODPart_xh
  86. #include "Part.xh"
  87. #endif
  88.  
  89. #ifndef SOM_ODFrame_xh
  90. #include "Frame.xh"
  91. #endif
  92.  
  93. #ifndef SOM_ODStorageUnit_xh
  94. #include <StorageU.xh>
  95. #endif
  96.  
  97. #ifndef SOM_ODSession_xh
  98. #include <ODSessn.xh>
  99. #endif
  100.  
  101. // ----- Platform Includes -----
  102.  
  103. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  104. #include <Script.h>
  105. #endif
  106.  
  107. #if defined(FW_BUILD_WIN32S) && !defined(__WINNT_H)
  108. #include <winnt.h>
  109. #endif
  110.  
  111.  
  112. //========================================================================================
  113. //    Runtime Info
  114. //========================================================================================
  115.  
  116. #ifdef FW_BUILD_MAC
  117. #pragma segment odfcommands
  118. #endif
  119.  
  120. FW_DEFINE_AUTO(FW_CCommand)
  121.  
  122. //========================================================================================
  123. // FW_CCommand class
  124. //========================================================================================
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // FW_CCommand constructor
  128. //----------------------------------------------------------------------------------------
  129.  
  130. FW_CCommand::FW_CCommand(Environment* ev,
  131.                          ODCommandID id,
  132.                          FW_CFrame* frame,
  133.                          FW_Boolean canUndo) :
  134.     fCommandID(id),
  135.     fCanUndo(canUndo),
  136.     fCausesChange(TRUE),
  137.     fActionType(kODSingleAction),
  138.     fPart(NULL),
  139.     fFrame(frame),        
  140.     fUndoString(NULL),
  141.     fRedoString(NULL),
  142.     fUndo(NULL),
  143.     fHasAddedAction(FALSE)
  144. {
  145.     FW_ASSERT(fFrame);
  146.     
  147.     fPart = fFrame->GetPart(ev);
  148.     fPart->GetODPart(ev)->Acquire(ev);
  149.  
  150.     // [HLX] ATTENTION: Because secondary frames may have dissepear during Undo
  151.     // we need to use the source frame
  152.     fSourceFrame = fFrame->GetSourceFrame(ev);
  153.  
  154.     fPresentation = fFrame->GetPresentation(ev);
  155.     fUndo = fPart->GetSession(ev)->GetUndo(ev);
  156.  
  157.     FW_END_CONSTRUCTOR
  158. }
  159.  
  160. //----------------------------------------------------------------------------------------
  161. // FW_CCommand destructor
  162. //----------------------------------------------------------------------------------------
  163.  
  164. FW_CCommand::~FW_CCommand()
  165. {
  166.     FW_START_DESTRUCTOR
  167.  
  168.     FW_SOMEnvironment ev;    
  169.     fPart->GetODPart(ev)->Release(ev);
  170.  
  171.     PrivDisposeUndoStrings();
  172. }
  173.  
  174. //----------------------------------------------------------------------------------------
  175. // FW_CCommand::PrivDisposeUndoStrings
  176. //----------------------------------------------------------------------------------------
  177.  
  178. void FW_CCommand::PrivDisposeUndoStrings()
  179. {
  180.     if (fUndoString)
  181.         ::DisposeIText(fUndoString);
  182.     if (fRedoString)
  183.         ::DisposeIText(fRedoString);
  184.     
  185.     fUndoString = fRedoString = NULL;
  186. }
  187.  
  188. //----------------------------------------------------------------------------------------
  189. // FW_CCommand::Execute
  190. //----------------------------------------------------------------------------------------
  191. //    Note: Execute returns true if the command is on the undo stack. It returns false if the
  192. //    command has been deleted because it was not undoable
  193.  
  194. FW_Boolean FW_CCommand::Execute(Environment* ev, FW_Boolean deleteIfNotUndoable)
  195. {
  196.     FW_Boolean wasDone = FALSE;
  197.     FW_Boolean isOnUndoStack = TRUE;
  198.     FW_VOLATILE(wasDone);
  199.     
  200.     FW_TRY
  201.     {
  202.         this->DoIt(ev);
  203.         wasDone = TRUE;
  204.     
  205.         if (fCausesChange)        // ??? It might be a good idea to rethrow here
  206.             fPart->Changed(ev);    // in case we're linking
  207.             
  208.         if (GetCanUndo(ev))
  209.             AddAction(ev, fActionType, (octet*) &this, sizeof(FW_CCommand*), GetUndoString(ev), GetRedoString(ev));
  210.     }
  211.     FW_CATCH_BEGIN
  212.     FW_CATCH_EVERYTHING()
  213.     {
  214.         if (!wasDone)
  215.         {
  216.             CommitDone(ev);
  217.             delete this;
  218.             FW_THROW_SAME();
  219.         }
  220.         
  221.         // Don't rethrow if the command was completed
  222.         SetCanUndo(ev, FALSE);
  223.     }
  224.     FW_CATCH_END
  225.     
  226.     // If the command has not been added to the action stack then kill it
  227.  
  228.     if (!HasAddedAction(ev))
  229.     {
  230.         CommitDone(ev);
  231.         if (deleteIfNotUndoable)
  232.             delete this;
  233.         isOnUndoStack = FALSE;
  234.     }
  235.     
  236.     return isOnUndoStack;
  237. }
  238.  
  239. //----------------------------------------------------------------------------------------
  240. // FW_CCommand::UndoIt
  241. //----------------------------------------------------------------------------------------
  242.  
  243. void FW_CCommand::UndoIt(Environment* ev)
  244. {
  245. FW_UNUSED(ev);
  246. }
  247.  
  248. //----------------------------------------------------------------------------------------
  249. // FW_CCommand::RedoIt
  250. //----------------------------------------------------------------------------------------
  251.  
  252. void FW_CCommand::RedoIt(Environment* ev)
  253. {
  254. FW_UNUSED(ev);
  255. }
  256.  
  257. //----------------------------------------------------------------------------------------
  258. // FW_CCommand::CommitDone
  259. //----------------------------------------------------------------------------------------
  260.  
  261. void FW_CCommand::CommitDone(Environment* ev)
  262. {
  263.     // Called by FW_CPart::DisposeActionState before deleting the command
  264.     // The command has been done, and possibly redone
  265.  
  266.     if (GetCanUndo(ev))
  267.         this->FreeUndoState(ev);
  268. }
  269.  
  270. //----------------------------------------------------------------------------------------
  271. // FW_CCommand::CommitUndone
  272. //----------------------------------------------------------------------------------------
  273.  
  274. void FW_CCommand::CommitUndone(Environment* ev)
  275. {
  276.     // Called by FW_CPart::DisposeActionState before deleting the command
  277.     // The last action for this command was an Undo
  278.  
  279.     if (GetCanUndo(ev))
  280.         this->FreeRedoState(ev);
  281. }
  282.  
  283. //----------------------------------------------------------------------------------------
  284. // FW_CCommand::SaveUndoState
  285. //----------------------------------------------------------------------------------------
  286. void FW_CCommand::SaveUndoState(Environment* ev)
  287. {
  288. FW_UNUSED(ev);
  289. }
  290.  
  291. //----------------------------------------------------------------------------------------
  292. // FW_CCommand::SaveRedoState
  293. //----------------------------------------------------------------------------------------
  294. void FW_CCommand::SaveRedoState(Environment* ev)
  295. {
  296. FW_UNUSED(ev);
  297. }
  298.  
  299. //----------------------------------------------------------------------------------------
  300. // FW_CCommand::FreeUndoState
  301. //----------------------------------------------------------------------------------------
  302. void FW_CCommand::FreeUndoState(Environment* ev)
  303. {
  304. FW_UNUSED(ev);
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. // FW_CCommand::FreeRedoState
  309. //----------------------------------------------------------------------------------------
  310. void FW_CCommand::FreeRedoState(Environment* ev)
  311. {
  312. FW_UNUSED(ev);
  313. }
  314.  
  315. //----------------------------------------------------------------------------------------
  316. // FW_CCommand::SetMenuStrings
  317. //----------------------------------------------------------------------------------------
  318.  
  319. void FW_CCommand::SetMenuStrings(Environment* ev,
  320.                                  const FW_CString& undoString,
  321.                                  const FW_CString& redoString)
  322. {
  323. FW_UNUSED(ev);
  324.     PrivDisposeUndoStrings();
  325.     fUndoString = ::CopyIText(undoString.RevealODIText());
  326.     fRedoString = ::CopyIText(redoString.RevealODIText());
  327. }
  328.  
  329. //----------------------------------------------------------------------------------------
  330. // FW_CCommand::SetMenuStrings
  331. //----------------------------------------------------------------------------------------
  332.  
  333. void FW_CCommand::SetMenuStrings(Environment* ev,
  334.                                  char* undoChars,
  335.                                  char* redoChars)
  336. {
  337.     FW_CString255 undoString(undoChars);
  338.     FW_CString255 redoString(redoChars);
  339.     this->SetMenuStrings(ev, undoString, redoString);
  340. }
  341.  
  342. //----------------------------------------------------------------------------------------
  343. // FW_CCommand::SetMenuStringsFromResource
  344. //----------------------------------------------------------------------------------------
  345. void FW_CCommand::SetMenuStringsFromResource(Environment* ev, 
  346.                                              short stringResourceID, 
  347.                                              short undoStringID, 
  348.                                              short redoStringID)
  349. {
  350.     FW_CSharedLibraryResourceFile resFile(ev);
  351.     FW_CString undoString;
  352.     FW_CString redoString;
  353.     ::FW_LoadStringByID(ev, resFile, stringResourceID, MULTISTRINGRES, undoStringID, undoString);
  354.     ::FW_LoadStringByID(ev, resFile, stringResourceID, MULTISTRINGRES, redoStringID, redoString);
  355.     this->SetMenuStrings(ev, undoString, redoString);
  356. }
  357.  
  358. //----------------------------------------------------------------------------------------
  359. // FW_CCommand::AddAction
  360. //----------------------------------------------------------------------------------------
  361.  
  362. void FW_CCommand::AddAction(Environment* ev, 
  363.                             ODActionType actionType, 
  364.                             octet* dataPtr, 
  365.                             unsigned long dataSize,
  366.                             ODName* undoActionLabel, 
  367.                             ODName* redoActionLabel)
  368. {
  369.     // Set up the action data record
  370.     ODActionData actionState;
  371.     actionState._maximum = dataSize;
  372.     actionState._length = dataSize;
  373.     actionState._buffer = dataPtr;
  374.  
  375.     fUndo->AddActionToHistory(ev, fPart->GetODPart(ev),
  376.                               &actionState,
  377.                               actionType,
  378.                               undoActionLabel,
  379.                               redoActionLabel);
  380.     if (dataSize != 0)
  381.         fHasAddedAction = TRUE;    // OpenDoc has a ptr to this command object
  382. }
  383.  
  384. //----------------------------------------------------------------------------------------
  385. // FW_CCommand::IsOKtoEdit
  386. //----------------------------------------------------------------------------------------
  387.  
  388. FW_Boolean FW_CCommand::IsOKtoEdit(Environment* ev)
  389. {
  390.     FW_Boolean result = TRUE;
  391.  
  392.     if (fCausesChange)
  393.     {
  394.         if (fPart->IsReadOnly(ev))
  395.             result = FALSE;
  396.         else
  397.         {
  398.             // Check the frame's link status to see if editing is allowed
  399.             FW_ASSERT(fFrame != NULL);
  400.             ODLinkStatus status = fFrame->GetODFrame(ev)->GetLinkStatus(ev);
  401.             if (status == kODInLinkDestination)
  402.             {
  403.                 // uh-oh, make sure Part::EditInLinkAttempted is called
  404.                 result = fFrame->GetODFrame(ev)->EditInLink(ev);
  405.             }
  406.         }
  407.     }
  408.  
  409.     return result;
  410. }
  411.  
  412. //----------------------------------------------------------------------------------------
  413. // FW_CCommand::PrivLoadDefaultUndoStrings
  414. //----------------------------------------------------------------------------------------
  415.  
  416. void FW_CCommand::PrivLoadDefaultUndoStrings(Environment* ev)
  417. {
  418.     FW_CString undoString;
  419.     FW_CString redoString;
  420.     
  421.     ::FW_PrivLoadDefaultUndoStrings(ev, undoString, redoString);
  422.     SetMenuStrings(ev, undoString, redoString);
  423. }
  424.