]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
backported fix for proper tree ctrl refershing after changing item colour/font from 2.2
[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
KB
124{
125 int position = ::GetScrollPos((HWND) control, SB_CTL);
126 int minPos, maxPos;
127 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
a23fd0e1 128
2bda0e17 129#if defined(__WIN95__)
a23fd0e1
VZ
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__
2bda0e17 136
7798a18e 137 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
138
139 int nScrollInc;
140 switch ( wParam )
141 {
a23fd0e1
VZ
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
a23fd0e1 172 case SB_THUMBPOSITION:
7d56fb8f
GRG
173 nScrollInc = pos - position;
174 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
175 break;
feda3011
GRG
176
177 case SB_THUMBTRACK:
a23fd0e1
VZ
178 nScrollInc = pos - position;
179 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
180 break;
181
182 default:
183 nScrollInc = 0;
2bda0e17
KB
184 }
185
7d56fb8f
GRG
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 ))
2bda0e17 189 {
a23fd0e1 190 return FALSE;
2bda0e17 191 }
2bda0e17 192
a23fd0e1
VZ
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);
2bda0e17
KB
206}
207
4fabb575 208void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17
KB
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
4fabb575 224int wxScrollBar::GetThumbPosition(void) const
2bda0e17
KB
225{
226 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
227}
228
debe6624
JS
229void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
230 bool refresh)
2bda0e17
KB
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 {
a23fd0e1 245 range1 += (m_pageSize - 1);
2bda0e17
KB
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:
266In 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.
268If the scroll bar has a page size greater than one, the maximum scrolling position
269is less than the maximum range value. You can use the following formula to calculate
270the maximum scrolling position:
33ac7e6f
KB
271
272MaxScrollPos = MaxRangeValue - (PageSize - 1)
2bda0e17
KB
273*/
274
275#if WXWIN_COMPATIBILITY
debe6624 276void wxScrollBar::SetPageSize(int pageLength)
2bda0e17
KB
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
debe6624 290void wxScrollBar::SetObjectLength(int objectLength)
2bda0e17
KB
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 {
a23fd0e1 303 range += (m_pageSize - 1);
2bda0e17
KB
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
debe6624 320void wxScrollBar::SetViewLength(int viewLength)
2bda0e17
KB
321{
322 m_viewSize = viewLength;
323}
324
325void 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
33ac7e6f
KB
335WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
336 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
2bda0e17
KB
337{
338 return 0;
339}
340
341void wxScrollBar::Command(wxCommandEvent& event)
342{
ca5e9f67 343 SetThumbPosition(event.m_commandInt);
2bda0e17
KB
344 ProcessCommand(event);
345}
346
347#if WXWIN_COMPATIBILITY
348// Backward compatibility
349void wxScrollBar::OnScroll(wxScrollEvent& event)
350{
7798a18e 351 wxEventType oldEvent = event.GetEventType();
2bda0e17
KB
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
1e6feb95
VZ
361
362#endif // wxUSE_SCROLLBAR