ctlnownd.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. #include "stdafx.h"
  11. #ifdef AFXCTL_CORE1_SEG
  12. #pragma code_seg(AFXCTL_CORE1_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CWindowlessDC - used by COleControl::GetDC and COleControl::ReleaseDC
  21. class CWindowlessDC : public CDC
  22. {
  23. DECLARE_DYNAMIC(CWindowlessDC)
  24. public:
  25. CWindowlessDC(HDC hDC, CPoint& pointOrigin);
  26. HDC Detach();
  27. protected:
  28. CPoint m_pointOrigin;
  29. };
  30. IMPLEMENT_DYNAMIC(CWindowlessDC, CDC)
  31. CWindowlessDC::CWindowlessDC(HDC hDC, CPoint& pointOrigin)
  32. {
  33. m_hDC = m_hAttribDC = hDC;
  34. m_pointOrigin = GetViewportOrg();
  35. SetViewportOrg(m_pointOrigin + pointOrigin);
  36. }
  37. HDC CWindowlessDC::Detach()
  38. {
  39. SetViewportOrg(m_pointOrigin);
  40. HDC hDC = m_hDC;
  41. m_hDC = m_hAttribDC = NULL;
  42. return hDC;
  43. }
  44. /////////////////////////////////////////////////////////////////////////////
  45. // Overridables used with the various windowless interfaces
  46. void COleControl::GetClientOffset(long* pdxOffset, long* pdyOffset) const
  47. {
  48. int nOffset = (m_sBorderStyle == 1) + (2 * (m_sAppearance == 1));
  49. if (nOffset > 0)
  50. {
  51. *pdxOffset = nOffset * GetSystemMetrics(SM_CXBORDER);
  52. *pdyOffset = nOffset * GetSystemMetrics(SM_CYBORDER);
  53. }
  54. else
  55. {
  56. *pdxOffset = *pdyOffset = 0;
  57. }
  58. }
  59. UINT COleControl::ParentToClient(LPCRECT lprcBounds, LPPOINT pPoint,
  60. BOOL bHitTest) const
  61. {
  62. long dxOffset;
  63. long dyOffset;
  64. GetClientOffset(&dxOffset, &dyOffset);
  65. UINT nHitTest = HTNOWHERE;
  66. if (bHitTest && ::PtInRect(lprcBounds, *pPoint))
  67. {
  68. if (dxOffset > 0)
  69. {
  70. CRect rectClient(lprcBounds);
  71. rectClient.InflateRect(-dxOffset, -dyOffset);
  72. nHitTest = rectClient.PtInRect(*pPoint) ? HTCLIENT : HTBORDER;
  73. }
  74. else
  75. {
  76. nHitTest = HTCLIENT;
  77. }
  78. }
  79. pPoint->x -= lprcBounds->left + dxOffset;
  80. pPoint->y -= lprcBounds->top + dyOffset;
  81. return nHitTest;
  82. }
  83. void COleControl::ClientToParent(LPCRECT lprcBounds, LPPOINT pPoint) const
  84. {
  85. long dxOffset;
  86. long dyOffset;
  87. GetClientOffset(&dxOffset, &dyOffset);
  88. pPoint->x += lprcBounds->left + dxOffset;
  89. pPoint->y += lprcBounds->top + dyOffset;
  90. }
  91. /////////////////////////////////////////////////////////////////////////////
  92. // Overridables for IPointerInactive methods
  93. DWORD COleControl::GetActivationPolicy()
  94. {
  95. return 0;
  96. }
  97. BOOL COleControl::OnInactiveSetCursor(LPCRECT lprcBounds, long x, long y,
  98. DWORD dwMouseMsg, BOOL bSetAlways)
  99. {
  100. CPoint point(x, y);
  101. UINT nHitTest = ParentToClient(lprcBounds, &point, TRUE);
  102. LRESULT lResult = 0;
  103. OnWndMsg(WM_SETCURSOR, nHitTest, dwMouseMsg, &lResult);
  104. if (bSetAlways && ! lResult)
  105. ::SetCursor(::LoadCursor(NULL, IDC_ARROW));
  106. return bSetAlways || lResult;
  107. }
  108. void COleControl::OnInactiveMouseMove(LPCRECT lprcBounds, long x, long y,
  109. DWORD dwKeyState)
  110. {
  111. CPoint point(x, y);
  112. ParentToClient(lprcBounds, &point);
  113. OnWndMsg(WM_MOUSEMOVE, dwKeyState, MAKELONG(point.x, point.y), NULL);
  114. }
  115. /////////////////////////////////////////////////////////////////////////////
  116. // COleControl::XPointerInactive
  117. STDMETHODIMP_(ULONG) COleControl::XPointerInactive::AddRef()
  118. {
  119. METHOD_PROLOGUE_EX_(COleControl, PointerInactive)
  120. return (ULONG)pThis->ExternalAddRef();
  121. }
  122. STDMETHODIMP_(ULONG) COleControl::XPointerInactive::Release()
  123. {
  124. METHOD_PROLOGUE_EX_(COleControl, PointerInactive)
  125. return (ULONG)pThis->ExternalRelease();
  126. }
  127. STDMETHODIMP COleControl::XPointerInactive::QueryInterface(
  128. REFIID iid, LPVOID* ppvObj)
  129. {
  130. METHOD_PROLOGUE_EX_(COleControl, PointerInactive)
  131. return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  132. }
  133. STDMETHODIMP COleControl::XPointerInactive::GetActivationPolicy(
  134. DWORD* pdwPolicy)
  135. {
  136. METHOD_PROLOGUE_EX_(COleControl, PointerInactive)
  137. *pdwPolicy = pThis->GetActivationPolicy();
  138. return S_OK;
  139. }
  140. STDMETHODIMP COleControl::XPointerInactive::OnInactiveSetCursor(
  141. LPCRECT lprcBounds, long x, long y, DWORD dwMouseMsg, BOOL bSetAlways)
  142. {
  143. METHOD_PROLOGUE_EX_(COleControl, PointerInactive)
  144. return pThis->OnInactiveSetCursor(lprcBounds, x, y, dwMouseMsg, bSetAlways) ?
  145. S_OK : S_FALSE;
  146. }
  147. STDMETHODIMP COleControl::XPointerInactive::OnInactiveMouseMove(
  148. LPCRECT lprcBounds, long x, long y, DWORD dwKeyState)
  149. {
  150. METHOD_PROLOGUE_EX_(COleControl, PointerInactive)
  151. pThis->OnInactiveMouseMove(lprcBounds, x, y, dwKeyState);
  152. return S_OK;
  153. }
  154. /////////////////////////////////////////////////////////////////////////////
  155. // Overridables for IOleInPlaceObjectWindowless methods
  156. BOOL COleControl::OnWindowlessMessage(UINT msg, WPARAM wParam, LPARAM lParam,
  157. LRESULT* plResult)
  158. {
  159. if ((msg >= WM_MOUSEFIRST && msg <= WM_MOUSELAST) || (msg == WM_CONTEXTMENU))
  160. {
  161. CPoint point(LOWORD(lParam), HIWORD(lParam));
  162. ParentToClient(m_rcPos, &point, FALSE);
  163. lParam = MAKELONG(point.x, point.y);
  164. }
  165. return OnWndMsg(msg, wParam, lParam, plResult);
  166. }
  167. IDropTarget* COleControl::GetWindowlessDropTarget()
  168. {
  169. return NULL;
  170. }
  171. /////////////////////////////////////////////////////////////////////////////
  172. // COleControl::XOleInPlaceObject (IOleInPlaceObjectWindowless methods)
  173. STDMETHODIMP COleControl::XOleInPlaceObject::OnWindowMessage(UINT msg,
  174. WPARAM wParam, LPARAM lParam, LRESULT* plResult)
  175. {
  176. METHOD_PROLOGUE_EX_(COleControl, OleInPlaceObject)
  177. return pThis->OnWindowlessMessage(msg, wParam, lParam, plResult) ?
  178. S_OK : S_FALSE;
  179. }
  180. STDMETHODIMP COleControl::XOleInPlaceObject::GetDropTarget(
  181. IDropTarget** ppDropTarget)
  182. {
  183. METHOD_PROLOGUE_EX_(COleControl, OleInPlaceObject)
  184. *ppDropTarget = pThis->GetWindowlessDropTarget();
  185. return (*ppDropTarget != NULL) ? S_OK : E_NOTIMPL;
  186. }
  187. /////////////////////////////////////////////////////////////////////////////
  188. // Cover functions for IOleInPlaceSiteWindowless methods
  189. CWnd* COleControl::SetCapture()
  190. {
  191. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  192. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  193. {
  194. CWnd* pWndPrev = GetCapture();
  195. m_pInPlaceSiteWndless->SetCapture(TRUE);
  196. return pWndPrev;
  197. }
  198. else
  199. {
  200. return CWnd::SetCapture();
  201. }
  202. }
  203. BOOL COleControl::ReleaseCapture()
  204. {
  205. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  206. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  207. return (m_pInPlaceSiteWndless->SetCapture(FALSE) == S_OK);
  208. else
  209. return ::ReleaseCapture();
  210. }
  211. CWnd* COleControl::GetCapture()
  212. {
  213. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  214. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  215. {
  216. return (m_pInPlaceSiteWndless->GetCapture() == S_OK) ?
  217. this : NULL;
  218. }
  219. else
  220. {
  221. return CWnd::GetCapture();
  222. }
  223. }
  224. CWnd* COleControl::SetFocus()
  225. {
  226. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  227. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  228. {
  229. CWnd* pWndPrev = GetFocus();
  230. m_pInPlaceSiteWndless->SetFocus(TRUE);
  231. return pWndPrev;
  232. }
  233. else
  234. {
  235. return CWnd::SetFocus();
  236. }
  237. }
  238. CWnd* COleControl::GetFocus()
  239. {
  240. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  241. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  242. {
  243. return (m_pInPlaceSiteWndless->GetFocus() == S_OK) ?
  244. this : NULL;
  245. }
  246. else
  247. {
  248. return CWnd::GetFocus();
  249. }
  250. }
  251. CDC* COleControl::GetDC(LPCRECT lprcRect, DWORD dwFlags)
  252. {
  253. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  254. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  255. {
  256. CPoint point(0, 0);
  257. ClientToParent(m_rcPos, &point);
  258. CRect rect;
  259. if (lprcRect != NULL)
  260. {
  261. rect.CopyRect(lprcRect);
  262. rect.OffsetRect(point);
  263. lprcRect = &rect;
  264. }
  265. HDC hDC;
  266. if (FAILED(m_pInPlaceSiteWndless->GetDC(lprcRect, dwFlags, &hDC)))
  267. return NULL;
  268. CDC* pDC = NULL;
  269. TRY
  270. {
  271. pDC = new CWindowlessDC(hDC, point);
  272. }
  273. CATCH_ALL(e)
  274. {
  275. m_pInPlaceSiteWndless->ReleaseDC(hDC);
  276. }
  277. END_CATCH_ALL
  278. return pDC;
  279. }
  280. else
  281. {
  282. // NOTE: can only use non-default values for these parameters when
  283. // activated windowless.
  284. ASSERT(lprcRect == NULL);
  285. ASSERT(dwFlags == OLEDC_PAINTBKGND);
  286. return CWnd::GetDC();
  287. }
  288. }
  289. BOOL COleControl::ReleaseDC(CDC* pDC)
  290. {
  291. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  292. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  293. {
  294. CWindowlessDC* pWindowlessDC = DYNAMIC_DOWNCAST(CWindowlessDC, pDC);
  295. ASSERT(pWindowlessDC != NULL);
  296. HDC hDC = pWindowlessDC->Detach();
  297. delete pWindowlessDC;
  298. return m_pInPlaceSiteWndless->ReleaseDC(hDC);
  299. }
  300. else
  301. {
  302. return CWnd::ReleaseDC(pDC);
  303. }
  304. }
  305. void COleControl::InvalidateRgn(CRgn* pRgn, BOOL bErase)
  306. {
  307. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  308. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  309. {
  310. CRgn rgn;
  311. if (pRgn != NULL)
  312. {
  313. CPoint point(0, 0);
  314. ClientToParent(m_rcPos, &point);
  315. rgn.CopyRgn(pRgn);
  316. rgn.OffsetRgn(point);
  317. }
  318. m_pInPlaceSiteWndless->InvalidateRgn(rgn, bErase);
  319. }
  320. else
  321. {
  322. CWnd::InvalidateRgn(pRgn, bErase);
  323. }
  324. }
  325. void COleControl::ScrollWindow(int xAmount, int yAmount, LPCRECT lpRect,
  326. LPCRECT lpClipRect)
  327. {
  328. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  329. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  330. {
  331. if (lpRect != NULL || lpClipRect != NULL)
  332. {
  333. CPoint point(0, 0);
  334. ClientToParent(m_rcPos, &point);
  335. CRect rect;
  336. CRect rectClip;
  337. if (lpRect != NULL)
  338. {
  339. rect.CopyRect(lpRect);
  340. rect.OffsetRect(point);
  341. lpRect = &rect;
  342. }
  343. if (lpClipRect != NULL)
  344. {
  345. rectClip.CopyRect(lpClipRect);
  346. rectClip.OffsetRect(point);
  347. lpClipRect = &rectClip;
  348. }
  349. }
  350. m_pInPlaceSiteWndless->ScrollRect(xAmount, yAmount, lpRect, lpClipRect);
  351. }
  352. else
  353. {
  354. CWnd::ScrollWindow(xAmount, yAmount, lpRect, lpClipRect);
  355. }
  356. }
  357. BOOL COleControl::ClipCaretRect(LPRECT lpRect)
  358. {
  359. BOOL bNotEmpty = FALSE;
  360. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  361. {
  362. CPoint point(0, 0);
  363. ClientToParent(m_rcPos, &point);
  364. CRect rect(lpRect);
  365. rect.OffsetRect(point);
  366. bNotEmpty = (m_pInPlaceSiteWndless->AdjustRect(rect) == S_OK);
  367. rect.OffsetRect(-point.x, -point.y);
  368. }
  369. return bNotEmpty;
  370. }
  371. /////////////////////////////////////////////////////////////////////////////
  372. // Helper functions for windowless controls
  373. void COleControl::GetClientRect(LPRECT lpRect) const
  374. {
  375. ASSERT((m_hWnd != NULL) || (m_bInPlaceSiteWndless && m_bInPlaceActive));
  376. if (m_bInPlaceSiteWndless && m_bInPlaceActive)
  377. {
  378. long dxOffset;
  379. long dyOffset;
  380. GetClientOffset(&dxOffset, &dyOffset);
  381. ::CopyRect(lpRect, m_rcPos);
  382. ::InflateRect(lpRect, -dxOffset, -dyOffset);
  383. #ifdef _DEBUG
  384. CPoint point(0, 0);
  385. ClientToParent(m_rcPos, &point);
  386. ASSERT(point.x == lpRect->left && point.y == lpRect->top);
  387. #endif
  388. ::OffsetRect(lpRect, -lpRect->left, -lpRect->top);
  389. }
  390. else if (m_hWnd != NULL)
  391. {
  392. CWnd::GetClientRect(lpRect);
  393. }
  394. else
  395. {
  396. ::SetRectEmpty(lpRect);
  397. }
  398. }