Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.4 KiB

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