1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/scrolbar.cpp
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"
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
)
85 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
;
87 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
88 wstyle
|= WS_CLIPSIBLINGS
;
90 // Now create scrollbar
91 DWORD _direction
= (style
& wxHORIZONTAL
) ?
93 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(style
), wxT("SCROLLBAR"), wxT("scrollbar"),
95 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
96 wxGetInstance(), NULL
);
102 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
103 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
104 ShowWindow(scroll_bar
, SW_SHOW
);
106 SetFont(parent
->GetFont());
108 m_hWnd
= (WXHWND
)scroll_bar
;
110 // Subclass again for purposes of dialog editing mode
111 SubclassWin((WXHWND
) scroll_bar
);
113 SetSize(x
, y
, width
, height
);
118 wxScrollBar::~wxScrollBar(void)
122 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
123 WXWORD pos
, WXHWND control
)
125 int position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
127 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
129 #if defined(__WIN95__)
130 // A page size greater than one has the effect of reducing the effective
131 // range, therefore the range has already been boosted artificially - so
133 if ( m_pageSize
> 1 )
134 maxPos
-= (m_pageSize
- 1);
137 wxEventType scrollEvent
= wxEVT_NULL
;
143 nScrollInc
= maxPos
- position
;
144 scrollEvent
= wxEVT_SCROLL_TOP
;
148 nScrollInc
= -position
;
149 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
154 scrollEvent
= wxEVT_SCROLL_LINEUP
;
159 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
163 nScrollInc
= -GetPageSize();
164 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
168 nScrollInc
= GetPageSize();
169 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
172 case SB_THUMBPOSITION
:
173 nScrollInc
= pos
- position
;
174 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
178 nScrollInc
= pos
- position
;
179 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
184 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
193 position
+= nScrollInc
;
197 if ( position
> maxPos
)
200 SetThumbPosition(position
);
202 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
203 scrollEvent
!= wxEVT_SCROLL_ENDSCROLL
)
205 // don't process the event if there is no displacement,
206 // unless this is a thumb release or end scroll event.
210 wxScrollEvent
event(scrollEvent
, m_windowId
);
211 event
.SetPosition(position
);
212 event
.SetEventObject( this );
214 return GetEventHandler()->ProcessEvent(event
);
217 void wxScrollBar::SetThumbPosition(int viewStart
)
219 #if defined(__WIN95__)
221 info
.cbSize
= sizeof(SCROLLINFO
);
224 info
.nPos
= viewStart
;
225 info
.fMask
= SIF_POS
;
227 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
229 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
233 int wxScrollBar::GetThumbPosition(void) const
235 return ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
238 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
241 m_viewSize
= pageSize
;
242 m_pageSize
= thumbSize
;
243 m_objectSize
= range
;
245 // The range (number of scroll steps) is the
246 // object length minus the page size.
247 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
249 #if defined(__WIN95__)
250 // Try to adjust the range to cope with page size > 1
251 // (see comment for SetPageLength)
252 if ( m_pageSize
> 1 )
254 range1
+= (m_pageSize
- 1);
258 info
.cbSize
= sizeof(SCROLLINFO
);
259 info
.nPage
= m_pageSize
;
262 info
.nPos
= position
;
264 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
266 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
268 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
269 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
274 /* From the WIN32 documentation:
275 In version 4.0 or later, the maximum value that a scroll bar can report
276 (that is, the maximum scrolling position) depends on the page size.
277 If the scroll bar has a page size greater than one, the maximum scrolling position
278 is less than the maximum range value. You can use the following formula to calculate
279 the maximum scrolling position:
281 MaxScrollPos = MaxRangeValue - (PageSize - 1)
284 #if WXWIN_COMPATIBILITY
285 void wxScrollBar::SetPageSize(int pageLength
)
287 m_pageSize
= pageLength
;
289 #if defined(__WIN95__)
291 info
.cbSize
= sizeof(SCROLLINFO
);
292 info
.nPage
= pageLength
;
293 info
.fMask
= SIF_PAGE
;
295 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
299 void wxScrollBar::SetObjectLength(int objectLength
)
301 m_objectSize
= objectLength
;
303 // The range (number of scroll steps) is the
304 // object length minus the view size.
305 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
307 #if defined(__WIN95__)
308 // Try to adjust the range to cope with page size > 1
309 // (see comment for SetPageLength)
310 if ( m_pageSize
> 1 )
312 range
+= (m_pageSize
- 1);
316 info
.cbSize
= sizeof(SCROLLINFO
);
321 info
.fMask
= SIF_RANGE
;
323 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
325 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
329 void wxScrollBar::SetViewLength(int viewLength
)
331 m_viewSize
= viewLength
;
334 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
335 int *pageLength
) const
337 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
338 *viewLength
= m_viewSize
;
339 *objectLength
= m_objectSize
;
340 *pageLength
= m_pageSize
;
344 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
345 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
350 void wxScrollBar::Command(wxCommandEvent
& event
)
352 SetThumbPosition(event
.m_commandInt
);
353 ProcessCommand(event
);
356 #if WXWIN_COMPATIBILITY
357 // Backward compatibility
358 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
360 wxEventType oldEvent
= event
.GetEventType();
361 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
362 if ( !GetEventHandler()->ProcessEvent(event
) )
364 event
.SetEventType( oldEvent
);
365 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
371 #endif // wxUSE_SCROLLBAR