]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: msw/scrolbar.cpp | |
3 | // Purpose: wxScrollBar | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "scrolbar.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #if wxUSE_SCROLLBAR | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/utils.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/scrolbar.h" | |
30 | #include "wx/msw/private.h" | |
31 | ||
32 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl) | |
33 | ||
34 | BEGIN_EVENT_TABLE(wxScrollBar, wxControl) | |
35 | #if WXWIN_COMPATIBILITY | |
36 | EVT_SCROLL(wxScrollBar::OnScroll) | |
37 | #endif | |
38 | END_EVENT_TABLE() | |
39 | ||
40 | ||
41 | // Scrollbar | |
42 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, | |
43 | const wxPoint& pos, | |
44 | const wxSize& size, long style, | |
45 | const wxValidator& validator, | |
46 | const wxString& name) | |
47 | { | |
48 | if (!parent) | |
49 | return FALSE; | |
50 | parent->AddChild(this); | |
51 | SetName(name); | |
52 | #if wxUSE_VALIDATORS | |
53 | SetValidator(validator); | |
54 | #endif // wxUSE_VALIDATORS | |
55 | ||
56 | SetBackgroundColour(parent->GetBackgroundColour()) ; | |
57 | SetForegroundColour(parent->GetForegroundColour()) ; | |
58 | m_windowStyle = style; | |
59 | ||
60 | if ( id == -1 ) | |
61 | m_windowId = (int)NewControlId(); | |
62 | else | |
63 | m_windowId = id; | |
64 | ||
65 | int x = pos.x; | |
66 | int y = pos.y; | |
67 | int width = size.x; | |
68 | int height = size.y; | |
69 | ||
70 | if (width == -1) | |
71 | { | |
72 | if (style & wxHORIZONTAL) | |
73 | width = 140; | |
74 | else | |
75 | width = 14; | |
76 | } | |
77 | if (height == -1) | |
78 | { | |
79 | if (style & wxVERTICAL) | |
80 | height = 140; | |
81 | else | |
82 | height = 14; | |
83 | } | |
84 | ||
85 | DWORD wstyle = WS_VISIBLE | WS_CHILD; | |
86 | ||
87 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
88 | wstyle |= WS_CLIPSIBLINGS; | |
89 | ||
90 | // Now create scrollbar | |
91 | DWORD _direction = (style & wxHORIZONTAL) ? | |
92 | SBS_HORZ: SBS_VERT; | |
93 | HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(style), wxT("SCROLLBAR"), wxT("scrollbar"), | |
94 | _direction | wstyle, | |
95 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
96 | wxGetInstance(), NULL); | |
97 | ||
98 | m_pageSize = 1; | |
99 | m_viewSize = 1; | |
100 | m_objectSize = 1; | |
101 | ||
102 | ::SetScrollRange(scroll_bar, SB_CTL, 0, 1, FALSE); | |
103 | ::SetScrollPos(scroll_bar, SB_CTL, 0, FALSE); | |
104 | ShowWindow(scroll_bar, SW_SHOW); | |
105 | ||
106 | SetFont(parent->GetFont()); | |
107 | ||
108 | m_hWnd = (WXHWND)scroll_bar; | |
109 | ||
110 | // Subclass again for purposes of dialog editing mode | |
111 | SubclassWin((WXHWND) scroll_bar); | |
112 | ||
113 | SetSize(x, y, width, height); | |
114 | ||
115 | return TRUE; | |
116 | } | |
117 | ||
118 | wxScrollBar::~wxScrollBar(void) | |
119 | { | |
120 | } | |
121 | ||
122 | bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam, | |
123 | WXWORD pos, WXHWND control) | |
124 | { | |
125 | // current and max positions | |
126 | int position, | |
127 | maxPos, trackPos = pos; | |
128 | ||
129 | #ifdef __WIN32__ | |
130 | // when we're dragging the scrollbar we can't use pos parameter because it | |
131 | // is limited to 16 bits | |
132 | if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK ) | |
133 | { | |
134 | SCROLLINFO scrollInfo; | |
135 | wxZeroMemory(scrollInfo); | |
136 | scrollInfo.cbSize = sizeof(SCROLLINFO); | |
137 | ||
138 | // also get the range if we call GetScrollInfo() anyhow -- this is less | |
139 | // expensive than call it once here and then call GetScrollRange() | |
140 | // below | |
141 | scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS; | |
142 | ||
143 | if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) ) | |
144 | { | |
145 | wxLogLastError(_T("GetScrollInfo")); | |
146 | } | |
147 | ||
148 | trackPos = scrollInfo.nTrackPos; | |
149 | position = scrollInfo.nPos; | |
150 | maxPos = scrollInfo.nMax; | |
151 | } | |
152 | else | |
153 | #endif // Win32 | |
154 | { | |
155 | position = ::GetScrollPos((HWND) control, SB_CTL); | |
156 | int minPos; | |
157 | ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos); | |
158 | } | |
159 | ||
160 | #if defined(__WIN95__) | |
161 | // A page size greater than one has the effect of reducing the effective | |
162 | // range, therefore the range has already been boosted artificially - so | |
163 | // reduce it again. | |
164 | if ( m_pageSize > 1 ) | |
165 | maxPos -= (m_pageSize - 1); | |
166 | #endif // __WIN95__ | |
167 | ||
168 | wxEventType scrollEvent = wxEVT_NULL; | |
169 | ||
170 | int nScrollInc; | |
171 | switch ( wParam ) | |
172 | { | |
173 | case SB_BOTTOM: | |
174 | nScrollInc = maxPos - position; | |
175 | scrollEvent = wxEVT_SCROLL_TOP; | |
176 | break; | |
177 | ||
178 | case SB_TOP: | |
179 | nScrollInc = -position; | |
180 | scrollEvent = wxEVT_SCROLL_BOTTOM; | |
181 | break; | |
182 | ||
183 | case SB_LINEUP: | |
184 | nScrollInc = -1; | |
185 | scrollEvent = wxEVT_SCROLL_LINEUP; | |
186 | break; | |
187 | ||
188 | case SB_LINEDOWN: | |
189 | nScrollInc = 1; | |
190 | scrollEvent = wxEVT_SCROLL_LINEDOWN; | |
191 | break; | |
192 | ||
193 | case SB_PAGEUP: | |
194 | nScrollInc = -GetPageSize(); | |
195 | scrollEvent = wxEVT_SCROLL_PAGEUP; | |
196 | break; | |
197 | ||
198 | case SB_PAGEDOWN: | |
199 | nScrollInc = GetPageSize(); | |
200 | scrollEvent = wxEVT_SCROLL_PAGEDOWN; | |
201 | break; | |
202 | ||
203 | case SB_THUMBPOSITION: | |
204 | nScrollInc = trackPos - position; | |
205 | scrollEvent = wxEVT_SCROLL_THUMBRELEASE; | |
206 | break; | |
207 | ||
208 | case SB_THUMBTRACK: | |
209 | nScrollInc = trackPos - position; | |
210 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; | |
211 | break; | |
212 | ||
213 | case SB_ENDSCROLL: | |
214 | nScrollInc = 0; | |
215 | scrollEvent = wxEVT_SCROLL_ENDSCROLL; | |
216 | break; | |
217 | ||
218 | default: | |
219 | nScrollInc = 0; | |
220 | } | |
221 | ||
222 | if ( nScrollInc ) | |
223 | { | |
224 | position += nScrollInc; | |
225 | ||
226 | if ( position < 0 ) | |
227 | position = 0; | |
228 | if ( position > maxPos ) | |
229 | position = maxPos; | |
230 | ||
231 | SetThumbPosition(position); | |
232 | } | |
233 | else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE && | |
234 | scrollEvent != wxEVT_SCROLL_ENDSCROLL ) | |
235 | { | |
236 | // don't process the event if there is no displacement, | |
237 | // unless this is a thumb release or end scroll event. | |
238 | return FALSE; | |
239 | } | |
240 | ||
241 | wxScrollEvent event(scrollEvent, m_windowId); | |
242 | event.SetPosition(position); | |
243 | event.SetEventObject( this ); | |
244 | ||
245 | return GetEventHandler()->ProcessEvent(event); | |
246 | } | |
247 | ||
248 | void wxScrollBar::SetThumbPosition(int viewStart) | |
249 | { | |
250 | #if defined(__WIN95__) | |
251 | SCROLLINFO info; | |
252 | info.cbSize = sizeof(SCROLLINFO); | |
253 | info.nPage = 0; | |
254 | info.nMin = 0; | |
255 | info.nPos = viewStart; | |
256 | info.fMask = SIF_POS ; | |
257 | ||
258 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE); | |
259 | #else | |
260 | ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE); | |
261 | #endif | |
262 | } | |
263 | ||
264 | int wxScrollBar::GetThumbPosition(void) const | |
265 | { | |
266 | return ::GetScrollPos((HWND)m_hWnd, SB_CTL); | |
267 | } | |
268 | ||
269 | void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, | |
270 | bool refresh) | |
271 | { | |
272 | m_viewSize = pageSize; | |
273 | m_pageSize = thumbSize; | |
274 | m_objectSize = range; | |
275 | ||
276 | // The range (number of scroll steps) is the | |
277 | // object length minus the page size. | |
278 | int range1 = wxMax((m_objectSize - m_pageSize), 0) ; | |
279 | ||
280 | #if defined(__WIN95__) | |
281 | // Try to adjust the range to cope with page size > 1 | |
282 | // (see comment for SetPageLength) | |
283 | if ( m_pageSize > 1 ) | |
284 | { | |
285 | range1 += (m_pageSize - 1); | |
286 | } | |
287 | ||
288 | SCROLLINFO info; | |
289 | info.cbSize = sizeof(SCROLLINFO); | |
290 | info.nPage = m_pageSize; | |
291 | info.nMin = 0; | |
292 | info.nMax = range1; | |
293 | info.nPos = position; | |
294 | ||
295 | info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS; | |
296 | ||
297 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh); | |
298 | #else | |
299 | ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE); | |
300 | ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE); | |
301 | #endif | |
302 | } | |
303 | ||
304 | ||
305 | /* From the WIN32 documentation: | |
306 | In version 4.0 or later, the maximum value that a scroll bar can report | |
307 | (that is, the maximum scrolling position) depends on the page size. | |
308 | If the scroll bar has a page size greater than one, the maximum scrolling position | |
309 | is less than the maximum range value. You can use the following formula to calculate | |
310 | the maximum scrolling position: | |
311 | ||
312 | MaxScrollPos = MaxRangeValue - (PageSize - 1) | |
313 | */ | |
314 | ||
315 | #if WXWIN_COMPATIBILITY | |
316 | void wxScrollBar::SetPageSize(int pageLength) | |
317 | { | |
318 | m_pageSize = pageLength; | |
319 | ||
320 | #if defined(__WIN95__) | |
321 | SCROLLINFO info; | |
322 | info.cbSize = sizeof(SCROLLINFO); | |
323 | info.nPage = pageLength; | |
324 | info.fMask = SIF_PAGE ; | |
325 | ||
326 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE); | |
327 | #endif | |
328 | } | |
329 | ||
330 | void wxScrollBar::SetObjectLength(int objectLength) | |
331 | { | |
332 | m_objectSize = objectLength; | |
333 | ||
334 | // The range (number of scroll steps) is the | |
335 | // object length minus the view size. | |
336 | int range = wxMax((objectLength - m_viewSize), 0) ; | |
337 | ||
338 | #if defined(__WIN95__) | |
339 | // Try to adjust the range to cope with page size > 1 | |
340 | // (see comment for SetPageLength) | |
341 | if ( m_pageSize > 1 ) | |
342 | { | |
343 | range += (m_pageSize - 1); | |
344 | } | |
345 | ||
346 | SCROLLINFO info; | |
347 | info.cbSize = sizeof(SCROLLINFO); | |
348 | info.nPage = 0; | |
349 | info.nMin = 0; | |
350 | info.nMax = range; | |
351 | info.nPos = 0; | |
352 | info.fMask = SIF_RANGE ; | |
353 | ||
354 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE); | |
355 | #else | |
356 | ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range, TRUE); | |
357 | #endif | |
358 | } | |
359 | ||
360 | void wxScrollBar::SetViewLength(int viewLength) | |
361 | { | |
362 | m_viewSize = viewLength; | |
363 | } | |
364 | ||
365 | void wxScrollBar::GetValues(int *viewStart, int *viewLength, int *objectLength, | |
366 | int *pageLength) const | |
367 | { | |
368 | *viewStart = ::GetScrollPos((HWND)m_hWnd, SB_CTL); | |
369 | *viewLength = m_viewSize; | |
370 | *objectLength = m_objectSize; | |
371 | *pageLength = m_pageSize; | |
372 | } | |
373 | #endif | |
374 | ||
375 | WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), | |
376 | WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam)) | |
377 | { | |
378 | return 0; | |
379 | } | |
380 | ||
381 | void wxScrollBar::Command(wxCommandEvent& event) | |
382 | { | |
383 | SetThumbPosition(event.m_commandInt); | |
384 | ProcessCommand(event); | |
385 | } | |
386 | ||
387 | #if WXWIN_COMPATIBILITY | |
388 | // Backward compatibility | |
389 | void wxScrollBar::OnScroll(wxScrollEvent& event) | |
390 | { | |
391 | wxEventType oldEvent = event.GetEventType(); | |
392 | event.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED ); | |
393 | if ( !GetEventHandler()->ProcessEvent(event) ) | |
394 | { | |
395 | event.SetEventType( oldEvent ); | |
396 | if (!GetParent()->GetEventHandler()->ProcessEvent(event)) | |
397 | event.Skip(); | |
398 | } | |
399 | } | |
400 | #endif | |
401 | ||
402 | #endif // wxUSE_SCROLLBAR |