123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- #include "StdAfx.h"
- #include "UISlider.h"
- namespace DuiLib
- {
- IMPLEMENT_DUICONTROL(CSliderUI)
- CSliderUI::CSliderUI() : m_uButtonState(0), m_nStep(1),m_bSendMove(false)
- {
- m_uTextStyle = DT_SINGLELINE | DT_CENTER;
- m_szThumb.cx = m_szThumb.cy = 10;
- }
- LPCTSTR CSliderUI::GetClass() const
- {
- return _T("SliderUI");
- }
- UINT CSliderUI::GetControlFlags() const
- {
- if( IsEnabled() ) return UIFLAG_SETCURSOR;
- else return 0;
- }
- LPVOID CSliderUI::GetInterface(LPCTSTR pstrName)
- {
- if( _tcsicmp(pstrName, DUI_CTR_SLIDER) == 0 ) return static_cast<CSliderUI*>(this);
- return CProgressUI::GetInterface(pstrName);
- }
- void CSliderUI::SetEnabled(bool bEnable)
- {
- CControlUI::SetEnabled(bEnable);
- if( !IsEnabled() ) {
- m_uButtonState = 0;
- }
- }
- int CSliderUI::GetChangeStep()
- {
- return m_nStep;
- }
- void CSliderUI::SetChangeStep(int step)
- {
- m_nStep = step;
- }
- void CSliderUI::SetThumbSize(SIZE szXY)
- {
- m_szThumb = szXY;
- }
- RECT CSliderUI::GetThumbRect() const
- {
- RECT rcThumb = {0};
- SIZE m_szThumb = CSliderUI::m_szThumb;
- if (GetManager() != NULL) {
- GetManager()->GetDPIObj()->Scale(&m_szThumb);
- }
- if( m_bHorizontal ) {
- int left = m_rcItem.left + (m_rcItem.right - m_rcItem.left - m_szThumb.cx) * (m_nValue - m_nMin) / (m_nMax - m_nMin);
- int top = (m_rcItem.bottom + m_rcItem.top - m_szThumb.cy) / 2;
- rcThumb = CDuiRect(left, top, left + m_szThumb.cx, top + m_szThumb.cy);
- }
- else {
- int left = (m_rcItem.right + m_rcItem.left - m_szThumb.cx) / 2;
- 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);
- rcThumb = CDuiRect(left, top, left + m_szThumb.cx, top + m_szThumb.cy);
- }
- if(m_pManager != NULL) {
- //m_pManager->GetDPIObj()->Scale(&rcThumb);
- }
- return rcThumb;
- }
- LPCTSTR CSliderUI::GetThumbImage() const
- {
- return m_sThumbImage;
- }
- void CSliderUI::SetThumbImage(LPCTSTR pStrImage)
- {
- m_sThumbImage = pStrImage;
- Invalidate();
- }
- LPCTSTR CSliderUI::GetThumbHotImage() const
- {
- return m_sThumbHotImage;
- }
- void CSliderUI::SetThumbHotImage(LPCTSTR pStrImage)
- {
- m_sThumbHotImage = pStrImage;
- Invalidate();
- }
- LPCTSTR CSliderUI::GetThumbPushedImage() const
- {
- return m_sThumbPushedImage;
- }
- void CSliderUI::SetThumbPushedImage(LPCTSTR pStrImage)
- {
- m_sThumbPushedImage = pStrImage;
- Invalidate();
- }
- void CSliderUI::SetValue(int nValue)
- {
- if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) return;
- CProgressUI::SetValue(nValue);
- }
- void CSliderUI::DoEvent(TEventUI& event)
- {
- if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
- if( m_pParent != NULL ) m_pParent->DoEvent(event);
- else CProgressUI::DoEvent(event);
- return;
- }
- if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK ) {
- if( IsEnabled() ) {
- m_uButtonState |= UISTATE_CAPTURED;
- int nValue;
- if( m_bHorizontal ) {
- if( event.ptMouse.x >= m_rcItem.right - m_szThumb.cx / 2 ) nValue = m_nMax;
- else if( event.ptMouse.x <= m_rcItem.left + m_szThumb.cx / 2 ) nValue = m_nMin;
- 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);
- }
- else {
- if( event.ptMouse.y >= m_rcItem.bottom - m_szThumb.cy / 2 ) nValue = m_nMin;
- else if( event.ptMouse.y <= m_rcItem.top + m_szThumb.cy / 2 ) nValue = m_nMax;
- 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);
- }
- if(m_nValue != nValue && nValue >= m_nMin && nValue <= m_nMax) {
- m_nValue = nValue;
- Invalidate();
- }
- UpdateText();
- }
- return;
- }
- if( event.Type == UIEVENT_BUTTONUP || event.Type == UIEVENT_RBUTTONUP) {
- if( IsEnabled() ) {
- int nValue = 0;
- if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
- m_uButtonState &= ~UISTATE_CAPTURED;
- }
- if( m_bHorizontal ) {
- if( event.ptMouse.x >= m_rcItem.right - m_szThumb.cx / 2 ) nValue = m_nMax;
- else if( event.ptMouse.x <= m_rcItem.left + m_szThumb.cx / 2 ) nValue = m_nMin;
- 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);
- }
- else {
- if( event.ptMouse.y >= m_rcItem.bottom - m_szThumb.cy / 2 ) nValue = m_nMin;
- else if( event.ptMouse.y <= m_rcItem.top + m_szThumb.cy / 2 ) nValue = m_nMax;
- 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);
- }
- if(nValue >= m_nMin && nValue <= m_nMax) {
- m_nValue =nValue;
- m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
- Invalidate();
- }
- UpdateText();
- return;
- }
- }
- if( event.Type == UIEVENT_CONTEXTMENU )
- {
- return;
- }
- if( event.Type == UIEVENT_SCROLLWHEEL )
- {
- if( IsEnabled() ) {
- switch( LOWORD(event.wParam) ) {
- case SB_LINEUP:
- SetValue(GetValue() + GetChangeStep());
- m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
- return;
- case SB_LINEDOWN:
- SetValue(GetValue() - GetChangeStep());
- m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED);
- return;
- }
- }
- }
- if( event.Type == UIEVENT_MOUSEMOVE ) {
- if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
- if( m_bHorizontal ) {
- if( event.ptMouse.x >= m_rcItem.right - m_szThumb.cx / 2 ) m_nValue = m_nMax;
- else if( event.ptMouse.x <= m_rcItem.left + m_szThumb.cx / 2 ) m_nValue = m_nMin;
- 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);
- }
- else {
- if( event.ptMouse.y >= m_rcItem.bottom - m_szThumb.cy / 2 ) m_nValue = m_nMin;
- else if( event.ptMouse.y <= m_rcItem.top + m_szThumb.cy / 2 ) m_nValue = m_nMax;
- 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);
- }
- if (m_bSendMove) {
- UpdateText();
- m_pManager->SendNotify(this, DUI_MSGTYPE_VALUECHANGED_MOVE);
- }
- Invalidate();
- }
- POINT pt = event.ptMouse;
- RECT rcThumb = GetThumbRect();
- if( IsEnabled() && ::PtInRect(&rcThumb, event.ptMouse) ) {
- m_uButtonState |= UISTATE_HOT;
- Invalidate();
- }
- else {
- m_uButtonState &= ~UISTATE_HOT;
- Invalidate();
- }
- return;
- }
- if( event.Type == UIEVENT_SETCURSOR )
- {
- RECT rcThumb = GetThumbRect();
- if( IsEnabled()) {
- ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
- return;
- }
- }
- if( event.Type == UIEVENT_MOUSELEAVE )
- {
- if( IsEnabled() ) {
- m_uButtonState &= ~UISTATE_HOT;
- Invalidate();
- }
- return;
- }
- CControlUI::DoEvent(event);
- }
- void CSliderUI::SetCanSendMove(bool bCanSend)
- {
- m_bSendMove = bCanSend;
- }
- bool CSliderUI::GetCanSendMove() const
- {
- return m_bSendMove;
- }
- void CSliderUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
- {
- if( _tcsicmp(pstrName, _T("thumbimage")) == 0 ) SetThumbImage(pstrValue);
- else if( _tcsicmp(pstrName, _T("thumbhotimage")) == 0 ) SetThumbHotImage(pstrValue);
- else if( _tcsicmp(pstrName, _T("thumbpushedimage")) == 0 ) SetThumbPushedImage(pstrValue);
- else if( _tcsicmp(pstrName, _T("thumbsize")) == 0 ) {
- SIZE szXY = {0};
- LPTSTR pstr = NULL;
- szXY.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
- szXY.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- SetThumbSize(szXY);
- }
- else if( _tcsicmp(pstrName, _T("step")) == 0 ) {
- SetChangeStep(_ttoi(pstrValue));
- }
- else if( _tcsicmp(pstrName, _T("sendmove")) == 0 ) {
- SetCanSendMove(_tcsicmp(pstrValue, _T("true")) == 0);
- }
- else CProgressUI::SetAttribute(pstrName, pstrValue);
- }
- void CSliderUI::PaintForeImage(HDC hDC)
- {
- CProgressUI::PaintForeImage(hDC);
- RECT rcThumb = GetThumbRect();
- rcThumb.left -= m_rcItem.left;
- rcThumb.top -= m_rcItem.top;
- rcThumb.right -= m_rcItem.left;
- rcThumb.bottom -= m_rcItem.top;
- GetManager()->GetDPIObj()->ScaleBack(&rcThumb);
- if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
- if( !m_sThumbPushedImage.IsEmpty() ) {
- m_sImageModify.Empty();
- m_sImageModify.SmallFormat(_T("dest='%d,%d,%d,%d'"), rcThumb.left, rcThumb.top, rcThumb.right, rcThumb.bottom);
- if( !DrawImage(hDC, (LPCTSTR)m_sThumbPushedImage, (LPCTSTR)m_sImageModify) ) {}
- else return;
- }
- }
- else if( (m_uButtonState & UISTATE_HOT) != 0 ) {
- if( !m_sThumbHotImage.IsEmpty() ) {
- m_sImageModify.Empty();
- m_sImageModify.SmallFormat(_T("dest='%d,%d,%d,%d'"), rcThumb.left, rcThumb.top, rcThumb.right, rcThumb.bottom);
- if( !DrawImage(hDC, (LPCTSTR)m_sThumbHotImage, (LPCTSTR)m_sImageModify) ) {}
- else return;
- }
- }
- if( !m_sThumbImage.IsEmpty() ) {
- m_sImageModify.Empty();
- m_sImageModify.SmallFormat(_T("dest='%d,%d,%d,%d'"), rcThumb.left, rcThumb.top, rcThumb.right, rcThumb.bottom);
- if( !DrawImage(hDC, (LPCTSTR)m_sThumbImage, (LPCTSTR)m_sImageModify) ) {}
- else return;
- }
- }
- }
|