MultiDocument And MultView (and per document) :: MFC

kuphryn

Senior member
Jan 7, 2001
400
0
0
Hi.

I created the core program in doc/view architecture. I understand that the idea behind doc/view is one document for each view. A document could have more than one view, but a view can only display one document, period.

Let consider I have a program that deals with text and document format. For example, I can open edit text for (*.txt) and document file (*.doc). If I am correct, I would need two document classes (derived) and at least two view. Right? If yes, what is the how do I add a new document class into the program *and link* it with a *new/different* view class? I have been using the AppWizard to generate a doc/view. I have never added a new document and a view with it.

Thanks,
Kuphryn
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
To make everything clear, I am using doc/view *single* document. I am using single document because at anytime in the program, one one document is active. To my understanding, multiple document means the user can work with multiple documents at one time similar to how the user can open, view, and edit more than one images in Photoshop.

Anyways, here is the default core document template straight from a freshly generated MFC application:

-----
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMultiDocDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMultiDocView));
AddDocTemplate(pDocTemplate);
-----

I added a *two* new classes. One was derived from CDocument. The second was derived from CEditView The name of the class was "CMultiDocAct1."

New class: CMultiDocAct1Doc (derived from CDocument)
New class: CMultiDocAct1View (derived from CEditView)

Is the following correct?

-----
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMultiDocDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMultiDocView));
AddDocTemplate(pDocTemplate);

// I added the following

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMultiDocAct1Doc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMultiDocAct1View));
AddDocTemplate(pDocTemplate);

-----

All I did was added the names of the two new classes.

Lastly, I need to access the *new document* class from a view class, most likely the new view class (CMultiDocAct1View). I noticed the following code was in the original AppWizard generated view class:

-----
#ifndef _DEBUG // debug version in MultiDocView.cpp
inline CMultiDocDoc* CMultiDocView::GetDocument()
{ return (CMultiDocDoc*)m_pDocument; }
#endif
-----

Do I need to add something similar to the new view class?

Thanks,
Kuphryn