]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
avoiding a OS-Level set focus for a control that already has the focus
[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)
bc9fb572
JS
45
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
SC
74 wxPROPERTY( ThumbPosition , int , SetThumbPosition, GetThumbPosition, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
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,
ac8d0c11 93 const wxValidator& wxVALIDATOR_PARAM(validator),
2bda0e17
KB
94 const wxString& name)
95{
96 if (!parent)
97 return FALSE;
98 parent->AddChild(this);
99 SetName(name);
11b6a93b 100#if wxUSE_VALIDATORS
a23fd0e1 101 SetValidator(validator);
11b6a93b 102#endif // wxUSE_VALIDATORS
33ac7e6f 103
7051e6c7
JS
104 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
105 style |= wxNO_BORDER;
106
fd71308f
JS
107 SetBackgroundColour(parent->GetBackgroundColour()) ;
108 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
109 m_windowStyle = style;
110
111 if ( id == -1 )
a23fd0e1 112 m_windowId = (int)NewControlId();
2bda0e17 113 else
a23fd0e1 114 m_windowId = id;
2bda0e17
KB
115
116 int x = pos.x;
117 int y = pos.y;
118 int width = size.x;
119 int height = size.y;
120
121 if (width == -1)
122 {
123 if (style & wxHORIZONTAL)
124 width = 140;
125 else
126 width = 14;
127 }
128 if (height == -1)
129 {
130 if (style & wxVERTICAL)
131 height = 140;
132 else
133 height = 14;
134 }
135
fe3d9123
JS
136 WXDWORD exStyle = 0;
137 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
b0766406 138
2bda0e17
KB
139 // Now create scrollbar
140 DWORD _direction = (style & wxHORIZONTAL) ?
141 SBS_HORZ: SBS_VERT;
fe3d9123 142 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
b0766406 143 _direction | wstyle,
2bda0e17
KB
144 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
145 wxGetInstance(), NULL);
146
147 m_pageSize = 1;
148 m_viewSize = 1;
149 m_objectSize = 1;
150
151 ::SetScrollRange(scroll_bar, SB_CTL, 0, 1, FALSE);
152 ::SetScrollPos(scroll_bar, SB_CTL, 0, FALSE);
153 ShowWindow(scroll_bar, SW_SHOW);
154
155 m_hWnd = (WXHWND)scroll_bar;
156
157 // Subclass again for purposes of dialog editing mode
158 SubclassWin((WXHWND) scroll_bar);
159
160 SetSize(x, y, width, height);
161
162 return TRUE;
163}
164
165wxScrollBar::~wxScrollBar(void)
166{
167}
168
a23fd0e1 169bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
2eb10e2a 170 WXWORD pos, WXHWND WXUNUSED(control))
2bda0e17 171{
2b5f62a0
VZ
172 // current and max positions
173 int position,
174 maxPos, trackPos = pos;
175
5cb598ae
WS
176 wxUnusedVar(trackPos);
177
2b5f62a0
VZ
178 // when we're dragging the scrollbar we can't use pos parameter because it
179 // is limited to 16 bits
4676948b
JS
180 // JACS: now always using GetScrollInfo, since there's no reason
181 // not to
182// if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
2b5f62a0
VZ
183 {
184 SCROLLINFO scrollInfo;
185 wxZeroMemory(scrollInfo);
186 scrollInfo.cbSize = sizeof(SCROLLINFO);
187
188 // also get the range if we call GetScrollInfo() anyhow -- this is less
189 // expensive than call it once here and then call GetScrollRange()
190 // below
191 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
192
193 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
194 {
195 wxLogLastError(_T("GetScrollInfo"));
196 }
197
198 trackPos = scrollInfo.nTrackPos;
199 position = scrollInfo.nPos;
200 maxPos = scrollInfo.nMax;
201 }
4676948b 202#if 0
2b5f62a0 203 else
2b5f62a0
VZ
204 {
205 position = ::GetScrollPos((HWND) control, SB_CTL);
206 int minPos;
207 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
208 }
4676948b 209#endif
a23fd0e1 210
2bda0e17 211#if defined(__WIN95__)
a23fd0e1
VZ
212 // A page size greater than one has the effect of reducing the effective
213 // range, therefore the range has already been boosted artificially - so
214 // reduce it again.
215 if ( m_pageSize > 1 )
216 maxPos -= (m_pageSize - 1);
217#endif // __WIN95__
2bda0e17 218
7798a18e 219 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
220
221 int nScrollInc;
222 switch ( wParam )
223 {
a29ffc34 224 case SB_BOTTOM:
a23fd0e1
VZ
225 nScrollInc = maxPos - position;
226 scrollEvent = wxEVT_SCROLL_TOP;
227 break;
228
a29ffc34
VZ
229 case SB_TOP:
230 nScrollInc = -position;
a23fd0e1
VZ
231 scrollEvent = wxEVT_SCROLL_BOTTOM;
232 break;
233
234 case SB_LINEUP:
235 nScrollInc = -1;
236 scrollEvent = wxEVT_SCROLL_LINEUP;
237 break;
238
239 case SB_LINEDOWN:
240 nScrollInc = 1;
241 scrollEvent = wxEVT_SCROLL_LINEDOWN;
242 break;
243
244 case SB_PAGEUP:
245 nScrollInc = -GetPageSize();
246 scrollEvent = wxEVT_SCROLL_PAGEUP;
247 break;
248
249 case SB_PAGEDOWN:
250 nScrollInc = GetPageSize();
251 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
252 break;
253
a23fd0e1 254 case SB_THUMBPOSITION:
2b5f62a0 255 nScrollInc = trackPos - position;
7d56fb8f
GRG
256 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
257 break;
feda3011
GRG
258
259 case SB_THUMBTRACK:
2b5f62a0 260 nScrollInc = trackPos - position;
a23fd0e1
VZ
261 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
262 break;
263
e8b669d3
VZ
264 case SB_ENDSCROLL:
265 nScrollInc = 0;
266 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
267 break;
268
a23fd0e1
VZ
269 default:
270 nScrollInc = 0;
2bda0e17
KB
271 }
272
e8b669d3 273 if ( nScrollInc )
2bda0e17 274 {
e8b669d3 275 position += nScrollInc;
2bda0e17 276
e8b669d3
VZ
277 if ( position < 0 )
278 position = 0;
279 if ( position > maxPos )
280 position = maxPos;
a23fd0e1 281
e8b669d3
VZ
282 SetThumbPosition(position);
283 }
284 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
285 scrollEvent != wxEVT_SCROLL_ENDSCROLL )
286 {
287 // don't process the event if there is no displacement,
288 // unless this is a thumb release or end scroll event.
289 return FALSE;
290 }
a23fd0e1 291
a23fd0e1 292 wxScrollEvent event(scrollEvent, m_windowId);
27e229f5 293 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e8b669d3 294 event.SetPosition(position);
a23fd0e1
VZ
295 event.SetEventObject( this );
296
297 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
298}
299
4fabb575 300void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17
KB
301{
302#if defined(__WIN95__)
2b5f62a0
VZ
303 SCROLLINFO info;
304 info.cbSize = sizeof(SCROLLINFO);
305 info.nPage = 0;
306 info.nMin = 0;
307 info.nPos = viewStart;
308 info.fMask = SIF_POS ;
309
310 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
2bda0e17 311#else
2b5f62a0 312 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
2bda0e17
KB
313#endif
314}
315
4fabb575 316int wxScrollBar::GetThumbPosition(void) const
2bda0e17 317{
4676948b
JS
318 SCROLLINFO scrollInfo;
319 wxZeroMemory(scrollInfo);
320 scrollInfo.cbSize = sizeof(SCROLLINFO);
321 scrollInfo.fMask = SIF_POS;
f0a126fe 322
4676948b
JS
323 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
324 {
325 wxLogLastError(_T("GetScrollInfo"));
326 }
327 return scrollInfo.nPos;
328// return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
2bda0e17
KB
329}
330
debe6624
JS
331void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
332 bool refresh)
2bda0e17
KB
333{
334 m_viewSize = pageSize;
335 m_pageSize = thumbSize;
336 m_objectSize = range;
337
338 // The range (number of scroll steps) is the
339 // object length minus the page size.
340 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
341
342#if defined(__WIN95__)
343 // Try to adjust the range to cope with page size > 1
344 // (see comment for SetPageLength)
345 if ( m_pageSize > 1 )
346 {
a23fd0e1 347 range1 += (m_pageSize - 1);
2bda0e17
KB
348 }
349
350 SCROLLINFO info;
351 info.cbSize = sizeof(SCROLLINFO);
352 info.nPage = m_pageSize;
353 info.nMin = 0;
354 info.nMax = range1;
355 info.nPos = position;
356
357 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
358
359 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
360#else
361 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
362 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
363#endif
364}
365
366
33ac7e6f
KB
367WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
368 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
2bda0e17
KB
369{
370 return 0;
371}
372
373void wxScrollBar::Command(wxCommandEvent& event)
374{
ca5e9f67 375 SetThumbPosition(event.m_commandInt);
2bda0e17
KB
376 ProcessCommand(event);
377}
378
caafd082
DS
379wxSize wxScrollBar::DoGetBestSize() const
380{
381 int w = 100;
382 int h = 100;
383
384 if ( IsVertical() )
385 {
386 w = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
387 }
388 else
389 {
390 h = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
391 }
392
393 return wxSize(w, h);
394}
395
1e6feb95 396#endif // wxUSE_SCROLLBAR