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