]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/scrolbar.cpp
Applied patch [ 774837 ] OGL wxLineShape::HitTest: smaller region
[wxWidgets.git] / src / msw / scrolbar.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: msw/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
9// Licence: wxWindows licence
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#if wxUSE_SCROLLBAR
24
25#ifndef WX_PRECOMP
26 #include "wx/utils.h"
27#endif
28
29#include "wx/scrolbar.h"
30#include "wx/msw/private.h"
31
32IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
33
34BEGIN_EVENT_TABLE(wxScrollBar, wxControl)
35#if WXWIN_COMPATIBILITY
36 EVT_SCROLL(wxScrollBar::OnScroll)
37#endif
38END_EVENT_TABLE()
39
40
41// Scrollbar
42bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
43 const wxPoint& pos,
44 const wxSize& size, long style,
45 const wxValidator& validator,
46 const wxString& name)
47{
48 if (!parent)
49 return FALSE;
50 parent->AddChild(this);
51 SetName(name);
52#if wxUSE_VALIDATORS
53 SetValidator(validator);
54#endif // wxUSE_VALIDATORS
55
56 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
57 style |= wxNO_BORDER;
58
59 SetBackgroundColour(parent->GetBackgroundColour()) ;
60 SetForegroundColour(parent->GetForegroundColour()) ;
61 m_windowStyle = style;
62
63 if ( id == -1 )
64 m_windowId = (int)NewControlId();
65 else
66 m_windowId = id;
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
88 WXDWORD exStyle = 0;
89 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
90
91 // Now create scrollbar
92 DWORD _direction = (style & wxHORIZONTAL) ?
93 SBS_HORZ: SBS_VERT;
94 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
95 _direction | wstyle,
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
107 SetFont(parent->GetFont());
108
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
123bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
124 WXWORD pos, WXHWND WXUNUSED(control))
125{
126 // current and max positions
127 int position,
128 maxPos, trackPos = pos;
129
130 // when we're dragging the scrollbar we can't use pos parameter because it
131 // is limited to 16 bits
132 // JACS: now always using GetScrollInfo, since there's no reason
133 // not to
134// if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
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 }
154#if 0
155 else
156 {
157 position = ::GetScrollPos((HWND) control, SB_CTL);
158 int minPos;
159 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
160 }
161#endif
162
163#if defined(__WIN95__)
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__
170
171 wxEventType scrollEvent = wxEVT_NULL;
172
173 int nScrollInc;
174 switch ( wParam )
175 {
176 case SB_BOTTOM:
177 nScrollInc = maxPos - position;
178 scrollEvent = wxEVT_SCROLL_TOP;
179 break;
180
181 case SB_TOP:
182 nScrollInc = -position;
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
206 case SB_THUMBPOSITION:
207 nScrollInc = trackPos - position;
208 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
209 break;
210
211 case SB_THUMBTRACK:
212 nScrollInc = trackPos - position;
213 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
214 break;
215
216 case SB_ENDSCROLL:
217 nScrollInc = 0;
218 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
219 break;
220
221 default:
222 nScrollInc = 0;
223 }
224
225 if ( nScrollInc )
226 {
227 position += nScrollInc;
228
229 if ( position < 0 )
230 position = 0;
231 if ( position > maxPos )
232 position = maxPos;
233
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 }
243
244 wxScrollEvent event(scrollEvent, m_windowId);
245 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
246 event.SetPosition(position);
247 event.SetEventObject( this );
248
249 return GetEventHandler()->ProcessEvent(event);
250}
251
252void wxScrollBar::SetThumbPosition(int viewStart)
253{
254#if defined(__WIN95__)
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);
263#else
264 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
265#endif
266}
267
268int wxScrollBar::GetThumbPosition(void) const
269{
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);
281}
282
283void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
284 bool refresh)
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 {
299 range1 += (m_pageSize - 1);
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:
325
326MaxScrollPos = MaxRangeValue - (PageSize - 1)
327*/
328
329#if WXWIN_COMPATIBILITY
330void wxScrollBar::SetPageSize(int pageLength)
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
344void wxScrollBar::SetObjectLength(int objectLength)
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 {
357 range += (m_pageSize - 1);
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
374void wxScrollBar::SetViewLength(int viewLength)
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
389WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
390 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
391{
392 return 0;
393}
394
395void wxScrollBar::Command(wxCommandEvent& event)
396{
397 SetThumbPosition(event.m_commandInt);
398 ProcessCommand(event);
399}
400
401#if WXWIN_COMPATIBILITY
402// Backward compatibility
403void wxScrollBar::OnScroll(wxScrollEvent& event)
404{
405 wxEventType oldEvent = event.GetEventType();
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
415
416#endif // wxUSE_SCROLLBAR