1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/scrthumb.cpp
3 // Purpose: wxScrollThumb and related classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "univscrthumb.h"
24 #include "wx/wxprec.h"
31 #include "wx/window.h"
34 #include "wx/univ/scrtimer.h"
35 #include "wx/univ/scrthumb.h"
37 // ----------------------------------------------------------------------------
38 // wxScrollThumbCaptureData: the struct used while dragging the scroll thumb
39 // ----------------------------------------------------------------------------
41 struct WXDLLEXPORT wxScrollThumbCaptureData
43 // start mouse capture after the user clicked the mouse button btn on this
44 // part of the control
45 wxScrollThumbCaptureData(wxScrollThumb::Shaft part
,
47 wxControlWithThumb
*control
)
53 m_window
= control
->GetWindow();
54 m_window
->CaptureMouse();
58 ~wxScrollThumbCaptureData()
62 m_window
->ReleaseMouse();
68 // the thumb part being held pressed
69 wxScrollThumb::Shaft m_shaftPart
;
71 // the mouse button which started the capture (-1 if none)
74 // the window which has captured the mouse
77 // the offset between the mouse position and the start of the thumb which
78 // is kept constant while dragging the thumb
81 // the timer for generating the scroll events when scrolling by page
82 wxScrollTimer
*m_timerScroll
;
85 // ----------------------------------------------------------------------------
86 // wxScrollTimer: the timer used when the arrow is kept pressed
87 // ----------------------------------------------------------------------------
89 class wxScrollThumbTimer
: public wxScrollTimer
92 wxScrollThumbTimer(wxControlWithThumb
*control
,
93 wxScrollThumb::Shaft shaftPart
)
98 case wxScrollThumb::Shaft_Above
:
103 wxFAIL_MSG(_T("unexpected shaft part in wxScrollThumbTimer"));
106 case wxScrollThumb::Shaft_Below
:
111 m_control
->OnPageScrollStart();
117 virtual bool DoNotify()
119 return m_control
->OnPageScroll(m_inc
);
122 wxControlWithThumb
*m_control
;
126 // ============================================================================
128 // ============================================================================
130 // ----------------------------------------------------------------------------
131 // wxScrollThumb constructor and dtor
132 // ----------------------------------------------------------------------------
134 wxScrollThumb::wxScrollThumb(wxControlWithThumb
*control
)
136 m_shaftPart
= Shaft_None
;
138 m_captureData
= NULL
;
141 wxScrollThumb::~wxScrollThumb()
143 // it should have been destroyed
144 wxASSERT_MSG( !m_captureData
, _T("memory leak in wxScrollThumb") );
147 // ----------------------------------------------------------------------------
148 // wxScrollThumb mouse handling
149 // ----------------------------------------------------------------------------
151 bool wxScrollThumb::HandleMouse(const wxMouseEvent
& event
) const
153 // is this a click event?
154 int btn
= event
.GetButton();
161 // when the mouse is pressed on any scrollbar element, we capture it
162 // and hold capture until the same mouse button is released
163 if ( event
.ButtonDown() || event
.ButtonDClick() )
167 // mouse already captured, nothing to do
171 // determine which part of the window the user clicked in
172 Shaft shaftPart
= m_control
->HitTest(event
.GetPosition());
174 if ( shaftPart
== Shaft_None
)
176 // mouse pressed over something else
181 wxConstCast(this, wxScrollThumb
)->m_captureData
=
182 new wxScrollThumbCaptureData(shaftPart
, btn
, m_control
);
184 // modify the visual appearance before sending the event which will
186 m_control
->SetShaftPartState(shaftPart
, wxCONTROL_PRESSED
);
188 if ( shaftPart
== Shaft_Thumb
)
190 // save the mouse offset from the thumb position - we will keep it
191 // constant while dragging the thumb
192 m_captureData
->m_ofsMouse
=
193 GetMouseCoord(event
) - m_control
->ThumbPosToPixel();
195 // generate an additional event if we start dragging the thumb
196 m_control
->OnThumbDragStart(GetThumbPos(event
));
198 else // not the thumb
200 // start timer for auto scrolling when the user presses the mouse
201 // in the shaft above or below the thumb
202 m_captureData
->m_timerScroll
=
203 new wxScrollThumbTimer(m_control
, shaftPart
);
206 // release mouse if the *same* button went up
207 else if ( HasCapture() && (btn
== m_captureData
->m_btnCapture
) )
209 Shaft shaftPart
= m_captureData
->m_shaftPart
;
211 // if we were dragging the thumb, send the one last event
212 if ( shaftPart
== Shaft_Thumb
)
214 m_control
->OnThumbDragEnd(GetThumbPos(event
));
217 // release the mouse and free capture data
218 delete m_captureData
;
219 wxConstCast(this, wxScrollThumb
)->m_captureData
= NULL
;
221 m_control
->SetShaftPartState(shaftPart
, wxCONTROL_PRESSED
, FALSE
);
223 else // another mouse button released
225 // we don't process this
232 bool wxScrollThumb::HandleMouseMove(const wxMouseEvent
& event
) const
236 if ( (m_captureData
->m_shaftPart
== Shaft_Thumb
) && event
.Moving() )
238 // make the thumb follow the mouse by keeping the same offset
239 // between the mouse position and the top/left of the thumb
240 m_control
->OnThumbDrag(GetThumbPos(event
));
243 // we process all mouse events while the mouse is captured by us
249 if ( event
.Leaving() )
251 // no part of the shaft has mouse if it left the window completely
252 shaftPart
= Shaft_None
;
254 else // Moving() or Entering(), treat them the same here
256 shaftPart
= m_control
->HitTest(event
.GetPosition());
259 if ( shaftPart
!= m_shaftPart
)
261 // update the highlighted state
262 m_control
->SetShaftPartState(m_shaftPart
, wxCONTROL_CURRENT
, FALSE
);
263 wxConstCast(this, wxScrollThumb
)->m_shaftPart
= shaftPart
;
264 m_control
->SetShaftPartState(m_shaftPart
, wxCONTROL_CURRENT
, TRUE
);
267 // if the event happened on the shaft, it was for us and we processed
269 return shaftPart
!= Shaft_None
;
273 wxCoord
wxScrollThumb::GetMouseCoord(const wxMouseEvent
& event
) const
275 wxPoint pt
= event
.GetPosition();
276 return m_control
->IsVertical() ? pt
.y
: pt
.x
;
279 int wxScrollThumb::GetThumbPos(const wxMouseEvent
& event
) const
281 wxCHECK_MSG( m_captureData
&& m_captureData
->m_shaftPart
== Shaft_Thumb
, 0,
282 _T("can't be called when thumb is not dragged") );
284 int x
= GetMouseCoord(event
) - m_captureData
->m_ofsMouse
;
285 return m_control
->PixelToThumbPos(x
);