| Tenouk C & C++ | MFC Home | Separating the Document from Its View 3 | Separating the Document from Its View 5 | Download | Site Index |


 

 

 

 

 

 

Module 10c:

Separating the Document from Its View 4

 

 

 

 

 

 

 

 

This is a continuation from the previous module... Program examples compiled using Visual C++ 6.0 (MFC 6.0) compiler on Windows XP Pro machine with Service Pack 2. Topics and sub topics for this Tutorial are listed below:

  1. Data Members

  2. Constructor

  3. GetList()

  4. DeleteContents()

  5. Dump()

  6. CMymfc16Doc Class

  7. ClassWizard and CMymfc16View

 

 

Data Members

 

The document class provides for an embedded CStudentList object, the m_studentList data member, which holds pointers to CStudent objects. The list object is constructed when the CMymfc16Doc object is constructed, and it is destroyed at program exit. CStudentList is a typedef for a CTypedPtrList for CStudent pointers.

 

 

Listing 13.

 

Constructor

 

The document constructor sets the depth of the dump context so that a dump of the list causes dumps of the individual list elements.

 

 

Listing 14.

 

GetList()

 

The inline GetList() function helps isolate the view from the document. The document class must be specific to the type of object in the list, in this case, objects of the class CStudent(). A generic list view base class, however, can use a member function to get a pointer to the list without knowing the name of the list object.

 

 

Listing 15.

 

DeleteContents()

 

The DeleteContents() function is a virtual override function that is called by other document functions and by the application framework. Its job is to remove all student object pointers from the document's list and to delete those student objects. An important point to remember here is that SDI document objects are reused after they are closed. DeleteContents() also dumps the student list.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Listing 16.

 

Dump()

 

AppWizard generates the Dump() function skeleton between the lines #ifdef _DEBUG and #endif. Because the afxDump depth was set to 1 in the document constructor, all the CStudent objects contained in the list are dumped.

 

 

 

Listing 17.

 

CMymfc16Doc Class

 

AppWizard originally generated the CMymfc16Doc class. Listing 18 shows the code used in the MYMFC16 example.

 

 

MYMFC16DOC.H

// Mymfc16Doc.h : interface of the CMymfc16Doc class

//

//////////////////////////////////////////////////////////////////////

 

#if !defined(AFX_MYMFC16DOC_H__4D011047_7E1C_11D0_8FE0_00C04FC2A0C2__INCLUDED_)

#define AFX_MYMFC16DOC_H__4D011047_7E1C_11D0_8FE0_00C04FC2A0C2__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#include "student.h"

 

class CMymfc16Doc : public CDocument

{

protected: // create from serialization only

    CMymfc16Doc();

    DECLARE_DYNCREATE(CMymfc16Doc)

 

// Attributes

public:

    CStudentList* GetList() {

        return &m_studentList;

    }

 

// Operations

public:

 

// Overrides

    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CMymfc16Doc)

    public:

    virtual BOOL OnNewDocument();

    virtual void Serialize(CArchive& ar);

    virtual void DeleteContents();

    //}}AFX_VIRTUAL

 

// Implementation

public:

    virtual ~CMymfc16Doc();

#ifdef _DEBUG

    virtual void AssertValid() const;

    virtual void Dump(CDumpContext& dc) const;

#endif

 

protected:

 

// Generated message map functions

protected:

    //{{AFX_MSG(CMymfc16Doc)

    afx_msg void OnEditClearAll();

    afx_msg void OnUpdateEditClearAll(CCmdUI* pCmdUI);

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

private:

    CStudentList m_studentList;

};

 

//////////////////////////////////////////////////////////////////////

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations

// immediately before the previous line.

 

#endif // !defined(AFX_MYMFC16DOC_H__4D011047_7E1C_11D0_8FE0_00C04FC2A0C2__INCLUDED_)

 

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MYMFC16DOC.CPP

// Mymfc16Doc.cpp : implementation of the CMymfc16Doc class

//

 

#include "stdafx.h"

#include "mymfc16.h"

 

#include "Mymfc16Doc.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[ ] = __FILE__;

#endif

 

//////////////////////////////////////////////////////////////////////

// CMymfc16Doc

 

IMPLEMENT_DYNCREATE(CMymfc16Doc, CDocument)

 

BEGIN_MESSAGE_MAP(CMymfc16Doc, CDocument)

    //{{AFX_MSG_MAP(CMymfc16Doc)

    ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)

    ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

//////////////////////////////////////////////////////////////////////

// CMymfc16Doc construction/destruction

 

CMymfc16Doc::CMymfc16Doc()

{

    TRACE("Entering CMymfc16Doc constructor\n");

#ifdef _DEBUG

    afxDump.SetDepth(1); // Ensure dump of list elements

#endif // _DEBUG

}

 

CMymfc16Doc::~CMymfc16Doc()

{

}

 

BOOL CMymfc16Doc::OnNewDocument()

{

    TRACE("Entering CMymfc16Doc::OnNewDocument\n");

    if (!CDocument::OnNewDocument())

        return FALSE;

 

    // TODO: add re-initialization code here

    // (SDI documents will reuse this document)

 

    return TRUE;

}

