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
) ;
89 if ( m_windowStyle & wxCLIP_SIBLINGS )
90 wstyle |= WS_CLIPSIBLINGS;
93 // Now create scrollbar
94 DWORD _direction
= (style
& wxHORIZONTAL
) ?
96 HWND scroll_bar
= CreateWindowEx(exStyle
, wxT("SCROLLBAR"), wxT("scrollbar"),
98 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
99 wxGetInstance(), NULL
);
105 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
106 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
107 ShowWindow(scroll_bar
, SW_SHOW
);
109 SetFont(parent
->GetFont());
111 m_hWnd
= (WXHWND
)scroll_bar
;
113 // Subclass again for purposes of dialog editing mode
114 SubclassWin((WXHWND
) scroll_bar
);
116 SetSize(x
, y
, width
, height
);
121 wxScrollBar::~wxScrollBar(void)
125 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
126 WXWORD pos
, WXHWND control
)
128 // current and max positions
130 maxPos
, trackPos
= pos
;
133 // when we're dragging the scrollbar we can't use pos parameter because it
134 // is limited to 16 bits
135 if ( wParam
== SB_THUMBPOSITION
|| wParam
== SB_THUMBTRACK
)
137 SCROLLINFO scrollInfo
;
138 wxZeroMemory(scrollInfo
);
139 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
141 // also get the range if we call GetScrollInfo() anyhow -- this is less
142 // expensive than call it once here and then call GetScrollRange()
144 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
146 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
148 wxLogLastError(_T("GetScrollInfo"));
151 trackPos
= scrollInfo
.nTrackPos
;
152 position
= scrollInfo
.nPos
;
153 maxPos
= scrollInfo
.nMax
;
158 position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
160 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
163 #if defined(__WIN95__)
164 // A page size greater than one has the effect of reducing the effective
165 // range, therefore the range has already been boosted artificially - so
167 if ( m_pageSize
> 1 )
168 maxPos
-= (m_pageSize
- 1);
171 wxEventType scrollEvent
= wxEVT_NULL
;
177 nScrollInc
= maxPos
- position
;
178 scrollEvent
= wxEVT_SCROLL_TOP
;
182 nScrollInc
= -position
;
183 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
188 scrollEvent
= wxEVT_SCROLL_LINEUP
;
193 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
197 nScrollInc
= -GetPageSize();
198 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
202 nScrollInc
= GetPageSize();
203 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
206 case SB_THUMBPOSITION
:
207 nScrollInc
= trackPos
- position
;
208 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
212 nScrollInc
= trackPos
- position
;
213 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
218 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
227 position
+= nScrollInc
;
231 if ( position
> maxPos
)
234 SetThumbPosition(position
);
236 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
237 scrollEvent
!= wxEVT_SCROLL_ENDSCROLL
)
239 // don't process the event if there is no displacement,
240 // unless this is a thumb release or end scroll event.
244 wxScrollEvent
event(scrollEvent
, m_windowId
);
245 event
.SetOrientation(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
246 event
.SetPosition(position
);
247 event
.SetEventObject( this );
249 return GetEventHandler()->ProcessEvent(event
);
252 void wxScrollBar::SetThumbPosition(int viewStart
)
254 #if defined(__WIN95__)
256 info
.cbSize
= sizeof(SCROLLINFO
);
259 info
.nPos
= viewStart
;
260 info
.fMask
= SIF_POS
;
262 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
264 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
268 int wxScrollBar::GetThumbPosition(void) const
270 return ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
273 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
276 m_viewSize
= pageSize
;
277 m_pageSize
= thumbSize
;
278 m_objectSize
= range
;
280 // The range (number of scroll steps) is the
281 // object length minus the page size.
282 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
284 #if defined(__WIN95__)
285 // Try to adjust the range to cope with page size > 1
286 // (see comment for SetPageLength)
287 if ( m_pageSize
> 1 )
289 range1
+= (m_pageSize
- 1);
293 info
.cbSize
= sizeof(SCROLLINFO
);
294 info
.nPage
= m_pageSize
;
297 info
.nPos
= position
;
299 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
301 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
303 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
304 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
309 /* From the WIN32 documentation:
310 In version 4.0 or later, the maximum value that a scroll bar can report
311 (that is, the maximum scrolling position) depends on the page size.
312 If the scroll bar has a page size greater than one, the maximum scrolling position
313 is less than the maximum range value. You can use the following formula to calculate
314 the maximum scrolling position:
316 MaxScrollPos = MaxRangeValue - (PageSize - 1)
319 #if WXWIN_COMPATIBILITY
320 void wxScrollBar::SetPageSize(int pageLength
)
322 m_pageSize
= pageLength
;
324 #if defined(__WIN95__)
326 info
.cbSize
= sizeof(SCROLLINFO
);
327 info
.nPage
= pageLength
;
328 info
.fMask
= SIF_PAGE
;
330 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
334 void wxScrollBar::SetObjectLength(int objectLength
)
336 m_objectSize
= objectLength
;
338 // The range (number of scroll steps) is the
339 // object length minus the view size.
340 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
342 #if defined(__WIN95__)
343 // Try to adjust the range to cope with page size > 1
344 // (see comment for SetPageLength)
345 if ( m_pageSize
> 1 )
347 range
+= (m_pageSize
- 1);
351 info
.cbSize
= sizeof(SCROLLINFO
);
356 info
.fMask
= SIF_RANGE
;
358 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
360 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
364 void wxScrollBar::SetViewLength(int viewLength
)
366 m_viewSize
= viewLength
;
369 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
370 int *pageLength
) const
372 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
373 *viewLength
= m_viewSize
;
374 *objectLength
= m_objectSize
;
375 *pageLength
= m_pageSize
;
379 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
380 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
385 void wxScrollBar::Command(wxCommandEvent
& event
)
387 SetThumbPosition(event
.m_commandInt
);
388 ProcessCommand(event
);
391 #if WXWIN_COMPATIBILITY
392 // Backward compatibility
393 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
395 wxEventType oldEvent
= event
.GetEventType();
396 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
397 if ( !GetEventHandler()->ProcessEvent(event
) )
399 event
.SetEventType( oldEvent
);
400 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
406 #endif // wxUSE_SCROLLBAR