]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
generate key events for Space/Enter in addition to the activate events, as wxMSW...
[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$
8// Copyright: (c) Julian Smart and Markus Holzem
a23fd0e1 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
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"
31
2bda0e17
KB
32IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
33
2bda0e17 34BEGIN_EVENT_TABLE(wxScrollBar, wxControl)
ca5e9f67 35#if WXWIN_COMPATIBILITY
2bda0e17 36 EVT_SCROLL(wxScrollBar::OnScroll)
2bda0e17 37#endif
ca5e9f67 38END_EVENT_TABLE()
2bda0e17 39
2bda0e17
KB
40
41// Scrollbar
debe6624 42bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
2bda0e17 43 const wxPoint& pos,
debe6624 44 const wxSize& size, long style,
2bda0e17
KB
45 const wxValidator& validator,
46 const wxString& name)
47{
48 if (!parent)
49 return FALSE;
50 parent->AddChild(this);
51 SetName(name);
11b6a93b 52#if wxUSE_VALIDATORS
a23fd0e1 53 SetValidator(validator);
11b6a93b 54#endif // wxUSE_VALIDATORS
33ac7e6f 55
fd71308f
JS
56 SetBackgroundColour(parent->GetBackgroundColour()) ;
57 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
58 m_windowStyle = style;
59
60 if ( id == -1 )
a23fd0e1 61 m_windowId = (int)NewControlId();
2bda0e17 62 else
a23fd0e1 63 m_windowId = id;
2bda0e17
KB
64
65 int x = pos.x;
66 int y = pos.y;
67 int width = size.x;
68 int height = size.y;
69
70 if (width == -1)
71 {
72 if (style & wxHORIZONTAL)
73 width = 140;
74 else
75 width = 14;
76 }
77 if (height == -1)
78 {
79 if (style & wxVERTICAL)
80 height = 140;
81 else
82 height = 14;
83 }
84
b0766406
JS
85 DWORD wstyle = WS_VISIBLE | WS_CHILD;
86
87 if ( m_windowStyle & wxCLIP_SIBLINGS )
88 wstyle |= WS_CLIPSIBLINGS;
89
2bda0e17
KB
90 // Now create scrollbar
91 DWORD _direction = (style & wxHORIZONTAL) ?
92 SBS_HORZ: SBS_VERT;
223d09f6 93 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(style), wxT("SCROLLBAR"), wxT("scrollbar"),
b0766406 94 _direction | wstyle,
2bda0e17
KB
95 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
96 wxGetInstance(), NULL);
97
98 m_pageSize = 1;
99 m_viewSize = 1;
100 m_objectSize = 1;
101
102 ::SetScrollRange(scroll_bar, SB_CTL, 0, 1, FALSE);
103 ::SetScrollPos(scroll_bar, SB_CTL, 0, FALSE);
104 ShowWindow(scroll_bar, SW_SHOW);
105
c0ed460c 106 SetFont(parent->GetFont());
1c089c47 107
2bda0e17
KB
108 m_hWnd = (WXHWND)scroll_bar;
109
110 // Subclass again for purposes of dialog editing mode
111 SubclassWin((WXHWND) scroll_bar);
112
113 SetSize(x, y, width, height);
114
115 return TRUE;
116}
117
118wxScrollBar::~wxScrollBar(void)
119{
120}
121
a23fd0e1
VZ
122bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
123 WXWORD pos, WXHWND control)
2bda0e17
KB
124{
125 int position = ::GetScrollPos((HWND) control, SB_CTL);
126 int minPos, maxPos;
127 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
a23fd0e1 128
2bda0e17 129#if defined(__WIN95__)
a23fd0e1
VZ
130 // A page size greater than one has the effect of reducing the effective
131 // range, therefore the range has already been boosted artificially - so
132 // reduce it again.
133 if ( m_pageSize > 1 )
134 maxPos -= (m_pageSize - 1);
135#endif // __WIN95__
2bda0e17 136
7798a18e 137 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
138
139 int nScrollInc;
140 switch ( wParam )
141 {
a29ffc34 142 case SB_BOTTOM:
a23fd0e1
VZ
143 nScrollInc = maxPos - position;
144 scrollEvent = wxEVT_SCROLL_TOP;
145 break;
146
a29ffc34
VZ
147 case SB_TOP:
148 nScrollInc = -position;
a23fd0e1
VZ
149 scrollEvent = wxEVT_SCROLL_BOTTOM;
150 break;
151
152 case SB_LINEUP:
153 nScrollInc = -1;
154 scrollEvent = wxEVT_SCROLL_LINEUP;
155 break;
156
157 case SB_LINEDOWN:
158 nScrollInc = 1;
159 scrollEvent = wxEVT_SCROLL_LINEDOWN;
160 break;
161
162 case SB_PAGEUP:
163 nScrollInc = -GetPageSize();
164 scrollEvent = wxEVT_SCROLL_PAGEUP;
165 break;
166
167 case SB_PAGEDOWN:
168 nScrollInc = GetPageSize();
169 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
170 break;
171
a23fd0e1 172 case SB_THUMBPOSITION:
7d56fb8f
GRG
173 nScrollInc = pos - position;
174 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
175 break;
feda3011
GRG
176
177 case SB_THUMBTRACK:
a23fd0e1
VZ
178 nScrollInc = pos - position;
179 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
180 break;
181
e8b669d3
VZ
182 case SB_ENDSCROLL:
183 nScrollInc = 0;
184 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
185 break;
186
a23fd0e1
VZ
187 default:
188 nScrollInc = 0;
2bda0e17
KB
189 }
190
e8b669d3 191 if ( nScrollInc )
2bda0e17 192 {
e8b669d3 193 position += nScrollInc;
2bda0e17 194
e8b669d3
VZ
195 if ( position < 0 )
196 position = 0;
197 if ( position > maxPos )
198 position = maxPos;
a23fd0e1 199
e8b669d3
VZ
200 SetThumbPosition(position);
201 }
202 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
203 scrollEvent != wxEVT_SCROLL_ENDSCROLL )
204 {
205 // don't process the event if there is no displacement,
206 // unless this is a thumb release or end scroll event.
207 return FALSE;
208 }
a23fd0e1 209
a23fd0e1 210 wxScrollEvent event(scrollEvent, m_windowId);
e8b669d3 211 event.SetPosition(position);
a23fd0e1
VZ
212 event.SetEventObject( this );
213
214 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
215}
216
4fabb575 217void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17
KB
218{
219#if defined(__WIN95__)
220 SCROLLINFO info;
221 info.cbSize = sizeof(SCROLLINFO);
222 info.nPage = 0;
223 info.nMin = 0;
224 info.nPos = viewStart;
225 info.fMask = SIF_POS ;
226
227 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
228#else
229 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
230#endif
231}
232
4fabb575 233int wxScrollBar::GetThumbPosition(void) const
2bda0e17
KB
234{
235 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
236}
237
debe6624
JS
238void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
239 bool refresh)
2bda0e17
KB
240{
241 m_viewSize = pageSize;
242 m_pageSize = thumbSize;
243 m_objectSize = range;
244
245 // The range (number of scroll steps) is the
246 // object length minus the page size.
247 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
248
249#if defined(__WIN95__)
250 // Try to adjust the range to cope with page size > 1
251 // (see comment for SetPageLength)
252 if ( m_pageSize > 1 )
253 {
a23fd0e1 254 range1 += (m_pageSize - 1);
2bda0e17
KB
255 }
256
257 SCROLLINFO info;
258 info.cbSize = sizeof(SCROLLINFO);
259 info.nPage = m_pageSize;
260 info.nMin = 0;
261 info.nMax = range1;
262 info.nPos = position;
263
264 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
265
266 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
267#else
268 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
269 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
270#endif
271}
272
273
274/* From the WIN32 documentation:
275In version 4.0 or later, the maximum value that a scroll bar can report
276(that is, the maximum scrolling position) depends on the page size.
277If the scroll bar has a page size greater than one, the maximum scrolling position
278is less than the maximum range value. You can use the following formula to calculate
279the maximum scrolling position:
33ac7e6f
KB
280
281MaxScrollPos = MaxRangeValue - (PageSize - 1)
2bda0e17
KB
282*/
283
284#if WXWIN_COMPATIBILITY
debe6624 285void wxScrollBar::SetPageSize(int pageLength)
2bda0e17
KB
286{
287 m_pageSize = pageLength;
288
289#if defined(__WIN95__)
290 SCROLLINFO info;
291 info.cbSize = sizeof(SCROLLINFO);
292 info.nPage = pageLength;
293 info.fMask = SIF_PAGE ;
294
295 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
296#endif
297}
298
debe6624 299void wxScrollBar::SetObjectLength(int objectLength)
2bda0e17
KB
300{
301 m_objectSize = objectLength;
302
303 // The range (number of scroll steps) is the
304 // object length minus the view size.
305 int range = wxMax((objectLength - m_viewSize), 0) ;
306
307#if defined(__WIN95__)
308 // Try to adjust the range to cope with page size > 1
309 // (see comment for SetPageLength)
310 if ( m_pageSize > 1 )
311 {
a23fd0e1 312 range += (m_pageSize - 1);
2bda0e17
KB
313 }
314
315 SCROLLINFO info;
316 info.cbSize = sizeof(SCROLLINFO);
317 info.nPage = 0;
318 info.nMin = 0;
319 info.nMax = range;
320 info.nPos = 0;
321 info.fMask = SIF_RANGE ;
322
323 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
324#else
325 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range, TRUE);
326#endif
327}
328
debe6624 329void wxScrollBar::SetViewLength(int viewLength)
2bda0e17
KB
330{
331 m_viewSize = viewLength;
332}
333
334void wxScrollBar::GetValues(int *viewStart, int *viewLength, int *objectLength,
335 int *pageLength) const
336{
337 *viewStart = ::GetScrollPos((HWND)m_hWnd, SB_CTL);
338 *viewLength = m_viewSize;
339 *objectLength = m_objectSize;
340 *pageLength = m_pageSize;
341}
342#endif
343
33ac7e6f
KB
344WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
345 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
2bda0e17
KB
346{
347 return 0;
348}
349
350void wxScrollBar::Command(wxCommandEvent& event)
351{
ca5e9f67 352 SetThumbPosition(event.m_commandInt);
2bda0e17
KB
353 ProcessCommand(event);
354}
355
356#if WXWIN_COMPATIBILITY
357// Backward compatibility
358void wxScrollBar::OnScroll(wxScrollEvent& event)
359{
7798a18e 360 wxEventType oldEvent = event.GetEventType();
2bda0e17
KB
361 event.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED );
362 if ( !GetEventHandler()->ProcessEvent(event) )
363 {
364 event.SetEventType( oldEvent );
365 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
366 event.Skip();
367 }
368}
369#endif
1e6feb95
VZ
370
371#endif // wxUSE_SCROLLBAR