UIVerticalLayout.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include "StdAfx.h"
  2. #include "UIVerticalLayout.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(CVerticalLayoutUI)
  6. CVerticalLayoutUI::CVerticalLayoutUI() : m_iSepHeight(0), m_uButtonState(0), m_bImmMode(false)
  7. {
  8. ptLastMouse.x = ptLastMouse.y = 0;
  9. ::ZeroMemory(&m_rcNewPos, sizeof(m_rcNewPos));
  10. }
  11. LPCTSTR CVerticalLayoutUI::GetClass() const
  12. {
  13. return _T("VerticalLayoutUI");
  14. }
  15. LPVOID CVerticalLayoutUI::GetInterface(LPCTSTR pstrName)
  16. {
  17. if( _tcsicmp(pstrName, DUI_CTR_VERTICALLAYOUT) == 0 ) return static_cast<CVerticalLayoutUI*>(this);
  18. return CContainerUI::GetInterface(pstrName);
  19. }
  20. UINT CVerticalLayoutUI::GetControlFlags() const
  21. {
  22. if( IsEnabled() && m_iSepHeight != 0 ) return UIFLAG_SETCURSOR;
  23. else return 0;
  24. }
  25. void CVerticalLayoutUI::SetPos(RECT rc, bool bNeedInvalidate)
  26. {
  27. CControlUI::SetPos(rc, bNeedInvalidate);
  28. rc = m_rcItem;
  29. // Adjust for inset
  30. RECT rcInset = GetInset();
  31. rc.left += rcInset.left;
  32. rc.top += rcInset.top;
  33. rc.right -= rcInset.right;
  34. rc.bottom -= rcInset.bottom;
  35. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) rc.right -= m_pVerticalScrollBar->GetFixedWidth();
  36. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) rc.bottom -= m_pHorizontalScrollBar->GetFixedHeight();
  37. if( m_items.GetSize() == 0) {
  38. ProcessScrollBar(rc, 0, 0);
  39. return;
  40. }
  41. int iChildPadding = GetChildPadding();
  42. // Determine the minimum size
  43. SIZE szAvailable = { rc.right - rc.left, rc.bottom - rc.top };
  44. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() )
  45. szAvailable.cx += m_pHorizontalScrollBar->GetScrollRange();
  46. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() )
  47. szAvailable.cy += m_pVerticalScrollBar->GetScrollRange();
  48. int cxNeeded = 0;
  49. int nAdjustables = 0;
  50. int cyFixed = 0;
  51. int nEstimateNum = 0;
  52. SIZE szControlAvailable;
  53. int iControlMaxWidth = 0;
  54. int iControlMaxHeight = 0;
  55. for( int it1 = 0; it1 < m_items.GetSize(); it1++ ) {
  56. CControlUI* pControl = static_cast<CControlUI*>(m_items[it1]);
  57. if( !pControl->IsVisible() ) continue;
  58. if( pControl->IsFloat() ) continue;
  59. szControlAvailable = szAvailable;
  60. RECT rcPadding = pControl->GetPadding();
  61. szControlAvailable.cx -= rcPadding.left + rcPadding.right;
  62. iControlMaxWidth = pControl->GetFixedWidth();
  63. iControlMaxHeight = pControl->GetFixedHeight();
  64. if (iControlMaxWidth <= 0) iControlMaxWidth = pControl->GetMaxWidth();
  65. if (iControlMaxHeight <= 0) iControlMaxHeight = pControl->GetMaxHeight();
  66. if (szControlAvailable.cx > iControlMaxWidth) szControlAvailable.cx = iControlMaxWidth;
  67. if (szControlAvailable.cy > iControlMaxHeight) szControlAvailable.cy = iControlMaxHeight;
  68. SIZE sz = pControl->EstimateSize(szControlAvailable);
  69. if( sz.cy == 0 ) {
  70. nAdjustables++;
  71. }
  72. else {
  73. if( sz.cy < pControl->GetMinHeight() ) sz.cy = pControl->GetMinHeight();
  74. if( sz.cy > pControl->GetMaxHeight() ) sz.cy = pControl->GetMaxHeight();
  75. }
  76. cyFixed += sz.cy + pControl->GetPadding().top + pControl->GetPadding().bottom;
  77. sz.cx = MAX(sz.cx, 0);
  78. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  79. if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
  80. cxNeeded = MAX(cxNeeded, sz.cx + rcPadding.left + rcPadding.right);
  81. nEstimateNum++;
  82. }
  83. cyFixed += (nEstimateNum - 1) * iChildPadding;
  84. // Place elements
  85. int cyNeeded = 0;
  86. int cyExpand = 0;
  87. if( nAdjustables > 0 ) cyExpand = MAX(0, (szAvailable.cy - cyFixed) / nAdjustables);
  88. // Position the elements
  89. SIZE szRemaining = szAvailable;
  90. int iPosY = rc.top;
  91. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) {
  92. iPosY -= m_pVerticalScrollBar->GetScrollPos();
  93. }
  94. else {
  95. // ×ӿؼþ´¹Ö±¶ÔÆ䷽ʽ
  96. if(nAdjustables <= 0) {
  97. UINT iChildAlign = GetChildVAlign();
  98. if (iChildAlign == DT_VCENTER) {
  99. iPosY += (szAvailable.cy -cyFixed) / 2;
  100. }
  101. else if (iChildAlign == DT_BOTTOM) {
  102. iPosY += (szAvailable.cy - cyFixed);
  103. }
  104. }
  105. }
  106. int iEstimate = 0;
  107. int iAdjustable = 0;
  108. int cyFixedRemaining = cyFixed;
  109. for( int it2 = 0; it2 < m_items.GetSize(); it2++ ) {
  110. CControlUI* pControl = static_cast<CControlUI*>(m_items[it2]);
  111. if( !pControl->IsVisible() ) continue;
  112. if( pControl->IsFloat() ) {
  113. SetFloatPos(it2);
  114. continue;
  115. }
  116. iEstimate += 1;
  117. RECT rcPadding = pControl->GetPadding();
  118. szRemaining.cy -= rcPadding.top;
  119. szControlAvailable = szRemaining;
  120. szControlAvailable.cx -= rcPadding.left + rcPadding.right;
  121. iControlMaxWidth = pControl->GetFixedWidth();
  122. iControlMaxHeight = pControl->GetFixedHeight();
  123. if (iControlMaxWidth <= 0) iControlMaxWidth = pControl->GetMaxWidth();
  124. if (iControlMaxHeight <= 0) iControlMaxHeight = pControl->GetMaxHeight();
  125. if (szControlAvailable.cx > iControlMaxWidth) szControlAvailable.cx = iControlMaxWidth;
  126. if (szControlAvailable.cy > iControlMaxHeight) szControlAvailable.cy = iControlMaxHeight;
  127. cyFixedRemaining = cyFixedRemaining - (rcPadding.top + rcPadding.bottom);
  128. if (iEstimate > 1) cyFixedRemaining = cyFixedRemaining - iChildPadding;
  129. SIZE sz = pControl->EstimateSize(szControlAvailable);
  130. if( sz.cy == 0 ) {
  131. iAdjustable++;
  132. sz.cy = cyExpand;
  133. // Distribute remaining to last element (usually round-off left-overs)
  134. if( iAdjustable == nAdjustables ) {
  135. sz.cy = MAX(0, szRemaining.cy - rcPadding.bottom - cyFixedRemaining);
  136. }
  137. if( sz.cy < pControl->GetMinHeight() ) sz.cy = pControl->GetMinHeight();
  138. if( sz.cy > pControl->GetMaxHeight() ) sz.cy = pControl->GetMaxHeight();
  139. }
  140. else {
  141. if( sz.cy < pControl->GetMinHeight() ) sz.cy = pControl->GetMinHeight();
  142. if( sz.cy > pControl->GetMaxHeight() ) sz.cy = pControl->GetMaxHeight();
  143. cyFixedRemaining -= sz.cy;
  144. }
  145. sz.cx = MAX(sz.cx, 0);
  146. if( sz.cx == 0 ) sz.cx = szAvailable.cx - rcPadding.left - rcPadding.right;
  147. if( sz.cx > szControlAvailable.cx ) sz.cx = szControlAvailable.cx;
  148. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  149. UINT iChildAlign = GetChildAlign();
  150. if (iChildAlign == DT_CENTER) {
  151. int iPosX = (rc.right + rc.left) / 2;
  152. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) {
  153. iPosX += m_pHorizontalScrollBar->GetScrollRange() / 2;
  154. iPosX -= m_pHorizontalScrollBar->GetScrollPos();
  155. }
  156. RECT rcCtrl = { iPosX - sz.cx/2, iPosY + rcPadding.top, iPosX + sz.cx - sz.cx/2, iPosY + sz.cy + rcPadding.top };
  157. pControl->SetPos(rcCtrl, false);
  158. }
  159. else if (iChildAlign == DT_RIGHT) {
  160. int iPosX = rc.right;
  161. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) {
  162. iPosX += m_pHorizontalScrollBar->GetScrollRange();
  163. iPosX -= m_pHorizontalScrollBar->GetScrollPos();
  164. }
  165. RECT rcCtrl = { iPosX - rcPadding.right - sz.cx, iPosY + rcPadding.top, iPosX - rcPadding.right, iPosY + sz.cy + rcPadding.top };
  166. pControl->SetPos(rcCtrl, false);
  167. }
  168. else {
  169. int iPosX = rc.left;
  170. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) {
  171. iPosX -= m_pHorizontalScrollBar->GetScrollPos();
  172. }
  173. RECT rcCtrl = { iPosX + rcPadding.left, iPosY + rcPadding.top, iPosX + rcPadding.left + sz.cx, iPosY + sz.cy + rcPadding.top };
  174. pControl->SetPos(rcCtrl, false);
  175. }
  176. iPosY += sz.cy + iChildPadding + rcPadding.top + rcPadding.bottom;
  177. cyNeeded += sz.cy + rcPadding.top + rcPadding.bottom;
  178. szRemaining.cy -= sz.cy + iChildPadding + rcPadding.bottom;
  179. }
  180. cyNeeded += (nEstimateNum - 1) * iChildPadding;
  181. // Process the scrollbar
  182. ProcessScrollBar(rc, cxNeeded, cyNeeded);
  183. }
  184. void CVerticalLayoutUI::DoPostPaint(HDC hDC, const RECT& rcPaint)
  185. {
  186. if( (m_uButtonState & UISTATE_CAPTURED) != 0 && !m_bImmMode ) {
  187. RECT rcSeparator = GetThumbRect(true);
  188. CRenderEngine::DrawColor(hDC, rcSeparator, 0xAA000000);
  189. }
  190. }
  191. void CVerticalLayoutUI::SetSepHeight(int iHeight)
  192. {
  193. m_iSepHeight = iHeight;
  194. }
  195. int CVerticalLayoutUI::GetSepHeight() const
  196. {
  197. if(m_pManager != NULL) m_pManager->GetDPIObj()->Scale(m_iSepHeight);
  198. return m_iSepHeight;
  199. }
  200. void CVerticalLayoutUI::SetSepImmMode(bool bImmediately)
  201. {
  202. if( m_bImmMode == bImmediately ) return;
  203. if( (m_uButtonState & UISTATE_CAPTURED) != 0 && !m_bImmMode && m_pManager != NULL ) {
  204. m_pManager->RemovePostPaint(this);
  205. }
  206. m_bImmMode = bImmediately;
  207. }
  208. bool CVerticalLayoutUI::IsSepImmMode() const
  209. {
  210. return m_bImmMode;
  211. }
  212. void CVerticalLayoutUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  213. {
  214. if( _tcsicmp(pstrName, _T("sepheight")) == 0 ) SetSepHeight(_ttoi(pstrValue));
  215. else if( _tcsicmp(pstrName, _T("sepimm")) == 0 ) SetSepImmMode(_tcsicmp(pstrValue, _T("true")) == 0);
  216. else CContainerUI::SetAttribute(pstrName, pstrValue);
  217. }
  218. void CVerticalLayoutUI::DoEvent(TEventUI& event)
  219. {
  220. if( m_iSepHeight != 0 ) {
  221. if( event.Type == UIEVENT_BUTTONDOWN && IsEnabled() )
  222. {
  223. RECT rcSeparator = GetThumbRect(false);
  224. if( ::PtInRect(&rcSeparator, event.ptMouse) ) {
  225. m_uButtonState |= UISTATE_CAPTURED;
  226. ptLastMouse = event.ptMouse;
  227. m_rcNewPos = m_rcItem;
  228. if( !m_bImmMode && m_pManager ) m_pManager->AddPostPaint(this);
  229. return;
  230. }
  231. }
  232. if( event.Type == UIEVENT_BUTTONUP )
  233. {
  234. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  235. m_uButtonState &= ~UISTATE_CAPTURED;
  236. m_rcItem = m_rcNewPos;
  237. if( !m_bImmMode && m_pManager ) m_pManager->RemovePostPaint(this);
  238. NeedParentUpdate();
  239. return;
  240. }
  241. }
  242. if( event.Type == UIEVENT_MOUSEMOVE )
  243. {
  244. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  245. LONG cy = event.ptMouse.y - ptLastMouse.y;
  246. ptLastMouse = event.ptMouse;
  247. RECT rc = m_rcNewPos;
  248. if( m_iSepHeight >= 0 ) {
  249. if( cy > 0 && event.ptMouse.y < m_rcNewPos.bottom + m_iSepHeight ) return;
  250. if( cy < 0 && event.ptMouse.y > m_rcNewPos.bottom ) return;
  251. rc.bottom += cy;
  252. if( rc.bottom - rc.top <= GetMinHeight() ) {
  253. if( m_rcNewPos.bottom - m_rcNewPos.top <= GetMinHeight() ) return;
  254. rc.bottom = rc.top + GetMinHeight();
  255. }
  256. if( rc.bottom - rc.top >= GetMaxHeight() ) {
  257. if( m_rcNewPos.bottom - m_rcNewPos.top >= GetMaxHeight() ) return;
  258. rc.bottom = rc.top + GetMaxHeight();
  259. }
  260. }
  261. else {
  262. if( cy > 0 && event.ptMouse.y < m_rcNewPos.top ) return;
  263. if( cy < 0 && event.ptMouse.y > m_rcNewPos.top + m_iSepHeight ) return;
  264. rc.top += cy;
  265. if( rc.bottom - rc.top <= GetMinHeight() ) {
  266. if( m_rcNewPos.bottom - m_rcNewPos.top <= GetMinHeight() ) return;
  267. rc.top = rc.bottom - GetMinHeight();
  268. }
  269. if( rc.bottom - rc.top >= GetMaxHeight() ) {
  270. if( m_rcNewPos.bottom - m_rcNewPos.top >= GetMaxHeight() ) return;
  271. rc.top = rc.bottom - GetMaxHeight();
  272. }
  273. }
  274. CDuiRect rcInvalidate = GetThumbRect(true);
  275. m_rcNewPos = rc;
  276. m_cxyFixed.cy = GetManager()->GetDPIObj()->Scale(m_rcNewPos.bottom - m_rcNewPos.top);
  277. if( m_bImmMode ) {
  278. m_rcItem = m_rcNewPos;
  279. NeedParentUpdate();
  280. }
  281. else {
  282. rcInvalidate.Join(GetThumbRect(true));
  283. rcInvalidate.Join(GetThumbRect(false));
  284. if( m_pManager ) m_pManager->Invalidate(rcInvalidate);
  285. }
  286. return;
  287. }
  288. }
  289. if( event.Type == UIEVENT_SETCURSOR )
  290. {
  291. RECT rcSeparator = GetThumbRect(false);
  292. if( IsEnabled() && ::PtInRect(&rcSeparator, event.ptMouse) ) {
  293. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
  294. return;
  295. }
  296. }
  297. }
  298. CContainerUI::DoEvent(event);
  299. }
  300. RECT CVerticalLayoutUI::GetThumbRect(bool bUseNew) const
  301. {
  302. if( (m_uButtonState & UISTATE_CAPTURED) != 0 && bUseNew) {
  303. if( m_iSepHeight >= 0 )
  304. return CDuiRect(m_rcNewPos.left, MAX(m_rcNewPos.bottom - m_iSepHeight, m_rcNewPos.top),
  305. m_rcNewPos.right, m_rcNewPos.bottom);
  306. else
  307. return CDuiRect(m_rcNewPos.left, m_rcNewPos.top, m_rcNewPos.right,
  308. MIN(m_rcNewPos.top - m_iSepHeight, m_rcNewPos.bottom));
  309. }
  310. else {
  311. if( m_iSepHeight >= 0 )
  312. return CDuiRect(m_rcItem.left, MAX(m_rcItem.bottom - m_iSepHeight, m_rcItem.top), m_rcItem.right,
  313. m_rcItem.bottom);
  314. else
  315. return CDuiRect(m_rcItem.left, m_rcItem.top, m_rcItem.right,
  316. MIN(m_rcItem.top - m_iSepHeight, m_rcItem.bottom));
  317. }
  318. }
  319. }