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

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "TmpGraph.h"
  6.  
  7. #include "TGrafDoc.h"
  8. #include "GrafView.h"
  9. #include "NumbView.h"
  10. #include "Dialogs.h"
  11.  
  12. #include "MainFrm.h"
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. UINT reg_msg = RegisterWindowMessage(MY_TEMP_MSG);
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CMainFrame
  24.  
  25. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  26.  
  27. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  28.     //{{AFX_MSG_MAP(CMainFrame)
  29.     ON_COMMAND(ID_CHANGE_SETDAILYTEMP, OnChangeSetdailytemp)
  30.     //}}AFX_MSG_MAP
  31.     ON_REGISTERED_MESSAGE(reg_msg, MyTemperatureHandler)
  32.     ON_COMMAND_RANGE(ID_MONTH_JANUARY, ID_MONTH_LEAPFEBRUARY, OnMonth)
  33. END_MESSAGE_MAP()
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CMainFrame construction/destruction
  37. void CMainFrame::OnMonth(UINT nID)
  38. {
  39.     CTemperatureGraphDoc * pDoc = (CTemperatureGraphDoc *)GetActiveDocument();
  40.     int daycount, formersize;
  41.  
  42.     formersize = pDoc->temps.GetSize();
  43.  
  44.     switch (nID)
  45.     {
  46.         case ID_MONTH_JANUARY:
  47.         case ID_MONTH_MARCH:
  48.         case ID_MONTH_MAY:
  49.         case ID_MONTH_JULY:
  50.         case ID_MONTH_AUGUST:
  51.         case ID_MONTH_OCTOBER:
  52.         case ID_MONTH_DECEMBER:
  53.             daycount = 31;
  54.             break;
  55.         case ID_MONTH_FEBRUARY:
  56.             daycount = 28;
  57.             break;
  58.         case ID_MONTH_APRIL:
  59.         case ID_MONTH_JUNE:
  60.         case ID_MONTH_SEPTEMBER:
  61.         case ID_MONTH_NOVEMBER:
  62.             daycount = 30;
  63.             break;
  64.         default:    // Leap year February
  65.             daycount = 29;
  66.     }
  67.     pDoc->temps.SetSize(daycount);
  68.  
  69.     // If the array has been extended, then the newly appended
  70.     // elements will have to be initialized.
  71.     if (formersize < daycount)
  72.         while (formersize <= daycount)
  73.         {
  74.             pDoc->temps[formersize - 1].x = formersize - 1;
  75.             pDoc->temps[formersize - 1].y = DEFAULT_TEMPERATURE;
  76.             formersize++;
  77.         }
  78.  
  79.     // convert nID to a subscript so as to load a string
  80.     // from the string table, where the months have consecutive
  81.     // IDs of IDS_JANUARY..IDS_LEAP_FEBRUARY
  82.     CString s;
  83.     s.LoadString(IDS_JANUARY + (nID-ID_MONTH_JANUARY));
  84.     
  85.     pDoc->m_month = s;
  86.     pDoc->UpdateAllViews(NULL);
  87.     pDoc->SetModifiedFlag(TRUE);
  88. }
  89.  
  90. LRESULT CMainFrame::MyTemperatureHandler(WPARAM wParam, LPARAM lParam)
  91. {
  92.     CTemperatureGraphDoc * pDoc;
  93.     pDoc = (CTemperatureGraphDoc *)GetActiveDocument();
  94.  
  95.     switch (wParam)
  96.     {
  97.         case ML_APPLY:
  98.             // If the temperature for the given day has changed,
  99.             // capture the variable and redraw the views.
  100.             if (m_pDlg->m_temperature != pDoc->temps[m_pDlg->m_day-1].y)
  101.             {
  102.                  pDoc->temps[m_pDlg->m_day-1].y = m_pDlg->m_temperature;
  103.                  pDoc->UpdateAllViews(NULL);
  104.                  pDoc->SetModifiedFlag(TRUE);
  105.             }
  106.             break;
  107.         case ML_CANCEL:
  108.             m_pDlg = 0;
  109.     }
  110.     return TRUE;
  111. }
  112.  
  113. CMainFrame::CMainFrame()
  114. {
  115.     m_pDlg = 0;
  116. }
  117.  
  118. CMainFrame::~CMainFrame()
  119. {
  120. }
  121.  
  122. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  123. {
  124.     return CFrameWnd::PreCreateWindow(cs);
  125. }
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CMainFrame diagnostics
  129.  
  130. #ifdef _DEBUG
  131. void CMainFrame::AssertValid() const
  132. {
  133.     CFrameWnd::AssertValid();
  134. }
  135.  
  136. void CMainFrame::Dump(CDumpContext& dc) const
  137. {
  138.     CFrameWnd::Dump(dc);
  139. }
  140.  
  141. #endif //_DEBUG
  142.  
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CMainFrame message handlers
  145.  
  146. BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
  147. {
  148.     // This is the code for a static splitter.
  149.     CRect cr;
  150.     GetClientRect(&cr);
  151.  
  152.     int rc;
  153.     m_wndSplitter.CreateStatic(this, 2, 1);
  154.     
  155.     // The top pane gets the default view
  156.     rc = m_wndSplitter.CreateView(0, 0,
  157.                                 pContext->m_pNewViewClass,
  158.                                 CSize(cr.Width(), cr.Height() / 2),
  159.                                 pContext);
  160.     if (FALSE == rc)
  161.         return rc;
  162.  
  163.     // The bottom pane gets the additional view.
  164.     rc = m_wndSplitter.CreateView(1, 0,
  165.                                 RUNTIME_CLASS(CTemperatureNumbersView),
  166.                                 CSize(cr.Width(), cr.Height() / 2),
  167.                                 pContext);
  168.     
  169.     return rc;
  170. }
  171.  
  172. // In an SDI app with a splitter with 2 different views, you have to
  173. // put command handlers in the frame class to make them available
  174. // regardless of which pane has focus.
  175. // If you put it in a view class, then that menu is not available when
  176. // the other view is active.
  177. void CMainFrame::OnChangeSetdailytemp() 
  178. {
  179.     CTemperatureGraphDoc * pDoc = 
  180.         (CTemperatureGraphDoc *)GetActiveDocument();
  181.     
  182.     // Create the dialog object.
  183.     m_pDlg = new CTemperatureDlg(this);
  184.     
  185.     // Initialize members of the dialog class.
  186.     m_pDlg->m_day = 1;
  187.     m_pDlg->m_temperature = pDoc->temps[0].y;
  188.     m_pDlg->m_MaxDays = pDoc->temps.GetSize();
  189.  
  190.     m_pDlg->Create(IDD_TEMPERATURE);
  191.  
  192.     m_pDlg->ShowWindow(SW_RESTORE);
  193. }
  194.