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