UIText.cpp 3.8 KB

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