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