1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/scrolbar.cpp
3 // Purpose: wxScrollBar
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/scrolbar.h"
24 #include "wx/settings.h"
27 #include "wx/msw/private.h"
30 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
32 const wxSize
& size
, long style
,
33 const wxValidator
& validator
,
36 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
39 if (!MSWCreateControl(wxT("ScrollBar"), wxEmptyString
, pos
, size
))
42 SetScrollbar(0, 1, 2, 1, false);
47 wxScrollBar::~wxScrollBar(void)
51 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
52 WXWORD
WXUNUSED(pos
), WXHWND
WXUNUSED(control
))
54 // don't use pos parameter because it is limited to 16 bits, get the full
55 // 32 bit position from the control itself instead
56 WinStruct
<SCROLLINFO
> scrollInfo
;
57 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
59 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
61 wxLogLastError(wxT("GetScrollInfo"));
65 int maxPos
= scrollInfo
.nMax
;
67 // A page size greater than one has the effect of reducing the effective
68 // range, therefore the range has already been boosted artificially - so
71 maxPos
-= (m_pageSize
- 1);
73 int position
= scrollInfo
.nPos
;
74 wxEventType scrollEvent
= wxEVT_NULL
;
79 scrollEvent
= wxEVT_SCROLL_TOP
;
84 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
89 scrollEvent
= wxEVT_SCROLL_LINEUP
;
94 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
98 position
-= GetPageSize();
99 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
103 position
+= GetPageSize();
104 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
107 case SB_THUMBPOSITION
:
109 position
= scrollInfo
.nTrackPos
;
110 scrollEvent
= wParam
== SB_THUMBPOSITION
? wxEVT_SCROLL_THUMBRELEASE
111 : wxEVT_SCROLL_THUMBTRACK
;
115 scrollEvent
= wxEVT_SCROLL_CHANGED
;
119 if ( position
!= scrollInfo
.nPos
)
123 if ( position
> maxPos
)
126 SetThumbPosition(position
);
128 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
129 scrollEvent
!= wxEVT_SCROLL_CHANGED
)
131 // don't process the event if there is no displacement,
132 // unless this is a thumb release or end scroll event.
136 wxScrollEvent
event(scrollEvent
, m_windowId
);
137 event
.SetOrientation(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
138 event
.SetPosition(position
);
139 event
.SetEventObject( this );
141 return HandleWindowEvent(event
);
144 void wxScrollBar::SetThumbPosition(int viewStart
)
147 info
.cbSize
= sizeof(SCROLLINFO
);
150 info
.nPos
= viewStart
;
151 info
.fMask
= SIF_POS
;
153 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
156 int wxScrollBar::GetThumbPosition(void) const
158 WinStruct
<SCROLLINFO
> scrollInfo
;
159 scrollInfo
.fMask
= SIF_POS
;
161 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
163 wxLogLastError(wxT("GetScrollInfo"));
165 return scrollInfo
.nPos
;
168 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
171 m_viewSize
= pageSize
;
172 m_pageSize
= thumbSize
;
173 m_objectSize
= range
;
175 // The range (number of scroll steps) is the
176 // object length minus the page size.
177 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
179 // Try to adjust the range to cope with page size > 1
180 // (see comment for SetPageLength)
181 if ( m_pageSize
> 1 )
183 range1
+= (m_pageSize
- 1);
187 info
.cbSize
= sizeof(SCROLLINFO
);
188 info
.nPage
= m_pageSize
;
191 info
.nPos
= position
;
193 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
195 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
198 void wxScrollBar::Command(wxCommandEvent
& event
)
200 SetThumbPosition(event
.GetInt());
201 ProcessCommand(event
);
204 wxSize
wxScrollBar::DoGetBestSize() const
211 w
= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
215 h
= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
223 WXDWORD
wxScrollBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
225 // we never have an external border
226 WXDWORD msStyle
= wxControl::MSWGetStyle
228 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
231 // SBS_HORZ is 0 anyhow, but do mention it explicitly for clarity
232 msStyle
|= style
& wxSB_HORIZONTAL
? SBS_HORZ
: SBS_VERT
;
237 WXHBRUSH
wxScrollBar::MSWControlColor(WXHDC pDC
, WXHWND hWnd
)
239 // unless we have an explicitly set bg colour, use default (gradient under
240 // XP) brush instead of GetBackgroundColour() one as the base class would
242 // note that fg colour isn't used for a scrollbar
243 return UseBgCol() ? wxControl::MSWControlColor(pDC
, hWnd
) : NULL
;
246 #endif // wxUSE_SCROLLBAR