Download presentation
Presentation is loading. Please wait.
Published byBrook Watts Modified over 6 years ago
1
MULTIPLE DOCUMENTS AND MULTIPLE VIEWS Prosise - Chapter 11
Jim Fawcett CSE778 – Advanced Windows Programming Summer 2003 Derived from a presentation by Kemal OZDEMIR & Veysel DEMIR
2
INTRODUCTION MFC AND THE MULTIPLE DOCUMENT INTERFACE SPLITTER WINDOWS
Synchronizing Multiple Views of a Document The MdiSquares Application Supporting Multiple Document Types Alternatives to MDI SPLITTER WINDOWS Dynamic Splitter Windows The Sketch Application Static Splitter Windows The Wanderer Application Custom Command Routing Three-Way Splitter Windows Dynamic Splitter Windows with Multiple View Types
3
MFC AND THE MULTIPLE DOCUMENT INTERFACE
Using splitter windows provided by MFC, an SDI application can present one or more views of the same document in resizable “panes” that subdivide frame window’s client area. The document/view architecture also extends to MDI applications that support multiple views of a document , multiple open documents, and even multiple document types. It is a simple matter to extend SDI document/view application paradigm to encompass multiple documents and multiple views.
4
USER POINT OF VIEW - MDI & SDI
MDI applications permit the user to have two or more documents open for editing at once. SDI applications require the user to close the currently open document before opening an other.
5
USER POINT OF VIEW - MDI & SDI
MDI applications support multiple document types. For example, an all-in-one word processing, spreadsheet and charting program might be implemented as an MDI application that supports three document types. SDI applications support only one document type.
6
USER POINT OF VIEW - MDI & SDI
MDI applications feature a Window menu with a New Window command for opening secondary views of a document and commands for arranging the windows in which the views appear. The window menu also contains a list of open views. SDI does not support window menu.
7
USER POINT OF VIEW - MDI & SDI
SDI applications generally feature just one menu. MDI applications have at least two menus. One that is displayed when no documents are open. The other are displayed when at least one document is open. Generally one menu per document type.
8
USER POINT OF VIEW - MDI & SDI
SDI applications use just one frame window – the top-level frame window. MDI applications use two: A top-level frame window Child-frames or document frames that float within the top level frame window.
10
MFC SUPPORT FOR MDI Without MFC support MDI applications require more effort to create than SDI applications. For example, it is developers responsibility to update the menu as documents are opened, closed and switched between, implement the window menu To create and manage the document frames that float within the top-level frame window. ……….. These features of the MDI user interface model translate into dozens of annoying little implementation details.
11
MFC SUPPORT FOR MDI MFC document/view architecture abstracts the user interface model so that writing MDI is slightly different than SDI. Like SDI, MDI applications store data in document objects based on CDocument and present views of that data in view objects based on CView or one of its derivatives.
12
MFC POINT OF VIEW - MDI & SDI
MDI applications : derive their top-level frame window classes from CMDIFrameWnd rather than CFrameWnd. use classes based on CMDIChildWnd to represent the child frame windows that frame views of their documents.
13
MFC POINT OF VIEW - MDI & SDI
MDI applications : use CMultiDocTemplate rather than CSingleDocTemplate to create the document templates. Child frame window class is referenced in CMultiDocTemplate’s constructor rather than top-level frame window class. have at least two menu resources : One is displayed when no documents are open Other is displayed when at least one document is open.
14
MFC POINT OF VIEW - MDI & SDI
MFC manages almost every aspect of an application’s user interface. Details that are not automatically handled by MFC are handled by AppWizard’s step-1 dialogue box. If Multiple Document is chosen AppWizard emits an MDI application skeleton. You just write a document/view application.
22
SYNCHRONIZING MULTIPLE VIEWS OF A DOCUMENT
When the New Window command is selected from the window menu, an MFC provided command handler pulls up the document template, extracts CRuntimeClass pointers identifying the view class and the frame window class, and instantiates a new view and a new frame window (a child frame). The secondary view’s address is added to the list of views maintained by the document object. If either view is asked to repaint, it calls GetDocument to acquire a pointer to the document object, queries the document for the data it needs, and repaints. Because both views are connected to the same document object, each view access to the same set of data.
23
SYNCHRONIZING MULTIPLE VIEWS OF A DOCUMENT
If the user edits the document in one of the views the other views should be updated to reflect the change. Since the update does not happen automatically, it is programmer’s responsibility to make sure that when the view is edited in one view, the other views are updated too. CDocument::UpdateAllViews and Cview::OnUpdate functions are provided by the frame work for this purpose.
24
UpdateAllViews FUNCTION
UpdateAllViews : When a document’s data is modified in a multiple-view application, this function should be called by either the view object or the document object. UpdateAllViews iterates through the list of views associated with the document, calling each views virtual OnUpdate function.
25
OnUpdate FUNCTION Cview provides a trivial implementation of OnUpdate that invalidates the view and forces a call to OnDraw. A full repaint is default for OnUpdate. To make changes as efficient as possible, you can override OnUpdate in the view class and make use of hint information passed to UpdateAllWindows.
26
PROTOTYPES void UpdateAllViews (Cview* pSender,LPARAM lHint = 0L, Cobject* pHint = NULL) virtual void OnUpdate(Cview* pSender,LPARAM lHint, Cobject* pHint) lHint and pHint carry hint information from UpdateAllViews to OnUpdate. How to use these parameters is highly application specific. For example: lHint may be used to send address of a Crect object so that it can be used for a call to InvalidateRect , and pSender can be used to omit a view from the update cycle.
27
THE MdiSquares APPLICATION
The MdiSquares application is an MDI version of Chapter 9’s SdiSquares. First document is opened automatically. Additional documents can be opened by selecting New from File Menu. An other view of a document can be opened by selecting New Window form Window Menu. If a color changes in a view of a document, changes are reflected to other views of the same document by SetSquare function of the application. This function calls UpdateAllViews(NULL); after adding a color to a square.
28
CLASSES //MdiSquares.h class CMdiSquaresApp : public CWinApp //MainFrame.h class CMainFrame : public CMDIFrameWnd //ChildFrm.h class CChildFrame : public CMDIChildWnd The main frame window class, CMainFrame , represents the applications top level window. Views are displayed in instances of the child frame window class, CChildFrame.
29
InitInstance //MdiSquares.cpp,InitInstance CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( IDR_MDISQUTYPE, RUNTIME_CLASS(CSquaresDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CSquaresView)); AddDocTemplate(pDocTemplate); In InitInstance, CChildFrame, not CMainFrame, is identified as the frame window class when the document template is initialized.
30
MAIN FRAME WINDOW Consequently, calling ProcessShellCommand in an MDI application creates a new child frame window. So, an MDI application must create the top-level frame window itself before calling ProcessShellCommand. //MdiSquares.cpp,InitInstance // create main MDI Frame window CMainFrame* pMainFrame = new CMainFrame; if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; m_pMainWnd = pMainFrame;
32
RESOURCES If list of resources browsed, two icons, two menus and two document strings can be seen. Their resource IDs are IDR_MAINFRAME and IDR_MDISQUTYPE.
33
ICONS The IDR_MAINFRAME icon is displayed in the title bar of the top-level window. The IDR_MDISQUTYPE icon is displayed in the title bars of the child frames.
34
MENUS The IDR_MAINFRAME menu is displayed when no documents are open. It is a minimal menu that features a File menu with New, Open and Exit commands and a recently used file list. The IDR_MDISQUTYPE menu is displayed when at least one document is open. This is a full blown menu with all the commands that pertain to MdiSquares documents.
35
DOCUMENT STRING The IDR_MAINFRAME document string contains the title that appears in the main windows title bar. The IDR_MDISQUTYPE document string contains all relevant information about the document type.
36
SUPPORTING MULTIPLE DOCUMENT TYPES
An MDI application written in MFC supports multiple document instances by default. A new document instance is created each time the user executes a File/New command. MDI also support multiple document types, each characterized by a unique document template.
37
ADD A NEW DOCUMENT TYPE Derive a new document class and a new view class(CCirclesDoc, CCirclesView). Make classes dynamically creatable just like AppWizard generated ones. Add four new resources to the project: an icon, a menu, an accelerator(optional), and a document string. Assign the same resource ID to all these resources. (IDR_CIRCLETYPE) Modify InitInstance to create a new document template containing the resource ID and CRuntimeClass pointers for the document, view and frame window classes.
38
InitInstance //InitInstance CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate( IDR_MDISQUTYPE, RUNTIME_CLASS(CSquaresDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CSquaresView)); AddDocTemplate(pDocTemplate); IDR_CIRCLETYPE, RUNTIME_CLASS(CCirclesDoc), RUNTIME_CLASS(CCirclesView));
39
ALTERNATIVES TO MDI To edit several windows at once:
A workspace-based model that groups related documents in objects called workspaces(Visual C++). A workbook model in which individual views occupy the full client area of a top level frame window but only one window at a time is visible. A project model that groups related documents in projects but allows individual documents to be edited in SDI-like frame windows. *The Windows Interface Guidelines for Software Design
40
Introduction MFC AND THE MULTIPLE DOCUMENT INTERFACE SPLITTER WINDOWS
Synchronizing Multiple Views of a Document The MdiSquares Application Supporting Multiple Document Types Alternatives to MDI SPLITTER WINDOWS Dynamic Splitter Windows The Sketch Application Static Splitter Windows The Wanderer Application Custom Command Routing Three-Way Splitter Windows Dynamic Splitter Windows with Multiple View Types
41
SPLITTER WINDOWS A window that can be divided into two or more panes horizontally, vertically, or both. Two types of splitter Window Static : If user is not allowed to change splitter windows rows and columns. Dynamic
42
DYNAMIC SPLITTER WINDOWS
Created by CSplitterWnd::Create 2 steps Add a CSplitterWnd data member to the frame window class. Override the frame windows’ virtual OnCreateClient function and call CSplitterWnd::Create. BOOL CMainFrame::OnCreateClient (LPCREATESTRUCT lpcs, CCreateContext* pContext) { return m_wndSplitter.Create(this, 2, 1, CSize (1,1), pContext); }
43
DYNAMIC SPLITTER WINDOWS
To add a Split command to your application’s menu, include a menu item whose ID is ID_WINDOW_SPLIT. ID_WINDOW_SPLIT is linked to the handlers Cview::OnSplitCmd & OnUpdateSplitCmd for update.
44
DYNAMIC SPLITTER WINDOWS
45
THE SKETCH APPLICATION
A dynamic splitter window Application Simple line drawings. For split windows, use UpdateAllViews. Does not exhibit the flashing effect that afflicts MdiSquares .
46
STATIC SPLITTER WINDOWS
Requires an extra step than dynamic one. Steps to create static splitter window Add a CsplitterWnd data member to the frame window class. Override the frame windows’ OnCreateClient function and call CSplitterWnd::CreateStatic to create a static splitter window. Use CSplitterWnd::CreateView to create a view.
47
THE CODE FOR CREATION PROCESS
BOOL CMainFrame::OnCreateClient (LPCREATESTRUCT lpsc, CCreateContext* pContext) { if (!m_wndSplitter.CreateStatic (this, 1, 2) || !m_wndSplitter. CreateView (0,0, RUNTIME_CLASS (CTextView), Csize (128,0), pContext) || !m_wndSplitter.CreateView (0,1, RUNTIME_CLASS (CPictureView), Csize (0,0), pContext)) return FALSE; return TRUE; }
48
THE WANDERER APPLICATION
A static splitter window application The right pane is created before the left pane. Put the picture.
49
CUSTOM COMMAND ROUTING
If View is not active, then View window is not updated. Therefore, the need for custom command routing. Done through OnCmdMsg function.
50
THREE-WAY SPLITTER WINDOWS
Static creation is easier than dynamic creation. Sometimes access violations when splitting the dynamic splitter window Solution? Next Slide.
51
THREE-WAY SPLITTER WINDOWS
Derive a class from CSplitterWnd and replace CSplitterWnd::SplitRow in the derived class with; BOOL CNestedSplitterWnd::SplitRow (int cyBefore) { GetParentFrame () -> SetActiveView ((Cview *) GetPane (0,0)); return CSplitterWnd::SplitRow (cyBefore); } Make the nested dynamic splitter an instance of the derived class rather than an instance of CSplitterWnd.
52
DYNAMIC SPLITTER WINDOWS WITH MULTIPLE VIEW TYPES
CSplitterWnd class has some virtual functions that can be overridden. Override CSplitterWnd function CreateView to get different types of views in different panes.
53
End of Presentation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.