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

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "splitter.h"
  6.  
  7. #include "MainFrm.h"
  8.  
  9. #include "SplitDoc.h"
  10. #include "SpltView.h"
  11. #include "ItalView.h"
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CMainFrame
  21.  
  22. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  23.  
  24. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  25.     //{{AFX_MSG_MAP(CMainFrame)
  26.     //}}AFX_MSG_MAP
  27.     ON_COMMAND_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnColors)
  28.     ON_UPDATE_COMMAND_UI_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnUpdateColors)
  29. END_MESSAGE_MAP()
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CMainFrame construction/destruction
  33.  
  34. CMainFrame::CMainFrame()
  35. {
  36. }
  37.  
  38. CMainFrame::~CMainFrame()
  39. {
  40. }
  41.  
  42. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  43. {
  44.     // TODO: Modify the Window class or styles here by modifying
  45.     //  the CREATESTRUCT cs
  46.  
  47.     return CFrameWnd::PreCreateWindow(cs);
  48. }
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CMainFrame diagnostics
  52.  
  53. #ifdef _DEBUG
  54. void CMainFrame::AssertValid() const
  55. {
  56.     CFrameWnd::AssertValid();
  57. }
  58.  
  59. void CMainFrame::Dump(CDumpContext& dc) const
  60. {
  61.     CFrameWnd::Dump(dc);
  62. }
  63.  
  64. #endif //_DEBUG
  65.  
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CMainFrame message handlers
  68.  
  69. BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
  70. {
  71. #if 0
  72.     // This is the code for a dynamic splitter.
  73.     CSize minWindow(10, 10);
  74.     
  75.     // These variables must both be 1 or 2 for dynamic splitters.
  76.     int nRows = 2, nColumns = 2;
  77.     
  78.     return m_wndSplitter.Create(this, nRows, nColumns,
  79.                                 minWindow, pContext);
  80. #endif
  81.  
  82. #if 1
  83.     // This is the code for a static splitter.
  84.     CRect cr;
  85.     GetClientRect(&cr);
  86.  
  87.     CSize paneSize(cr.Width(), cr.Height() / 2);
  88.  
  89.     int rc;
  90.     m_wndSplitter.CreateStatic(this, 2, 1);
  91.  
  92.     rc = m_wndSplitter.CreateView(0, 0,
  93.                                 RUNTIME_CLASS(CSplitterView),
  94.                                 paneSize, pContext);
  95.  
  96.     // The view that is associated with the document when the template
  97.     // is created can also be specified this way.
  98.     // rc = m_wndSplitter.CreateView(0, 0,
  99.     //                             pContext->m_pNewViewClass,
  100.     //                             paneSize, pContext);
  101.  
  102.     if (FALSE == rc)
  103.         return rc;
  104.     
  105.     rc = m_wndSplitter.CreateView(1, 0,
  106.                                 RUNTIME_CLASS(CItalicsView),
  107.                                 paneSize, pContext);
  108.     return rc;
  109. #endif
  110. }
  111.  
  112. const COLORREF BLACK=RGB(0,0,0);
  113. const COLORREF RED=RGB(255,0,0);
  114. const COLORREF GREEN=RGB(0,255,0);
  115. const COLORREF BLUE=RGB(0,0,255);
  116. const COLORREF WHITE=RGB(255,255,255);
  117.  
  118. // This function converts one of the 4 Command IDs to a COLORREF value.
  119. COLORREF CMainFrame::IDtoColorRef(int nID)
  120. {
  121.     switch (nID)
  122.     {
  123.         case ID_COLORS_RED:
  124.             return RED;
  125.         case ID_COLORS_GREEN:
  126.             return GREEN;        
  127.         case ID_COLORS_BLUE:
  128.             return BLUE;
  129.         default:
  130.             return BLACK;        
  131.     }
  132. }
  133.  
  134. // In an application with multiple views displayed, as in this app's
  135. // 2 panes, it's necessary to place command handlers in the frame class
  136. // so their associated menus are available regardless of which pane
  137. // currently has the focus.
  138. void CMainFrame::OnColors(UINT nID)
  139. {
  140.     CSplitterDoc* pDoc = (CSplitterDoc*) GetActiveDocument();
  141.     ASSERT_VALID(pDoc);
  142.  
  143.     pDoc->SetColor(IDtoColorRef(nID));
  144.     // When a single view is involved, we'd call
  145.     //      Invalidate();
  146.     // but when there's more than 1 view, call
  147.     pDoc->UpdateAllViews(NULL);
  148. }
  149.  
  150. void CMainFrame::OnUpdateColors(CCmdUI* pCmdUI)
  151. {
  152.     CSplitterDoc* pDoc = (CSplitterDoc*) GetActiveDocument();
  153.     ASSERT_VALID(pDoc);
  154.  
  155.     pCmdUI->SetCheck(pDoc->GetColor() == IDtoColorRef(pCmdUI->m_nID));
  156. }
  157.  
  158.