|
This chapter describes how to convert the application created in Step 10 to use the Multiple Document Interface, or MDI for short. The application in Step 10 is what is known as a Single Document Interface, or SDI, application. That means the application can support and display only a single document at a time. In the sense that it's used here, document doesn't have the same meaning you might be used to. Instead of a paper document or a word-processing document, a document refers to any set of data that your application displays and manipulates. In the case of the tutorial application, documents are the drawing files that the application creates. Converting the application to use MDI adds the ability to support multiple drawings open at the same time in multiple child windows. Understanding the MDI modelAn MDI application functions a little differently from an SDI application. In Step 10, the Drawing Pad application displayed a single drawing in a window. The window that actually displayed the drawing was a client of the frame window. The frame window managed general application tasks, such as menu handling, resizing, painting menus and control bars, and so on. The client window managed tasks specific to the application, such as handling mouse movements and button clicks in the client area, painting the lines in the drawing, responding to application-specific events, and so on. In comparison, MDI applications divide tasks up three ways instead of two:
In this step, you'll take the example from Step 10 and restructure it to support MDI functionality. It's not as complicated as it may seem; most of the new classes you'll construct can be taken straight from the existing TDrawWindow class! Adding the MDI header filesThere are a number of new header files you need to include to add MDI capability to your application. This section describes the header files that need to be changed or added. It also describes the classes that are defined in each header file. Changing the resource script fileYou need to change the include statement for the STEP10.RC resource script file to include the STEP11.RC resource script file. There are only two changes you need to make to STEP11.RC:
The functions that handle these events are described later on.
Replacing the frame window header fileIn the place of owl\decframe.h, you need to include owl\decmdifr.h. This header file contains the definition of the TDecoratedMDIFrame class, which is derived from TMDIFrame and TDecoratedFrame. TMDIFrame, defined in the owl\mdi.h header file, adds the support for containing an MDI client window to the support already provided by TFrameWindow for command processing and keyboard navigation. MDI client windows are discussed below. As shown in the previous step of the tutorial, TDecoratedFrame provides the ability to support decorations such as control bars and status bars. Since the tutorial application already supports decorations from the previous step, you can use the decorated version of the MDI frame window to keep this functionality. Adding the MDI client and child header filesYou need to add the owl\mdi.h and owl\mdichild.h header files. owl\mdi.h contains the definition of the TMDIFrame and TMDIClient classes. TMDIClient provides the functionality necessary for managing MDI child windows. MDI child windows are the windows that the user of your application actually works with and that display the data contained in each document. TMDIClient provides the ability to
owl\mdichild.h contains the definition of the TMDIChild class, which is derived from TWindow. TMDIChild overrides a number of TWindow's function to provide the ability to function as an MDI child. You usually derive new classes from both TMDIClient and TMDIChild to provide the specific functionality required by your application. Creating new classes from TMDIClient and TMDIChild to support the Drawing Pad application is discussed later in this step. Changing the frame windowThe first step in moving the drawing application to MDI is to change the frame window. MDI applications use specialized MDI frame windows. As discussed earlier, ObjectWindows provides two MDI frame window classes, TMDIFrame and TDecoratedMDIFrame. Because we're using the TDecoratedMDIFrame class for the frame window, discussion of the TMDIFrame class is left for "Window objects" of the ObjectWindows Programmer's Guide. Here's the constructor for TDecoratedMDIFrame:
where:
Besides adding the owl\decmdifr.h header file, two other changes are required to use a TDecoratedMDIFrame in the tutorial application. The first is changing the line in the TDrawApp::InitMainWindow function where the frame window is created:
As before, the frame window caption is Drawing Pad. The frame window is initialized with the COMMANDS menu resource. The client window is a new TDrawMDIClient, which is a TMDIClient-derived class that you'll define a little bit later in this step. The final parameter indicates that menu tracking should be on for this window. The module parameter is left to its default value of 0. The second change is removing the AssignMenu call at the end of the InitMainWindow function of Step 10. This call is no longer necessary because the menu resource is set up by the second parameter of the TDecoratedMDIFrame constructor. Your InitMainWindow function should now look something like this:
These are the only changes necessary to the TDrawApp class to support MDI functionality. Creating the MDI window classesThe functionality contained in the TDrawWindow class in the previous step needs to be divided up into two classes in the MDI model. The reason for this is that there are two windows that handle messages and user input:
The next sections discuss how to create the MDI client and child window classes for the tutorial application. Creating the MDI child window classYou need to create a class declaration for the TDrawMDIChild class, along with defining the functions for the class. You can reuse most of the class declaration for TDrawWindow from Step 10, along with most of the functions with only a few changes. Declaring the TDrawMDIChild classThe class declaration for TDrawMDIChild is very similar to the declaration of the TDrawWindow class from Step10. Here are the changes you need to make:
Your class declaration should look something like this:
Creating the TDrawMDIChild functionsJust about all of the functions in TDrawMDIChild can be carried over from the TDrawWindow class. The only thing you need to do is change the class identifier in the function declarations from TDrawWindow to TDrawMDIChild. For example, the declaration for the EvLButtonDown function changes from this:
to this:
Change the class identifiers for the following functions:
There is one minor change you need to make to the CmFileSaveAs function. Because the name of the drawing usually changes when the user calls the File|Save As command, you need to set the caption of the window to the file name. To do this, use the SetCaption function. This function takes a char*, which in this case should be the FileName member of the FileData object. The CmFileSaveAs function should now look like this:
Creating the TDrawMDIChild constructorThe main difference between TDrawMDIChild and the TDrawWindow class, other than the fact that TDrawMDIChild has three fewer functions than TDrawWindow, is in the constructor. Initializing data membersLike TDrawWindow, TDrawMDIChild contains the device context object that displays the drawing and manages the arrays that contain the line drawing information. It also contains the IsDirty flag, setting it to false when the drawing is first created or opened and setting it to true when the drawing is modified. So the variables that contain the data for these functions-DragDC, Line, Lines, and IsDirty-need to be initialized in the TDrawMDIChild constructor. This looks just the same as their initialization in the TDrawWindow class.
There are some notable changes from TDrawWindow's constructor here, however. First, the Init function is no longer called. TMDIChild does not provide an Init function. Instead, you should just call the base class constructor in the TDrawMDIChild initialization list, like so:
Initializing file information data membersYou can no longer simply initialize the IsNewFile variable to true, assuming that you are creating a new drawing whenever you create a window. In earlier steps this was a valid assumption: when the window was created, it hadn't opened a file yet, but was available to be drawn in. The IsNewFile flag was only set to false once a drawing had either been saved to a file or an existing drawing had been opened from a file into a window that had already been created. In this case, the MDI client parent window will handle the file creation and opening operations. It then creates a child window to contain the new or existing drawing. The child window has to find out from the parent whether this is a new drawing or an existing drawing opened from a file. For the same reason, the MDI child window does not necessarily create the TOpenSaveDialog::TData referenced by the FileData member. The TDrawMDIClient class has a function (or will have, when you get around to creating it) called GetFileData. This function takes no parameters and returns a pointer to a TOpenSaveDialog::TData object. If the MDI client window is creating the child window in response to a CM_FILEOPEN event, it creates a new TOpenSaveDialog::TData object containing the information about the file to be opened. GetFileData returns a pointer to that object. But if the client window is creating the child window in response to a CM_FILENEW event, TDrawMDIClient doesn't create a TOpenSaveDialog::TData object and GetFileData returns 0. So the MDI child can find out whether this is a new drawing or not by testing the return value of GetFileData. If GetFileData returns a valid object, then it should assign the pointer to this object to its FileData member and set IsNewFile to false. It can then call the OpenFile function to load the drawing just as it did before. If GetFileData doesn't return a valid object (that is, it returns 0), the MDI child should set IsNewFile to true and create a new TOpenSaveDialog::TData object. The file name in the new object is set in the CmFileSaveAs function, just as it was in previous steps. The constructor for TDrawMDIChild should look something like this:
Note that, in the case of an open operation, the child assigns the pointer returned by GetFileData to its FileData member. Once this is done, the child takes over responsibility for the TOpenSaveDialog::TData object, including responsibility for cleaning it up. Since this is already done in the destructor, you don't have to do anything else. Creating the MDI client window classThe TDrawMDIClient class manages the multiple child windows open on its client area and all the attendant functionality, such as creating new children, closing windows either singly or all at one time, tiling or cascading the windows, and arranging the icons of minimized children. TDrawMDIClient inherits a great deal of this functionality from the TMDIClient class. TMDIClient functionalityIt is important to understand the TMDIClient class, for the main reason that it is going to do a lot of work for you. TMDIClient is virtually derived from the TWindow class. TMDIClient overrides two of TWindow's virtual functions, PreProcessMsg and Create, to provide specific keyboard and menu handling functionality required by the client window. TMDIClient also handles a number of events, which are described in Table 11.2.
The Drawing Pad application actually only provides menu items for four of these-CM_TILECHILDREN, CM_CASCADECHILDREN, CM_ARRANGEICONS, and CM_CLOSECHILDREN. These response functions are simply wrappers for other TMDIClient functions that actually perform the work necessary. Each response function calls a function with the same name without the Cm prefix, so that CmCreateChild calls the CreateChild function. The only exception is CmTileChildrenHoriz, which calls the TileChildren function with the MDITILE_HORIZONTAL parameter. Another function provided by TMDIClient is the GetActiveMDIChild function, which returns a pointer to the active MDI child window. Note that there can only be one active MDI child window at any time, but there is always one active MDI child window, even if all the MDI child windows are minimized. There is one other function to discuss, InitChild. This is the only function in TMDIClient that you need to override in TDrawMDIClient. InitChild and overriding it to work with TDrawMDIClient are discussed on this page. Data members in TDrawMDIClientTDrawMDIClient requires a couple of new data members. These should both be declared private. The first is NewChildNum. The only function of this variable is to keep track of the number of new drawing created by the CmFileNew function. This number is used for the window caption of all new drawings. It is initialized to 0 in the TDrawMDIClient constructor. The second is FileData, a pointer to a TOpenSaveDialog::TData object, just like the FileData member of TDrawMDIChild. FileData is used to hold the file information when a user opens an existing file. It is set to 0 in the constructor. FileData is also set to 0 once the MDI child window has been opened. As shown on this page, the object returned by GetFileData is assigned to the FileData member of TDrawMDIChild. The object returned by GetFileData is actually the object (or lack thereof in the case of a new file) pointed to by TDrawMDIClient`s FileData member. Adding response functionsIn addition to the events handled by TMDIClient, TDrawMDIClient also handles the events formerly handled by TDrawWindow and not handled by TDrawMDIChild-CM_FILENEW, CM_FILEOPEN, and CM_ABOUT. The CmAbout response function is mostly unchanged from the TDrawWindow version, other than changing the class specifier. On the other hand, the CmFileNew and CmFileOpen functions must be substantially changed. CmFileNewThe CmFileNew function is actually simplified from its TDrawWindow version. It no longer has to deal with flushing the line arrays, invalidating the window, and setting flags. Instead it sets FileData to 0 so that the MDI child object can tell that it is displaying a new drawing, increments NewChildNum, then calls CreateChild. CreateChild is the function that actually creates and displays the new MDI child window. It is discussed in more detail in the discussion of the InitChild function. The CmFileNew function should now look something like this:
CmFileOpenThere are a number of differences between the TDrawWindow version of CmFileOpen and the TDrawMDIClient version.
Your CmFileOpen function should look something like this:
GetFileDataThe only new function required for TDrawMDIClient is GetFileData. This function is called by TDrawMDIChild in its constructor. This function should take no parameters and return a pointer to a TOpenSaveDialog::TData object. Its function is to return a pointer to the object pointed to by TDrawMDIClient's FileData member. If FileData references a valid object (that is, during a file open operation), GetFileData should return FileData. If FileData doesn't reference a valid object (that is, during a file new operation), GetFileData should return 0. The actual function definition is very simple and can be inlined by defining the function inside the class declaration. Your GetFileData function should look something like this:
Overriding InitChildThe only TMDIClient function that TDrawMDIChild overrides is the InitChild function. InitChild takes no parameters and returns a pointer to a TMDIChild object. The CreateChild function calls InitChild before creating a new MDI child window. It is in InitChild that you create the TMDIChild or TMDIChild-derived object for the MDI child window. This is the only function of TMDIClient that you'll override when you create the TDrawMDIClient class. The InitChild function for TDrawMDIClient is fairly straightforward. If FileData is 0, you should create a character array to contain a default window title. This can be initialized using the value of NewChildNum so that each new drawing has a different title. Then you should create a TMDIChild* and create a new TDrawMDIChild object. The constructor for TDrawMDIChild takes two parameters, a reference to a TDrawMDIClient object for its parent window and a const char* containing the MDI child window's caption. In this case, the first parameter should be the dereferenced this pointer. The second parameter should be either the FileName member of the FileData object if FileData references a valid object or the character array you created earlier if not. Once the MDI child object has been created, you need to call the SetIcon function for the object. SetIcon associates an icon resource with the function's object. This icon is displayed in the client area when the child window is minimized. You can set the icon to the icon provided for the tutorial application called IDI_TUTORIAL. The last step of the function is to return the TMDIChild pointer. Your InitChild function should look something like this:
Where to find more informationMDI frame, client, and child windows are described in "Window objects" in the ObjectWindows Programmer's Guide. |
||||||||||||||||||||||||||||||||||||||||||||||||
Last updated: NA |