1 /////////////////////////////////////////////////////////////////////////////
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"
28 #include "wx/scrolbar.h"
29 #include "wx/msw/private.h"
31 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
33 BEGIN_EVENT_TABLE(wxScrollBar
, wxControl
)
34 #if WXWIN_COMPATIBILITY
35 EVT_SCROLL(wxScrollBar::OnScroll
)
41 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
43 const wxSize
& size
, long style
,
44 const wxValidator
& validator
,
49 parent
->AddChild(this);
52 SetValidator(validator
);
53 #endif // wxUSE_VALIDATORS
55 SetBackgroundColour(parent
->GetBackgroundColour()) ;
56 SetForegroundColour(parent
->GetForegroundColour()) ;
57 m_windowStyle
= style
;
60 m_windowId
= (int)NewControlId();
71 if (style
& wxHORIZONTAL
)
78 if (style
& wxVERTICAL
)
84 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
;
86 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
87 wstyle
|= WS_CLIPSIBLINGS
;
89 // Now create scrollbar
90 DWORD _direction
= (style
& wxHORIZONTAL
) ?
92 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(style
), wxT("SCROLLBAR"), wxT("scrollbar"),
94 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
95 wxGetInstance(), NULL
);
101 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
102 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
103 ShowWindow(scroll_bar
, SW_SHOW
);
105 SetFont(parent
->GetFont());
107 m_hWnd
= (WXHWND
)scroll_bar
;
109 // Subclass again for purposes of dialog editing mode
110 SubclassWin((WXHWND
) scroll_bar
);
112 SetSize(x
, y
, width
, height
);
117 wxScrollBar::~wxScrollBar(void)
121 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
122 WXWORD pos
, WXHWND control
)
124 int position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
126 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
128 #if defined(__WIN95__)
129 // A page size greater than one has the effect of reducing the effective
130 // range, therefore the range has already been boosted artificially - so
132 if ( m_pageSize
> 1 )
133 maxPos
-= (m_pageSize
- 1);
136 wxEventType scrollEvent
= wxEVT_NULL
;
142 nScrollInc
= maxPos
- position
;
143 scrollEvent
= wxEVT_SCROLL_TOP
;
147 nScrollInc
= - position
;
148 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
153 scrollEvent
= wxEVT_SCROLL_LINEUP
;
158 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
162 nScrollInc
= -GetPageSize();
163 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
167 nScrollInc
= GetPageSize();
168 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
171 case SB_THUMBPOSITION
:
172 nScrollInc
= pos
- position
;
173 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
177 nScrollInc
= pos
- position
;
178 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
185 // don't process the event if there is no displacement,
186 // unless this is a thumb release event.
187 if (( nScrollInc
== 0 ) && ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
))
192 int new_pos
= position
+ nScrollInc
;
196 if (new_pos
> maxPos
)
199 SetThumbPosition(new_pos
);
200 wxScrollEvent
event(scrollEvent
, m_windowId
);
201 event
.SetPosition(new_pos
);
202 event
.SetEventObject( this );
204 return GetEventHandler()->ProcessEvent(event
);
207 void wxScrollBar::SetThumbPosition(int viewStart
)
209 #if defined(__WIN95__)
211 info
.cbSize
= sizeof(SCROLLINFO
);
214 info
.nPos
= viewStart
;
215 info
.fMask
= SIF_POS
;
217 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
219 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
223 int wxScrollBar::GetThumbPosition(void) const
225 return ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
228 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
231 m_viewSize
= pageSize
;
232 m_pageSize
= thumbSize
;
233 m_objectSize
= range
;
235 // The range (number of scroll steps) is the
236 // object length minus the page size.
237 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
239 #if defined(__WIN95__)
240 // Try to adjust the range to cope with page size > 1
241 // (see comment for SetPageLength)
242 if ( m_pageSize
> 1 )
244 range1
+= (m_pageSize
- 1);
248 info
.cbSize
= sizeof(SCROLLINFO
);
249 info
.nPage
= m_pageSize
;
252 info
.nPos
= position
;
254 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
256 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
258 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
259 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
264 /* From the WIN32 documentation:
265 In version 4.0 or later, the maximum value that a scroll bar can report
266 (that is, the maximum scrolling position) depends on the page size.
267 If the scroll bar has a page size greater than one, the maximum scrolling position
268 is less than the maximum range value. You can use the following formula to calculate
269 the maximum scrolling position:
271 MaxScrollPos = MaxRangeValue - (PageSize - 1)
274 #if WXWIN_COMPATIBILITY
275 void wxScrollBar::SetPageSize(int pageLength
)
277 m_pageSize
= pageLength
;
279 #if defined(__WIN95__)
281 info
.cbSize
= sizeof(SCROLLINFO
);
282 info
.nPage
= pageLength
;
283 info
.fMask
= SIF_PAGE
;
285 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
289 void wxScrollBar::SetObjectLength(int objectLength
)
291 m_objectSize
= objectLength
;
293 // The range (number of scroll steps) is the
294 // object length minus the view size.
295 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
297 #if defined(__WIN95__)
298 // Try to adjust the range to cope with page size > 1
299 // (see comment for SetPageLength)
300 if ( m_pageSize
> 1 )
302 range
+= (m_pageSize
- 1);
306 info
.cbSize
= sizeof(SCROLLINFO
);
311 info
.fMask
= SIF_RANGE
;
313 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
315 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
319 void wxScrollBar::SetViewLength(int viewLength
)
321 m_viewSize
= viewLength
;
324 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
325 int *pageLength
) const
327 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
328 *viewLength
= m_viewSize
;
329 *objectLength
= m_objectSize
;
330 *pageLength
= m_pageSize
;
334 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
335 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
340 void wxScrollBar::Command(wxCommandEvent
& event
)
342 SetThumbPosition(event
.m_commandInt
);
343 ProcessCommand(event
);
346 #if WXWIN_COMPATIBILITY
347 // Backward compatibility
348 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
350 wxEventType oldEvent
= event
.GetEventType();
351 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
352 if ( !GetEventHandler()->ProcessEvent(event
) )
354 event
.SetEventType( oldEvent
);
355 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))