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
;
186 // don't process the event if there is no displacement,
187 // unless this is a thumb release event.
188 if (( nScrollInc
== 0 ) && ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
))
193 int new_pos
= position
+ nScrollInc
;
197 if (new_pos
> maxPos
)
200 SetThumbPosition(new_pos
);
201 wxScrollEvent
event(scrollEvent
, m_windowId
);
202 event
.SetPosition(new_pos
);
203 event
.SetEventObject( this );
205 return GetEventHandler()->ProcessEvent(event
);
208 void wxScrollBar::SetThumbPosition(int viewStart
)
210 #if defined(__WIN95__)
212 info
.cbSize
= sizeof(SCROLLINFO
);
215 info
.nPos
= viewStart
;
216 info
.fMask
= SIF_POS
;
218 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
220 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
224 int wxScrollBar::GetThumbPosition(void) const
226 return ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
229 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
232 m_viewSize
= pageSize
;
233 m_pageSize
= thumbSize
;
234 m_objectSize
= range
;
236 // The range (number of scroll steps) is the
237 // object length minus the page size.
238 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
240 #if defined(__WIN95__)
241 // Try to adjust the range to cope with page size > 1
242 // (see comment for SetPageLength)
243 if ( m_pageSize
> 1 )
245 range1
+= (m_pageSize
- 1);
249 info
.cbSize
= sizeof(SCROLLINFO
);
250 info
.nPage
= m_pageSize
;
253 info
.nPos
= position
;
255 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
257 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
259 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
260 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
265 /* From the WIN32 documentation:
266 In version 4.0 or later, the maximum value that a scroll bar can report
267 (that is, the maximum scrolling position) depends on the page size.
268 If the scroll bar has a page size greater than one, the maximum scrolling position
269 is less than the maximum range value. You can use the following formula to calculate
270 the maximum scrolling position:
272 MaxScrollPos = MaxRangeValue - (PageSize - 1)
275 #if WXWIN_COMPATIBILITY
276 void wxScrollBar::SetPageSize(int pageLength
)
278 m_pageSize
= pageLength
;
280 #if defined(__WIN95__)
282 info
.cbSize
= sizeof(SCROLLINFO
);
283 info
.nPage
= pageLength
;
284 info
.fMask
= SIF_PAGE
;
286 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
290 void wxScrollBar::SetObjectLength(int objectLength
)
292 m_objectSize
= objectLength
;
294 // The range (number of scroll steps) is the
295 // object length minus the view size.
296 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
298 #if defined(__WIN95__)
299 // Try to adjust the range to cope with page size > 1
300 // (see comment for SetPageLength)
301 if ( m_pageSize
> 1 )
303 range
+= (m_pageSize
- 1);
307 info
.cbSize
= sizeof(SCROLLINFO
);
312 info
.fMask
= SIF_RANGE
;
314 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
316 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
320 void wxScrollBar::SetViewLength(int viewLength
)
322 m_viewSize
= viewLength
;
325 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
326 int *pageLength
) const
328 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
329 *viewLength
= m_viewSize
;
330 *objectLength
= m_objectSize
;
331 *pageLength
= m_pageSize
;
335 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
336 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
341 void wxScrollBar::Command(wxCommandEvent
& event
)
343 SetThumbPosition(event
.m_commandInt
);
344 ProcessCommand(event
);
347 #if WXWIN_COMPATIBILITY
348 // Backward compatibility
349 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
351 wxEventType oldEvent
= event
.GetEventType();
352 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
353 if ( !GetEventHandler()->ProcessEvent(event
) )
355 event
.SetEventType( oldEvent
);
356 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
362 #endif // wxUSE_SCROLLBAR