home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / OLEDOC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  6.1 KB  |  310 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of TOleDocument. Doc/View document that supports Ole2
  6. //   using OCF TOcDocument
  7. //----------------------------------------------------------------------------
  8. #define INC_OLE2
  9. #include <owl/owlpch.h>
  10. #include <owl/docmanag.h>
  11. #include <owl/olemdifr.h>
  12. #include <ocf/ocfpch.h>
  13. #include <ocf/ocdoc.h>
  14. #include <ocf/ocapp.h>
  15. #include <owl/oledoc.h>
  16. #include <owl/oleframe.h>
  17. #include <owl/oleview.h>
  18.  
  19.  
  20. TOleDocument::TOleDocument(TDocument* parent)
  21. :
  22.   TStorageDocument(parent),
  23.   OcDoc(0),
  24.   Closing(false)
  25. {
  26. }
  27.  
  28. //
  29. // For an OLE container the compound file remains open
  30. // until the application shuts down
  31. //
  32. TOleDocument::~TOleDocument()
  33. {
  34.   delete OcDoc;
  35. }
  36.  
  37. //
  38. // Prepare document shutdown
  39. //
  40. bool
  41. TOleDocument::CanClose()
  42. {
  43.   // Just say yes if we are already in the closing process, or are embedded,
  44.   // or have multiple views open
  45.   //
  46.   if (Closing || IsEmbedded())
  47.     return true;
  48.  
  49.   return TDocument::CanClose();
  50. }
  51.  
  52. //
  53. //  Shut down the TOleView's
  54. //
  55. void
  56. TOleDocument::OleViewClose()
  57. {
  58.   TView* curView = GetViewList();
  59.   while (curView) {
  60.     TOleView* oleView = TYPESAFE_DOWNCAST(curView, TOleView);
  61.     if (oleView)
  62.       oleView->OleShutDown();
  63.  
  64.     curView = curView->GetNextView();
  65.   }
  66. }
  67.  
  68. //
  69. // Close the compound file
  70. //
  71. bool
  72. TOleDocument::Close()
  73. {
  74.   // Make sure that TOleView's are closed first
  75.   //
  76.   OleViewClose();
  77.   OcDoc->Close();
  78.   return TStorageDocument::Close();
  79. }
  80.  
  81. //
  82. // Close the OLE document when the server is done with the
  83. // given IStorage from its container
  84. //
  85. bool
  86. TOleDocument::ReleaseDoc()
  87. {
  88.   PRECONDITION(OcDoc);
  89.  
  90.   TStorageDocument::ReleaseDoc();
  91.   OcDoc->SetStorage((IStorage*)0);
  92.  
  93.   return true;
  94. }
  95.  
  96. //
  97. // Open the OLE document when the server is provided with an
  98. // IStorage from its container
  99. //
  100. bool
  101. TOleDocument::SetStorage(IStorage* stg, bool remember)
  102. {
  103.   PRECONDITION(OcDoc);
  104.  
  105.   // If a storage is provided, then we are now using container's IStorage
  106.   //
  107.   if (stg)
  108.     Embedded = true;
  109.   
  110.   OcDoc->SetStorage(stg, remember);
  111.   TStorageDocument::SetStorage(stg, remember);
  112.  
  113.   return true;
  114. }
  115.  
  116. //
  117. // Restores the original root IStorage before the save operation
  118. //
  119. bool
  120. TOleDocument::RestoreStorage()
  121. {
  122.   PRECONDITION(OcDoc);
  123.  
  124.   OcDoc->RestoreStorage();
  125.   TStorageDocument::RestoreStorage();
  126.  
  127.   return true;
  128. }
  129.  
  130. //
  131. // Set the initial open mode
  132. //
  133. void
  134. TOleDocument::PreOpen()
  135. {
  136.   SetOpenMode(ofReadWrite | ofTransacted);
  137. }
  138.  
  139. //
  140. // Open the compound file so that we have an IStorage for use
  141. // with embedded objects. A document partner is created
  142. // to handle OLE related stuff.
  143. //
  144. bool
  145. TOleDocument::InitDoc()
  146. {
  147.   if (IsOpen())
  148.     return true; // compound file already open
  149.  
  150.   // Give user a chance to set a different open mode
  151.   //
  152.   PreOpen();
  153.  
  154.   if (GetDocPath())
  155.     SetOpenMode(GetOpenMode() | ofNoCreate);
  156.   else
  157.     SetOpenMode(GetOpenMode() | ofTemporary);
  158.  
  159.   if (TStorageDocument::Open(GetOpenMode(), GetDocPath())) {
  160.     if (OcDoc) { // use the existing ocdoc
  161.       OcDoc->SetStorage(StorageI);
  162.     }
  163.     else if (GetOcApp()) {
  164.       OcDoc = new TOcDocument(*GetOcApp(), GetDocPath(), StorageI);
  165.     }
  166.  
  167.     return true;
  168.   }
  169.  
  170.   return false;
  171. }
  172.  
  173. //
  174. // Save the embedded objects, if any
  175. //
  176. bool
  177. TOleDocument::Commit(bool force)
  178. {
  179.   if (Write())
  180.     return TStorageDocument::Commit(force);
  181.   else
  182.     return false;
  183. }
  184.  
  185. //
  186. // Load the embedded objects, if any
  187. //
  188. bool
  189. TOleDocument::Open(int, const char far* path)
  190. {
  191.   if (path)
  192.     SetDocPath(path);
  193.  
  194.   return Read();
  195. }
  196.  
  197. //
  198. // Check if current document path is the same as the
  199. // OcDoc's.
  200. //
  201. bool TOleDocument::PathChanged()
  202. {
  203.   string::set_case_sensitive(false);
  204.   return OcDoc->GetName() != string(GetDocPath());
  205. }
  206.  
  207. //
  208. // Save embed objects to the compound file
  209. //
  210. bool
  211. TOleDocument::Write()
  212. {
  213.   // Switch to new storage if path has changed & it is permanent ("SaveAs")
  214.   //
  215.   IStorage* newStorageI;
  216.   bool saveAs = PathChanged() && !OrgStorageI;      // also is 'remember'
  217.   bool sameAsLoad = !PathChanged() && !OrgStorageI; // use current storage
  218.   if (saveAs) {
  219.     // Update link monikers
  220.     //
  221.     string newName(GetDocPath());
  222.     OcDoc->SetName(newName);
  223.  
  224.     if (IsEmbedded())
  225.       newStorageI = StorageI; // Use the one assigned by container
  226.     else
  227.       newStorageI = GetNewStorage();
  228.   }
  229.   else
  230.     newStorageI = StorageI;
  231.  
  232.   return newStorageI ?
  233.     OcDoc->SaveParts(newStorageI, sameAsLoad, saveAs) :
  234.     false;
  235. }
  236.  
  237. //
  238. // Load embed objects from the compound file
  239. //
  240. bool
  241. TOleDocument::Read()
  242. {
  243.   // Load the embedded objects, if any
  244.   //
  245.   return OcDoc->LoadParts();
  246. }
  247.  
  248. //
  249. // Get a new IStorage, typically in a SaveAs
  250. // situation.
  251. //
  252. IStorage*
  253. TOleDocument::GetNewStorage()
  254. {
  255.   PRECONDITION(StorageI);
  256.  
  257.   // Create another root storage based on the new doc path
  258.   // Should close the current document and open a new one.
  259.   //
  260.   IStorage* newStorage = 0;
  261.  
  262.   HRESULT hres;
  263.   STATSTG stgInfo;
  264.   if (!SUCCEEDED(StorageI->Stat(&stgInfo, STATFLAG_NONAME)))
  265.     return 0;
  266.  
  267.   hres = ::StgOpenStorage(OleStr(GetDocPath()), 0, stgInfo.grfMode, 0, 0, &newStorage);
  268.   if (!SUCCEEDED(hres))
  269.     hres = ::StgCreateDocfile(OleStr(GetDocPath()), stgInfo.grfMode, 0, &newStorage);
  270.  
  271.   if (SUCCEEDED(hres)) {
  272.     StorageI->Release(); // Release the old root storage
  273.     StorageI = newStorage;
  274.   }
  275.  
  276.   return newStorage;
  277. }
  278.  
  279. //
  280. // Revert to last saved compound file
  281. //
  282. bool
  283. TOleDocument::Revert(bool clear)
  284. {
  285.   if (!StorageI)
  286.     return true;                    // return OK if storage already released
  287.  
  288.   if (!TDocument::Revert(clear) || !ReleaseDoc())
  289.     return false;
  290.  
  291.   if (!clear) {
  292.     InitDoc();
  293.     Open(0);
  294.   }
  295.  
  296.   SetDirty(false);
  297.   return true;
  298. }
  299.  
  300. //
  301. // Get OCF application partner
  302. //
  303. TOcApp*
  304. TOleDocument::GetOcApp()
  305. {
  306.   TOleFrame* olefr = TYPESAFE_DOWNCAST(GetDocManager().GetApplication()->GetMainWindow(), TOleFrame);
  307.  
  308.   return olefr->GetOcApp();
  309. }
  310.