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