]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
Use wxBitmapBase in wxCocoa
[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
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 124{
2b5f62a0
VZ
125 // current and max positions
126 int position,
127 maxPos, trackPos = pos;
128
129#ifdef __WIN32__
130 // when we're dragging the scrollbar we can't use pos parameter because it
131 // is limited to 16 bits
132 if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
133 {
134 SCROLLINFO scrollInfo;
135 wxZeroMemory(scrollInfo);
136 scrollInfo.cbSize = sizeof(SCROLLINFO);
137
138 // also get the range if we call GetScrollInfo() anyhow -- this is less
139 // expensive than call it once here and then call GetScrollRange()
140 // below
141 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
142
143 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
144 {
145 wxLogLastError(_T("GetScrollInfo"));
146 }
147
148 trackPos = scrollInfo.nTrackPos;
149 position = scrollInfo.nPos;
150 maxPos = scrollInfo.nMax;
151 }
152 else
153#endif // Win32
154 {
155 position = ::GetScrollPos((HWND) control, SB_CTL);
156 int minPos;
157 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
158 }
a23fd0e1 159
2bda0e17 160#if defined(__WIN95__)
a23fd0e1
VZ
161 // A page size greater than one has the effect of reducing the effective
162 // range, therefore the range has already been boosted artificially - so
163 // reduce it again.
164 if ( m_pageSize > 1 )
165 maxPos -= (m_pageSize - 1);
166#endif // __WIN95__
2bda0e17 167
7798a18e 168 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
169
170 int nScrollInc;
171 switch ( wParam )
172 {
a29ffc34 173 case SB_BOTTOM:
a23fd0e1
VZ
174 nScrollInc = maxPos - position;
175 scrollEvent = wxEVT_SCROLL_TOP;
176 break;
177
a29ffc34
VZ
178 case SB_TOP:
179 nScrollInc = -position;
a23fd0e1
VZ
180 scrollEvent = wxEVT_SCROLL_BOTTOM;
181 break;
182
183 case SB_LINEUP:
184 nScrollInc = -1;
185 scrollEvent = wxEVT_SCROLL_LINEUP;
186 break;
187
188 case SB_LINEDOWN:
189 nScrollInc = 1;
190 scrollEvent = wxEVT_SCROLL_LINEDOWN;
191 break;
192
193 case SB_PAGEUP:
194 nScrollInc = -GetPageSize();
195 scrollEvent = wxEVT_SCROLL_PAGEUP;
196 break;
197
198 case SB_PAGEDOWN:
199 nScrollInc = GetPageSize();
200 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
201 break;
202
a23fd0e1 203 case SB_THUMBPOSITION:
2b5f62a0 204 nScrollInc = trackPos - position;
7d56fb8f
GRG
205 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
206 break;
feda3011
GRG
207
208 case SB_THUMBTRACK:
2b5f62a0 209 nScrollInc = trackPos - position;
a23fd0e1
VZ
210 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
211 break;
212
e8b669d3
VZ
213 case SB_ENDSCROLL:
214 nScrollInc = 0;
215 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
216 break;
217
a23fd0e1
VZ
218 default:
219 nScrollInc = 0;
2bda0e17
KB
220 }
221
e8b669d3 222 if ( nScrollInc )
2bda0e17 223 {
e8b669d3 224 position += nScrollInc;
2bda0e17 225
e8b669d3
VZ
226 if ( position < 0 )
227 position = 0;
228 if ( position > maxPos )
229 position = maxPos;
a23fd0e1 230
e8b669d3
VZ
231 SetThumbPosition(position);
232 }
233 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
234 scrollEvent != wxEVT_SCROLL_ENDSCROLL )
235 {
236 // don't process the event if there is no displacement,
237 // unless this is a thumb release or end scroll event.
238 return FALSE;
239 }
a23fd0e1 240
a23fd0e1 241 wxScrollEvent event(scrollEvent, m_windowId);
27e229f5 242 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e8b669d3 243 event.SetPosition(position);
a23fd0e1
VZ
244 event.SetEventObject( this );
245
246 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
247}
248
4fabb575 249void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17
KB
250{
251#if defined(__WIN95__)
2b5f62a0
VZ
252 SCROLLINFO info;
253 info.cbSize = sizeof(SCROLLINFO);
254 info.nPage = 0;
255 info.nMin = 0;
256 info.nPos = viewStart;
257 info.fMask = SIF_POS ;
258
259 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
2bda0e17 260#else
2b5f62a0 261 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
2bda0e17
KB
262#endif
263}
264
4fabb575 265int wxScrollBar::GetThumbPosition(void) const
2bda0e17
KB
266{
267 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
268}
269
debe6624
JS
270void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
271 bool refresh)
2bda0e17
KB
272{
273 m_viewSize = pageSize;
274 m_pageSize = thumbSize;
275 m_objectSize = range;
276
277 // The range (number of scroll steps) is the
278 // object length minus the page size.
279 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
280
281#if defined(__WIN95__)
282 // Try to adjust the range to cope with page size > 1
283 // (see comment for SetPageLength)
284 if ( m_pageSize > 1 )
285 {
a23fd0e1 286 range1 += (m_pageSize - 1);
2bda0e17
KB
287 }
288
289 SCROLLINFO info;
290 info.cbSize = sizeof(SCROLLINFO);
291 info.nPage = m_pageSize;
292 info.nMin = 0;
293 info.nMax = range1;
294 info.nPos = position;
295
296 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
297
298 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
299#else
300 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
301 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
302#endif
303}
304
305
306/* From the WIN32 documentation:
307In version 4.0 or later, the maximum value that a scroll bar can report
308(that is, the maximum scrolling position) depends on the page size.
309If the scroll bar has a page size greater than one, the maximum scrolling position
310is less than the maximum range value. You can use the following formula to calculate
311the maximum scrolling position:
33ac7e6f
KB
312
313MaxScrollPos = MaxRangeValue - (PageSize - 1)
2bda0e17
KB
314*/
315
316#if WXWIN_COMPATIBILITY
debe6624 317void wxScrollBar::SetPageSize(int pageLength)
2bda0e17
KB
318{
319 m_pageSize = pageLength;
320
321#if defined(__WIN95__)
322 SCROLLINFO info;
323 info.cbSize = sizeof(SCROLLINFO);
324 info.nPage = pageLength;
325 info.fMask = SIF_PAGE ;
326
327 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
328#endif
329}
330
debe6624 331void wxScrollBar::SetObjectLength(int objectLength)
2bda0e17
KB
332{
333 m_objectSize = objectLength;
334
335 // The range (number of scroll steps) is the
336 // object length minus the view size.
337 int range = wxMax((objectLength - m_viewSize), 0) ;
338
339#if defined(__WIN95__)
340 // Try to adjust the range to cope with page size > 1
341 // (see comment for SetPageLength)
342 if ( m_pageSize > 1 )
343 {
a23fd0e1 344 range += (m_pageSize - 1);
2bda0e17
KB
345 }
346
347 SCROLLINFO info;
348 info.cbSize = sizeof(SCROLLINFO);
349 info.nPage = 0;
350 info.nMin = 0;
351 info.nMax = range;
352 info.nPos = 0;
353 info.fMask = SIF_RANGE ;
354
355 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
356#else
357 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range, TRUE);
358#endif
359}
360
debe6624 361void wxScrollBar::SetViewLength(int viewLength)
2bda0e17
KB
362{
363 m_viewSize = viewLength;
364}
365
366void wxScrollBar::GetValues(int *viewStart, int *viewLength, int *objectLength,
367 int *pageLength) const
368{
369 *viewStart = ::GetScrollPos((HWND)m_hWnd, SB_CTL);
370 *viewLength = m_viewSize;
371 *objectLength = m_objectSize;
372 *pageLength = m_pageSize;
373}
374#endif
375
33ac7e6f
KB
376WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
377 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
2bda0e17
KB
378{
379 return 0;
380}
381
382void wxScrollBar::Command(wxCommandEvent& event)
383{
ca5e9f67 384 SetThumbPosition(event.m_commandInt);
2bda0e17
KB
385 ProcessCommand(event);
386}
387
388#if WXWIN_COMPATIBILITY
389// Backward compatibility
390void wxScrollBar::OnScroll(wxScrollEvent& event)
391{
7798a18e 392 wxEventType oldEvent = event.GetEventType();
2bda0e17
KB
393 event.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED );
394 if ( !GetEventHandler()->ProcessEvent(event) )
395 {
396 event.SetEventType( oldEvent );
397 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
398 event.Skip();
399 }
400}
401#endif
1e6feb95
VZ
402
403#endif // wxUSE_SCROLLBAR