home *** CD-ROM | disk | FTP | other *** search
/ Beginning Direct3D Game Programming / Direct3D.iso / directx / dxf / samples / multimedia / directinput / diconfig / cdeviceviewtext.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-22  |  3.0 KB  |  140 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-2000 Microsoft Corporation. All Rights Reserved.
  10. //-----------------------------------------------------------------------------
  11.  
  12. #include "common.hpp"
  13.  
  14.  
  15. CDeviceViewText::CDeviceViewText(CDeviceUI &ui, CDeviceView &view) :
  16.     m_ui(ui), m_view(view),
  17.     m_hFont(NULL),
  18.     m_rgbText(RGB(255,255,255)),
  19.     m_rgbBk(RGB(0,0,0)),
  20.     m_bWrap(FALSE),
  21.     m_ptszText(NULL)
  22. {
  23.     m_rect.left = m_rect.top = m_rect.right = m_rect.bottom = 0;
  24. }
  25.  
  26. CDeviceViewText::~CDeviceViewText()
  27. {
  28.     if (m_ptszText)
  29.         free(m_ptszText);
  30.     m_ptszText = NULL;
  31. }
  32.  
  33. void CDeviceViewText::SetLook(HFONT a, COLORREF b, COLORREF c)
  34. {
  35.     m_hFont = a;
  36.     m_rgbText = b;
  37.     m_rgbBk = c;
  38.  
  39.     Invalidate();
  40. }
  41.  
  42. void CDeviceViewText::SetPosition(int x, int y)
  43. {
  44.     int w = m_rect.right - m_rect.left;
  45.     int h = m_rect.bottom - m_rect.top;
  46.  
  47.     m_rect.left = x;
  48.     m_rect.right = x + w;
  49.  
  50.     m_rect.top = y;
  51.     m_rect.bottom = y + h;
  52.  
  53.     Invalidate();
  54. }
  55.  
  56. void CDeviceViewText::SetRect(const RECT &r)
  57. {
  58.     m_rect = r;
  59.     Invalidate();
  60. }
  61.  
  62. void CDeviceViewText::_SetText(LPCTSTR t)
  63. {
  64.     if (m_ptszText)
  65.         free(m_ptszText);
  66.     if (t)
  67.         m_ptszText = AllocLPTSTR(t);
  68. }
  69.  
  70. void CDeviceViewText::SetText(LPCTSTR t)
  71. {
  72.     _SetText(t);
  73.     Invalidate(TRUE);
  74. }
  75.  
  76. void CDeviceViewText::SetTextAndResizeTo(LPCTSTR t)
  77. {
  78.     _SetText(t);
  79.     SIZE s = GetTextSize(m_ptszText, m_hFont);
  80.     m_rect.right = m_rect.left + s.cx;
  81.     m_rect.bottom = m_rect.top + s.cy;
  82.     Invalidate(TRUE);
  83. }
  84.  
  85. void CDeviceViewText::SetTextAndResizeToWrapped(LPCTSTR t)
  86. {
  87.     _SetText(t);
  88.     if (!m_ptszText)
  89.     {
  90.         m_rect.right = m_rect.left;
  91.         m_rect.bottom = m_rect.top;
  92.         Invalidate(TRUE);
  93.         return;
  94.     }
  95.     RECT rect = {m_rect.left, m_rect.top, g_sizeImage.cx, m_rect.top + 1};
  96.     HDC hDC = CreateCompatibleDC(NULL);
  97.     if (hDC != NULL)
  98.     {
  99.         HGDIOBJ hOld = NULL;
  100.         if (m_hFont)
  101.             hOld = SelectObject(hDC, m_hFont);
  102.         DrawText(hDC, m_ptszText, -1, &rect, DT_CALCRECT | DT_NOPREFIX | DT_WORDBREAK);
  103.         if (m_hFont)
  104.             SelectObject(hDC, hOld);
  105.         DeleteDC(hDC);
  106.     }
  107.     m_rect = rect;
  108.     m_bWrap = TRUE;
  109.     Invalidate(TRUE);
  110. }
  111.  
  112. void CDeviceViewText::SetWrap(BOOL bWrap)
  113. {
  114.     m_bWrap = bWrap;
  115.     Invalidate();
  116. }
  117.  
  118. void CDeviceViewText::Invalidate(BOOL bForce)
  119. {
  120.     if (m_ptszText || bForce)
  121.         m_view.Invalidate();
  122. }
  123.  
  124. void CDeviceViewText::OnPaint(HDC hDC)
  125. {
  126.     if (!m_ptszText)
  127.         return;
  128.  
  129.     SetTextColor(hDC, m_rgbText);
  130.     SetBkColor(hDC, m_rgbBk);
  131.     SetBkMode(hDC, OPAQUE);
  132.     RECT rect = m_rect;
  133.     HGDIOBJ hOld = NULL;
  134.     if (m_hFont)
  135.         hOld = SelectObject(hDC, m_hFont);
  136.     DrawText(hDC, m_ptszText, -1, &rect, DT_NOPREFIX | (m_bWrap ? DT_WORDBREAK : 0) | DT_RIGHT);
  137.     if (m_hFont)
  138.         SelectObject(hDC, hOld);
  139. }
  140.