home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c06 / modal / modlview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.3 KB  |  169 lines

  1. // ModlView.cpp : implementation of the CModalView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Modal.h"
  6.  
  7. #include "Dialogs.h"
  8. #include "XtraFunc.h"
  9.  
  10. #include "ModalDoc.h"
  11. #include "ModlView.h"
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CModalView
  21.  
  22. IMPLEMENT_DYNCREATE(CModalView, CView)
  23.  
  24. BEGIN_MESSAGE_MAP(CModalView, CView)
  25.     //{{AFX_MSG_MAP(CModalView)
  26.     ON_COMMAND(ID_MODIFY_SHOWDIALOG, OnModifyShowdialog)
  27.     ON_COMMAND(ID_MODIFY_SHOWLISTDIALOG, OnModifyShowlistdialog)
  28.     ON_COMMAND(ID_MODIFY_SHOWLISTITEMDATADIALOG, OnModifyShowlistitemdatadialog)
  29.     //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CModalView construction/destruction
  34.  
  35. CModalView::CModalView()
  36. {
  37. }
  38.  
  39. CModalView::~CModalView()
  40. {
  41. }
  42.  
  43. BOOL CModalView::PreCreateWindow(CREATESTRUCT& cs)
  44. {
  45.     return CView::PreCreateWindow(cs);
  46. }
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CModalView drawing
  50.  
  51. void CModalView::OnDraw(CDC* pDC)
  52. {
  53.     CModalDoc* pDoc = GetDocument();
  54.     ASSERT_VALID(pDoc);
  55.  
  56.     CRect r;
  57.     GetClientRect(&r);
  58.     int x = r.right / 2, y = r.bottom / 2;
  59.  
  60.     pDC->SetTextColor(pDoc->GetColor());
  61.     pDC->SetTextAlign (TA_CENTER | TA_BASELINE);
  62.     pDC->TextOut (x, y, pDoc->GetPhrase());
  63. }
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CModalView diagnostics
  67.  
  68. #ifdef _DEBUG
  69. void CModalView::AssertValid() const
  70. {
  71.     CView::AssertValid();
  72. }
  73.  
  74. void CModalView::Dump(CDumpContext& dc) const
  75. {
  76.     CView::Dump(dc);
  77. }
  78.  
  79. CModalDoc* CModalView::GetDocument() // non-debug version is inline
  80. {
  81.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CModalDoc)));
  82.     return (CModalDoc*)m_pDocument;
  83. }
  84. #endif //_DEBUG
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CModalView message handlers
  88.  
  89. void CModalView::OnModifyShowdialog() 
  90. {
  91.     CColorPhraseDlg dlg;
  92.     CModalDoc* pDoc = GetDocument();
  93.     ASSERT_VALID(pDoc);
  94.  
  95.     // Load the dialog box's members before
  96.     // displaying it.
  97.     dlg.m_phrase = pDoc->GetPhrase();
  98.     dlg.m_color = RgbToInt(pDoc->GetColor());
  99.     
  100.     // If the user clicks on OK, replace the 
  101.     // document's members with the fields from
  102.     // the dialog box, and repaint the view.
  103.     if (IDOK == dlg.DoModal())
  104.     {
  105.         pDoc->SetPhrase(dlg.m_phrase);
  106.         pDoc->SetColor(IntToRgb(dlg.m_color));
  107.         Invalidate();
  108.     }
  109. }
  110.  
  111. void CModalView::OnModifyShowlistdialog() 
  112. {
  113.     CModalDoc* pDoc = GetDocument();
  114.     ASSERT_VALID(pDoc);
  115.  
  116.     // In order to set the current selection in its list box, the 
  117.     // dialog box needs to know the current color. See the dialog box's
  118.     // constructor and OnInitDialog for more information.
  119.     CColorListDlg dlg2(pDoc->GetColor());
  120.  
  121.     // Load the dialog box's members before
  122.     // displaying it.
  123.     dlg2.m_phrase = pDoc->GetPhrase();
  124.  
  125.     // The dialog box's must do all the initialization of the
  126.     // list box in its own overridden OnInitDialog function.
  127.  
  128.     // If the user clicks on OK, replace the document's 
  129.     // members with the fields from the dialog box, and repaint
  130.     // the view. A list box can give back eitehr a string or an
  131.     // int, but it will only give an int if the list box's sort
  132.     // property is set to false.
  133.     if (IDOK == dlg2.DoModal())
  134.     {
  135.         pDoc->SetPhrase(dlg2.m_phrase);
  136.         pDoc->SetColor(IntToRgb(dlg2.m_color));
  137.         Invalidate();
  138.     }
  139. }
  140.  
  141. void CModalView::OnModifyShowlistitemdatadialog() 
  142. {
  143.     CModalDoc* pDoc = GetDocument();
  144.     ASSERT_VALID(pDoc);
  145.  
  146.     CListWithItemDataDlg dlg3;
  147.  
  148.     // Load the dialog box's members before    displaying it.
  149.     // Note that for this dialog box class, m_color was 
  150.     // manually added. Examine the dialog box's DoDataExchange
  151.     // and InitDialog functions to see how it's used.
  152.     dlg3.m_phrase = pDoc->GetPhrase();
  153.     dlg3.m_color = pDoc->GetColor();
  154.  
  155.     // The dialog box's must do all the initialization of the
  156.     // list box in its own overridden OnInitDialog function.
  157.  
  158.     // If the user clicks on OK, replace the 
  159.     // document's members with the fields from
  160.     // the dialog box, and repaint the view.
  161.     if (IDOK == dlg3.DoModal())
  162.     {
  163.         pDoc->SetPhrase(dlg3.m_phrase);
  164.         pDoc->SetColor(dlg3.m_color);        // NO CONVERSIONS!
  165.         Invalidate();
  166.     }
  167. }
  168.  
  169.