home *** CD-ROM | disk | FTP | other *** search
/ Beginning Direct3D Game Programming / Direct3D.iso / directx / dxf / samples / multimedia / directinput / diconfig / flextooltip.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-22  |  6.4 KB  |  218 lines

  1. //-----------------------------------------------------------------------------
  2. // File: flextooltip.cpp
  3. //
  4. // Desc: Implements a tooltip class that displays a text string as a tooltip.
  5. //       CFlexTooltip (derived from CFlexWnd) is used throughout the UI when
  6. //       a control needs to have a tooltip.
  7. //
  8. // Copyright (C) 1999-2000 Microsoft Corporation. All Rights Reserved.
  9. //-----------------------------------------------------------------------------
  10.  
  11. #include "common.hpp"
  12.  
  13. UINT_PTR CFlexToolTip::s_uiTimerID;
  14. DWORD CFlexToolTip::s_dwLastTimeStamp;  // Last time stamp for mouse move
  15. TOOLTIPINIT CFlexToolTip::s_TTParam;  // Parameters to initialize the tooltip
  16.  
  17. // TimerFunc is called periodically.  It checks if a tooltip should be displayed.
  18. // If a window has indicated a need for tooltip, TimerFunc will initialize
  19. // for displaying here.  CFlexWnd will do the actual displaying, since
  20. // it has to monitor WM_MOUSEMOVE message to be sure that tooltips only
  21. // display after a period of inactivity.
  22. void CALLBACK CFlexToolTip::TimerFunc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
  23. {
  24.     // If it has been one sec already since last mouse move, display the tooltip
  25.     if (dwTime > CFlexWnd::s_dwLastMouseMove + 1000)
  26.     {
  27.         if (s_TTParam.hWndParent && !CFlexWnd::s_ToolTip.IsEnabled())
  28.         {
  29.             // Check if the mouse cursor is outside the window.  If so, don't activate tooltip.
  30.             POINT pt;
  31.             RECT rect;
  32.             GetCursorPos(&pt);
  33.             ScreenToClient(s_TTParam.hWndParent, &pt);
  34.             ::GetClientRect(s_TTParam.hWndParent, &rect);
  35.             if (!PtInRect(&rect, pt))
  36.                 return;
  37.  
  38.             SetParent(CFlexWnd::s_ToolTip.m_hWnd, s_TTParam.hWndParent);
  39.             CFlexWnd::s_ToolTip.SetSBWidth(s_TTParam.iSBWidth);
  40.             CFlexWnd::s_ToolTip.SetID(s_TTParam.dwID);
  41.             CFlexWnd::s_ToolTip.SetNotifyWindow(s_TTParam.hWndNotify);
  42.             CFlexWnd::s_ToolTip.SetText(s_TTParam.tszCaption);
  43.             CFlexWnd::s_ToolTip.SetEnable(TRUE);
  44.         }
  45.     }
  46. }
  47.  
  48. // We use the constructor and destructor to load and unload WINMM.DLL since the UI will only create this once.
  49.  
  50. CFlexToolTip::CFlexToolTip() :
  51.     m_tszText(NULL), m_hNotifyWnd(NULL), m_dwID(0), m_bEnabled(FALSE)
  52. {
  53. }
  54.  
  55. CFlexToolTip::~CFlexToolTip()
  56. {
  57.     delete[] m_tszText;
  58. }
  59.  
  60. HWND CFlexToolTip::Create(HWND hParent, const RECT &rect, BOOL bVisible, int iSBWidth)
  61. {
  62.     m_iSBWidth = iSBWidth;
  63.     return CFlexWnd::Create(hParent, rect, bVisible);
  64. }
  65.  
  66. // Set the tooltip position. pt is the upper-left point in screen coordinates.
  67. // bOffsetForMouseCursor is TRUE if the tooltip is to be displayed next to mouse cursor.  SetPosition()
  68. // will offset the position of the tooltip so that the cursor doesn't block the text of the tooltip.
  69. void CFlexToolTip::SetPosition(POINT pt, BOOL bOffsetForMouseCursor)
  70. {
  71.     // Check the top, right and bottom edges.  If they are outside the main config window
  72.     RECT rc;
  73.     RECT cliprc;
  74.     RECT parentrc;
  75.     GetWindowRect(GetParent(), &parentrc);
  76.     GetClientRect(&rc);
  77.     GetWindowRect(GetParent(), &cliprc);
  78.     if (bOffsetForMouseCursor)
  79.     {
  80.         pt.y -= rc.bottom;  // Align the lower left corner to the cursor
  81.         pt.x += 1; pt.y -= 1;  // Offset x and y by 2 pixels so that when the mouse is moved up or right, we don't get over the tooltip window.
  82.     }
  83.     if (pt.y < cliprc.top) pt.y += cliprc.top - pt.y;  // Top
  84.     if (pt.x + rc.right > (cliprc.right - m_iSBWidth)) pt.x += cliprc.right - m_iSBWidth - (pt.x + rc.right);  // Right
  85.     if (pt.y + rc.bottom > cliprc.bottom) pt.y += cliprc.bottom - (pt.y + rc.bottom);  // Bottom
  86.     ScreenToClient(GetParent(), &pt);
  87.     SetWindowPos(m_hWnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE);
  88. }
  89.  
  90. void CFlexToolTip::SetText(LPCTSTR tszText, POINT *textpos)
  91. {
  92.     // Figure out window size and position
  93.     RECT rc = {0, 0, 320, 480};  // Only go to half the window width max
  94.     HDC hDC = CreateCompatibleDC(NULL);
  95.     if (hDC != NULL)
  96.     {
  97.         DrawText(hDC, CFlexToolTip::s_TTParam.tszCaption, -1, &rc, DT_CALCRECT);
  98.     //    DrawText(hDC, m_tszText, -1, &rc, DT_CALCRECT);
  99.         SetWindowPos(m_hWnd, HWND_TOP, 0, 0, rc.right, rc.bottom, 0);  // Set window pos as needed by text
  100.         DeleteDC(hDC);
  101.     }
  102.  
  103.     POINT pt;
  104.     if (textpos)
  105.     {
  106.         pt = *textpos;
  107.         SetPosition(pt, FALSE);
  108.     }
  109.     else
  110.     {
  111.         GetCursorPos(&pt);
  112.         SetPosition(pt);
  113.     }
  114.     SetWindowPos(m_hWnd, HWND_TOP, 0, 0, rc.right, rc.bottom, SWP_NOMOVE);  // Set size
  115. }
  116.  
  117. void CFlexToolTip::OnClick(POINT point, WPARAM fwKeys, BOOL bLeft)
  118. {
  119.     ClientToScreen(m_hWnd, &point);
  120.     ScreenToClient(m_hNotifyWnd, &point);
  121.     if (bLeft)
  122.         PostMessage(m_hNotifyWnd, WM_LBUTTONDOWN, fwKeys, point.x | (point.y << 16));
  123.     else
  124.         PostMessage(m_hNotifyWnd, WM_RBUTTONDOWN, fwKeys, point.x | (point.y << 16));
  125. }
  126.  
  127. void CFlexToolTip::OnDoubleClick(POINT point, WPARAM fwKeys, BOOL bLeft)
  128. {
  129.     ClientToScreen(m_hWnd, &point);
  130.     ScreenToClient(m_hNotifyWnd, &point);
  131.     if (bLeft)
  132.         PostMessage(m_hNotifyWnd, WM_LBUTTONDBLCLK, fwKeys, point.x | (point.y << 16));
  133.     else
  134.         PostMessage(m_hNotifyWnd, WM_RBUTTONDBLCLK, fwKeys, point.x | (point.y << 16));
  135. }
  136.  
  137. void CFlexToolTip::OnPaint(HDC hDC)
  138. {
  139.     HDC hBDC = NULL, hODC = NULL;
  140.     CBitmap *pbm = NULL;
  141.  
  142.     if (!InRenderMode())
  143.     {
  144.         hODC = hDC;
  145.         pbm = CBitmap::Create(GetClientSize(), RGB(0,0,0), hDC);
  146.         if (pbm != NULL)
  147.         {
  148.             hBDC = pbm->BeginPaintInto();
  149.             if (hBDC != NULL)
  150.             {
  151.                 hDC = hBDC;
  152.             }
  153.         }
  154.     }
  155.  
  156.     InternalPaint(hDC);
  157.  
  158.     if (!InRenderMode())
  159.     {
  160.         if (pbm != NULL)
  161.         {
  162.             if (hBDC != NULL)
  163.             {
  164.                 pbm->EndPaintInto(hBDC);
  165.                 pbm->Draw(hODC);
  166.             }
  167.             delete pbm;
  168.         }
  169.     }
  170. }
  171.  
  172. void CFlexToolTip::InternalPaint(HDC hDC)
  173. {
  174.     HGDIOBJ hPen = (HGDIOBJ)CreatePen(PS_SOLID, 1, m_rgbBk);
  175.     if (hPen != NULL)
  176.     {
  177.         HGDIOBJ hOldPen = SelectObject(hDC, hPen);
  178.  
  179.         HGDIOBJ hBrush = CreateSolidBrush(m_rgbBk);
  180.         if (hBrush != NULL)
  181.         {
  182.             HGDIOBJ hOldBrush = SelectObject(hDC, hBrush);
  183.             RECT rc = {0,0,0,0};
  184.  
  185.             GetClientRect(&rc);
  186.             DrawText(hDC, CFlexToolTip::s_TTParam.tszCaption, -1, &rc, DT_LEFT);
  187.  
  188.             SelectObject(hDC, hOldBrush);
  189.             DeleteObject(hBrush);
  190.         }
  191.  
  192.         SelectObject(hDC, hOldPen);
  193.         DeleteObject(hPen);
  194.     }
  195. }
  196.  
  197. LRESULT CFlexToolTip::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  198. {
  199.     return CFlexWnd::WndProc(hWnd, msg, wParam, lParam);
  200. }
  201.  
  202. LRESULT CFlexToolTip::OnCreate(LPCREATESTRUCT lpCreateStruct)
  203. {
  204.     // Create a timer
  205.     CFlexToolTip::s_uiTimerID = SetTimer(m_hWnd, 6, 50, CFlexToolTip::TimerFunc);
  206.     return 0;
  207. }
  208.  
  209. void CFlexToolTip::OnDestroy()
  210. {
  211.     // Kill the timer
  212.     if (CFlexToolTip::s_uiTimerID)
  213.     {
  214.         KillTimer(m_hWnd, 6);
  215.         CFlexToolTip::s_uiTimerID = 0;
  216.     }
  217. }
  218.