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