]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
Applied patch [ 549256 ] fix minor bug in widgets sample
[wxWidgets.git] / src / msw / scrolbar.cpp
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 and Markus Holzem
9 // Licence: wxWindows license
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
32 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
33
34 BEGIN_EVENT_TABLE(wxScrollBar, wxControl)
35 #if WXWIN_COMPATIBILITY
36 EVT_SCROLL(wxScrollBar::OnScroll)
37 #endif
38 END_EVENT_TABLE()
39
40
41 // Scrollbar
42 bool 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 SetBackgroundColour(parent->GetBackgroundColour()) ;
57 SetForegroundColour(parent->GetForegroundColour()) ;
58 m_windowStyle = style;
59
60 if ( id == -1 )
61 m_windowId = (int)NewControlId();
62 else
63 m_windowId = id;
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
85 DWORD wstyle = WS_VISIBLE | WS_CHILD;
86
87 if ( m_windowStyle & wxCLIP_SIBLINGS )
88 wstyle |= WS_CLIPSIBLINGS;
89
90 // Now create scrollbar
91 DWORD _direction = (style & wxHORIZONTAL) ?
92 SBS_HORZ: SBS_VERT;
93 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(style), wxT("SCROLLBAR"), wxT("scrollbar"),
94 _direction | wstyle,
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
106 SetFont(parent->GetFont());
107
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
118 wxScrollBar::~wxScrollBar(void)
119 {
120 }
121
122 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
123 WXWORD pos, WXHWND control)
124 {
125 int position = ::GetScrollPos((HWND) control, SB_CTL);
126 int minPos, maxPos;
127 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
128
129 #if defined(__WIN95__)
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__
136
137 wxEventType scrollEvent = wxEVT_NULL;
138
139 int nScrollInc;
140 switch ( wParam )
141 {
142 case SB_BOTTOM:
143 nScrollInc = maxPos - position;
144 scrollEvent = wxEVT_SCROLL_TOP;
145 break;
146
147 case SB_TOP:
148 nScrollInc = -position;
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
172 case SB_THUMBPOSITION:
173 nScrollInc = pos - position;
174 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
175 break;
176
177 case SB_THUMBTRACK:
178 nScrollInc = pos - position;
179 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
180 break;
181
182 case SB_ENDSCROLL:
183 nScrollInc = 0;
184 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
185 break;
186
187 default:
188 nScrollInc = 0;
189 }
190
191 if ( nScrollInc )
192 {
193 position += nScrollInc;
194
195 if ( position < 0 )
196 position = 0;
197 if ( position > maxPos )
198 position = maxPos;
199
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 }
209
210 wxScrollEvent event(scrollEvent, m_windowId);
211 event.SetPosition(position);
212 event.SetEventObject( this );
213
214 return GetEventHandler()->ProcessEvent(event);
215 }
216
217 void wxScrollBar::SetThumbPosition(int viewStart)
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
233 int wxScrollBar::GetThumbPosition(void) const
234 {
235 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
236 }
237
238 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
239 bool refresh)
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 {
254 range1 += (m_pageSize - 1);
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:
275 In 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.
277 If the scroll bar has a page size greater than one, the maximum scrolling position
278 is less than the maximum range value. You can use the following formula to calculate
279 the maximum scrolling position:
280
281 MaxScrollPos = MaxRangeValue - (PageSize - 1)
282 */
283
284 #if WXWIN_COMPATIBILITY
285 void wxScrollBar::SetPageSize(int pageLength)
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
299 void wxScrollBar::SetObjectLength(int objectLength)
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 {
312 range += (m_pageSize - 1);
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
329 void wxScrollBar::SetViewLength(int viewLength)
330 {
331 m_viewSize = viewLength;
332 }
333
334 void 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
344 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
345 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
346 {
347 return 0;
348 }
349
350 void wxScrollBar::Command(wxCommandEvent& event)
351 {
352 SetThumbPosition(event.m_commandInt);
353 ProcessCommand(event);
354 }
355
356 #if WXWIN_COMPATIBILITY
357 // Backward compatibility
358 void wxScrollBar::OnScroll(wxScrollEvent& event)
359 {
360 wxEventType oldEvent = event.GetEventType();
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
370
371 #endif // wxUSE_SCROLLBAR