1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/scrolbar.cpp
3 // Purpose: wxScrollBar
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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
)
86 WXDWORD wstyle
= MSWGetStyle(style
, & exStyle
) ;
88 // Now create scrollbar
89 DWORD _direction
= (style
& wxHORIZONTAL
) ?
91 HWND scroll_bar
= CreateWindowEx(exStyle
, wxT("SCROLLBAR"), wxT("scrollbar"),
93 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
94 wxGetInstance(), NULL
);
100 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
101 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
102 ShowWindow(scroll_bar
, SW_SHOW
);
104 SetFont(parent
->GetFont());
106 m_hWnd
= (WXHWND
)scroll_bar
;
108 // Subclass again for purposes of dialog editing mode
109 SubclassWin((WXHWND
) scroll_bar
);
111 SetSize(x
, y
, width
, height
);
116 wxScrollBar::~wxScrollBar(void)
120 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
121 WXWORD pos
, WXHWND control
)
123 // current and max positions
125 maxPos
, trackPos
= pos
;
128 // when we're dragging the scrollbar we can't use pos parameter because it
129 // is limited to 16 bits
130 if ( wParam
== SB_THUMBPOSITION
|| wParam
== SB_THUMBTRACK
)
132 SCROLLINFO scrollInfo
;
133 wxZeroMemory(scrollInfo
);
134 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
136 // also get the range if we call GetScrollInfo() anyhow -- this is less
137 // expensive than call it once here and then call GetScrollRange()
139 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
141 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
143 wxLogLastError(_T("GetScrollInfo"));
146 trackPos
= scrollInfo
.nTrackPos
;
147 position
= scrollInfo
.nPos
;
148 maxPos
= scrollInfo
.nMax
;
153 position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
155 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
158 #if defined(__WIN95__)
159 // A page size greater than one has the effect of reducing the effective
160 // range, therefore the range has already been boosted artificially - so
162 if ( m_pageSize
> 1 )
163 maxPos
-= (m_pageSize
- 1);
166 wxEventType scrollEvent
= wxEVT_NULL
;
172 nScrollInc
= maxPos
- position
;
173 scrollEvent
= wxEVT_SCROLL_TOP
;
177 nScrollInc
= -position
;
178 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
183 scrollEvent
= wxEVT_SCROLL_LINEUP
;
188 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
192 nScrollInc
= -GetPageSize();
193 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
197 nScrollInc
= GetPageSize();
198 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
201 case SB_THUMBPOSITION
:
202 nScrollInc
= trackPos
- position
;
203 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
207 nScrollInc
= trackPos
- position
;
208 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
213 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
222 position
+= nScrollInc
;
226 if ( position
> maxPos
)
229 SetThumbPosition(position
);
231 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
232 scrollEvent
!= wxEVT_SCROLL_ENDSCROLL
)
234 // don't process the event if there is no displacement,
235 // unless this is a thumb release or end scroll event.
239 wxScrollEvent
event(scrollEvent
, m_windowId
);
240 event
.SetOrientation(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
241 event
.SetPosition(position
);
242 event
.SetEventObject( this );
244 return GetEventHandler()->ProcessEvent(event
);
247 void wxScrollBar::SetThumbPosition(int viewStart
)
249 #if defined(__WIN95__)
251 info
.cbSize
= sizeof(SCROLLINFO
);
254 info
.nPos
= viewStart
;
255 info
.fMask
= SIF_POS
;
257 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
259 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
263 int wxScrollBar::GetThumbPosition(void) const
265 return ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
268 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
271 m_viewSize
= pageSize
;
272 m_pageSize
= thumbSize
;
273 m_objectSize
= range
;
275 // The range (number of scroll steps) is the
276 // object length minus the page size.
277 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
279 #if defined(__WIN95__)
280 // Try to adjust the range to cope with page size > 1
281 // (see comment for SetPageLength)
282 if ( m_pageSize
> 1 )
284 range1
+= (m_pageSize
- 1);
288 info
.cbSize
= sizeof(SCROLLINFO
);
289 info
.nPage
= m_pageSize
;
292 info
.nPos
= position
;
294 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
296 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
298 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
299 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
304 /* From the WIN32 documentation:
305 In version 4.0 or later, the maximum value that a scroll bar can report
306 (that is, the maximum scrolling position) depends on the page size.
307 If the scroll bar has a page size greater than one, the maximum scrolling position
308 is less than the maximum range value. You can use the following formula to calculate
309 the maximum scrolling position:
311 MaxScrollPos = MaxRangeValue - (PageSize - 1)
314 #if WXWIN_COMPATIBILITY
315 void wxScrollBar::SetPageSize(int pageLength
)
317 m_pageSize
= pageLength
;
319 #if defined(__WIN95__)
321 info
.cbSize
= sizeof(SCROLLINFO
);
322 info
.nPage
= pageLength
;
323 info
.fMask
= SIF_PAGE
;
325 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
329 void wxScrollBar::SetObjectLength(int objectLength
)
331 m_objectSize
= objectLength
;
333 // The range (number of scroll steps) is the
334 // object length minus the view size.
335 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
337 #if defined(__WIN95__)
338 // Try to adjust the range to cope with page size > 1
339 // (see comment for SetPageLength)
340 if ( m_pageSize
> 1 )
342 range
+= (m_pageSize
- 1);
346 info
.cbSize
= sizeof(SCROLLINFO
);
351 info
.fMask
= SIF_RANGE
;
353 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
355 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
359 void wxScrollBar::SetViewLength(int viewLength
)
361 m_viewSize
= viewLength
;
364 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
365 int *pageLength
) const
367 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
368 *viewLength
= m_viewSize
;
369 *objectLength
= m_objectSize
;
370 *pageLength
= m_pageSize
;
374 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
375 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
380 void wxScrollBar::Command(wxCommandEvent
& event
)
382 SetThumbPosition(event
.m_commandInt
);
383 ProcessCommand(event
);
386 #if WXWIN_COMPATIBILITY
387 // Backward compatibility
388 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
390 wxEventType oldEvent
= event
.GetEventType();
391 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
392 if ( !GetEventHandler()->ProcessEvent(event
) )
394 event
.SetEventType( oldEvent
);
395 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
401 #endif // wxUSE_SCROLLBAR