home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / collect / mainfrm.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  5KB  |  206 lines

  1. // mainfrm.cpp : implementation of the CMainFrame class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "collect.h"
  15.  
  16. #include "mainfrm.h"
  17.  
  18. #include "colledoc.h"
  19. #include "strlstvw.h"
  20. #include "typlstvw.h"
  21. #include "intlstvw.h"
  22. #include "dwarryvw.h"
  23. #include "typaryvw.h"
  24. #include "ptarryvw.h"
  25. #include "mapssvw.h"
  26. #include "typtrmap.h"
  27. #include "mapdwvw.h"
  28.  
  29. #ifdef _DEBUG
  30. #undef THIS_FILE
  31. static char BASED_CODE THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CMainFrame
  36.  
  37. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  38.  
  39. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  40.     ON_COMMAND_RANGE(ID_STRINGLIST, ID_MAP, OnExample)
  41.     ON_UPDATE_COMMAND_UI_RANGE(ID_STRINGLIST, ID_MAP, OnUpdateExampleUI)
  42.     //{{AFX_MSG_MAP(CMainFrame)
  43.     ON_WM_CREATE()
  44.     //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // arrays of IDs used to initialize control bars
  49.  
  50. // toolbar buttons - IDs are command buttons
  51. static UINT BASED_CODE buttons[] =
  52. {
  53.     // same order as in the bitmap 'toolbar.bmp'
  54.     ID_FILE_NEW,
  55.     ID_FILE_OPEN,
  56.     ID_FILE_SAVE,
  57.         ID_SEPARATOR,
  58.     ID_EDIT_CUT,
  59.     ID_EDIT_COPY,
  60.     ID_EDIT_PASTE,
  61.         ID_SEPARATOR,
  62.     ID_FILE_PRINT,
  63.     ID_APP_ABOUT,
  64. };
  65.  
  66. static UINT BASED_CODE indicators[] =
  67. {
  68.     ID_SEPARATOR,           // status line indicator
  69.     ID_INDICATOR_CAPS,
  70.     ID_INDICATOR_NUM,
  71.     ID_INDICATOR_SCRL,
  72. };
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CMainFrame construction/destruction
  76.  
  77. CMainFrame::CMainFrame()
  78. {
  79.     m_nCurrentExample = ID_STRINGLIST;
  80. }
  81.  
  82. CMainFrame::~CMainFrame()
  83. {
  84. }
  85.  
  86. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  87. {
  88.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  89.         return -1;
  90.  
  91.     if (!m_wndToolBar.Create(this) ||
  92.         !m_wndToolBar.LoadBitmap(IDR_MAINFRAME) ||
  93.         !m_wndToolBar.SetButtons(buttons,
  94.           sizeof(buttons)/sizeof(UINT)))
  95.     {
  96.         TRACE0("Failed to create toolbar\n");
  97.         return -1;      // fail to create
  98.     }
  99.  
  100.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  101.     EnableDocking(CBRS_ALIGN_ANY);
  102.     DockControlBar(&m_wndToolBar);
  103.  
  104.     if (!m_wndStatusBar.Create(this) ||
  105.         !m_wndStatusBar.SetIndicators(indicators,
  106.           sizeof(indicators)/sizeof(UINT)))
  107.     {
  108.         TRACE0("Failed to create status bar\n");
  109.         return -1;      // fail to create
  110.     }
  111.  
  112.     return 0;
  113. }
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CMainFrame diagnostics
  117.  
  118. #ifdef _DEBUG
  119. void CMainFrame::AssertValid() const
  120. {
  121.     CFrameWnd::AssertValid();
  122. }
  123.  
  124. void CMainFrame::Dump(CDumpContext& dc) const
  125. {
  126.     CFrameWnd::Dump(dc);
  127. }
  128.  
  129. #endif //_DEBUG
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CMainFrame message handlers
  133.  
  134. void CMainFrame::OnExample(UINT nCmdID)
  135. {
  136.     if (nCmdID == m_nCurrentExample)
  137.         return;  // already selected
  138.  
  139.     // Set the child window ID of the active view to AFX_IDW_PANE_FIRST.
  140.     // This is necessary so that CFrameWnd::RecalcLayout will allocate
  141.     // this "first pane" to that portion of the frame window's client
  142.     // area not allocated to control bars.  Set the child ID of
  143.     // the previously active view to some other ID; we will use the
  144.     // command ID as the child ID.
  145.     CView* pOldActiveView = GetActiveView();
  146.         ::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, m_nCurrentExample);
  147.  
  148.     CRuntimeClass* pNewViewClass;
  149.     switch (nCmdID)
  150.     {
  151.         case ID_STRINGLIST:
  152.             pNewViewClass = RUNTIME_CLASS(CStringListView);
  153.             break;
  154.         case ID_TYPEDLIST:
  155.             pNewViewClass = RUNTIME_CLASS(CTypedPtrListView);
  156.             break;
  157.         case ID_INTLIST:
  158.             pNewViewClass = RUNTIME_CLASS(CIntListView);
  159.             break;
  160.         case ID_DWORDARRAY:
  161.             pNewViewClass = RUNTIME_CLASS(CDWordArrayView);
  162.             break;
  163.         case ID_TYPEDPTRARRAY:
  164.             pNewViewClass = RUNTIME_CLASS(CTypedPtrArrayView);
  165.             break;
  166.         case ID_POINTARRAY:
  167.             pNewViewClass = RUNTIME_CLASS(CPointArrayView);
  168.             break;
  169.         case ID_MAPSTRINGTOSTRING:
  170.             pNewViewClass = RUNTIME_CLASS(CMapStringToStringView);
  171.             break;
  172.         case ID_TYPEDPTRMAP:
  173.             pNewViewClass = RUNTIME_CLASS(CTypedPtrMapView);
  174.             break;
  175.         case ID_MAPDWORDTOMYSTRUCT:
  176.             pNewViewClass = RUNTIME_CLASS(CMapDWordToMyStructView);
  177.             break;
  178.         default:
  179.             ASSERT(0);
  180.             return;
  181.     }
  182.  
  183.     // create the new view
  184.     CCreateContext context;
  185.     context.m_pNewViewClass = pNewViewClass;
  186.     context.m_pCurrentDoc = GetActiveDocument();
  187.     CView* pNewView = STATIC_DOWNCAST(CView, CreateView(&context));
  188.     if (pNewView != NULL)
  189.     {
  190.         // the new view is there, but invisible and not active...
  191.         pNewView->ShowWindow(SW_SHOW);
  192.         pNewView->OnInitialUpdate();
  193.         SetActiveView(pNewView);
  194.         RecalcLayout();
  195.         m_nCurrentExample = nCmdID;
  196.  
  197.         // finally destroy the old view...
  198.         pOldActiveView->DestroyWindow();
  199.     }
  200. }
  201.  
  202. void CMainFrame::OnUpdateExampleUI(CCmdUI* pCmdUI)
  203. {
  204.     pCmdUI->SetCheck(pCmdUI->m_nID == m_nCurrentExample);
  205. }
  206.