]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
Expanded wxSystemOptions docs, documented wxTE_CAPITALIZE
[wxWidgets.git] / src / msw / scrolbar.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: msw/scrolbar.cpp
2bda0e17
KB
3// Purpose: wxScrollBar
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
1e6feb95 13 #pragma implementation "scrolbar.h"
2bda0e17
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
1e6feb95 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
1e6feb95
VZ
23#if wxUSE_SCROLLBAR
24
2bda0e17 25#ifndef WX_PRECOMP
1e6feb95 26 #include "wx/utils.h"
2bda0e17
KB
27#endif
28
29#include "wx/scrolbar.h"
30#include "wx/msw/private.h"
98773c1c 31#include "wx/settings.h"
2bda0e17 32
f0a126fe 33#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
34WX_DEFINE_FLAGS( wxScrollBarStyle )
35
3ff066a4 36wxBEGIN_FLAGS( wxScrollBarStyle )
bc9fb572
JS
37 // new style border flags, we put them first to
38 // use them for streaming out
3ff066a4
SC
39 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
40 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
41 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
42 wxFLAGS_MEMBER(wxBORDER_RAISED)
43 wxFLAGS_MEMBER(wxBORDER_STATIC)
44 wxFLAGS_MEMBER(wxBORDER_NONE)
57f4f925 45
bc9fb572 46 // old style border flags
3ff066a4
SC
47 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
48 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
49 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
50 wxFLAGS_MEMBER(wxRAISED_BORDER)
51 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 52 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
53
54 // standard window styles
3ff066a4
SC
55 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
56 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
57 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
58 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 59 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
60 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
61 wxFLAGS_MEMBER(wxVSCROLL)
62 wxFLAGS_MEMBER(wxHSCROLL)
bc9fb572 63
3ff066a4
SC
64 wxFLAGS_MEMBER(wxSB_HORIZONTAL)
65 wxFLAGS_MEMBER(wxSB_VERTICAL)
bc9fb572 66
3ff066a4 67wxEND_FLAGS( wxScrollBarStyle )
bc9fb572 68
f0a126fe
SC
69IMPLEMENT_DYNAMIC_CLASS_XTI(wxScrollBar, wxControl,"wx/scrolbar.h")
70
3ff066a4
SC
71wxBEGIN_PROPERTIES_TABLE(wxScrollBar)
72 wxEVENT_RANGE_PROPERTY( Scroll , wxEVT_SCROLL_TOP , wxEVT_SCROLL_ENDSCROLL , wxScrollEvent )
c5ca409b 73
3ff066a4 74 wxPROPERTY( ThumbPosition , int , SetThumbPosition, GetThumbPosition, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
57f4f925
WS
75 wxPROPERTY( Range , int , SetRange, GetRange, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
76 wxPROPERTY( ThumbSize , int , SetThumbSize, GetThumbSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 wxPROPERTY( PageSize , int , SetPageSize, GetPageSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
af498247 78 wxPROPERTY_FLAGS( WindowStyle , wxScrollBarStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 79wxEND_PROPERTIES_TABLE()
2bda0e17 80
3ff066a4
SC
81wxBEGIN_HANDLERS_TABLE(wxScrollBar)
82wxEND_HANDLERS_TABLE()
066f1b7a 83
3ff066a4 84wxCONSTRUCTOR_5( wxScrollBar , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
f0a126fe
SC
85#else
86IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
87#endif
2bda0e17
KB
88
89// Scrollbar
debe6624 90bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
2bda0e17 91 const wxPoint& pos,
debe6624 92 const wxSize& size, long style,
720afa24 93 const wxValidator& validator,
2bda0e17
KB
94 const wxString& name)
95{
720afa24 96 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
57f4f925 97 return false;
33ac7e6f 98
720afa24
DS
99 if (!MSWCreateControl(wxT("ScrollBar"), wxEmptyString, pos, size))
100 return false;
2bda0e17 101
720afa24 102 SetScrollbar(0, 1, 2, 1, false);
2bda0e17 103
57f4f925 104 return true;
2bda0e17
KB
105}
106
107wxScrollBar::~wxScrollBar(void)
108{
109}
110
a23fd0e1 111bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
2eb10e2a 112 WXWORD pos, WXHWND WXUNUSED(control))
2bda0e17 113{
2b5f62a0
VZ
114 // current and max positions
115 int position,
116 maxPos, trackPos = pos;
117
5cb598ae
WS
118 wxUnusedVar(trackPos);
119
2b5f62a0
VZ
120 // when we're dragging the scrollbar we can't use pos parameter because it
121 // is limited to 16 bits
4676948b
JS
122 // JACS: now always using GetScrollInfo, since there's no reason
123 // not to
124// if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
2b5f62a0
VZ
125 {
126 SCROLLINFO scrollInfo;
127 wxZeroMemory(scrollInfo);
128 scrollInfo.cbSize = sizeof(SCROLLINFO);
129
130 // also get the range if we call GetScrollInfo() anyhow -- this is less
131 // expensive than call it once here and then call GetScrollRange()
132 // below
133 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
134
135 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
136 {
137 wxLogLastError(_T("GetScrollInfo"));
138 }
139
140 trackPos = scrollInfo.nTrackPos;
141 position = scrollInfo.nPos;
142 maxPos = scrollInfo.nMax;
143 }
4676948b 144#if 0
2b5f62a0 145 else
2b5f62a0
VZ
146 {
147 position = ::GetScrollPos((HWND) control, SB_CTL);
148 int minPos;
149 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
150 }
4676948b 151#endif
a23fd0e1 152
2bda0e17 153#if defined(__WIN95__)
a23fd0e1
VZ
154 // A page size greater than one has the effect of reducing the effective
155 // range, therefore the range has already been boosted artificially - so
156 // reduce it again.
157 if ( m_pageSize > 1 )
158 maxPos -= (m_pageSize - 1);
159#endif // __WIN95__
2bda0e17 160
7798a18e 161 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
162
163 int nScrollInc;
164 switch ( wParam )
165 {
a29ffc34 166 case SB_BOTTOM:
a23fd0e1
VZ
167 nScrollInc = maxPos - position;
168 scrollEvent = wxEVT_SCROLL_TOP;
169 break;
170
a29ffc34
VZ
171 case SB_TOP:
172 nScrollInc = -position;
a23fd0e1
VZ
173 scrollEvent = wxEVT_SCROLL_BOTTOM;
174 break;
175
176 case SB_LINEUP:
177 nScrollInc = -1;
178 scrollEvent = wxEVT_SCROLL_LINEUP;
179 break;
180
181 case SB_LINEDOWN:
182 nScrollInc = 1;
183 scrollEvent = wxEVT_SCROLL_LINEDOWN;
184 break;
185
186 case SB_PAGEUP:
187 nScrollInc = -GetPageSize();
188 scrollEvent = wxEVT_SCROLL_PAGEUP;
189 break;
190
191 case SB_PAGEDOWN:
192 nScrollInc = GetPageSize();
193 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
194 break;
195
a23fd0e1 196 case SB_THUMBPOSITION:
2b5f62a0 197 nScrollInc = trackPos - position;
7d56fb8f
GRG
198 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
199 break;
feda3011
GRG
200
201 case SB_THUMBTRACK:
2b5f62a0 202 nScrollInc = trackPos - position;
a23fd0e1
VZ
203 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
204 break;
205
e8b669d3
VZ
206 case SB_ENDSCROLL:
207 nScrollInc = 0;
208 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
209 break;
210
a23fd0e1
VZ
211 default:
212 nScrollInc = 0;
2bda0e17
KB
213 }
214
e8b669d3 215 if ( nScrollInc )
2bda0e17 216 {
e8b669d3 217 position += nScrollInc;
2bda0e17 218
e8b669d3
VZ
219 if ( position < 0 )
220 position = 0;
221 if ( position > maxPos )
222 position = maxPos;
a23fd0e1 223
e8b669d3
VZ
224 SetThumbPosition(position);
225 }
226 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
227 scrollEvent != wxEVT_SCROLL_ENDSCROLL )
228 {
229 // don't process the event if there is no displacement,
230 // unless this is a thumb release or end scroll event.
57f4f925 231 return false;
e8b669d3 232 }
a23fd0e1 233
a23fd0e1 234 wxScrollEvent event(scrollEvent, m_windowId);
27e229f5 235 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e8b669d3 236 event.SetPosition(position);
a23fd0e1
VZ
237 event.SetEventObject( this );
238
239 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
240}
241
4fabb575 242void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17
KB
243{
244#if defined(__WIN95__)
2b5f62a0
VZ
245 SCROLLINFO info;
246 info.cbSize = sizeof(SCROLLINFO);
247 info.nPage = 0;
248 info.nMin = 0;
249 info.nPos = viewStart;
250 info.fMask = SIF_POS ;
251
252 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
2bda0e17 253#else
2b5f62a0 254 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
2bda0e17
KB
255#endif
256}
257
4fabb575 258int wxScrollBar::GetThumbPosition(void) const
2bda0e17 259{
4676948b
JS
260 SCROLLINFO scrollInfo;
261 wxZeroMemory(scrollInfo);
262 scrollInfo.cbSize = sizeof(SCROLLINFO);
263 scrollInfo.fMask = SIF_POS;
f0a126fe 264
4676948b
JS
265 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
266 {
267 wxLogLastError(_T("GetScrollInfo"));
268 }
269 return scrollInfo.nPos;
270// return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
2bda0e17
KB
271}
272
debe6624
JS
273void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
274 bool refresh)
2bda0e17
KB
275{
276 m_viewSize = pageSize;
277 m_pageSize = thumbSize;
278 m_objectSize = range;
279
280 // The range (number of scroll steps) is the
281 // object length minus the page size.
282 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
283
284#if defined(__WIN95__)
285 // Try to adjust the range to cope with page size > 1
286 // (see comment for SetPageLength)
287 if ( m_pageSize > 1 )
288 {
a23fd0e1 289 range1 += (m_pageSize - 1);
2bda0e17
KB
290 }
291
292 SCROLLINFO info;
293 info.cbSize = sizeof(SCROLLINFO);
294 info.nPage = m_pageSize;
295 info.nMin = 0;
296 info.nMax = range1;
297 info.nPos = position;
298
299 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
300
301 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
302#else
720afa24
DS
303 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, refresh);
304 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, refresh);
2bda0e17
KB
305#endif
306}
307
2bda0e17
KB
308void wxScrollBar::Command(wxCommandEvent& event)
309{
687706f5 310 SetThumbPosition(event.GetInt());
2bda0e17
KB
311 ProcessCommand(event);
312}
313
caafd082
DS
314wxSize wxScrollBar::DoGetBestSize() const
315{
316 int w = 100;
317 int h = 100;
318
319 if ( IsVertical() )
320 {
321 w = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
322 }
323 else
324 {
325 h = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
326 }
327
328 return wxSize(w, h);
329}
330
720afa24
DS
331WXDWORD wxScrollBar::MSWGetStyle(long style, WXDWORD *exstyle) const
332{
333 // we never have an external border
334 WXDWORD msStyle = wxControl::MSWGetStyle
335 (
336 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
337 );
338
339 // SBS_HORZ is 0 anyhow, but do mention it explicitly for clarity
340 msStyle |= style & wxSB_HORIZONTAL ? SBS_HORZ : SBS_VERT;
341
342 return msStyle;
343}
344
1e6feb95 345#endif // wxUSE_SCROLLBAR