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