OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
oledoc.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectComponents
3// Copyright (c) 1994, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of TOleDocument. Doc/View document that supports OLE 2
7/// using OCF TOcDocument
8//----------------------------------------------------------------------------
9#include <ocf/pch.h>
10
11
12#include <owl/docmanag.h>
13#include <ocf/olemdifr.h>
14#include <ocf/ocdoc.h>
15#include <ocf/ocapp.h>
16#include <ocf/oledoc.h>
17#include <ocf/oleframe.h>
18#include <ocf/oleview.h>
19
20namespace ocf {
21
22using namespace owl;
23
25
26//
27/// Constructs a TOleDocument object associated with the given parent TDocument object.
28//
30:
31 TStorageDocument(parent),
32 OcDoc(0),
33 Closing(false)
34{
35}
36
37//
38/// Destroys the TOleDocument object. In the case of an OLE container, the compound
39/// file remains open until all the views shut down.
40//
42{
43 delete OcDoc;
44}
45
46//
47/// Prepares the document for closing. Before closing the current document, checks
48/// to see if all child documents can be closed. If any child returns false,
49/// CanClose returns false and aborts the process. If all children return true,
50/// CanClose checks to see if the document has been changed. If so, it asks the user
51/// to save the document, discard any changes, or cancel the operation. If the
52/// document has not been changed and all child documents return true, this CanClose
53/// function returns true, thus indicating that the document can be closed.
54/// CanClose also calls ReleaseDoc on its associated ObjectComponents document to
55/// make sure that all the embedded objects are closed properly.
56//
57bool
59{
60 //
61 // if it's an open edit dll stop the closing process
63 while (curView) {
64 // get the ole view
66 if (oleView && oleView->IsOpenEditing() && !GetOcApp()->IsOptionSet(amExeMode)) {
67 TOleFrame* olefr = TYPESAFE_DOWNCAST(oleView->GetApplication()->GetMainWindow(), TOleFrame);
68 CHECK(olefr);
69 olefr->ShowWindow(SW_HIDE);
70 oleView->OleShutDown();
71 return false; // don't close
72 }
73 curView = curView->GetNextView();
74 }
75
76 // Just say yes if we are already in the closing process, or are embedded,
77 // or have multiple views open
78 //
79
80 if (Closing || IsEmbedded())
81 return true;
82
83 return TDocument::CanClose();
84}
85
86//
87/// Shuts down the TOleView's.
88//
89void
91{
93 while (curView) {
95 if (oleView)
96 oleView->OleShutDown();
97
98 curView = curView->GetNextView();
99 }
100}
101
102//
103/// Ensures that the IStorage is released properly and disconnects any active server
104/// in the document. A compound file must be closed before it is reopened.
105//
106bool
108{
109 // Make sure that TOleView's are closed first
110 //
111 OleViewClose();
112 OcDoc->Close();
114}
115
116//
117/// Releases the ObjectComponents document when the server is finished using the
118/// document.
119//
120bool
122{
123 PRECONDITION(OcDoc);
124
126 OcDoc->SetStorage((IStorage*)0);
127
128 return true;
129}
130
131//
132/// Attaches the IStorage pointer (stg) to this document. If successful, SetStorage
133/// returns true.
134//
135bool
137{
138 PRECONDITION(OcDoc);
139
140 // If a storage is provided, then we are now using container's IStorage
141 //
142 if (stg)
143 Embedded = true;
144
145 OcDoc->SetStorage(stg, remember);
147
148 return true;
149}
150
151//
152/// Restores the original root IStorage before the save operation.
153//
154bool
156{
157 PRECONDITION(OcDoc);
158
159 OcDoc->RestoreStorage();
161
162 return true;
163}
164
165//
166/// Before the document is actually opened, PreOpen gives the derived class a chance
167/// to perform a particular operation; for example, setting a different open mode
168/// for the compound document.
169//
170void
175
176//
177/// Overrides the TDocument::InitDoc function and creates or opens a compound file
178/// so that there is an IStorage associated with this document's embedded objects.
179/// Uses a TOcDocument object to perform the actual interaction with the OLE
180/// IStorage and IStream interfaces, which are ultimately responsible for
181/// establishing the relationship between a compound file and its storage.
182//
183bool
185{
186 if (IsOpen())
187 return true; // compound file already open
188
189 // Give user a chance to set a different open mode
190 //
191 PreOpen();
192
193 if (GetDocPath())
195 else
197
199 if (OcDoc) { // use the existing ocdoc
200 OcDoc->SetStorage(StorageI);
201 }
202 else if (GetOcApp()) {
203 OcDoc = new TOcDocument(*GetOcApp(), GetDocPath(), StorageI);
204 }
205
206 return true;
207 }
208
209 return false;
210}
211
212//
213/// Commits the current document's data to storage. If force is true and the data is
214/// not dirty, all data is written to storage and Commit returns true. If force is
215/// false, the data is written only if it is dirty.
216//
217bool
219{
220 if (Write())
222 else
223 return false;
224}
225
226//
227/// Loads the embedded objects, if any, using the path specified in path. mode is a
228/// combination of bits that specify how the embedded objects are opened (for
229/// example, read only, read/write, and so on). By default, objects are opened in
230/// ofReadWrite and ofTransacted modes.
231//
232bool
234{
235 if (path)
237
238 return Read();
239}
240//
241/// Checks to see if the current document's path is the same as the TOcDocument's
242/// path. If the paths are not the same, PathChanged returns true.
243//
245{
246 return _tcsicmp(OcDoc->GetName().c_str(), GetDocPath()) != 0;
247}
248//
249/// Saves the embedded objects to the compound file. A container should call this
250/// function to save its embedded objects to storage.
251//
252bool
254{
255 // Switch to new storage if path has changed & it is permanent ("SaveAs")
256 //
258 bool saveAs = PathChanged() && !OrgStorageI; // also is 'remember'
259 bool sameAsLoad = !PathChanged() && !OrgStorageI; // use current storage
260 if (saveAs) {
261 // Update link monikers
262 //
264 OcDoc->SetName(newName);
265
266 if (IsEmbedded())
267 newStorageI = StorageI; // Use the one assigned by container
268 else
270 }
271 else
272 newStorageI = StorageI;
273
274 return newStorageI ?
276 false;
277}
278
279//
280/// Loads the embedded objects from the compound file. A container should call this
281/// function to load any embedded objects.
282//
283bool
285{
286 // Load the embedded objects, if any
287 //
288 return OcDoc->LoadParts();
289}
290
291//
292/// Performs the reverse of Commit. Revert cancels any changes made to the document
293/// since the last time the document was saved to storage.
294//
295bool
297{
298 if (!StorageI)
299 return true; // return OK if storage already released
300
301 if (!TDocument::Revert(clear) || !ReleaseDoc())
302 return false;
303
304 if (!clear) {
305 InitDoc();
306 Open(0);
307 }
308
309 SetDirty(false);
310 return true;
311}
312
313//
314/// Returns the ObjectComponents application associated with this TOleDocument object.
315//
316TOcApp*
318{
319 TOleFrame* olefr = TYPESAFE_DOWNCAST(GetDocManager().GetApplication()->GetMainWindow(), TOleFrame);
320
321 return olefr->GetOcApp();
322}
323
324
325
326} // OCF namespace
327
328//==============================================================================
329
#define CHECK(condition)
Definition checks.h:239
#define PRECONDITION(condition)
Definition checks.h:227
OCF Application class.
Definition ocapp.h:144
bool SaveParts(IStorage *storage=0, bool sameAsLoaded=true, bool remember=true)
Save the embedded parts to the provided storage.
Definition ocdoc.cpp:155
void SetStorage(IStorage *storage, bool remember=true)
Set the storage for this document.
Definition ocdoc.cpp:92
bool RestoreStorage()
Restore the original root IStorage before the save operation.
Definition ocdoc.cpp:125
bool LoadParts()
Loads the parts from the current storage into the PartCollection.
Definition ocdoc.cpp:230
void SetName(const owl::tstring &newName)
Notify container that doc pathname has changed.
Definition ocdoc.cpp:349
owl::tstring GetName() const
Definition ocdoc.h:82
bool Revert(bool clear)
Performs the reverse of Commit.
Definition oledoc.cpp:296
TOleDocument(owl::TDocument *parent=0)
Constructs a TOleDocument object associated with the given parent TDocument object.
Definition oledoc.cpp:29
bool CanClose()
Prepares the document for closing.
Definition oledoc.cpp:58
bool SetStorage(IStorage *stg, bool remember=true)
Attaches the IStorage pointer (stg) to this document.
Definition oledoc.cpp:136
bool ReleaseDoc()
Releases the ObjectComponents document when the server is finished using the document.
Definition oledoc.cpp:121
TOcApp * GetOcApp()
Returns the ObjectComponents application associated with this TOleDocument object.
Definition oledoc.cpp:317
~TOleDocument()
Destroys the TOleDocument object.
Definition oledoc.cpp:41
bool Open(int mode, LPCTSTR path=0)
Loads the embedded objects, if any, using the path specified in path.
Definition oledoc.cpp:233
virtual bool Read()
Loads the embedded objects from the compound file.
Definition oledoc.cpp:284
virtual bool Write()
Saves the embedded objects to the compound file.
Definition oledoc.cpp:253
void OleViewClose()
Shuts down the TOleView's.
Definition oledoc.cpp:90
virtual void PreOpen()
Before the document is actually opened, PreOpen gives the derived class a chance to perform a particu...
Definition oledoc.cpp:171
bool PathChanged()
Checks to see if the current document's path is the same as the TOcDocument's path.
Definition oledoc.cpp:244
bool Close()
Ensures that the IStorage is released properly and disconnects any active server in the document.
Definition oledoc.cpp:107
bool RestoreStorage()
Restores the original root IStorage before the save operation.
Definition oledoc.cpp:155
bool Commit(bool force)
Commits the current document's data to storage.
Definition oledoc.cpp:218
bool InitDoc()
Overrides the TDocument::InitDoc function and creates or opens a compound file so that there is an IS...
Definition oledoc.cpp:184
Decorated frame that supports OLE 2 using OCF.
Definition oleframe.h:65
The OLE2 window view class. Used as a view in doc/view model.
Definition oleview.h:123
bool Commit(bool force=false)
Definition stgdoc.cpp:612
virtual bool SetStorage(IStorage *stg, bool remember=true)
Give an IStorage to document. This typically happens for OLE servers.
Definition stgdoc.cpp:331
bool Open(int omode, LPCTSTR stgId)
Open the compound file with a given path.
Definition stgdoc.cpp:219
virtual bool ReleaseDoc()
Release the IStorage and close the document.
Definition stgdoc.cpp:209
bool IsOpen()
Return 'true' if the storage document object has opened an OLE storage.
Definition stgdoc.h:122
virtual IStorage * GetNewStorage()
Get a new IStorage, typically in a SaveAs situation.
Definition stgdoc.cpp:402
virtual bool RestoreStorage()
Restore the original root IStorage before the save operation.
Definition stgdoc.cpp:384
bool SetDocPath(LPCTSTR path)
Definition stgdoc.cpp:602
An abstract base class, TDocument is the base class for all document objects and serves as an interfa...
Definition docview.h:187
LPCTSTR GetDocPath() const
Returns the directory path for the document.
Definition docview.h:874
virtual bool CanClose()
Returns false if unable to close.
Definition document.cpp:775
TDocManager & GetDocManager()
Returns a pointer to the current document manager.
Definition docview.h:850
void SetOpenMode(int mode)
Sets the mode and protection flag values for the current document.
Definition docview.h:996
void SetDirty(bool dirty=true)
Updates the document's dirty flag using the specified parameter.
Definition docview.h:888
TView * GetViewList() const
Return pointer to the head of the link list of views associated with this document.
Definition docview.h:947
bool IsEmbedded() const
Returns true if the document is embedded in an OLE 2 container.
Definition docview.h:911
int GetOpenMode() const
Gets the mode and protection flag values for the current document.
Definition docview.h:987
virtual bool Revert(bool clear=false)
abort changes, no reload if true
Definition document.cpp:604
Abstract base class for view access from document.
Definition docview.h:397
#define _tcsicmp
Definition cygwin.h:76
Definition of class TDocManager.
@ ofReadWrite
Definition docview.h:65
@ ofTransacted
STGM_TRANSACTED, supports commit and revert.
Definition docview.h:84
@ ofNoCreate
ios::nocreate, open fails if file doesn't exist
Definition docview.h:74
@ ofTemporary
STGM_DELETEONRELEASE, delete when destructed.
Definition docview.h:87
Include for OC, gets common headers when precompiled headers are enabled.
Object Component Framework (COM encapsulation)
Definition appdesc.h:22
class _ICLASS TOcDocument
Definition ocpart.h:30
@ amExeMode
may be overridden per instance if running DLL
Definition ocreg.h:88
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
OWL_DIAGINFO
Definition animctrl.cpp:14
std::string tstring
Definition defs.h:79
Definition of TOcApp application connection class.
Definition of TOcDocument Class.
interface _ICLASS IStorage
Definition ocdoc.h:25
#define TYPESAFE_DOWNCAST(object, toClass)
Definition defs.h:269