UIText.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "StdAfx.h"
  2. #include "UIText.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(CTextUI)
  6. CTextUI::CTextUI() : m_nLinks(0), m_nHoverLink(-1)
  7. {
  8. m_uTextStyle = DT_WORDBREAK;
  9. m_rcTextPadding.left = 2;
  10. m_rcTextPadding.right = 2;
  11. ::ZeroMemory(m_rcLinks, sizeof(m_rcLinks));
  12. }
  13. CTextUI::~CTextUI()
  14. {
  15. }
  16. LPCTSTR CTextUI::GetClass() const
  17. {
  18. return _T("TextUI");
  19. }
  20. LPVOID CTextUI::GetInterface(LPCTSTR pstrName)
  21. {
  22. if( _tcsicmp(pstrName, DUI_CTR_TEXT) == 0 ) return static_cast<CTextUI*>(this);
  23. return CLabelUI::GetInterface(pstrName);
  24. }
  25. UINT CTextUI::GetControlFlags() const
  26. {
  27. if( IsEnabled() && m_nLinks > 0 ) return UIFLAG_SETCURSOR;
  28. else return 0;
  29. }
  30. CDuiString* CTextUI::GetLinkContent(int iIndex)
  31. {
  32. if( iIndex >= 0 && iIndex < m_nLinks ) return &m_sLinks[iIndex];
  33. return NULL;
  34. }
  35. void CTextUI::DoEvent(TEventUI& event)
  36. {
  37. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  38. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  39. else CLabelUI::DoEvent(event);
  40. return;
  41. }
  42. if( event.Type == UIEVENT_SETCURSOR ) {
  43. for( int i = 0; i < m_nLinks; i++ ) {
  44. if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) {
  45. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
  46. return;
  47. }
  48. }
  49. }
  50. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK && IsEnabled() ) {
  51. for( int i = 0; i < m_nLinks; i++ ) {
  52. if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) {
  53. Invalidate();
  54. return;
  55. }
  56. }
  57. }
  58. if( event.Type == UIEVENT_BUTTONUP && IsEnabled() ) {
  59. for( int i = 0; i < m_nLinks; i++ ) {
  60. if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) {
  61. m_pManager->SendNotify(this, DUI_MSGTYPE_LINK, i);
  62. return;
  63. }
  64. }
  65. }
  66. if( event.Type == UIEVENT_CONTEXTMENU )
  67. {
  68. return;
  69. }
  70. // When you move over a link
  71. if( m_nLinks > 0 && event.Type == UIEVENT_MOUSEMOVE && IsEnabled() ) {
  72. int nHoverLink = -1;
  73. for( int i = 0; i < m_nLinks; i++ ) {
  74. if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) {
  75. nHoverLink = i;
  76. break;
  77. }
  78. }
  79. if(m_nHoverLink != nHoverLink) {
  80. m_nHoverLink = nHoverLink;
  81. Invalidate();
  82. return;
  83. }
  84. }
  85. if( event.Type == UIEVENT_MOUSELEAVE ) {
  86. if( m_nLinks > 0 && IsEnabled() ) {
  87. if(m_nHoverLink != -1) {
  88. m_nHoverLink = -1;
  89. Invalidate();
  90. return;
  91. }
  92. }
  93. }
  94. CLabelUI::DoEvent(event);
  95. }
  96. SIZE CTextUI::EstimateSize(SIZE szAvailable)
  97. {
  98. CDuiString sText = GetText();
  99. RECT m_rcTextPadding = GetTextPadding();
  100. RECT rcText = { 0, 0, m_bAutoCalcWidth ? 9999 : GetManager()->GetDPIObj()->Scale(m_cxyFixed.cx), 9999 };
  101. rcText.left += m_rcTextPadding.left;
  102. rcText.right -= m_rcTextPadding.right;
  103. if( m_bShowHtml ) {
  104. int nLinks = 0;
  105. CRenderEngine::DrawHtmlText(m_pManager->GetPaintDC(), m_pManager, rcText, sText, m_dwTextColor, NULL, NULL, nLinks, m_iFont, DT_CALCRECT | m_uTextStyle);
  106. }
  107. else {
  108. CRenderEngine::DrawText(m_pManager->GetPaintDC(), m_pManager, rcText, sText, m_dwTextColor, m_iFont, DT_CALCRECT | m_uTextStyle);
  109. }
  110. SIZE cXY = {rcText.right - rcText.left + m_rcTextPadding.left + m_rcTextPadding.right,
  111. rcText.bottom - rcText.top + m_rcTextPadding.top + m_rcTextPadding.bottom};
  112. if (m_bAutoCalcWidth)
  113. {
  114. m_cxyFixed.cx = MulDiv(cXY.cx, 100.0, GetManager()->GetDPIObj()->GetScale());
  115. }
  116. return CControlUI::EstimateSize(szAvailable);
  117. }
  118. void CTextUI::PaintText(HDC hDC)
  119. {
  120. CDuiString sText = GetText();
  121. if( sText.IsEmpty() ) {
  122. m_nLinks = 0;
  123. return;
  124. }
  125. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  126. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  127. m_nLinks = lengthof(m_rcLinks);
  128. RECT rc = m_rcItem;
  129. rc.left += m_rcTextPadding.left;
  130. rc.right -= m_rcTextPadding.right;
  131. rc.top += m_rcTextPadding.top;
  132. rc.bottom -= m_rcTextPadding.bottom;
  133. if( IsEnabled() ) {
  134. if( m_bShowHtml )
  135. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, sText, m_dwTextColor, \
  136. m_rcLinks, m_sLinks, m_nLinks, m_iFont, m_uTextStyle);
  137. else
  138. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwTextColor, \
  139. m_iFont, m_uTextStyle);
  140. }
  141. else {
  142. if( m_bShowHtml )
  143. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, \
  144. m_rcLinks, m_sLinks, m_nLinks, m_iFont, m_uTextStyle);
  145. else
  146. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, \
  147. m_iFont, m_uTextStyle);
  148. }
  149. }
  150. }