]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
Acutally use the m_maxChars parameter value instead of ignoring it
[wxWidgets.git] / src / msw / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/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
9 // Licence: wxWindows licence
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 #if wxUSE_SCROLLBAR
24
25 #ifndef WX_PRECOMP
26 #include "wx/utils.h"
27 #endif
28
29 #include "wx/scrolbar.h"
30 #include "wx/msw/private.h"
31
32 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
33
34 BEGIN_EVENT_TABLE(wxScrollBar, wxControl)
35 #if WXWIN_COMPATIBILITY
36 EVT_SCROLL(wxScrollBar::OnScroll)
37 #endif
38 END_EVENT_TABLE()
39
40
41 // Scrollbar
42 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
43 const wxPoint& pos,
44 const wxSize& size, long style,
45 const wxValidator& validator,
46 const wxString& name)
47 {
48 if (!parent)
49 return FALSE;
50 parent->AddChild(this);
51 SetName(name);
52 #if wxUSE_VALIDATORS
53 SetValidator(validator);
54 #endif // wxUSE_VALIDATORS
55
56 SetBackgroundColour(parent->GetBackgroundColour()) ;
57 SetForegroundColour(parent->GetForegroundColour()) ;
58 m_windowStyle = style;
59
60 if ( id == -1 )
61 m_windowId = (int)NewControlId();
62 else
63 m_windowId = id;
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
85 WXDWORD exStyle = 0;
86 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
87
88 // Now create scrollbar
89 DWORD _direction = (style & wxHORIZONTAL) ?
90 SBS_HORZ: SBS_VERT;
91 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
92 _direction | wstyle,
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
104 SetFont(parent->GetFont());
105
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
120 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
121 WXWORD pos, WXHWND control)
122 {
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 }
157
158 #if defined(__WIN95__)
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__
165
166 wxEventType scrollEvent = wxEVT_NULL;
167
168 int nScrollInc;
169 switch ( wParam )
170 {
171 case SB_BOTTOM:
172 nScrollInc = maxPos - position;
173 scrollEvent = wxEVT_SCROLL_TOP;
174 break;
175
176 case SB_TOP:
177 nScrollInc = -position;
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
201 case SB_THUMBPOSITION:
202 nScrollInc = trackPos - position;
203 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
204 break;
205
206 case SB_THUMBTRACK:
207 nScrollInc = trackPos - position;
208 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
209 break;
210
211 case SB_ENDSCROLL:
212 nScrollInc = 0;
213 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
214 break;
215
216 default:
217 nScrollInc = 0;
218 }
219
220 if ( nScrollInc )
221 {
222 position += nScrollInc;
223
224 if ( position < 0 )
225 position = 0;
226 if ( position > maxPos )
227 position = maxPos;
228
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 }
238
239 wxScrollEvent event(scrollEvent, m_windowId);
240 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
241 event.SetPosition(position);
242 event.SetEventObject( this );
243
244 return GetEventHandler()->ProcessEvent(event);
245 }
246
247 void wxScrollBar::SetThumbPosition(int viewStart)
248 {
249 #if defined(__WIN95__)
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);
258 #else
259 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
260 #endif
261 }
262
263 int wxScrollBar::GetThumbPosition(void) const
264 {
265 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
266 }
267
268 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
269 bool refresh)
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 {
284 range1 += (m_pageSize - 1);
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:
310
311 MaxScrollPos = MaxRangeValue - (PageSize - 1)
312 */
313
314 #if WXWIN_COMPATIBILITY
315 void wxScrollBar::SetPageSize(int pageLength)
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
329 void wxScrollBar::SetObjectLength(int objectLength)
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 {
342 range += (m_pageSize - 1);
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
359 void wxScrollBar::SetViewLength(int viewLength)
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
374 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
375 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
376 {
377 return 0;
378 }
379
380 void wxScrollBar::Command(wxCommandEvent& event)
381 {
382 SetThumbPosition(event.m_commandInt);
383 ProcessCommand(event);
384 }
385
386 #if WXWIN_COMPATIBILITY
387 // Backward compatibility
388 void wxScrollBar::OnScroll(wxScrollEvent& event)
389 {
390 wxEventType oldEvent = event.GetEventType();
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
400
401 #endif // wxUSE_SCROLLBAR