ResizableLayout.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // ResizableLayout.cpp: implementation of the CResizableLayout class.
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. // Copyright (C) 2000-2001 by Paolo Messina
  6. // (http://www.geocities.com/ppescher - ppescher@yahoo.com)
  7. //
  8. // The contents of this file are subject to the Artistic License (the "License").
  9. // You may not use this file except in compliance with the License.
  10. // You may obtain a copy of the License at:
  11. // http://www.opensource.org/licenses/artistic-license.html
  12. //
  13. // If you find this code useful, credits would be nice!
  14. //
  15. /////////////////////////////////////////////////////////////////////////////
  16. #include "stdafx.h"
  17. #include "ResizableLayout.h"
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[]=__FILE__;
  21. #define new DEBUG_NEW
  22. #endif
  23. //////////////////////////////////////////////////////////////////////
  24. // Construction/Destruction
  25. //////////////////////////////////////////////////////////////////////
  26. #define BTS_TYPEMASK SS_TYPEMASK
  27. void CResizableLayout::AddAnchor(HWND hWnd, CSize sizeTypeTL, CSize sizeTypeBR)
  28. {
  29. CWnd* pParent = GetResizableWnd();
  30. // child window must be valid
  31. ASSERT(::IsWindow(hWnd));
  32. // must be child of parent window
  33. ASSERT(::IsChild(pParent->GetSafeHwnd(), hWnd));
  34. // top-left anchor must be valid
  35. ASSERT(sizeTypeTL != NOANCHOR);
  36. // get control's window class
  37. CString st;
  38. GetClassName(hWnd, st.GetBufferSetLength(MAX_PATH), MAX_PATH);
  39. st.ReleaseBuffer();
  40. // add the style 'clipsiblings' to a GroupBox
  41. // to avoid unnecessary repainting of controls inside
  42. if (st == "Button")
  43. {
  44. DWORD style = GetWindowLong(hWnd, GWL_STYLE);
  45. if ((style & BTS_TYPEMASK) == BS_GROUPBOX)
  46. SetWindowLong(hWnd, GWL_STYLE, style | WS_CLIPSIBLINGS);
  47. }
  48. // window classes that don't redraw client area correctly
  49. // when the hor scroll pos changes due to a resizing
  50. BOOL bHScroll = FALSE;
  51. if (st == "ListBox")
  52. bHScroll = TRUE;
  53. // window classes that need refresh when resized
  54. BOOL bRefresh = NeedsRefresh(hWnd);
  55. // get parent window's rect
  56. CRect rectParent;
  57. GetTotalClientRect(&rectParent);
  58. // and child control's rect
  59. CRect rectChild;
  60. ::GetWindowRect(hWnd, &rectChild);
  61. pParent->ScreenToClient(&rectChild);
  62. // go calculate margins
  63. CSize sizeMarginTL, sizeMarginBR;
  64. if (sizeTypeBR == NOANCHOR)
  65. sizeTypeBR = sizeTypeTL;
  66. // calculate margin for the top-left corner
  67. sizeMarginTL.cx = rectChild.left - rectParent.Width() * sizeTypeTL.cx / 100;
  68. sizeMarginTL.cy = rectChild.top - rectParent.Height() * sizeTypeTL.cy / 100;
  69. // calculate margin for the bottom-right corner
  70. sizeMarginBR.cx = rectChild.right - rectParent.Width() * sizeTypeBR.cx / 100;
  71. sizeMarginBR.cy = rectChild.bottom - rectParent.Height() * sizeTypeBR.cy / 100;
  72. // add to the list
  73. LayoutInfo layout(hWnd, sizeTypeTL, sizeMarginTL,
  74. sizeTypeBR, sizeMarginBR, bHScroll, bRefresh);
  75. // always add before callbacks
  76. m_arrLayout.InsertAt(m_iFirstCallback++, layout);
  77. }
  78. // one callback control cannot rely upon another callback control's
  79. // size and/or position (they're updated all together at the end)
  80. void CResizableLayout::AddAnchorCallback(UINT nCallbackID)
  81. {
  82. // add to the list
  83. LayoutInfo layout;
  84. layout.nCallbackID = nCallbackID;
  85. // callbacks added to tail (for efficiency)
  86. m_arrLayout.Add(layout);
  87. }
  88. BOOL CResizableLayout::ArrangeLayoutCallback(LayoutInfo& /*layout*/)
  89. {
  90. ASSERT(FALSE);
  91. // must be overridden, if callback is used
  92. return FALSE; // no output data
  93. }
  94. void CResizableLayout::ArrangeLayout()
  95. {
  96. CWnd* pParent = GetResizableWnd();
  97. // get parent window's rect
  98. CRect rectParent;
  99. GetTotalClientRect(&rectParent);
  100. // init some vars
  101. int i, count = m_arrLayout.GetSize();
  102. HDWP hdwp = BeginDeferWindowPos(count);
  103. for (i=0; i<count; ++i)
  104. {
  105. LayoutInfo layout = m_arrLayout[i];
  106. if (layout.hWnd == NULL) // callback
  107. {
  108. if (i == m_iFirstCallback) // first time only
  109. {
  110. // update previous controls
  111. EndDeferWindowPos(hdwp);
  112. // start again for callback controls
  113. hdwp = BeginDeferWindowPos(count-i);
  114. }
  115. // callbacks are added at the end, so that
  116. // you don't have multiple screen updates
  117. if (!ArrangeLayoutCallback(layout)) // request data
  118. continue;
  119. }
  120. CRect rectChild, newrc;
  121. CWnd* pWnd = CWnd::FromHandle(layout.hWnd); // temporary solution
  122. pWnd->GetWindowRect(&rectChild);
  123. pParent->ScreenToClient(&rectChild);
  124. // calculate new top-left corner
  125. newrc.left = layout.sizeMarginTL.cx + rectParent.Width() * layout.sizeTypeTL.cx / 100;
  126. newrc.top = layout.sizeMarginTL.cy + rectParent.Height() * layout.sizeTypeTL.cy / 100;
  127. // calculate new bottom-right corner
  128. newrc.right = layout.sizeMarginBR.cx + rectParent.Width() * layout.sizeTypeBR.cx / 100;
  129. newrc.bottom = layout.sizeMarginBR.cy + rectParent.Height() * layout.sizeTypeBR.cy / 100;
  130. if (!newrc.EqualRect(&rectChild))
  131. {
  132. if (layout.bAdjHScroll)
  133. {
  134. // needs repainting, due to horiz scrolling
  135. int diff = newrc.Width() - rectChild.Width();
  136. int max = pWnd->GetScrollLimit(SB_HORZ);
  137. layout.bNeedRefresh = FALSE;
  138. if (max > 0 && pWnd->GetScrollPos(SB_HORZ) > max - diff)
  139. {
  140. layout.bNeedRefresh = TRUE;
  141. }
  142. }
  143. // set flags
  144. DWORD flags = SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREPOSITION;
  145. if (layout.bNeedRefresh)
  146. flags |= SWP_NOCOPYBITS;
  147. if (newrc.TopLeft() == rectChild.TopLeft())
  148. flags |= SWP_NOMOVE;
  149. if (newrc.Size() == rectChild.Size())
  150. flags |= SWP_NOSIZE;
  151. hdwp = DeferWindowPos(hdwp, layout.hWnd, NULL, newrc.left, newrc.top,
  152. newrc.Width(), newrc.Height(), flags);
  153. }
  154. }
  155. // go re-arrange child windows
  156. EndDeferWindowPos(hdwp);
  157. }
  158. void CResizableLayout::EnumAndClipChildWindow(HWND hWnd, CDC* pDC)
  159. {
  160. // obtain window position
  161. CRect rect;
  162. ::GetWindowRect(hWnd, &rect);
  163. GetResizableWnd()->ScreenToClient(&rect);
  164. pDC->DPtoLP(&rect);
  165. // use window region if any
  166. CRgn rgn;
  167. rgn.CreateRectRgn(0,0,0,0);
  168. if (COMPLEXREGION == ::GetWindowRgn(hWnd, rgn))
  169. {
  170. rgn.OffsetRgn(rect.TopLeft());
  171. }
  172. else
  173. {
  174. rgn.SetRectRgn(&rect);
  175. }
  176. // go clipping?
  177. if (LikesClipping(hWnd))
  178. pDC->SelectClipRgn(&rgn, RGN_DIFF);
  179. else
  180. pDC->SelectClipRgn(&rgn, RGN_OR);
  181. }
  182. void CResizableLayout::ClipChildren(CDC *pDC)
  183. {
  184. // only clips anchored controls
  185. for (int i=0; i<m_arrLayout.GetSize(); ++i)
  186. {
  187. HWND hWnd = m_arrLayout[i].hWnd;
  188. if (hWnd != NULL && ::IsWindowVisible(hWnd))
  189. EnumAndClipChildWindow(m_arrLayout[i].hWnd, pDC);
  190. }
  191. }
  192. void CResizableLayout::GetTotalClientRect(LPRECT lpRect)
  193. {
  194. GetResizableWnd()->GetClientRect(lpRect);
  195. }
  196. BOOL CResizableLayout::NeedsRefresh(HWND hWnd)
  197. {
  198. // get window class
  199. CString st;
  200. GetClassName(hWnd, st.GetBufferSetLength(MAX_PATH), MAX_PATH);
  201. st.ReleaseBuffer();
  202. // optimistic, no need to refresh
  203. BOOL bRefresh = FALSE;
  204. // window classes that need refresh when resized
  205. if (st == "Static")
  206. {
  207. DWORD style = GetWindowLong(hWnd, GWL_STYLE);
  208. switch (style & SS_TYPEMASK)
  209. {
  210. case SS_LEFT:
  211. case SS_CENTER:
  212. case SS_RIGHT:
  213. case SS_ENHMETAFILE:
  214. case SS_BITMAP:
  215. // word-wrapped text needs bRefresh
  216. bRefresh = TRUE;
  217. break;
  218. case SS_ICON:
  219. case SS_SIMPLE:
  220. case SS_ETCHEDHORZ:
  221. case SS_ETCHEDVERT:
  222. break;
  223. default:
  224. // centered images or text need bRefresh
  225. if (style & SS_CENTERIMAGE)
  226. bRefresh = TRUE;
  227. }
  228. }
  229. return bRefresh;
  230. }
  231. BOOL CResizableLayout::LikesClipping(HWND hWnd)
  232. {
  233. // check child type
  234. CString st;
  235. GetClassName(hWnd, st.GetBufferSetLength(MAX_PATH), MAX_PATH);
  236. st.ReleaseBuffer();
  237. DWORD style = GetWindowLong(hWnd, GWL_STYLE);
  238. // skip windows that wants background repainted
  239. if (st == TOOLBARCLASSNAME && (style & TBSTYLE_TRANSPARENT))
  240. return FALSE;
  241. if (st == "Button" && (style & BTS_TYPEMASK) == BS_GROUPBOX)
  242. return FALSE;
  243. if (st == "Static")
  244. {
  245. switch (style & SS_TYPEMASK)
  246. {
  247. case SS_BLACKRECT:
  248. case SS_GRAYRECT:
  249. case SS_WHITERECT:
  250. case SS_ETCHEDHORZ:
  251. case SS_ETCHEDVERT:
  252. case SS_BITMAP:
  253. break;
  254. case SS_ICON:
  255. case SS_ENHMETAFILE:
  256. if (style & SS_CENTERIMAGE)
  257. return FALSE;
  258. break;
  259. default:
  260. return FALSE;
  261. }
  262. }
  263. // assume the others like clipping
  264. return TRUE;
  265. }