UITabLayout.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "StdAfx.h"
  2. #include "UITabLayout.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(CTabLayoutUI)
  6. CTabLayoutUI::CTabLayoutUI() : m_iCurSel(-1)
  7. {
  8. }
  9. LPCTSTR CTabLayoutUI::GetClass() const
  10. {
  11. return _T("TabLayoutUI");
  12. }
  13. LPVOID CTabLayoutUI::GetInterface(LPCTSTR pstrName)
  14. {
  15. if( _tcsicmp(pstrName, DUI_CTR_TABLAYOUT) == 0 ) return static_cast<CTabLayoutUI*>(this);
  16. return CContainerUI::GetInterface(pstrName);
  17. }
  18. bool CTabLayoutUI::Add(CControlUI* pControl)
  19. {
  20. bool ret = CContainerUI::Add(pControl);
  21. if( !ret ) return ret;
  22. if(m_iCurSel == -1 && pControl->IsVisible())
  23. {
  24. m_iCurSel = GetItemIndex(pControl);
  25. }
  26. else
  27. {
  28. pControl->SetVisible(false);
  29. }
  30. return ret;
  31. }
  32. bool CTabLayoutUI::AddAt(CControlUI* pControl, int iIndex)
  33. {
  34. bool ret = CContainerUI::AddAt(pControl, iIndex);
  35. if( !ret ) return ret;
  36. if(m_iCurSel == -1 && pControl->IsVisible())
  37. {
  38. m_iCurSel = GetItemIndex(pControl);
  39. }
  40. else if( m_iCurSel != -1 && iIndex <= m_iCurSel )
  41. {
  42. m_iCurSel += 1;
  43. }
  44. else
  45. {
  46. pControl->SetVisible(false);
  47. }
  48. return ret;
  49. }
  50. bool CTabLayoutUI::Remove(CControlUI* pControl)
  51. {
  52. if( pControl == NULL) return false;
  53. int index = GetItemIndex(pControl);
  54. bool ret = CContainerUI::Remove(pControl);
  55. if( !ret ) return false;
  56. if( m_iCurSel == index)
  57. {
  58. if( GetCount() > 0 )
  59. {
  60. m_iCurSel=0;
  61. GetItemAt(m_iCurSel)->SetVisible(true);
  62. }
  63. else
  64. m_iCurSel=-1;
  65. NeedParentUpdate();
  66. }
  67. else if( m_iCurSel > index )
  68. {
  69. m_iCurSel -= 1;
  70. }
  71. return ret;
  72. }
  73. void CTabLayoutUI::RemoveAll()
  74. {
  75. m_iCurSel = -1;
  76. CContainerUI::RemoveAll();
  77. NeedParentUpdate();
  78. }
  79. int CTabLayoutUI::GetCurSel() const
  80. {
  81. return m_iCurSel;
  82. }
  83. bool CTabLayoutUI::SelectItem(int iIndex)
  84. {
  85. if( iIndex < 0 || iIndex >= m_items.GetSize() ) return false;
  86. if( iIndex == m_iCurSel ) return true;
  87. int iOldSel = m_iCurSel;
  88. m_iCurSel = iIndex;
  89. for( int it = 0; it < m_items.GetSize(); it++ )
  90. {
  91. if( it == iIndex ) {
  92. GetItemAt(it)->SetVisible(true);
  93. GetItemAt(it)->SetFocus();
  94. SetPos(m_rcItem);
  95. }
  96. else GetItemAt(it)->SetVisible(false);
  97. }
  98. NeedParentUpdate();
  99. if( m_pManager != NULL ) {
  100. m_pManager->SetNextTabControl();
  101. m_pManager->SendNotify(this, DUI_MSGTYPE_TABSELECT, m_iCurSel, iOldSel);
  102. }
  103. return true;
  104. }
  105. bool CTabLayoutUI::SelectItem( CControlUI* pControl )
  106. {
  107. int iIndex=GetItemIndex(pControl);
  108. if (iIndex==-1)
  109. return false;
  110. else
  111. return SelectItem(iIndex);
  112. }
  113. void CTabLayoutUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  114. {
  115. if( _tcsicmp(pstrName, _T("selectedid")) == 0 ) SelectItem(_ttoi(pstrValue));
  116. return CContainerUI::SetAttribute(pstrName, pstrValue);
  117. }
  118. void CTabLayoutUI::SetPos(RECT rc, bool bNeedInvalidate)
  119. {
  120. CControlUI::SetPos(rc, bNeedInvalidate);
  121. rc = m_rcItem;
  122. RECT rcInset = GetInset();
  123. // Adjust for inset
  124. rc.left += rcInset.left;
  125. rc.top += rcInset.top;
  126. rc.right -= rcInset.right;
  127. rc.bottom -= rcInset.bottom;
  128. for( int it = 0; it < m_items.GetSize(); it++ ) {
  129. CControlUI* pControl = static_cast<CControlUI*>(m_items[it]);
  130. if( !pControl->IsVisible() ) continue;
  131. if( pControl->IsFloat() ) {
  132. SetFloatPos(it);
  133. continue;
  134. }
  135. if( it != m_iCurSel ) continue;
  136. RECT rcPadding = pControl->GetPadding();
  137. rc.left += rcPadding.left;
  138. rc.top += rcPadding.top;
  139. rc.right -= rcPadding.right;
  140. rc.bottom -= rcPadding.bottom;
  141. SIZE szAvailable = { rc.right - rc.left, rc.bottom - rc.top };
  142. SIZE sz = pControl->EstimateSize(szAvailable);
  143. if( sz.cx == 0 ) {
  144. sz.cx = MAX(0, szAvailable.cx);
  145. }
  146. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  147. if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
  148. if(sz.cy == 0) {
  149. sz.cy = MAX(0, szAvailable.cy);
  150. }
  151. if( sz.cy < pControl->GetMinHeight() ) sz.cy = pControl->GetMinHeight();
  152. if( sz.cy > pControl->GetMaxHeight() ) sz.cy = pControl->GetMaxHeight();
  153. RECT rcCtrl = { rc.left, rc.top, rc.left + sz.cx, rc.top + sz.cy};
  154. pControl->SetPos(rcCtrl);
  155. }
  156. }
  157. }