]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
Revamped border handling.
[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
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
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 WXDWORD exStyle = 0;
86 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
87
88 /*
89 if ( m_windowStyle & wxCLIP_SIBLINGS )
90 wstyle |= WS_CLIPSIBLINGS;
91 */
92
93 // Now create scrollbar
94 DWORD _direction = (style & wxHORIZONTAL) ?
95 SBS_HORZ: SBS_VERT;
96 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
97 _direction | wstyle,
98 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
99 wxGetInstance(), NULL);
100
101 m_pageSize = 1;
102 m_viewSize = 1;
103 m_objectSize = 1;
104
105 ::SetScrollRange(scroll_bar, SB_CTL, 0, 1, FALSE);
106 ::SetScrollPos(scroll_bar, SB_CTL, 0, FALSE);
107 ShowWindow(scroll_bar, SW_SHOW);
108
109 SetFont(parent->GetFont());
110
111 m_hWnd = (WXHWND)scroll_bar;
112
113 // Subclass again for purposes of dialog editing mode
114 SubclassWin((WXHWND) scroll_bar);
115
116 SetSize(x, y, width, height);
117
118 return TRUE;
119 }
120
121 wxScrollBar::~wxScrollBar(void)
122 {
123 }
124
125 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
126 WXWORD pos, WXHWND control)
127 {
128 // current and max positions
129 int position,
130 maxPos, trackPos = pos;
131
132 #ifdef __WIN32__
133 // when we're dragging the scrollbar we can't use pos parameter because it
134 // is limited to 16 bits
135 if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
136 {
137 SCROLLINFO scrollInfo;
138 wxZeroMemory(scrollInfo);
139 scrollInfo.cbSize = sizeof(SCROLLINFO);
140
141 // also get the range if we call GetScrollInfo() anyhow -- this is less
142 // expensive than call it once here and then call GetScrollRange()
143 // below
144 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
145
146 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
147 {
148 wxLogLastError(_T("GetScrollInfo"));
149 }
150
151 trackPos = scrollInfo.nTrackPos;
152 position = scrollInfo.nPos;
153 maxPos = scrollInfo.nMax;
154 }
155 else
156 #endif // Win32
157 {
158 position = ::GetScrollPos((HWND) control, SB_CTL);
159 int minPos;
160 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
161 }
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
252 void 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
268 int wxScrollBar::GetThumbPosition(void) const
269 {
270 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
271 }
272
273 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
274 bool refresh)
275 {
276 m_viewSize = pageSize;
277 m_pageSize = thumbSize;
278 m_objectSize = range;
279
280 // The range (number of scroll steps) is the
281 // object length minus the page size.
282 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
283
284 #if defined(__WIN95__)
285 // Try to adjust the range to cope with page size > 1
286 // (see comment for SetPageLength)
287 if ( m_pageSize > 1 )
288 {
289 range1 += (m_pageSize - 1);
290 }
291
292 SCROLLINFO info;
293 info.cbSize = sizeof(SCROLLINFO);
294 info.nPage = m_pageSize;
295 info.nMin = 0;
296 info.nMax = range1;
297 info.nPos = position;
298
299 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
300
301 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
302 #else
303 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
304 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
305 #endif
306 }
307
308
309 /* From the WIN32 documentation:
310 In version 4.0 or later, the maximum value that a scroll bar can report
311 (that is, the maximum scrolling position) depends on the page size.
312 If the scroll bar has a page size greater than one, the maximum scrolling position
313 is less than the maximum range value. You can use the following formula to calculate
314 the maximum scrolling position:
315
316 MaxScrollPos = MaxRangeValue - (PageSize - 1)
317 */
318
319 #if WXWIN_COMPATIBILITY
320 void wxScrollBar::SetPageSize(int pageLength)
321 {
322 m_pageSize = pageLength;
323
324 #if defined(__WIN95__)
325 SCROLLINFO info;
326 info.cbSize = sizeof(SCROLLINFO);
327 info.nPage = pageLength;
328 info.fMask = SIF_PAGE ;
329
330 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
331 #endif
332 }
333
334 void wxScrollBar::SetObjectLength(int objectLength)
335 {
336 m_objectSize = objectLength;
337
338 // The range (number of scroll steps) is the
339 // object length minus the view size.
340 int range = wxMax((objectLength - m_viewSize), 0) ;
341
342 #if defined(__WIN95__)
343 // Try to adjust the range to cope with page size > 1
344 // (see comment for SetPageLength)
345 if ( m_pageSize > 1 )
346 {
347 range += (m_pageSize - 1);
348 }
349
350 SCROLLINFO info;
351 info.cbSize = sizeof(SCROLLINFO);
352 info.nPage = 0;
353 info.nMin = 0;
354 info.nMax = range;
355 info.nPos = 0;
356 info.fMask = SIF_RANGE ;
357
358 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
359 #else
360 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range, TRUE);
361 #endif
362 }
363
364 void wxScrollBar::SetViewLength(int viewLength)
365 {
366 m_viewSize = viewLength;
367 }
368
369 void wxScrollBar::GetValues(int *viewStart, int *viewLength, int *objectLength,
370 int *pageLength) const
371 {
372 *viewStart = ::GetScrollPos((HWND)m_hWnd, SB_CTL);
373 *viewLength = m_viewSize;
374 *objectLength = m_objectSize;
375 *pageLength = m_pageSize;
376 }
377 #endif
378
379 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
380 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
381 {
382 return 0;
383 }
384
385 void wxScrollBar::Command(wxCommandEvent& event)
386 {
387 SetThumbPosition(event.m_commandInt);
388 ProcessCommand(event);
389 }
390
391 #if WXWIN_COMPATIBILITY
392 // Backward compatibility
393 void wxScrollBar::OnScroll(wxScrollEvent& event)
394 {
395 wxEventType oldEvent = event.GetEventType();
396 event.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED );
397 if ( !GetEventHandler()->ProcessEvent(event) )
398 {
399 event.SetEventType( oldEvent );
400 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
401 event.Skip();
402 }
403 }
404 #endif
405
406 #endif // wxUSE_SCROLLBAR