The sample program shown in Figure 12-8 demonstrates many of the principles discussed in the preceding sections. MyWord is a miniature word processor built around a CRichEditView. MFC's CRichEditView class is like a CEditView on steroids; based on the rich text edit control supplied in the common controls library, it features superior text formatting capabilities and the ability to read and write rich text format (RTF) files with a simple function call. MyWord doesn't use all the features of a CRichEditView; in fact, it barely scratches the surface. (For a more in-depth look at CRichEditView, see the Wordpad sample program provided with MFC. The Wordpad files are the actual source code for the Wordpad applet that ships with Windows.) But MyWord packs a lot of punch for a program that's only a few hundred lines long, and it's a good starting point for writing CRichEditView-based applications of your own.
Figure 12-8. The MyWord window.
MyWord uses two toolbars and one status bar. The main toolbar includes buttons that serve as shortcuts for the New, Open, and Save items in the File menu and the Cut, Copy, Paste, and Undo items in the Edit menu. The other toolbar, which I'll refer to as the style bar, includes check push buttons for setting the character format (bold, italic, and underline), radio push buttons for setting the paragraph alignment (left aligned, centered, and right aligned), and combo boxes for selecting typefaces and font sizes. Both toolbars can be detached from the frame window, floated, and docked at other locations; and both can be resized while they're floating. Try it: Drag the main toolbar to the right side of the window, and dock it in a vertical position. Grab the style bar and release it in the center of the window so that it becomes a floating palette. Use the View menu to hide and display the toolbars and the status bar. You can also hide a toolbar by clicking the close button in the mini frame window it floats in when it's detached from the main frame window. To redisplay the toolbar, simply select the Toolbar or Style Bar command in the View menu.
The status bar at the bottom of MyWord's frame window displays help text for menu items and toolbar controls. It also includes Caps Lock and Num Lock indicators and a line number display that's continually updated as the caret moves through the document. The Caps Lock and Num Lock indicators were added using MFC's predefined ID_INDICATOR_CAPS and ID_INDICATOR_NUM IDs. The line number indicator is updated by an ON_UPDATE_COMMAND_UI handler that, when called, retrieves the current line number from the CRichEditView, formulates a text string containing the line number, and updates the status bar display with CCmdUI::SetText. The line number pane is sized to fit the dummy string "Line 00000," whose resource ID, ID_INDICATOR_LINE, is identical to the status bar pane's ID. The dummy string is never seen because the pane is updated with a real line number before the status bar appears on the screen.
I used AppWizard to begin MyWord. I checked the Docking Toolbar and Initial Status Bar options in AppWizard's Step 4 dialog box, and in Step 6, I selected CRichEditView as the base class for the view. I next derived a class named CStyleBar to represent the style bar, added a CStyleBar data member to the frame window class, and modified the frame window's OnCreate function to create the style bar. (I used ClassWizard to perform the class derivation, but because ClassWizard doesn't support CToolBar as a base class, I derived CStyleBar from CCmdTarget and then manually patched up the code to change the base class to CToolBar.) I used Visual C++'s Insert-Resource command to create the toolbar resource from which the style bar is created, and I added buttons in the toolbar editor. Finishing MyWord was a matter of writing the command handlers, update handlers, and ordinary class member functions that form the core of the application.
The source code for MyWord's frame window, document, view, and style bar classes is listed in Figure 12-9. Take a moment to look it over to see how the toolbars and status bar are handled. Then go to "The Main Toolbar" to read about pertinent parts of the source code in greater detail.
Figure 12-9. The MyWord application.