summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2DlgItemScr.cpp
blob: 46b034f04a9c5cb6535e1bfc80e67cb10246eb0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#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 theThumbPosition, theThumbSize;
				clientRect.DeflateRect(0, 16);
				CalcScrollBarThumb(&theThumbPosition, &theThumbSize);

				int val = PositionToValue(clientRect) - theThumbSize;
				if (val == 0)
					val = 1;

				ScrollBarAction(
                    Area_Thumb,
					mMouseDownValue + ((PositionToValue(pt) - PositionToValue(mMouseDownPoint)) * (mRange - mPage)) / val
				);
				break;
			}
			case Area_Up: {
				CRect buttonArea;
				GetUpButtonRect(clientRect, buttonArea);
				BOOL mouseInRect = buttonArea.PtInRect(pt);
				if (mouseInRect != m74) {
					m74 = mouseInRect;
					mUpImage = m74 ? 100 : 0;
					InvalidateRect(buttonArea);
				}
				break;
			}
			case Area_Down: {
				CRect buttonArea;
				GetDnButtonRect(clientRect, buttonArea);
				BOOL mouseInRect = buttonArea.PtInRect(pt);
				if (mouseInRect != m74) {
					m74 = mouseInRect;
					mDownImage = m74 ? 100 : 0;
					InvalidateRect(buttonArea);
				}
				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 ((mRange - mPage) <= mValue)
			mValue = mRange - mPage;
	}

    Invalidate();
	Notify(GetDlgCtrlID(), 0, &mValue);
}

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);
}