home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c05 / custsbar / custview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.0 KB  |  162 lines

  1. // CustView.cpp : implementation of the CCustomStatusBarView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "CustSBar.h"
  6.  
  7. #include "MyStatBa.h"
  8. #include "MainFrm.h"
  9.  
  10. #include "CustSDoc.h"
  11. #include "CustView.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. const COLORREF WHITE=RGB(255,255,255);
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CCustomStatusBarView
  27.  
  28. IMPLEMENT_DYNCREATE(CCustomStatusBarView, CView)
  29.  
  30. BEGIN_MESSAGE_MAP(CCustomStatusBarView, CView)
  31.     //{{AFX_MSG_MAP(CCustomStatusBarView)
  32.     //}}AFX_MSG_MAP
  33.     ON_COMMAND_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnColors)
  34.     ON_UPDATE_COMMAND_UI_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnUpdateColors)
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CCustomStatusBarView construction/destruction
  39.  
  40. CCustomStatusBarView::CCustomStatusBarView()
  41. {
  42. }
  43.  
  44. CCustomStatusBarView::~CCustomStatusBarView()
  45. {
  46. }
  47.  
  48. BOOL CCustomStatusBarView::PreCreateWindow(CREATESTRUCT& cs)
  49. {
  50.     // TODO: Modify the Window class or styles here by modifying
  51.     //  the CREATESTRUCT cs
  52.  
  53.     return CView::PreCreateWindow(cs);
  54. }
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CCustomStatusBarView drawing
  58.  
  59. void CCustomStatusBarView::OnDraw(CDC* pDC)
  60. {
  61.     CCustomStatusBarDoc* pDoc = GetDocument();
  62.     ASSERT_VALID(pDoc);
  63.  
  64.     CRect r;
  65.     GetClientRect(&r);
  66.     int x = r.right / 2, y = r.bottom / 2;
  67.  
  68.     pDC->SetTextColor(pDoc->GetColor());
  69.     pDC->SetTextAlign (TA_CENTER | TA_BASELINE);
  70.     pDC->TextOut (x, y, pDoc->GetPhrase());
  71. }
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CCustomStatusBarView diagnostics
  75.  
  76. #ifdef _DEBUG
  77. void CCustomStatusBarView::AssertValid() const
  78. {
  79.     CView::AssertValid();
  80. }
  81.  
  82. void CCustomStatusBarView::Dump(CDumpContext& dc) const
  83. {
  84.     CView::Dump(dc);
  85. }
  86.  
  87. CCustomStatusBarDoc* CCustomStatusBarView::GetDocument() // non-debug version is inline
  88. {
  89.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCustomStatusBarDoc)));
  90.     return (CCustomStatusBarDoc*)m_pDocument;
  91. }
  92. #endif //_DEBUG
  93.  
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CCustomStatusBarView message handlers
  96.  
  97. // This function converts one of the 4 Command IDs to a COLORREF value.
  98. COLORREF CCustomStatusBarView::IDtoColorRef(int nID)
  99. {
  100.     switch (nID)
  101.     {
  102.         case ID_COLORS_RED:
  103.             return RED;
  104.         case ID_COLORS_GREEN:
  105.             return GREEN;        
  106.         case ID_COLORS_BLUE:
  107.             return BLUE;
  108.         default:
  109.             return BLACK;        
  110.     }
  111. }
  112.  
  113. void CCustomStatusBarView::OnColors(UINT nID)
  114. {
  115.     CCustomStatusBarDoc* pDoc = GetDocument();
  116.     ASSERT_VALID(pDoc);
  117.  
  118.     // Change the color (stored in the document class)
  119.     // and invalidate the view.
  120.     pDoc->SetColor(IDtoColorRef(nID));
  121.     Invalidate();
  122.  
  123.     // Since the color changed, the status bar pane
  124.     // which holds the color bitmap needs to be
  125.     // redrawn. CCustomStatusBar::DrawItem is passed
  126.     // a struct that contains a pointer (itemData)
  127.     // to a struct (PaneTool, established by CMainFrame)
  128.     // that contains the ID of the bitmap that is to be drawn
  129.     // in the current pane. See CMainFrame::DoCustomStatusBar
  130.     // which is where this structure is first filled, and
  131.     // CCustomStatusBar::DrawItem where it is extracted and used.
  132.     CMainFrame * cmf = (CMainFrame *)GetParent();
  133.     UINT mID;
  134.     switch(pDoc->GetColor())
  135.     {
  136.         case RED:
  137.             mID = IDM_REDBITMAP;
  138.             break;
  139.         case GREEN:
  140.             mID = IDM_GREENBITMAP;
  141.             break;
  142.         case BLUE:
  143.             mID = IDM_BLUEBITMAP;
  144.             break;
  145.         default:
  146.             mID = IDM_BLACKBITMAP;
  147.     }
  148.     cmf->pt.bitmapID = mID;
  149.  
  150.     // Force the status bar to be redrawn.
  151.     cmf->m_wndStatusBar.Invalidate();
  152. }
  153.  
  154. void CCustomStatusBarView::OnUpdateColors(CCmdUI* pCmdUI)
  155. {
  156.     CCustomStatusBarDoc* pDoc = GetDocument();
  157.     ASSERT_VALID(pDoc);
  158.  
  159.     pCmdUI->SetCheck(pDoc->GetColor() == IDtoColorRef(pCmdUI->m_nID));
  160. }
  161.  
  162.