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