]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
Added licence/copyright information
[wxWidgets.git] / src / msw / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #if wxUSE_SCROLLBAR
24
25 #ifndef WX_PRECOMP
26 #include "wx/utils.h"
27 #endif
28
29 #include "wx/scrolbar.h"
30 #include "wx/msw/private.h"
31 #include "wx/settings.h"
32
33 #if wxUSE_EXTENDED_RTTI
34 WX_DEFINE_FLAGS( wxScrollBarStyle )
35
36 wxBEGIN_FLAGS( wxScrollBarStyle )
37 // new style border flags, we put them first to
38 // use them for streaming out
39 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
40 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
41 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
42 wxFLAGS_MEMBER(wxBORDER_RAISED)
43 wxFLAGS_MEMBER(wxBORDER_STATIC)
44 wxFLAGS_MEMBER(wxBORDER_NONE)
45
46 // old style border flags
47 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
48 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
49 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
50 wxFLAGS_MEMBER(wxRAISED_BORDER)
51 wxFLAGS_MEMBER(wxSTATIC_BORDER)
52 wxFLAGS_MEMBER(wxBORDER)
53
54 // standard window styles
55 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
56 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
57 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
58 wxFLAGS_MEMBER(wxWANTS_CHARS)
59 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
60 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
61 wxFLAGS_MEMBER(wxVSCROLL)
62 wxFLAGS_MEMBER(wxHSCROLL)
63
64 wxFLAGS_MEMBER(wxSB_HORIZONTAL)
65 wxFLAGS_MEMBER(wxSB_VERTICAL)
66
67 wxEND_FLAGS( wxScrollBarStyle )
68
69 IMPLEMENT_DYNAMIC_CLASS_XTI(wxScrollBar, wxControl,"wx/scrolbar.h")
70
71 wxBEGIN_PROPERTIES_TABLE(wxScrollBar)
72 wxEVENT_RANGE_PROPERTY( Scroll , wxEVT_SCROLL_TOP , wxEVT_SCROLL_ENDSCROLL , wxScrollEvent )
73
74 wxPROPERTY( ThumbPosition , int , SetThumbPosition, GetThumbPosition, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
75 wxPROPERTY( Range , int , SetRange, GetRange, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
76 wxPROPERTY( ThumbSize , int , SetThumbSize, GetThumbSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 wxPROPERTY( PageSize , int , SetPageSize, GetPageSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
78 wxPROPERTY_FLAGS( WindowStyle , wxScrollBarStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
79 wxEND_PROPERTIES_TABLE()
80
81 wxBEGIN_HANDLERS_TABLE(wxScrollBar)
82 wxEND_HANDLERS_TABLE()
83
84 wxCONSTRUCTOR_5( wxScrollBar , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
85 #else
86 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
87 #endif
88
89 // Scrollbar
90 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
91 const wxPoint& pos,
92 const wxSize& size, long style,
93 const wxValidator& validator,
94 const wxString& name)
95 {
96 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
97 return false;
98
99 if (!MSWCreateControl(wxT("ScrollBar"), wxEmptyString, pos, size))
100 return false;
101
102 SetScrollbar(0, 1, 2, 1, false);
103
104 return true;
105 }
106
107 wxScrollBar::~wxScrollBar(void)
108 {
109 }
110
111 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
112 WXWORD pos, WXHWND WXUNUSED(control))
113 {
114 // current and max positions
115 int position,
116 maxPos, trackPos = pos;
117
118 wxUnusedVar(trackPos);
119
120 // when we're dragging the scrollbar we can't use pos parameter because it
121 // is limited to 16 bits
122 // JACS: now always using GetScrollInfo, since there's no reason
123 // not to
124 // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
125 {
126 SCROLLINFO scrollInfo;
127 wxZeroMemory(scrollInfo);
128 scrollInfo.cbSize = sizeof(SCROLLINFO);
129
130 // also get the range if we call GetScrollInfo() anyhow -- this is less
131 // expensive than call it once here and then call GetScrollRange()
132 // below
133 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
134
135 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
136 {
137 wxLogLastError(_T("GetScrollInfo"));
138 }
139
140 trackPos = scrollInfo.nTrackPos;
141 position = scrollInfo.nPos;
142 maxPos = scrollInfo.nMax;
143 }
144 #if 0
145 else
146 {
147 position = ::GetScrollPos((HWND) control, SB_CTL);
148 int minPos;
149 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
150 }
151 #endif
152
153 #if defined(__WIN95__)
154 // A page size greater than one has the effect of reducing the effective
155 // range, therefore the range has already been boosted artificially - so
156 // reduce it again.
157 if ( m_pageSize > 1 )
158 maxPos -= (m_pageSize - 1);
159 #endif // __WIN95__
160
161 wxEventType scrollEvent = wxEVT_NULL;
162
163 int nScrollInc;
164 switch ( wParam )
165 {
166 case SB_BOTTOM:
167 nScrollInc = maxPos - position;
168 scrollEvent = wxEVT_SCROLL_TOP;
169 break;
170
171 case SB_TOP:
172 nScrollInc = -position;
173 scrollEvent = wxEVT_SCROLL_BOTTOM;
174 break;
175
176 case SB_LINEUP:
177 nScrollInc = -1;
178 scrollEvent = wxEVT_SCROLL_LINEUP;
179 break;
180
181 case SB_LINEDOWN:
182 nScrollInc = 1;
183 scrollEvent = wxEVT_SCROLL_LINEDOWN;
184 break;
185
186 case SB_PAGEUP:
187 nScrollInc = -GetPageSize();
188 scrollEvent = wxEVT_SCROLL_PAGEUP;
189 break;
190
191 case SB_PAGEDOWN:
192 nScrollInc = GetPageSize();
193 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
194 break;
195
196 case SB_THUMBPOSITION:
197 nScrollInc = trackPos - position;
198 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
199 break;
200
201 case SB_THUMBTRACK:
202 nScrollInc = trackPos - position;
203 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
204 break;
205
206 case SB_ENDSCROLL:
207 nScrollInc = 0;
208 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
209 break;
210
211 default:
212 nScrollInc = 0;
213 }
214
215 if ( nScrollInc )
216 {
217 position += nScrollInc;
218
219 if ( position < 0 )
220 position = 0;
221 if ( position > maxPos )
222 position = maxPos;
223
224 SetThumbPosition(position);
225 }
226 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
227 scrollEvent != wxEVT_SCROLL_ENDSCROLL )
228 {
229 // don't process the event if there is no displacement,
230 // unless this is a thumb release or end scroll event.
231 return false;
232 }
233
234 wxScrollEvent event(scrollEvent, m_windowId);
235 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
236 event.SetPosition(position);
237 event.SetEventObject( this );
238
239 return GetEventHandler()->ProcessEvent(event);
240 }
241
242 void wxScrollBar::SetThumbPosition(int viewStart)
243 {
244 #if defined(__WIN95__)
245 SCROLLINFO info;
246 info.cbSize = sizeof(SCROLLINFO);
247 info.nPage = 0;
248 info.nMin = 0;
249 info.nPos = viewStart;
250 info.fMask = SIF_POS ;
251
252 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
253 #else
254 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
255 #endif
256 }
257
258 int wxScrollBar::GetThumbPosition(void) const
259 {
260 SCROLLINFO scrollInfo;
261 wxZeroMemory(scrollInfo);
262 scrollInfo.cbSize = sizeof(SCROLLINFO);
263 scrollInfo.fMask = SIF_POS;
264
265 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
266 {
267 wxLogLastError(_T("GetScrollInfo"));
268 }
269 return scrollInfo.nPos;
270 // return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
271 }
272
273 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
274 bool refresh)
275 {
276 m_viewSize = pageSize;
277 m_pageSize = thumbSize;
278 m_objectSize = range;
279
280 // The range (number of scroll steps) is the
281 // object length minus the page size.
282 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
283
284 #if defined(__WIN95__)
285 // Try to adjust the range to cope with page size > 1
286 // (see comment for SetPageLength)
287 if ( m_pageSize > 1 )
288 {
289 range1 += (m_pageSize - 1);
290 }
291
292 SCROLLINFO info;
293 info.cbSize = sizeof(SCROLLINFO);
294 info.nPage = m_pageSize;
295 info.nMin = 0;
296 info.nMax = range1;
297 info.nPos = position;
298
299 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
300
301 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
302 #else
303 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, refresh);
304 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, refresh);
305 #endif
306 }
307
308 void wxScrollBar::Command(wxCommandEvent& event)
309 {
310 SetThumbPosition(event.GetInt());
311 ProcessCommand(event);
312 }
313
314 wxSize wxScrollBar::DoGetBestSize() const
315 {
316 int w = 100;
317 int h = 100;
318
319 if ( IsVertical() )
320 {
321 w = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
322 }
323 else
324 {
325 h = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
326 }
327
328 return wxSize(w, h);
329 }
330
331 WXDWORD wxScrollBar::MSWGetStyle(long style, WXDWORD *exstyle) const
332 {
333 // we never have an external border
334 WXDWORD msStyle = wxControl::MSWGetStyle
335 (
336 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
337 );
338
339 // SBS_HORZ is 0 anyhow, but do mention it explicitly for clarity
340 msStyle |= style & wxSB_HORIZONTAL ? SBS_HORZ : SBS_VERT;
341
342 return msStyle;
343 }
344
345 #endif // wxUSE_SCROLLBAR