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 WX_DEFINE_FLAGS( wxScrollBarStyle
)
35 WX_BEGIN_FLAGS( wxScrollBarStyle
)
36 // new style border flags, we put them first to
37 // use them for streaming out
38 WX_FLAGS_MEMBER(wxBORDER_SIMPLE
)
39 WX_FLAGS_MEMBER(wxBORDER_SUNKEN
)
40 WX_FLAGS_MEMBER(wxBORDER_DOUBLE
)
41 WX_FLAGS_MEMBER(wxBORDER_RAISED
)
42 WX_FLAGS_MEMBER(wxBORDER_STATIC
)
43 WX_FLAGS_MEMBER(wxBORDER_NONE
)
45 // old style border flags
46 WX_FLAGS_MEMBER(wxSIMPLE_BORDER
)
47 WX_FLAGS_MEMBER(wxSUNKEN_BORDER
)
48 WX_FLAGS_MEMBER(wxDOUBLE_BORDER
)
49 WX_FLAGS_MEMBER(wxRAISED_BORDER
)
50 WX_FLAGS_MEMBER(wxSTATIC_BORDER
)
51 WX_FLAGS_MEMBER(wxNO_BORDER
)
53 // standard window styles
54 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL
)
55 WX_FLAGS_MEMBER(wxCLIP_CHILDREN
)
56 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
57 WX_FLAGS_MEMBER(wxWANTS_CHARS
)
58 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
59 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB
)
60 WX_FLAGS_MEMBER(wxVSCROLL
)
61 WX_FLAGS_MEMBER(wxHSCROLL
)
63 WX_FLAGS_MEMBER(wxSB_HORIZONTAL
)
64 WX_FLAGS_MEMBER(wxSB_VERTICAL
)
66 WX_END_FLAGS( wxScrollBarStyle
)
68 IMPLEMENT_DYNAMIC_CLASS_XTI(wxScrollBar
, wxControl
,"wx/scrolbar.h")
70 WX_BEGIN_PROPERTIES_TABLE(wxScrollBar
)
71 WX_PROPERTY( ThumbPosition
, int , SetThumbPosition
, GetThumbPosition
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
72 WX_PROPERTY( Range
, int , SetRange
, GetRange
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
73 WX_PROPERTY( ThumbSize
, int , SetThumbSize
, GetThumbSize
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
74 WX_PROPERTY( PageSize
, int , SetPageSize
, GetPageSize
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
75 WX_PROPERTY_FLAGS( WindowStyle
, wxScrollBarStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
76 WX_END_PROPERTIES_TABLE()
78 WX_BEGIN_HANDLERS_TABLE(wxScrollBar
)
79 WX_END_HANDLERS_TABLE()
81 WX_CONSTRUCTOR_5( wxScrollBar
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
83 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
87 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
89 const wxSize
& size
, long style
,
90 const wxValidator
& validator
,
95 parent
->AddChild(this);
98 SetValidator(validator
);
99 #endif // wxUSE_VALIDATORS
101 if ((style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
102 style
|= wxNO_BORDER
;
104 SetBackgroundColour(parent
->GetBackgroundColour()) ;
105 SetForegroundColour(parent
->GetForegroundColour()) ;
106 m_windowStyle
= style
;
109 m_windowId
= (int)NewControlId();
120 if (style
& wxHORIZONTAL
)
127 if (style
& wxVERTICAL
)
134 WXDWORD wstyle
= MSWGetStyle(style
, & exStyle
) ;
136 // Now create scrollbar
137 DWORD _direction
= (style
& wxHORIZONTAL
) ?
139 HWND scroll_bar
= CreateWindowEx(exStyle
, wxT("SCROLLBAR"), wxT("scrollbar"),
141 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
142 wxGetInstance(), NULL
);
148 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
149 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
150 ShowWindow(scroll_bar
, SW_SHOW
);
152 SetFont(parent
->GetFont());
154 m_hWnd
= (WXHWND
)scroll_bar
;
156 // Subclass again for purposes of dialog editing mode
157 SubclassWin((WXHWND
) scroll_bar
);
159 SetSize(x
, y
, width
, height
);
164 wxScrollBar::~wxScrollBar(void)
168 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
169 WXWORD pos
, WXHWND
WXUNUSED(control
))
171 // current and max positions
173 maxPos
, trackPos
= pos
;
175 // when we're dragging the scrollbar we can't use pos parameter because it
176 // is limited to 16 bits
177 // JACS: now always using GetScrollInfo, since there's no reason
179 // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
181 SCROLLINFO scrollInfo
;
182 wxZeroMemory(scrollInfo
);
183 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
185 // also get the range if we call GetScrollInfo() anyhow -- this is less
186 // expensive than call it once here and then call GetScrollRange()
188 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
190 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
192 wxLogLastError(_T("GetScrollInfo"));
195 trackPos
= scrollInfo
.nTrackPos
;
196 position
= scrollInfo
.nPos
;
197 maxPos
= scrollInfo
.nMax
;
202 position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
204 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
208 #if defined(__WIN95__)
209 // A page size greater than one has the effect of reducing the effective
210 // range, therefore the range has already been boosted artificially - so
212 if ( m_pageSize
> 1 )
213 maxPos
-= (m_pageSize
- 1);
216 wxEventType scrollEvent
= wxEVT_NULL
;
222 nScrollInc
= maxPos
- position
;
223 scrollEvent
= wxEVT_SCROLL_TOP
;
227 nScrollInc
= -position
;
228 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
233 scrollEvent
= wxEVT_SCROLL_LINEUP
;
238 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
242 nScrollInc
= -GetPageSize();
243 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
247 nScrollInc
= GetPageSize();
248 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
251 case SB_THUMBPOSITION
:
252 nScrollInc
= trackPos
- position
;
253 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
257 nScrollInc
= trackPos
- position
;
258 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
263 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
272 position
+= nScrollInc
;
276 if ( position
> maxPos
)
279 SetThumbPosition(position
);
281 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
282 scrollEvent
!= wxEVT_SCROLL_ENDSCROLL
)
284 // don't process the event if there is no displacement,
285 // unless this is a thumb release or end scroll event.
289 wxScrollEvent
event(scrollEvent
, m_windowId
);
290 event
.SetOrientation(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
291 event
.SetPosition(position
);
292 event
.SetEventObject( this );
294 return GetEventHandler()->ProcessEvent(event
);
297 void wxScrollBar::SetThumbPosition(int viewStart
)
299 #if defined(__WIN95__)
301 info
.cbSize
= sizeof(SCROLLINFO
);
304 info
.nPos
= viewStart
;
305 info
.fMask
= SIF_POS
;
307 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
309 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
313 int wxScrollBar::GetThumbPosition(void) const
315 SCROLLINFO scrollInfo
;
316 wxZeroMemory(scrollInfo
);
317 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
318 scrollInfo
.fMask
= SIF_POS
;
320 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
322 wxLogLastError(_T("GetScrollInfo"));
324 return scrollInfo
.nPos
;
325 // return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
328 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
331 m_viewSize
= pageSize
;
332 m_pageSize
= thumbSize
;
333 m_objectSize
= range
;
335 // The range (number of scroll steps) is the
336 // object length minus the page size.
337 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
339 #if defined(__WIN95__)
340 // Try to adjust the range to cope with page size > 1
341 // (see comment for SetPageLength)
342 if ( m_pageSize
> 1 )
344 range1
+= (m_pageSize
- 1);
348 info
.cbSize
= sizeof(SCROLLINFO
);
349 info
.nPage
= m_pageSize
;
352 info
.nPos
= position
;
354 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
356 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
358 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
359 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
364 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
365 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
370 void wxScrollBar::Command(wxCommandEvent
& event
)
372 SetThumbPosition(event
.m_commandInt
);
373 ProcessCommand(event
);
376 #endif // wxUSE_SCROLLBAR