UIOption.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. #include "StdAfx.h"
  2. #include "UIOption.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(COptionUI)
  6. COptionUI::COptionUI() : m_bSelected(false) ,m_iSelectedFont(-1), m_dwSelectedTextColor(0), m_dwSelectedBkColor(0), m_nSelectedStateCount(0)
  7. {
  8. }
  9. COptionUI::~COptionUI()
  10. {
  11. if( !m_sGroupName.IsEmpty() && m_pManager ) m_pManager->RemoveOptionGroup(m_sGroupName, this);
  12. }
  13. LPCTSTR COptionUI::GetClass() const
  14. {
  15. return _T("OptionUI");
  16. }
  17. LPVOID COptionUI::GetInterface(LPCTSTR pstrName)
  18. {
  19. if( _tcsicmp(pstrName, DUI_CTR_OPTION) == 0 ) return static_cast<COptionUI*>(this);
  20. return CButtonUI::GetInterface(pstrName);
  21. }
  22. void COptionUI::SetManager(CPaintManagerUI* pManager, CControlUI* pParent, bool bInit)
  23. {
  24. CControlUI::SetManager(pManager, pParent, bInit);
  25. if( bInit && !m_sGroupName.IsEmpty() ) {
  26. if (m_pManager) m_pManager->AddOptionGroup(m_sGroupName, this);
  27. }
  28. }
  29. LPCTSTR COptionUI::GetGroup() const
  30. {
  31. return m_sGroupName;
  32. }
  33. void COptionUI::SetGroup(LPCTSTR pStrGroupName)
  34. {
  35. if( pStrGroupName == NULL ) {
  36. if( m_sGroupName.IsEmpty() ) return;
  37. m_sGroupName.Empty();
  38. }
  39. else {
  40. if( m_sGroupName == pStrGroupName ) return;
  41. if (!m_sGroupName.IsEmpty() && m_pManager) m_pManager->RemoveOptionGroup(m_sGroupName, this);
  42. m_sGroupName = pStrGroupName;
  43. }
  44. if( !m_sGroupName.IsEmpty() ) {
  45. if (m_pManager) m_pManager->AddOptionGroup(m_sGroupName, this);
  46. }
  47. else {
  48. if (m_pManager) m_pManager->RemoveOptionGroup(m_sGroupName, this);
  49. }
  50. Selected(m_bSelected);
  51. }
  52. LPCTSTR COptionUI::GetGroupType() const
  53. {
  54. return m_sGroupType;
  55. }
  56. void COptionUI::SetGroupType(LPCTSTR pStrGroupType)
  57. {
  58. if( pStrGroupType == NULL ) {
  59. if( m_sGroupType.IsEmpty() ) return;
  60. m_sGroupType.Empty();
  61. }
  62. else {
  63. m_sGroupType = pStrGroupType;
  64. }
  65. }
  66. bool COptionUI::IsSelected() const
  67. {
  68. return m_bSelected;
  69. }
  70. void COptionUI::Selected(bool bSelected, bool bMsg/* = true*/)
  71. {
  72. if(m_bSelected == bSelected) return;
  73. m_bSelected = bSelected;
  74. if( m_bSelected ) m_uButtonState |= UISTATE_SELECTED;
  75. else m_uButtonState &= ~UISTATE_SELECTED;
  76. if( m_pManager != NULL ) {
  77. if( !m_sGroupName.IsEmpty() ) {
  78. if( m_bSelected ) {
  79. CStdPtrArray* aOptionGroup = m_pManager->GetOptionGroup(m_sGroupName);
  80. for( int i = 0; i < aOptionGroup->GetSize(); i++ ) {
  81. COptionUI* pControl = static_cast<COptionUI*>(aOptionGroup->GetAt(i));
  82. if( pControl != this && (m_sGroupType.IsEmpty() || m_sGroupType.CompareNoCase(pControl->GetGroupType()) != 0)) {
  83. pControl->Selected(false, bMsg);
  84. }
  85. }
  86. if(bMsg) {
  87. m_pManager->SendNotify(this, DUI_MSGTYPE_SELECTCHANGED);
  88. }
  89. }
  90. else {
  91. if(bMsg) {
  92. m_pManager->SendNotify(this, DUI_MSGTYPE_SELECTCHANGED);
  93. }
  94. }
  95. }
  96. else {
  97. if(bMsg) {
  98. m_pManager->SendNotify(this, DUI_MSGTYPE_SELECTCHANGED);
  99. }
  100. }
  101. }
  102. Invalidate();
  103. }
  104. bool COptionUI::Activate()
  105. {
  106. if( !CButtonUI::Activate() ) return false;
  107. if( !m_sGroupName.IsEmpty() ) {
  108. Selected(true);
  109. }
  110. else Selected(!m_bSelected);
  111. Invalidate();
  112. return true;
  113. }
  114. LPCTSTR COptionUI::GetSelectedImage()
  115. {
  116. return m_sSelectedImage;
  117. }
  118. void COptionUI::SetSelectedImage(LPCTSTR pStrImage)
  119. {
  120. m_sSelectedImage = pStrImage;
  121. Invalidate();
  122. }
  123. LPCTSTR COptionUI::GetSelectedHotImage()
  124. {
  125. return m_sSelectedHotImage;
  126. }
  127. void COptionUI::SetSelectedHotImage( LPCTSTR pStrImage )
  128. {
  129. m_sSelectedHotImage = pStrImage;
  130. Invalidate();
  131. }
  132. LPCTSTR COptionUI::GetSelectedPushedImage()
  133. {
  134. return m_sSelectedPushedImage;
  135. }
  136. void COptionUI::SetSelectedPushedImage(LPCTSTR pStrImage)
  137. {
  138. m_sSelectedPushedImage = pStrImage;
  139. Invalidate();
  140. }
  141. void COptionUI::SetSelectedTextColor(DWORD dwTextColor)
  142. {
  143. m_dwSelectedTextColor = dwTextColor;
  144. }
  145. DWORD COptionUI::GetSelectedTextColor()
  146. {
  147. if (m_dwSelectedTextColor == 0) m_dwSelectedTextColor = m_pManager->GetDefaultFontColor();
  148. return m_dwSelectedTextColor;
  149. }
  150. void COptionUI::SetSelectedBkColor( DWORD dwBkColor )
  151. {
  152. m_dwSelectedBkColor = dwBkColor;
  153. }
  154. DWORD COptionUI::GetSelectBkColor()
  155. {
  156. return m_dwSelectedBkColor;
  157. }
  158. LPCTSTR COptionUI::GetSelectedForedImage()
  159. {
  160. return m_sSelectedForeImage;
  161. }
  162. void COptionUI::SetSelectedForedImage(LPCTSTR pStrImage)
  163. {
  164. m_sSelectedForeImage = pStrImage;
  165. Invalidate();
  166. }
  167. void COptionUI::SetSelectedStateCount(int nCount)
  168. {
  169. m_nSelectedStateCount = nCount;
  170. Invalidate();
  171. }
  172. int COptionUI::GetSelectedStateCount() const
  173. {
  174. return m_nSelectedStateCount;
  175. }
  176. LPCTSTR COptionUI::GetSelectedStateImage()
  177. {
  178. return m_sSelectedStateImage;
  179. }
  180. void COptionUI::SetSelectedStateImage( LPCTSTR pStrImage )
  181. {
  182. m_sSelectedStateImage = pStrImage;
  183. Invalidate();
  184. }
  185. void COptionUI::SetSelectedFont(int index)
  186. {
  187. m_iSelectedFont = index;
  188. Invalidate();
  189. }
  190. int COptionUI::GetSelectedFont() const
  191. {
  192. return m_iSelectedFont;
  193. }
  194. void COptionUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  195. {
  196. if( _tcsicmp(pstrName, _T("group")) == 0 ) SetGroup(pstrValue);
  197. else if( _tcsicmp(pstrName, _T("grouptype")) == 0 ) SetGroupType(pstrValue);
  198. else if( _tcsicmp(pstrName, _T("selected")) == 0 ) Selected(_tcsicmp(pstrValue, _T("true")) == 0);
  199. else if( _tcsicmp(pstrName, _T("selectedimage")) == 0 ) SetSelectedImage(pstrValue);
  200. else if( _tcsicmp(pstrName, _T("selectedhotimage")) == 0 ) SetSelectedHotImage(pstrValue);
  201. else if( _tcsicmp(pstrName, _T("selectedpushedimage")) == 0 ) SetSelectedPushedImage(pstrValue);
  202. else if( _tcsicmp(pstrName, _T("selectedforeimage")) == 0 ) SetSelectedForedImage(pstrValue);
  203. else if( _tcsicmp(pstrName, _T("selectedstateimage")) == 0 ) SetSelectedStateImage(pstrValue);
  204. else if( _tcsicmp(pstrName, _T("selectedstatecount")) == 0 ) SetSelectedStateCount(_ttoi(pstrValue));
  205. else if( _tcsicmp(pstrName, _T("selectedbkcolor")) == 0 ) {
  206. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  207. LPTSTR pstr = NULL;
  208. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  209. SetSelectedBkColor(clrColor);
  210. }
  211. else if( _tcsicmp(pstrName, _T("selectedtextcolor")) == 0 ) {
  212. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  213. LPTSTR pstr = NULL;
  214. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  215. SetSelectedTextColor(clrColor);
  216. }
  217. else if( _tcsicmp(pstrName, _T("selectedfont")) == 0 ) SetSelectedFont(_ttoi(pstrValue));
  218. else CButtonUI::SetAttribute(pstrName, pstrValue);
  219. }
  220. void COptionUI::PaintBkColor(HDC hDC)
  221. {
  222. if(IsSelected()) {
  223. if(m_dwSelectedBkColor != 0) {
  224. CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwSelectedBkColor));
  225. }
  226. }
  227. else {
  228. return CButtonUI::PaintBkColor(hDC);
  229. }
  230. }
  231. void COptionUI::PaintStatusImage(HDC hDC)
  232. {
  233. if(IsSelected()) {
  234. if(!m_sSelectedStateImage.IsEmpty() && m_nSelectedStateCount > 0)
  235. {
  236. TDrawInfo info;
  237. info.Parse(m_sSelectedStateImage, _T(""), m_pManager);
  238. const TImageInfo* pImage = m_pManager->GetImageEx(info.sImageName, info.sResType, info.dwMask, info.bHSL, info.bGdiplus);
  239. if(m_sSelectedImage.IsEmpty() && pImage != NULL)
  240. {
  241. SIZE szImage = {pImage->nX, pImage->nY};
  242. SIZE szStatus = {pImage->nX / m_nSelectedStateCount, pImage->nY};
  243. if( szImage.cx > 0 && szImage.cy > 0 )
  244. {
  245. RECT rcSrc = {0, 0, szImage.cx, szImage.cy};
  246. if(m_nSelectedStateCount > 0) {
  247. int iLeft = rcSrc.left + 0 * szStatus.cx;
  248. int iRight = iLeft + szStatus.cx;
  249. int iTop = rcSrc.top;
  250. int iBottom = iTop + szStatus.cy;
  251. m_sSelectedImage.Format(_T("res='%s' restype='%s' dest='%d,%d,%d,%d' source='%d,%d,%d,%d' corner='%d,%d,%d,%d'"), info.sImageName.GetData(), info.sResType.GetData(), info.rcDest.left, info.rcDest.top, info.rcDest.right, info.rcDest.bottom, iLeft, iTop, iRight, iBottom, info.rcCorner.left, info.rcCorner.top, info.rcCorner.right, info.rcCorner.bottom);
  252. }
  253. if(m_nSelectedStateCount > 1) {
  254. int iLeft = rcSrc.left + 1 * szStatus.cx;
  255. int iRight = iLeft + szStatus.cx;
  256. int iTop = rcSrc.top;
  257. int iBottom = iTop + szStatus.cy;
  258. m_sSelectedHotImage.Format(_T("res='%s' restype='%s' dest='%d,%d,%d,%d' source='%d,%d,%d,%d' corner='%d,%d,%d,%d'"), info.sImageName.GetData(), info.sResType.GetData(), info.rcDest.left, info.rcDest.top, info.rcDest.right, info.rcDest.bottom, iLeft, iTop, iRight, iBottom, info.rcCorner.left, info.rcCorner.top, info.rcCorner.right, info.rcCorner.bottom);
  259. m_sSelectedPushedImage.Format(_T("res='%s' restype='%s' dest='%d,%d,%d,%d' source='%d,%d,%d,%d' corner='%d,%d,%d,%d'"), info.sImageName.GetData(), info.sResType.GetData(), info.rcDest.left, info.rcDest.top, info.rcDest.right, info.rcDest.bottom, iLeft, iTop, iRight, iBottom, info.rcCorner.left, info.rcCorner.top, info.rcCorner.right, info.rcCorner.bottom);
  260. }
  261. if(m_nSelectedStateCount > 2) {
  262. int iLeft = rcSrc.left + 2 * szStatus.cx;
  263. int iRight = iLeft + szStatus.cx;
  264. int iTop = rcSrc.top;
  265. int iBottom = iTop + szStatus.cy;
  266. m_sSelectedPushedImage.Format(_T("res='%s' restype='%s' dest='%d,%d,%d,%d' source='%d,%d,%d,%d' corner='%d,%d,%d,%d'"), info.sImageName.GetData(), info.sResType.GetData(), info.rcDest.left, info.rcDest.top, info.rcDest.right, info.rcDest.bottom, iLeft, iTop, iRight, iBottom, info.rcCorner.left, info.rcCorner.top, info.rcCorner.right, info.rcCorner.bottom);
  267. }
  268. }
  269. }
  270. }
  271. if( (m_uButtonState & UISTATE_PUSHED) != 0 && !m_sSelectedPushedImage.IsEmpty()) {
  272. if( !DrawImage(hDC, (LPCTSTR)m_sSelectedPushedImage) ) {}
  273. else return;
  274. }
  275. else if( (m_uButtonState & UISTATE_HOT) != 0 && !m_sSelectedHotImage.IsEmpty()) {
  276. if( !DrawImage(hDC, (LPCTSTR)m_sSelectedHotImage) ) {}
  277. else return;
  278. }
  279. if( !m_sSelectedImage.IsEmpty() ) {
  280. if( !DrawImage(hDC, (LPCTSTR)m_sSelectedImage) ) {}
  281. }
  282. }
  283. else {
  284. CButtonUI::PaintStatusImage(hDC);
  285. }
  286. }
  287. void COptionUI::PaintForeImage(HDC hDC)
  288. {
  289. if(IsSelected()) {
  290. if( !m_sSelectedForeImage.IsEmpty() ) {
  291. if( !DrawImage(hDC, (LPCTSTR)m_sSelectedForeImage) ) {}
  292. else return;
  293. }
  294. }
  295. return CButtonUI::PaintForeImage(hDC);
  296. }
  297. void COptionUI::PaintText(HDC hDC)
  298. {
  299. if( (m_uButtonState & UISTATE_SELECTED) != 0 )
  300. {
  301. DWORD oldTextColor = m_dwTextColor;
  302. if( m_dwSelectedTextColor != 0 ) m_dwTextColor = m_dwSelectedTextColor;
  303. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  304. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  305. int iFont = GetFont();
  306. if(GetSelectedFont() != -1) {
  307. iFont = GetSelectedFont();
  308. }
  309. CDuiString sText = GetText();
  310. if( sText.IsEmpty() ) return;
  311. int nLinks = 0;
  312. RECT rc = m_rcItem;
  313. RECT rcTextPadding = GetTextPadding();
  314. rc.left += rcTextPadding.left;
  315. rc.right -= rcTextPadding.right;
  316. rc.top += rcTextPadding.top;
  317. rc.bottom -= rcTextPadding.bottom;
  318. if( m_bShowHtml )
  319. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, sText, IsEnabled()?m_dwTextColor:m_dwDisabledTextColor, \
  320. NULL, NULL, nLinks, iFont, m_uTextStyle);
  321. else
  322. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, IsEnabled()?m_dwTextColor:m_dwDisabledTextColor, \
  323. iFont, m_uTextStyle);
  324. m_dwTextColor = oldTextColor;
  325. }
  326. else
  327. CButtonUI::PaintText(hDC);
  328. }
  329. //////////////////////////////////////////////////////////////////////////////////////////////////
  330. //
  331. IMPLEMENT_DUICONTROL(CCheckBoxUI)
  332. CCheckBoxUI::CCheckBoxUI() : m_bAutoCheck(false)
  333. {
  334. }
  335. LPCTSTR CCheckBoxUI::GetClass() const
  336. {
  337. return _T("CheckBoxUI");
  338. }
  339. LPVOID CCheckBoxUI::GetInterface(LPCTSTR pstrName)
  340. {
  341. if( _tcsicmp(pstrName, DUI_CTR_CHECKBOX) == 0 ) return static_cast<CCheckBoxUI*>(this);
  342. return COptionUI::GetInterface(pstrName);
  343. }
  344. void CCheckBoxUI::SetCheck(bool bCheck)
  345. {
  346. Selected(bCheck);
  347. }
  348. bool CCheckBoxUI::GetCheck() const
  349. {
  350. return IsSelected();
  351. }
  352. void CCheckBoxUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  353. {
  354. if( _tcsicmp(pstrName, _T("EnableAutoCheck")) == 0 ) SetAutoCheck(_tcsicmp(pstrValue, _T("true")) == 0);
  355. COptionUI::SetAttribute(pstrName, pstrValue);
  356. }
  357. void CCheckBoxUI::SetAutoCheck(bool bEnable)
  358. {
  359. m_bAutoCheck = bEnable;
  360. }
  361. void CCheckBoxUI::DoEvent(TEventUI& event)
  362. {
  363. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  364. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  365. else COptionUI::DoEvent(event);
  366. return;
  367. }
  368. if( m_bAutoCheck && (event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK)) {
  369. if( ::PtInRect(&m_rcItem, event.ptMouse) && IsEnabled() ) {
  370. SetCheck(!GetCheck());
  371. m_pManager->SendNotify(this, DUI_MSGTYPE_CHECKCLICK, 0, 0);
  372. Invalidate();
  373. }
  374. return;
  375. }
  376. COptionUI::DoEvent(event);
  377. }
  378. void CCheckBoxUI::Selected(bool bSelected, bool bMsg/* = true*/)
  379. {
  380. if( m_bSelected == bSelected ) return;
  381. m_bSelected = bSelected;
  382. if( m_bSelected ) m_uButtonState |= UISTATE_SELECTED;
  383. else m_uButtonState &= ~UISTATE_SELECTED;
  384. if( m_pManager != NULL ) {
  385. if( !m_sGroupName.IsEmpty() ) {
  386. if( m_bSelected ) {
  387. CStdPtrArray* aOptionGroup = m_pManager->GetOptionGroup(m_sGroupName);
  388. for( int i = 0; i < aOptionGroup->GetSize(); i++ ) {
  389. COptionUI* pControl = static_cast<COptionUI*>(aOptionGroup->GetAt(i));
  390. if( pControl != this ) {
  391. pControl->Selected(false, bMsg);
  392. }
  393. }
  394. if(bMsg) {
  395. m_pManager->SendNotify(this, DUI_MSGTYPE_SELECTCHANGED, m_bSelected, 0);
  396. }
  397. }
  398. }
  399. else {
  400. if(bMsg) {
  401. m_pManager->SendNotify(this, DUI_MSGTYPE_SELECTCHANGED, m_bSelected, 0);
  402. }
  403. }
  404. }
  405. Invalidate();
  406. }
  407. }