//////////////////////////////////////////////////////////////////////

// CMymfc16Doc serialization

 

void CMymfc16Doc::Serialize(CArchive& ar)

{

    if (ar.IsStoring())

    {

        // TODO: add storing code here

    }

    else

    {

        // TODO: add loading code here

    }

}

 

//////////////////////////////////////////////////////////////////////

// CMymfc16Doc diagnostics

 

#ifdef _DEBUG

void CMymfc16Doc::AssertValid() const

{

    CDocument::AssertValid();

}

 

void CMymfc16Doc::Dump(CDumpContext& dc) const

{

    CDocument::Dump(dc);

    dc << "\n" << m_studentList << "\n";

}

#endif //_DEBUG

 

//////////////////////////////////////////////////////////////////////

// CMymfc16Doc commands

 

void CMymfc16Doc::DeleteContents()

{

#ifdef _DEBUG

    Dump(afxDump);

#endif

    while (m_studentList.GetHeadPosition()) {

        delete m_studentList.RemoveHead();

    }

}

 

void CMymfc16Doc::OnEditClearAll()

{

    DeleteContents();

    UpdateAllViews(NULL);

}

 

void CMymfc16Doc::OnUpdateEditClearAll(CCmdUI* pCmdUI)

{

    pCmdUI->Enable(!m_studentList.IsEmpty());

}

 

Listing 18: The CMymfc16Doc class listing.

 

ClassWizard and CMymfc16View Class

 

ClassWizard was used to map the CMymfc16View Clear pushbutton notification message as follows.

 

Object ID

Message

Member Function

IDC_CLEAR

BN_CLICKED

OnClear()

 

Table 10.

 

Mapping the CMymfc16View Clear pushbutton notification message.

 

Figure 27: Mapping the CMymfc16View Clear pushbutton notification message.

 

Because CMymfc16View is derived from CFormView, ClassWizard supports the definition of dialog data members. The variables shown here were added with the Add Variables button.

 

Control ID

Member Variable

Category

Variable Type

IDC_GRADE

m_nGrade

Value

int

IDC_NAME

m_strName

Value

CString

 

Table 11.

 

Adding member variables.

 

Figure 28: Adding member variables.

 

The minimum value of the m_nGrade data member was set to 0, and its maximum value was set to 100. The maximum length of the m_strName data member was set to 20 characters.

 

ClassWizard maps toolbar button commands to their handlers.

 

Modifying the toolbar button properties.

 

Figure 29: Modifying the toolbar button properties.

 

 

Here are the commands and the handler functions to which they were mapped.

 

 

Object ID

Message

Member Function

ID_STUDENT_HOME

COMMAND

OnStudentHome()

ID_STUDENT_END

COMMAND

OnStudentEnd()

ID_STUDENT_PREV

COMMAND

OnStudentPrev()

ID_STUDENT_NEXT

COMMAND

OnStudentNext()

ID_STUDENT_INS

COMMAND

OnStudentIns()

ID_STUDENT_DEL

COMMAND

OnStudentDel()

 

Table 12.

 

The message mapping using ClassWizard.

 

Message mapping of the commands and their handler functions.

 

Figure 30: Message mapping of the commands and their handler functions.

 

Each command handler has built-in error checking. The following update command UI message handlers are called during idle processing to update the state of the toolbar buttons and when the Student menu is painted, to update the menu items.

 

Object ID

Message

Member Function

ID_STUDENT_HOME

UPDATE_COMMAND_UI

OnUpdateStudentHome()

ID_STUDENT_END

UPDATE_COMMAND_UI

OnUpdateStudentEnd()

ID_STUDENT_PREV

UPDATE_COMMAND_UI

OnUpdateStudentHome()

ID_STUDENT_NEXT

UPDATE_COMMAND_UI

OnUpdateStudentEnd()

ID_STUDENT_DEL

UPDATE_COMMAND_UI

OnUpdateCommandDel()

 

Table 13.

 

Message mapping of the commands and their UI handler functions.

 

Figure 31: Message mapping of the commands and their UI handler functions.

 

For example, this button:

 

Which retrieves the first student record, is disabled when the list is empty and when the m_position variable is already set to the head of the list. The Previous button is disabled under the same circumstances, so it uses the same update command UI handler. The End and the Next buttons share a handler for similar reasons. Because a delay sometimes occurs in calling the update command UI functions, the command message handlers must look for error conditions.

 

 

Continue on next module...part 5.

 

 

 

 

Further reading and digging:

  1. MSDN MFC 7.0 class library online documentation.

  2. MSDN MFC 9.0 class library online documentation - latest version.

  3. Porting & Migrating your older programs.

  4. MSDN Library

  5. DCOM at MSDN.

  6. COM+ at MSDN.

  7. COM at MSDN.

  8. Windows data type.

  9. Win32 programming Tutorial.

  10. The best of C/C++, MFC, Windows and other related books.

  11. Unicode and Multibyte character set: Story and program examples.

 

 

 


 

 | Tenouk C & C++ | MFC Home | Separating the Document from Its View 3 | Separating the Document from Its View 5 | Download | Site Index |