home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / propdlg / shapesvw.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  9KB  |  330 lines

  1. // shapesvw.cpp : implementation of the CShapesView 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. #include "stdafx.h"
  14. #include "propdlg.h"
  15.  
  16. #include "shapeobj.h"
  17. #include "colorpge.h"
  18. #include "stylepge.h"
  19. #include "preview.h"
  20. #include "propsht.h"
  21. #include "propsht2.h"
  22. #include "shapedoc.h"
  23. #include "shapesvw.h"
  24.  
  25. #include "minifrm.h"
  26. #include "mainfrm.h"
  27.  
  28. #ifdef _DEBUG
  29. #undef THIS_FILE
  30. static char BASED_CODE THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CShapesView
  35.  
  36. IMPLEMENT_DYNCREATE(CShapesView, CView)
  37.  
  38. BEGIN_MESSAGE_MAP(CShapesView, CView)
  39.     ON_MESSAGE(WM_USER_CHANGE_OBJECT_PROPERTIES, OnChangeObjectProps)
  40.     //{{AFX_MSG_MAP(CShapesView)
  41.     ON_WM_LBUTTONDOWN()
  42.     ON_COMMAND(ID_SIMPLE_PROPERTY_SHEET, OnSimplePropertySheet)
  43.     ON_UPDATE_COMMAND_UI(ID_SIMPLE_PROPERTY_SHEET, OnUpdateSimplePropertySheet)
  44.     ON_COMMAND(ID_PROPERTY_SHEET_WITH_PREVIEW, OnPropertySheetWithPreview)
  45.     ON_WM_KEYDOWN()
  46.     ON_UPDATE_COMMAND_UI(ID_PROPERTY_SHEET_WITH_PREVIEW, OnUpdatePropertySheetWithPreview)
  47.     ON_WM_LBUTTONDBLCLK()
  48.     //}}AFX_MSG_MAP
  49.     // Standard printing commands
  50.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  51.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  52. END_MESSAGE_MAP()
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CShapesView construction/destruction
  56.  
  57. CShapesView::CShapesView()
  58. {
  59.     m_pShapeSelected = NULL;
  60.     m_pModalShapePropSheet = NULL;
  61. }
  62.  
  63. CShapesView::~CShapesView()
  64. {
  65. }
  66.  
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // Operations
  70.  
  71. CModelessShapePropSheet* CShapesView::GetModelessShapePropSheet()
  72. {
  73.     CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  74.     if (pMainFrame->m_pShapePropFrame == NULL)
  75.         return NULL;
  76.     return pMainFrame->m_pShapePropFrame->m_pModelessShapePropSheet;
  77. }
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CShapesView drawing
  81.  
  82. void CShapesView::OnDraw(CDC* pDC)
  83. {
  84.     CShapesDoc* pDoc = GetDocument();
  85.     ASSERT_VALID(pDoc);
  86.  
  87.     CRect rectDirty, rectIntersect;
  88.     pDC->GetClipBox(&rectDirty);
  89.     if (rectDirty.IsRectNull())
  90.         GetClientRect(&rectDirty);
  91.  
  92.     CShape* pShape;
  93.     POSITION pos = pDoc->m_shapeList.GetTailPosition();
  94.     // Draw all of the shapes, except the currently selected shape, from
  95.     // back to front.
  96.     while (pos != NULL)
  97.     {
  98.         pShape = pDoc->m_shapeList.GetPrev(pos);
  99.         if (rectIntersect.IntersectRect(&pShape->m_rect, &rectDirty) != 0
  100.             && pShape != m_pShapeSelected)
  101.             pShape->Draw(pDC, FALSE);
  102.     }
  103.     // Draw the selected shape on top
  104.     if (m_pShapeSelected != NULL)
  105.         m_pShapeSelected->Draw(pDC, TRUE);
  106. }
  107.  
  108. /////////////////////////////////////////////////////////////////////////////
  109. // CShapesView printing
  110.  
  111. BOOL CShapesView::OnPreparePrinting(CPrintInfo* pInfo)
  112. {
  113.     // default preparation
  114.     return DoPreparePrinting(pInfo);
  115. }
  116.  
  117. void CShapesView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  118. {
  119. }
  120.  
  121. void CShapesView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  122. {
  123. }
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126. // CShapesView diagnostics
  127.  
  128. #ifdef _DEBUG
  129. void CShapesView::AssertValid() const
  130. {
  131.     CView::AssertValid();
  132. }
  133.  
  134. void CShapesView::Dump(CDumpContext& dc) const
  135. {
  136.     CView::Dump(dc);
  137. }
  138.  
  139. CShapesDoc* CShapesView::GetDocument() // non-debug version is inline
  140. {
  141.     ASSERT_KINDOF(CShapesDoc, m_pDocument);
  142.     return (CShapesDoc*)m_pDocument;
  143. }
  144. #endif //_DEBUG
  145.  
  146. /////////////////////////////////////////////////////////////////////////////
  147. // CShapesView message handlers
  148.  
  149. void CShapesView::OnLButtonDown(UINT /*nFlags*/, CPoint point)
  150. {
  151.     CShapesDoc* pDoc = GetDocument();
  152.     CModelessShapePropSheet* pModelessShapePropSheet
  153.         = GetModelessShapePropSheet();
  154.  
  155.     CClientDC dc(this);
  156.  
  157.     CShape* pShapeHit;
  158.     pShapeHit = pDoc->HitTest(point);
  159.     if (pShapeHit != NULL)
  160.     {   // Select the object that was clicked.
  161.  
  162.         CRect rectInvalid;
  163.         // Invalidate the previously selected object, so it will
  164.         // be redrawn as unselected.
  165.         if (m_pShapeSelected != NULL)
  166.             InvalidateRect(&m_pShapeSelected->m_rect);
  167.         m_pShapeSelected = pShapeHit;
  168.  
  169.         // Invalidate the newly selected object, so it will be
  170.         // redrawn as selected.
  171.         InvalidateRect(&m_pShapeSelected->m_rect);
  172.  
  173.         // If the modeless property is visible, update its
  174.         // settings to reflect the newly selected shape.
  175.         if (pModelessShapePropSheet != NULL
  176.             && pModelessShapePropSheet->IsWindowVisible())
  177.         {
  178.            pModelessShapePropSheet->
  179.                SetSheetPropsFromShape(m_pShapeSelected);
  180.         }
  181.     }
  182.     else
  183.     {   // Add a new shape
  184.  
  185.         CShape* pShapeNew = new CShape(
  186.             black,
  187.             rectangle,
  188.             CRect(point.x-50, point.y-50, point.x + 50, point.y + 50));
  189.         pDoc->AddShape(pShapeNew);
  190.         m_pShapeSelected = pShapeNew;
  191.         // If the modeless property sheet exists, then apply its current
  192.         // settings to the new shape.
  193.         if (pModelessShapePropSheet != NULL)
  194.         {
  195.            pModelessShapePropSheet->
  196.                SetShapePropsFromSheet(m_pShapeSelected);
  197.         }
  198.     }
  199. }
  200.  
  201. void CShapesView::OnLButtonDblClk(UINT /*nFlags*/, CPoint /*point*/)
  202. {
  203.     // Double-click anywhere brings up modeless property sheet.
  204.     CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  205.     pMainFrame->OnMiniFramePropertySheet();
  206. }
  207.  
  208.  
  209. void CShapesView::OnSimplePropertySheet()
  210. {
  211.     if (m_pShapeSelected == NULL)
  212.         return;
  213.  
  214.     CPropertySheet dlgPropertySheet(AFX_IDS_APP_TITLE);
  215.     CStylePage stylePage;
  216.     CColorPage colorPage;
  217.  
  218.     stylePage.m_nShapeStyle = m_pShapeSelected->m_shapestyle;
  219.     colorPage.m_nColor = m_pShapeSelected->m_shapecolor;
  220.  
  221.     dlgPropertySheet.AddPage(&stylePage);
  222.     dlgPropertySheet.AddPage(&colorPage);
  223.  
  224.     if (dlgPropertySheet.DoModal() == IDOK)
  225.     {
  226.         m_pShapeSelected->m_shapecolor = colorPage.m_nColor;
  227.         m_pShapeSelected->m_shapestyle
  228.             = (SHAPE_STYLE)stylePage.m_nShapeStyle;
  229.         GetDocument()->SetModifiedFlag();
  230.         GetDocument()->UpdateAllViews(NULL);
  231.     }
  232. }
  233.  
  234. void CShapesView::OnUpdateSimplePropertySheet(CCmdUI* pCmdUI)
  235. {
  236.     // Borrow logic from Property Sheet with Preview command.
  237.     OnUpdatePropertySheetWithPreview(pCmdUI);
  238. }
  239.  
  240. void CShapesView::OnPropertySheetWithPreview()
  241. {
  242.     if (m_pShapeSelected == NULL)
  243.         return;
  244.  
  245.     // Save pointer to the modal property sheet so that when the
  246.     // user choose Apply Now and the CModalShapePropSheet sends
  247.     // the WM_USER_CHANGE_OBJECT_PROPERTIES message to the view,
  248.     // the view can easily refer back to the property sheet
  249.     // to get the current settings and apply them to the currently
  250.     // selected object.
  251.     m_pModalShapePropSheet = new CModalShapePropSheet;
  252.  
  253.     // Cannot use CModalShapePropSheet::SetSheetPropsFromShape here
  254.     // because it was designed to be called from the handler of
  255.     // the user-defined WM_USER_CHANGE_OBJECT_PROPS message when
  256.     // the user chooses Apply Now.  The window for the modal
  257.     // property sheet here has not been created yet.  Calling
  258.     // SetSheetPropsFromShape would result in a failure in trying
  259.     // to update the window.
  260.     m_pModalShapePropSheet->m_stylePage.m_nShapeStyle
  261.         = m_pShapeSelected->m_shapestyle;
  262.     m_pModalShapePropSheet->m_colorPage.m_nColor = m_pShapeSelected->m_shapecolor;
  263.  
  264.     if (m_pModalShapePropSheet->DoModal() == IDOK)
  265.     {
  266.         m_pModalShapePropSheet->SetShapePropsFromSheet(m_pShapeSelected);
  267.         GetDocument()->SetModifiedFlag();
  268.         GetDocument()->UpdateAllViews(NULL);
  269.     }
  270.     delete m_pModalShapePropSheet;
  271.     m_pModalShapePropSheet = NULL;
  272. }
  273.  
  274. void CShapesView::OnUpdatePropertySheetWithPreview(CCmdUI* pCmdUI)
  275. {
  276.     BOOL bEnable = TRUE;
  277.     if (m_pShapeSelected == NULL)
  278.     {
  279.         bEnable = FALSE;
  280.     }
  281.     else
  282.     {
  283.         CModelessShapePropSheet* pModelessShapePropSheet
  284.             = GetModelessShapePropSheet();
  285.         if (pModelessShapePropSheet != NULL
  286.             && pModelessShapePropSheet->IsWindowVisible())
  287.         {
  288.             // Don't display modal property sheet if the modeless
  289.             // property sheet is already displayed.
  290.             bEnable = FALSE;
  291.         }
  292.     }
  293.     pCmdUI->Enable(bEnable);
  294. }
  295.  
  296. LRESULT CShapesView::OnChangeObjectProps(WPARAM, LPARAM)
  297. {
  298.     // The user-defined WM_USER_CHANGE_OBJECT_PROPS message is sent
  299.     // by the modal CModalShapePropSheet when the user chooses Apply Now,
  300.     // or by the modeless CModelessShapePropSheet when the user
  301.     // changes any setting in a property page.
  302.  
  303.     if (m_pShapeSelected == NULL)
  304.         return 0;
  305.  
  306.     CModelessShapePropSheet* pModelessShapePropSheet
  307.         = GetModelessShapePropSheet();
  308.     if (pModelessShapePropSheet != NULL)
  309.         pModelessShapePropSheet->SetShapePropsFromSheet(m_pShapeSelected);
  310.  
  311.     if (m_pModalShapePropSheet != NULL)
  312.         m_pModalShapePropSheet->SetShapePropsFromSheet(m_pShapeSelected);
  313.  
  314.     GetDocument()->SetModifiedFlag();
  315.     GetDocument()->UpdateAllViews(NULL);
  316.     return 0;
  317. }
  318.  
  319. void CShapesView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  320. {
  321.     if (nChar == VK_ESCAPE)
  322.     {
  323.         CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  324.         pMainFrame->HideModelessPropSheet();
  325.         return;
  326.     }
  327.  
  328.     CView::OnKeyDown(nChar, nRepCnt, nFlags);
  329. }
  330.