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

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "tree.h"
  6.  
  7. #include "AnmlData.h"
  8.  
  9. #include "treeDoc.h"
  10. #include "TreeView.h"
  11. #include "ListView.h"
  12.  
  13. #include "MainFrm.h"
  14.  
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CMainFrame
  23.  
  24. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  25.  
  26. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  27.     //{{AFX_MSG_MAP(CMainFrame)
  28.     ON_WM_CREATE()
  29.     ON_COMMAND(ID_EDIT_ADDANIMAL, OnEditAddAnimal)
  30.     ON_COMMAND(ID_EDIT_MODIFYANIMAL, OnEditModifyAnimal)
  31.     ON_COMMAND(ID_EDIT_DELETEANIMAL, OnEditDeleteAnimal)
  32.     ON_UPDATE_COMMAND_UI(ID_EDIT_DELETEANIMAL, OnUpdateEditDeleteAnimal)
  33.     ON_UPDATE_COMMAND_UI(ID_EDIT_MODIFYANIMAL, OnUpdateEditModifyAnimal)
  34.     //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36.  
  37. static UINT indicators[] =
  38. {
  39.     ID_SEPARATOR,           // status line indicator
  40.     ID_INDICATOR_CAPS,
  41.     ID_INDICATOR_NUM,
  42.     ID_INDICATOR_SCRL,
  43. };
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CMainFrame construction/destruction
  47.  
  48. CMainFrame::CMainFrame()
  49. {
  50. }
  51.  
  52. CMainFrame::~CMainFrame()
  53. {
  54. }
  55.  
  56. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  57. {
  58.     if (CFrameWnd::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 CFrameWnd::PreCreateWindow(cs);
  92. }
  93.  
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CMainFrame diagnostics
  96.  
  97. #ifdef _DEBUG
  98. void CMainFrame::AssertValid() const
  99. {
  100.     CFrameWnd::AssertValid();
  101. }
  102.  
  103. void CMainFrame::Dump(CDumpContext& dc) const
  104. {
  105.     CFrameWnd::Dump(dc);
  106. }
  107.  
  108. #endif //_DEBUG
  109.  
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CMainFrame message handlers
  112.  
  113. BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
  114. {
  115.     // This is the code for a static splitter.
  116.  
  117.     // Create a splitter window with 1 row, 2 columns.
  118.     m_wndSplitter.CreateStatic(this, 1, 2);
  119.  
  120.     CRect cr;
  121.     GetClientRect(&cr);
  122.  
  123.     // The left pane holds the tree view.
  124.     int rc = m_wndSplitter.CreateView(0, 0,
  125.                                 RUNTIME_CLASS(CSimpleTreeView),
  126.                                 CSize(cr.Width() / 2, cr.Height()),
  127.                                 pContext);
  128.     if (FALSE == rc)
  129.         return rc;
  130.  
  131.     // The right pane holds the list view.
  132.     rc = m_wndSplitter.CreateView(0, 1,
  133.                                 RUNTIME_CLASS(CSimpleListView),
  134.                                 CSize(cr.Width() / 2, cr.Height()),
  135.                                 pContext);
  136.     return rc;
  137. }
  138.  
  139. // These 3 command handlers (and UI functions for 2 of them) are placed
  140. // in the frame class to make them available regardless of which view has
  141. // focus. The actual code that does the work is placed in the tree view class.
  142. void CMainFrame::OnEditAddAnimal() 
  143. {
  144.     CTreeDoc * pDoc = (CTreeDoc *) GetActiveDocument();
  145.     pDoc->m_pTreeView->EditSelection();
  146. }
  147.  
  148. void CMainFrame::OnEditModifyAnimal() 
  149. {
  150.     CTreeDoc * pDoc = (CTreeDoc *) GetActiveDocument();
  151.     pDoc->m_pTreeView->ModifySelection();
  152. }
  153.  
  154. void CMainFrame::OnEditDeleteAnimal() 
  155. {
  156.     CTreeDoc * pDoc = (CTreeDoc *) GetActiveDocument();
  157.     pDoc->m_pTreeView->DeleteSelection();
  158. }
  159.  
  160. // Only enable the delete menu if a specific animal is selected.
  161. // Do not enable if node at level 0 or level 1 is selected.
  162. void CMainFrame::OnUpdateEditDeleteAnimal(CCmdUI* pCmdUI) 
  163. {
  164.     CTreeDoc * pDoc = (CTreeDoc *) GetActiveDocument();
  165.     CTreeCtrl & ctc = pDoc->m_pTreeView->GetTreeCtrl();
  166.  
  167.     HTREEITEM selectedNode = ctc.GetSelectedItem();
  168.     if (selectedNode == NULL)
  169.         pCmdUI->Enable(FALSE);
  170.     else
  171.     {
  172.         DWORD itemData = ctc.GetItemData(selectedNode);
  173.         if (0 == itemData || -1 == itemData)
  174.             pCmdUI->Enable(FALSE);
  175.         else
  176.             pCmdUI->Enable(TRUE);
  177.     }
  178. }
  179.  
  180. // The modify menu is enabled if a level 1 node OR an animal is selected.
  181. // Do not enable if node at level 0 is selected.
  182. void CMainFrame::OnUpdateEditModifyAnimal(CCmdUI* pCmdUI) 
  183. {
  184.     CTreeDoc * pDoc = (CTreeDoc *) GetActiveDocument();
  185.     CTreeCtrl & ctc = pDoc->m_pTreeView->GetTreeCtrl();
  186.  
  187.     HTREEITEM selectedNode = ctc.GetSelectedItem();
  188.     if (selectedNode == NULL)
  189.         pCmdUI->Enable(FALSE);
  190.     else
  191.     {
  192.         DWORD itemData = ctc.GetItemData(selectedNode);
  193.         if (0 == itemData)
  194.             pCmdUI->Enable(FALSE);
  195.         else
  196.         {
  197.             pCmdUI->Enable(TRUE);
  198.             if (-1 == itemData)
  199.                 pCmdUI->SetText("&Modify Type");
  200.             else
  201.                 pCmdUI->SetText("&Modify Animal");
  202.         }
  203.     }
  204. }
  205.