1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/scrolbar.cpp
3 // Purpose: wxScrollBar
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "scrolbar.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/scrolbar.h"
30 #include "wx/msw/private.h"
32 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
34 BEGIN_EVENT_TABLE(wxScrollBar
, wxControl
)
35 #if WXWIN_COMPATIBILITY
36 EVT_SCROLL(wxScrollBar::OnScroll
)
42 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
44 const wxSize
& size
, long style
,
45 const wxValidator
& validator
,
50 parent
->AddChild(this);
53 SetValidator(validator
);
54 #endif // wxUSE_VALIDATORS
56 SetBackgroundColour(parent
->GetBackgroundColour()) ;
57 SetForegroundColour(parent
->GetForegroundColour()) ;
58 m_windowStyle
= style
;
61 m_windowId
= (int)NewControlId();
72 if (style
& wxHORIZONTAL
)
79 if (style
& wxVERTICAL
)
85 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
;
87 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
88 wstyle
|= WS_CLIPSIBLINGS
;
90 // Now create scrollbar
91 DWORD _direction
= (style
& wxHORIZONTAL
) ?
93 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(style
), wxT("SCROLLBAR"), wxT("scrollbar"),
95 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
96 wxGetInstance(), NULL
);
102 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
103 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
104 ShowWindow(scroll_bar
, SW_SHOW
);
106 SetFont(parent
->GetFont());
108 m_hWnd
= (WXHWND
)scroll_bar
;
110 // Subclass again for purposes of dialog editing mode
111 SubclassWin((WXHWND
) scroll_bar
);
113 SetSize(x
, y
, width
, height
);
118 wxScrollBar::~wxScrollBar(void)
122 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
123 WXWORD pos
, WXHWND control
)
125 // current and max positions
127 maxPos
, trackPos
= pos
;
130 // when we're dragging the scrollbar we can't use pos parameter because it
131 // is limited to 16 bits
132 if ( wParam
== SB_THUMBPOSITION
|| wParam
== SB_THUMBTRACK
)
134 SCROLLINFO scrollInfo
;
135 wxZeroMemory(scrollInfo
);
136 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
138 // also get the range if we call GetScrollInfo() anyhow -- this is less
139 // expensive than call it once here and then call GetScrollRange()
141 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
143 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
145 wxLogLastError(_T("GetScrollInfo"));
148 trackPos
= scrollInfo
.nTrackPos
;
149 position
= scrollInfo
.nPos
;
150 maxPos
= scrollInfo
.nMax
;
155 position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
157 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
160 #if defined(__WIN95__)
161 // A page size greater than one has the effect of reducing the effective
162 // range, therefore the range has already been boosted artificially - so
164 if ( m_pageSize
> 1 )
165 maxPos
-= (m_pageSize
- 1);
168 wxEventType scrollEvent
= wxEVT_NULL
;
174 nScrollInc
= maxPos
- position
;
175 scrollEvent
= wxEVT_SCROLL_TOP
;
179 nScrollInc
= -position
;
180 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
185 scrollEvent
= wxEVT_SCROLL_LINEUP
;
190 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
194 nScrollInc
= -GetPageSize();
195 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
199 nScrollInc
= GetPageSize();
200 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
203 case SB_THUMBPOSITION
:
204 nScrollInc
= trackPos
- position
;
205 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
209 nScrollInc
= trackPos
- position
;
210 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
215 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
224 position
+= nScrollInc
;
228 if ( position
> maxPos
)
231 SetThumbPosition(position
);
233 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
234 scrollEvent
!= wxEVT_SCROLL_ENDSCROLL
)
236 // don't process the event if there is no displacement,
237 // unless this is a thumb release or end scroll event.
241 wxScrollEvent
event(scrollEvent
, m_windowId
);
242 event
.SetPosition(position
);
243 event
.SetEventObject( this );
245 return GetEventHandler()->ProcessEvent(event
);
248 void wxScrollBar::SetThumbPosition(int viewStart
)
250 #if defined(__WIN95__)
252 info
.cbSize
= sizeof(SCROLLINFO
);
255 info
.nPos
= viewStart
;
256 info
.fMask
= SIF_POS
;
258 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
260 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
264 int wxScrollBar::GetThumbPosition(void) const
266 return ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
269 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
272 m_viewSize
= pageSize
;
273 m_pageSize
= thumbSize
;
274 m_objectSize
= range
;
276 // The range (number of scroll steps) is the
277 // object length minus the page size.
278 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
280 #if defined(__WIN95__)
281 // Try to adjust the range to cope with page size > 1
282 // (see comment for SetPageLength)
283 if ( m_pageSize
> 1 )
285 range1
+= (m_pageSize
- 1);
289 info
.cbSize
= sizeof(SCROLLINFO
);
290 info
.nPage
= m_pageSize
;
293 info
.nPos
= position
;
295 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
297 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
299 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
300 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
305 /* From the WIN32 documentation:
306 In version 4.0 or later, the maximum value that a scroll bar can report
307 (that is, the maximum scrolling position) depends on the page size.
308 If the scroll bar has a page size greater than one, the maximum scrolling position
309 is less than the maximum range value. You can use the following formula to calculate
310 the maximum scrolling position:
312 MaxScrollPos = MaxRangeValue - (PageSize - 1)
315 #if WXWIN_COMPATIBILITY
316 void wxScrollBar::SetPageSize(int pageLength
)
318 m_pageSize
= pageLength
;
320 #if defined(__WIN95__)
322 info
.cbSize
= sizeof(SCROLLINFO
);
323 info
.nPage
= pageLength
;
324 info
.fMask
= SIF_PAGE
;
326 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
330 void wxScrollBar::SetObjectLength(int objectLength
)
332 m_objectSize
= objectLength
;
334 // The range (number of scroll steps) is the
335 // object length minus the view size.
336 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
338 #if defined(__WIN95__)
339 // Try to adjust the range to cope with page size > 1
340 // (see comment for SetPageLength)
341 if ( m_pageSize
> 1 )
343 range
+= (m_pageSize
- 1);
347 info
.cbSize
= sizeof(SCROLLINFO
);
352 info
.fMask
= SIF_RANGE
;
354 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
356 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
360 void wxScrollBar::SetViewLength(int viewLength
)
362 m_viewSize
= viewLength
;
365 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
366 int *pageLength
) const
368 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
369 *viewLength
= m_viewSize
;
370 *objectLength
= m_objectSize
;
371 *pageLength
= m_pageSize
;
375 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
376 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
381 void wxScrollBar::Command(wxCommandEvent
& event
)
383 SetThumbPosition(event
.m_commandInt
);
384 ProcessCommand(event
);
387 #if WXWIN_COMPATIBILITY
388 // Backward compatibility
389 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
391 wxEventType oldEvent
= event
.GetEventType();
392 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
393 if ( !GetEventHandler()->ProcessEvent(event
) )
395 event
.SetEventType( oldEvent
);
396 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
402 #endif // wxUSE_SCROLLBAR