UISlider.cpp 9.0 KB

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