UIOption.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 _T("OptionUI");
  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. CStdPtrArray* 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. }
  72. if(bTriggerEvent) m_pManager->SendNotify (this, DUI_MSGTYPE_SELECTCHANGED, bSelected);
  73. }
  74. else {
  75. if(bTriggerEvent) m_pManager->SendNotify (this, DUI_MSGTYPE_SELECTCHANGED, bSelected);
  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::GetSelectBkColor()
  131. {
  132. return m_dwSelectedBkColor;
  133. }
  134. LPCTSTR COptionUI::GetForeImage()
  135. {
  136. return m_diFore.sDrawString;
  137. }
  138. void COptionUI::SetForeImage(LPCTSTR pStrImage)
  139. {
  140. if( m_diFore.sDrawString == pStrImage && m_diFore.pImageInfo != NULL ) return;
  141. m_diFore.Clear();
  142. m_diFore.sDrawString = pStrImage;
  143. Invalidate();
  144. }
  145. SIZE COptionUI::EstimateSize(SIZE szAvailable)
  146. {
  147. if( m_cxyFixed.cy == 0 ) return CDuiSize(m_cxyFixed.cx, m_pManager->GetFontInfo(GetFont())->tm.tmHeight + 8);
  148. return CControlUI::EstimateSize(szAvailable);
  149. }
  150. void COptionUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  151. {
  152. if( _tcscmp(pstrName, _T("group")) == 0 ) SetGroup(pstrValue);
  153. else if( _tcscmp(pstrName, _T("selected")) == 0 ) Selected(_tcscmp(pstrValue, _T("true")) == 0);
  154. else if( _tcscmp(pstrName, _T("selectedimage")) == 0 ) SetSelectedImage(pstrValue);
  155. else if( _tcscmp(pstrName, _T("selectedhotimage")) == 0 ) SetSelectedHotImage(pstrValue);
  156. else if( _tcscmp(pstrName, _T("foreimage")) == 0 ) SetForeImage(pstrValue);
  157. else if( _tcscmp(pstrName, _T("selectedbkcolor")) == 0 ) {
  158. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  159. LPTSTR pstr = NULL;
  160. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  161. SetSelectedBkColor(clrColor);
  162. }
  163. else if( _tcscmp(pstrName, _T("selectedtextcolor")) == 0 ) {
  164. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  165. LPTSTR pstr = NULL;
  166. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  167. SetSelectedTextColor(clrColor);
  168. }
  169. else CButtonUI::SetAttribute(pstrName, pstrValue);
  170. }
  171. void COptionUI::PaintStatusImage(HDC hDC)
  172. {
  173. if( (m_uButtonState & UISTATE_SELECTED) != 0 ) {
  174. if ((m_uButtonState & UISTATE_HOT) != 0)
  175. {
  176. if (DrawImage(hDC, m_diSelectedHot)) goto Label_ForeImage;
  177. }
  178. if( DrawImage(hDC, m_diSelected) ) goto Label_ForeImage;
  179. else if(m_dwSelectedBkColor != 0) {
  180. CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwSelectedBkColor));
  181. goto Label_ForeImage;
  182. }
  183. }
  184. UINT uSavedState = m_uButtonState;
  185. m_uButtonState &= ~UISTATE_PUSHED;
  186. CButtonUI::PaintStatusImage(hDC);
  187. m_uButtonState = uSavedState;
  188. Label_ForeImage:
  189. DrawImage(hDC, m_diFore);
  190. }
  191. void COptionUI::PaintText(HDC hDC)
  192. {
  193. if( (m_uButtonState & UISTATE_SELECTED) != 0 )
  194. {
  195. DWORD oldTextColor = m_dwTextColor;
  196. if( m_dwSelectedTextColor != 0 ) m_dwTextColor = m_dwSelectedTextColor;
  197. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  198. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  199. if( m_sText.IsEmpty() ) return;
  200. int nLinks = 0;
  201. RECT rc = m_rcItem;
  202. rc.left += m_rcTextPadding.left;
  203. rc.right -= m_rcTextPadding.right;
  204. rc.top += m_rcTextPadding.top;
  205. rc.bottom -= m_rcTextPadding.bottom;
  206. if( m_bShowHtml )
  207. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, IsEnabled()?m_dwTextColor:m_dwDisabledTextColor, \
  208. NULL, NULL, nLinks, m_uTextStyle);
  209. else
  210. CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, IsEnabled()?m_dwTextColor:m_dwDisabledTextColor, \
  211. m_iFont, m_uTextStyle);
  212. m_dwTextColor = oldTextColor;
  213. }
  214. else
  215. {
  216. UINT uSavedState = m_uButtonState;
  217. m_uButtonState &= ~UISTATE_PUSHED;
  218. CButtonUI::PaintText(hDC);
  219. m_uButtonState = uSavedState;
  220. }
  221. }
  222. }