home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / dcom / atldraw / atldview.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  8KB  |  343 lines

  1. // ATLDrawView.cpp : implementation of the CATLDrawView class
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12. //
  13.  
  14. #include "preatldr.h"
  15. #include "ATLDraw.h"
  16.  
  17. #include "ATLDDoc.h"
  18. #include "ATLDView.h"
  19.  
  20. #define IID_DEFINED
  21. #include "..\DrawServ\DrawServ_i.c"
  22.  
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CATLDrawView
  31.  
  32. IMPLEMENT_DYNCREATE(CATLDrawView, CView)
  33.  
  34. BEGIN_MESSAGE_MAP(CATLDrawView, CView)
  35.     //{{AFX_MSG_MAP(CATLDrawView)
  36.     ON_WM_LBUTTONDOWN()
  37.     ON_WM_MOUSEMOVE()
  38.     ON_WM_LBUTTONUP()
  39.     ON_COMMAND(ID_SERVER_CONNECT, OnServerConnect)
  40.     ON_COMMAND(ID_SERVER_DISCONNENT, OnServerDisconnent)
  41.     ON_UPDATE_COMMAND_UI(ID_SERVER_CONNECT, OnUpdateServerConnect)
  42.     ON_UPDATE_COMMAND_UI(ID_SERVER_DISCONNENT, OnUpdateServerDisconnent)
  43.     ON_COMMAND(ID_VIEW_COLOR, OnViewColor)
  44.     //}}AFX_MSG_MAP
  45.     // Standard printing commands
  46.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  47.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  48.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  49. END_MESSAGE_MAP()
  50.  
  51. BEGIN_INTERFACE_MAP(CATLDrawView, CCmdTarget)
  52.     INTERFACE_PART(CATLDrawView, IID_IDrawServ, DrawServ)
  53. END_INTERFACE_MAP()
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CATLDrawView construction/destruction
  57.  
  58. CATLDrawView::CATLDrawView()
  59. {
  60.     HRESULT hr;
  61.  
  62.     hr = CoInitialize(NULL);
  63.     ASSERT(SUCCEEDED(hr));
  64.  
  65.     hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
  66.         RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
  67.     ASSERT(SUCCEEDED(hr));
  68.  
  69.     m_bDragging = FALSE;
  70.     m_pDrawServ = NULL;
  71.     m_col = RGB(255, 0, 0);
  72. }
  73.  
  74. CATLDrawView::~CATLDrawView()
  75. {
  76.     if (m_pDrawServ != NULL)
  77.     {
  78.         DisconnectSink(IID_IDrawServ,m_dwDrawServ);
  79.         m_pDrawServ->Release();
  80.     }
  81.     CoUninitialize();
  82. }
  83.  
  84. BOOL CATLDrawView::PreCreateWindow(CREATESTRUCT& cs)
  85. {
  86.     // TODO: Modify the Window class or styles here by modifying
  87.     //  the CREATESTRUCT cs
  88.  
  89.     return CView::PreCreateWindow(cs);
  90. }
  91.  
  92. /////////////////////////////////////////////////////////////////////////////
  93. // CATLDrawView drawing
  94.  
  95. void CATLDrawView::OnDraw(CDC* pDC)
  96. {
  97.     pDC;
  98.     CATLDrawDoc* pDoc = GetDocument();
  99.     ASSERT_VALID(pDoc);
  100.  
  101.     // TODO: add draw code for native data here
  102. }
  103.  
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CATLDrawView printing
  106.  
  107. BOOL CATLDrawView::OnPreparePrinting(CPrintInfo* pInfo)
  108. {
  109.     // default preparation
  110.     return DoPreparePrinting(pInfo);
  111. }
  112.  
  113. void CATLDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  114. {
  115.     // TODO: add extra initialization before printing
  116. }
  117.  
  118. void CATLDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  119. {
  120.     // TODO: add cleanup after printing
  121. }
  122.  
  123. /////////////////////////////////////////////////////////////////////////////
  124. // CATLDrawView diagnostics
  125.  
  126. #ifdef _DEBUG
  127. void CATLDrawView::AssertValid() const
  128. {
  129.     CView::AssertValid();
  130. }
  131.  
  132. void CATLDrawView::Dump(CDumpContext& dc) const
  133. {
  134.     CView::Dump(dc);
  135. }
  136.  
  137. CATLDrawDoc* CATLDrawView::GetDocument() // non-debug version is inline
  138. {
  139.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CATLDrawDoc)));
  140.     return (CATLDrawDoc*)m_pDocument;
  141. }
  142. #endif //_DEBUG
  143.  
  144. /////////////////////////////////////////////////////////////////////////////
  145. // CATLDrawView message handlers
  146.  
  147. void CATLDrawView::OnLButtonDown(UINT nFlags, CPoint point)
  148. {
  149.     m_bDragging = TRUE;
  150.     m_pos = point;
  151.  
  152.     CView::OnLButtonDown(nFlags, point);
  153. }
  154.  
  155. void CATLDrawView::OnMouseMove(UINT nFlags, CPoint point)
  156. {
  157.     if (m_bDragging)
  158.     {
  159.         if (m_pDrawServ != NULL)
  160.         {
  161.             HRESULT hr;
  162.             hr = m_pDrawServ->Draw(
  163.                 m_pos.x, m_pos.y,
  164.                 point.x, point.y,
  165.                 m_col);
  166.             if (FAILED(hr))
  167.             {
  168.                 TCHAR buf[32];
  169.                 wsprintf(buf, _T("%xd"), hr);
  170.                 AfxMessageBox(buf);
  171.             }
  172.         }
  173.         m_pos = point;
  174.     }
  175.  
  176.     CView::OnMouseMove(nFlags, point);
  177. }
  178.  
  179. void CATLDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  180. {
  181.     m_bDragging = FALSE;
  182.  
  183.     CView::OnLButtonUp(nFlags, point);
  184. }
  185.  
  186. void CATLDrawView::Draw(long x1, long y1, long x2, long y2, unsigned long col)
  187. {
  188.     CClientDC dc(this);
  189.  
  190.     CPen pen(PS_SOLID, 2, (COLORREF) col);
  191.     CPen* oldPen = dc.SelectObject(&pen);
  192.     dc.MoveTo(x1, y1);
  193.     dc.LineTo(x2, y2);
  194.     dc.SelectObject(oldPen);
  195. }
  196.  
  197. void CATLDrawView::OnServerConnect()
  198. {
  199.     HRESULT hr;
  200.  
  201.     hr = CoCreateInstance(CLSID_CDrawServ, NULL, CLSCTX_SERVER, IID_IDrawServ, (void**)&m_pDrawServ);
  202.     if (FAILED(hr))
  203.     {
  204.         AfxMessageBox(_T("Failed to connect to server"));
  205.         return;
  206.     }
  207.  
  208.     IUnknown* pUnk;
  209.     m_xDrawServ.QueryInterface(IID_IUnknown, (void**) &pUnk);
  210.     if (ConnectSink(IID_IDrawServ, pUnk) == FALSE)
  211.     {
  212.         m_pDrawServ->Release();
  213.         m_pDrawServ = NULL;
  214.     }
  215.     pUnk->Release();
  216. }
  217.  
  218. void CATLDrawView::OnServerDisconnent()
  219. {
  220.     if (m_pDrawServ != NULL)
  221.     {
  222.         DisconnectSink(IID_IDrawServ,m_dwDrawServ);
  223.         m_pDrawServ->Release();
  224.         m_pDrawServ = NULL;
  225.         AfxMessageBox("Disconnected");
  226.     }
  227. }
  228.  
  229. BOOL CATLDrawView::ConnectSink(REFIID iid, LPUNKNOWN punkSink)
  230. {
  231.     BOOL    bRC = FALSE;
  232.     ASSERT(m_pDrawServ != NULL);
  233.  
  234.     LPCONNECTIONPOINTCONTAINER pConnPtCont;
  235.  
  236.     if ((m_pDrawServ != NULL) &&
  237.         SUCCEEDED(m_pDrawServ->QueryInterface(IID_IConnectionPointContainer,
  238.             (LPVOID*)&pConnPtCont)))
  239.     {
  240.         ASSERT(pConnPtCont != NULL);
  241.         LPCONNECTIONPOINT pConnPt = NULL;
  242.  
  243.         HRESULT hRes = pConnPtCont->FindConnectionPoint(iid, &pConnPt);
  244.         ASSERT(hRes == S_OK);
  245.         if (SUCCEEDED(hRes))
  246.         {
  247.             ASSERT(pConnPt != NULL);
  248.             hRes = pConnPt->Advise(punkSink, &m_dwDrawServ);
  249.             if (FAILED(hRes))
  250.             {
  251.                 TCHAR buf[32];
  252.                 wsprintf(buf, _T("%x"), hRes);
  253.                 AfxMessageBox(buf);
  254.                 return FALSE;
  255.             }
  256.             AfxMessageBox(_T("Connected"));
  257.             bRC = TRUE;
  258.             pConnPt->Release();
  259.         }
  260.         pConnPtCont->Release();
  261.     }
  262.     return bRC;
  263. }
  264.  
  265. void CATLDrawView::DisconnectSink(REFIID iid, DWORD dwCookie)
  266. {
  267.     if (dwCookie == 0)
  268.         return;
  269.  
  270.     ASSERT(m_pDrawServ != NULL);
  271.  
  272.     LPCONNECTIONPOINTCONTAINER pConnPtCont;
  273.  
  274.     if ((m_pDrawServ != NULL) &&
  275.         SUCCEEDED(m_pDrawServ->QueryInterface(IID_IConnectionPointContainer,
  276.             (LPVOID*)&pConnPtCont)))
  277.     {
  278.         ASSERT(pConnPtCont != NULL);
  279.         LPCONNECTIONPOINT pConnPt = NULL;
  280.  
  281.         if (SUCCEEDED(pConnPtCont->FindConnectionPoint(iid, &pConnPt)))
  282.         {
  283.             ASSERT(pConnPt != NULL);
  284.             pConnPt->Unadvise(dwCookie);
  285.             pConnPt->Release();
  286.         }
  287.  
  288.         pConnPtCont->Release();
  289.     }
  290. }
  291.  
  292. STDMETHODIMP_(ULONG) CATLDrawView::XDrawServ::AddRef()
  293. {
  294.     METHOD_PROLOGUE_EX(CATLDrawView, DrawServ)
  295.     TRACE("AddRef\n");
  296.     return (ULONG)pThis->ExternalAddRef();
  297. }
  298.  
  299. STDMETHODIMP_(ULONG) CATLDrawView::XDrawServ::Release()
  300. {
  301.     METHOD_PROLOGUE_EX(CATLDrawView, DrawServ)
  302.     TRACE("Release\n");
  303.     return (ULONG)pThis->ExternalRelease();
  304. }
  305.  
  306. STDMETHODIMP CATLDrawView::XDrawServ::QueryInterface(
  307.     REFIID iid, LPVOID far * ppvObj)
  308. {
  309.     METHOD_PROLOGUE_EX(CATLDrawView, DrawServ)
  310.     TRACE("QueryInterface\n");
  311.     return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  312. }
  313.  
  314. STDMETHODIMP CATLDrawView::XDrawServ::Draw(
  315.     long x1, long y1, long x2, long y2, unsigned long col)
  316. {
  317.     METHOD_PROLOGUE_EX(CATLDrawView, DrawServ)
  318.  
  319.     pThis->Draw(x1, y1, x2, y2, col);
  320.  
  321.     return S_OK;
  322. }
  323.  
  324. void CATLDrawView::OnUpdateServerConnect(CCmdUI* pCmdUI)
  325. {
  326.     pCmdUI->Enable(m_pDrawServ == NULL);
  327. }
  328.  
  329. void CATLDrawView::OnUpdateServerDisconnent(CCmdUI* pCmdUI)
  330. {
  331.     pCmdUI->Enable(m_pDrawServ != NULL);
  332. }
  333.  
  334. void CATLDrawView::OnViewColor()
  335. {
  336.     CColorDialog dlg;
  337.  
  338.     if (dlg.DoModal() == IDOK)
  339.     {
  340.         m_col = dlg.GetColor();
  341.     }
  342. }
  343.