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
WXUNUSED(control
))
126 // current and max positions
128 maxPos
, trackPos
= pos
;
130 // when we're dragging the scrollbar we can't use pos parameter because it
131 // is limited to 16 bits
132 // JACS: now always using GetScrollInfo, since there's no reason
134 // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
136 SCROLLINFO scrollInfo
;
137 wxZeroMemory(scrollInfo
);
138 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
140 // also get the range if we call GetScrollInfo() anyhow -- this is less
141 // expensive than call it once here and then call GetScrollRange()
143 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
145 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
147 wxLogLastError(_T("GetScrollInfo"));
150 trackPos
= scrollInfo
.nTrackPos
;
151 position
= scrollInfo
.nPos
;
152 maxPos
= scrollInfo
.nMax
;
157 position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
159 ::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 SCROLLINFO scrollInfo
;
271 wxZeroMemory(scrollInfo
);
272 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
273 scrollInfo
.fMask
= SIF_POS
;
275 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
277 wxLogLastError(_T("GetScrollInfo"));
279 return scrollInfo
.nPos
;
280 // return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
283 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
286 m_viewSize
= pageSize
;
287 m_pageSize
= thumbSize
;
288 m_objectSize
= range
;
290 // The range (number of scroll steps) is the
291 // object length minus the page size.
292 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
294 #if defined(__WIN95__)
295 // Try to adjust the range to cope with page size > 1
296 // (see comment for SetPageLength)
297 if ( m_pageSize
> 1 )
299 range1
+= (m_pageSize
- 1);
303 info
.cbSize
= sizeof(SCROLLINFO
);
304 info
.nPage
= m_pageSize
;
307 info
.nPos
= position
;
309 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
311 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
313 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
314 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
319 /* From the WIN32 documentation:
320 In version 4.0 or later, the maximum value that a scroll bar can report
321 (that is, the maximum scrolling position) depends on the page size.
322 If the scroll bar has a page size greater than one, the maximum scrolling position
323 is less than the maximum range value. You can use the following formula to calculate
324 the maximum scrolling position:
326 MaxScrollPos = MaxRangeValue - (PageSize - 1)
329 #if WXWIN_COMPATIBILITY
330 void wxScrollBar::SetPageSize(int pageLength
)
332 m_pageSize
= pageLength
;
334 #if defined(__WIN95__)
336 info
.cbSize
= sizeof(SCROLLINFO
);
337 info
.nPage
= pageLength
;
338 info
.fMask
= SIF_PAGE
;
340 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
344 void wxScrollBar::SetObjectLength(int objectLength
)
346 m_objectSize
= objectLength
;
348 // The range (number of scroll steps) is the
349 // object length minus the view size.
350 int range
= wxMax((objectLength
- m_viewSize
), 0) ;
352 #if defined(__WIN95__)
353 // Try to adjust the range to cope with page size > 1
354 // (see comment for SetPageLength)
355 if ( m_pageSize
> 1 )
357 range
+= (m_pageSize
- 1);
361 info
.cbSize
= sizeof(SCROLLINFO
);
366 info
.fMask
= SIF_RANGE
;
368 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
370 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range
, TRUE
);
374 void wxScrollBar::SetViewLength(int viewLength
)
376 m_viewSize
= viewLength
;
379 void wxScrollBar::GetValues(int *viewStart
, int *viewLength
, int *objectLength
,
380 int *pageLength
) const
382 *viewStart
= ::GetScrollPos((HWND
)m_hWnd
, SB_CTL
);
383 *viewLength
= m_viewSize
;
384 *objectLength
= m_objectSize
;
385 *pageLength
= m_pageSize
;
389 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
390 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
395 void wxScrollBar::Command(wxCommandEvent
& event
)
397 SetThumbPosition(event
.m_commandInt
);
398 ProcessCommand(event
);
401 #if WXWIN_COMPATIBILITY
402 // Backward compatibility
403 void wxScrollBar::OnScroll(wxScrollEvent
& event
)
405 wxEventType oldEvent
= event
.GetEventType();
406 event
.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED
);
407 if ( !GetEventHandler()->ProcessEvent(event
) )
409 event
.SetEventType( oldEvent
);
410 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
416 #endif // wxUSE_SCROLLBAR