home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / directshow / dmo / gargledmo / controlbase / controlhelp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-02  |  5.5 KB  |  221 lines

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