Click here to Skip to main content
15,896,409 members
Articles / Desktop Programming / MFC

SVDI: a "Single View, Multiple Documents" MFC Architecture

Rate me:
Please Sign up or sign in to vote.
3.73/5 (5 votes)
20 May 20043 min read 74.9K   4.5K   27  
An article on a "Single View, Multiple Documents" MFC Architecture with a new VC++ code wizard
// SplitSVDIDoc.cpp : implementation of the CSplitSVDIDoc class
//

#include "stdafx.h"
#include "SplitSVDI.h"

#include "SplitSVDIDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CSplitSVDIDoc

IMPLEMENT_DYNCREATE(CSplitSVDIDoc, CDocument)

BEGIN_MESSAGE_MAP(CSplitSVDIDoc, CDocument)
END_MESSAGE_MAP()


// CSplitSVDIDoc construction/destruction

CSplitSVDIDoc::CSplitSVDIDoc()
{
	// TODO: add one-time construction code here

}

CSplitSVDIDoc::~CSplitSVDIDoc()
{
}

BOOL CSplitSVDIDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}




// CSplitSVDIDoc serialization

void CSplitSVDIDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}


// CSplitSVDIDoc diagnostics

#ifdef _DEBUG
void CSplitSVDIDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CSplitSVDIDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG



void CSplitSVDIDoc::OnChangedViewList()
{
	//CDocument::OnChangedViewList();
	// The default implementation calls OnCloseDocument if there is no associated views;
	// we want to prevent this
	// See CDocument::OnChangedViewList, doccore.cpp

	// update the frame counts as needed
	UpdateFrameCounts();
}

void CSplitSVDIDoc::OnCloseDocument()
{
	//CDocument::OnCloseDocument();
	// The default implementation destroys all frames viewing this document;
	// we want to do something different: if there are some other documents,
	// one of them is displayed in the view; otherwise the view is destroyed.
	// See CDocument::OnCloseDocument, doccore.cpp

	// Common new code
	if (!m_viewList.IsEmpty()) {
		// There is one or several views (normally one only)

		// Seraching for another open document
		CDocument* pOtherDoc = NULL;
		POSITION pos = m_pDocTemplate->GetFirstDocPosition();
		while (pos != NULL) {
			pOtherDoc = m_pDocTemplate->GetNextDoc(pos);
			if (pOtherDoc == NULL)
				break;	// No more open doc
			if (pOtherDoc == this)
				pOtherDoc = NULL;
			else
				break;	// Open document found
		}

		CView* pView = NULL;
		if (pOtherDoc != NULL) {
			// Connect all views to this document
			pos = m_viewList.GetHeadPosition();
			while (pos != NULL) {
				pView = (CView*)m_viewList.GetNext(pos);
				this->RemoveView(pView);
				pOtherDoc->AddView(pView);
				pView->SendMessage(_afxMsgUpdateView);
			}
		}
		else {
			// Destroy all views
			pos = m_viewList.GetHeadPosition();
			while (pos != NULL)	{
				pView = (CView*)m_viewList.GetNext(pos);

				// Special case of splitter window
				CSplitterWnd* pSplitter = CView::GetParentSplitter(pView, TRUE);
				if (pSplitter != NULL) {
					// Destroy splitter window; we assume here that splitter window contains all views
					pSplitter->DestroyWindow();
					break;
				}

				// Destroy view
				pView->DestroyWindow();
			}

			// The view object will be destroyed in CView::PostNcDestroy()

			CFrameWnd* pFrame = (CFrameWnd*)AfxGetApp()->m_pMainWnd;
			if (pFrame != NULL)	{
				ASSERT_KINDOF(CFrameWnd, pFrame);

				// Update frame title
				pFrame->OnUpdateFrameTitle(TRUE);

				// Redraw entire frame window
				pFrame->RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE | RDW_ALLCHILDREN);
			}
		}
	} // if (!m_viewList.IsEmpty()

	// clean up contents of document before destroying the document itself
	DeleteContents();


	// Common code
	// delete the document if necessary
	if (m_bAutoDelete)
		delete this;
}


// CSplitSVDIDoc commands

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions