]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
fix for IsAlive() called after Kill() or Exit()
[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 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
119 wxScrollBar::~wxScrollBar(void)
120 {
121 }
122
123 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
124 WXWORD pos, WXHWND control)
125 {
126 // current and max positions
127 int position,
128 maxPos, trackPos = pos;
129
130 #ifdef __WIN32__
131 // when we're dragging the scrollbar we can't use pos parameter because it
132 // is limited to 16 bits
133 if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
134 {
135 SCROLLINFO scrollInfo;
136 wxZeroMemory(scrollInfo);
137 scrollInfo.cbSize = sizeof(SCROLLINFO);
138
139 // also get the range if we call GetScrollInfo() anyhow -- this is less
140 // expensive than call it once here and then call GetScrollRange()
141 // below
142 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
143
144 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
145 {
146 wxLogLastError(_T("GetScrollInfo"));
147 }
148
149 trackPos = scrollInfo.nTrackPos;
150 position = scrollInfo.nPos;
151 maxPos = scrollInfo.nMax;
152 }
153 else
154 #endif // Win32
155 {
156 position = ::GetScrollPos((HWND) control, SB_CTL);
157 int minPos;
158 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
159 }
160
161 #if defined(__WIN95__)
162 // A page size greater than one has the effect of reducing the effective
163 // range, therefore the range has already been boosted artificially - so
164 // reduce it again.
165 if ( m_pageSize > 1 )
166 maxPos -= (m_pageSize - 1);
167 #endif // __WIN95__
168
169 wxEventType scrollEvent = wxEVT_NULL;
170
171 int nScrollInc;
172 switch ( wParam )
173 {
174 case SB_BOTTOM:
175 nScrollInc = maxPos - position;
176 scrollEvent = wxEVT_SCROLL_TOP;
177 break;
178
179 case SB_TOP:
180 nScrollInc = -position;
181 scrollEvent = wxEVT_SCROLL_BOTTOM;
182 break;
183
184 case SB_LINEUP:
185 nScrollInc = -1;
186 scrollEvent = wxEVT_SCROLL_LINEUP;
187 break;
188
189 case SB_LINEDOWN:
190 nScrollInc = 1;
191 scrollEvent = wxEVT_SCROLL_LINEDOWN;
192 break;
193
194 case SB_PAGEUP:
195 nScrollInc = -GetPageSize();
196 scrollEvent = wxEVT_SCROLL_PAGEUP;
197 break;
198
199 case SB_PAGEDOWN:
200 nScrollInc = GetPageSize();
201 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
202 break;
203
204 case SB_THUMBPOSITION:
205 nScrollInc = trackPos - position;
206 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
207 break;
208
209 case SB_THUMBTRACK:
210 nScrollInc = trackPos - position;
211 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
212 break;
213
214 case SB_ENDSCROLL:
215 nScrollInc = 0;
216 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
217 break;
218
219 default:
220 nScrollInc = 0;
221 }
222
223 if ( nScrollInc )
224 {
225 position += nScrollInc;
226
227 if ( position < 0 )
228 position = 0;
229 if ( position > maxPos )
230 position = maxPos;
231
232 SetThumbPosition(position);
233 }
234 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
235 scrollEvent != wxEVT_SCROLL_ENDSCROLL )
236 {
237 // don't process the event if there is no displacement,
238 // unless this is a thumb release or end scroll event.
239 return FALSE;
240 }
241
242 wxScrollEvent event(scrollEvent, m_windowId);
243 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
244 event.SetPosition(position);
245 event.SetEventObject( this );
246
247 return GetEventHandler()->ProcessEvent(event);
248 }
249
250 void wxScrollBar::SetThumbPosition(int viewStart)
251 {
252 #if defined(__WIN95__)
253 SCROLLINFO info;
254 info.cbSize = sizeof(SCROLLINFO);
255 info.nPage = 0;
256 info.nMin = 0;
257 info.nPos = viewStart;
258 info.fMask = SIF_POS ;
259
260 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
261 #else
262 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
263 #endif
264 }
265
266 int wxScrollBar::GetThumbPosition(void) const
267 {
268 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
269 }
270
271 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
272 bool refresh)
273 {
274 m_viewSize = pageSize;
275 m_pageSize = thumbSize;
276 m_objectSize = range;
277
278 // The range (number of scroll steps) is the
279 // object length minus the page size.
280 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
281
282 #if defined(__WIN95__)
283 // Try to adjust the range to cope with page size > 1
284 // (see comment for SetPageLength)
285 if ( m_pageSize > 1 )
286 {
287 range1 += (m_pageSize - 1);
288 }
289
290 SCROLLINFO info;
291 info.cbSize = sizeof(SCROLLINFO);
292 info.nPage = m_pageSize;
293 info.nMin = 0;
294 info.nMax = range1;
295 info.nPos = position;
296
297 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
298
299 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
300 #else
301 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
302 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
303 #endif
304 }
305
306
307 /* From the WIN32 documentation:
308 In version 4.0 or later, the maximum value that a scroll bar can report
309 (that is, the maximum scrolling position) depends on the page size.
310 If the scroll bar has a page size greater than one, the maximum scrolling position
311 is less than the maximum range value. You can use the following formula to calculate
312 the maximum scrolling position:
313
314 MaxScrollPos = MaxRangeValue - (PageSize - 1)
315 */
316
317 #if WXWIN_COMPATIBILITY
318 void wxScrollBar::SetPageSize(int pageLength)
319 {
320 m_pageSize = pageLength;
321
322 #if defined(__WIN95__)
323 SCROLLINFO info;
324 info.cbSize = sizeof(SCROLLINFO);
325 info.nPage = pageLength;
326 info.fMask = SIF_PAGE ;
327
328 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
329 #endif
330 }
331
332 void wxScrollBar::SetObjectLength(int objectLength)
333 {
334 m_objectSize = objectLength;
335
336 // The range (number of scroll steps) is the
337 // object length minus the view size.
338 int range = wxMax((objectLength - m_viewSize), 0) ;
339
340 #if defined(__WIN95__)
341 // Try to adjust the range to cope with page size > 1
342 // (see comment for SetPageLength)
343 if ( m_pageSize > 1 )
344 {
345 range += (m_pageSize - 1);
346 }
347
348 SCROLLINFO info;
349 info.cbSize = sizeof(SCROLLINFO);
350 info.nPage = 0;
351 info.nMin = 0;
352 info.nMax = range;
353 info.nPos = 0;
354 info.fMask = SIF_RANGE ;
355
356 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
357 #else
358 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range, TRUE);
359 #endif
360 }
361
362 void wxScrollBar::SetViewLength(int viewLength)
363 {
364 m_viewSize = viewLength;
365 }
366
367 void wxScrollBar::GetValues(int *viewStart, int *viewLength, int *objectLength,
368 int *pageLength) const
369 {
370 *viewStart = ::GetScrollPos((HWND)m_hWnd, SB_CTL);
371 *viewLength = m_viewSize;
372 *objectLength = m_objectSize;
373 *pageLength = m_pageSize;
374 }
375 #endif
376
377 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
378 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
379 {
380 return 0;
381 }
382
383 void wxScrollBar::Command(wxCommandEvent& event)
384 {
385 SetThumbPosition(event.m_commandInt);
386 ProcessCommand(event);
387 }
388
389 #if WXWIN_COMPATIBILITY
390 // Backward compatibility
391 void wxScrollBar::OnScroll(wxScrollEvent& event)
392 {
393 wxEventType oldEvent = event.GetEventType();
394 event.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED );
395 if ( !GetEventHandler()->ProcessEvent(event) )
396 {
397 event.SetEventType( oldEvent );
398 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
399 event.Skip();
400 }
401 }
402 #endif
403
404 #endif // wxUSE_SCROLLBAR