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.

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