Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

460 lines
13 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 1999
  5. *
  6. * File: vwtrack.cpp
  7. *
  8. * Contents: Implementation file for CViewTracker
  9. *
  10. * History: 01-May-98 JeffRo Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #include "stdafx.h"
  14. #include "windowsx.h"
  15. #include "vwtrack.h"
  16. #include "subclass.h" // for CSubclasser
  17. IMPLEMENT_DYNAMIC (CViewTracker, CObject)
  18. // Tracker subclasser base class
  19. class CTrackingSubclasserBase : public CSubclasser
  20. {
  21. public:
  22. CTrackingSubclasserBase(CViewTracker*, HWND);
  23. virtual ~CTrackingSubclasserBase();
  24. virtual LRESULT Callback (HWND& hwnd, UINT& msg, WPARAM& wParam,
  25. LPARAM& lParam, bool& fPassMessageOn) = 0;
  26. protected:
  27. HWND const m_hwnd;
  28. CViewTracker* const m_pTracker;
  29. };
  30. // Focus window subclasser
  31. class CFocusSubclasser : public CTrackingSubclasserBase
  32. {
  33. public:
  34. CFocusSubclasser(CViewTracker*, HWND);
  35. virtual LRESULT Callback (HWND& hwnd, UINT& msg, WPARAM& wParam,
  36. LPARAM& lParam, bool& fPassMessageOn);
  37. };
  38. // View window subclasser
  39. class CViewSubclasser : public CTrackingSubclasserBase
  40. {
  41. public:
  42. CViewSubclasser(CViewTracker*, HWND);
  43. virtual LRESULT Callback (HWND& hwnd, UINT& msg, WPARAM& wParam,
  44. LPARAM& lParam, bool& fPassMessageOn);
  45. };
  46. // Frame window subclasser
  47. class CFrameSubclasser : public CTrackingSubclasserBase
  48. {
  49. public:
  50. CFrameSubclasser(CViewTracker*, HWND);
  51. virtual LRESULT Callback (HWND& hwnd, UINT& msg, WPARAM& wParam,
  52. LPARAM& lParam, bool& fPassMessageOn);
  53. };
  54. /*+-------------------------------------------------------------------------*
  55. * IsFullWindowDragEnabled
  56. *
  57. * Returns true if the user has enabled the "Show window contents while
  58. * dragging" on the Effects page of the Display Properties property sheet.
  59. *--------------------------------------------------------------------------*/
  60. static bool IsFullWindowDragEnabled ()
  61. {
  62. BOOL fEnabled;
  63. if (!SystemParametersInfo (SPI_GETDRAGFULLWINDOWS, 0, &fEnabled, 0))
  64. return (false);
  65. return (fEnabled != FALSE);
  66. }
  67. /*+-------------------------------------------------------------------------*
  68. * CViewTracker::CViewTracker
  69. *
  70. * CViewTracker ctor. This function is private so we can control how
  71. * CViewTrackers are allocated. We want to insure that they're allocated
  72. * from the heap so it's safe to "delete this".
  73. *--------------------------------------------------------------------------*/
  74. CViewTracker::CViewTracker (TRACKER_INFO& TrackerInfo)
  75. : m_fFullWindowDrag (IsFullWindowDragEnabled()),
  76. m_fRestoreClipChildrenStyle (false),
  77. m_Info (TrackerInfo),
  78. m_dc (PrepTrackedWindow (TrackerInfo.pView)),
  79. m_pFocusSubclasser (NULL),
  80. m_pViewSubclasser (NULL),
  81. m_pFrameSubclasser (NULL),
  82. m_lOriginalTrackerLeft (TrackerInfo.rectTracker.left)
  83. {
  84. DECLARE_SC (sc, _T("CViewTracker::CViewTracker"));
  85. sc = ScCheckPointers (m_Info.pView);
  86. if (sc)
  87. sc.Throw();
  88. ASSERT_VALID (m_Info.pView);
  89. // subclass the focus window to catch VK_ESCAPE
  90. HWND hwndFocus = ::GetFocus();
  91. if (hwndFocus != NULL)
  92. {
  93. m_pFocusSubclasser = new CFocusSubclasser (this, hwndFocus);
  94. if (m_pFocusSubclasser == NULL)
  95. AfxThrowMemoryException();
  96. }
  97. // subclass view window to get mouse events
  98. ASSERT(IsWindow(m_Info.pView->m_hWnd));
  99. m_pViewSubclasser = new CViewSubclasser (this, m_Info.pView->m_hWnd);
  100. if (m_pViewSubclasser == NULL)
  101. AfxThrowMemoryException();
  102. // subclass the frame window to catch WM_CANCELMODE
  103. HWND hwndFrame = m_Info.pView->GetTopLevelFrame()->GetSafeHwnd();
  104. if ((hwndFrame != NULL))
  105. {
  106. m_pFrameSubclasser = new CFrameSubclasser (this, hwndFrame);
  107. if (m_pFrameSubclasser == NULL)
  108. AfxThrowMemoryException();
  109. }
  110. // Draw initial tracker bar
  111. DrawTracker(m_Info.rectTracker);
  112. }
  113. /*+-------------------------------------------------------------------------*
  114. * CViewTracker::StartTracking
  115. *
  116. * CViewTracker factory. It allocates CViewTrackers from the heap.
  117. *--------------------------------------------------------------------------*/
  118. bool CViewTracker::StartTracking (TRACKER_INFO* pInfo)
  119. {
  120. ASSERT(pInfo != NULL);
  121. CViewTracker* pTracker = NULL;
  122. try
  123. {
  124. /*
  125. * This doesn't leak. CViewTracker ctor fills in a back-pointer
  126. * that tracks the new object. pTracker is also not dereferenced
  127. * after allocation, so it doesn't need to be checked.
  128. */
  129. pTracker = new CViewTracker(*pInfo);
  130. }
  131. catch (CException* pe)
  132. {
  133. pe->Delete();
  134. }
  135. return (pTracker != NULL);
  136. }
  137. /*+-------------------------------------------------------------------------*
  138. * CViewTracker::StopTracking
  139. *
  140. *
  141. *--------------------------------------------------------------------------*/
  142. void CViewTracker::StopTracking (BOOL bAcceptChange)
  143. {
  144. // unsubclass the windows we subclassed
  145. delete m_pFrameSubclasser;
  146. delete m_pFocusSubclasser;
  147. delete m_pViewSubclasser;
  148. // erase tracker rectangle
  149. DrawTracker (m_Info.rectTracker);
  150. // undo changes we made to the view
  151. UnprepTrackedWindow (m_Info.pView);
  152. /*
  153. * if we're continuously resizing, but the user pressed Esc, restore
  154. * the original size
  155. */
  156. if (m_fFullWindowDrag && !bAcceptChange)
  157. {
  158. m_Info.rectTracker.left = m_lOriginalTrackerLeft;
  159. bAcceptChange = true;
  160. }
  161. // notify client through callback function
  162. ASSERT(m_Info.pCallback != NULL);
  163. (*m_Info.pCallback)(&m_Info, bAcceptChange, m_fFullWindowDrag);
  164. delete this;
  165. }
  166. /*+-------------------------------------------------------------------------*
  167. * CViewTracker::Track
  168. *
  169. * Mouse movement handler for CViewTracker.
  170. *--------------------------------------------------------------------------*/
  171. void CViewTracker::Track(CPoint pt)
  172. {
  173. // if we lost the capture, terminate tracking
  174. if (CWnd::GetCapture() != m_Info.pView)
  175. {
  176. Trace (tagSplitterTracking, _T("Stopping tracking, lost capture)"));
  177. StopTracking (false);
  178. }
  179. // Apply movement limits
  180. // if outside area and pane hiding allowed, snap to area edge
  181. // else if outside bounds, snap to bounds edge
  182. if (pt.x < m_Info.rectArea.left && m_Info.bAllowLeftHide)
  183. pt.x = m_Info.rectArea.left;
  184. else if (pt.x < m_Info.rectBounds.left)
  185. pt.x = m_Info.rectBounds.left;
  186. else if (pt.x > m_Info.rectArea.right && m_Info.bAllowRightHide)
  187. pt.x = m_Info.rectArea.right;
  188. else if (pt.x > m_Info.rectBounds.right)
  189. pt.x = m_Info.rectBounds.right;
  190. // Erase and redraw tracker rect if moved
  191. if (pt.x != m_Info.rectTracker.left)
  192. {
  193. DrawTracker (m_Info.rectTracker);
  194. m_Info.rectTracker.OffsetRect (pt.x - m_Info.rectTracker.left, 0);
  195. Trace (tagSplitterTracking, _T("new tracker x=%d"), m_Info.rectTracker.left);
  196. /*
  197. * if full window drag is enabled, tell the callback the size has
  198. * changed
  199. */
  200. if (m_fFullWindowDrag)
  201. (*m_Info.pCallback)(&m_Info, true, true);
  202. DrawTracker (m_Info.rectTracker);
  203. }
  204. }
  205. /*+-------------------------------------------------------------------------*
  206. * CViewTracker::DrawTracker
  207. *
  208. *
  209. *--------------------------------------------------------------------------*/
  210. void CViewTracker::DrawTracker (CRect& rect) const
  211. {
  212. /*
  213. * we don't draw a tracker bar if we're doing full window drag
  214. */
  215. if (m_fFullWindowDrag)
  216. return;
  217. ASSERT (!rect.IsRectEmpty());
  218. ASSERT ((m_Info.pView->GetStyle() & WS_CLIPCHILDREN) == 0);
  219. // invert the brush pattern (looks just like frame window sizing)
  220. m_dc.PatBlt (rect.left, rect.top, rect.Width(), rect.Height(), PATINVERT);
  221. }
  222. /*+-------------------------------------------------------------------------*
  223. * CViewTracker::PrepTrackedWindow
  224. *
  225. * Prepares the tracked window prior to obtaining a DC for it.
  226. *--------------------------------------------------------------------------*/
  227. CWnd* CViewTracker::PrepTrackedWindow (CWnd* pView)
  228. {
  229. // make sure no updates are pending
  230. pView->UpdateWindow ();
  231. // steal capture (no need to steal focus)
  232. pView->SetCapture();
  233. // we need to draw in children, so remove clip-children while we track
  234. if (!m_fFullWindowDrag && (pView->GetStyle() & WS_CLIPCHILDREN))
  235. {
  236. pView->ModifyStyle (WS_CLIPCHILDREN, 0);
  237. m_fRestoreClipChildrenStyle = true;
  238. }
  239. return (pView);
  240. }
  241. /*+-------------------------------------------------------------------------*
  242. * CViewTracker::UnprepTrackedWindow
  243. *
  244. * "Unprepares" the tracked window prior to obtaining a DC for it.
  245. *--------------------------------------------------------------------------*/
  246. void CViewTracker::UnprepTrackedWindow (CWnd* pView)
  247. {
  248. if (m_fRestoreClipChildrenStyle)
  249. pView->ModifyStyle (0, WS_CLIPCHILDREN);
  250. ReleaseCapture();
  251. }
  252. /*+-------------------------------------------------------------------------*
  253. * CTrackingSubclasserBase::CTrackingSubclasserBase
  254. *
  255. *
  256. *--------------------------------------------------------------------------*/
  257. CTrackingSubclasserBase::CTrackingSubclasserBase (CViewTracker* pTracker, HWND hwnd)
  258. : m_hwnd (hwnd),
  259. m_pTracker (pTracker)
  260. {
  261. GetSubclassManager().SubclassWindow (m_hwnd, this);
  262. }
  263. /*+-------------------------------------------------------------------------*
  264. * CTrackingSubclasserBase::~CTrackingSubclasserBase
  265. *
  266. *
  267. *--------------------------------------------------------------------------*/
  268. CTrackingSubclasserBase::~CTrackingSubclasserBase ()
  269. {
  270. GetSubclassManager().UnsubclassWindow (m_hwnd, this);
  271. }
  272. /*+-------------------------------------------------------------------------*
  273. * CFocusSubclasser::CFocusSubclasser
  274. *
  275. *
  276. *--------------------------------------------------------------------------*/
  277. CFocusSubclasser::CFocusSubclasser (CViewTracker* pTracker, HWND hwnd)
  278. : CTrackingSubclasserBase (pTracker, hwnd)
  279. {
  280. }
  281. /*+-------------------------------------------------------------------------*
  282. * CFrameSubclasser::CFrameSubclasser
  283. *
  284. *
  285. *--------------------------------------------------------------------------*/
  286. CFrameSubclasser::CFrameSubclasser (CViewTracker* pTracker, HWND hwnd)
  287. : CTrackingSubclasserBase (pTracker, hwnd)
  288. {
  289. }
  290. /*+-------------------------------------------------------------------------*
  291. * CViewSubclasser::CViewSubclasser
  292. *
  293. *
  294. *--------------------------------------------------------------------------*/
  295. CViewSubclasser::CViewSubclasser (CViewTracker* pTracker, HWND hwnd)
  296. : CTrackingSubclasserBase (pTracker, hwnd)
  297. {
  298. }
  299. /*+-------------------------------------------------------------------------*
  300. * CFocusSubclasser::Callback
  301. *
  302. *
  303. *--------------------------------------------------------------------------*/
  304. LRESULT CFocusSubclasser::Callback (
  305. HWND& hwnd,
  306. UINT& msg,
  307. WPARAM& wParam,
  308. LPARAM& lParam,
  309. bool& fPassMessageOn)
  310. {
  311. if (((msg == WM_CHAR) && (wParam == VK_ESCAPE)) ||
  312. (msg == WM_KILLFOCUS))
  313. {
  314. #ifdef DBG
  315. if (msg == WM_CHAR)
  316. Trace (tagSplitterTracking, _T("Stopping tracking, user pressed Esc"));
  317. else
  318. Trace (tagSplitterTracking, _T("Stopping tracking, lost focus to hwnd=0x%08x"), ::GetFocus());
  319. #endif
  320. m_pTracker->StopTracking (false);
  321. fPassMessageOn = false;
  322. }
  323. return (0);
  324. }
  325. /*+-------------------------------------------------------------------------*
  326. * CFrameSubclasser::Callback
  327. *
  328. *
  329. *--------------------------------------------------------------------------*/
  330. LRESULT CFrameSubclasser::Callback (
  331. HWND& hwnd,
  332. UINT& msg,
  333. WPARAM& wParam,
  334. LPARAM& lParam,
  335. bool& fPassMessageOn)
  336. {
  337. if (msg == WM_CANCELMODE)
  338. {
  339. Trace (tagSplitterTracking, _T("Stopping tracking, got WM_CANCELMODE"));
  340. m_pTracker->StopTracking (false);
  341. }
  342. return (0);
  343. }
  344. /*+-------------------------------------------------------------------------*
  345. * CViewSubclasser::Callback
  346. *
  347. *
  348. *--------------------------------------------------------------------------*/
  349. LRESULT CViewSubclasser::Callback (
  350. HWND& hwnd,
  351. UINT& msg,
  352. WPARAM& wParam,
  353. LPARAM& lParam,
  354. bool& fPassMessageOn)
  355. {
  356. switch (msg)
  357. {
  358. case WM_MOUSEMOVE:
  359. {
  360. CPoint pt(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
  361. m_pTracker->Track (pt);
  362. }
  363. break;
  364. case WM_LBUTTONUP:
  365. Trace (tagSplitterTracking, _T("Stopping tracking, accepting new position"));
  366. m_pTracker->StopTracking (true);
  367. break;
  368. }
  369. return (0);
  370. }