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