home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Bin / DXUtils / Visual Studio 6.0 Wizards / AEDMOWiz.awx / TEMPLATE / CONTROLHELP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-12-11  |  5.9 KB  |  235 lines

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