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






3.73/5 (5 votes)
May 21, 2004
3 min read

75546

4490
An article on a "Single View, Multiple Documents" MFC Architecture with a new VC++ code wizard
- Download demo project - 31.4 Kb
- Download demo project with splitter window - 32.2 Kb
- Download document giving methods to convert manually your SDI project into a MSDI project - 4.36 Kb
- Download VC++ .NET wizard setup - 1616 Kb
Introduction
This article shows a document/view architecture different from SDI and MDI. It uses one view to display multiple documents.
Background: SDI and MDI architectures
Typically, MFC AppWizard suggests two kinds of Document-View Architectures.
- SDI, Single Document Interface: only one document is displayed in a single view, which is contained in the main frame window (
CFrameWnd
). When you select the "File, New" command, the document data are destroyed (seeCDocument::DeleteContents
) and replaced by a new content. There is no "File, Close" command. The document, the view and the frame are destroyed together when the user exits the application. All this is managed through aCSingleDocTemplate
template object. - MDI, Multiple Document Interface: there can be zero, one or several documents. Each document is displayed in a separate view, which is contained in a child frame window (
CMDIChildWnd
). All child frame windows are contained in the main frame window (CMDIFrameWnd
). When you select the "File, New" command, a new document, a new view and a new child frame are created. When you select the "File, Close" command, the active document, the active view and the active child frame are destroyed. All this is managed through aCMultiDocTemplate
template object.
SDI and MDI comply with this assumption: for each document there is a view and a frame (CFrameWnd
in SDI, CMDIChildWnd
in MDI). All three objects are created and destroyed together. All documents are displayed (of course some windows can be hidden or minimized).
Looking at the MFC source code, I noticed that:
- when a document is created, most of the work is achieved by the
OpenDocumentFile
method of template object; - when a document is destroyed, most of the work is achieved by method
CDocument::OnCloseDocument
.
Single View, multiple documents: SVDI Architecture
The aim of this article is to show another kind of architecture called SVDI, Single View Document Interface. It is a "Single View, Multiple Documents" Architecture. There can be zero, one or several documents, zero or one view, and one main frame window. The view is created when the first document is created, and destroyed when the last document is destroyed. Only one document can be displayed at a time.
When you select the "File, New" command, a new document is created; if there is no view, a view is created to display the document. When you select the "File, Close" command, the active document is destroyed. If there are some other documents, one of them is displayed in the view; otherwise the view is destroyed. This does not comply with the preceding assumption.
For the sake of simplicity, there are only a few minor differences between the SDI and SVDI architectures. I just added a new template class called CSingleViewMultiDocTemplate
, which does nearly all work concerning the opening of a document (see CSingleViewMultiDocTemplate::OpenDocumentFile
). A few methods are overloaded. The "File, Close" command and a list of documents in the View menu have been added.
Using the code
You can either:
- Make your own application, taking your inspiration from my demo projects;
- If you have Visual C++ .NET, use the wizard I have created for you, which generates automatically a SDI, SVDI or MDI application;
- If you don't have Visual C++ .NET, create your SDI application as usual, and convert it to a SVDI application following the method given above.
Compatibility
This project has been successfully tested with Microsoft Visual C++ .NET 2002 Professional (French version) and Microsoft Visual C++ .NET 2003 Professional (English version). The wizard should work with all English/French versions of Microsoft Visual C++ 2002/2003 (sorry for people from other countries, some files are specific to each localized version).
Note for Microsoft Visual C++ 6 users: unfortunately I do not have Visual C++ 6, so I did no test the demos. You will have to re-create an empty project and add all the files.