Objective Chart : PART II Programmer’s Guide : Chapter 20 Communication Document System : SECDocManager: An MFC Application Document Manager
SECDocManager: An MFC Application Document Manager
To allow the CApplication object to use the SECComDoc document type, you must modify the document manager. Normally, given a choice of several different document template types, the document manager simply asks the user which one is required in response to the OnFileNew() command. The ComDoc system adds the ability to decide in advance which document type should be invoked. The mechanism that displays the standard document choice dialog is extended for this purpose.
A string contained in the document resources identifies each separate document type registered in an application. This string contains the name of the document type, the file extension used, the document title text, and other information. By using SECDocManager and an overloaded version of OnFileNew(), it’s possible to call up a file by using the document name stored in the template string. The prototype of this function is shown below.
 
virtual CDocument *OnFileNew(LPCSTR DocIdent,
UINT nOpCode,UINT nSubCode=0, DWORD dwData=NULL,
SECComDoc *parent=NULL);
The first parameter, DocIdent, should be the document’s template string name. The other parameters are the op-code, sub-code, and data for the new document’s initialization procedure and a pointer to the document that is to act as parent.
The following string is the document template string from the Objective Chart document used in the MultiGrph example:
 
\nGraph\nGraph\nGraph documents\n.gph\nGraph.Document\nGraph Document
Using this template, a graph document may be invoked using the “Graph” name as the DocIdent.
Using SECDocManager
You must make modifications to your CWinApp-derived class to include ComDoc functionality in your application.
1. Add the following code to the class definition:
 
class CMyApp : public CWinApp
{
// normal class items
//Starts new files without the document type dialog
// and allows passing of parameters to the document.
virtual CDocument* OnFileNew(LPCSTR DocIdent,
UINT nOpCode, UINT nSubCode, DWORD dwData,
SECComDoc *pParent);
 
//To conform with default behaviors
virtual void OnFileNew();
 
//To use our own SECDocTemplate class
//to manage documents
virtual void AddDocTemplate(CDocTemplate* pTemplate);
// normal class items
}
2. Add the following code to the application class implementation:
 
void CMyApp::OnFileNew()
{
if (m_pDocManager != NULL)
((SECDocManager *)m_pDocManager)->OnFileNew();
}
 
// Overridden AddDocTemplate which uses the special
// SECDocManager derivative class
void CMyApp::AddDocTemplate(CDocTemplate* pTemplate)
{
if (m_pDocManager == NULL)
m_pDocManager = new SECDocManager;
m_pDocManager->AddDocTemplate(pTemplate);
}
 
CDocument *CMyApp::OnFileNew(LPCSTR DocIdent,
UINT nOpCode, UINT nSubCode, DWORD dwData,
SECComDoc* pParent)
{
if (m_pDocManager != NULL)
return ((SECDocManager*)m_pDocManager)-> OnFileNew(DocIdent,nOpCode,nSubCode,dwData,pParent);
// NOTE... The pointer to the DocManager must point to a
// SECDocManager object or
// catastrophic failure will result.
return NULL;
}
These modifications allow the invocation of a document by its document template string name. Documents may also be initialized by sending data directly to them rather than going through the process of opening a disk file. It is this process that allows MultiGrph’s Objective Grid document to create and initialize a separate document using Objective Chart.
See the MultiGrph sample source code for the complete implementation of a ComDoc application. MultiGrph uses the OC document and view classes CGraphDoc and SRGraphView directly, so you will only see grid-related classes in Visual Studio’s Class View. For more information, see CMultigrphApp::InitInstance().