#include "T2DlgItemScr.h" T2DlgItemScr::T2DlgItemScr(T2TowerDoc* towerDoc, T2ImageObj* imageObj, CPalette* palette) : T2DlgItem(towerDoc, imageObj, palette) { mUpImage = 200; mDownImage = 200; mValue = 0; mRange = 0; mPage = 0; mMouseDownPoint = CPoint(0, 0); mClickedArea = 0; } /*virtual*/ void T2DlgItemScr::SetPage(int page) { mPage = page; if (!IsScrollable() && mValue != 0) { mValue = 0; Notify(GetDlgCtrlID(), 0, &mValue); } Invalidate(); } /*virtual*/ void T2DlgItemScr::SetRange(int range) { mRange = range; if (!IsScrollable() && mValue != 0) { mValue = 0; Notify(GetDlgCtrlID(), 0, &mValue); } Invalidate(); } /*virtual*/ void T2DlgItemScr::SetValue(int value) { if (!IsScrollable()) value = 0; if (mValue != value) { mValue = value; Notify(GetDlgCtrlID(), 0, &mValue); Invalidate(); } } /*virtual*/ void T2DlgItemScr::OnT2DlgItemLButtonDown(UINT nFlags, CPoint pt) { SetCapture(); m74 = true; mTimerID = 0; mClickedArea = ScrollBarHittest(pt); if (mClickedArea != 0) { if (mClickedArea == Area_Thumb) { mMouseDownPoint = pt; mMouseDownValue = mValue; } else { if (mClickedArea == Area_Up) mUpImage = 100; else if (mClickedArea == Area_Down) mDownImage = 100; ScrollBarAction(mClickedArea, 0); if (mClickedArea == Area_Up || mClickedArea == Area_Down) mTimerID = SetTimer(999, 100, NULL); } } } /*virtual*/ void T2DlgItemScr::OnT2DlgItemLButtonUp(UINT nFlags, CPoint pt) { if (mClickedArea && mTimerID) KillTimer(mTimerID); ReleaseCapture(); m74 = false; mUpImage = 0; mDownImage = 0; mClickedArea = 0; Invalidate(); } /*virtual*/ void T2DlgItemScr::OnT2DlgItemMouseMove(UINT nFlags, CPoint pt) { if (mClickedArea) { CRect clientRect; GetClientRect(clientRect); switch (mClickedArea) { case Area_Thumb: { int thumbPosition, thumbSize; clientRect.DeflateRect(0, 16); CalcScrollBarThumb(&thumbPosition, &thumbSize); int val = PositionToValue(clientRect) - thumbSize; if (val == 0) val = 1; ScrollBarAction( Area_Thumb, mMouseDownValue + ((PositionToValue(pt) - PositionToValue(mMouseDownPoint)) * (mRange - mPage)) / val ); break; } case Area_Up: { CRect upButtonRect; GetUpButtonRect(clientRect, upButtonRect); BOOL inUpButton = upButtonRect.PtInRect(pt); if (inUpButton != m74) { m74 = inUpButton; mUpImage = m74 ? 100 : 0; InvalidateRect(upButtonRect); } break; } case Area_Down: { CRect downButtonRect; GetDnButtonRect(clientRect, downButtonRect); BOOL inDownButton = downButtonRect.PtInRect(pt); if (inDownButton != m74) { m74 = inDownButton; mDownImage = m74 ? 100 : 0; InvalidateRect(downButtonRect); } break; } } } } void T2DlgItemScr::CalcScrollBarThumb(int* thumbPosition, int* thumbSize) const { if (!IsScrollable()) { *thumbPosition = 16; *thumbSize = 0; } else { CRect clientRect; GetClientRect(clientRect); int value = PositionToValue(clientRect); value -= 32; *thumbSize = (value * mPage) / mRange; if (*thumbSize < 10) *thumbSize = 10; *thumbPosition = (((value - *thumbSize) * mValue) / (mRange - mPage)) + 16; } } void T2DlgItemScr::ScrollBarAction(int area, int arg) { if (!IsScrollable()) { mValue = 0; } else { switch (area) { case Area_Up: mValue--; break; case Area_PageUp: mValue -= mPage / 2; break; case Area_Thumb: mValue = arg; break; case Area_PageDown: mValue += mPage / 2; break; case Area_Down: mValue++; break; } if (mValue < 0) mValue = 0; if (mValue > (mRange - mPage)) mValue = mRange - mPage; } Notify(GetDlgCtrlID(), 0, &mValue); Invalidate(); } BOOL T2DlgItemScr::IsScrollable() const { return (mRange > 0) && (mPage < mRange); } /*virtual*/ void T2DlgItemScr::OnT2Timer(UINT id) { if (id == mTimerID && m74 && (mClickedArea == Area_Up || mClickedArea == Area_Down)) ScrollBarAction(mClickedArea, 0); }