MultiDocument And MultView (and per document) :: MFC

Status
Not open for further replies.
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
 

Tychom

Ars Tribunus Militum
2,391
Subscriptor
in your main project file you shall see code like this:

Code:
	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_KUPHTYPE,
		RUNTIME_CLASS(CKuphDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CKuphView));
	AddDocTemplate(pDocTemplate);

where IDR_KUPHTYPE is an UINT id referring to certain resources, a quick search reveals these:

Code:
D:\code\ars\kuph\kuph.rc(70):IDR_KUPHTYPE         ICON    DISCARDABLE     "res\\kuphDoc.ico"
D:\code\ars\kuph\kuph.rc(136):IDR_KUPHTYPE MENU PRELOAD DISCARDABLE 
D:\code\ars\kuph\kuph.rc(293):	IDR_KUPHTYPE         "\nKuph\nKuph\n\n\nKuph.Document\nKuph Document"

ie. they define the toolbar id, the document strings and the icon to use for the view.

Most interesting is the last line, it's seperated by \n's, each token has a different use: window title, docName, fileNewName, filterName, filterExt, regFileTypeId, regFileTypeName. You'll want to change the 5th to .txt and .doc in the respective id's (IDR_KUPHTYPE "\nKuph\nKuph\n\n.txt\nKuph.Document\nKuph Document"

To create new single views on multiple doc types you can simple do this:



Code:
	CMultiDocTemplate* pDocTemplate;

//original code
	pDocTemplate = new CMultiDocTemplate(
		IDR_KUPHTYPE,
		RUNTIME_CLASS(CKuphDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CKuphView));
	AddDocTemplate(pDocTemplate);

//new code
	pDocTemplate = new CMultiDocTemplate(
		IDR_DOCFILES_KUPHTYPE,
		RUNTIME_CLASS(CKuphDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CKuphView));
	AddDocTemplate(pDocTemplate);

note the second template added, it uses a new resource id called IDR_DOCFILES_KUPHTYPE, which will basically match the one described earlier.

Multiple views on multiple documents would be more akin to this:

Code:
	CMultiDocTemplate* pDocTemplate;

//original code
	pDocTemplate = new CMultiDocTemplate(
		IDR_KUPHTYPE,
		RUNTIME_CLASS(CKuphDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CKuphView));
	AddDocTemplate(pDocTemplate);

//new code
	pDocTemplate = new CMultiDocTemplate(
		IDR_DOCFILES_KUPHTYPE,
		RUNTIME_CLASS(CKuphDocFileDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CKuphDocFileView));
	AddDocTemplate(pDocTemplate);

here we have the new resource, and a new view and document (create those through class wizard)
 
Thanks. I want to be sure I have understand it.

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
 
The line gets blurry as I consider when to use SDI and MDI. For example, in the program I am working on there are numerous "tools" that manipulate a text file. I am not certain if I should create a new document for each tool since each tool does a very specific job that manipulate the text file. That is basically what is causing my indecision.

Can you give me a very clear example of cases when you would use SDI and/or MDI?

Kuphryn
 
Status
Not open for further replies.