home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectInput / DIConfig / cdeviceviewtext.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  4.3 KB  |  192 lines

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