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

  1. // NumbView.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "TmpGraph.h"
  6. #include "TGrafDoc.h"
  7. #include "NumbView.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CTemperatureNumbersView
  17.  
  18. IMPLEMENT_DYNCREATE(CTemperatureNumbersView, CView)
  19.  
  20. CTemperatureNumbersView::CTemperatureNumbersView()
  21. {
  22. }
  23.  
  24. CTemperatureNumbersView::~CTemperatureNumbersView()
  25. {
  26. }
  27.  
  28.  
  29. BEGIN_MESSAGE_MAP(CTemperatureNumbersView, CView)
  30.     //{{AFX_MSG_MAP(CTemperatureNumbersView)
  31.     //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CTemperatureNumbersView drawing
  36.  
  37. void CTemperatureNumbersView::OnDraw(CDC* pDC)
  38. {
  39.     const int DAYS = 31;
  40.     CTemperatureGraphDoc * pDoc = GetDocument();
  41.  
  42.     CFont *    prevFont = (CFont*)
  43.         pDC->SelectStockObject(ANSI_FIXED_FONT);
  44.  
  45.     UINT oldAlignment = pDC->GetTextAlign();
  46.  
  47.     TEXTMETRIC tm;
  48.     pDC->GetTextMetrics(&tm);
  49.  
  50.     CRect rect;
  51.     GetClientRect(&rect);
  52.  
  53.     CString s;
  54.     int i, x, y;
  55.  
  56.     // Display a header line, centered in the bottom pane.
  57.     pDC->SetTextAlign(TA_CENTER);
  58.     s.Format("Daily high temperatures (Celsius) for %s in %s, %d",
  59.             (LPCTSTR) pDoc->m_city, (LPCTSTR) pDoc->m_month,
  60.             pDoc->m_year);
  61.     pDC->TextOut (rect.Width() / 2, 0, s);
  62.     
  63.     // Now that the heading line is displayed, move the origin
  64.     // down 2 lines and display the numbers. This is slightly
  65.     // easier than adjusting the y location in code.
  66.     pDC->SetViewportOrg(0, 2 * tm.tmHeight);
  67.     pDC->SetTextAlign(TA_LEFT);
  68.  
  69.     for(i = 0; i < pDoc->temps.GetSize(); i++)
  70.     {
  71.         s.Format("%2d: %2d", pDoc->temps[i].x + 1, pDoc->temps[i].y);
  72.         if (i < DAYS)
  73.         {
  74.             x = i / 6 * 100;
  75.             y = i % 6 * tm.tmHeight;
  76.         }
  77.         else
  78.         // 31 is a special case to make it appear in last column.
  79.         {
  80.             x = 4 * 100;
  81.             y = 6 * tm.tmHeight;
  82.         }
  83.         
  84.         pDC->TextOut(x, y, s);
  85.     }
  86.  
  87.     pDC->SelectObject(prevFont);
  88.     pDC->SetTextAlign(oldAlignment);
  89. }
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CTemperatureNumbersView diagnostics
  93. #ifdef _DEBUG
  94.  
  95. void CTemperatureNumbersView::AssertValid() const
  96. {
  97.     CView::AssertValid();
  98. }
  99.  
  100. void CTemperatureNumbersView::Dump(CDumpContext& dc) const
  101. {
  102.     CView::Dump(dc);
  103. }
  104.  
  105. CTemperatureGraphDoc* CTemperatureNumbersView::GetDocument()
  106. {
  107.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTemperatureGraphDoc)));
  108.     return (CTemperatureGraphDoc*)m_pDocument;
  109. }
  110.  
  111. #endif //_DEBUG
  112.  
  113. /////////////////////////////////////////////////////////////////////////////
  114. // CTemperatureNumbersView message handlers
  115.