]>
git.saurik.com Git - wxWidgets.git/blob - src/common/mousemanager.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/mousemanager.cpp
3 // Purpose: implementation of wxMouseEventsManager class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/settings.h"
28 #include "wx/window.h"
31 #include "wx/mousemanager.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 BEGIN_EVENT_TABLE(wxMouseEventsManager
, wxEvtHandler
)
38 EVT_MOUSE_CAPTURE_LOST(wxMouseEventsManager::OnCaptureLost
)
39 EVT_LEFT_DOWN(wxMouseEventsManager::OnLeftDown
)
40 EVT_LEFT_UP(wxMouseEventsManager::OnLeftUp
)
41 EVT_MOTION(wxMouseEventsManager::OnMove
)
44 // ============================================================================
45 // wxMouseEventsManager implementation
46 // ============================================================================
48 void wxMouseEventsManager::Init()
51 m_state
= State_Normal
;
55 bool wxMouseEventsManager::Create(wxWindow
*win
)
57 wxASSERT_MSG( !m_win
, "Create() must not be called twice" );
60 win
->PushEventHandler(this);
65 wxMouseEventsManager::~wxMouseEventsManager()
68 m_win
->RemoveEventHandler(this);
71 void wxMouseEventsManager::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
76 wxFAIL_MSG( "mouse shouldn't be captured in normal state" );
80 MouseClickCancelled(m_item
);
84 MouseDragCancelled(m_item
);
88 m_state
= State_Normal
;
92 void wxMouseEventsManager::OnLeftDown(wxMouseEvent
& event
)
94 wxASSERT_MSG( m_state
== State_Normal
,
95 "state hasn't been reset to normal somehow" );
97 m_posLast
= event
.GetPosition();
98 m_item
= MouseHitTest(m_posLast
);
99 if ( m_item
== wxNOT_FOUND
)
105 m_state
= State_Pressed
;
107 m_win
->CaptureMouse();
108 MouseClickBegin(m_item
);
111 void wxMouseEventsManager::OnLeftUp(wxMouseEvent
& event
)
116 // ignore it, the mouse hasn't been pressed over any item initially
117 // so releasing it shouldn't do anything
120 // skip releasing the capture below
124 if ( MouseHitTest(event
.GetPosition()) == m_item
)
126 // mouse released over the same item, so it was a click
127 MouseClicked(m_item
);
132 MouseDragEnd(m_item
, event
.GetPosition());
136 m_state
= State_Normal
;
137 m_item
= wxNOT_FOUND
;
138 m_win
->ReleaseMouse();
141 void wxMouseEventsManager::OnMove(wxMouseEvent
& event
)
150 wxASSERT_MSG( event
.LeftIsDown(),
151 "should have detected mouse being released" );
154 // it's probably a bad idea to query the system for these
155 // values every time the mouse is moved so cache them on the
156 // assumption that they don't change -- which is wrong, of
157 // course, the user can change them but it doesn't happen often
159 dragMinX
= wxSystemSettings::GetMetric(wxSYS_DRAG_X
);
161 dragMinY
= wxSystemSettings::GetMetric(wxSYS_DRAG_Y
);
163 const wxPoint
& pos
= event
.GetPosition();
164 const wxPoint ofs
= pos
- m_posLast
;
165 if ( abs(ofs
.x
) > dragMinX
|| abs(ofs
.y
) > dragMinY
)
167 // the mouse left the rectangle inside which its movements
168 // are considered to be too small to constitute a start of
169 // drag operation, do [attempt to] start it now
170 if ( MouseDragBegin(m_item
, pos
) )
172 m_state
= State_Dragging
;
175 else // still didn't move far enough away
183 m_posLast
= event
.GetPosition();
184 MouseDragging(m_item
, m_posLast
);