The first of this chapter's two examples shows a very simple document-view interaction. The CEx16aDoc document class, derived from CDocument, allows for a single embedded CStudent object. The CStudent class represents a student record composed of a CString name and an integer grade. The CEx16aView view class is derived from CFormView. It is a visual representation of a student record that has edit controls for the name and grade. The default Enter pushbutton updates the document with data from the edit controls. Figure 16-1 shows the EX16A program window.
Figure 16-1. The EX16A program in action.
Figure 16-2 shows the code for the CStudent class. Most of the class's features serve EX16A, but a few items carry forward to EX16B and the programs discussed in Chapter 17. For now, take note of the two data members, the default constructor, the operators, and the Dump function declaration. The DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros ensure that the class name is available for the diagnostic dump.
STUDENT.H
// student.h #ifndef _INSIDE_VISUAL_CPP_STUDENT #define _INSIDE_VISUAL_CPP_STUDENT class CStudent : public CObject { DECLARE_DYNAMIC(CStudent) public: CString m_strName; int m_nGrade; CStudent() { m_nGrade = 0; } CStudent(const char* szName, int nGrade) : m_strName(szName) { m_nGrade = nGrade; } CStudent(const CStudent& s) : m_strName(s.m_strName) { // copy constructor m_nGrade = s.m_nGrade; } const CStudent& operator =(const CStudent& s) { m_strName = s.m_strName; m_nGrade = s.m_nGrade; return *this; } BOOL operator ==(const CStudent& s) const { if ((m_strName == s.m_strName) && (m_nGrade == s.m_nGrade)) { return TRUE; } else { return FALSE; } } BOOL operator !=(const CStudent& s) const { // Let's make use of the operator we just defined! return !(*this == s); } #ifdef _DEBUG void Dump(CDumpContext& dc) const; #endif // _DEBUG }; #endif // _INSIDE_VISUAL_CPP_STUDENT |
STUDENT.CPP
#include "stdafx.h" #include "student.h" IMPLEMENT_DYNAMIC(CStudent, CObject) #ifdef _DEBUG void CStudent::Dump(CDumpContext& dc) const { CObject::Dump(dc); dc << "m_strName = " << m_strName << "\nm_nGrade = " <<m_nGrade; } #endif // _DEBUG |
Follow these steps to build the EX16A example:
The options and the default class names are shown here.
Use the default constant ID_EDIT_CLEAR_ALL, which is assigned by the application framework. A menu prompt automatically appears.
Be sure that the Styles properties are set exactly as shown in the Dialog Properties dialog (Style = Child; Border = None) and that Visible is unchecked.
Use the following IDs for the controls.
Control | ID |
Name edit control | IDC_NAME |
Grade edit control | IDC_GRADE |
Enter pushbutton | IDC_ENTER |
Object ID | Message | Member Function |
IDC_ENTER | BN_CLICKED | OnEnter |
ID_EDIT_CLEAR_ALL | COMMAND | OnEditClearAll |
ID_EDIT_CLEAR_ALL | UPDATE_COMMAND_UI | OnUpdateEditClearAll |
Control ID | Member Variable | Category | Variable Type |
IDC_GRADE | m_nGrade | Value | int |
IDC_NAME | m_strName | Value | CString |
For m_nGrade, enter a minimum value of 0 and a maximum value of 100. Notice that ClassWizard generates the code necessary to validate data entered by the user.
private: void UpdateControlsFromDoc();
void CEx16aView::OnInitialUpdate() { // called on startup UpdateControlsFromDoc(); } void CEx16aView::UpdateControlsFromDoc() { // called from OnInitialUpdate and OnEditClearAll CEx16aDoc* pDoc = GetDocument(); m_nGrade = pDoc->m_student.m_nGrade; m_strName = pDoc->m_student.m_strName;
UpdateData(FALSE); // calls DDX }
The OnEnter function replaces the OnOK function you'd expect to see in a dialog class. The function transfers data from the edit controls to the view's data members and then to the document. Add the boldface code shown here:
void CEx16aView::OnEnter() { CEx16aDoc* pDoc = GetDocument(); UpdateData(TRUE); pDoc->m_student.m_nGrade = m_nGrade; pDoc->m_student.m_strName = m_strName; }
In a complex multiview application, the Edit Clear All command would be routed directly to the document. In this simple example, it's routed to the view. The update command UI handler disables the menu item if the document's student object is already blank. Add the following boldface code:
void CEx16aView::OnEditClearAll() { GetDocument()->m_student = CStudent(); // "blank" student object UpdateControlsFromDoc(); } void CEx16aView::OnUpdateEditClearAll(CCmdUI* pCmdUI) { pCmdUI->Enable(GetDocument()->m_student != CStudent()); // blank? }
public: CStudent m_student;
The CStudent constructor is called when the document object is constructed, and the CStudent destructor is called when the document object is destroyed.
CEx16aDoc::CEx16aDoc() : m_student("default value", 0) { TRACE("Document object constructed\n"); }
We can't tell whether the EX16A program works properly unless we dump the document when the program exits. We'll use the destructor to call the document's Dump function, which calls the CStudent::Dump function shown here:
CEx16aDoc::~CEx16aDoc() { #ifdef _DEBUG Dump(afxDump); #endif // _DEBUG } void CEx16aDoc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); dc << "\n" << m_student << "\n"; }
a CEx16aDoc at $411580 m_strTitle = Untitled m_strPathName = m_bModified = 0 m_pDocTemplate = $4113A0 a CStudent at $4115D4 m_strName = Sullivan, Walter m_nGrade = 78
To see these messages, you must compile the application with the Win32 Debug target selected and you must run the program from the debugger.