Showing posts with label SDI/Splitter Window/MDI. Show all posts
Showing posts with label SDI/Splitter Window/MDI. Show all posts

Simple MDI app

In this article we will see how to enable the user to open more that one document at a time.
In an MDI application there is a top-level frame window. This window is derived from CMDIFrameWnd rather than CFrameWnd. Every document that we open would contain a three-way splitter window. The view of every document that we open would be housed in MDI child windows whose functionality is encapsulated in MFC’s CMDIChildWnd class. The MDI version of myapp::InitInstance( ) is similar to the SDI version, but it differs in several important respects. To begin with, the class from which the document template is created is CMultiDocTemplate instead of CSingleDocTemplate. The resource id passed to the document template’s constructor is IDR_CHILDFRAME instead of IDR_MAINFRAME. There are separate menus, icons and document strings for each of these resource ids. For example, the menu associated with IDR_MAINFRAME contains only two menu items ‘File’ and ‘About’. As against this, the menu associated with IDR_CHILDFRAME contains items like ‘File’, ‘Width’, ‘Colors’, ‘Brush’ and ‘Shapes’. Similarly, the document strings of IDR_MAINFRAME and IDR_CHILDFRAME are different. These are shown below: IDR_MAINFRAME Shapes App IDR_CHILDFRAME \nNoname\n\nShape Files(*.shp) \n.shp \nShapes.Document\nOur Shapes Document Note that IDR_MAINFRAME’s document string now contains only the caption of the top-level frame window. The other 6 substrings have now been shifted to IDR_CHILDFRAME’s document string. We have dropped the first subsrting from IDR_CHILDFRAME’s document string. This is because for every new document that we open, a new window to house its view is created. The title of this window is same as the name of the document. This means that for every new document that we open a new title would appear in the caption bar. The framework manages the displaying of this title internally. Even if we mention the first substring, framework will simply ignore it. What would be the title if we open a new document? In this case the framework uses the name mentioned in the second substring of IDR_CHILDFRAME’s document string. In our case this name would he ‘Noname’. The IDR_MAINFRAME’s icon is displayed in the title bar of the top-level MDI frame window. The IDR_CHILDFRAME’s icon is displayed in the title bars of the each document’s frame windows. Another difference that you all will find in myapp::InitInstance( ) is the way the top-level frame window is created. In a SDI application, to create a frame window dynamically we just had to pass the RUNTIME_CLASS information about myframe while creating the CSingleDocTemplate object. However, in a MDI application we have passed the RUNTIME_CLASS information of mychildwnd (rather than myframe) while creating the CMultiDocTemplate object. Hence it is necessary to create the top-level MDI frame window ourselves. We have done this through the statements:
myframe *f = new myframe ;
f -> LoadFrame ( IDR_MAINFRAME ) ;
m_pMainWnd = f ;
T his code would create the top-level frame window and load its icon, menu, accelerator and the document string, all in one step. Imagine a situation where we have already opened a document in our application. If we now double click a ‘.shp’ document in the explorer window another instance of our application would begin. To prevent this we have called CWinApp::EnableShellOpen( ). Now the double-clicked document will be opened in the existing instance of our application rather than causing another instance to be started. In a MDI application we want splitter windows in MDI child windows. Hence we have created splitter windows in mychildwnd::OnCreateClient( ). The process of creation of splitter windows is same as in SDI applications.

    Opening differnt types of documents in an MDI application


    The MDI architecture not only permits us to open more than one document of a particular type, but it also allows us to open different types of documents in an application. To help you understand this facility we have put together an application, which has the following provisions:
    (a) There are two types of documents: one, which stores information about the lines and rectangles drawn interactively, and another, which stores information about the circles drawn interactively. Here onwards we would call these documents as landr and circle documents respectively.
    (b) When the landr document is opened, two views of the document are displayed in splitter windows. We have first created a static splitter window containing two panes. The right pane contains the text view showing the line count, the rectangle count, the current shape, line color, line thickness and brush color. The left pane houses a dynamic splitter window containing two panes. Each of these panes show the graphics view of lines and rectangles. When the circle document is opened, using the document data, circles are built in a view window. Unlike the view of landr document mentioned above, this view does not contain any splitter window.
     (c) Whenever we open a landr or a circle document the window housing its view(s) would show an appropriate icon. Note that these icons are different than the icon of the frame window.
    (d) Whenever we attempt to open a new document using ‘File | New’, a dialog box is popped up asking the user to select the type of document to open. A similar dialog is also popped up when we start the application with an empty document. This dialog is shown below:Figure 1. Opening a document in an MDI application.
    The names of the types of documents in the above dialog are obtained through the third sub-string of the document string. The document strings that we have created for the two types of documents are as under:
    I DR_CHILDFRAME1 :  \nNoname\nLine Rectangle Document \nLineRect Files(*.lnr)\n.lnr \nShapes.Document\nOur Shapes Document
    IDR_CHILDFRAME2 :  \nNoname\nCircle Document \nCircle Files(*.cir) \n.cir\nShapes.Document \nOur Shapes
    Document Note that the fourth and the fifth sub-string in the above two document strings represent two different types of documents that the application supports. Figure 2 shows two documents created using this program. One shows line and rectangle drawn using the program and another shows the circles drawn using it.

      Figure 2. Mutiple types of documents opened in an MDI app.

      A simple splitter window example

      A splitter can be divided into two or more panes horizontally as well as vertically. Each pane size can be changed dynamically using movable splitter bars. Each pane can contain one view of the document’s data. The views are children of the splitter window, whereas, the splitter window is a child of a frame window. In this article we have created a static splitter window with two panes, one displays text and another graphics.