home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / DMO / GargleDMO / ControlBase / controlhelp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  5.7 KB  |  225 lines

  1. //------------------------------------------------------------------------------
  2. // File: ControlHelp.cpp
  3. //
  4. // Desc: DirectShow sample code - implementation of CSliderValue and
  5. //       CRadioChoice classes.
  6. //
  7. // Copyright (c) 2000-2001 Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10.  
  11. #include <windows.h>
  12. #include "ControlHelp.h"
  13. #include <commctrl.h>
  14. #include <stdio.h>
  15. #include <tchar.h>
  16. #include <atlbase.h>
  17.  
  18. //////////////////////////////////////////////////////////////////////////////
  19. // CSliderValue
  20.  
  21. const short g_sMaxContinuousTicks = 100;
  22. const int g_iMaxCharBuffer = 50; // # characters big enough to hold -FLT_MAX with room to spare
  23.  
  24. CSliderValue::CSliderValue()
  25.   : m_fInit(false)
  26. {
  27. }
  28.  
  29. void CSliderValue::Init(
  30.         HWND        hwndSlider,
  31.         HWND        hwndEdit,
  32.         float       fMin, 
  33.         float       fMax, 
  34.         bool        fDiscrete)
  35. {
  36.     m_hwndSlider = hwndSlider;
  37.     m_hwndEdit = hwndEdit;
  38.     m_fMin = fMin;
  39.     m_fMax = fMax;
  40.     m_fDiscrete = fDiscrete;
  41.  
  42.     short sMin;
  43.     short sMax;
  44.     short sTicks = 4; // Lots of ticks become less useful as guides.  Use quarters for fine-grained sliders.
  45.     if (m_fDiscrete) 
  46.     {
  47.         sMin = static_cast<short>(fMin);
  48.         sMax = static_cast<short>(fMax);
  49.         if (sMax - sMin <= 10)
  50.             sTicks = (short) (sMax - sMin);
  51.     }
  52.     else
  53.     {
  54.         sMin = 0;
  55.         sMax = g_sMaxContinuousTicks;
  56.     }
  57.     
  58.     SendMessage(m_hwndSlider, TBM_SETRANGE, TRUE, MAKELONG(sMin, sMax));
  59.     SendMessage(m_hwndSlider, TBM_SETTICFREQ, (sMax - sMin) / sTicks, 0);
  60.     m_fInit = true;
  61. }
  62.  
  63. void CSliderValue::SetValue(float fPos)
  64. {
  65.     if (!m_fInit)
  66.         return;
  67.  
  68.     UpdateEditBox(fPos);
  69.     UpdateSlider();
  70. }
  71.  
  72. float CSliderValue::GetValue()
  73. {
  74.     if (!m_fInit)
  75.         return 0;
  76.  
  77.     LRESULT lrLen = SendMessage(m_hwndEdit, WM_GETTEXTLENGTH, 0, 0);
  78.     if (lrLen >= g_iMaxCharBuffer)
  79.         return 0;
  80.  
  81.     TCHAR szText[g_iMaxCharBuffer] = TEXT("");
  82.     SendMessage(m_hwndEdit, WM_GETTEXT, g_iMaxCharBuffer, reinterpret_cast<LPARAM>(szText));
  83.  
  84.     USES_CONVERSION;
  85.     float fVal = static_cast<float>(m_fDiscrete ? _ttoi(szText) : atof(T2A(szText)));
  86.  
  87.     if (fVal < m_fMin) fVal = m_fMin;
  88.     if (fVal > m_fMax) fVal = m_fMax;
  89.     return fVal;
  90. }
  91.  
  92. float CSliderValue::GetSliderValue()
  93. {
  94.     short sPos = static_cast<short>(SendMessage(m_hwndSlider, TBM_GETPOS, 0, 0));
  95.     if (m_fDiscrete)
  96.     {
  97.         return sPos;
  98.     }
  99.  
  100.     float fRet = (m_fMax - m_fMin) * sPos / g_sMaxContinuousTicks + m_fMin;
  101.     return fRet;
  102. }
  103.  
  104. LRESULT CSliderValue::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  105. {
  106.     if (!m_fInit)
  107.         return FALSE;
  108.  
  109.     bHandled = FALSE;
  110.  
  111.     switch (uMsg)
  112.     {
  113.     case WM_HSCROLL:
  114.         if (reinterpret_cast<HWND>(lParam) == m_hwndSlider && LOWORD(wParam) >= TB_LINEUP && LOWORD(wParam) <= TB_ENDTRACK)
  115.         {
  116.             UpdateEditBox(GetSliderValue());
  117.             bHandled = TRUE;
  118.         }
  119.         break;
  120.  
  121.     case WM_COMMAND:
  122.         if (HIWORD(wParam) == EN_KILLFOCUS && reinterpret_cast<HWND>(lParam) == m_hwndEdit)
  123.         {
  124.             UpdateSlider();
  125.             bHandled = TRUE;
  126.         }
  127.         break;
  128.     }
  129.  
  130.     return 0;
  131. }
  132.  
  133. void CSliderValue::UpdateEditBox(float fPos)
  134. {
  135.     TCHAR szText[g_iMaxCharBuffer] = TEXT("");
  136.  
  137.     if (m_fDiscrete)
  138.     {
  139.         short sPos = static_cast<short>(fPos);
  140.         wsprintf(szText, TEXT("%hd"), sPos);
  141.     }
  142.     else
  143.     {
  144.         wsprintf(szText, TEXT("%.3hf"), fPos);
  145.     }
  146.  
  147.     SendMessage(m_hwndEdit, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(szText));
  148. }
  149.  
  150. void CSliderValue::UpdateSlider()
  151. {
  152.     float fVal = GetValue();
  153.     short sPos = static_cast<short>(m_fDiscrete ? fVal : g_sMaxContinuousTicks * ((fVal - m_fMin) / (m_fMax - m_fMin)));
  154.     SendMessage(m_hwndSlider, TBM_SETPOS, TRUE, sPos);
  155.     UpdateEditBox(fVal); // this resets the input box back to the set float value in case the input was invalid
  156. }
  157.  
  158. //////////////////////////////////////////////////////////////////////////////
  159. // CSliderValue
  160.  
  161. CRadioChoice::CRadioChoice(const ButtonEntry *pButtonInfo)
  162.   : m_pButtonInfo(pButtonInfo)
  163. {
  164. }
  165.  
  166. void CRadioChoice::SetChoice(HWND hDlg, LONG lValue)
  167. {
  168.     for (const ButtonEntry *p = m_pButtonInfo; p->nIDDlgItem; ++p)
  169.     {
  170.         if (p->lValue == lValue)
  171.         {
  172.             CheckDlgButton(hDlg, p->nIDDlgItem, BST_CHECKED);
  173.             return;
  174.         }
  175.     }
  176. }
  177.  
  178. LONG CRadioChoice::GetChoice(HWND hDlg)
  179. {
  180.     for (const ButtonEntry *p = m_pButtonInfo; p->nIDDlgItem; ++p)
  181.     {
  182.         if (BST_CHECKED == IsDlgButtonChecked(hDlg, p->nIDDlgItem))
  183.         {
  184.             return p->lValue;
  185.         }
  186.     }
  187.  
  188.     return 0;
  189. }
  190.  
  191. LRESULT CRadioChoice::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  192. {
  193.     bHandled = FALSE;
  194.     UNREFERENCED_PARAMETER(lParam);
  195.  
  196.     if (uMsg == WM_COMMAND && HIWORD(wParam) == BN_CLICKED)
  197.     {
  198.         for (const ButtonEntry *p = m_pButtonInfo; p->nIDDlgItem; ++p)
  199.         {
  200.             if (p->nIDDlgItem == LOWORD(wParam))
  201.             {
  202.                 bHandled = TRUE;
  203.                 return 0;
  204.             }
  205.         }
  206.     }
  207.  
  208.     return 0;
  209. }
  210.  
  211. //////////////////////////////////////////////////////////////////////////////
  212. // MessageHandlerChain
  213.  
  214. LRESULT MessageHandlerChain(Handler **ppHandlers, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  215. {
  216.     LRESULT lr = 0;
  217.     bHandled = FALSE;
  218.  
  219.     for (Handler **pp = ppHandlers; *pp && !bHandled; ++pp)
  220.     {
  221.         lr = (*pp)->MessageHandler(uMsg, wParam, lParam, bHandled);
  222.     }
  223.     return lr;
  224. }
  225.