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 if ((style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
59 SetBackgroundColour(parent
->GetBackgroundColour()) ;
60 SetForegroundColour(parent
->GetForegroundColour()) ;
61 m_windowStyle
= style
;
64 m_windowId
= (int)NewControlId();
75 if (style
& wxHORIZONTAL
)
82 if (style
& wxVERTICAL
)
89 WXDWORD wstyle
= MSWGetStyle(style
, & exStyle
) ;
91 // Now create scrollbar
92 DWORD _direction
= (style
& wxHORIZONTAL
) ?
94 HWND scroll_bar
= CreateWindowEx(exStyle
, wxT("SCROLLBAR"), wxT("scrollbar"),
96 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
97 wxGetInstance(), NULL
);
103 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
104 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
105 ShowWindow(scroll_bar
, SW_SHOW
);
107 SetFont(parent
->GetFont());
109 m_hWnd
= (WXHWND
)scroll_bar
;
111 // Subclass again for purposes of dialog editing mode
112 SubclassWin((WXHWND
) scroll_bar
);
114 SetSize(x
, y
, width
, height
);
119 wxScrollBar::~wxScrollBar(void)
123 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
124 WXWORD pos
, WXHWND control
)
126 // current and max positions
128 maxPos
, trackPos
= pos
;
131 // when we're dragging the scrollbar we can't use pos parameter because it
132 // is limited to 16 bits
133 if ( wParam
== SB_THUMBPOSITION
|| wParam
== SB_THUMBTRACK
)
135 SCROLLINFO scrollInfo
;
136 wxZeroMemory(scrollInfo
);
137 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
139 // also get the range if we call GetScrollInfo() anyhow -- this is less
140 // expensive than call it once here and then call GetScrollRange()
142 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
144 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
146 wxLogLastError(_T("GetScrollInfo"));
149 trackPos
= scrollInfo
.nTrackPos
;
150 position
= scrollInfo
.nPos
;
151 maxPos
= scrollInfo
.nMax
;
156 position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
158 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
161 #if defined(__WIN95__)
162 // A page size greater than one has the effect of reducing the effective
163 // range, therefore the range has already been boosted artificially - so
165 if ( m_pageSize
> 1 )
166 maxPos
-= (m_pageSize
- 1);
169 wxEventType scrollEvent
= wxEVT_NULL
;
175 nScrollInc
= maxPos
- position
;
176 scrollEvent
= wxEVT_SCROLL_TOP
;
180 nScrollInc
= -position
;
181 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
186 scrollEvent
= wxEVT_SCROLL_LINEUP
;
191 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
195 nScrollInc
= -GetPageSize();
196 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
200 nScrollInc
= GetPageSize();
201 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
204 case SB_THUMBPOSITION
:
205 nScrollInc
= trackPos
- position
;
206 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
210 nScrollInc
= trackPos
- position
;
211 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
216 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
225 position
+= nScrollInc
;
229 if ( position
> maxPos
)
232 SetThumbPosition(position
);
234 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
235 scrollEvent
!= wxEVT_SCROLL_ENDSCROLL
)
237 // don't process the event if there is no displacement,
238 // unless this is a thumb release or end scroll event.
242 wxScrollEvent
event(scrollEvent
, m_windowId
);
243 event
.SetOrientation(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
244 event
.SetPosition(position
);
245 event
.SetEventObject( this );
247 return GetEventHandler()->ProcessEvent(event
);
250 void wxScrollBar::SetThumbPosition(int viewStart
)
252 #if defined(__WIN95__)
254 info
.cbSize
= sizeof(SCROLLINFO
);
257 info
.nPos
= viewStart
;
258 info
.fMask
= SIF_POS
;
260 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
262 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
266 int wxScrollBar::GetThumbPosition(void) const
268 return ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
271 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
274 m_viewSize
= pageSize
;
275 m_pageSize
= thumbSize
;
276 m_objectSize
= range
;
278 // The range (number of scroll steps) is the
279 // object length minus the page size.
280 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
282 #if defined(__WIN95__)
283 // Try to adjust the range to cope with page size > 1
284 // (see comment for SetPageLength)
285 if ( m_pageSize
> 1 )
287 range1
+= (m_pageSize
- 1);
291 info
.cbSize
= sizeof(SCROLLINFO
);
292 info
.nPage
= m_pageSize
;
295 info
.nPos
= position
;
297 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
299 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
301 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
302 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
307 /* From the WIN32 documentation:
308 In version 4.0 or later, the maximum value that a scroll bar can report
309 (that is, the maximum scrolling position) depends on the page size.
310 If the scroll bar has a page size greater than one, the maximum scrolling position
311 is less than the maximum range value. You can use the following formula to calculate
312 the maximum scrolling position:
314 MaxScrollPos = MaxRangeValue - (PageSize - 1)
317 #if WXWIN_COMPATIBILITY
318 void wxScrollBar::SetPageSize(int pageLength
)
320 m_pageSize
= pageLength
;
322 #if defined(__WIN95__)
324 info
.cbSize
= sizeof(SCROLLINFO
);
325 info
.nPage
= pageLength
;
326 info
.fMask
= SIF_PAGE
;
328 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
332 void wxScrollBar::SetObjectLength(int objectLength
)
334 m_objectSize
= objectLength
;
336 // The range (number of scroll steps) is the
337 // object length minus the view size.
338 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
340 #if defined(__WIN95__)
341 // Try to adjust the range to cope with page size > 1
342 // (see comment for SetPageLength)
343 if ( m_pageSize
> 1 )
345 range
+= (m_pageSize
- 1);
349 info
.cbSize
= sizeof(SCROLLINFO
);
354 info
.fMask
= SIF_RANGE
;
356 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
358 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
362 void wxScrollBar::SetViewLength(int viewLength
)
364 m_viewSize
= viewLength
;
367 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
368 int *pageLength
) const
370 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
371 *viewLength
= m_viewSize
;
372 *objectLength
= m_objectSize
;
373 *pageLength
= m_pageSize
;
377 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
378 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
383 void wxScrollBar::Command(wxCommandEvent
& event
)
385 SetThumbPosition(event
.m_commandInt
);
386 ProcessCommand(event
);
389 #if WXWIN_COMPATIBILITY
390 // Backward compatibility
391 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
393 wxEventType oldEvent
= event
.GetEventType();
394 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
395 if ( !GetEventHandler()->ProcessEvent(event
) )
397 event
.SetEventType( oldEvent
);
398 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
404 #endif // wxUSE_SCROLLBAR