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

  1. // MlesView.cpp : implementation of the CModelessView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Modeless.h"
  6.  
  7. #include "Dialogs.H"
  8.  
  9. #include "MdlesDoc.h"
  10. #include "MlesView.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CModelessView
  20.  
  21. IMPLEMENT_DYNCREATE(CModelessView, CView)
  22.  
  23. BEGIN_MESSAGE_MAP(CModelessView, CView)
  24.     //{{AFX_MSG_MAP(CModelessView)
  25.     ON_COMMAND(ID_MODIFY_DISPLAYMODELESSDIALOG, OnModifyDisplaymodelessdialog)
  26.     ON_UPDATE_COMMAND_UI(ID_MODIFY_DISPLAYMODELESSDIALOG, OnUpdateModifyDisplaymodelessdialog)
  27.     //}}AFX_MSG_MAP
  28.     ON_REGISTERED_MESSAGE(CModelessView::m_UserMsg, MyMessageHandler)
  29. END_MESSAGE_MAP()
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CModelessView construction/destruction
  33.  
  34. // Static member variables must be initialized.
  35. // Register the message the dialog box will send to this object.
  36. UINT CModelessView::m_UserMsg = 
  37.     RegisterWindowMessage(MY_DIALOGBOX_MSG);
  38.  
  39. CModelessView::CModelessView()
  40.     :m_pDlg(0)
  41. {
  42. }
  43.  
  44. CModelessView::~CModelessView()
  45. {
  46. }
  47.  
  48. BOOL CModelessView::PreCreateWindow(CREATESTRUCT& cs)
  49. {
  50.     return CView::PreCreateWindow(cs);
  51. }
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CModelessView drawing
  55.  
  56. void CModelessView::OnDraw(CDC* pDC)
  57. {
  58.     CModelessDoc* pDoc = GetDocument();
  59.     ASSERT_VALID(pDoc);
  60.  
  61.     CRect r;
  62.     GetClientRect(&r);
  63.     int x = r.right / 2, y = r.bottom / 2;
  64.  
  65.     pDC->SetTextColor(pDoc->GetColor());
  66.     pDC->SetTextAlign (TA_CENTER | TA_BASELINE);
  67.     pDC->TextOut (x, y, pDoc->GetPhrase());
  68. }
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CModelessView diagnostics
  72.  
  73. #ifdef _DEBUG
  74. void CModelessView::AssertValid() const
  75. {
  76.     CView::AssertValid();
  77. }
  78.  
  79. void CModelessView::Dump(CDumpContext& dc) const
  80. {
  81.     CView::Dump(dc);
  82. }
  83.  
  84. CModelessDoc* CModelessView::GetDocument() // non-debug version is inline
  85. {
  86.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CModelessDoc)));
  87.     return (CModelessDoc*)m_pDocument;
  88. }
  89. #endif //_DEBUG
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CModelessView message handlers
  93.  
  94. void CModelessView::OnModifyDisplaymodelessdialog() 
  95. {
  96.     CModelessDoc* pDoc = GetDocument();
  97.     ASSERT_VALID(pDoc);
  98.  
  99.     // Create and initialize the dialog box object.
  100.     // It must be persistent (not stack-based).
  101.     m_pDlg = new CListWithItemDataDlg(this);
  102.     m_pDlg->m_color = pDoc->GetColor();
  103.     m_pDlg->m_phrase = pDoc->GetPhrase();
  104.     m_pDlg->Create(IDD_PHRASE_MODELESS);
  105.  
  106.     // The call to Create has caused DDX to occur.
  107.     // Everything's done, show the window. Note that the
  108.     // DB itself has its visible property set to false.
  109.     m_pDlg->ShowWindow(SW_RESTORE);
  110. }
  111.  
  112. LRESULT CModelessView::MyMessageHandler(WPARAM wParam, LPARAM lParam)
  113. {
  114.     CModelessDoc* pDoc = GetDocument();
  115.     ASSERT_VALID(pDoc);
  116.  
  117.     switch (wParam)
  118.     {
  119.         case ML_APPLY:
  120.             pDoc->SetPhrase(m_pDlg->m_phrase);
  121.             pDoc->SetColor(m_pDlg->m_color);
  122.             Invalidate();
  123.             break;
  124.  
  125.         case ML_CANCEL:
  126.             // The dialog box's own code will properly destroy
  127.             // the object, but it's important that this class
  128.             // set its pointer to the dialog box to 0.
  129.             m_pDlg = 0;
  130.     }
  131.     return TRUE;
  132. }
  133.  
  134. void CModelessView::OnUpdateModifyDisplaymodelessdialog(CCmdUI* pCmdUI) 
  135. {
  136.     pCmdUI->Enable(0 == m_pDlg);
  137. }
  138.