UIHorizontalLayout.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "StdAfx.h"
  2. #include "UIHorizontalLayout.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(CHorizontalLayoutUI)
  6. CHorizontalLayoutUI::CHorizontalLayoutUI() : m_iSepWidth(0), m_uButtonState(0), m_bImmMode(false)
  7. {
  8. ptLastMouse.x = ptLastMouse.y = 0;
  9. ::ZeroMemory(&m_rcNewPos, sizeof(m_rcNewPos));
  10. }
  11. LPCTSTR CHorizontalLayoutUI::GetClass() const
  12. {
  13. return _T("HorizontalLayoutUI");
  14. }
  15. LPVOID CHorizontalLayoutUI::GetInterface(LPCTSTR pstrName)
  16. {
  17. if( _tcsicmp(pstrName, DUI_CTR_HORIZONTALLAYOUT) == 0 ) return static_cast<CHorizontalLayoutUI*>(this);
  18. return CContainerUI::GetInterface(pstrName);
  19. }
  20. UINT CHorizontalLayoutUI::GetControlFlags() const
  21. {
  22. if( IsEnabled() && m_iSepWidth != 0 ) return UIFLAG_SETCURSOR;
  23. else return 0;
  24. }
  25. void CHorizontalLayoutUI::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 cyNeeded = 0;
  49. int nAdjustables = 0;
  50. int cxFixed = 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.cy -= rcPadding.top + rcPadding.bottom;
  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.cx == 0 ) {
  70. nAdjustables++;
  71. }
  72. else {
  73. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  74. if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
  75. }
  76. cxFixed += sz.cx + pControl->GetPadding().left + pControl->GetPadding().right;
  77. sz.cy = MAX(sz.cy, 0);
  78. if( sz.cy < pControl->GetMinHeight() ) sz.cy = pControl->GetMinHeight();
  79. if( sz.cy > pControl->GetMaxHeight() ) sz.cy = pControl->GetMaxHeight();
  80. cyNeeded = MAX(cyNeeded, sz.cy + rcPadding.top + rcPadding.bottom);
  81. nEstimateNum++;
  82. }
  83. cxFixed += (nEstimateNum - 1) * iChildPadding;
  84. // Place elements
  85. int cxNeeded = 0;
  86. int cxExpand = 0;
  87. if( nAdjustables > 0 ) cxExpand = MAX(0, (szAvailable.cx - cxFixed) / nAdjustables);
  88. // Position the elements
  89. SIZE szRemaining = szAvailable;
  90. int iPosX = rc.left;
  91. // 滚动条
  92. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) {
  93. iPosX -= m_pHorizontalScrollBar->GetScrollPos();
  94. }
  95. else {
  96. // 子控件横向对其方式
  97. if(nAdjustables <= 0) {
  98. UINT iChildAlign = GetChildAlign();
  99. if (iChildAlign == DT_CENTER) {
  100. iPosX += (szAvailable.cx -cxFixed) / 2;
  101. }
  102. else if (iChildAlign == DT_RIGHT) {
  103. iPosX += (szAvailable.cx - cxFixed);
  104. }
  105. }
  106. }
  107. int iEstimate = 0;
  108. int iAdjustable = 0;
  109. int cxFixedRemaining = cxFixed;
  110. for( int it2 = 0; it2 < m_items.GetSize(); it2++ ) {
  111. CControlUI* pControl = static_cast<CControlUI*>(m_items[it2]);
  112. if( !pControl->IsVisible() ) continue;
  113. if( pControl->IsFloat() ) {
  114. SetFloatPos(it2);
  115. continue;
  116. }
  117. iEstimate += 1;
  118. RECT rcPadding = pControl->GetPadding();
  119. szRemaining.cx -= rcPadding.left;
  120. szControlAvailable = szRemaining;
  121. szControlAvailable.cy -= rcPadding.top + rcPadding.bottom;
  122. iControlMaxWidth = pControl->GetFixedWidth();
  123. iControlMaxHeight = pControl->GetFixedHeight();
  124. if (iControlMaxWidth <= 0) iControlMaxWidth = pControl->GetMaxWidth();
  125. if (iControlMaxHeight <= 0) iControlMaxHeight = pControl->GetMaxHeight();
  126. if (szControlAvailable.cx > iControlMaxWidth) szControlAvailable.cx = iControlMaxWidth;
  127. if (szControlAvailable.cy > iControlMaxHeight) szControlAvailable.cy = iControlMaxHeight;
  128. cxFixedRemaining = cxFixedRemaining - (rcPadding.left + rcPadding.right);
  129. if (iEstimate > 1) cxFixedRemaining = cxFixedRemaining - iChildPadding;
  130. SIZE sz = pControl->EstimateSize(szControlAvailable);
  131. if( sz.cx == 0 ) {
  132. iAdjustable++;
  133. sz.cx = cxExpand;
  134. // Distribute remaining to last element (usually round-off left-overs)
  135. if( iAdjustable == nAdjustables ) {
  136. sz.cx = MAX(0, szRemaining.cx - rcPadding.right - cxFixedRemaining);
  137. }
  138. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  139. if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
  140. }
  141. else {
  142. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  143. if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
  144. cxFixedRemaining -= sz.cx;
  145. }
  146. sz.cy = pControl->GetMaxHeight();
  147. if( sz.cy == 0 ) sz.cy = szAvailable.cy - rcPadding.top - rcPadding.bottom;
  148. if( sz.cy < 0 ) sz.cy = 0;
  149. if( sz.cy > szControlAvailable.cy ) sz.cy = szControlAvailable.cy;
  150. if( sz.cy < pControl->GetMinHeight() ) sz.cy = pControl->GetMinHeight();
  151. UINT iChildAlign = GetChildVAlign();
  152. if (iChildAlign == DT_VCENTER) {
  153. int iPosY = (rc.bottom + rc.top) / 2;
  154. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) {
  155. iPosY += m_pVerticalScrollBar->GetScrollRange() / 2;
  156. iPosY -= m_pVerticalScrollBar->GetScrollPos();
  157. }
  158. RECT rcCtrl = { iPosX + rcPadding.left, iPosY - sz.cy/2, iPosX + sz.cx + rcPadding.left, iPosY + sz.cy - sz.cy/2 };
  159. pControl->SetPos(rcCtrl, false);
  160. }
  161. else if (iChildAlign == DT_BOTTOM) {
  162. int iPosY = rc.bottom;
  163. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) {
  164. iPosY += m_pVerticalScrollBar->GetScrollRange();
  165. iPosY -= m_pVerticalScrollBar->GetScrollPos();
  166. }
  167. RECT rcCtrl = { iPosX + rcPadding.left, iPosY - rcPadding.bottom - sz.cy, iPosX + sz.cx + rcPadding.left, iPosY - rcPadding.bottom };
  168. pControl->SetPos(rcCtrl, false);
  169. }
  170. else {
  171. int iPosY = rc.top;
  172. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) {
  173. iPosY -= m_pVerticalScrollBar->GetScrollPos();
  174. }
  175. RECT rcCtrl = { iPosX + rcPadding.left, iPosY + rcPadding.top, iPosX + sz.cx + rcPadding.left, iPosY + sz.cy + rcPadding.top };
  176. pControl->SetPos(rcCtrl, false);
  177. }
  178. iPosX += sz.cx + iChildPadding + rcPadding.left + rcPadding.right;
  179. cxNeeded += sz.cx + rcPadding.left + rcPadding.right;
  180. szRemaining.cx -= sz.cx + iChildPadding + rcPadding.right;
  181. }
  182. cxNeeded += (nEstimateNum - 1) * iChildPadding;
  183. // Process the scrollbar
  184. ProcessScrollBar(rc, cxNeeded, cyNeeded);
  185. }
  186. void CHorizontalLayoutUI::DoPostPaint(HDC hDC, const RECT& rcPaint)
  187. {
  188. if( (m_uButtonState & UISTATE_CAPTURED) != 0 && !m_bImmMode ) {
  189. RECT rcSeparator = GetThumbRect(true);
  190. CRenderEngine::DrawColor(hDC, rcSeparator, 0xAA000000);
  191. }
  192. }
  193. void CHorizontalLayoutUI::SetSepWidth(int iWidth)
  194. {
  195. m_iSepWidth = iWidth;
  196. }
  197. int CHorizontalLayoutUI::GetSepWidth() const
  198. {
  199. return m_iSepWidth;
  200. }
  201. void CHorizontalLayoutUI::SetSepImmMode(bool bImmediately)
  202. {
  203. if( m_bImmMode == bImmediately ) return;
  204. if( (m_uButtonState & UISTATE_CAPTURED) != 0 && !m_bImmMode && m_pManager != NULL ) {
  205. m_pManager->RemovePostPaint(this);
  206. }
  207. m_bImmMode = bImmediately;
  208. }
  209. bool CHorizontalLayoutUI::IsSepImmMode() const
  210. {
  211. return m_bImmMode;
  212. }
  213. void CHorizontalLayoutUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  214. {
  215. if( _tcsicmp(pstrName, _T("sepwidth")) == 0 ) SetSepWidth(_ttoi(pstrValue));
  216. else if( _tcsicmp(pstrName, _T("sepimm")) == 0 ) SetSepImmMode(_tcsicmp(pstrValue, _T("true")) == 0);
  217. else CContainerUI::SetAttribute(pstrName, pstrValue);
  218. }
  219. void CHorizontalLayoutUI::DoEvent(TEventUI& event)
  220. {
  221. if( m_iSepWidth != 0 ) {
  222. if( event.Type == UIEVENT_BUTTONDOWN && IsEnabled() )
  223. {
  224. RECT rcSeparator = GetThumbRect(false);
  225. if( ::PtInRect(&rcSeparator, event.ptMouse) ) {
  226. m_uButtonState |= UISTATE_CAPTURED;
  227. ptLastMouse = event.ptMouse;
  228. m_rcNewPos = m_rcItem;
  229. if( !m_bImmMode && m_pManager ) m_pManager->AddPostPaint(this);
  230. return;
  231. }
  232. }
  233. if( event.Type == UIEVENT_BUTTONUP )
  234. {
  235. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  236. m_uButtonState &= ~UISTATE_CAPTURED;
  237. m_rcItem = m_rcNewPos;
  238. if( !m_bImmMode && m_pManager ) m_pManager->RemovePostPaint(this);
  239. NeedParentUpdate();
  240. return;
  241. }
  242. }
  243. if( event.Type == UIEVENT_MOUSEMOVE )
  244. {
  245. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  246. LONG cx = event.ptMouse.x - ptLastMouse.x;
  247. ptLastMouse = event.ptMouse;
  248. RECT rc = m_rcNewPos;
  249. if( m_iSepWidth >= 0 ) {
  250. if( cx > 0 && event.ptMouse.x < m_rcNewPos.right - m_iSepWidth ) return;
  251. if( cx < 0 && event.ptMouse.x > m_rcNewPos.right ) return;
  252. rc.right += cx;
  253. if( rc.right - rc.left <= GetMinWidth() ) {
  254. if( m_rcNewPos.right - m_rcNewPos.left <= GetMinWidth() ) return;
  255. rc.right = rc.left + GetMinWidth();
  256. }
  257. if( rc.right - rc.left >= GetMaxWidth() ) {
  258. if( m_rcNewPos.right - m_rcNewPos.left >= GetMaxWidth() ) return;
  259. rc.right = rc.left + GetMaxWidth();
  260. }
  261. }
  262. else {
  263. if( cx > 0 && event.ptMouse.x < m_rcNewPos.left ) return;
  264. if( cx < 0 && event.ptMouse.x > m_rcNewPos.left - m_iSepWidth ) return;
  265. rc.left += cx;
  266. if( rc.right - rc.left <= GetMinWidth() ) {
  267. if( m_rcNewPos.right - m_rcNewPos.left <= GetMinWidth() ) return;
  268. rc.left = rc.right - GetMinWidth();
  269. }
  270. if( rc.right - rc.left >= GetMaxWidth() ) {
  271. if( m_rcNewPos.right - m_rcNewPos.left >= GetMaxWidth() ) return;
  272. rc.left = rc.right - GetMaxWidth();
  273. }
  274. }
  275. CDuiRect rcInvalidate = GetThumbRect(true);
  276. m_rcNewPos = rc;
  277. m_cxyFixed.cx = m_rcNewPos.right - m_rcNewPos.left;
  278. if( m_bImmMode ) {
  279. m_rcItem = m_rcNewPos;
  280. NeedParentUpdate();
  281. }
  282. else {
  283. rcInvalidate.Join(GetThumbRect(true));
  284. rcInvalidate.Join(GetThumbRect(false));
  285. if( m_pManager ) m_pManager->Invalidate(rcInvalidate);
  286. }
  287. return;
  288. }
  289. }
  290. if( event.Type == UIEVENT_SETCURSOR )
  291. {
  292. RECT rcSeparator = GetThumbRect(false);
  293. if( IsEnabled() && ::PtInRect(&rcSeparator, event.ptMouse) ) {
  294. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
  295. return;
  296. }
  297. }
  298. }
  299. CContainerUI::DoEvent(event);
  300. }
  301. RECT CHorizontalLayoutUI::GetThumbRect(bool bUseNew) const
  302. {
  303. if( (m_uButtonState & UISTATE_CAPTURED) != 0 && bUseNew) {
  304. if( m_iSepWidth >= 0 ) return CDuiRect(m_rcNewPos.right - m_iSepWidth, m_rcNewPos.top, m_rcNewPos.right, m_rcNewPos.bottom);
  305. else return CDuiRect(m_rcNewPos.left, m_rcNewPos.top, m_rcNewPos.left - m_iSepWidth, m_rcNewPos.bottom);
  306. }
  307. else {
  308. if( m_iSepWidth >= 0 ) return CDuiRect(m_rcItem.right - m_iSepWidth, m_rcItem.top, m_rcItem.right, m_rcItem.bottom);
  309. else return CDuiRect(m_rcItem.left, m_rcItem.top, m_rcItem.left - m_iSepWidth, m_rcItem.bottom);
  310. }
  311. }
  312. }