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_EVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_ENDSCROLL
, wxScrollEvent
)
73 WX_PROPERTY( ThumbPosition
, int , SetThumbPosition
, GetThumbPosition
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
74 WX_PROPERTY( Range
, int , SetRange
, GetRange
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
75 WX_PROPERTY( ThumbSize
, int , SetThumbSize
, GetThumbSize
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
76 WX_PROPERTY( PageSize
, int , SetPageSize
, GetPageSize
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 WX_PROPERTY_FLAGS( WindowStyle
, wxScrollBarStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
78 WX_END_PROPERTIES_TABLE()
80 WX_BEGIN_HANDLERS_TABLE(wxScrollBar
)
81 WX_END_HANDLERS_TABLE()
83 WX_CONSTRUCTOR_5( wxScrollBar
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
85 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
89 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
91 const wxSize
& size
, long style
,
92 const wxValidator
& validator
,
97 parent
->AddChild(this);
100 SetValidator(validator
);
101 #endif // wxUSE_VALIDATORS
103 if ((style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
104 style
|= wxNO_BORDER
;
106 SetBackgroundColour(parent
->GetBackgroundColour()) ;
107 SetForegroundColour(parent
->GetForegroundColour()) ;
108 m_windowStyle
= style
;
111 m_windowId
= (int)NewControlId();
122 if (style
& wxHORIZONTAL
)
129 if (style
& wxVERTICAL
)
136 WXDWORD wstyle
= MSWGetStyle(style
, & exStyle
) ;
138 // Now create scrollbar
139 DWORD _direction
= (style
& wxHORIZONTAL
) ?
141 HWND scroll_bar
= CreateWindowEx(exStyle
, wxT("SCROLLBAR"), wxT("scrollbar"),
143 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
144 wxGetInstance(), NULL
);
150 ::SetScrollRange(scroll_bar
, SB_CTL
, 0, 1, FALSE
);
151 ::SetScrollPos(scroll_bar
, SB_CTL
, 0, FALSE
);
152 ShowWindow(scroll_bar
, SW_SHOW
);
154 SetFont(parent
->GetFont());
156 m_hWnd
= (WXHWND
)scroll_bar
;
158 // Subclass again for purposes of dialog editing mode
159 SubclassWin((WXHWND
) scroll_bar
);
161 SetSize(x
, y
, width
, height
);
166 wxScrollBar::~wxScrollBar(void)
170 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
171 WXWORD pos
, WXHWND
WXUNUSED(control
))
173 // current and max positions
175 maxPos
, trackPos
= pos
;
177 // when we're dragging the scrollbar we can't use pos parameter because it
178 // is limited to 16 bits
179 // JACS: now always using GetScrollInfo, since there's no reason
181 // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
183 SCROLLINFO scrollInfo
;
184 wxZeroMemory(scrollInfo
);
185 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
187 // also get the range if we call GetScrollInfo() anyhow -- this is less
188 // expensive than call it once here and then call GetScrollRange()
190 scrollInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_TRACKPOS
;
192 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
194 wxLogLastError(_T("GetScrollInfo"));
197 trackPos
= scrollInfo
.nTrackPos
;
198 position
= scrollInfo
.nPos
;
199 maxPos
= scrollInfo
.nMax
;
204 position
= ::GetScrollPos((HWND
) control
, SB_CTL
);
206 ::GetScrollRange((HWND
) control
, SB_CTL
, &minPos
, &maxPos
);
210 #if defined(__WIN95__)
211 // A page size greater than one has the effect of reducing the effective
212 // range, therefore the range has already been boosted artificially - so
214 if ( m_pageSize
> 1 )
215 maxPos
-= (m_pageSize
- 1);
218 wxEventType scrollEvent
= wxEVT_NULL
;
224 nScrollInc
= maxPos
- position
;
225 scrollEvent
= wxEVT_SCROLL_TOP
;
229 nScrollInc
= -position
;
230 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
235 scrollEvent
= wxEVT_SCROLL_LINEUP
;
240 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
244 nScrollInc
= -GetPageSize();
245 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
249 nScrollInc
= GetPageSize();
250 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
253 case SB_THUMBPOSITION
:
254 nScrollInc
= trackPos
- position
;
255 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
259 nScrollInc
= trackPos
- position
;
260 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
265 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
274 position
+= nScrollInc
;
278 if ( position
> maxPos
)
281 SetThumbPosition(position
);
283 else if ( scrollEvent
!= wxEVT_SCROLL_THUMBRELEASE
&&
284 scrollEvent
!= wxEVT_SCROLL_ENDSCROLL
)
286 // don't process the event if there is no displacement,
287 // unless this is a thumb release or end scroll event.
291 wxScrollEvent
event(scrollEvent
, m_windowId
);
292 event
.SetOrientation(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
293 event
.SetPosition(position
);
294 event
.SetEventObject( this );
296 return GetEventHandler()->ProcessEvent(event
);
299 void wxScrollBar::SetThumbPosition(int viewStart
)
301 #if defined(__WIN95__)
303 info
.cbSize
= sizeof(SCROLLINFO
);
306 info
.nPos
= viewStart
;
307 info
.fMask
= SIF_POS
;
309 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, TRUE
);
311 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, viewStart
, TRUE
);
315 int wxScrollBar::GetThumbPosition(void) const
317 SCROLLINFO scrollInfo
;
318 wxZeroMemory(scrollInfo
);
319 scrollInfo
.cbSize
= sizeof(SCROLLINFO
);
320 scrollInfo
.fMask
= SIF_POS
;
322 if ( !::GetScrollInfo(GetHwnd(), SB_CTL
, &scrollInfo
) )
324 wxLogLastError(_T("GetScrollInfo"));
326 return scrollInfo
.nPos
;
327 // return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
330 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
,
333 m_viewSize
= pageSize
;
334 m_pageSize
= thumbSize
;
335 m_objectSize
= range
;
337 // The range (number of scroll steps) is the
338 // object length minus the page size.
339 int range1
= wxMax((m_objectSize
- m_pageSize
), 0) ;
341 #if defined(__WIN95__)
342 // Try to adjust the range to cope with page size > 1
343 // (see comment for SetPageLength)
344 if ( m_pageSize
> 1 )
346 range1
+= (m_pageSize
- 1);
350 info
.cbSize
= sizeof(SCROLLINFO
);
351 info
.nPage
= m_pageSize
;
354 info
.nPos
= position
;
356 info
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
358 ::SetScrollInfo((HWND
) GetHWND(), SB_CTL
, &info
, refresh
);
360 ::SetScrollPos((HWND
)m_hWnd
, SB_CTL
, position
, TRUE
);
361 ::SetScrollRange((HWND
)m_hWnd
, SB_CTL
, 0, range1
, TRUE
);
366 WXHBRUSH
wxScrollBar::OnCtlColor(WXHDC
WXUNUSED(pDC
), WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
367 WXUINT
WXUNUSED(message
), WXWPARAM
WXUNUSED(wParam
), WXLPARAM
WXUNUSED(lParam
))
372 void wxScrollBar::Command(wxCommandEvent
& event
)
374 SetThumbPosition(event
.m_commandInt
);
375 ProcessCommand(event
);
378 #endif // wxUSE_SCROLLBAR