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

  1. // Dialogs.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "TmpGraph.h"
  6. #include "Dialogs.h"
  7.  
  8. #include "TGrafDoc.h"
  9. #include "MainFrm.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CTemperatureDlg dialog
  19.  
  20. CTemperatureDlg::CTemperatureDlg(CWnd * pWnd, CWnd* pParent /*=NULL*/)
  21.     : CDialog(CTemperatureDlg::IDD, pParent)
  22. {
  23.     //{{AFX_DATA_INIT(CTemperatureDlg)
  24.     m_temperature = 0;
  25.     m_day = 0;
  26.     //}}AFX_DATA_INIT
  27.     m_pWnd = pWnd;
  28.     m_UserMsg = RegisterWindowMessage(MY_TEMP_MSG);
  29. }
  30.  
  31. void CTemperatureDlg::DoDataExchange(CDataExchange* pDX)
  32. {
  33.     CDialog::DoDataExchange(pDX);
  34.     //{{AFX_DATA_MAP(CTemperatureDlg)
  35.     DDX_Control(pDX, IDC_TEMP_SPINNER, m_temp_spinner);
  36.     DDX_Control(pDX, IDC_DAY_SPINNER, m_day_spinner);
  37.     DDX_Text(pDX, IDC_TEMPERATURE, m_temperature);
  38.     DDV_MinMaxInt(pDX, m_temperature, -10, 40);
  39.     DDX_Text(pDX, IDC_DAY, m_day);
  40.     DDV_MinMaxInt(pDX, m_day, 1, 31);
  41.     //}}AFX_DATA_MAP
  42.  
  43.     // Only proceed if the day is in range. Doing otherwise
  44.     // causes an access violation in the recipient of this message.
  45.     if (m_day < 1 || m_day > m_MaxDays)
  46.     {
  47.         AfxMessageBox("Day is out of range");
  48.         DDX_Text(pDX, IDC_DAY, m_day);
  49.         pDX->Fail();
  50.     }
  51. }
  52.  
  53. BEGIN_MESSAGE_MAP(CTemperatureDlg, CDialog)
  54.     //{{AFX_MSG_MAP(CTemperatureDlg)
  55.     ON_BN_CLICKED(IDC_APPLY, OnApply)
  56.     ON_NOTIFY(UDN_DELTAPOS, IDC_DAY_SPINNER, OnDeltaposDaySpinner)
  57.     ON_EN_CHANGE(IDC_TEMPERATURE, OnChangeTemperature)
  58.     //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CTemperatureDlg message handlers
  63.  
  64. BOOL CTemperatureDlg::OnInitDialog() 
  65. {
  66.     CDialog::OnInitDialog();
  67.     
  68.     m_day_spinner.SetRange(1, 31);
  69.     m_temp_spinner.SetRange(-10, 40);
  70.  
  71.     // Initially, the Apply button is disabled.
  72.     CButton * cb = (CButton *)GetDlgItem(IDC_APPLY);
  73.     cb->EnableWindow(FALSE);
  74.  
  75.     return TRUE;
  76. }
  77.  
  78. void CTemperatureDlg::OnApply() 
  79. {
  80.     UpdateData(TRUE);
  81.     
  82.     // Only send the apply message and disable the apply button
  83.     // if the m_day is in range.
  84.     if (m_day >=1 && m_day <= m_MaxDays)
  85.     {
  86.         m_pWnd->SendMessage(m_UserMsg, ML_APPLY);
  87.         CButton * cb = (CButton *)GetDlgItem(IDC_APPLY);
  88.         cb->EnableWindow(FALSE);
  89.     }
  90. }
  91.  
  92. void CTemperatureDlg::OnCancel() 
  93. {
  94.     m_pWnd->SendMessage(m_UserMsg, ML_CANCEL);
  95.     DestroyWindow();
  96. }
  97.  
  98. void CTemperatureDlg::PostNcDestroy() 
  99. {
  100.     delete this;
  101.     CDialog::PostNcDestroy();
  102. }
  103.  
  104. void CTemperatureDlg::OnDeltaposDaySpinner(NMHDR* pNMHDR, LRESULT* pResult) 
  105. {
  106.     NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
  107.  
  108.     CTemperatureGraphDoc * pDoc;
  109.     CMainFrame * pFrame;
  110.     int newValue;
  111.  
  112.     pFrame = (CMainFrame *) m_pWnd;
  113.     pDoc = (CTemperatureGraphDoc *)pFrame->GetActiveDocument();
  114.  
  115.     newValue = pNMUpDown->iPos + pNMUpDown->iDelta;
  116.     
  117.     if (newValue < 1 || newValue > m_MaxDays)
  118.     {
  119.         *pResult = TRUE;
  120.         return;
  121.     }
  122.  
  123.     m_day = newValue;
  124.     m_temperature = pDoc->temps[m_day-1].y;
  125.     UpdateData(FALSE);
  126.     
  127.     *pResult = FALSE;
  128. }
  129.  
  130. void CTemperatureDlg::OnChangeTemperature() 
  131. {
  132.     // When temperature changes enable the Apply button.
  133.     CButton * cb = (CButton *)GetDlgItem(IDC_APPLY);
  134.     
  135.     // This function may be called too early in the life of the
  136.     // dialog box, so do nothing if the control doesn't exist yet.
  137.     if (cb)
  138.         cb->EnableWindow(TRUE);
  139. }
  140.