UIText.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 _T("TextUI");
  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. m_nHoverLink = -1;
  88. Invalidate();
  89. return;
  90. }
  91. }
  92. }
  93. CLabelUI::DoEvent(event);
  94. }
  95. SIZE CTextUI::EstimateSize(SIZE szAvailable)
  96. {
  97. RECT rcText = { 0, 0, MAX(szAvailable.cx, m_cxyFixed.cx), 9999 };
  98. rcText.left += m_rcTextPadding.left;
  99. rcText.right -= m_rcTextPadding.right;
  100. if( m_bShowHtml ) {
  101. int nLinks = 0;
  102. CRenderEngine::DrawHtmlText(m_pManager->GetPaintDC(), m_pManager, rcText, m_sText, m_dwTextColor, NULL, NULL, nLinks, DT_CALCRECT | m_uTextStyle);
  103. }
  104. else {
  105. CRenderEngine::DrawText(m_pManager->GetPaintDC(), m_pManager, rcText, m_sText, m_dwTextColor, m_iFont, DT_CALCRECT | m_uTextStyle);
  106. }
  107. SIZE cXY = {rcText.right - rcText.left + m_rcTextPadding.left + m_rcTextPadding.right,
  108. rcText.bottom - rcText.top + m_rcTextPadding.top + m_rcTextPadding.bottom};
  109. if( m_cxyFixed.cy != 0 ) cXY.cy = m_cxyFixed.cy;
  110. return cXY;
  111. }
  112. void CTextUI::PaintText(HDC hDC)
  113. {
  114. if( m_sText.IsEmpty() ) {
  115. m_nLinks = 0;
  116. return;
  117. }
  118. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  119. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  120. if( m_sText.IsEmpty() ) return;
  121. m_nLinks = lengthof(m_rcLinks);
  122. RECT rc = m_rcItem;
  123. rc.left += m_rcTextPadding.left;
  124. rc.right -= m_rcTextPadding.right;
  125. rc.top += m_rcTextPadding.top;
  126. rc.bottom -= m_rcTextPadding.bottom;
  127. if( IsEnabled() ) {
  128. if( m_bShowHtml )
  129. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwTextColor, \
  130. m_rcLinks, m_sLinks, m_nLinks, m_uTextStyle);
  131. else
  132. CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, m_dwTextColor, \
  133. m_iFont, m_uTextStyle);
  134. }
  135. else {
  136. if( m_bShowHtml )
  137. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor, \
  138. m_rcLinks, m_sLinks, m_nLinks, m_uTextStyle);
  139. else
  140. CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor, \
  141. m_iFont, m_uTextStyle);
  142. }
  143. }
  144. }