home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / npp / mainfrm.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  248 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.  
  14. #include "stdafx.h"
  15. #include "np.h"
  16. #include "combobar.h"
  17. #include "mainfrm.h"
  18. #include "npdoc.h"
  19. #include "npview.h"
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CMainFrame
  28.  
  29. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  30.  
  31. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  32.     //{{AFX_MSG_MAP(CMainFrame)
  33.     ON_WM_CREATE()
  34.     ON_WM_CLOSE()
  35.     ON_COMMAND(ID_VIEW_STATUS_BAR, OnViewStatusBar)
  36.     ON_COMMAND(ID_EDIT_FIND_COMBO, OnEditFindCombo)
  37.     //}}AFX_MSG_MAP
  38.     ON_UPDATE_COMMAND_UI(ID_VIEW_STATUS_BAR, OnUpdateViewStatusBar)
  39.     ON_UPDATE_COMMAND_UI(ID_INDICATOR_LINE, OnUpdateLineNumber)
  40.     ON_UPDATE_COMMAND_UI(ID_INDICATOR_CLOCK, OnUpdateClock)
  41. END_MESSAGE_MAP()
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // arrays of IDs used to initialize control bars
  45.  
  46. // toolbar buttons - IDs are command buttons
  47. static UINT BASED_CODE buttons[] =
  48. {
  49.     // same order as in the bitmap 'toolbar.bmp'
  50.     ID_FILE_NEW,
  51.     ID_FILE_OPEN,
  52.     ID_FILE_SAVE,
  53.         ID_SEPARATOR,
  54.     ID_EDIT_CUT,
  55.     ID_EDIT_COPY,
  56.     ID_EDIT_PASTE,
  57.         ID_SEPARATOR,
  58.         ID_SEPARATOR, //(8) CComboBox location in the toolbar
  59.         ID_SEPARATOR,
  60.     ID_FILE_PRINT,
  61.     ID_APP_ABOUT,
  62. };
  63.  
  64. static UINT BASED_CODE indicators[] =
  65. {
  66.     ID_SEPARATOR,           // status line indicator
  67.     ID_INDICATOR_LINE,
  68.     ID_INDICATOR_CAPS,
  69.     ID_INDICATOR_NUM,
  70.     ID_INDICATOR_SCRL,
  71.     ID_INDICATOR_CLOCK,
  72. };
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CMainFrame construction/destruction
  76.  
  77. CMainFrame::CMainFrame()
  78. {
  79. }
  80.  
  81. CMainFrame::~CMainFrame()
  82. {
  83. }
  84.  
  85. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  86. {
  87.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  88.         return -1;
  89.  
  90.     if (!m_wndToolBar.Create(this) ||
  91.         !m_wndToolBar.LoadBitmap(IDR_MAINFRAME) ||
  92.         !m_wndToolBar.SetButtons(buttons,
  93.           sizeof(buttons)/sizeof(UINT)))
  94.     {
  95.         TRACE0("Failed to create toolbar\n");
  96.         return -1;      // fail to create
  97.     }
  98.  
  99.     if (!CreateComboBox())
  100.     {
  101.         TRACE0("Failed to create combobox in toolbar\n");
  102.         return -1;      // fail to create
  103.     }
  104.  
  105.     if (!m_wndStatusBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM , AFX_IDW_STATUS_BAR) ||
  106.         !m_wndStatusBar.SetIndicators(indicators,
  107.           sizeof(indicators)/sizeof(UINT)))
  108.     {
  109.         TRACE0("Failed to create status bar\n");
  110.         return -1;      // fail to create
  111.     }
  112.  
  113.     m_wndToolBar.EnableDocking(CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM);
  114.     EnableDocking(CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM);
  115.     DockControlBar(&m_wndToolBar);
  116.  
  117.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  118.         CBRS_TOOLTIPS | CBRS_FLYBY);
  119.  
  120.     // create the timer to update the status bar clock
  121.     if(!SetTimer(TIMER_ID, 2000, &CMainFrame::UpdateTime))
  122.         return -1;
  123.  
  124.     CMainFrame::UpdateTime(m_hWnd, 0, 0, 0); // update the time now
  125.  
  126.     return 0;
  127. }
  128.  
  129. int CMainFrame::CreateComboBox()
  130. {
  131.     CRect rect, comboRect;
  132.     m_wndToolBar.SetButtonInfo(8, IDW_COMBO, TBBS_SEPARATOR, COMBO_WIDTH);
  133.     m_wndToolBar.GetItemRect(8, &rect);
  134.     rect.bottom += COMBO_HEIGHT;
  135.  
  136.     if (!m_wndToolBar.m_toolBarCombo.Create(CBS_DROPDOWN|WS_VISIBLE|WS_TABSTOP|CBS_AUTOHSCROLL,
  137.             rect, &m_wndToolBar, IDW_COMBO))
  138.     {
  139.         return FALSE;
  140.     }
  141.  
  142.     // center combo box edit control vertically within tool bar
  143.     rect.bottom -= COMBO_HEIGHT;
  144.     m_wndToolBar.m_toolBarCombo.GetWindowRect(&comboRect);
  145.     m_wndToolBar.m_toolBarCombo.ScreenToClient(&comboRect);
  146.  
  147.     m_wndToolBar.m_toolBarCombo.SetWindowPos(&m_wndToolBar.m_toolBarCombo, rect.left,
  148.         rect.top + (rect.Height() - comboRect.Height())/2+1, 0, 0,
  149.         SWP_NOZORDER|SWP_NOSIZE|SWP_NOACTIVATE);
  150.  
  151.     return TRUE;
  152. }
  153. /////////////////////////////////////////////////////////////////////////////
  154. // CMainFrame diagnostics
  155.  
  156. #ifdef _DEBUG
  157. void CMainFrame::AssertValid() const
  158. {
  159.     CFrameWnd::AssertValid();
  160. }
  161.  
  162. void CMainFrame::Dump(CDumpContext& dc) const
  163. {
  164.     CFrameWnd::Dump(dc);
  165. }
  166.  
  167. #endif //_DEBUG
  168.  
  169. /////////////////////////////////////////////////////////////////////////////
  170. // CMainFrame message handlers
  171.  
  172. void CMainFrame::OnViewStatusBar()
  173. {
  174.     CWnd* pStatusBar;
  175.     if (pStatusBar = GetDescendantWindow(AFX_IDW_STATUS_BAR))
  176.     {
  177.         pStatusBar->ShowWindow(!(pStatusBar->GetStyle() & WS_VISIBLE));
  178.         RecalcLayout();
  179.     }
  180. }
  181.  
  182. void CMainFrame::OnUpdateViewStatusBar(CCmdUI* pCmdUI)
  183. {
  184.     CWnd* pStatusBar;
  185.     if (pStatusBar = GetDescendantWindow(AFX_IDW_STATUS_BAR))
  186.         pCmdUI->SetCheck((pStatusBar->GetStyle() & WS_VISIBLE));
  187. }
  188.  
  189. void CMainFrame::OnUpdateLineNumber(CCmdUI* pCmdUI)
  190. {
  191.     pCmdUI->Enable(TRUE);
  192. }
  193.  
  194. void CMainFrame::OnUpdateClock(CCmdUI* pCmdUI)
  195. {
  196.     pCmdUI->Enable(TRUE);
  197. }
  198.  
  199. void CMainFrame::OnEditFindCombo()
  200. {
  201.     m_wndToolBar.m_toolBarCombo.SetFocus();
  202. }
  203.  
  204. void CMainFrame::OnClose()
  205. {
  206.     KillTimer(TIMER_ID);
  207.     CFrameWnd::OnClose();
  208. }
  209.  
  210. void CALLBACK EXPORT CMainFrame::UpdateTime(HWND hWnd,UINT nMsg, UINT nIDEvent, DWORD dwTime)
  211. {
  212.     CString time;
  213.     BOOL bPM = TRUE;
  214.     SYSTEMTIME tm;
  215.  
  216.     CMainFrame* pFrame = (CMainFrame*)CWnd::FromHandle(hWnd);
  217.     CStatusBar* pStatusBar = (CStatusBar*) pFrame->GetDescendantWindow(AFX_IDW_STATUS_BAR);
  218.  
  219.     if (pStatusBar == NULL)
  220.         return;
  221.  
  222.     GetLocalTime(&tm);
  223.  
  224.     if (tm.wHour >= 0 && tm.wHour < 12)
  225.         bPM = FALSE;
  226.  
  227.     // convert 24 hour clock
  228.     if (tm.wHour > 12)      // 13 - 23
  229.         tm.wHour -= 12;
  230.     else if (tm.wHour == 0) // midnight
  231.         tm.wHour = 12;
  232.  
  233.     time.Format(_T(" %d:%02.2d %s "), tm.wHour, tm.wMinute, ((bPM) ? _T("PM"):_T("AM")));
  234.  
  235.     // get current pane font information and update time
  236.     UINT nID, nStyle;
  237.     int nWidth, nIndex = pStatusBar->CommandToIndex(ID_INDICATOR_CLOCK);
  238.  
  239.     CClientDC dc(pStatusBar);
  240.     CFont* pOldFont = dc.SelectObject(pStatusBar->GetFont());
  241.     CSize szExtent = dc.GetTextExtent(time, time.GetLength());
  242.     dc.SelectObject(pOldFont);
  243.  
  244.     pStatusBar->GetPaneInfo(nIndex, nID, nStyle, nWidth);
  245.     pStatusBar->SetPaneInfo(nIndex, nID, nStyle, szExtent.cx);
  246.     pStatusBar->SetPaneText(nIndex, time);
  247. }
  248.