UIHorizontalLayout.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "stdafx.h"
  2. #include "UIHorizontalLayout.h"
  3. namespace DuiLib
  4. {
  5. CHorizontalLayoutUI::CHorizontalLayoutUI()
  6. : m_iSepWidth(0),
  7. m_uButtonState(0),
  8. m_bImmMode(false)
  9. {
  10. __super::m_dwFillMode = GRADIENT_FILL_MODE::HOZ;
  11. ptLastMouse.x = ptLastMouse.y = 0;
  12. ::ZeroMemory(&m_rcNewPos, sizeof(m_rcNewPos));
  13. }
  14. LPCTSTR CHorizontalLayoutUI::GetClass() const
  15. {
  16. return _T("HorizontalLayoutUI");
  17. }
  18. LPVOID CHorizontalLayoutUI::GetInterface(LPCTSTR pstrName)
  19. {
  20. if( _tcscmp(pstrName, DUI_CTR_HORIZONTALLAYOUT) == 0 ) return static_cast<CHorizontalLayoutUI*>(this);
  21. return CContainerUI::GetInterface(pstrName);
  22. }
  23. UINT CHorizontalLayoutUI::GetControlFlags() const
  24. {
  25. if( IsEnabled() && m_iSepWidth != 0 ) return UIFLAG_SETCURSOR;
  26. else return 0;
  27. }
  28. void CHorizontalLayoutUI::SetPos(RECT rc, bool bNeedInvalidate)
  29. {
  30. CControlUI::SetPos(rc, bNeedInvalidate);
  31. rc = m_rcItem;
  32. // Adjust for inset
  33. rc.left += m_rcInset.left;
  34. rc.top += m_rcInset.top;
  35. rc.right -= m_rcInset.right;
  36. rc.bottom -= m_rcInset.bottom;
  37. if( m_items.GetSize() == 0) {
  38. ProcessScrollBar(rc, 0, 0);
  39. return;
  40. }
  41. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) rc.right -= m_pVerticalScrollBar->GetFixedWidth();
  42. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) rc.bottom -= m_pHorizontalScrollBar->GetFixedHeight();
  43. // Determine the width of elements that are sizeable
  44. SIZE szAvailable = { rc.right - rc.left, rc.bottom - rc.top };
  45. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() )
  46. szAvailable.cx += m_pHorizontalScrollBar->GetScrollRange();
  47. int nAdjustables = 0;
  48. int cxFixed = 0;
  49. int nEstimateNum = 0;
  50. for( int it1 = 0; it1 < m_items.GetSize(); it1++ ) {
  51. CControlUI* pControl = static_cast<CControlUI*>(m_items[it1]);
  52. if( !pControl->IsVisible() ) continue;
  53. if( pControl->IsFloat() ) continue;
  54. SIZE sz = pControl->EstimateSize(szAvailable);
  55. if( sz.cx == 0 ) {
  56. nAdjustables++;
  57. }
  58. else {
  59. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  60. if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
  61. }
  62. cxFixed += sz.cx + pControl->GetPadding().left + pControl->GetPadding().right;
  63. nEstimateNum++;
  64. }
  65. cxFixed += (nEstimateNum - 1) * m_iChildPadding;
  66. int cxExpand = 0;
  67. int cxNeeded = 0;
  68. if( nAdjustables > 0 ) cxExpand = MAX(0, (szAvailable.cx - cxFixed) / nAdjustables);
  69. // Position the elements
  70. SIZE szRemaining = szAvailable;
  71. int iPosX = rc.left;
  72. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) {
  73. iPosX -= m_pHorizontalScrollBar->GetScrollPos();
  74. }
  75. int iAdjustable = 0;
  76. int cxFixedRemaining = cxFixed;
  77. for( int it2 = 0; it2 < m_items.GetSize(); it2++ ) {
  78. CControlUI* pControl = static_cast<CControlUI*>(m_items[it2]);
  79. if( !pControl->IsVisible() ) continue;
  80. if( pControl->IsFloat() ) {
  81. SetFloatPos(it2);
  82. continue;
  83. }
  84. RECT rcPadding = pControl->GetPadding();
  85. szRemaining.cx -= rcPadding.left;
  86. SIZE sz = pControl->EstimateSize(szRemaining);
  87. if( sz.cx == 0 ) {
  88. iAdjustable++;
  89. sz.cx = cxExpand;
  90. // Distribute remaining to last element (usually round-off left-overs)
  91. if( iAdjustable == nAdjustables ) {
  92. sz.cx = MAX(0, szRemaining.cx - rcPadding.right - cxFixedRemaining);
  93. }
  94. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  95. if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
  96. }
  97. else {
  98. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  99. if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
  100. cxFixedRemaining -= sz.cx + rcPadding.left + rcPadding.right ;
  101. }
  102. cxFixedRemaining -= m_iChildPadding;
  103. sz.cy = pControl->GetFixedHeight();
  104. if( sz.cy == 0 ) sz.cy = rc.bottom - rc.top - rcPadding.top - rcPadding.bottom;
  105. if( sz.cy < 0 ) sz.cy = 0;
  106. if( sz.cy < pControl->GetMinHeight() ) sz.cy = pControl->GetMinHeight();
  107. if( sz.cy > pControl->GetMaxHeight() ) sz.cy = pControl->GetMaxHeight();
  108. RECT rcCtrl = { iPosX + rcPadding.left, rc.top + rcPadding.top, iPosX + sz.cx + rcPadding.left , rc.top + rcPadding.top + sz.cy};
  109. pControl->SetPos(rcCtrl, false);
  110. iPosX += sz.cx + m_iChildPadding + rcPadding.left + rcPadding.right;
  111. cxNeeded += sz.cx + rcPadding.left + rcPadding.right;
  112. szRemaining.cx -= sz.cx + m_iChildPadding + rcPadding.right;
  113. }
  114. cxNeeded += (nEstimateNum - 1) * m_iChildPadding;
  115. // Process the scrollbar
  116. ProcessScrollBar(rc, cxNeeded, 0);
  117. }
  118. void CHorizontalLayoutUI::DoPostPaint(HDC hDC, const RECT& rcPaint)
  119. {
  120. if( (m_uButtonState & UISTATE_CAPTURED) != 0 && !m_bImmMode ) {
  121. RECT rcSeparator = GetThumbRect(true);
  122. CRenderEngine::DrawColor(hDC, rcSeparator, 0xAA000000);
  123. }
  124. }
  125. void CHorizontalLayoutUI::SetSepWidth(int iWidth)
  126. {
  127. m_iSepWidth = iWidth;
  128. }
  129. int CHorizontalLayoutUI::GetSepWidth() const
  130. {
  131. return m_iSepWidth;
  132. }
  133. void CHorizontalLayoutUI::SetSepImmMode(bool bImmediately)
  134. {
  135. if( m_bImmMode == bImmediately ) return;
  136. if( (m_uButtonState & UISTATE_CAPTURED) != 0 && !m_bImmMode && m_pManager != NULL ) {
  137. m_pManager->RemovePostPaint(this);
  138. }
  139. m_bImmMode = bImmediately;
  140. }
  141. bool CHorizontalLayoutUI::IsSepImmMode() const
  142. {
  143. return m_bImmMode;
  144. }
  145. void CHorizontalLayoutUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  146. {
  147. if( _tcscmp(pstrName, _T("sepwidth")) == 0 ) SetSepWidth(_ttoi(pstrValue));
  148. else if( _tcscmp(pstrName, _T("sepimm")) == 0 ) SetSepImmMode(_tcscmp(pstrValue, _T("true")) == 0);
  149. else CContainerUI::SetAttribute(pstrName, pstrValue);
  150. }
  151. void CHorizontalLayoutUI::DoEvent(TEventUI& event)
  152. {
  153. if( m_iSepWidth != 0 ) {
  154. if( event.Type == UIEVENT_BUTTONDOWN && IsEnabled() )
  155. {
  156. RECT rcSeparator = GetThumbRect(false);
  157. if( ::PtInRect(&rcSeparator, event.ptMouse) ) {
  158. m_uButtonState |= UISTATE_CAPTURED;
  159. ptLastMouse = event.ptMouse;
  160. m_rcNewPos = m_rcItem;
  161. if( !m_bImmMode && m_pManager ) m_pManager->AddPostPaint(this);
  162. return;
  163. }
  164. }
  165. if( event.Type == UIEVENT_BUTTONUP )
  166. {
  167. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  168. m_uButtonState &= ~UISTATE_CAPTURED;
  169. m_rcItem = m_rcNewPos;
  170. if( !m_bImmMode && m_pManager ) m_pManager->RemovePostPaint(this);
  171. NeedParentUpdate();
  172. return;
  173. }
  174. }
  175. if( event.Type == UIEVENT_MOUSEMOVE )
  176. {
  177. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  178. LONG cx = event.ptMouse.x - ptLastMouse.x;
  179. ptLastMouse = event.ptMouse;
  180. RECT rc = m_rcNewPos;
  181. if( m_iSepWidth >= 0 ) {
  182. if( cx > 0 && event.ptMouse.x < m_rcNewPos.right - m_iSepWidth ) return;
  183. if( cx < 0 && event.ptMouse.x > m_rcNewPos.right ) return;
  184. rc.right += cx;
  185. if( rc.right - rc.left <= GetMinWidth() ) {
  186. if( m_rcNewPos.right - m_rcNewPos.left <= GetMinWidth() ) return;
  187. rc.right = rc.left + GetMinWidth();
  188. }
  189. if( rc.right - rc.left >= GetMaxWidth() ) {
  190. if( m_rcNewPos.right - m_rcNewPos.left >= GetMaxWidth() ) return;
  191. rc.right = rc.left + GetMaxWidth();
  192. }
  193. }
  194. else {
  195. if( cx > 0 && event.ptMouse.x < m_rcNewPos.left ) return;
  196. if( cx < 0 && event.ptMouse.x > m_rcNewPos.left - m_iSepWidth ) return;
  197. rc.left += cx;
  198. if( rc.right - rc.left <= GetMinWidth() ) {
  199. if( m_rcNewPos.right - m_rcNewPos.left <= GetMinWidth() ) return;
  200. rc.left = rc.right - GetMinWidth();
  201. }
  202. if( rc.right - rc.left >= GetMaxWidth() ) {
  203. if( m_rcNewPos.right - m_rcNewPos.left >= GetMaxWidth() ) return;
  204. rc.left = rc.right - GetMaxWidth();
  205. }
  206. }
  207. CDuiRect rcInvalidate = GetThumbRect(true);
  208. m_rcNewPos = rc;
  209. m_cxyFixed.cx = m_rcNewPos.right - m_rcNewPos.left;
  210. if( m_bImmMode ) {
  211. m_rcItem = m_rcNewPos;
  212. NeedParentUpdate();
  213. }
  214. else {
  215. rcInvalidate.Join(GetThumbRect(true));
  216. rcInvalidate.Join(GetThumbRect(false));
  217. if( m_pManager ) m_pManager->Invalidate(rcInvalidate);
  218. }
  219. return;
  220. }
  221. }
  222. if( event.Type == UIEVENT_SETCURSOR )
  223. {
  224. RECT rcSeparator = GetThumbRect(false);
  225. if( IsEnabled() && ::PtInRect(&rcSeparator, event.ptMouse) ) {
  226. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
  227. return;
  228. }
  229. }
  230. }
  231. CContainerUI::DoEvent(event);
  232. }
  233. RECT CHorizontalLayoutUI::GetThumbRect(bool bUseNew) const
  234. {
  235. if( (m_uButtonState & UISTATE_CAPTURED) != 0 && bUseNew) {
  236. if( m_iSepWidth >= 0 ) return CDuiRect(m_rcNewPos.right - m_iSepWidth, m_rcNewPos.top, m_rcNewPos.right, m_rcNewPos.bottom);
  237. else return CDuiRect(m_rcNewPos.left, m_rcNewPos.top, m_rcNewPos.left - m_iSepWidth, m_rcNewPos.bottom);
  238. }
  239. else {
  240. if( m_iSepWidth >= 0 ) return CDuiRect(m_rcItem.right - m_iSepWidth, m_rcItem.top, m_rcItem.right, m_rcItem.bottom);
  241. else return CDuiRect(m_rcItem.left, m_rcItem.top, m_rcItem.left - m_iSepWidth, m_rcItem.bottom);
  242. }
  243. }
  244. }