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

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "SDI2Views.h"
  6.  
  7. #include "SDI2ViewsDoc.h"
  8. #include "ItalicsView.h"
  9. #include "DefaultView.h"
  10.  
  11. #include "MainFrm.h"
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. const COLORREF BLACK=RGB(0,0,0);
  20. const COLORREF RED=RGB(255,0,0);
  21. const COLORREF GREEN=RGB(0,255,0);
  22. const COLORREF BLUE=RGB(0,0,255);
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CMainFrame
  26.  
  27. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  28.  
  29. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  30.     //{{AFX_MSG_MAP(CMainFrame)
  31.     ON_WM_CREATE()
  32.     ON_COMMAND(ID_VIEW_DEFAULT, OnViewDefault)
  33.     ON_COMMAND(ID_VIEW_ITALICS, OnViewItalics)
  34.     ON_UPDATE_COMMAND_UI(ID_VIEW_DEFAULT, OnUpdateViewDefault)
  35.     ON_UPDATE_COMMAND_UI(ID_VIEW_ITALICS, OnUpdateViewItalics)
  36.     //}}AFX_MSG_MAP
  37.     ON_COMMAND_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnColors)
  38.     ON_UPDATE_COMMAND_UI_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnUpdateColors)
  39. END_MESSAGE_MAP()
  40.  
  41. static UINT indicators[] =
  42. {
  43.     ID_SEPARATOR,           // status line indicator
  44.     ID_INDICATOR_CAPS,
  45.     ID_INDICATOR_NUM,
  46.     ID_INDICATOR_SCRL,
  47. };
  48.  
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CMainFrame construction/destruction
  51.  
  52. CMainFrame::CMainFrame()
  53. {
  54.     // To give this SDI app 2 views, the main frame stores
  55.     // pointers to them both. 
  56.     m_pItalicsView = 0;
  57.     m_pDefaultView = 0;
  58. }
  59.  
  60. CMainFrame::~CMainFrame()
  61. {
  62.     if (0 == m_pItalicsView)
  63.     {
  64.         delete m_pItalicsView;
  65.         m_pItalicsView = 0;
  66.     }
  67. }
  68.  
  69. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  70. {
  71.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  72.         return -1;
  73.     
  74.     if (!m_wndToolBar.Create(this) ||
  75.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  76.     {
  77.         TRACE0("Failed to create toolbar\n");
  78.         return -1;      // fail to create
  79.     }
  80.  
  81.     if (!m_wndStatusBar.Create(this) ||
  82.         !m_wndStatusBar.SetIndicators(indicators,
  83.           sizeof(indicators)/sizeof(UINT)))
  84.     {
  85.         TRACE0("Failed to create status bar\n");
  86.         return -1;      // fail to create
  87.     }
  88.  
  89.     // TODO: Remove this if you don't want tool tips or a resizeable toolbar
  90.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  91.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  92.  
  93.     // TODO: Delete these three lines if you don't want the toolbar to
  94.     //  be dockable
  95.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  96.     EnableDocking(CBRS_ALIGN_ANY);
  97.     DockControlBar(&m_wndToolBar);
  98.  
  99.     return 0;
  100. }
  101.  
  102. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  103. {
  104.     return CFrameWnd::PreCreateWindow(cs);
  105. }
  106.  
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CMainFrame diagnostics
  109.  
  110. #ifdef _DEBUG
  111. void CMainFrame::AssertValid() const
  112. {
  113.     CFrameWnd::AssertValid();
  114. }
  115.  
  116. void CMainFrame::Dump(CDumpContext& dc) const
  117. {
  118.     CFrameWnd::Dump(dc);
  119. }
  120.  
  121. #endif //_DEBUG
  122.  
  123. /////////////////////////////////////////////////////////////////////////////
  124. // CMainFrame message handlers
  125.  
  126. void CMainFrame::OnViewDefault() 
  127. {
  128.     CDocument* pDoc = GetActiveDocument();
  129.  
  130.     // The UI handler will prevent this function executing before
  131.     // OnViewItalics, but just in case...
  132.     ASSERT (m_pItalicsView != 0);
  133.     ASSERT (m_pDefaultView != 0);
  134.  
  135.     pDoc->AddView(m_pDefaultView);
  136.  
  137.     // Show the default view and hide the italics view.
  138.     m_pDefaultView->ShowWindow(SW_SHOW);
  139.     m_pItalicsView->ShowWindow(SW_HIDE);
  140.  
  141.     // Swap IDs. See comment in next function.
  142.     m_pDefaultView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
  143.     m_pItalicsView->SetDlgCtrlID(AFX_IDW_PANE_FIRST + 1);
  144.     
  145.     SetActiveView(m_pDefaultView);
  146.     
  147.     // The program will be a bit more efficient if the
  148.     // unused view is removed. This way it won't receive
  149.     // update messages whenever CDocument::UpdateAllViews is called.
  150.     pDoc->RemoveView(m_pItalicsView);
  151.     RecalcLayout();
  152. }
  153.  
  154. void CMainFrame::OnViewItalics() 
  155. {
  156.     CDocument* pDoc = GetActiveDocument();
  157.     
  158.     // Only need to do this one time.
  159.     if (0 == m_pItalicsView)
  160.     {
  161.         m_pDefaultView = (CDefaultView *)GetActiveView();
  162.         // This object is destroyed in main frame's d'tor.
  163.         m_pItalicsView = new CItalicsView(NULL);
  164.  
  165.         m_pItalicsView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
  166.                 rectDefault, this, AFX_IDW_PANE_FIRST + 1);
  167.     }
  168.  
  169.     pDoc->AddView(m_pItalicsView);
  170.  
  171.     // Set the child i.d. of the italics view to AFX_IDW_PANE_FIRST,
  172.     // so that CFrameWnd::RecalcLayout will allocate to this 
  173.     // "first pane" that portion of the frame window's client area 
  174.     // not allocated to control bars.  Set the child i.d. of the 
  175.     // default view to AFX_IDW_PANE_FIRST + 1
  176.     m_pItalicsView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
  177.     m_pDefaultView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1);
  178.     
  179.     // Show the italics view and hide the default view.
  180.     m_pItalicsView->ShowWindow(SW_SHOW);
  181.     m_pDefaultView->ShowWindow(SW_HIDE);
  182.     
  183.     SetActiveView(m_pItalicsView);
  184.  
  185.     // See comment in OnViewDefault.
  186.     pDoc->RemoveView(m_pDefaultView);
  187.     RecalcLayout();
  188. }
  189.  
  190. void CMainFrame::OnUpdateViewDefault(CCmdUI* pCmdUI) 
  191. {
  192.     // Only enable the default view menu if the current view
  193.     // is the italics view.
  194.     pCmdUI->Enable(
  195.         GetActiveView()->IsKindOf(RUNTIME_CLASS(CItalicsView)));
  196. }
  197.  
  198. void CMainFrame::OnUpdateViewItalics(CCmdUI* pCmdUI) 
  199. {
  200.     // Only enable the italics view menu if the current view
  201.     // is the default view.
  202.     pCmdUI->Enable(
  203.         GetActiveView()->IsKindOf(RUNTIME_CLASS(CDefaultView)));
  204. }
  205.  
  206. void CMainFrame::OnColors(UINT nID)
  207. {
  208.     CSDI2ViewsDoc* pDoc = (CSDI2ViewsDoc*)GetActiveDocument();
  209.     ASSERT_VALID(pDoc);
  210.  
  211.     pDoc->SetColor(IDtoColorRef(nID));
  212.     Invalidate();
  213. }
  214.  
  215. void CMainFrame::OnUpdateColors(CCmdUI* pCmdUI)
  216. {
  217.     CSDI2ViewsDoc* pDoc = (CSDI2ViewsDoc*)GetActiveDocument();
  218.     ASSERT_VALID(pDoc);
  219.  
  220.     pCmdUI->SetCheck(pDoc->GetColor() == IDtoColorRef(pCmdUI->m_nID));
  221. }
  222.  
  223. // This function converts one of the 4 Command IDs to a COLORREF value.
  224. COLORREF CMainFrame::IDtoColorRef(int nID)
  225. {
  226.     switch (nID)
  227.     {
  228.         case ID_COLORS_RED:
  229.             return RED;
  230.         case ID_COLORS_GREEN:
  231.             return GREEN;        
  232.         case ID_COLORS_BLUE:
  233.             return BLUE;
  234.         default:
  235.             return BLACK;        
  236.     }
  237. }
  238.