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.

201 lines
4.8 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. //////////////////////////////////////////////////////////////////////////////
  10. // CSliderValue
  11. const short g_sMaxContinuousTicks = 200;
  12. const int g_iMaxCharBuffer = 50; // # characters big enough to hold -FLT_MAX with room to spare
  13. CSliderValue::CSliderValue()
  14. : m_fInit(false)
  15. {
  16. }
  17. void CSliderValue::SetRange(float fMin, float fMax)
  18. {
  19. if (m_fInit)
  20. {
  21. m_fMin = fMin;
  22. m_fMax = fMax;
  23. short sMin;
  24. short sMax;
  25. short sTicks = 4; // Lots of ticks become less useful as guides. Use quarters for fine-grained sliders.
  26. if (m_fDiscrete)
  27. {
  28. sMin = static_cast<short>(fMin);
  29. sMax = static_cast<short>(fMax);
  30. if (sMax - sMin <= 10)
  31. sTicks = sMax - sMin;
  32. }
  33. else
  34. {
  35. sMin = 0;
  36. sMax = g_sMaxContinuousTicks;
  37. }
  38. SendMessage(m_hwndSlider, TBM_SETRANGE, TRUE, MAKELONG(sMin, sMax));
  39. SendMessage(m_hwndSlider, TBM_SETTICFREQ, (sMax - sMin) / sTicks, 0);
  40. }
  41. }
  42. void CSliderValue::Init(
  43. HWND hwndSlider,
  44. HWND hwndEdit,
  45. float fMin,
  46. float fMax,
  47. bool fDiscrete)
  48. {
  49. if (m_fInit)
  50. return;
  51. m_hwndSlider = hwndSlider;
  52. m_hwndEdit = hwndEdit;
  53. m_fDiscrete = fDiscrete;
  54. m_fInit = true;
  55. SetRange(fMin,fMax);
  56. }
  57. void CSliderValue::SetValue(float fPos)
  58. {
  59. if (!m_fInit)
  60. return;
  61. UpdateEditBox(fPos);
  62. UpdateSlider();
  63. }
  64. float CSliderValue::GetValue()
  65. {
  66. if (!m_fInit)
  67. return 0;
  68. LRESULT lrLen = SendMessage(m_hwndEdit, WM_GETTEXTLENGTH, 0, 0);
  69. if (lrLen >= g_iMaxCharBuffer)
  70. return 0;
  71. char szText[g_iMaxCharBuffer] = "";
  72. SendMessage(m_hwndEdit, WM_GETTEXT, g_iMaxCharBuffer, reinterpret_cast<LPARAM>(szText));
  73. float fVal = static_cast<float>(m_fDiscrete ? atoi(szText) : atof(szText));
  74. if (fVal < m_fMin) fVal = m_fMin;
  75. if (fVal > m_fMax) fVal = m_fMax;
  76. return fVal;
  77. }
  78. float CSliderValue::GetSliderValue()
  79. {
  80. short sPos = static_cast<short>(SendMessage(m_hwndSlider, TBM_GETPOS, 0, 0));
  81. if (m_fDiscrete)
  82. {
  83. return sPos;
  84. }
  85. float fRet = (m_fMax - m_fMin) * sPos / g_sMaxContinuousTicks + m_fMin;
  86. return fRet;
  87. }
  88. LRESULT CSliderValue::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  89. {
  90. if (!m_fInit)
  91. return FALSE;
  92. switch (uMsg)
  93. {
  94. case WM_HSCROLL:
  95. if (bHandled = (reinterpret_cast<HWND>(lParam) == m_hwndSlider && LOWORD(wParam) >= TB_LINEUP && LOWORD(wParam) <= TB_ENDTRACK))
  96. UpdateEditBox(GetSliderValue());
  97. break;
  98. case WM_COMMAND:
  99. if (bHandled = (HIWORD(wParam) == EN_KILLFOCUS && reinterpret_cast<HWND>(lParam) == m_hwndEdit))
  100. UpdateSlider();
  101. break;
  102. default:
  103. bHandled = FALSE;
  104. break;
  105. }
  106. return 0;
  107. }
  108. void CSliderValue::UpdateEditBox(float fPos)
  109. {
  110. char szText[g_iMaxCharBuffer] = "";
  111. if (m_fDiscrete)
  112. {
  113. short sPos = static_cast<short>(fPos);
  114. sprintf(szText, "%hd", sPos);
  115. }
  116. else
  117. {
  118. sprintf(szText, "%.2hf", fPos);
  119. }
  120. SendMessage(m_hwndEdit, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(szText));
  121. }
  122. void CSliderValue::UpdateSlider()
  123. {
  124. float fVal = GetValue();
  125. short sPos = static_cast<short>(m_fDiscrete ? fVal : g_sMaxContinuousTicks * ((fVal - m_fMin) / (m_fMax - m_fMin)));
  126. SendMessage(m_hwndSlider, TBM_SETPOS, TRUE, sPos);
  127. UpdateEditBox(fVal); // this resets the input box back to the set float value in case the input was invalid
  128. }
  129. CComboHelp::CComboHelp()
  130. {
  131. m_hwndCombo = NULL;
  132. m_fInit = FALSE;
  133. }
  134. void CComboHelp::Init(HWND hwndCombo, int nID, char *pStrings[], DWORD cbStrings)
  135. {
  136. DWORD dwIndex;
  137. m_hwndCombo = hwndCombo;
  138. m_nID = nID;
  139. for (dwIndex = 0; dwIndex < cbStrings; dwIndex++)
  140. {
  141. SendMessage( hwndCombo,CB_ADDSTRING,0,(LPARAM)pStrings[dwIndex]);
  142. }
  143. m_fInit = TRUE;
  144. }
  145. void CComboHelp::SetValue(DWORD dwValue)
  146. {
  147. SendMessage(m_hwndCombo,CB_SETCURSEL,dwValue,0);
  148. }
  149. DWORD CComboHelp::GetValue()
  150. {
  151. return (DWORD) SendMessage( m_hwndCombo,CB_GETCURSEL,0,0);
  152. }
  153. LRESULT CComboHelp::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  154. {
  155. if (!m_fInit)
  156. return FALSE;
  157. switch (uMsg)
  158. {
  159. case WM_COMMAND:
  160. bHandled = ((HIWORD(wParam) == CBN_SELCHANGE) && (LOWORD(wParam) == m_nID));
  161. break;
  162. default:
  163. bHandled = FALSE;
  164. break;
  165. }
  166. return TRUE;
  167. }