]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
fix for IsAlive() called after Kill() or Exit()
[wxWidgets.git] / src / msw / scrolbar.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: msw/scrolbar.cpp
2bda0e17
KB
3// Purpose: wxScrollBar
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
1e6feb95 13 #pragma implementation "scrolbar.h"
2bda0e17
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
1e6feb95 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
1e6feb95
VZ
23#if wxUSE_SCROLLBAR
24
2bda0e17 25#ifndef WX_PRECOMP
1e6feb95 26 #include "wx/utils.h"
2bda0e17
KB
27#endif
28
29#include "wx/scrolbar.h"
30#include "wx/msw/private.h"
31
2bda0e17
KB
32IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
33
2bda0e17 34BEGIN_EVENT_TABLE(wxScrollBar, wxControl)
ca5e9f67 35#if WXWIN_COMPATIBILITY
2bda0e17 36 EVT_SCROLL(wxScrollBar::OnScroll)
2bda0e17 37#endif
ca5e9f67 38END_EVENT_TABLE()
2bda0e17 39
2bda0e17
KB
40
41// Scrollbar
debe6624 42bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
2bda0e17 43 const wxPoint& pos,
debe6624 44 const wxSize& size, long style,
2bda0e17
KB
45 const wxValidator& validator,
46 const wxString& name)
47{
48 if (!parent)
49 return FALSE;
50 parent->AddChild(this);
51 SetName(name);
11b6a93b 52#if wxUSE_VALIDATORS
a23fd0e1 53 SetValidator(validator);
11b6a93b 54#endif // wxUSE_VALIDATORS
33ac7e6f 55
7051e6c7
JS
56 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
57 style |= wxNO_BORDER;
58
fd71308f
JS
59 SetBackgroundColour(parent->GetBackgroundColour()) ;
60 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
61 m_windowStyle = style;
62
63 if ( id == -1 )
a23fd0e1 64 m_windowId = (int)NewControlId();
2bda0e17 65 else
a23fd0e1 66 m_windowId = id;
2bda0e17
KB
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
fe3d9123
JS
88 WXDWORD exStyle = 0;
89 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
b0766406 90
2bda0e17
KB
91 // Now create scrollbar
92 DWORD _direction = (style & wxHORIZONTAL) ?
93 SBS_HORZ: SBS_VERT;
fe3d9123 94 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
b0766406 95 _direction | wstyle,
2bda0e17
KB
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
c0ed460c 107 SetFont(parent->GetFont());
1c089c47 108
2bda0e17
KB
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
119wxScrollBar::~wxScrollBar(void)
120{
121}
122
a23fd0e1
VZ
123bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
124 WXWORD pos, WXHWND control)
2bda0e17 125{
2b5f62a0
VZ
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 }
a23fd0e1 160
2bda0e17 161#if defined(__WIN95__)
a23fd0e1
VZ
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__
2bda0e17 168
7798a18e 169 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
170
171 int nScrollInc;
172 switch ( wParam )
173 {
a29ffc34 174 case SB_BOTTOM:
a23fd0e1
VZ
175 nScrollInc = maxPos - position;
176 scrollEvent = wxEVT_SCROLL_TOP;
177 break;
178
a29ffc34
VZ
179 case SB_TOP:
180 nScrollInc = -position;
a23fd0e1
VZ
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
a23fd0e1 204 case SB_THUMBPOSITION:
2b5f62a0 205 nScrollInc = trackPos - position;
7d56fb8f
GRG
206 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
207 break;
feda3011
GRG
208
209 case SB_THUMBTRACK:
2b5f62a0 210 nScrollInc = trackPos - position;
a23fd0e1
VZ
211 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
212 break;
213
e8b669d3
VZ
214 case SB_ENDSCROLL:
215 nScrollInc = 0;
216 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
217 break;
218
a23fd0e1
VZ
219 default:
220 nScrollInc = 0;
2bda0e17
KB
221 }
222
e8b669d3 223 if ( nScrollInc )
2bda0e17 224 {
e8b669d3 225 position += nScrollInc;
2bda0e17 226
e8b669d3
VZ
227 if ( position < 0 )
228 position = 0;
229 if ( position > maxPos )
230 position = maxPos;
a23fd0e1 231
e8b669d3
VZ
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 }
a23fd0e1 241
a23fd0e1 242 wxScrollEvent event(scrollEvent, m_windowId);
27e229f5 243 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e8b669d3 244 event.SetPosition(position);
a23fd0e1
VZ
245 event.SetEventObject( this );
246
247 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
248}
249
4fabb575 250void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17
KB
251{
252#if defined(__WIN95__)
2b5f62a0
VZ
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);
2bda0e17 261#else
2b5f62a0 262 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
2bda0e17
KB
263#endif
264}
265
4fabb575 266int wxScrollBar::GetThumbPosition(void) const
2bda0e17
KB
267{
268 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
269}
270
debe6624
JS
271void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
272 bool refresh)
2bda0e17
KB
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 {
a23fd0e1 287 range1 += (m_pageSize - 1);
2bda0e17
KB
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:
308In 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.
310If the scroll bar has a page size greater than one, the maximum scrolling position
311is less than the maximum range value. You can use the following formula to calculate
312the maximum scrolling position:
33ac7e6f
KB
313
314MaxScrollPos = MaxRangeValue - (PageSize - 1)
2bda0e17
KB
315*/
316
317#if WXWIN_COMPATIBILITY
debe6624 318void wxScrollBar::SetPageSize(int pageLength)
2bda0e17
KB
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
debe6624 332void wxScrollBar::SetObjectLength(int objectLength)
2bda0e17
KB
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 {
a23fd0e1 345 range += (m_pageSize - 1);
2bda0e17
KB
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
debe6624 362void wxScrollBar::SetViewLength(int viewLength)
2bda0e17
KB
363{
364 m_viewSize = viewLength;
365}
366
367void 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
33ac7e6f
KB
377WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
378 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
2bda0e17
KB
379{
380 return 0;
381}
382
383void wxScrollBar::Command(wxCommandEvent& event)
384{
ca5e9f67 385 SetThumbPosition(event.m_commandInt);
2bda0e17
KB
386 ProcessCommand(event);
387}
388
389#if WXWIN_COMPATIBILITY
390// Backward compatibility
391void wxScrollBar::OnScroll(wxScrollEvent& event)
392{
7798a18e 393 wxEventType oldEvent = event.GetEventType();
2bda0e17
KB
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
1e6feb95
VZ
403
404#endif // wxUSE_SCROLLBAR