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