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