UISlider.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "StdAfx.h"
  2. #include "UISlider.h"
  3. namespace DuiLib
  4. {
  5. CSliderUI::CSliderUI() : m_uButtonState(0), m_nStep(1), m_bImmMode(false)
  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 DUI_CTR_SLIDER;
  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. bool CSliderUI::IsImmMode() const
  57. {
  58. return m_bImmMode;
  59. }
  60. void CSliderUI::SetImmMode(bool bImmMode)
  61. {
  62. m_bImmMode = bImmMode;
  63. }
  64. LPCTSTR CSliderUI::GetThumbImage() const
  65. {
  66. return m_diThumb.sDrawString;
  67. }
  68. void CSliderUI::SetThumbImage(LPCTSTR pStrImage)
  69. {
  70. if( m_diThumb.sDrawString == pStrImage && m_diThumb.pImageInfo != NULL ) return;
  71. m_diThumb.Clear();
  72. m_diThumb.sDrawString = pStrImage;
  73. Invalidate();
  74. }
  75. LPCTSTR CSliderUI::GetThumbHotImage() const
  76. {
  77. return m_diThumbHot.sDrawString;
  78. }
  79. void CSliderUI::SetThumbHotImage(LPCTSTR pStrImage)
  80. {
  81. if( m_diThumbHot.sDrawString == pStrImage && m_diThumbHot.pImageInfo != NULL ) return;
  82. m_diThumbHot.Clear();
  83. m_diThumbHot.sDrawString = pStrImage;
  84. Invalidate();
  85. }
  86. LPCTSTR CSliderUI::GetThumbPushedImage() const
  87. {
  88. return m_diThumbPushed.sDrawString;
  89. }
  90. void CSliderUI::SetThumbPushedImage(LPCTSTR pStrImage)
  91. {
  92. if( m_diThumbPushed.sDrawString == pStrImage && m_diThumbPushed.pImageInfo != NULL ) return;
  93. m_diThumbPushed.Clear();
  94. m_diThumbPushed.sDrawString = pStrImage;
  95. Invalidate();
  96. }
  97. void CSliderUI::DoEvent(TEventUI& event)
  98. {
  99. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  100. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  101. else CProgressUI::DoEvent(event);
  102. return;
  103. }
  104. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )
  105. {
  106. if( IsEnabled() ) {
  107. /*RECT rcThumb = GetThumbRect();
  108. if( ::PtInRect(&rcThumb, event.ptMouse) ) {
  109. m_uButtonState |= UISTATE_CAPTURED;
  110. }
  111. }
  112. return;*/
  113. m_uButtonState |= UISTATE_CAPTURED;
  114. int nValue;
  115. if (m_bHorizontal) {
  116. if (event.ptMouse.x >= m_rcItem.right - m_szThumb.cx / 2) nValue = m_nMax;
  117. else if (event.ptMouse.x <= m_rcItem.left + m_szThumb.cx / 2) nValue = m_nMin;
  118. 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);
  119. }
  120. else {
  121. if (event.ptMouse.y >= m_rcItem.bottom - m_szThumb.cy / 2) nValue = m_nMin;
  122. else if (event.ptMouse.y <= m_rcItem.top + m_szThumb.cy / 2) nValue = m_nMax;
  123. 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);
  124. }
  125. if (m_nValue != nValue && nValue >= m_nMin && nValue <= m_nMax)
  126. {
  127. m_nValue = nValue;
  128. Invalidate();
  129. }
  130. }
  131. return;
  132. }
  133. if( event.Type == UIEVENT_BUTTONUP )
  134. {
  135. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  136. if( m_bHorizontal ) {
  137. if( event.ptMouse.x >= m_rcItem.right - m_szThumb.cx / 2 ) m_nValue = m_nMax;
  138. else if( event.ptMouse.x <= m_rcItem.left + m_szThumb.cx / 2 ) m_nValue = m_nMin;
  139. 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);
  140. }
  141. else {
  142. if( event.ptMouse.y >= m_rcItem.bottom - m_szThumb.cy / 2 ) m_nValue = m_nMin;
  143. else if( event.ptMouse.y <= m_rcItem.top + m_szThumb.cy / 2 ) m_nValue = m_nMax;
  144. 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);
  145. }
  146. m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
  147. m_uButtonState &= ~UISTATE_CAPTURED;
  148. Invalidate();
  149. }
  150. return;
  151. }
  152. if( event.Type == UIEVENT_CONTEXTMENU )
  153. {
  154. return;
  155. }
  156. if( event.Type == UIEVENT_SCROLLWHEEL )
  157. {
  158. switch( LOWORD(event.wParam) ) {
  159. case SB_LINEUP:
  160. SetValue(GetValue() + GetChangeStep());
  161. m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
  162. return;
  163. case SB_LINEDOWN:
  164. SetValue(GetValue() - GetChangeStep());
  165. m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
  166. return;
  167. }
  168. }
  169. if( event.Type == UIEVENT_MOUSEMOVE )
  170. {
  171. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  172. if( m_bHorizontal ) {
  173. if( event.ptMouse.x >= m_rcItem.right - m_szThumb.cx / 2 ) m_nValue = m_nMax;
  174. else if( event.ptMouse.x <= m_rcItem.left + m_szThumb.cx / 2 ) m_nValue = m_nMin;
  175. 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);
  176. }
  177. else {
  178. if( event.ptMouse.y >= m_rcItem.bottom - m_szThumb.cy / 2 ) m_nValue = m_nMin;
  179. else if( event.ptMouse.y <= m_rcItem.top + m_szThumb.cy / 2 ) m_nValue = m_nMax;
  180. 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);
  181. }
  182. if( m_bImmMode ) m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
  183. Invalidate();
  184. }
  185. return;
  186. }
  187. if( event.Type == UIEVENT_SETCURSOR )
  188. {
  189. RECT rcThumb = GetThumbRect();
  190. if( IsEnabled() && ::PtInRect(&rcThumb, event.ptMouse) ) {
  191. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
  192. return;
  193. }
  194. }
  195. if( event.Type == UIEVENT_MOUSEENTER )
  196. {
  197. if( ::PtInRect(&m_rcItem, event.ptMouse ) ) {
  198. if( IsEnabled() ) {
  199. if( (m_uButtonState & UISTATE_HOT) == 0 ) {
  200. m_uButtonState |= UISTATE_HOT;
  201. Invalidate();
  202. }
  203. }
  204. }
  205. }
  206. if( event.Type == UIEVENT_MOUSELEAVE )
  207. {
  208. if( !::PtInRect(&m_rcItem, event.ptMouse ) ) {
  209. if( IsEnabled() ) {
  210. if( (m_uButtonState & UISTATE_HOT) != 0 ) {
  211. m_uButtonState &= ~UISTATE_HOT;
  212. Invalidate();
  213. }
  214. }
  215. if (m_pManager) m_pManager->RemoveMouseLeaveNeeded(this);
  216. }
  217. else {
  218. if (m_pManager) m_pManager->AddMouseLeaveNeeded(this);
  219. return;
  220. }
  221. }
  222. CControlUI::DoEvent(event);
  223. }
  224. void CSliderUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  225. {
  226. if( _tcscmp(pstrName, _T("thumbimage")) == 0 ) SetThumbImage(pstrValue);
  227. else if( _tcscmp(pstrName, _T("thumbhotimage")) == 0 ) SetThumbHotImage(pstrValue);
  228. else if( _tcscmp(pstrName, _T("thumbpushedimage")) == 0 ) SetThumbPushedImage(pstrValue);
  229. else if( _tcscmp(pstrName, _T("thumbsize")) == 0 ) {
  230. SIZE szXY = {0};
  231. LPTSTR pstr = NULL;
  232. szXY.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  233. szXY.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  234. SetThumbSize(szXY);
  235. }
  236. else if( _tcscmp(pstrName, _T("step")) == 0 ) {
  237. SetChangeStep(_ttoi(pstrValue));
  238. }
  239. else if( _tcscmp(pstrName, _T("imm")) == 0 ) SetImmMode(_tcscmp(pstrValue, _T("true")) == 0);
  240. else CProgressUI::SetAttribute(pstrName, pstrValue);
  241. }
  242. void CSliderUI::PaintStatusImage(HDC hDC)
  243. {
  244. CProgressUI::PaintStatusImage(hDC);
  245. RECT rcThumb = GetThumbRect();
  246. rcThumb.left -= m_rcItem.left;
  247. rcThumb.top -= m_rcItem.top;
  248. rcThumb.right -= m_rcItem.left;
  249. rcThumb.bottom -= m_rcItem.top;
  250. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  251. m_diThumbPushed.rcDestOffset = rcThumb;
  252. if( DrawImage(hDC, m_diThumbPushed) ) return;
  253. }
  254. else if( (m_uButtonState & UISTATE_HOT) != 0 ) {
  255. m_diThumbHot.rcDestOffset = rcThumb;
  256. if( DrawImage(hDC, m_diThumbHot) ) return;
  257. }
  258. m_diThumb.rcDestOffset = rcThumb;
  259. if( DrawImage(hDC, m_diThumb) ) return;
  260. }
  261. }