]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
fixes for new lib dirs for wxMSW
[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$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
f0a126fe
SC
32#if wxUSE_EXTENDED_RTTI
33IMPLEMENT_DYNAMIC_CLASS_XTI(wxScrollBar, wxControl,"wx/scrolbar.h")
34
35WX_BEGIN_PROPERTIES_TABLE(wxScrollBar)
067e9be6
SC
36 WX_PROPERTY( ThumbPosition , int , SetThumbPosition, GetThumbPosition, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
37 WX_PROPERTY( Range , int , SetRange, GetRange, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
38 WX_PROPERTY( ThumbSize , int , SetThumbSize, GetThumbSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
39 WX_PROPERTY( PageSize , int , SetPageSize, GetPageSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
f0a126fe 40WX_END_PROPERTIES_TABLE()
2bda0e17 41
f0a126fe
SC
42WX_BEGIN_HANDLERS_TABLE(wxScrollBar)
43WX_END_HANDLERS_TABLE()
066f1b7a 44
f0a126fe
SC
45WX_CONSTRUCTOR_5( wxScrollBar , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
46#else
47IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
48#endif
2bda0e17
KB
49
50// Scrollbar
debe6624 51bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
2bda0e17 52 const wxPoint& pos,
debe6624 53 const wxSize& size, long style,
2bda0e17
KB
54 const wxValidator& validator,
55 const wxString& name)
56{
57 if (!parent)
58 return FALSE;
59 parent->AddChild(this);
60 SetName(name);
11b6a93b 61#if wxUSE_VALIDATORS
a23fd0e1 62 SetValidator(validator);
11b6a93b 63#endif // wxUSE_VALIDATORS
33ac7e6f 64
7051e6c7
JS
65 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
66 style |= wxNO_BORDER;
67
fd71308f
JS
68 SetBackgroundColour(parent->GetBackgroundColour()) ;
69 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
70 m_windowStyle = style;
71
72 if ( id == -1 )
a23fd0e1 73 m_windowId = (int)NewControlId();
2bda0e17 74 else
a23fd0e1 75 m_windowId = id;
2bda0e17
KB
76
77 int x = pos.x;
78 int y = pos.y;
79 int width = size.x;
80 int height = size.y;
81
82 if (width == -1)
83 {
84 if (style & wxHORIZONTAL)
85 width = 140;
86 else
87 width = 14;
88 }
89 if (height == -1)
90 {
91 if (style & wxVERTICAL)
92 height = 140;
93 else
94 height = 14;
95 }
96
fe3d9123
JS
97 WXDWORD exStyle = 0;
98 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
b0766406 99
2bda0e17
KB
100 // Now create scrollbar
101 DWORD _direction = (style & wxHORIZONTAL) ?
102 SBS_HORZ: SBS_VERT;
fe3d9123 103 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
b0766406 104 _direction | wstyle,
2bda0e17
KB
105 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
106 wxGetInstance(), NULL);
107
108 m_pageSize = 1;
109 m_viewSize = 1;
110 m_objectSize = 1;
111
112 ::SetScrollRange(scroll_bar, SB_CTL, 0, 1, FALSE);
113 ::SetScrollPos(scroll_bar, SB_CTL, 0, FALSE);
114 ShowWindow(scroll_bar, SW_SHOW);
115
c0ed460c 116 SetFont(parent->GetFont());
1c089c47 117
2bda0e17
KB
118 m_hWnd = (WXHWND)scroll_bar;
119
120 // Subclass again for purposes of dialog editing mode
121 SubclassWin((WXHWND) scroll_bar);
122
123 SetSize(x, y, width, height);
124
125 return TRUE;
126}
127
128wxScrollBar::~wxScrollBar(void)
129{
130}
131
a23fd0e1 132bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
2eb10e2a 133 WXWORD pos, WXHWND WXUNUSED(control))
2bda0e17 134{
2b5f62a0
VZ
135 // current and max positions
136 int position,
137 maxPos, trackPos = pos;
138
2b5f62a0
VZ
139 // when we're dragging the scrollbar we can't use pos parameter because it
140 // is limited to 16 bits
4676948b
JS
141 // JACS: now always using GetScrollInfo, since there's no reason
142 // not to
143// if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
2b5f62a0
VZ
144 {
145 SCROLLINFO scrollInfo;
146 wxZeroMemory(scrollInfo);
147 scrollInfo.cbSize = sizeof(SCROLLINFO);
148
149 // also get the range if we call GetScrollInfo() anyhow -- this is less
150 // expensive than call it once here and then call GetScrollRange()
151 // below
152 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
153
154 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
155 {
156 wxLogLastError(_T("GetScrollInfo"));
157 }
158
159 trackPos = scrollInfo.nTrackPos;
160 position = scrollInfo.nPos;
161 maxPos = scrollInfo.nMax;
162 }
4676948b 163#if 0
2b5f62a0 164 else
2b5f62a0
VZ
165 {
166 position = ::GetScrollPos((HWND) control, SB_CTL);
167 int minPos;
168 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
169 }
4676948b 170#endif
a23fd0e1 171
2bda0e17 172#if defined(__WIN95__)
a23fd0e1
VZ
173 // A page size greater than one has the effect of reducing the effective
174 // range, therefore the range has already been boosted artificially - so
175 // reduce it again.
176 if ( m_pageSize > 1 )
177 maxPos -= (m_pageSize - 1);
178#endif // __WIN95__
2bda0e17 179
7798a18e 180 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
181
182 int nScrollInc;
183 switch ( wParam )
184 {
a29ffc34 185 case SB_BOTTOM:
a23fd0e1
VZ
186 nScrollInc = maxPos - position;
187 scrollEvent = wxEVT_SCROLL_TOP;
188 break;
189
a29ffc34
VZ
190 case SB_TOP:
191 nScrollInc = -position;
a23fd0e1
VZ
192 scrollEvent = wxEVT_SCROLL_BOTTOM;
193 break;
194
195 case SB_LINEUP:
196 nScrollInc = -1;
197 scrollEvent = wxEVT_SCROLL_LINEUP;
198 break;
199
200 case SB_LINEDOWN:
201 nScrollInc = 1;
202 scrollEvent = wxEVT_SCROLL_LINEDOWN;
203 break;
204
205 case SB_PAGEUP:
206 nScrollInc = -GetPageSize();
207 scrollEvent = wxEVT_SCROLL_PAGEUP;
208 break;
209
210 case SB_PAGEDOWN:
211 nScrollInc = GetPageSize();
212 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
213 break;
214
a23fd0e1 215 case SB_THUMBPOSITION:
2b5f62a0 216 nScrollInc = trackPos - position;
7d56fb8f
GRG
217 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
218 break;
feda3011
GRG
219
220 case SB_THUMBTRACK:
2b5f62a0 221 nScrollInc = trackPos - position;
a23fd0e1
VZ
222 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
223 break;
224
e8b669d3
VZ
225 case SB_ENDSCROLL:
226 nScrollInc = 0;
227 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
228 break;
229
a23fd0e1
VZ
230 default:
231 nScrollInc = 0;
2bda0e17
KB
232 }
233
e8b669d3 234 if ( nScrollInc )
2bda0e17 235 {
e8b669d3 236 position += nScrollInc;
2bda0e17 237
e8b669d3
VZ
238 if ( position < 0 )
239 position = 0;
240 if ( position > maxPos )
241 position = maxPos;
a23fd0e1 242
e8b669d3
VZ
243 SetThumbPosition(position);
244 }
245 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
246 scrollEvent != wxEVT_SCROLL_ENDSCROLL )
247 {
248 // don't process the event if there is no displacement,
249 // unless this is a thumb release or end scroll event.
250 return FALSE;
251 }
a23fd0e1 252
a23fd0e1 253 wxScrollEvent event(scrollEvent, m_windowId);
27e229f5 254 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e8b669d3 255 event.SetPosition(position);
a23fd0e1
VZ
256 event.SetEventObject( this );
257
258 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
259}
260
4fabb575 261void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17
KB
262{
263#if defined(__WIN95__)
2b5f62a0
VZ
264 SCROLLINFO info;
265 info.cbSize = sizeof(SCROLLINFO);
266 info.nPage = 0;
267 info.nMin = 0;
268 info.nPos = viewStart;
269 info.fMask = SIF_POS ;
270
271 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
2bda0e17 272#else
2b5f62a0 273 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
2bda0e17
KB
274#endif
275}
276
4fabb575 277int wxScrollBar::GetThumbPosition(void) const
2bda0e17 278{
4676948b
JS
279 SCROLLINFO scrollInfo;
280 wxZeroMemory(scrollInfo);
281 scrollInfo.cbSize = sizeof(SCROLLINFO);
282 scrollInfo.fMask = SIF_POS;
f0a126fe 283
4676948b
JS
284 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
285 {
286 wxLogLastError(_T("GetScrollInfo"));
287 }
288 return scrollInfo.nPos;
289// return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
2bda0e17
KB
290}
291
debe6624
JS
292void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
293 bool refresh)
2bda0e17
KB
294{
295 m_viewSize = pageSize;
296 m_pageSize = thumbSize;
297 m_objectSize = range;
298
299 // The range (number of scroll steps) is the
300 // object length minus the page size.
301 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
302
303#if defined(__WIN95__)
304 // Try to adjust the range to cope with page size > 1
305 // (see comment for SetPageLength)
306 if ( m_pageSize > 1 )
307 {
a23fd0e1 308 range1 += (m_pageSize - 1);
2bda0e17
KB
309 }
310
311 SCROLLINFO info;
312 info.cbSize = sizeof(SCROLLINFO);
313 info.nPage = m_pageSize;
314 info.nMin = 0;
315 info.nMax = range1;
316 info.nPos = position;
317
318 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
319
320 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
321#else
322 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
323 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
324#endif
325}
326
327
33ac7e6f
KB
328WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
329 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
2bda0e17
KB
330{
331 return 0;
332}
333
334void wxScrollBar::Command(wxCommandEvent& event)
335{
ca5e9f67 336 SetThumbPosition(event.m_commandInt);
2bda0e17
KB
337 ProcessCommand(event);
338}
339
1e6feb95 340#endif // wxUSE_SCROLLBAR