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

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MDI2Views.h"
  6.  
  7. #include "mdi2viewsdoc.h"
  8. #include "italicsview.h"
  9. #include "childfrm.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. /////////////////////////////////////////////////////////////////////////////
  20. // CMainFrame
  21.  
  22. IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
  23.  
  24. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  25.     //{{AFX_MSG_MAP(CMainFrame)
  26.     ON_WM_CREATE()
  27.     ON_COMMAND(ID_WINDOW_ITALICS, OnWindowItalics)
  28.     //}}AFX_MSG_MAP
  29. END_MESSAGE_MAP()
  30.  
  31. static UINT indicators[] =
  32. {
  33.     ID_SEPARATOR,           // status line indicator
  34.     ID_INDICATOR_CAPS,
  35.     ID_INDICATOR_NUM,
  36.     ID_INDICATOR_SCRL,
  37. };
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CMainFrame construction/destruction
  41.  
  42. CMainFrame::CMainFrame()
  43. {
  44.       m_pItalicsTemplate = 0;
  45. }
  46.  
  47. CMainFrame::~CMainFrame()
  48. {
  49.     if    (0 != m_pItalicsTemplate)
  50.     {
  51.         delete m_pItalicsTemplate;
  52.           m_pItalicsTemplate = 0;
  53.     }
  54. }
  55.  
  56. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  57. {
  58.     if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  59.         return -1;
  60.     
  61.     if (!m_wndToolBar.Create(this) ||
  62.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  63.     {
  64.         TRACE0("Failed to create toolbar\n");
  65.         return -1;      // fail to create
  66.     }
  67.  
  68.     if (!m_wndStatusBar.Create(this) ||
  69.         !m_wndStatusBar.SetIndicators(indicators,
  70.           sizeof(indicators)/sizeof(UINT)))
  71.     {
  72.         TRACE0("Failed to create status bar\n");
  73.         return -1;      // fail to create
  74.     }
  75.  
  76.     // TODO: Remove this if you don't want tool tips or a resizeable toolbar
  77.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  78.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  79.  
  80.     // TODO: Delete these three lines if you don't want the toolbar to
  81.     //  be dockable
  82.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  83.     EnableDocking(CBRS_ALIGN_ANY);
  84.     DockControlBar(&m_wndToolBar);
  85.  
  86.     return 0;
  87. }
  88.  
  89. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  90. {
  91.     return CMDIFrameWnd::PreCreateWindow(cs);
  92. }
  93.  
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CMainFrame diagnostics
  96. #ifdef _DEBUG
  97. void CMainFrame::AssertValid() const
  98. {
  99.     CMDIFrameWnd::AssertValid();
  100. }
  101.  
  102. void CMainFrame::Dump(CDumpContext& dc) const
  103. {
  104.     CMDIFrameWnd::Dump(dc);
  105. }
  106. #endif //_DEBUG
  107.  
  108. /////////////////////////////////////////////////////////////////////////////
  109. // CMainFrame message handlers
  110.  
  111. void CMainFrame::OnWindowItalics() 
  112. {
  113.     // The italics template is NOT added to the app's
  114.     // list of templates. Whenever the app's list contains
  115.     // more than 1 template, a list box is presented to the
  116.     // user when the app starts asking which type of view
  117.     // should be used.    By maintaining it myself, I easily
  118.     // avoid that situation. This other template is destroyed
  119.     // in the main frame's d'tor.
  120.     
  121.     // Don't create the second template until it's actually
  122.     // needed, and then only do it once.
  123.     if (0 == m_pItalicsTemplate)
  124.     {
  125.         m_pItalicsTemplate = new CMultiDocTemplate(
  126.             IDR_MDI2VITYPE,
  127.             RUNTIME_CLASS(CMDI2ViewsDoc),
  128.             RUNTIME_CLASS(CChildFrame),
  129.             RUNTIME_CLASS(CItalicsView));
  130.     }
  131.  
  132.     ASSERT (m_pItalicsTemplate != 0);
  133.  
  134.     // To begin, we'll need a pointer to the active document.
  135.     // This is obtained from the active child.
  136.     CMDIChildWnd* pActiveChild = MDIGetActive();
  137.     CDocument* pDocument;
  138.     if (NULL == pActiveChild ||
  139.       (pDocument = pActiveChild->GetActiveDocument()) == NULL)
  140.     {
  141.         TRACE0("Warning: No active document for WindowNew command.\n");
  142.         AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
  143.         return;     // command failed
  144.     }
  145.  
  146.     CFrameWnd* pFrame = m_pItalicsTemplate->
  147.                     CreateNewFrame(pDocument, pActiveChild);
  148.     if (NULL == pFrame)
  149.     {
  150.         TRACE0("Warning: failed to create new frame.\n");
  151.         return;     // command failed
  152.     }
  153.  
  154.     m_pItalicsTemplate->InitialUpdateFrame(pFrame, pDocument);
  155. }
  156.