This section explains the sixth step in writing Phone Book: add message-handler functions for the Person menu. Figure 6.3 shows the Phone Book Person menu.
·To add the Person menu message-handlers:
1.Add the following OnAdd message-handler member function to the CMainWindow section of your VIEW.CPP file below the OnExit member function:
// CMainWindow::OnAdd
// Using the EditDialog fill in a new person object. If the user
// selects OK then add the person, call OnSize to resize the scroll
// region, and invalidate the screen so it will be redrawn with the
// new person in the correct order.
//
void CMainWindow::OnAdd()
{
CPerson* person=new CPerson();
CEditDialog dlgAdd( person, this );
if ( dlgAdd.DoModal() == IDOK )
{
m_people.AddPerson( person );
OnSize( 0, m_cxClient, m_cyClient );
}
else
delete person;
}
OnAdd implements the Add command in the Person menu. It constructs a new CPerson object, constructs a CEditDialog object so the user can fill in the person's name and phone number, and calls member functions of the database object to add the person object to the list.
2.Add the following OnDelete message-handler member function to the CMainWindow section of your VIEW.CPP file below the OnAdd member function:
// CMainWindow::OnDelete
// Deletes the current selection. Check to see if the selection is
// now past the end of the list. Also call OnSize since the list
// length has now changed.
//
void CMainWindow::OnDelete()
{
if ( m_nSelectLine == -1 )
{
MessageBox( "Select a person to delete first" );
return;
}
m_people.DeletePerson( m_nSelectLine );
if ( m_nSelectLine >= (int)m_people.GetCount() )
m_nSelectLine--;
OnSize( 0, m_cxClient, m_cyClient );
}
OnDelete works on the currently selected person in the database. It implements the Delete command in the Person menu to delete the selected person.
3.Add the following OnFind message-handler member function to the CMainWindow section of your VIEW.CPP file below the OnDelete member function:
// CMainWindow::OnFind
// Gets information from the CFindDialog modal dialog box, then
// searches for matching people. Note the Add and Delete menu
// items are disabled after a find is made. Find All is enabled.
//
void CMainWindow::OnFind()
{
CFindDialog dlgFind( this );
if ( dlgFind.DoModal() == IDOK &&
dlgFind.GetFindString().GetLength() != 0 )
{
if ( m_people.DoFind( dlgFind.GetFindString() ) )
{
m_nSelectLine = -1;
CString tmp;
tmp = m_people.GetTitle() + " Found: "
+ dlgFind.GetFindString();
SetWindowText( tmp );
CMenu* pMenu = GetMenu();
pMenu -> EnableMenuItem( IDM_FINDALL, MF_ENABLED );
pMenu -> EnableMenuItem( IDM_DELETE, MF_GRAYED );
pMenu -> EnableMenuItem( IDM_ADD, MF_GRAYED );
OnSize( 0, m_cxClient, m_cyClient );
}
else
MessageBox( "No match found in list." );
}
}
OnFind constructs a dialog object requesting a search string. The string is passed to the database object's DoFind member function, which searches the database and returns a list of pointers to found person objects. The window's client area is invalidated so that the next time the window is painted, the found list is displayed instead of the main database list.
4.Add the following OnFindAll message-handler member function to the CMainWindow section of your VIEW.CPP file below the OnFind member function:
// CMainWindow::OnFindAll
// Returns to view the whole database. Add, Delete are re-enabled,
// and Find All is again disabled. OnSize is called because the list
// has changed length.
//
void CMainWindow::OnFindAll()
{
m_people.DoFind();
SetWindowText( m_people.GetTitle() );
CMenu* pMenu = GetMenu();
pMenu -> EnableMenuItem( IDM_FINDALL, MF_GRAYED );
pMenu -> EnableMenuItem( IDM_DELETE, MF_ENABLED );
pMenu -> EnableMenuItem( IDM_ADD, MF_ENABLED );
OnSize( 0, m_cxClient, m_cyClient );
}
When a list of finds is on display, OnFindAll returns the original database list to the display, replacing the list of found persons.
5.Add the following OnEdit message-handler member function to the CMainWindow section of your VIEW.CPP file below the OnFindAll member function:
// CMainWindow::OnEdit
// Using the member variable m_nSelectLine a CEditDialog is created
// and filled with the selected person. If the dialog OK button is
// used the dialog saves the changes into the object.
//
void CMainWindow::OnEdit()
{
if ( m_nSelectLine == -1 )
{
MessageBox( "Select a person to edit first" );
return;
}
// Get a pointer to the person in the list.
CPerson* pPerson = m_people.GetPerson( m_nSelectLine );
CPerson tmpPerson = *pPerson;
//Edit the data.
CEditDialog dlgEdit( &tmpPerson, this );
//if the ok button is pressed redraw the screen
if ( dlgEdit.DoModal() == IDOK )
{
m_people.ReplacePerson( pPerson, tmpPerson );
InvalidateLine();
}
}
OnEdit implements the Edit command in the Person menu. It constructs a dialog object that displays the information contained in the currently-selected person object. When the user ends the dialog, the selected person object is replaced by a new object containing the new information.
To continue the tutorial, see “Add Message Handlers for Help Menu Commands” on page 222. For more information on the handlers you just added, see the discussion in the next section.