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 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
34 BEGIN_EVENT_TABLE(wxScrollBar
, wxControl
)
35 #if WXWIN_COMPATIBILITY
36 EVT_SCROLL(wxScrollBar::OnScroll
)
43 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
45 const wxSize
& size
, long style
,
46 const wxValidator
& validator
,
51 parent
->AddChild(this);
53 SetValidator(validator
);
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 // Now create scrollbar
85 DWORD _direction
= (style
& wxHORIZONTAL
) ?
87 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(style
), wxT("SCROLLBAR"), wxT("scrollbar"),
88 _direction
| WS_CHILD
| WS_VISIBLE
,
89 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
90 wxGetInstance(), NULL
);
96 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
97 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
98 ShowWindow(scroll_bar
, SW_SHOW
);
100 SetFont(parent
->GetFont());
102 m_hWnd
= (WXHWND
)scroll_bar
;
104 // Subclass again for purposes of dialog editing mode
105 SubclassWin((WXHWND
) scroll_bar
);
107 SetSize(x
, y
, width
, height
);
112 wxScrollBar::~wxScrollBar(void)
116 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
117 WXWORD pos
, WXHWND control
)
119 int position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
121 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
123 #if defined(__WIN95__)
124 // A page size greater than one has the effect of reducing the effective
125 // range, therefore the range has already been boosted artificially - so
127 if ( m_pageSize
> 1 )
128 maxPos
-= (m_pageSize
- 1);
131 wxEventType scrollEvent
= wxEVT_NULL
;
137 nScrollInc
= maxPos
- position
;
138 scrollEvent
= wxEVT_SCROLL_TOP
;
142 nScrollInc
= - position
;
143 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
148 scrollEvent
= wxEVT_SCROLL_LINEUP
;
153 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
157 nScrollInc
= -GetPageSize();
158 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
162 nScrollInc
= GetPageSize();
163 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
167 case SB_THUMBPOSITION
:
168 nScrollInc
= pos
- position
;
169 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
176 if ( nScrollInc
== 0 )
178 // no event to process, so don't process it
182 int new_pos
= position
+ nScrollInc
;
186 if (new_pos
> maxPos
)
189 SetThumbPosition(new_pos
);
190 wxScrollEvent
event(scrollEvent
, m_windowId
);
191 event
.SetPosition(new_pos
);
192 event
.SetEventObject( this );
194 return GetEventHandler()->ProcessEvent(event
);
197 void wxScrollBar::SetThumbPosition(int viewStart
)
199 #if defined(__WIN95__)
201 info
.cbSize
= sizeof(SCROLLINFO
);
204 info
.nPos
= viewStart
;
205 info
.fMask
= SIF_POS
;
207 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
209 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
213 int wxScrollBar::GetThumbPosition(void) const
215 return ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
218 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
221 m_viewSize
= pageSize
;
222 m_pageSize
= thumbSize
;
223 m_objectSize
= range
;
225 // The range (number of scroll steps) is the
226 // object length minus the page size.
227 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
229 #if defined(__WIN95__)
230 // Try to adjust the range to cope with page size > 1
231 // (see comment for SetPageLength)
232 if ( m_pageSize
> 1 )
234 range1
+= (m_pageSize
- 1);
238 info
.cbSize
= sizeof(SCROLLINFO
);
239 info
.nPage
= m_pageSize
;
242 info
.nPos
= position
;
244 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
246 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
248 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
249 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
254 /* From the WIN32 documentation:
255 In version 4.0 or later, the maximum value that a scroll bar can report
256 (that is, the maximum scrolling position) depends on the page size.
257 If the scroll bar has a page size greater than one, the maximum scrolling position
258 is less than the maximum range value. You can use the following formula to calculate
259 the maximum scrolling position:
261 MaxScrollPos = MaxRangeValue - (PageSize - 1)
264 #if WXWIN_COMPATIBILITY
265 void wxScrollBar::SetPageSize(int pageLength
)
267 m_pageSize
= pageLength
;
269 #if defined(__WIN95__)
271 info
.cbSize
= sizeof(SCROLLINFO
);
272 info
.nPage
= pageLength
;
273 info
.fMask
= SIF_PAGE
;
275 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
279 void wxScrollBar::SetObjectLength(int objectLength
)
281 m_objectSize
= objectLength
;
283 // The range (number of scroll steps) is the
284 // object length minus the view size.
285 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
287 #if defined(__WIN95__)
288 // Try to adjust the range to cope with page size > 1
289 // (see comment for SetPageLength)
290 if ( m_pageSize
> 1 )
292 range
+= (m_pageSize
- 1);
296 info
.cbSize
= sizeof(SCROLLINFO
);
301 info
.fMask
= SIF_RANGE
;
303 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
305 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
309 void wxScrollBar::SetViewLength(int viewLength
)
311 m_viewSize
= viewLength
;
314 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
315 int *pageLength
) const
317 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
318 *viewLength
= m_viewSize
;
319 *objectLength
= m_objectSize
;
320 *pageLength
= m_pageSize
;
324 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
325 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
330 void wxScrollBar::Command(wxCommandEvent
& event
)
332 SetThumbPosition(event
.m_commandInt
);
333 ProcessCommand(event
);
336 #if WXWIN_COMPATIBILITY
337 // Backward compatibility
338 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
340 wxEventType oldEvent
= event
.GetEventType();
341 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
342 if ( !GetEventHandler()->ProcessEvent(event
) )
344 event
.SetEventType( oldEvent
);
345 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))