1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/scrolbar.cpp
3 // Purpose: wxScrollBar
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "scrolbar.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/scrolbar.h"
30 #include "wx/msw/private.h"
32 #if wxUSE_EXTENDED_RTTI
33 IMPLEMENT_DYNAMIC_CLASS_XTI(wxScrollBar
, wxControl
,"wx/scrolbar.h")
35 WX_BEGIN_PROPERTIES_TABLE(wxScrollBar
)
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"))
40 WX_END_PROPERTIES_TABLE()
42 WX_BEGIN_HANDLERS_TABLE(wxScrollBar
)
43 WX_END_HANDLERS_TABLE()
45 WX_CONSTRUCTOR_5( wxScrollBar
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
47 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
51 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
53 const wxSize
& size
, long style
,
54 const wxValidator
& validator
,
59 parent
->AddChild(this);
62 SetValidator(validator
);
63 #endif // wxUSE_VALIDATORS
65 if ((style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
68 SetBackgroundColour(parent
->GetBackgroundColour()) ;
69 SetForegroundColour(parent
->GetForegroundColour()) ;
70 m_windowStyle
= style
;
73 m_windowId
= (int)NewControlId();
84 if (style
& wxHORIZONTAL
)
91 if (style
& wxVERTICAL
)
98 WXDWORD wstyle
= MSWGetStyle(style
, & exStyle
) ;
100 // Now create scrollbar
101 DWORD _direction
= (style
& wxHORIZONTAL
) ?
103 HWND scroll_bar
= CreateWindowEx(exStyle
, wxT("SCROLLBAR"), wxT("scrollbar"),
105 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
106 wxGetInstance(), NULL
);
112 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
113 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
114 ShowWindow(scroll_bar
, SW_SHOW
);
116 SetFont(parent
->GetFont());
118 m_hWnd
= (WXHWND
)scroll_bar
;
120 // Subclass again for purposes of dialog editing mode
121 SubclassWin((WXHWND
) scroll_bar
);
123 SetSize(x
, y
, width
, height
);
128 wxScrollBar::~wxScrollBar(void)
132 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
133 WXWORD pos
, WXHWND
WXUNUSED(control
))
135 // current and max positions
137 maxPos
, trackPos
= pos
;
139 // when we're dragging the scrollbar we can't use pos parameter because it
140 // is limited to 16 bits
141 // JACS: now always using GetScrollInfo, since there's no reason
143 // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
145 SCROLLINFO scrollInfo
;
146 wxZeroMemory(scrollInfo
);
147 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
149 // also get the range if we call GetScrollInfo() anyhow -- this is less
150 // expensive than call it once here and then call GetScrollRange()
152 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
154 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
156 wxLogLastError(_T("GetScrollInfo"));
159 trackPos
= scrollInfo
.nTrackPos
;
160 position
= scrollInfo
.nPos
;
161 maxPos
= scrollInfo
.nMax
;
166 position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
168 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
172 #if defined(__WIN95__)
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
176 if ( m_pageSize
> 1 )
177 maxPos
-= (m_pageSize
- 1);
180 wxEventType scrollEvent
= wxEVT_NULL
;
186 nScrollInc
= maxPos
- position
;
187 scrollEvent
= wxEVT_SCROLL_TOP
;
191 nScrollInc
= -position
;
192 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
197 scrollEvent
= wxEVT_SCROLL_LINEUP
;
202 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
206 nScrollInc
= -GetPageSize();
207 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
211 nScrollInc
= GetPageSize();
212 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
215 case SB_THUMBPOSITION
:
216 nScrollInc
= trackPos
- position
;
217 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
221 nScrollInc
= trackPos
- position
;
222 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
227 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
236 position
+= nScrollInc
;
240 if ( position
> maxPos
)
243 SetThumbPosition(position
);
245 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
246 scrollEvent
!= wxEVT_SCROLL_ENDSCROLL
)
248 // don't process the event if there is no displacement,
249 // unless this is a thumb release or end scroll event.
253 wxScrollEvent
event(scrollEvent
, m_windowId
);
254 event
.SetOrientation(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
255 event
.SetPosition(position
);
256 event
.SetEventObject( this );
258 return GetEventHandler()->ProcessEvent(event
);
261 void wxScrollBar::SetThumbPosition(int viewStart
)
263 #if defined(__WIN95__)
265 info
.cbSize
= sizeof(SCROLLINFO
);
268 info
.nPos
= viewStart
;
269 info
.fMask
= SIF_POS
;
271 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
273 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
277 int wxScrollBar::GetThumbPosition(void) const
279 SCROLLINFO scrollInfo
;
280 wxZeroMemory(scrollInfo
);
281 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
282 scrollInfo
.fMask
= SIF_POS
;
284 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
286 wxLogLastError(_T("GetScrollInfo"));
288 return scrollInfo
.nPos
;
289 // return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
292 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
295 m_viewSize
= pageSize
;
296 m_pageSize
= thumbSize
;
297 m_objectSize
= range
;
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) ;
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 )
308 range1
+= (m_pageSize
- 1);
312 info
.cbSize
= sizeof(SCROLLINFO
);
313 info
.nPage
= m_pageSize
;
316 info
.nPos
= position
;
318 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
320 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
322 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
323 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
328 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
329 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
334 void wxScrollBar::Command(wxCommandEvent
& event
)
336 SetThumbPosition(event
.m_commandInt
);
337 ProcessCommand(event
);
340 #endif // wxUSE_SCROLLBAR