Replacing a view in a doc-view application

Sometimes the way a document is being visualized needs to be significantly changed. For example, in PowerPoint, you can see the slides in a WYSIWYG form or view only the text in a text edit window. In the MFC world, one can find an amazingly large quantity of programs that implement this behavior defining one CView descendant and making it responsible for all visualization changes. This path has several disadvantages:


  • Big, difficult to manage class definition file.
  • Diminished reusability: you could reuse one big CView descendant or nothing.
  • Hard to maintain (what if you want to modify or add a new “look” to the document?).
  • Wasted memory: some variables (objects) will exist in memory and wont be used.


For an application using the MFC document-view architecture, it is more appropriate to define different view classes and switch between them when necessary. This shall overcome all the disadvantages listed before. There probably will be some features common to all views for the same type of document, so it is a good idea to have a direct CView descendant that implement all the functionality common to all view types. The views used by the document should be descendants of this class (“grandchildren” of CView).


The code needed to implement view switching depends on the frame window containing the view. There are three common cases: the view is contained within a CFrameWnd (SDI application), the view is contained within a CMDIChildWnd (MDI application) and the view is a pane of a splitter window, either in SDI or MDI applications. In all cases what we need is a method in our document class to switch to the desired view. This method should receive the new view type as a parameter and return a success flag. The advantage of having this method in the document class becomes obvious when there are several document types each of which can have different view types. Lets start with an SDI application that doesnt have splitters:


BOOL CMyDocument::SwitchToView(CRuntimeClass* pNewViewClass)
{
CFrameWnd* pMainWnd = (CFrameWnd*)AfxGetMainWnd();
CView* pOldActiveView = pMainWnd->GetActiveView();

// If we’re already displaying this kind of view, no need to go further.
if (pOldActiveView->IsKindOf(pNewViewClass))
return TRUE;

// Set the child window ID of the active view to AFX_IDW_PANE_FIRST.
// This is necessary so that CFrameWnd::RecalcLayout will allocate
// this “first pane” to that portion of the frame window’s client
// area not allocated to control bars. Set the child ID of
// the previously active view to some other ID.
::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, 0);

// create the new view
CCreateContext context;
context.m_pNewViewClass = pNewViewClass;
context.m_pCurrentDoc = this;
CView* pNewView = STATIC_DOWNCAST(CView, pMainWnd->CreateView(&context));
if (pNewView != NULL)
{
// the new view is there, but invisible and not active…
pNewView->ShowWindow(SW_SHOW);
pNewView->OnInitialUpdate();
pMainWnd->SetActiveView(pNewView);
pMainWnd->RecalcLayout();

// destroy the old view…
pOldActiveView->DestroyWindow();
return TRUE;
}

return FALSE;
}



In the case of an MDI application (again without splitters):


BOOL CMyDocument::SwitchToView(CRuntimeClass* pNewViewClass)
{
CMDIFrameWnd* pMainWnd = (CMDIFrameWnd*)AfxGetMainWnd();
// Get the active MDI child window.
CMDIChildWnd* pChild = (CMDIChildWnd*)pMainWnd->MDIGetActive();
// Get the active view attached to the active MDI child window.
CView* pOldActiveView = pChild->GetActiveView();
// If we’re already displaying this kind of view, no need to go further.
if (pOldActiveView->IsKindOf(pNewViewClass))
return TRUE;

// Set flag so that document will not be deleted when view is destroyed.
BOOL bAutoDelete = m_bAutoDelete;
m_bAutoDelete = FALSE;
// Delete existing view
pOldActiveView->DestroyWindow();
// restore flag
m_bAutoDelete = bAutoDelete;

// Create new view.
CView* pNewView = (CView *)pNewViewClass->CreateObject();
if (pNewView == NULL)
{
TRACE1(“Warning: Dynamic create of view type %Fs failedn”, pNewViewClass->m_lpszClassName);
return FALSE;
}

// Draw new view.
CCreateContext context;
context.m_pNewViewClass = pNewViewClass;
context.m_pCurrentDoc = this;
context.m_pNewDocTemplate = NULL;
context.m_pLastView = NULL;
context.m_pCurrentFrame = pChild;
if (!pNewView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0),
pChild, AFX_IDW_PANE_FIRST, &context))
{
TRACE0(“Warning: couldn’t create view for framen”);
delete pNewView;
return FALSE;
}

pNewView->SendMessage(WM_INITIALUPDATE, 0, 0); // WM_INITIALUPDATE is defined in afxpriv.h
pChild->RecalcLayout();
pNewView->UpdateWindow();
pChild->SetActiveView(pNewView);
return TRUE;
}



When the view to replace is a pane of a splitter window, there is also a small difference between SDI and MDI applications, related to the retrieval of the current active view. In the method below you must comment out what you dont need depending on your application type:


BOOL CMyDocument::SwitchToView(CRuntimeClass* pNewViewClass)
{
/* Uncomment this if this is a SDI application

CFrameWnd* pMainWnd = (CFrameWnd*)AfxGetMainWnd();
CView* pOldActiveView = pMainWnd->GetActiveView();
*/
/* Uncomment this if this a MDI application

CMDIFrameWnd* pMainWnd = (CMDIFrameWnd*)AfxGetMainWnd();
// Get the active MDI child window.
CMDIChildWnd* pChild = (CMDIChildWnd*)pMainWnd->MDIGetActive();
// Get the active view attached to the active MDI child window.
CView* pOldActiveView = pChild->GetActiveView();
*/
// If we’re already displaying this kind of view, no need to go further.
if (pOldActiveView->IsKindOf(pNewViewClass))
return TRUE;

CSplitterWnd* pSplitter = (CSplitterWnd *)pOldActiveView->GetParent();
int row, col;
ASSERT(pSplitter->IsChildPane(pOldActiveView, row, col));
CRect viewrect;
pOldActiveView->GetWindowRect(&viewrect);

// set flag so that document will not be deleted when view is destroyed
m_bAutoDelete = FALSE;
// Delete existing view
pOldActiveView->DestroyWindow();
// set flag back to default
m_bAutoDelete = TRUE;

// Create new view
CCreateContext context;
context.m_pNewViewClass = pNewViewClass;
context.m_pCurrentDoc = this;
context.m_pNewDocTemplate = NULL;
context.m_pLastView = NULL;
context.m_pCurrentFrame = NULL;
if (!pSplitter->CreateView(row, col, pNewViewClass, viewrect.Size(), &context))
return FALSE;

// Set active
CView* pNewView = (CView *)pSplitter->GetPane(row, col);
pSplitter->GetParentFrame()->SetActiveView(pNewView);

pSplitter->RecalcLayout();
pNewView->SendMessage(WM_PAINT);
return TRUE;
}



Now that we have a method in our document class that will replace the current view, lets use it. The new view type should be decided (in response to a menu selection, for instance), and the function must be called as follows:


CRuntimeClass* pNewViewClass = RUNTIME_CLASS(CMyView);
if (!SwitchToView(pNewViewClass))
// failed
else
// succeeded


One final word to the class wizard fans. When you have a descendant of a CView descendant, the class wizard wont allow you to edit this class. To change this behavior, change all class wizard comments replacing the name of your direct CView descendant with CView. Class wizard will now work.

Last updated: January 15, 1999

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read