1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/scrthumb.cpp
3 // Purpose: wxScrollThumb and related classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
27 #include "wx/window.h"
28 #include "wx/renderer.h"
31 #include "wx/univ/scrtimer.h"
32 #include "wx/univ/scrthumb.h"
34 // ----------------------------------------------------------------------------
35 // wxScrollThumbCaptureData: the struct used while dragging the scroll thumb
36 // ----------------------------------------------------------------------------
38 struct WXDLLEXPORT wxScrollThumbCaptureData
40 // start mouse capture after the user clicked the mouse button btn on this
41 // part of the control
42 wxScrollThumbCaptureData(wxScrollThumb::Shaft part
,
44 wxControlWithThumb
*control
)
50 m_window
= control
->GetWindow();
51 m_window
->CaptureMouse();
55 ~wxScrollThumbCaptureData()
59 m_window
->ReleaseMouse();
65 // the thumb part being held pressed
66 wxScrollThumb::Shaft m_shaftPart
;
68 // the mouse button which started the capture (-1 if none)
71 // the window which has captured the mouse
74 // the offset between the mouse position and the start of the thumb which
75 // is kept constant while dragging the thumb
78 // the timer for generating the scroll events when scrolling by page
79 wxScrollTimer
*m_timerScroll
;
82 // ----------------------------------------------------------------------------
83 // wxScrollTimer: the timer used when the arrow is kept pressed
84 // ----------------------------------------------------------------------------
86 class wxScrollThumbTimer
: public wxScrollTimer
89 wxScrollThumbTimer(wxControlWithThumb
*control
,
90 wxScrollThumb::Shaft shaftPart
)
95 case wxScrollThumb::Shaft_Above
:
100 wxFAIL_MSG(_T("unexpected shaft part in wxScrollThumbTimer"));
103 case wxScrollThumb::Shaft_Below
:
108 m_control
->OnPageScrollStart();
114 virtual bool DoNotify()
116 return m_control
->OnPageScroll(m_inc
);
119 wxControlWithThumb
*m_control
;
123 // ============================================================================
125 // ============================================================================
127 // ----------------------------------------------------------------------------
128 // wxScrollThumb constructor and dtor
129 // ----------------------------------------------------------------------------
131 wxScrollThumb::wxScrollThumb(wxControlWithThumb
*control
)
133 m_shaftPart
= Shaft_None
;
135 m_captureData
= NULL
;
138 wxScrollThumb::~wxScrollThumb()
140 // it should have been destroyed
141 wxASSERT_MSG( !m_captureData
, _T("memory leak in wxScrollThumb") );
144 // ----------------------------------------------------------------------------
145 // wxScrollThumb mouse handling
146 // ----------------------------------------------------------------------------
148 bool wxScrollThumb::HandleMouse(const wxMouseEvent
& event
) const
150 // is this a click event?
151 int btn
= event
.GetButton();
158 // when the mouse is pressed on any scrollbar element, we capture it
159 // and hold capture until the same mouse button is released
160 if ( event
.ButtonDown() || event
.ButtonDClick() )
164 // mouse already captured, nothing to do
168 // determine which part of the window the user clicked in
169 Shaft shaftPart
= m_control
->HitTest(event
.GetPosition());
171 if ( shaftPart
== Shaft_None
)
173 // mouse pressed over something else
178 wxConstCast(this, wxScrollThumb
)->m_captureData
=
179 new wxScrollThumbCaptureData(shaftPart
, btn
, m_control
);
181 // modify the visual appearance before sending the event which will
183 m_control
->SetShaftPartState(shaftPart
, wxCONTROL_PRESSED
);
185 if ( shaftPart
== Shaft_Thumb
)
187 // save the mouse offset from the thumb position - we will keep it
188 // constant while dragging the thumb
189 m_captureData
->m_ofsMouse
=
190 GetMouseCoord(event
) - m_control
->ThumbPosToPixel();
192 // generate an additional event if we start dragging the thumb
193 m_control
->OnThumbDragStart(GetThumbPos(event
));
195 else // not the thumb
197 // start timer for auto scrolling when the user presses the mouse
198 // in the shaft above or below the thumb
199 m_captureData
->m_timerScroll
=
200 new wxScrollThumbTimer(m_control
, shaftPart
);
203 // release mouse if the *same* button went up
204 else if ( HasCapture() && (btn
== m_captureData
->m_btnCapture
) )
206 Shaft shaftPart
= m_captureData
->m_shaftPart
;
208 // if we were dragging the thumb, send the one last event
209 if ( shaftPart
== Shaft_Thumb
)
211 m_control
->OnThumbDragEnd(GetThumbPos(event
));
214 // release the mouse and free capture data
215 delete m_captureData
;
216 wxConstCast(this, wxScrollThumb
)->m_captureData
= NULL
;
218 m_control
->SetShaftPartState(shaftPart
, wxCONTROL_PRESSED
, false);
220 else // another mouse button released
222 // we don't process this
229 bool wxScrollThumb::HandleMouseMove(const wxMouseEvent
& event
) const
233 if ( (m_captureData
->m_shaftPart
== Shaft_Thumb
) && event
.Moving() )
235 // make the thumb follow the mouse by keeping the same offset
236 // between the mouse position and the top/left of the thumb
237 m_control
->OnThumbDrag(GetThumbPos(event
));
240 // we process all mouse events while the mouse is captured by us
246 if ( event
.Leaving() )
248 // no part of the shaft has mouse if it left the window completely
249 shaftPart
= Shaft_None
;
251 else // Moving() or Entering(), treat them the same here
253 shaftPart
= m_control
->HitTest(event
.GetPosition());
256 if ( shaftPart
!= m_shaftPart
)
258 // update the highlighted state
259 m_control
->SetShaftPartState(m_shaftPart
, wxCONTROL_CURRENT
, false);
260 wxConstCast(this, wxScrollThumb
)->m_shaftPart
= shaftPart
;
261 m_control
->SetShaftPartState(m_shaftPart
, wxCONTROL_CURRENT
, true);
264 // if the event happened on the shaft, it was for us and we processed
266 return shaftPart
!= Shaft_None
;
270 wxCoord
wxScrollThumb::GetMouseCoord(const wxMouseEvent
& event
) const
272 wxPoint pt
= event
.GetPosition();
273 return m_control
->IsVertical() ? pt
.y
: pt
.x
;
276 int wxScrollThumb::GetThumbPos(const wxMouseEvent
& event
) const
278 wxCHECK_MSG( m_captureData
&& m_captureData
->m_shaftPart
== Shaft_Thumb
, 0,
279 _T("can't be called when thumb is not dragged") );
281 int x
= GetMouseCoord(event
) - m_captureData
->m_ofsMouse
;
282 return m_control
->PixelToThumbPos(x
);