1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/scrolbar.cpp
3 // Purpose: wxScrollBar
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/scrolbar.h"
26 #include "wx/msw/private.h"
27 #include "wx/settings.h"
29 #if wxUSE_EXTENDED_RTTI
30 WX_DEFINE_FLAGS( wxScrollBarStyle
)
32 wxBEGIN_FLAGS( wxScrollBarStyle
)
33 // new style border flags, we put them first to
34 // use them for streaming out
35 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
36 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
37 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
38 wxFLAGS_MEMBER(wxBORDER_RAISED
)
39 wxFLAGS_MEMBER(wxBORDER_STATIC
)
40 wxFLAGS_MEMBER(wxBORDER_NONE
)
42 // old style border flags
43 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
44 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
45 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
46 wxFLAGS_MEMBER(wxRAISED_BORDER
)
47 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
48 wxFLAGS_MEMBER(wxBORDER
)
50 // standard window styles
51 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
52 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
53 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
54 wxFLAGS_MEMBER(wxWANTS_CHARS
)
55 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
56 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
57 wxFLAGS_MEMBER(wxVSCROLL
)
58 wxFLAGS_MEMBER(wxHSCROLL
)
60 wxFLAGS_MEMBER(wxSB_HORIZONTAL
)
61 wxFLAGS_MEMBER(wxSB_VERTICAL
)
63 wxEND_FLAGS( wxScrollBarStyle
)
65 IMPLEMENT_DYNAMIC_CLASS_XTI(wxScrollBar
, wxControl
,"wx/scrolbar.h")
67 wxBEGIN_PROPERTIES_TABLE(wxScrollBar
)
68 wxEVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_CHANGED
, wxScrollEvent
)
70 wxPROPERTY( ThumbPosition
, int , SetThumbPosition
, GetThumbPosition
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
71 wxPROPERTY( Range
, int , SetRange
, GetRange
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
72 wxPROPERTY( ThumbSize
, int , SetThumbSize
, GetThumbSize
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
73 wxPROPERTY( PageSize
, int , SetPageSize
, GetPageSize
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
74 wxPROPERTY_FLAGS( WindowStyle
, wxScrollBarStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
75 wxEND_PROPERTIES_TABLE()
77 wxBEGIN_HANDLERS_TABLE(wxScrollBar
)
78 wxEND_HANDLERS_TABLE()
80 wxCONSTRUCTOR_5( wxScrollBar
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
82 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
86 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
88 const wxSize
& size
, long style
,
89 const wxValidator
& validator
,
92 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
95 if (!MSWCreateControl(wxT("ScrollBar"), wxEmptyString
, pos
, size
))
98 SetScrollbar(0, 1, 2, 1, false);
103 wxScrollBar::~wxScrollBar(void)
107 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
108 WXWORD pos
, WXHWND
WXUNUSED(control
))
110 // current and max positions
112 maxPos
, trackPos
= pos
;
114 wxUnusedVar(trackPos
);
116 // when we're dragging the scrollbar we can't use pos parameter because it
117 // is limited to 16 bits
118 // JACS: now always using GetScrollInfo, since there's no reason
120 // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
122 SCROLLINFO scrollInfo
;
123 wxZeroMemory(scrollInfo
);
124 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
126 // also get the range if we call GetScrollInfo() anyhow -- this is less
127 // expensive than call it once here and then call GetScrollRange()
129 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
131 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
133 wxLogLastError(_T("GetScrollInfo"));
136 trackPos
= scrollInfo
.nTrackPos
;
137 position
= scrollInfo
.nPos
;
138 maxPos
= scrollInfo
.nMax
;
143 position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
145 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
149 #if defined(__WIN95__)
150 // A page size greater than one has the effect of reducing the effective
151 // range, therefore the range has already been boosted artificially - so
153 if ( m_pageSize
> 1 )
154 maxPos
-= (m_pageSize
- 1);
157 wxEventType scrollEvent
= wxEVT_NULL
;
163 nScrollInc
= maxPos
- position
;
164 scrollEvent
= wxEVT_SCROLL_TOP
;
168 nScrollInc
= -position
;
169 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
174 scrollEvent
= wxEVT_SCROLL_LINEUP
;
179 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
183 nScrollInc
= -GetPageSize();
184 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
188 nScrollInc
= GetPageSize();
189 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
192 case SB_THUMBPOSITION
:
193 nScrollInc
= trackPos
- position
;
194 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
198 nScrollInc
= trackPos
- position
;
199 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
204 scrollEvent
= wxEVT_SCROLL_CHANGED
;
213 position
+= nScrollInc
;
217 if ( position
> maxPos
)
220 SetThumbPosition(position
);
222 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
223 scrollEvent
!= wxEVT_SCROLL_CHANGED
)
225 // don't process the event if there is no displacement,
226 // unless this is a thumb release or end scroll event.
230 wxScrollEvent
event(scrollEvent
, m_windowId
);
231 event
.SetOrientation(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
232 event
.SetPosition(position
);
233 event
.SetEventObject( this );
235 return GetEventHandler()->ProcessEvent(event
);
238 void wxScrollBar::SetThumbPosition(int viewStart
)
240 #if defined(__WIN95__)
242 info
.cbSize
= sizeof(SCROLLINFO
);
245 info
.nPos
= viewStart
;
246 info
.fMask
= SIF_POS
;
248 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
250 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
254 int wxScrollBar::GetThumbPosition(void) const
256 SCROLLINFO scrollInfo
;
257 wxZeroMemory(scrollInfo
);
258 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
259 scrollInfo
.fMask
= SIF_POS
;
261 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
263 wxLogLastError(_T("GetScrollInfo"));
265 return scrollInfo
.nPos
;
266 // return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
269 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
272 m_viewSize
= pageSize
;
273 m_pageSize
= thumbSize
;
274 m_objectSize
= range
;
276 // The range (number of scroll steps) is the
277 // object length minus the page size.
278 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
280 #if defined(__WIN95__)
281 // Try to adjust the range to cope with page size > 1
282 // (see comment for SetPageLength)
283 if ( m_pageSize
> 1 )
285 range1
+= (m_pageSize
- 1);
289 info
.cbSize
= sizeof(SCROLLINFO
);
290 info
.nPage
= m_pageSize
;
293 info
.nPos
= position
;
295 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
297 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
299 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, refresh
);
300 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, refresh
);
304 void wxScrollBar::Command(wxCommandEvent
& event
)
306 SetThumbPosition(event
.GetInt());
307 ProcessCommand(event
);
310 wxSize
wxScrollBar::DoGetBestSize() const
317 w
= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
321 h
= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
329 WXDWORD
wxScrollBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
331 // we never have an external border
332 WXDWORD msStyle
= wxControl::MSWGetStyle
334 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
337 // SBS_HORZ is 0 anyhow, but do mention it explicitly for clarity
338 msStyle
|= style
& wxSB_HORIZONTAL
? SBS_HORZ
: SBS_VERT
;
343 WXHBRUSH
wxScrollBar::MSWControlColor(WXHDC pDC
, WXHWND hWnd
)
345 // unless we have an explicitly set bg colour, use default (gradient under
346 // XP) brush instead of GetBackgroundColour() one as the base class would
348 // note that fg colour isn't used for a scrollbar
349 return UseBgCol() ? wxControl::MSWControlColor(pDC
, hWnd
) : NULL
;
352 #endif // wxUSE_SCROLLBAR