home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c06 / modeless / dialogs.cpp next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.0 KB  |  148 lines

  1. // Dialogs.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Modeless.h"
  6. #include "Dialogs.h"
  7.  
  8. #include "XtraFunc.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CListWithItemDataDlg dialog
  18.  
  19. CListWithItemDataDlg::CListWithItemDataDlg(CWnd * pWnd, CWnd* pParent /*=NULL*/)
  20.     : CDialog(CListWithItemDataDlg::IDD, pParent),
  21.     m_pWnd (pWnd)
  22. {
  23.     //{{AFX_DATA_INIT(CListWithItemDataDlg)
  24.     m_phrase = _T("");
  25.     //}}AFX_DATA_INIT
  26.     m_UserMsg = RegisterWindowMessage(MY_DIALOGBOX_MSG);
  27. }
  28.  
  29. void CListWithItemDataDlg::DoDataExchange(CDataExchange* pDX)
  30. {
  31.     CDialog::DoDataExchange(pDX);
  32.     //{{AFX_DATA_MAP(CListWithItemDataDlg)
  33.     DDX_Text(pDX, IDC_PHRASE, m_phrase);
  34.     //}}AFX_DATA_MAP
  35.  
  36.     // 'Programmer-extended' DDV
  37.     if (pDX->m_bSaveAndValidate)
  38.         if (m_phrase.GetLength() == 0)
  39.         {
  40.             MessageBox("Phrase cannot be empty!");
  41.             DDX_Text(pDX, IDC_PHRASE, m_phrase);
  42.             pDX->Fail();
  43.         }
  44.  
  45.     // If you want the list box's ItemData field available
  46.     // in the dialog class, you've got to do the transfer
  47.     // yourself. For this particular variable (m_color)
  48.     // true DDX isn't appropriate. The variable m_color
  49.     // is used to set the initial list box selection in
  50.     // OnInitDialog.
  51.     if (pDX->m_bSaveAndValidate)
  52.     {
  53.         CListBox * clb = (CListBox *)GetDlgItem(IDC_COLOR_LIST);
  54.         m_color = clb->GetItemData(clb->GetCurSel());
  55.     }
  56. }
  57.  
  58. BEGIN_MESSAGE_MAP(CListWithItemDataDlg, CDialog)
  59.     //{{AFX_MSG_MAP(CListWithItemDataDlg)
  60.     ON_BN_CLICKED(IDC_APPLY, OnApply)
  61.     ON_LBN_SELCHANGE(IDC_COLOR_LIST, EnableApplyButton)
  62.     ON_EN_CHANGE(IDC_PHRASE, EnableApplyButton)
  63.     //}}AFX_MSG_MAP
  64. END_MESSAGE_MAP()
  65.  
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CListWithItemDataDlg message handlers
  68.  
  69. BOOL CListWithItemDataDlg::OnInitDialog() 
  70. {
  71.     CDialog::OnInitDialog();
  72.     
  73.     // Since no Listbox member exists in the dialog class
  74.     // we'll access the list box via a pointer obtained
  75.     // from GetDlgItem.
  76.     CListBox * clb = (CListBox *)GetDlgItem(IDC_COLOR_LIST);
  77.     
  78.     // In this dialog box, the string is for appearance
  79.     // only. The real value of the user's selection will be
  80.     // the 32-bit value that is stored via SetItemData.
  81.     // If there were more than 4 entries in the list box,
  82.     // this section of code could be improved with some
  83.     // arrays and a for loop.
  84.     clb->AddString("Black");
  85.     clb->SetItemData(0, BLACK);
  86.     clb->AddString("Red");
  87.     clb->SetItemData(1, RED);
  88.     clb->AddString("Green");
  89.     clb->SetItemData(2, GREEN);
  90.     clb->AddString("Blue");
  91.     clb->SetItemData(3, BLUE);
  92.     
  93.     // Set the initial selection in the list box.
  94.     clb->SetCurSel(RgbToInt(m_color));
  95.  
  96.     // Size the list box.
  97.     CRect rect;
  98.     int itemHeight;
  99.     clb->GetClientRect(&rect);
  100.     clb->MapWindowPoints(this, & rect);
  101.     itemHeight = clb->GetItemHeight(0) + 1;
  102.     rect.bottom = rect.top + 4 * itemHeight;
  103.     clb->MoveWindow(&rect);
  104.  
  105.     // Initially, the Apply button is disabled.
  106.     CButton * cb = (CButton *)GetDlgItem(IDC_APPLY);
  107.     cb->EnableWindow(FALSE);
  108.     
  109.     return TRUE;
  110. }
  111.  
  112. void CListWithItemDataDlg::OnApply() 
  113. {
  114.     UpdateData(TRUE);
  115.     m_pWnd->SendMessage(m_UserMsg, ML_APPLY);
  116.  
  117.     // Now that the Apply button's work is done,
  118.     // disable it.
  119.     CButton * cb = (CButton *)GetDlgItem(IDC_APPLY);
  120.     cb->EnableWindow(FALSE);
  121. }
  122.  
  123. void CListWithItemDataDlg::OnCancel() 
  124. {
  125.     m_pWnd->SendMessage(m_UserMsg, ML_CANCEL);
  126.     DestroyWindow();
  127. }
  128.  
  129. void CListWithItemDataDlg::PostNcDestroy() 
  130. {
  131.     delete this;
  132.     CDialog::PostNcDestroy();
  133. }
  134.  
  135. void CListWithItemDataDlg::EnableApplyButton() 
  136. {
  137.     // Enable the Apply button whenever a change is made in
  138.     // the edit box, or the selection changes in the list box.
  139.     // Note (from the message map) that this function is the
  140.     // handler for 2 separate events.
  141.     CButton * cb = (CButton *)GetDlgItem(IDC_APPLY);
  142.  
  143.     // Occasionally, at start up, cb may be null, so don't try
  144.     // to use it unless it's good.
  145.     if (cb)
  146.         cb->EnableWindow(TRUE);
  147. }
  148.