mfc >> Multiple views Single Document MDI

by ggurubasavaraja » Mon, 20 Aug 2007 12:56:57 GMT

Hi Folks,

I have a situation where I need 4 views in my application. All the 4
views should co exist and shall be updated independently/
simultaneously.

I searched the forum and all I got was how to create multiple views
for a SDI and for MDI creating multiple document templates and make
them refer to a single document .

I dont know whether creating multiple document templates is a good
solution as I read somewhere it is not an optimal solution.

Is there any simple/optimal method by which I can go on adding a view
for the document? If possible please paste a link to the sample
application.

Thanks in Advance.

Regrads,
JustLikeDat


mfc >> Multiple views Single Document MDI

by David Wilkinson » Mon, 20 Aug 2007 16:21:45 GMT



JLD:

Why are you posting the same question again? You already have a detailed
response in your previous thread.

--
David Wilkinson
Visual C++ MVP

mfc >> Multiple views Single Document MDI

by Joseph M. Newcomer » Mon, 20 Aug 2007 16:25:13 GMT

You posted this same question a few days ago, and I already answered it.
joe



Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

mfc >> Multiple views Single Document MDI

by Daniel James » Mon, 20 Aug 2007 16:43:41 GMT


Document templates in MFC defines the relationships between document types
and their associated view types. These relationships are needed so that the
framework can manage things like automatically updating all of the views
when the document's data are changed.

Defining multiple document templates is really the ONLY way to achieve what
you want if you are going to use MFC's Doc/View logic at all.

The Doc/View architecture in MFC is, perhaps, not itself "optimal" (it's a
slightly flawed implementation of the model/view/controller design from
SmallTalk) but it is what MFC provides and it does work well. Just use it!

Cheers,
Daniel.

mfc >> Multiple views Single Document MDI

by ggurubasavaraja » Mon, 20 Aug 2007 19:30:19 GMT

What is worrying me is I am unable to access my previous post.I
searched the post and got no results .
Even I tried to reach it though my profile( my all posts) but was
unsuccessful.

mfc >> Multiple views Single Document MDI

by Joseph M. Newcomer » Mon, 20 Aug 2007 21:32:42 GMT

have a "sanitized" version of some code I did a few years ago that I put together for
this. Send me a private email and I'll send you a copy. The quick summary is below.

I did this from the mainfrm.cpp file. The code goes like this:

Add a protected variable to the mainframe class:

CMultiDocTemplate * GreenTemplate;

In the CMainFrame::CMainFrame constructor, clear it out
GreenTemplate = NULL;
Add a new protected class member function
CDocument * GetCurrentDocument();
For symmetry, I added an additional variable
CMultiDocTemplate * RedTemplate;
and set it to NULL as well.

Create a new CView-derived class, in my case CGreenView (the names have been sanitized to
protected certain secure information).

The default view is a Red view, as exemplified by the standard InitInstance code:

pDocTemplate = new CMultiDocTemplate(IDR_RedViewTYPE,
RUNTIME_CLASS(CMultiViewDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CRedView));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);


I added the following:

a new menu

IDR_GreenViewTYPE=xxx

where xxx is a resource number you have not already used. In my case it was 130. It has
to have an ICON of that ID, and a STRINGTABLE resource of that ID, so the ID numeric value
cannot be used already in either a string, icon, or menu context.

I added to the Window menu of the exisiting IDR_RedViewTYPE the item
&Green View = ID_WINDOW_GREENVIEW
and to the new menu for the IDR_GreenViewTYPE the item
&Red View = ID_WINDOW_REDVIEW


I Created an icon of the ID IDR_GreenTYPE.

In mainfrm.cpp, I added the handler to create a Green View:

CDocument * CMainFrame::GetCurrentDocument()
{
CMDIFrameWnd * frame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
if(frame == NULL)
return NULL;
CMDIChildWnd * child = (CMDIChildWnd *)frame->GetActiveFrame();
if(child == NULL)
return NULL;
CView * view = child->GetActiveView();
if(view == NULL)
return NULL;
CDocument * doc = view->GetDocument();
return doc;
} // CMainFrame::GetCurrentDocument

void CMainFrame::OnWindowGreenview()
{
if(GreenTemplate == NULL)
GreenTemplate = new CMultiDocTemplate(IDR_GreenViewTYPE,
RUNTIME_CLASS(CMultiViewDoc),
RUNTIME_CLASS(CChildFrame),
RUNTIME_CLASS(CGreenView));

CMultiViewDoc * doc = (CMultiViewDoc *)GetCurrentDocument();
CFrameWnd * frame = GreenTemplate->CreateNewFrame(doc, NULL);
if(frame != NULL)
{ /* frame created */
frame->InitialUpdateFrame(doc, TRUE);
} /* frame created */
}

void CMainFrame::OnUpdateWindowGreenview(CCmdUI *pCmdUI)
{
CMultiViewDoc * doc = (CMultiViewDoc *)GetCurrentDocument();
pCmdUI->Enable(doc != NULL);
}

And one for a Red view:

void CMainFrame::OnWindowRedview()
{
if(RedTemplate == NULL)
RedTemplate = new CMultiDocTemplate(IDR_RedViewTYPE,
RUNTIME_CLASS(CMultiViewDoc),
RUNTIME_CLASS(CChildFrame),

mfc >> Multiple views Single Document MDI

by ggurubasavaraja » Tue, 21 Aug 2007 21:14:18 GMT

Thanks a lot Joseph for the reply.

If I create multiple document templates how to link them to a single
document?
Please throw some light on this..

Regards,
JustLikeDat

mfc >> Multiple views Single Document MDI

by Joseph M. Newcomer » Tue, 21 Aug 2007 21:25:27 GMT

You don't link them to a "single document". Note also that I create these templates
without adding them to the CWinApp database via AddDocTemplate. A document template is
just a document template, nothing more, and it is not linked to any document whatsoever. I
use the document template to create the frame and view, and I explicitly specify what
document to link it to at the time I create the new view.
joe



Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Similar Threads

1. Multiple views Single Document MDI - no splitter views

Hi Folks,

I have a situation where I need 4 views in my application. All the 4
views should co exist and shall be updated independently/
simultaneously.

I searched the forum and all I got was how to create multiple views
for a SDI and for MDI creating multiple document templates and make
them refer to a single document .

I dont know whether creating multiple document templates is a good
solution as I read somewhere it is not an optimal solution.

Is there any simple/optimal method by which I can go on adding a view
for the document? If possible please paste a link to the sample
application.

Thanks in Advance.

Regrads,
JustLikeDat

2. looking for sample:multiple view single document in C# or VB - .Net Framework

3. multiple view single document

Could some out point out to me how i can make a multiple view single doc
implementation
suppose i have 2 view and want to use the same CDocument

could anyone give me pointers

fordge



4. Creating Multiple Splitter Views in a Single Document MFC project

5. Multiple view for one document in MDI application

Hi all,

I hava a MDI application with document/view architecture. Now I want to 
change my application so that each document can have multiple difference 
views. Please tell me how to do it?

Thanks and best regard. 


6. Multiple Views: how to open both views at startup or on new document

7. Single Document MDI-like interface questions

8. Merging two single document view applications possibly using a DLL