home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / winocc.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  12KB  |  458 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #include "occimpl.h"
  13.  
  14. #ifdef AFX_CORE1_SEG
  15. #pragma code_seg(AFX_CORE1_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #define new DEBUG_NEW
  24.  
  25. #ifndef _AFX_NO_OCC_SUPPORT
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CWnd overridable for ambient properties
  29.  
  30. BOOL CWnd::OnAmbientProperty(COleControlSite* pSite, DISPID dispid,
  31.     VARIANT* pvar)
  32. {
  33.     ASSERT(m_pCtrlCont != NULL);
  34.     return m_pCtrlCont->GetAmbientProp(pSite, dispid, pvar);
  35. }
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CWnd access to underlying OLE control interface
  39.  
  40. LPUNKNOWN CWnd::GetControlUnknown()
  41. {
  42.     if (m_pCtrlSite == NULL)
  43.         return NULL;
  44.  
  45.     return m_pCtrlSite->m_pObject;
  46. }
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CWnd functions with special cases for OLE Control containment
  50.  
  51. void CWnd::CheckDlgButton(int nIDButton, UINT nCheck)
  52. {
  53.     ASSERT(::IsWindow(m_hWnd));
  54.  
  55.     if (m_pCtrlCont == NULL)
  56.         ::CheckDlgButton(m_hWnd, nIDButton, nCheck);
  57.     else
  58.         m_pCtrlCont->CheckDlgButton(nIDButton, nCheck);
  59. }
  60.  
  61. void CWnd::CheckRadioButton(int nIDFirstButton, int nIDLastButton,
  62.     int nIDCheckButton)
  63. {
  64.     ASSERT(::IsWindow(m_hWnd));
  65.  
  66.     if (m_pCtrlCont == NULL)
  67.         ::CheckRadioButton(m_hWnd, nIDFirstButton, nIDLastButton,
  68.             nIDCheckButton);
  69.     else
  70.         m_pCtrlCont->CheckRadioButton(nIDFirstButton, nIDLastButton,
  71.             nIDCheckButton);
  72. }
  73.  
  74. CWnd* CWnd::GetDlgItem(int nID) const
  75. {
  76.     ASSERT(::IsWindow(m_hWnd));
  77.  
  78.     if (m_pCtrlCont == NULL)
  79.         return CWnd::FromHandle(::GetDlgItem(m_hWnd, nID));
  80.     else
  81.         return m_pCtrlCont->GetDlgItem(nID);
  82. }
  83.  
  84. void CWnd::GetDlgItem(int nID, HWND* phWnd) const
  85. {
  86.     ASSERT(::IsWindow(m_hWnd));
  87.     ASSERT(phWnd != NULL);
  88.  
  89.     if (m_pCtrlCont == NULL)
  90.         *phWnd = ::GetDlgItem(m_hWnd, nID);
  91.     else
  92.         m_pCtrlCont->GetDlgItem(nID, phWnd);
  93. }
  94.  
  95. UINT CWnd::GetDlgItemInt(int nID, BOOL* lpTrans, BOOL bSigned) const
  96. {
  97.     ASSERT(::IsWindow(m_hWnd));
  98.  
  99.     if (m_pCtrlCont == NULL)
  100.         return ::GetDlgItemInt(m_hWnd, nID, lpTrans, bSigned);
  101.     else
  102.         return m_pCtrlCont->GetDlgItemInt(nID, lpTrans, bSigned);
  103. }
  104.  
  105. int CWnd::GetDlgItemText(int nID, LPTSTR lpStr, int nMaxCount) const
  106. {
  107.     ASSERT(::IsWindow(m_hWnd));
  108.  
  109.     if (m_pCtrlCont == NULL)
  110.         return ::GetDlgItemText(m_hWnd, nID, lpStr, nMaxCount);
  111.     else
  112.         return m_pCtrlCont->GetDlgItemText(nID, lpStr, nMaxCount);
  113. }
  114.  
  115. LRESULT CWnd::SendDlgItemMessage(int nID, UINT message, WPARAM wParam,
  116.     LPARAM lParam)
  117. {
  118.     ASSERT(::IsWindow(m_hWnd));
  119.  
  120.     if (m_pCtrlCont == NULL)
  121.         return ::SendDlgItemMessage(m_hWnd, nID, message, wParam, lParam);
  122.     else
  123.         return m_pCtrlCont->SendDlgItemMessage(nID, message, wParam, lParam);
  124. }
  125.  
  126. void CWnd::SetDlgItemInt(int nID, UINT nValue, BOOL bSigned)
  127. {
  128.     ASSERT(::IsWindow(m_hWnd));
  129.  
  130.     if (m_pCtrlCont == NULL)
  131.         ::SetDlgItemInt(m_hWnd, nID, nValue, bSigned);
  132.     else
  133.         m_pCtrlCont->SetDlgItemInt(nID, nValue, bSigned);
  134. }
  135.  
  136. void CWnd::SetDlgItemText(int nID, LPCTSTR lpszString)
  137. {
  138.     ASSERT(::IsWindow(m_hWnd));
  139.  
  140.     if (m_pCtrlCont == NULL)
  141.         ::SetDlgItemText(m_hWnd, nID, lpszString);
  142.     else
  143.         m_pCtrlCont->SetDlgItemText(nID, lpszString);
  144. }
  145.  
  146. UINT CWnd::IsDlgButtonChecked(int nIDButton) const
  147. {
  148.     ASSERT(::IsWindow(m_hWnd));
  149.  
  150.     if (m_pCtrlCont == NULL)
  151.         return ::IsDlgButtonChecked(m_hWnd, nIDButton);
  152.     else
  153.         return m_pCtrlCont->IsDlgButtonChecked(nIDButton);
  154. }
  155.  
  156. int CWnd::ScrollWindowEx(int dx, int dy, LPCRECT lpRectScroll,
  157.     LPCRECT lpRectClip, CRgn* prgnUpdate, LPRECT lpRectUpdate,
  158.     UINT flags)
  159. {
  160.     ASSERT(::IsWindow(m_hWnd));
  161.  
  162.     int iReturn = ::ScrollWindowEx(m_hWnd, dx, dy, lpRectScroll, lpRectClip,
  163.             (HRGN)prgnUpdate->GetSafeHandle(), lpRectUpdate, flags);
  164.  
  165.     if ((m_pCtrlCont == NULL) || !(flags & SW_SCROLLCHILDREN))
  166.         return iReturn;
  167.  
  168.     // the following code is for OLE control containers only
  169.  
  170.     m_pCtrlCont->ScrollChildren(dx, dy);
  171.     return iReturn;
  172. }
  173.  
  174. BOOL CWnd::IsDialogMessage(LPMSG lpMsg)
  175. {
  176.     ASSERT(::IsWindow(m_hWnd));
  177.  
  178.     if (m_nFlags & WF_OLECTLCONTAINER)
  179.         return afxOccManager->IsDialogMessage(this, lpMsg);
  180.     else
  181.         return ::IsDialogMessage(m_hWnd, lpMsg);
  182. }
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185. // CWnd functions with special cases for OLE Control wrappers
  186.  
  187. DWORD CWnd::GetStyle() const
  188. {
  189.     ASSERT(::IsWindow(m_hWnd));
  190.  
  191.     if (m_pCtrlSite == NULL)
  192.         return (DWORD)GetWindowLong(m_hWnd, GWL_STYLE);
  193.     else
  194.         return m_pCtrlSite->GetStyle();
  195. }
  196.  
  197. DWORD CWnd::GetExStyle() const
  198. {
  199.     ASSERT(::IsWindow(m_hWnd));
  200.  
  201.     if (m_pCtrlSite == NULL)
  202.         return (DWORD)GetWindowLong(m_hWnd, GWL_EXSTYLE);
  203.     else
  204.         return m_pCtrlSite->GetExStyle();
  205. }
  206.  
  207. BOOL CWnd::ModifyStyle(DWORD dwRemove, DWORD dwAdd, UINT nFlags)
  208. {
  209.     ASSERT(::IsWindow(m_hWnd));
  210.  
  211.     if (m_pCtrlSite == NULL)
  212.         return ModifyStyle(m_hWnd, dwRemove, dwAdd, nFlags);
  213.     else
  214.         return m_pCtrlSite->ModifyStyle(dwRemove, dwAdd, nFlags);
  215. }
  216.  
  217. BOOL CWnd::ModifyStyleEx(DWORD dwRemove, DWORD dwAdd, UINT nFlags)
  218. {
  219.     ASSERT(::IsWindow(m_hWnd));
  220.  
  221.     if (m_pCtrlSite == NULL)
  222.         return ModifyStyleEx(m_hWnd, dwRemove, dwAdd, nFlags);
  223.     else
  224.         return m_pCtrlSite->ModifyStyleEx(dwRemove, dwAdd, nFlags);
  225. }
  226.  
  227. void CWnd::SetWindowText(LPCTSTR lpszString)
  228. {
  229.     ASSERT(::IsWindow(m_hWnd));
  230.  
  231.     if (m_pCtrlSite == NULL)
  232.         ::SetWindowText(m_hWnd, lpszString);
  233.     else
  234.         m_pCtrlSite->SetWindowText(lpszString);
  235. }
  236.  
  237. int CWnd::GetWindowText(LPTSTR lpszString, int nMaxCount) const
  238. {
  239.     ASSERT(::IsWindow(m_hWnd));
  240.  
  241.     if (m_pCtrlSite == NULL)
  242.         return ::GetWindowText(m_hWnd, lpszString, nMaxCount);
  243.     else
  244.         return m_pCtrlSite->GetWindowText(lpszString, nMaxCount);
  245. }
  246.  
  247. int CWnd::GetWindowTextLength() const
  248. {
  249.     ASSERT(::IsWindow(m_hWnd));
  250.  
  251.     if (m_pCtrlSite == NULL)
  252.         return ::GetWindowTextLength(m_hWnd);
  253.     else
  254.         return m_pCtrlSite->GetWindowTextLength();
  255. }
  256.  
  257. int CWnd::GetDlgCtrlID() const
  258. {
  259.     ASSERT(::IsWindow(m_hWnd));
  260.  
  261.     if (m_pCtrlSite == NULL)
  262.         return ::GetDlgCtrlID(m_hWnd);
  263.     else
  264.         return m_pCtrlSite->GetDlgCtrlID();
  265. }
  266.  
  267. int CWnd::SetDlgCtrlID(int nID)
  268. {
  269.     ASSERT(::IsWindow(m_hWnd));
  270.  
  271.     if (m_pCtrlSite == NULL)
  272.         return (int)::SetWindowLong(m_hWnd, GWL_ID, nID);
  273.     else
  274.         return m_pCtrlSite->SetDlgCtrlID(nID);
  275. }
  276.  
  277. void CWnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint)
  278. {
  279.     ASSERT(::IsWindow(m_hWnd));
  280.  
  281.     if (m_pCtrlSite == NULL)
  282.         ::MoveWindow(m_hWnd, x, y, nWidth, nHeight, bRepaint);
  283.     else
  284.         m_pCtrlSite->MoveWindow(x, y, nWidth, nHeight, bRepaint);
  285. }
  286.  
  287. BOOL CWnd::SetWindowPos(const CWnd* pWndInsertAfter, int x, int y, int cx,
  288.     int cy, UINT nFlags)
  289. {
  290.     ASSERT(::IsWindow(m_hWnd));
  291.  
  292.     if (m_pCtrlSite == NULL)
  293.         return ::SetWindowPos(m_hWnd, pWndInsertAfter->GetSafeHwnd(),
  294.             x, y, cx, cy, nFlags);
  295.     else
  296.         return m_pCtrlSite->SetWindowPos(pWndInsertAfter, x, y, cx, cy, nFlags);
  297. }
  298.  
  299. BOOL CWnd::ShowWindow(int nCmdShow)
  300. {
  301.     ASSERT(::IsWindow(m_hWnd));
  302.  
  303.     if (m_pCtrlSite == NULL)
  304.         return ::ShowWindow(m_hWnd, nCmdShow);
  305.     else
  306.         return m_pCtrlSite->ShowWindow(nCmdShow);
  307. }
  308.  
  309. BOOL CWnd::IsWindowEnabled() const
  310. {
  311.     ASSERT(::IsWindow(m_hWnd));
  312.  
  313.     if (m_pCtrlSite == NULL)
  314.         return ::IsWindowEnabled(m_hWnd);
  315.     else
  316.         return m_pCtrlSite->IsWindowEnabled();
  317. }
  318.  
  319. BOOL CWnd::EnableWindow(BOOL bEnable)
  320. {
  321.     ASSERT(::IsWindow(m_hWnd));
  322.  
  323.     if (m_pCtrlSite == NULL)
  324.         return ::EnableWindow(m_hWnd, bEnable);
  325.     else
  326.         return m_pCtrlSite->EnableWindow(bEnable);
  327. }
  328.  
  329. CWnd* CWnd::SetFocus()
  330. {
  331.     ASSERT(::IsWindow(m_hWnd));
  332.  
  333.     if (m_pCtrlSite == NULL)
  334.         return CWnd::FromHandle(::SetFocus(m_hWnd));
  335.     else
  336.         return m_pCtrlSite->SetFocus();
  337. }
  338.  
  339. /////////////////////////////////////////////////////////////////////////////
  340. // CWnd invoke helpers for OLE Control wrappers
  341.  
  342. void AFX_CDECL CWnd::InvokeHelper(DISPID dwDispID, WORD wFlags, VARTYPE vtRet,
  343.     void* pvRet, const BYTE* pbParamInfo, ...)
  344. {
  345.     ASSERT(m_pCtrlSite != NULL);    // not an OLE control (not yet, at least)
  346.  
  347.     if (m_pCtrlSite == NULL)
  348.         return;
  349.  
  350.     va_list argList;
  351.     va_start(argList, pbParamInfo);
  352.     m_pCtrlSite->InvokeHelperV(dwDispID, wFlags, vtRet, pvRet, pbParamInfo,
  353.         argList);
  354.     va_end(argList);
  355. }
  356.  
  357. void CWnd::GetProperty(DISPID dwDispID, VARTYPE vtProp,
  358.     void* pvProp) const
  359. {
  360.     ASSERT(m_pCtrlSite != NULL);    // not an OLE control (not yet, at least)
  361.  
  362.     if (m_pCtrlSite == NULL)
  363.         return;
  364.  
  365.     const_cast<CWnd*>(this)->InvokeHelper(dwDispID, DISPATCH_PROPERTYGET,
  366.         vtProp, pvProp, NULL);
  367. }
  368.  
  369. void AFX_CDECL CWnd::SetProperty(DISPID dwDispID, VARTYPE vtProp, ...)
  370. {
  371.     ASSERT(m_pCtrlSite != NULL);    // not an OLE control (not yet, at least)
  372.  
  373.     if (m_pCtrlSite == NULL)
  374.         return;
  375.  
  376.     va_list argList;    // really only one arg, but...
  377.     va_start(argList, vtProp);
  378.     m_pCtrlSite->SetPropertyV(dwDispID, vtProp, argList);
  379.     va_end(argList);
  380. }
  381.  
  382. IUnknown* CWnd::GetDSCCursor()
  383. {
  384.     ASSERT(m_pCtrlSite != NULL);    // not an OLE control (not yet, at least)
  385.     if (m_pCtrlSite == NULL)
  386.         return NULL;
  387.  
  388.     m_pCtrlSite->EnableDSC();
  389.  
  390.     IUnknown* pCursor = m_pCtrlSite->m_pDataSourceControl->GetCursor();
  391.     ASSERT(pCursor != NULL);  // data source control has no cursor
  392.  
  393.     return pCursor;
  394. }
  395.  
  396. void CWnd::BindDefaultProperty(DISPID dwDispID, VARTYPE vtProp, LPCTSTR szFieldName, CWnd* pDSCWnd)
  397. {
  398.     ASSERT(m_pCtrlSite != NULL); // not an OLE control (not yet, at least)
  399.     m_pCtrlSite->BindDefaultProperty(dwDispID, vtProp, szFieldName, pDSCWnd);
  400. }
  401.  
  402. void CWnd::BindProperty(DISPID dwDispId, CWnd* pWndDSC)
  403. {
  404.     ASSERT(m_pCtrlSite != NULL); // not an OLE control (not yet, at least)
  405.     m_pCtrlSite->BindProperty(dwDispId, pWndDSC);
  406. }
  407.  
  408. /////////////////////////////////////////////////////////////////////////////
  409. // CWnd implementation helpers
  410.  
  411. void CWnd::AttachControlSite(CHandleMap* pMap)
  412. {
  413.     if (this != NULL && m_pCtrlSite == NULL)
  414.     {
  415.         // Determine if parent is an OLE control container
  416.         CWnd* pWndParent = (CWnd*)pMap->LookupPermanent(::GetParent(m_hWnd));
  417.         if (pWndParent != NULL && pWndParent->m_pCtrlCont != NULL)
  418.         {
  419.             // delegate through helper in COleControlSite
  420.             pWndParent->m_pCtrlCont->AttachControlSite(this);
  421.         }
  422.     }
  423. }
  424.  
  425. void CWnd::AttachControlSite(CWnd* pWndParent)
  426. {
  427.     ASSERT(this != NULL);
  428.     ASSERT(pWndParent != NULL);
  429.  
  430.     if (m_pCtrlSite == NULL && pWndParent->m_pCtrlCont != NULL)
  431.     {
  432.         // delegate through helper in COleControlSite
  433.         pWndParent->m_pCtrlCont->AttachControlSite(this);
  434.     }
  435. }
  436.  
  437. void COleControlContainer::AttachControlSite(CWnd* pWnd)
  438. {
  439.     ASSERT(this != NULL);
  440.     ASSERT(pWnd != NULL);
  441.  
  442.     // If a matching control site exists, it's an OLE control
  443.     COleControlSite* pSite = (COleControlSite*)m_siteMap.GetValueAt(pWnd->m_hWnd);
  444.     if (pSite != NULL)
  445.     {
  446.         // detach any existing CWnd from this site (last one wins)
  447.         CWnd* pOldCtrl = pSite->m_pWndCtrl;
  448.         if (pOldCtrl != NULL && pOldCtrl->m_pCtrlSite == pSite)
  449.             pOldCtrl->m_pCtrlSite = NULL;
  450.  
  451.         // now wire the site and CWnd together
  452.         pWnd->m_pCtrlSite = pSite;
  453.         pSite->m_pWndCtrl = pWnd;
  454.     }
  455. }
  456.  
  457. #endif // !_AFX_NO_OCC_SUPPORT
  458.