Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
4.1 KiB

  1. //-----------------------------------------------------------------------------
  2. // File: cdeviceviewtext.cpp
  3. //
  4. // Desc: CDeviceViewText is a class representing a text string in the view
  5. // window. It is used when the view type is a list view. CDeviceViewText
  6. // will print the text of the control name, while CDeviceControl will
  7. // print the text of the action assigned to that control.
  8. //
  9. // Copyright (C) 1999-2000 Microsoft Corporation. All Rights Reserved.
  10. //-----------------------------------------------------------------------------
  11. #include "common.hpp"
  12. #include <string.h>
  13. CDeviceViewText::CDeviceViewText(CDeviceUI &ui, CDeviceView &view) :
  14. m_ui(ui), m_view(view),
  15. m_hFont(NULL),
  16. m_rgbText(RGB(255,255,255)),
  17. m_rgbBk(RGB(0,0,0)),
  18. m_bWrap(FALSE),
  19. m_bClipped(FALSE),
  20. m_ptszText(NULL)
  21. {
  22. m_rect.left = m_rect.top = m_rect.right = m_rect.bottom = 0;
  23. }
  24. CDeviceViewText::~CDeviceViewText()
  25. {
  26. if (m_ptszText)
  27. free(m_ptszText);
  28. m_ptszText = NULL;
  29. }
  30. void CDeviceViewText::SetLook(HFONT a, COLORREF b, COLORREF c)
  31. {
  32. m_hFont = a;
  33. m_rgbText = b;
  34. m_rgbBk = c;
  35. Invalidate();
  36. }
  37. void CDeviceViewText::SetPosition(int x, int y)
  38. {
  39. int w = m_rect.right - m_rect.left;
  40. int h = m_rect.bottom - m_rect.top;
  41. m_rect.left = x;
  42. m_rect.right = x + w;
  43. m_rect.top = y;
  44. m_rect.bottom = y + h;
  45. Invalidate();
  46. }
  47. void CDeviceViewText::SetRect(const RECT &r)
  48. {
  49. m_rect = r;
  50. CheckClipped();
  51. Invalidate();
  52. }
  53. void CDeviceViewText::_SetText(LPCTSTR t)
  54. {
  55. if (m_ptszText)
  56. free(m_ptszText);
  57. if (t)
  58. m_ptszText = AllocLPTSTR(t);
  59. }
  60. // Check if the text is clipped when printed and set flag appropriately.
  61. void CDeviceViewText::CheckClipped()
  62. {
  63. RECT rect = m_rect;
  64. HDC hDC = CreateCompatibleDC(NULL);
  65. if (hDC != NULL)
  66. {
  67. HGDIOBJ hOld = NULL;
  68. if (m_hFont)
  69. hOld = SelectObject(hDC, m_hFont);
  70. DrawText(hDC, m_ptszText, -1, &rect, DT_CALCRECT | DT_NOPREFIX | DT_LEFT);
  71. if (m_hFont)
  72. SelectObject(hDC, hOld);
  73. DeleteDC(hDC);
  74. }
  75. if (rect.right > m_rect.right || rect.bottom > m_rect.bottom)
  76. m_bClipped = TRUE;
  77. else
  78. m_bClipped = FALSE;
  79. }
  80. void CDeviceViewText::SetText(LPCTSTR t)
  81. {
  82. _SetText(t);
  83. CheckClipped();
  84. Invalidate(TRUE);
  85. }
  86. void CDeviceViewText::SetTextAndResizeTo(LPCTSTR t)
  87. {
  88. _SetText(t);
  89. SIZE s = GetTextSize(m_ptszText, m_hFont);
  90. m_rect.right = m_rect.left + s.cx;
  91. m_rect.bottom = m_rect.top + s.cy;
  92. CheckClipped();
  93. Invalidate(TRUE);
  94. }
  95. void CDeviceViewText::SetTextAndResizeToWrapped(LPCTSTR t)
  96. {
  97. _SetText(t);
  98. if (!m_ptszText)
  99. {
  100. m_rect.right = m_rect.left;
  101. m_rect.bottom = m_rect.top;
  102. Invalidate(TRUE);
  103. return;
  104. }
  105. RECT rect = {m_rect.left, m_rect.top, g_sizeImage.cx, m_rect.top + 1};
  106. HDC hDC = CreateCompatibleDC(NULL);
  107. if (hDC != NULL)
  108. {
  109. HGDIOBJ hOld = NULL;
  110. if (m_hFont)
  111. hOld = SelectObject(hDC, m_hFont);
  112. DrawText(hDC, m_ptszText, -1, &rect, DT_CALCRECT | DT_NOPREFIX | DT_WORDBREAK);
  113. if (m_hFont)
  114. SelectObject(hDC, hOld);
  115. DeleteDC(hDC);
  116. }
  117. m_rect = rect;
  118. m_bWrap = TRUE;
  119. CheckClipped();
  120. Invalidate(TRUE);
  121. }
  122. void CDeviceViewText::SetWrap(BOOL bWrap)
  123. {
  124. m_bWrap = bWrap;
  125. Invalidate();
  126. }
  127. void CDeviceViewText::Invalidate(BOOL bForce)
  128. {
  129. if (m_ptszText || bForce)
  130. m_view.Invalidate();
  131. }
  132. void CDeviceViewText::OnPaint(HDC hDC)
  133. {
  134. if (!m_ptszText)
  135. return;
  136. SetTextColor(hDC, m_rgbText);
  137. SetBkColor(hDC, m_rgbBk);
  138. SetBkMode(hDC, OPAQUE);
  139. RECT rect = m_rect;
  140. HGDIOBJ hOld = NULL;
  141. if (m_hFont)
  142. hOld = SelectObject(hDC, m_hFont);
  143. DrawText(hDC, m_ptszText, -1, &rect, DT_NOPREFIX | (m_bWrap ? DT_WORDBREAK : 0) | DT_RIGHT | DT_END_ELLIPSIS);
  144. if (m_hFont)
  145. SelectObject(hDC, hOld);
  146. }
  147. // We will have to know the view's scrolling offset to adjust the tooltip's position.
  148. void CDeviceViewText::OnMouseOver(POINT point)
  149. {
  150. // Tooltip only if the callout text is clipped.
  151. if (m_bClipped)
  152. {
  153. TOOLTIPINITPARAM ttip;
  154. ttip.hWndParent = GetParent(m_view.m_hWnd); // Parent is the page window.
  155. ttip.iSBWidth = 0;
  156. ttip.dwID = 0;
  157. ttip.hWndNotify = m_view.m_hWnd;
  158. ttip.tszCaption = GetText();
  159. CFlexToolTip::UpdateToolTipParam(ttip);
  160. } else
  161. CFlexWnd::s_ToolTip.SetToolTipParent(NULL);
  162. }
  163. DEVCTRLHITRESULT CDeviceViewText::HitTest(POINT test)
  164. {
  165. if (PtInRect(&m_rect, test))
  166. return DCHT_CAPTION;
  167. return DCHT_NOHIT;
  168. }