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.

249 lines
7.4 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998
  4. *
  5. * TITLE: SLIDEDIT.H
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 1/10/2000
  12. *
  13. * DESCRIPTION: Encapsulate the slider and edit and preview control interaction
  14. *
  15. *******************************************************************************/
  16. #ifndef __SLIDEDIT_H_INCLUDED
  17. #define __SLIDEDIT_H_INCLUDED
  18. #include <windows.h>
  19. #include "vwiaset.h"
  20. class CSliderAndEdit
  21. {
  22. private:
  23. HWND m_hWndSlider;
  24. HWND m_hWndEdit;
  25. HWND m_hWndPreview;
  26. UINT m_nPreviewMessage;
  27. CValidWiaSettings *m_pValidWiaSettings;
  28. public:
  29. CSliderAndEdit(void)
  30. : m_hWndSlider(NULL),
  31. m_hWndEdit(NULL),
  32. m_hWndPreview(NULL),
  33. m_nPreviewMessage(0),
  34. m_pValidWiaSettings(NULL)
  35. {
  36. }
  37. bool Initialize( HWND hWndSlider, HWND hWndEdit, HWND hWndPreview, UINT nPreviewMessage, CValidWiaSettings *pValidWiaSettings )
  38. {
  39. //
  40. // Save all of these settings
  41. //
  42. m_hWndSlider = hWndSlider;
  43. m_hWndEdit = hWndEdit;
  44. m_hWndPreview = hWndPreview;
  45. m_pValidWiaSettings = pValidWiaSettings;
  46. m_nPreviewMessage = nPreviewMessage;
  47. //
  48. // Make sure these are valid
  49. //
  50. if (m_hWndSlider && m_hWndEdit && m_pValidWiaSettings)
  51. {
  52. //
  53. // Set up the slider
  54. //
  55. SendMessage( m_hWndSlider, TBM_SETRANGE, TRUE, MAKELONG( 0, m_pValidWiaSettings->GetItemCount()-1 ) );
  56. //
  57. // Set the control's values
  58. //
  59. SetValue( m_pValidWiaSettings->InitialValue() );
  60. //
  61. // Everything is OK
  62. //
  63. return true;
  64. }
  65. return false;
  66. }
  67. void SetValue( LONG nValue )
  68. {
  69. if (IsValid())
  70. {
  71. //
  72. // Get the index of the intial value and set the position of the slider
  73. //
  74. int nIndex = m_pValidWiaSettings->FindIndexOfItem( nValue );
  75. if (nIndex >= 0)
  76. {
  77. SendMessage( m_hWndSlider, TBM_SETPOS, TRUE, nIndex );
  78. }
  79. //
  80. // Set up the preview control
  81. //
  82. if (m_hWndPreview && m_nPreviewMessage)
  83. {
  84. SendMessage( m_hWndPreview, m_nPreviewMessage, 0, ConvertToPreviewRange(nValue) );
  85. }
  86. //
  87. // Set up the edit control
  88. //
  89. SetDlgItemInt( GetParent(m_hWndEdit), GetWindowLong(m_hWndEdit,GWL_ID), nValue, TRUE );
  90. }
  91. }
  92. void Restore(void)
  93. {
  94. if (IsValid())
  95. {
  96. SetValue( m_pValidWiaSettings->InitialValue() );
  97. }
  98. }
  99. bool ValidateEditControl(void)
  100. {
  101. BOOL bSuccess = FALSE;
  102. if (IsValid())
  103. {
  104. //
  105. // Get the current value
  106. //
  107. LONG nValue = static_cast<LONG>(GetDlgItemInt( GetParent(m_hWndEdit), GetWindowLong(m_hWndEdit,GWL_ID), &bSuccess, TRUE ));
  108. if (bSuccess)
  109. {
  110. //
  111. // Assume it isn't a valid value
  112. //
  113. bSuccess = FALSE;
  114. //
  115. // Check to see if the edit control has a legal value in it
  116. //
  117. LONG nTestValue = nValue;
  118. if (m_pValidWiaSettings->FindClosestValue(nTestValue))
  119. {
  120. if (nValue == nTestValue)
  121. {
  122. bSuccess = TRUE;
  123. }
  124. }
  125. }
  126. }
  127. return (bSuccess != FALSE);
  128. }
  129. bool IsValid(void) const
  130. {
  131. return (m_hWndSlider && m_hWndEdit && m_pValidWiaSettings);
  132. }
  133. LONG ConvertToPreviewRange(LONG nValue) const
  134. {
  135. if (IsValid())
  136. {
  137. //
  138. // Convert the value to the range 0...100
  139. //
  140. nValue = ((nValue-m_pValidWiaSettings->Min()) * 100) / (m_pValidWiaSettings->Max() - m_pValidWiaSettings->Min());
  141. }
  142. return nValue;
  143. }
  144. void HandleSliderUpdate(void)
  145. {
  146. WIA_PUSH_FUNCTION((TEXT("CSliderAndEdit::HandleSliderUpdate")));
  147. if (IsValid())
  148. {
  149. //
  150. // Find out what the current index is
  151. //
  152. int nIndex = static_cast<int>(SendMessage( m_hWndSlider, TBM_GETPOS, 0, 0 ));
  153. WIA_TRACE((TEXT("nIndex = %d"), nIndex ));
  154. //
  155. // Get the value at that index, if it is valid set the edit control's text
  156. //
  157. LONG nValue;
  158. if (m_pValidWiaSettings->GetItemAtIndex(nIndex,nValue))
  159. {
  160. WIA_TRACE((TEXT("nValue = %d"), nValue ));
  161. SetDlgItemInt( GetParent(m_hWndEdit), GetWindowLong(m_hWndEdit,GWL_ID), nValue, TRUE );
  162. }
  163. //
  164. // If the preview window is valid, send it a message
  165. //
  166. if (m_nPreviewMessage && m_hWndPreview)
  167. {
  168. SendMessage( m_hWndPreview, m_nPreviewMessage, 0, ConvertToPreviewRange(nValue) );
  169. }
  170. }
  171. }
  172. LONG GetValueFromCurrentPos(void)
  173. {
  174. WIA_PUSH_FUNCTION((TEXT("CSliderAndEdit::GetValueFromCurrentPos")));
  175. if (IsValid())
  176. {
  177. //
  178. // Find out what the current index is
  179. //
  180. int nIndex = static_cast<int>(SendMessage( m_hWndSlider, TBM_GETPOS, 0, 0 ));
  181. WIA_TRACE((TEXT("nIndex = %d"), nIndex ));
  182. //
  183. // Get the value at that index, if it is valid return it
  184. //
  185. LONG nValue;
  186. if (m_pValidWiaSettings->GetItemAtIndex(nIndex,nValue))
  187. {
  188. return nValue;
  189. }
  190. return m_pValidWiaSettings->Min();
  191. }
  192. return 0;
  193. }
  194. void HandleEditUpdate(void)
  195. {
  196. WIA_PUSH_FUNCTION((TEXT("CSliderAndEdit::HandleSliderUpdate")));
  197. if (IsValid())
  198. {
  199. //
  200. // Get the current value
  201. //
  202. BOOL bSuccess = FALSE;
  203. LONG nValue = static_cast<LONG>(GetDlgItemInt( GetParent(m_hWndEdit), GetWindowLong(m_hWndEdit,GWL_ID), &bSuccess, TRUE ));
  204. if (bSuccess)
  205. {
  206. //
  207. // Round it and send it to the slider control
  208. //
  209. if (m_pValidWiaSettings->FindClosestValue(nValue))
  210. {
  211. int nIndex = m_pValidWiaSettings->FindIndexOfItem( nValue );
  212. if (nIndex >= 0)
  213. {
  214. SendMessage( m_hWndSlider, TBM_SETPOS, TRUE, nIndex );
  215. }
  216. //
  217. // If the preview window is valid, send it a message
  218. //
  219. if (m_nPreviewMessage && m_hWndPreview)
  220. {
  221. SendMessage( m_hWndPreview, m_nPreviewMessage, 0, ConvertToPreviewRange(nValue) );
  222. }
  223. }
  224. }
  225. }
  226. }
  227. };
  228. #endif //__SLIDEDIT_H_INCLUDED