]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
include "module.h"
[wxWidgets.git] / src / msw / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 and Markus Holzem
9 // Licence: wxWindows license
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 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #include "wx/utils.h"
26 #endif
27
28 #include "wx/scrolbar.h"
29 #include "wx/msw/private.h"
30
31 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
32
33 BEGIN_EVENT_TABLE(wxScrollBar, wxControl)
34 #if WXWIN_COMPATIBILITY
35 EVT_SCROLL(wxScrollBar::OnScroll)
36 #endif
37 END_EVENT_TABLE()
38
39
40 // Scrollbar
41 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
42 const wxPoint& pos,
43 const wxSize& size, long style,
44 const wxValidator& validator,
45 const wxString& name)
46 {
47 if (!parent)
48 return FALSE;
49 parent->AddChild(this);
50 SetName(name);
51 SetValidator(validator);
52
53 SetBackgroundColour(parent->GetBackgroundColour()) ;
54 SetForegroundColour(parent->GetForegroundColour()) ;
55 m_windowStyle = style;
56
57 if ( id == -1 )
58 m_windowId = (int)NewControlId();
59 else
60 m_windowId = id;
61
62 int x = pos.x;
63 int y = pos.y;
64 int width = size.x;
65 int height = size.y;
66
67 if (width == -1)
68 {
69 if (style & wxHORIZONTAL)
70 width = 140;
71 else
72 width = 14;
73 }
74 if (height == -1)
75 {
76 if (style & wxVERTICAL)
77 height = 140;
78 else
79 height = 14;
80 }
81
82 // Now create scrollbar
83 DWORD _direction = (style & wxHORIZONTAL) ?
84 SBS_HORZ: SBS_VERT;
85 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(style), wxT("SCROLLBAR"), wxT("scrollbar"),
86 _direction | WS_CHILD | WS_VISIBLE,
87 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
88 wxGetInstance(), NULL);
89
90 m_pageSize = 1;
91 m_viewSize = 1;
92 m_objectSize = 1;
93
94 ::SetScrollRange(scroll_bar, SB_CTL, 0, 1, FALSE);
95 ::SetScrollPos(scroll_bar, SB_CTL, 0, FALSE);
96 ShowWindow(scroll_bar, SW_SHOW);
97
98 SetFont(parent->GetFont());
99
100 m_hWnd = (WXHWND)scroll_bar;
101
102 // Subclass again for purposes of dialog editing mode
103 SubclassWin((WXHWND) scroll_bar);
104
105 SetSize(x, y, width, height);
106
107 return TRUE;
108 }
109
110 wxScrollBar::~wxScrollBar(void)
111 {
112 }
113
114 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
115 WXWORD pos, WXHWND control)
116 {
117 int position = ::GetScrollPos((HWND) control, SB_CTL);
118 int minPos, maxPos;
119 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
120
121 #if defined(__WIN95__)
122 // A page size greater than one has the effect of reducing the effective
123 // range, therefore the range has already been boosted artificially - so
124 // reduce it again.
125 if ( m_pageSize > 1 )
126 maxPos -= (m_pageSize - 1);
127 #endif // __WIN95__
128
129 wxEventType scrollEvent = wxEVT_NULL;
130
131 int nScrollInc;
132 switch ( wParam )
133 {
134 case SB_TOP:
135 nScrollInc = maxPos - position;
136 scrollEvent = wxEVT_SCROLL_TOP;
137 break;
138
139 case SB_BOTTOM:
140 nScrollInc = - position;
141 scrollEvent = wxEVT_SCROLL_BOTTOM;
142 break;
143
144 case SB_LINEUP:
145 nScrollInc = -1;
146 scrollEvent = wxEVT_SCROLL_LINEUP;
147 break;
148
149 case SB_LINEDOWN:
150 nScrollInc = 1;
151 scrollEvent = wxEVT_SCROLL_LINEDOWN;
152 break;
153
154 case SB_PAGEUP:
155 nScrollInc = -GetPageSize();
156 scrollEvent = wxEVT_SCROLL_PAGEUP;
157 break;
158
159 case SB_PAGEDOWN:
160 nScrollInc = GetPageSize();
161 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
162 break;
163
164 case SB_THUMBTRACK:
165 case SB_THUMBPOSITION:
166 nScrollInc = pos - position;
167 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
168 break;
169
170 default:
171 nScrollInc = 0;
172 }
173
174 if ( nScrollInc == 0 )
175 {
176 // no event to process, so don't process it
177 return FALSE;
178 }
179
180 int new_pos = position + nScrollInc;
181
182 if (new_pos < 0)
183 new_pos = 0;
184 if (new_pos > maxPos)
185 new_pos = maxPos;
186
187 SetThumbPosition(new_pos);
188 wxScrollEvent event(scrollEvent, m_windowId);
189 event.SetPosition(new_pos);
190 event.SetEventObject( this );
191
192 return GetEventHandler()->ProcessEvent(event);
193 }
194
195 void wxScrollBar::SetThumbPosition(int viewStart)
196 {
197 #if defined(__WIN95__)
198 SCROLLINFO info;
199 info.cbSize = sizeof(SCROLLINFO);
200 info.nPage = 0;
201 info.nMin = 0;
202 info.nPos = viewStart;
203 info.fMask = SIF_POS ;
204
205 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
206 #else
207 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
208 #endif
209 }
210
211 int wxScrollBar::GetThumbPosition(void) const
212 {
213 return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
214 }
215
216 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
217 bool refresh)
218 {
219 m_viewSize = pageSize;
220 m_pageSize = thumbSize;
221 m_objectSize = range;
222
223 // The range (number of scroll steps) is the
224 // object length minus the page size.
225 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
226
227 #if defined(__WIN95__)
228 // Try to adjust the range to cope with page size > 1
229 // (see comment for SetPageLength)
230 if ( m_pageSize > 1 )
231 {
232 range1 += (m_pageSize - 1);
233 }
234
235 SCROLLINFO info;
236 info.cbSize = sizeof(SCROLLINFO);
237 info.nPage = m_pageSize;
238 info.nMin = 0;
239 info.nMax = range1;
240 info.nPos = position;
241
242 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
243
244 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
245 #else
246 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
247 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
248 #endif
249 }
250
251
252 /* From the WIN32 documentation:
253 In version 4.0 or later, the maximum value that a scroll bar can report
254 (that is, the maximum scrolling position) depends on the page size.
255 If the scroll bar has a page size greater than one, the maximum scrolling position
256 is less than the maximum range value. You can use the following formula to calculate
257 the maximum scrolling position:
258
259 MaxScrollPos = MaxRangeValue - (PageSize - 1)
260 */
261
262 #if WXWIN_COMPATIBILITY
263 void wxScrollBar::SetPageSize(int pageLength)
264 {
265 m_pageSize = pageLength;
266
267 #if defined(__WIN95__)
268 SCROLLINFO info;
269 info.cbSize = sizeof(SCROLLINFO);
270 info.nPage = pageLength;
271 info.fMask = SIF_PAGE ;
272
273 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
274 #endif
275 }
276
277 void wxScrollBar::SetObjectLength(int objectLength)
278 {
279 m_objectSize = objectLength;
280
281 // The range (number of scroll steps) is the
282 // object length minus the view size.
283 int range = wxMax((objectLength - m_viewSize), 0) ;
284
285 #if defined(__WIN95__)
286 // Try to adjust the range to cope with page size > 1
287 // (see comment for SetPageLength)
288 if ( m_pageSize > 1 )
289 {
290 range += (m_pageSize - 1);
291 }
292
293 SCROLLINFO info;
294 info.cbSize = sizeof(SCROLLINFO);
295 info.nPage = 0;
296 info.nMin = 0;
297 info.nMax = range;
298 info.nPos = 0;
299 info.fMask = SIF_RANGE ;
300
301 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
302 #else
303 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range, TRUE);
304 #endif
305 }
306
307 void wxScrollBar::SetViewLength(int viewLength)
308 {
309 m_viewSize = viewLength;
310 }
311
312 void wxScrollBar::GetValues(int *viewStart, int *viewLength, int *objectLength,
313 int *pageLength) const
314 {
315 *viewStart = ::GetScrollPos((HWND)m_hWnd, SB_CTL);
316 *viewLength = m_viewSize;
317 *objectLength = m_objectSize;
318 *pageLength = m_pageSize;
319 }
320 #endif
321
322 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
323 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
324 {
325 return 0;
326 }
327
328 void wxScrollBar::Command(wxCommandEvent& event)
329 {
330 SetThumbPosition(event.m_commandInt);
331 ProcessCommand(event);
332 }
333
334 #if WXWIN_COMPATIBILITY
335 // Backward compatibility
336 void wxScrollBar::OnScroll(wxScrollEvent& event)
337 {
338 wxEventType oldEvent = event.GetEventType();
339 event.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED );
340 if ( !GetEventHandler()->ProcessEvent(event) )
341 {
342 event.SetEventType( oldEvent );
343 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
344 event.Skip();
345 }
346 }
347 #endif