UIComboBox.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "StdAfx.h"
  2. #include "UIComboBox.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(CComboBoxUI)
  6. CComboBoxUI::CComboBoxUI()
  7. {
  8. m_nArrowWidth = 0;
  9. }
  10. LPCTSTR CComboBoxUI::GetClass() const
  11. {
  12. return _T("ComboBoxUI");
  13. }
  14. void CComboBoxUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  15. {
  16. if (_tcsicmp(pstrName, _T("arrowimage")) == 0)
  17. m_sArrowImage = pstrValue;
  18. else
  19. CComboUI::SetAttribute(pstrName, pstrValue);
  20. }
  21. void CComboBoxUI::PaintStatusImage(HDC hDC)
  22. {
  23. if (m_sArrowImage.IsEmpty())
  24. CComboUI::PaintStatusImage(hDC);
  25. else
  26. {
  27. // get index
  28. if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
  29. else m_uButtonState &= ~ UISTATE_FOCUSED;
  30. if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
  31. else m_uButtonState &= ~ UISTATE_DISABLED;
  32. int nIndex = 0;
  33. if ((m_uButtonState & UISTATE_DISABLED) != 0)
  34. nIndex = 4;
  35. else if ((m_uButtonState & UISTATE_PUSHED) != 0)
  36. nIndex = 2;
  37. else if ((m_uButtonState & UISTATE_HOT) != 0)
  38. nIndex = 1;
  39. else if ((m_uButtonState & UISTATE_FOCUSED) != 0)
  40. nIndex = 3;
  41. // make modify string
  42. CDuiString sModify = m_sArrowImage;
  43. int nPos1 = sModify.Find(_T("source"));
  44. int nPos2 = sModify.Find(_T("'"), nPos1 + 7);
  45. if (nPos2 == -1) return; //first
  46. int nPos3 = sModify.Find(_T("'"), nPos2 + 1);
  47. if (nPos3 == -1) return; //second
  48. CDuiRect rcBmpPart;
  49. LPTSTR lpszValue = NULL;
  50. rcBmpPart.left = _tcstol(sModify.GetData() + nPos2 + 1, &lpszValue, 10); ASSERT(lpszValue);
  51. rcBmpPart.top = _tcstol(lpszValue + 1, &lpszValue, 10); ASSERT(lpszValue);
  52. rcBmpPart.right = _tcstol(lpszValue + 1, &lpszValue, 10); ASSERT(lpszValue);
  53. rcBmpPart.bottom = _tcstol(lpszValue + 1, &lpszValue, 10); ASSERT(lpszValue);
  54. m_nArrowWidth = rcBmpPart.GetWidth() / 5;
  55. rcBmpPart.left += nIndex * m_nArrowWidth;
  56. rcBmpPart.right = rcBmpPart.left + m_nArrowWidth;
  57. CDuiRect rcDest(0, 0, m_rcItem.right - m_rcItem.left, m_rcItem.bottom - m_rcItem.top);
  58. rcDest.Deflate(GetBorderSize(), GetBorderSize());
  59. rcDest.left = rcDest.right - m_nArrowWidth;
  60. CDuiString sSource = sModify.Mid(nPos1, nPos3 + 1 - nPos1);
  61. CDuiString sReplace;
  62. sReplace.SmallFormat(_T("source='%d,%d,%d,%d' dest='%d,%d,%d,%d'"),
  63. rcBmpPart.left, rcBmpPart.top, rcBmpPart.right, rcBmpPart.bottom,
  64. rcDest.left, rcDest.top, rcDest.right, rcDest.bottom);
  65. sModify.Replace(sSource, sReplace);
  66. // draw image
  67. if (!DrawImage(hDC, m_sArrowImage, sModify))
  68. {}
  69. }
  70. }
  71. void CComboBoxUI::PaintText(HDC hDC)
  72. {
  73. RECT rcText = m_rcItem;
  74. rcText.left += m_rcTextPadding.left;
  75. rcText.right -= m_rcTextPadding.right;
  76. rcText.top += m_rcTextPadding.top;
  77. rcText.bottom -= m_rcTextPadding.bottom;
  78. rcText.right -= m_nArrowWidth; // add this line than CComboUI::PaintText(HDC hDC)
  79. if( m_iCurSel >= 0 ) {
  80. CControlUI* pControl = static_cast<CControlUI*>(m_items[m_iCurSel]);
  81. IListItemUI* pElement = static_cast<IListItemUI*>(pControl->GetInterface(_T("ListItem")));
  82. if( pElement != NULL ) {
  83. pElement->DrawItemText(hDC, rcText);
  84. }
  85. else {
  86. RECT rcOldPos = pControl->GetPos();
  87. pControl->SetPos(rcText);
  88. pControl->DoPaint(hDC, rcText, NULL);
  89. pControl->SetPos(rcOldPos);
  90. }
  91. }
  92. }
  93. }