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 // 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
;
134 bool isScrolling
= TRUE
;
138 nScrollInc
= maxPos
- position
;
139 scrollEvent
= wxEVT_SCROLL_TOP
;
143 nScrollInc
= - position
;
144 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
149 scrollEvent
= wxEVT_SCROLL_LINEUP
;
154 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
158 nScrollInc
= -GetPageSize();
159 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
163 nScrollInc
= GetPageSize();
164 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
167 case SB_THUMBPOSITION
:
172 nScrollInc
= pos
- position
;
173 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
180 if (( nScrollInc
== 0 ) && isScrolling
)
182 // no event to process, so don't process it
183 // GRG: always process SB_THUMBPOSITION !
187 int new_pos
= position
+ nScrollInc
;
191 if (new_pos
> maxPos
)
194 SetThumbPosition(new_pos
);
195 wxScrollEvent
event(scrollEvent
, m_windowId
);
196 event
.SetPosition(new_pos
);
197 event
.SetEventObject( this );
198 event
.SetScrolling(isScrolling
);
200 return GetEventHandler()->ProcessEvent(event
);
203 void wxScrollBar::SetThumbPosition(int viewStart
)
205 #if defined(__WIN95__)
207 info
.cbSize
= sizeof(SCROLLINFO
);
210 info
.nPos
= viewStart
;
211 info
.fMask
= SIF_POS
;
213 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
215 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
219 int wxScrollBar::GetThumbPosition(void) const
221 return ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
224 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
227 m_viewSize
= pageSize
;
228 m_pageSize
= thumbSize
;
229 m_objectSize
= range
;
231 // The range (number of scroll steps) is the
232 // object length minus the page size.
233 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
235 #if defined(__WIN95__)
236 // Try to adjust the range to cope with page size > 1
237 // (see comment for SetPageLength)
238 if ( m_pageSize
> 1 )
240 range1
+= (m_pageSize
- 1);
244 info
.cbSize
= sizeof(SCROLLINFO
);
245 info
.nPage
= m_pageSize
;
248 info
.nPos
= position
;
250 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
252 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
254 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
255 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
260 /* From the WIN32 documentation:
261 In version 4.0 or later, the maximum value that a scroll bar can report
262 (that is, the maximum scrolling position) depends on the page size.
263 If the scroll bar has a page size greater than one, the maximum scrolling position
264 is less than the maximum range value. You can use the following formula to calculate
265 the maximum scrolling position:
267 MaxScrollPos = MaxRangeValue - (PageSize - 1)
270 #if WXWIN_COMPATIBILITY
271 void wxScrollBar::SetPageSize(int pageLength
)
273 m_pageSize
= pageLength
;
275 #if defined(__WIN95__)
277 info
.cbSize
= sizeof(SCROLLINFO
);
278 info
.nPage
= pageLength
;
279 info
.fMask
= SIF_PAGE
;
281 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
285 void wxScrollBar::SetObjectLength(int objectLength
)
287 m_objectSize
= objectLength
;
289 // The range (number of scroll steps) is the
290 // object length minus the view size.
291 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
293 #if defined(__WIN95__)
294 // Try to adjust the range to cope with page size > 1
295 // (see comment for SetPageLength)
296 if ( m_pageSize
> 1 )
298 range
+= (m_pageSize
- 1);
302 info
.cbSize
= sizeof(SCROLLINFO
);
307 info
.fMask
= SIF_RANGE
;
309 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
311 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
315 void wxScrollBar::SetViewLength(int viewLength
)
317 m_viewSize
= viewLength
;
320 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
321 int *pageLength
) const
323 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
324 *viewLength
= m_viewSize
;
325 *objectLength
= m_objectSize
;
326 *pageLength
= m_pageSize
;
330 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
331 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
336 void wxScrollBar::Command(wxCommandEvent
& event
)
338 SetThumbPosition(event
.m_commandInt
);
339 ProcessCommand(event
);
342 #if WXWIN_COMPATIBILITY
343 // Backward compatibility
344 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
346 wxEventType oldEvent
= event
.GetEventType();
347 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
348 if ( !GetEventHandler()->ProcessEvent(event
) )
350 event
.SetEventType( oldEvent
);
351 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))