UILabel.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include "StdAfx.h"
  2. #include "UILabel.h"
  3. #include <atlconv.h>
  4. namespace DuiLib
  5. {
  6. IMPLEMENT_DUICONTROL(CLabelUI)
  7. CLabelUI::CLabelUI() : m_uTextStyle(DT_VCENTER | DT_SINGLELINE), m_dwTextColor(0),
  8. m_dwDisabledTextColor(0),
  9. m_iFont(-1),
  10. m_bShowHtml(false),
  11. m_bAutoCalcWidth(false),
  12. m_bAutoCalcHeight(false),
  13. m_bNeedEstimateSize(false)
  14. {
  15. m_cxyFixedLast.cx = m_cxyFixedLast.cy = 0;
  16. m_szAvailableLast.cx = m_szAvailableLast.cy = 0;
  17. ::ZeroMemory(&m_rcTextPadding, sizeof(m_rcTextPadding));
  18. }
  19. CLabelUI::~CLabelUI()
  20. {
  21. }
  22. LPCTSTR CLabelUI::GetClass() const
  23. {
  24. return _T("LabelUI");
  25. }
  26. LPVOID CLabelUI::GetInterface(LPCTSTR pstrName)
  27. {
  28. if( _tcsicmp(pstrName, _T("Label")) == 0 ) return static_cast<CLabelUI*>(this);
  29. return CControlUI::GetInterface(pstrName);
  30. }
  31. UINT CLabelUI::GetControlFlags() const
  32. {
  33. return IsEnabled() ? UIFLAG_SETCURSOR : 0;
  34. }
  35. void CLabelUI::SetTextStyle(UINT uStyle)
  36. {
  37. m_uTextStyle = uStyle;
  38. Invalidate();
  39. }
  40. UINT CLabelUI::GetTextStyle() const
  41. {
  42. return m_uTextStyle;
  43. }
  44. void CLabelUI::SetTextColor(DWORD dwTextColor)
  45. {
  46. m_dwTextColor = dwTextColor;
  47. Invalidate();
  48. }
  49. DWORD CLabelUI::GetTextColor() const
  50. {
  51. return m_dwTextColor;
  52. }
  53. void CLabelUI::SetDisabledTextColor(DWORD dwTextColor)
  54. {
  55. m_dwDisabledTextColor = dwTextColor;
  56. Invalidate();
  57. }
  58. DWORD CLabelUI::GetDisabledTextColor() const
  59. {
  60. return m_dwDisabledTextColor;
  61. }
  62. void CLabelUI::SetFont(int index)
  63. {
  64. m_iFont = index;
  65. m_bNeedEstimateSize = true;
  66. Invalidate();
  67. }
  68. int CLabelUI::GetFont() const
  69. {
  70. return m_iFont;
  71. }
  72. RECT CLabelUI::GetTextPadding() const
  73. {
  74. RECT rcTextPadding = m_rcTextPadding;
  75. if(m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcTextPadding);
  76. return rcTextPadding;
  77. }
  78. void CLabelUI::SetTextPadding(RECT rc)
  79. {
  80. m_rcTextPadding = rc;
  81. m_bNeedEstimateSize = true;
  82. Invalidate();
  83. }
  84. bool CLabelUI::IsShowHtml()
  85. {
  86. return m_bShowHtml;
  87. }
  88. void CLabelUI::SetShowHtml(bool bShowHtml)
  89. {
  90. if( m_bShowHtml == bShowHtml ) return;
  91. m_bShowHtml = bShowHtml;
  92. m_bNeedEstimateSize = true;
  93. Invalidate();
  94. }
  95. SIZE CLabelUI::EstimateSize(SIZE szAvailable)
  96. {
  97. RECT rcTextPadding = GetTextPadding();
  98. if (m_cxyFixed.cx > 0 && m_cxyFixed.cy > 0) {
  99. return GetFixedSize();
  100. }
  101. if ((szAvailable.cx != m_szAvailableLast.cx || szAvailable.cy != m_szAvailableLast.cy)) {
  102. m_bNeedEstimateSize = true;
  103. }
  104. if (m_bNeedEstimateSize) {
  105. CDuiString sText = GetText();
  106. m_bNeedEstimateSize = false;
  107. m_szAvailableLast = szAvailable;
  108. m_cxyFixedLast = GetFixedSize();
  109. // 自动计算宽度
  110. if ((m_uTextStyle & DT_SINGLELINE) != 0) {
  111. // 高度
  112. if (m_cxyFixedLast.cy == 0) {
  113. m_cxyFixedLast.cy = m_pManager->GetFontInfo(m_iFont)->tm.tmHeight + 8;
  114. m_cxyFixedLast.cy += rcTextPadding.top + rcTextPadding.bottom;
  115. }
  116. // 宽度
  117. if (m_cxyFixedLast.cx == 0) {
  118. if(m_bAutoCalcWidth) {
  119. RECT rcText = { 0, 0, 9999, m_cxyFixedLast.cy };
  120. if( m_bShowHtml ) {
  121. int nLinks = 0;
  122. CRenderEngine::DrawHtmlText(m_pManager->GetPaintDC(), m_pManager, rcText, sText, 0, NULL, NULL, nLinks, m_iFont, DT_CALCRECT | m_uTextStyle & ~DT_RIGHT & ~DT_CENTER);
  123. }
  124. else {
  125. CRenderEngine::DrawText(m_pManager->GetPaintDC(), m_pManager, rcText, sText, 0, m_iFont, DT_CALCRECT | m_uTextStyle & ~DT_RIGHT & ~DT_CENTER);
  126. }
  127. m_cxyFixedLast.cx = rcText.right - rcText.left + GetManager()->GetDPIObj()->Scale(m_rcTextPadding.left + m_rcTextPadding.right);
  128. }
  129. }
  130. }
  131. // 自动计算高度
  132. else if(m_cxyFixedLast.cy == 0) {
  133. if(m_bAutoCalcHeight) {
  134. RECT rcText = { 0, 0, m_cxyFixedLast.cx, 9999 };
  135. rcText.left += rcTextPadding.left;
  136. rcText.right -= rcTextPadding.right;
  137. if( m_bShowHtml ) {
  138. int nLinks = 0;
  139. CRenderEngine::DrawHtmlText(m_pManager->GetPaintDC(), m_pManager, rcText, sText, 0, NULL, NULL, nLinks, m_iFont, DT_CALCRECT | m_uTextStyle & ~DT_RIGHT & ~DT_CENTER);
  140. }
  141. else {
  142. CRenderEngine::DrawText(m_pManager->GetPaintDC(), m_pManager, rcText, sText, 0, m_iFont, DT_CALCRECT | m_uTextStyle & ~DT_RIGHT & ~DT_CENTER);
  143. }
  144. m_cxyFixedLast.cy = rcText.bottom - rcText.top + rcTextPadding.top + rcTextPadding.bottom;
  145. }
  146. }
  147. }
  148. return m_cxyFixedLast;
  149. }
  150. void CLabelUI::DoEvent(TEventUI& event)
  151. {
  152. if( event.Type == UIEVENT_SETFOCUS )
  153. {
  154. m_bFocused = true;
  155. return;
  156. }
  157. if( event.Type == UIEVENT_KILLFOCUS )
  158. {
  159. m_bFocused = false;
  160. return;
  161. }
  162. CControlUI::DoEvent(event);
  163. }
  164. void CLabelUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  165. {
  166. if( _tcsicmp(pstrName, _T("align")) == 0 ) {
  167. if( _tcsstr(pstrValue, _T("left")) != NULL ) {
  168. m_uTextStyle &= ~(DT_CENTER | DT_RIGHT);
  169. m_uTextStyle |= DT_LEFT;
  170. }
  171. if( _tcsstr(pstrValue, _T("center")) != NULL ) {
  172. m_uTextStyle &= ~(DT_LEFT | DT_RIGHT );
  173. m_uTextStyle |= DT_CENTER;
  174. }
  175. if( _tcsstr(pstrValue, _T("right")) != NULL ) {
  176. m_uTextStyle &= ~(DT_LEFT | DT_CENTER);
  177. m_uTextStyle |= DT_RIGHT;
  178. }
  179. }
  180. else if( _tcsicmp(pstrName, _T("valign")) == 0 ) {
  181. if( _tcsstr(pstrValue, _T("top")) != NULL ) {
  182. m_uTextStyle &= ~(DT_BOTTOM | DT_VCENTER | DT_WORDBREAK);
  183. m_uTextStyle |= (DT_TOP | DT_SINGLELINE);
  184. }
  185. if( _tcsstr(pstrValue, _T("vcenter")) != NULL ) {
  186. m_uTextStyle &= ~(DT_TOP | DT_BOTTOM | DT_WORDBREAK);
  187. m_uTextStyle |= (DT_VCENTER | DT_SINGLELINE);
  188. }
  189. if( _tcsstr(pstrValue, _T("bottom")) != NULL ) {
  190. m_uTextStyle &= ~(DT_TOP | DT_VCENTER | DT_WORDBREAK);
  191. m_uTextStyle |= (DT_BOTTOM | DT_SINGLELINE);
  192. }
  193. }
  194. else if( _tcsicmp(pstrName, _T("endellipsis")) == 0 ) {
  195. if( _tcsicmp(pstrValue, _T("true")) == 0 ) m_uTextStyle |= DT_END_ELLIPSIS;
  196. else m_uTextStyle &= ~DT_END_ELLIPSIS;
  197. }
  198. else if( _tcsicmp(pstrName, _T("wordbreak")) == 0 ) {
  199. if( _tcsicmp(pstrValue, _T("true")) == 0 ) {
  200. m_uTextStyle &= ~DT_SINGLELINE;
  201. m_uTextStyle |= DT_WORDBREAK | DT_EDITCONTROL;
  202. }
  203. else {
  204. m_uTextStyle &= ~DT_WORDBREAK & ~DT_EDITCONTROL;
  205. m_uTextStyle |= DT_SINGLELINE;
  206. }
  207. }
  208. else if( _tcsicmp(pstrName, _T("noprefix")) == 0 ) {
  209. if( _tcsicmp(pstrValue, _T("true")) == 0)
  210. {
  211. m_uTextStyle |= DT_NOPREFIX;
  212. }
  213. else
  214. {
  215. m_uTextStyle = m_uTextStyle & ~DT_NOPREFIX;
  216. }
  217. }
  218. else if( _tcsicmp(pstrName, _T("font")) == 0 ) SetFont(_ttoi(pstrValue));
  219. else if( _tcsicmp(pstrName, _T("textcolor")) == 0 ) {
  220. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  221. LPTSTR pstr = NULL;
  222. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  223. SetTextColor(clrColor);
  224. }
  225. else if( _tcsicmp(pstrName, _T("disabledtextcolor")) == 0 ) {
  226. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  227. LPTSTR pstr = NULL;
  228. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  229. SetDisabledTextColor(clrColor);
  230. }
  231. else if( _tcsicmp(pstrName, _T("textpadding")) == 0 ) {
  232. RECT rcTextPadding = { 0 };
  233. LPTSTR pstr = NULL;
  234. rcTextPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  235. rcTextPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  236. rcTextPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  237. rcTextPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  238. SetTextPadding(rcTextPadding);
  239. }
  240. else if( _tcsicmp(pstrName, _T("showhtml")) == 0 ) SetShowHtml(_tcsicmp(pstrValue, _T("true")) == 0);
  241. else if( _tcsicmp(pstrName, _T("autocalcwidth")) == 0 ) {
  242. SetAutoCalcWidth(_tcsicmp(pstrValue, _T("true")) == 0);
  243. }
  244. else if( _tcsicmp(pstrName, _T("autocalcheight")) == 0 ) {
  245. SetAutoCalcHeight(_tcsicmp(pstrValue, _T("true")) == 0);
  246. }
  247. else CControlUI::SetAttribute(pstrName, pstrValue);
  248. }
  249. void CLabelUI::PaintText(HDC hDC)
  250. {
  251. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  252. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  253. RECT rc = m_rcItem;
  254. RECT m_rcTextPadding = CLabelUI::m_rcTextPadding;
  255. GetManager()->GetDPIObj()->Scale(&m_rcTextPadding);
  256. rc.left += m_rcTextPadding.left;
  257. rc.right -= m_rcTextPadding.right;
  258. rc.top += m_rcTextPadding.top;
  259. rc.bottom -= m_rcTextPadding.bottom;
  260. CDuiString sText = GetText();
  261. if( sText.IsEmpty() ) return;
  262. int nLinks = 0;
  263. if( IsEnabled() ) {
  264. if( m_bShowHtml )
  265. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, sText, m_dwTextColor, \
  266. NULL, NULL, nLinks, m_iFont, m_uTextStyle);
  267. else
  268. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwTextColor, \
  269. m_iFont, m_uTextStyle);
  270. }
  271. else {
  272. if( m_bShowHtml )
  273. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, \
  274. NULL, NULL, nLinks, m_iFont, m_uTextStyle);
  275. else
  276. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, \
  277. m_iFont, m_uTextStyle);
  278. }
  279. }
  280. bool CLabelUI::GetAutoCalcWidth() const
  281. {
  282. return m_bAutoCalcWidth;
  283. }
  284. void CLabelUI::SetAutoCalcWidth(bool bAutoCalcWidth)
  285. {
  286. m_bAutoCalcWidth = bAutoCalcWidth;
  287. }
  288. bool CLabelUI::GetAutoCalcHeight() const
  289. {
  290. return m_bAutoCalcHeight;
  291. }
  292. void CLabelUI::SetAutoCalcHeight(bool bAutoCalcHeight)
  293. {
  294. m_bAutoCalcHeight = bAutoCalcHeight;
  295. }
  296. void CLabelUI::SetText( LPCTSTR pstrText )
  297. {
  298. CControlUI::SetText(pstrText);
  299. if(GetAutoCalcWidth() || GetAutoCalcHeight()) {
  300. NeedParentUpdate();
  301. }
  302. }
  303. }