]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
supported controls on toolbar
[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 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 // 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 }
159
160 #if defined(__WIN95__)
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__
167
168 wxEventType scrollEvent = wxEVT_NULL;
169
170 int nScrollInc;
171 switch ( wParam )
172 {
173 case SB_BOTTOM:
174 nScrollInc = maxPos - position;
175 scrollEvent = wxEVT_SCROLL_TOP;
176 break;
177
178 case SB_TOP:
179 nScrollInc = -position;
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
203 case SB_THUMBPOSITION:
204 nScrollInc = trackPos - position;
205 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
206 break;
207
208 case SB_THUMBTRACK:
209 nScrollInc = trackPos - position;
210 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
211 break;
212
213 case SB_ENDSCROLL:
214 nScrollInc = 0;
215 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
216 break;
217
218 default:
219 nScrollInc = 0;
220 }
221
222 if ( nScrollInc )
223 {
224 position += nScrollInc;
225
226 if ( position < 0 )
227 position = 0;
228 if ( position > maxPos )
229 position = maxPos;
230
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 }
240
241 wxScrollEvent event(scrollEvent, m_windowId);
242 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
243 event.SetPosition(position);
244 event.SetEventObject( this );
245
246 return GetEventHandler()->ProcessEvent(event);
247 }
248
249 void wxScrollBar::SetThumbPosition(int viewStart)
250 {
251 #if defined(__WIN95__)
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);
260 #else
261 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
262 #endif
263 }
264
265 int wxScrollBar::GetThumbPosition(void) const
266 {
267 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
268 }
269
270 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
271 bool refresh)
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 {
286 range1 += (m_pageSize - 1);
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:
307 In 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.
309 If the scroll bar has a page size greater than one, the maximum scrolling position
310 is less than the maximum range value. You can use the following formula to calculate
311 the maximum scrolling position:
312
313 MaxScrollPos = MaxRangeValue - (PageSize - 1)
314 */
315
316 #if WXWIN_COMPATIBILITY
317 void wxScrollBar::SetPageSize(int pageLength)
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
331 void wxScrollBar::SetObjectLength(int objectLength)
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 {
344 range += (m_pageSize - 1);
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
361 void wxScrollBar::SetViewLength(int viewLength)
362 {
363 m_viewSize = viewLength;
364 }
365
366 void 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
376 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
377 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
378 {
379 return 0;
380 }
381
382 void wxScrollBar::Command(wxCommandEvent& event)
383 {
384 SetThumbPosition(event.m_commandInt);
385 ProcessCommand(event);
386 }
387
388 #if WXWIN_COMPATIBILITY
389 // Backward compatibility
390 void wxScrollBar::OnScroll(wxScrollEvent& event)
391 {
392 wxEventType oldEvent = event.GetEventType();
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
402
403 #endif // wxUSE_SCROLLBAR