]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
oops, forgot to commit yesterday changes related to wxStatusBarBase
[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_TOP:
143 nScrollInc = maxPos - position;
144 scrollEvent = wxEVT_SCROLL_TOP;
145 break;
146
147 case SB_BOTTOM:
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 default:
183 nScrollInc = 0;
184 }
185
186 // don't process the event if there is no displacement,
187 // unless this is a thumb release event.
188 if (( nScrollInc == 0 ) && ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE ))
189 {
190 return FALSE;
191 }
192
193 int new_pos = position + nScrollInc;
194
195 if (new_pos < 0)
196 new_pos = 0;
197 if (new_pos > maxPos)
198 new_pos = maxPos;
199
200 SetThumbPosition(new_pos);
201 wxScrollEvent event(scrollEvent, m_windowId);
202 event.SetPosition(new_pos);
203 event.SetEventObject( this );
204
205 return GetEventHandler()->ProcessEvent(event);
206 }
207
208 void wxScrollBar::SetThumbPosition(int viewStart)
209 {
210 #if defined(__WIN95__)
211 SCROLLINFO info;
212 info.cbSize = sizeof(SCROLLINFO);
213 info.nPage = 0;
214 info.nMin = 0;
215 info.nPos = viewStart;
216 info.fMask = SIF_POS ;
217
218 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
219 #else
220 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
221 #endif
222 }
223
224 int wxScrollBar::GetThumbPosition(void) const
225 {
226 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
227 }
228
229 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
230 bool refresh)
231 {
232 m_viewSize = pageSize;
233 m_pageSize = thumbSize;
234 m_objectSize = range;
235
236 // The range (number of scroll steps) is the
237 // object length minus the page size.
238 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
239
240 #if defined(__WIN95__)
241 // Try to adjust the range to cope with page size > 1
242 // (see comment for SetPageLength)
243 if ( m_pageSize > 1 )
244 {
245 range1 += (m_pageSize - 1);
246 }
247
248 SCROLLINFO info;
249 info.cbSize = sizeof(SCROLLINFO);
250 info.nPage = m_pageSize;
251 info.nMin = 0;
252 info.nMax = range1;
253 info.nPos = position;
254
255 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
256
257 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
258 #else
259 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
260 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
261 #endif
262 }
263
264
265 /* From the WIN32 documentation:
266 In version 4.0 or later, the maximum value that a scroll bar can report
267 (that is, the maximum scrolling position) depends on the page size.
268 If the scroll bar has a page size greater than one, the maximum scrolling position
269 is less than the maximum range value. You can use the following formula to calculate
270 the maximum scrolling position:
271
272 MaxScrollPos = MaxRangeValue - (PageSize - 1)
273 */
274
275 #if WXWIN_COMPATIBILITY
276 void wxScrollBar::SetPageSize(int pageLength)
277 {
278 m_pageSize = pageLength;
279
280 #if defined(__WIN95__)
281 SCROLLINFO info;
282 info.cbSize = sizeof(SCROLLINFO);
283 info.nPage = pageLength;
284 info.fMask = SIF_PAGE ;
285
286 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
287 #endif
288 }
289
290 void wxScrollBar::SetObjectLength(int objectLength)
291 {
292 m_objectSize = objectLength;
293
294 // The range (number of scroll steps) is the
295 // object length minus the view size.
296 int range = wxMax((objectLength - m_viewSize), 0) ;
297
298 #if defined(__WIN95__)
299 // Try to adjust the range to cope with page size > 1
300 // (see comment for SetPageLength)
301 if ( m_pageSize > 1 )
302 {
303 range += (m_pageSize - 1);
304 }
305
306 SCROLLINFO info;
307 info.cbSize = sizeof(SCROLLINFO);
308 info.nPage = 0;
309 info.nMin = 0;
310 info.nMax = range;
311 info.nPos = 0;
312 info.fMask = SIF_RANGE ;
313
314 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
315 #else
316 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range, TRUE);
317 #endif
318 }
319
320 void wxScrollBar::SetViewLength(int viewLength)
321 {
322 m_viewSize = viewLength;
323 }
324
325 void wxScrollBar::GetValues(int *viewStart, int *viewLength, int *objectLength,
326 int *pageLength) const
327 {
328 *viewStart = ::GetScrollPos((HWND)m_hWnd, SB_CTL);
329 *viewLength = m_viewSize;
330 *objectLength = m_objectSize;
331 *pageLength = m_pageSize;
332 }
333 #endif
334
335 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
336 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
337 {
338 return 0;
339 }
340
341 void wxScrollBar::Command(wxCommandEvent& event)
342 {
343 SetThumbPosition(event.m_commandInt);
344 ProcessCommand(event);
345 }
346
347 #if WXWIN_COMPATIBILITY
348 // Backward compatibility
349 void wxScrollBar::OnScroll(wxScrollEvent& event)
350 {
351 wxEventType oldEvent = event.GetEventType();
352 event.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED );
353 if ( !GetEventHandler()->ProcessEvent(event) )
354 {
355 event.SetEventType( oldEvent );
356 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
357 event.Skip();
358 }
359 }
360 #endif
361
362 #endif // wxUSE_SCROLLBAR