UIOption.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "stdafx.h"
  2. #include "UIOption.h"
  3. namespace DuiLib
  4. {
  5. COptionUI::COptionUI() : m_bSelected(false), m_dwSelectedBkColor(0), m_dwSelectedTextColor(0)
  6. {
  7. }
  8. COptionUI::~COptionUI()
  9. {
  10. if( !m_sGroupName.IsEmpty() && m_pManager ) m_pManager->RemoveOptionGroup(m_sGroupName, this);
  11. }
  12. LPCTSTR COptionUI::GetClass() const
  13. {
  14. return DUI_CTR_OPTION;
  15. }
  16. LPVOID COptionUI::GetInterface(LPCTSTR pstrName)
  17. {
  18. if( _tcscmp(pstrName, DUI_CTR_OPTION) == 0 ) return static_cast<COptionUI*>(this);
  19. return CButtonUI::GetInterface(pstrName);
  20. }
  21. void COptionUI::SetManager(CPaintManagerUI* pManager, CControlUI* pParent, bool bInit)
  22. {
  23. CControlUI::SetManager(pManager, pParent, bInit);
  24. if( bInit && !m_sGroupName.IsEmpty() ) {
  25. if (m_pManager) m_pManager->AddOptionGroup(m_sGroupName, this);
  26. }
  27. }
  28. LPCTSTR COptionUI::GetGroup() const
  29. {
  30. return m_sGroupName;
  31. }
  32. void COptionUI::SetGroup(LPCTSTR pStrGroupName)
  33. {
  34. if( pStrGroupName == NULL ) {
  35. if( m_sGroupName.IsEmpty() ) return;
  36. m_sGroupName.Empty();
  37. }
  38. else {
  39. if( m_sGroupName == pStrGroupName ) return;
  40. if (!m_sGroupName.IsEmpty() && m_pManager) m_pManager->RemoveOptionGroup(m_sGroupName, this);
  41. m_sGroupName = pStrGroupName;
  42. }
  43. if( !m_sGroupName.IsEmpty() ) {
  44. if (m_pManager) m_pManager->AddOptionGroup(m_sGroupName, this);
  45. }
  46. else {
  47. if (m_pManager) m_pManager->RemoveOptionGroup(m_sGroupName, this);
  48. }
  49. Selected(m_bSelected);
  50. }
  51. bool COptionUI::IsSelected() const
  52. {
  53. return m_bSelected;
  54. }
  55. void COptionUI::Selected(bool bSelected, bool bTriggerEvent)
  56. {
  57. if( m_bSelected == bSelected ) return;
  58. m_bSelected = bSelected;
  59. if( m_bSelected ) m_uButtonState |= UISTATE_SELECTED;
  60. else m_uButtonState &= ~UISTATE_SELECTED;
  61. if( m_pManager != NULL ) {
  62. if( !m_sGroupName.IsEmpty() ) {
  63. if( m_bSelected ) {
  64. CDuiPtrArray* aOptionGroup = m_pManager->GetOptionGroup(m_sGroupName);
  65. for( int i = 0; i < aOptionGroup->GetSize(); i++ ) {
  66. COptionUI* pControl = static_cast<COptionUI*>(aOptionGroup->GetAt(i));
  67. if( pControl != this ) {
  68. pControl->Selected(false, bTriggerEvent);
  69. }
  70. }
  71. if (bTriggerEvent) m_pManager->SendNotify(this, DUI_MSGTYPE_SELECTCHANGED);
  72. }
  73. }
  74. else {
  75. if (bTriggerEvent) m_pManager->SendNotify(this, DUI_MSGTYPE_SELECTCHANGED);
  76. }
  77. }
  78. Invalidate();
  79. }
  80. bool COptionUI::Activate()
  81. {
  82. if( !CButtonUI::Activate() ) return false;
  83. if( !m_sGroupName.IsEmpty() ) Selected(true);
  84. else Selected(!m_bSelected);
  85. return true;
  86. }
  87. void COptionUI::SetEnabled(bool bEnable)
  88. {
  89. CControlUI::SetEnabled(bEnable);
  90. if( !IsEnabled() ) {
  91. if( m_bSelected ) m_uButtonState = UISTATE_SELECTED;
  92. else m_uButtonState = 0;
  93. }
  94. }
  95. LPCTSTR COptionUI::GetSelectedImage()
  96. {
  97. return m_diSelected.sDrawString;
  98. }
  99. void COptionUI::SetSelectedImage(LPCTSTR pStrImage)
  100. {
  101. if( m_diSelected.sDrawString == pStrImage && m_diSelected.pImageInfo != NULL ) return;
  102. m_diSelected.Clear();
  103. m_diSelected.sDrawString = pStrImage;
  104. Invalidate();
  105. }
  106. LPCTSTR COptionUI::GetSelectedHotImage()
  107. {
  108. return m_diSelectedHot.sDrawString;
  109. }
  110. void COptionUI::SetSelectedHotImage( LPCTSTR pStrImage )
  111. {
  112. if( m_diSelectedHot.sDrawString == pStrImage && m_diSelectedHot.pImageInfo != NULL ) return;
  113. m_diSelectedHot.Clear();
  114. m_diSelectedHot.sDrawString = pStrImage;
  115. Invalidate();
  116. }
  117. void COptionUI::SetSelectedTextColor(DWORD dwTextColor)
  118. {
  119. m_dwSelectedTextColor = dwTextColor;
  120. }
  121. DWORD COptionUI::GetSelectedTextColor()
  122. {
  123. if (m_dwSelectedTextColor == 0) m_dwSelectedTextColor = m_pManager->GetDefaultFontColor();
  124. return m_dwSelectedTextColor;
  125. }
  126. void COptionUI::SetSelectedBkColor( DWORD dwBkColor )
  127. {
  128. m_dwSelectedBkColor = dwBkColor;
  129. }
  130. DWORD COptionUI::GetSelectedBkColor()
  131. {
  132. return m_dwSelectedBkColor;
  133. }
  134. DUI_DEPRECATED DWORD COptionUI::GetSelectBkColor()
  135. {
  136. return this->GetSelectedBkColor();
  137. }
  138. LPCTSTR COptionUI::GetForeImage()
  139. {
  140. return m_diFore.sDrawString;
  141. }
  142. void COptionUI::SetForeImage(LPCTSTR pStrImage)
  143. {
  144. if( m_diFore.sDrawString == pStrImage && m_diFore.pImageInfo != NULL ) return;
  145. m_diFore.Clear();
  146. m_diFore.sDrawString = pStrImage;
  147. Invalidate();
  148. }
  149. SIZE COptionUI::EstimateSize(SIZE szAvailable)
  150. {
  151. if( m_cxyFixed.cy == 0 ) return CDuiSize(m_cxyFixed.cx, m_pManager->GetFontInfo(GetFont())->tm.tmHeight + 8);
  152. return CControlUI::EstimateSize(szAvailable);
  153. }
  154. void COptionUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  155. {
  156. if( _tcscmp(pstrName, _T("group")) == 0 ) SetGroup(pstrValue);
  157. else if( _tcscmp(pstrName, _T("selected")) == 0 ) Selected(_tcscmp(pstrValue, _T("true")) == 0);
  158. else if( _tcscmp(pstrName, _T("selectedimage")) == 0 ) SetSelectedImage(pstrValue);
  159. else if( _tcscmp(pstrName, _T("selectedhotimage")) == 0 ) SetSelectedHotImage(pstrValue);
  160. else if( _tcscmp(pstrName, _T("foreimage")) == 0 ) SetForeImage(pstrValue);
  161. else if( _tcscmp(pstrName, _T("selectedbkcolor")) == 0 ) {
  162. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  163. LPTSTR pstr = NULL;
  164. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  165. SetSelectedBkColor(clrColor);
  166. }
  167. else if( _tcscmp(pstrName, _T("selectedtextcolor")) == 0 ) {
  168. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  169. LPTSTR pstr = NULL;
  170. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  171. SetSelectedTextColor(clrColor);
  172. }
  173. else CButtonUI::SetAttribute(pstrName, pstrValue);
  174. }
  175. void COptionUI::PaintStatusImage(HDC hDC)
  176. {
  177. if( (m_uButtonState & UISTATE_SELECTED) != 0 ) {
  178. if ((m_uButtonState & UISTATE_HOT) != 0)
  179. {
  180. if (DrawImage(hDC, m_diSelectedHot)) goto Label_ForeImage;
  181. }
  182. if( DrawImage(hDC, m_diSelected) ) goto Label_ForeImage;
  183. else if(m_dwSelectedBkColor != 0) {
  184. CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwSelectedBkColor));
  185. goto Label_ForeImage;
  186. }
  187. }
  188. UINT uSavedState = m_uButtonState;
  189. m_uButtonState &= ~UISTATE_PUSHED;
  190. CButtonUI::PaintStatusImage(hDC);
  191. m_uButtonState = uSavedState;
  192. Label_ForeImage:
  193. DrawImage(hDC, m_diFore);
  194. }
  195. void COptionUI::PaintText(HDC hDC)
  196. {
  197. if( (m_uButtonState & UISTATE_SELECTED) != 0 )
  198. {
  199. DWORD oldTextColor = m_dwTextColor;
  200. if( m_dwSelectedTextColor != 0 ) m_dwTextColor = m_dwSelectedTextColor;
  201. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  202. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  203. if( m_sText.IsEmpty() ) return;
  204. int nLinks = 0;
  205. RECT rc = m_rcItem;
  206. rc.left += m_rcTextPadding.left;
  207. rc.right -= m_rcTextPadding.right;
  208. rc.top += m_rcTextPadding.top;
  209. rc.bottom -= m_rcTextPadding.bottom;
  210. if( m_bShowHtml )
  211. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, IsEnabled()?m_dwTextColor:m_dwDisabledTextColor, \
  212. NULL, NULL, nLinks, m_iFont, m_uTextStyle);
  213. else
  214. CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, IsEnabled()?m_dwTextColor:m_dwDisabledTextColor, \
  215. m_iFont, m_uTextStyle);
  216. m_dwTextColor = oldTextColor;
  217. }
  218. else
  219. {
  220. UINT uSavedState = m_uButtonState;
  221. m_uButtonState &= ~UISTATE_PUSHED;
  222. CButtonUI::PaintText(hDC);
  223. m_uButtonState = uSavedState;
  224. }
  225. }
  226. }