UISlider.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "StdAfx.h"
  2. #include "UISlider.h"
  3. namespace DuiLib
  4. {
  5. CSliderUI::CSliderUI() : m_uButtonState(0), m_nStep(1)
  6. {
  7. m_uTextStyle = DT_SINGLELINE | DT_CENTER;
  8. m_szThumb.cx = m_szThumb.cy = 10;
  9. }
  10. LPCTSTR CSliderUI::GetClass() const
  11. {
  12. return _T("SliderUI");
  13. }
  14. UINT CSliderUI::GetControlFlags() const
  15. {
  16. if( IsEnabled() ) return UIFLAG_SETCURSOR | UIFLAG_TABSTOP;
  17. else return 0;
  18. }
  19. LPVOID CSliderUI::GetInterface(LPCTSTR pstrName)
  20. {
  21. if( _tcscmp(pstrName, DUI_CTR_SLIDER) == 0 ) return static_cast<CSliderUI*>(this);
  22. return CProgressUI::GetInterface(pstrName);
  23. }
  24. void CSliderUI::SetEnabled(bool bEnable)
  25. {
  26. CControlUI::SetEnabled(bEnable);
  27. if( !IsEnabled() ) {
  28. m_uButtonState = 0;
  29. }
  30. }
  31. int CSliderUI::GetChangeStep()
  32. {
  33. return m_nStep;
  34. }
  35. void CSliderUI::SetChangeStep(int step)
  36. {
  37. m_nStep = step;
  38. }
  39. void CSliderUI::SetThumbSize(SIZE szXY)
  40. {
  41. m_szThumb = szXY;
  42. }
  43. RECT CSliderUI::GetThumbRect() const
  44. {
  45. if( m_bHorizontal ) {
  46. int left = m_rcItem.left + (m_rcItem.right - m_rcItem.left - m_szThumb.cx) * (m_nValue - m_nMin) / (m_nMax - m_nMin);
  47. int top = (m_rcItem.bottom + m_rcItem.top - m_szThumb.cy) / 2;
  48. return CDuiRect(left, top, left + m_szThumb.cx, top + m_szThumb.cy);
  49. }
  50. else {
  51. int left = (m_rcItem.right + m_rcItem.left - m_szThumb.cx) / 2;
  52. int top = m_rcItem.bottom - m_szThumb.cy - (m_rcItem.bottom - m_rcItem.top - m_szThumb.cy) * (m_nValue - m_nMin) / (m_nMax - m_nMin);
  53. return CDuiRect(left, top, left + m_szThumb.cx, top + m_szThumb.cy);
  54. }
  55. }
  56. LPCTSTR CSliderUI::GetThumbImage() const
  57. {
  58. return m_diThumb.sDrawString;
  59. }
  60. void CSliderUI::SetThumbImage(LPCTSTR pStrImage)
  61. {
  62. if( m_diThumb.sDrawString == pStrImage && m_diThumb.pImageInfo != NULL ) return;
  63. m_diThumb.Clear();
  64. m_diThumb.sDrawString = pStrImage;
  65. Invalidate();
  66. }
  67. LPCTSTR CSliderUI::GetThumbHotImage() const
  68. {
  69. return m_diThumbHot.sDrawString;
  70. }
  71. void CSliderUI::SetThumbHotImage(LPCTSTR pStrImage)
  72. {
  73. if( m_diThumbHot.sDrawString == pStrImage && m_diThumbHot.pImageInfo != NULL ) return;
  74. m_diThumbHot.Clear();
  75. m_diThumbHot.sDrawString = pStrImage;
  76. Invalidate();
  77. }
  78. LPCTSTR CSliderUI::GetThumbPushedImage() const
  79. {
  80. return m_diThumbPushed.sDrawString;
  81. }
  82. void CSliderUI::SetThumbPushedImage(LPCTSTR pStrImage)
  83. {
  84. if( m_diThumbPushed.sDrawString == pStrImage && m_diThumbPushed.pImageInfo != NULL ) return;
  85. m_diThumbPushed.Clear();
  86. m_diThumbPushed.sDrawString = pStrImage;
  87. Invalidate();
  88. }
  89. void CSliderUI::DoEvent(TEventUI& event)
  90. {
  91. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  92. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  93. else CProgressUI::DoEvent(event);
  94. return;
  95. }
  96. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )
  97. {
  98. if( IsEnabled() ) {
  99. /*RECT rcThumb = GetThumbRect();
  100. if( ::PtInRect(&rcThumb, event.ptMouse) ) {
  101. m_uButtonState |= UISTATE_CAPTURED;
  102. }
  103. }
  104. return;*/
  105. m_uButtonState |= UISTATE_CAPTURED;
  106. int nValue;
  107. if (m_bHorizontal) {
  108. if (event.ptMouse.x >= m_rcItem.right - m_szThumb.cx / 2) nValue = m_nMax;
  109. else if (event.ptMouse.x <= m_rcItem.left + m_szThumb.cx / 2) nValue = m_nMin;
  110. else nValue = m_nMin + (m_nMax - m_nMin) * (event.ptMouse.x - m_rcItem.left - m_szThumb.cx / 2) / (m_rcItem.right - m_rcItem.left - m_szThumb.cx);
  111. }
  112. else {
  113. if (event.ptMouse.y >= m_rcItem.bottom - m_szThumb.cy / 2) nValue = m_nMin;
  114. else if (event.ptMouse.y <= m_rcItem.top + m_szThumb.cy / 2) nValue = m_nMax;
  115. else nValue = m_nMin + (m_nMax - m_nMin) * (m_rcItem.bottom - event.ptMouse.y - m_szThumb.cy / 2) / (m_rcItem.bottom - m_rcItem.top - m_szThumb.cy);
  116. }
  117. if (m_nValue != nValue && nValue >= m_nMin && nValue <= m_nMax)
  118. {
  119. m_nValue = nValue;
  120. Invalidate();
  121. }
  122. }
  123. return;
  124. }
  125. if( event.Type == UIEVENT_BUTTONUP )
  126. {
  127. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  128. if( m_bHorizontal ) {
  129. if( event.ptMouse.x >= m_rcItem.right - m_szThumb.cx / 2 ) m_nValue = m_nMax;
  130. else if( event.ptMouse.x <= m_rcItem.left + m_szThumb.cx / 2 ) m_nValue = m_nMin;
  131. else m_nValue = m_nMin + (m_nMax - m_nMin) * (event.ptMouse.x - m_rcItem.left - m_szThumb.cx / 2 ) / (m_rcItem.right - m_rcItem.left - m_szThumb.cx);
  132. }
  133. else {
  134. if( event.ptMouse.y >= m_rcItem.bottom - m_szThumb.cy / 2 ) m_nValue = m_nMin;
  135. else if( event.ptMouse.y <= m_rcItem.top + m_szThumb.cy / 2 ) m_nValue = m_nMax;
  136. else m_nValue = m_nMin + (m_nMax - m_nMin) * (m_rcItem.bottom - event.ptMouse.y - m_szThumb.cy / 2 ) / (m_rcItem.bottom - m_rcItem.top - m_szThumb.cy);
  137. }
  138. m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
  139. m_uButtonState &= ~UISTATE_CAPTURED;
  140. Invalidate();
  141. }
  142. return;
  143. }
  144. if( event.Type == UIEVENT_CONTEXTMENU )
  145. {
  146. return;
  147. }
  148. if( event.Type == UIEVENT_SCROLLWHEEL )
  149. {
  150. switch( LOWORD(event.wParam) ) {
  151. case SB_LINEUP:
  152. SetValue(GetValue() + GetChangeStep());
  153. m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
  154. return;
  155. case SB_LINEDOWN:
  156. SetValue(GetValue() - GetChangeStep());
  157. m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
  158. return;
  159. }
  160. }
  161. if( event.Type == UIEVENT_MOUSEMOVE )
  162. {
  163. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  164. if( m_bHorizontal ) {
  165. if( event.ptMouse.x >= m_rcItem.right - m_szThumb.cx / 2 ) m_nValue = m_nMax;
  166. else if( event.ptMouse.x <= m_rcItem.left + m_szThumb.cx / 2 ) m_nValue = m_nMin;
  167. else m_nValue = m_nMin + (m_nMax - m_nMin) * (event.ptMouse.x - m_rcItem.left - m_szThumb.cx / 2 ) / (m_rcItem.right - m_rcItem.left - m_szThumb.cx);
  168. }
  169. else {
  170. if( event.ptMouse.y >= m_rcItem.bottom - m_szThumb.cy / 2 ) m_nValue = m_nMin;
  171. else if( event.ptMouse.y <= m_rcItem.top + m_szThumb.cy / 2 ) m_nValue = m_nMax;
  172. else m_nValue = m_nMin + (m_nMax - m_nMin) * (m_rcItem.bottom - event.ptMouse.y - m_szThumb.cy / 2 ) / (m_rcItem.bottom - m_rcItem.top - m_szThumb.cy);
  173. }
  174. Invalidate();
  175. }
  176. return;
  177. }
  178. if( event.Type == UIEVENT_SETCURSOR )
  179. {
  180. RECT rcThumb = GetThumbRect();
  181. if( IsEnabled() && ::PtInRect(&rcThumb, event.ptMouse) ) {
  182. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
  183. return;
  184. }
  185. }
  186. if( event.Type == UIEVENT_MOUSEENTER )
  187. {
  188. if( IsEnabled() ) {
  189. m_uButtonState |= UISTATE_HOT;
  190. Invalidate();
  191. }
  192. return;
  193. }
  194. if( event.Type == UIEVENT_MOUSELEAVE )
  195. {
  196. if( IsEnabled() ) {
  197. m_uButtonState &= ~UISTATE_HOT;
  198. Invalidate();
  199. }
  200. return;
  201. }
  202. CControlUI::DoEvent(event);
  203. }
  204. void CSliderUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  205. {
  206. if( _tcscmp(pstrName, _T("thumbimage")) == 0 ) SetThumbImage(pstrValue);
  207. else if( _tcscmp(pstrName, _T("thumbhotimage")) == 0 ) SetThumbHotImage(pstrValue);
  208. else if( _tcscmp(pstrName, _T("thumbpushedimage")) == 0 ) SetThumbPushedImage(pstrValue);
  209. else if( _tcscmp(pstrName, _T("thumbsize")) == 0 ) {
  210. SIZE szXY = {0};
  211. LPTSTR pstr = NULL;
  212. szXY.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  213. szXY.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  214. SetThumbSize(szXY);
  215. }
  216. else if( _tcscmp(pstrName, _T("step")) == 0 ) {
  217. SetChangeStep(_ttoi(pstrValue));
  218. }
  219. else CProgressUI::SetAttribute(pstrName, pstrValue);
  220. }
  221. void CSliderUI::PaintStatusImage(HDC hDC)
  222. {
  223. CProgressUI::PaintStatusImage(hDC);
  224. RECT rcThumb = GetThumbRect();
  225. rcThumb.left -= m_rcItem.left;
  226. rcThumb.top -= m_rcItem.top;
  227. rcThumb.right -= m_rcItem.left;
  228. rcThumb.bottom -= m_rcItem.top;
  229. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  230. m_diThumbPushed.rcDestOffset = rcThumb;
  231. if( DrawImage(hDC, m_diThumbPushed) ) return;
  232. }
  233. else if( (m_uButtonState & UISTATE_HOT) != 0 ) {
  234. m_diThumbHot.rcDestOffset = rcThumb;
  235. if( DrawImage(hDC, m_diThumbHot) ) return;
  236. }
  237. m_diThumb.rcDestOffset = rcThumb;
  238. if( DrawImage(hDC, m_diThumb) ) return;
  239. }
  240. }