]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
Clean up memory if have to exit early
[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$
8// Copyright: (c) Julian Smart and Markus Holzem
a23fd0e1 9// Licence: wxWindows license
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
fd71308f
JS
56 SetBackgroundColour(parent->GetBackgroundColour()) ;
57 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
58 m_windowStyle = style;
59
60 if ( id == -1 )
a23fd0e1 61 m_windowId = (int)NewControlId();
2bda0e17 62 else
a23fd0e1 63 m_windowId = id;
2bda0e17
KB
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
b0766406
JS
85 DWORD wstyle = WS_VISIBLE | WS_CHILD;
86
87 if ( m_windowStyle & wxCLIP_SIBLINGS )
88 wstyle |= WS_CLIPSIBLINGS;
89
2bda0e17
KB
90 // Now create scrollbar
91 DWORD _direction = (style & wxHORIZONTAL) ?
92 SBS_HORZ: SBS_VERT;
223d09f6 93 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(style), wxT("SCROLLBAR"), wxT("scrollbar"),
b0766406 94 _direction | wstyle,
2bda0e17
KB
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
c0ed460c 106 SetFont(parent->GetFont());
1c089c47 107
2bda0e17
KB
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
118wxScrollBar::~wxScrollBar(void)
119{
120}
121
a23fd0e1
VZ
122bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
123 WXWORD pos, WXHWND control)
2bda0e17 124{
2b5f62a0
VZ
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 }
a23fd0e1 159
2bda0e17 160#if defined(__WIN95__)
a23fd0e1
VZ
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__
2bda0e17 167
7798a18e 168 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
169
170 int nScrollInc;
171 switch ( wParam )
172 {
a29ffc34 173 case SB_BOTTOM:
a23fd0e1
VZ
174 nScrollInc = maxPos - position;
175 scrollEvent = wxEVT_SCROLL_TOP;
176 break;
177
a29ffc34
VZ
178 case SB_TOP:
179 nScrollInc = -position;
a23fd0e1
VZ
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
a23fd0e1 203 case SB_THUMBPOSITION:
2b5f62a0 204 nScrollInc = trackPos - position;
7d56fb8f
GRG
205 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
206 break;
feda3011
GRG
207
208 case SB_THUMBTRACK:
2b5f62a0 209 nScrollInc = trackPos - position;
a23fd0e1
VZ
210 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
211 break;
212
e8b669d3
VZ
213 case SB_ENDSCROLL:
214 nScrollInc = 0;
215 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
216 break;
217
a23fd0e1
VZ
218 default:
219 nScrollInc = 0;
2bda0e17
KB
220 }
221
e8b669d3 222 if ( nScrollInc )
2bda0e17 223 {
e8b669d3 224 position += nScrollInc;
2bda0e17 225
e8b669d3
VZ
226 if ( position < 0 )
227 position = 0;
228 if ( position > maxPos )
229 position = maxPos;
a23fd0e1 230
e8b669d3
VZ
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 }
a23fd0e1 240
a23fd0e1 241 wxScrollEvent event(scrollEvent, m_windowId);
e8b669d3 242 event.SetPosition(position);
a23fd0e1
VZ
243 event.SetEventObject( this );
244
245 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
246}
247
4fabb575 248void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17
KB
249{
250#if defined(__WIN95__)
2b5f62a0
VZ
251 SCROLLINFO info;
252 info.cbSize = sizeof(SCROLLINFO);
253 info.nPage = 0;
254 info.nMin = 0;
255 info.nPos = viewStart;
256 info.fMask = SIF_POS ;
257
258 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
2bda0e17 259#else
2b5f62a0 260 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
2bda0e17
KB
261#endif
262}
263
4fabb575 264int wxScrollBar::GetThumbPosition(void) const
2bda0e17
KB
265{
266 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
267}
268
debe6624
JS
269void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
270 bool refresh)
2bda0e17
KB
271{
272 m_viewSize = pageSize;
273 m_pageSize = thumbSize;
274 m_objectSize = range;
275
276 // The range (number of scroll steps) is the
277 // object length minus the page size.
278 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
279
280#if defined(__WIN95__)
281 // Try to adjust the range to cope with page size > 1
282 // (see comment for SetPageLength)
283 if ( m_pageSize > 1 )
284 {
a23fd0e1 285 range1 += (m_pageSize - 1);
2bda0e17
KB
286 }
287
288 SCROLLINFO info;
289 info.cbSize = sizeof(SCROLLINFO);
290 info.nPage = m_pageSize;
291 info.nMin = 0;
292 info.nMax = range1;
293 info.nPos = position;
294
295 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
296
297 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
298#else
299 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
300 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
301#endif
302}
303
304
305/* From the WIN32 documentation:
306In version 4.0 or later, the maximum value that a scroll bar can report
307(that is, the maximum scrolling position) depends on the page size.
308If the scroll bar has a page size greater than one, the maximum scrolling position
309is less than the maximum range value. You can use the following formula to calculate
310the maximum scrolling position:
33ac7e6f
KB
311
312MaxScrollPos = MaxRangeValue - (PageSize - 1)
2bda0e17
KB
313*/
314
315#if WXWIN_COMPATIBILITY
debe6624 316void wxScrollBar::SetPageSize(int pageLength)
2bda0e17
KB
317{
318 m_pageSize = pageLength;
319
320#if defined(__WIN95__)
321 SCROLLINFO info;
322 info.cbSize = sizeof(SCROLLINFO);
323 info.nPage = pageLength;
324 info.fMask = SIF_PAGE ;
325
326 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
327#endif
328}
329
debe6624 330void wxScrollBar::SetObjectLength(int objectLength)
2bda0e17
KB
331{
332 m_objectSize = objectLength;
333
334 // The range (number of scroll steps) is the
335 // object length minus the view size.
336 int range = wxMax((objectLength - m_viewSize), 0) ;
337
338#if defined(__WIN95__)
339 // Try to adjust the range to cope with page size > 1
340 // (see comment for SetPageLength)
341 if ( m_pageSize > 1 )
342 {
a23fd0e1 343 range += (m_pageSize - 1);
2bda0e17
KB
344 }
345
346 SCROLLINFO info;
347 info.cbSize = sizeof(SCROLLINFO);
348 info.nPage = 0;
349 info.nMin = 0;
350 info.nMax = range;
351 info.nPos = 0;
352 info.fMask = SIF_RANGE ;
353
354 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
355#else
356 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range, TRUE);
357#endif
358}
359
debe6624 360void wxScrollBar::SetViewLength(int viewLength)
2bda0e17
KB
361{
362 m_viewSize = viewLength;
363}
364
365void wxScrollBar::GetValues(int *viewStart, int *viewLength, int *objectLength,
366 int *pageLength) const
367{
368 *viewStart = ::GetScrollPos((HWND)m_hWnd, SB_CTL);
369 *viewLength = m_viewSize;
370 *objectLength = m_objectSize;
371 *pageLength = m_pageSize;
372}
373#endif
374
33ac7e6f
KB
375WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
376 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
2bda0e17
KB
377{
378 return 0;
379}
380
381void wxScrollBar::Command(wxCommandEvent& event)
382{
ca5e9f67 383 SetThumbPosition(event.m_commandInt);
2bda0e17
KB
384 ProcessCommand(event);
385}
386
387#if WXWIN_COMPATIBILITY
388// Backward compatibility
389void wxScrollBar::OnScroll(wxScrollEvent& event)
390{
7798a18e 391 wxEventType oldEvent = event.GetEventType();
2bda0e17
KB
392 event.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED );
393 if ( !GetEventHandler()->ProcessEvent(event) )
394 {
395 event.SetEventType( oldEvent );
396 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
397 event.Skip();
398 }
399}
400#endif
1e6feb95
VZ
401
402#endif // wxUSE_SCROLLBAR