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