]>
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
;
106 m_win
->CaptureMouse();
107 MouseClickBegin(m_item
);
110 void wxMouseEventsManager::OnLeftUp(wxMouseEvent
& event
)
115 // ignore it, the mouse hasn't been pressed over any item initially
116 // so releasing it shouldn't do anything
119 // skip releasing the capture below
123 if ( MouseHitTest(event
.GetPosition()) == m_item
)
125 // mouse released over the same item, so it was a click
126 MouseClicked(m_item
);
131 MouseDragEnd(m_item
, event
.GetPosition());
135 m_state
= State_Normal
;
136 m_item
= wxNOT_FOUND
;
137 m_win
->ReleaseMouse();
140 void wxMouseEventsManager::OnMove(wxMouseEvent
& event
)
149 wxASSERT_MSG( event
.LeftIsDown(),
150 "should have detected mouse being released" );
153 // it's probably a bad idea to query the system for these
154 // values every time the mouse is moved so cache them on the
155 // assumption that they don't change -- which is wrong, of
156 // course, the user can change them but it doesn't happen often
158 dragMinX
= wxSystemSettings::GetMetric(wxSYS_DRAG_X
);
160 dragMinY
= wxSystemSettings::GetMetric(wxSYS_DRAG_Y
);
162 const wxPoint
& pos
= event
.GetPosition();
163 const wxPoint ofs
= pos
- m_posLast
;
164 if ( abs(ofs
.x
) > dragMinX
|| abs(ofs
.y
) > dragMinY
)
166 // the mouse left the rectangle inside which its movements
167 // are considered to be too small to constitute a start of
168 // drag operation, do [attempt to] start it now
169 if ( MouseDragBegin(m_item
, pos
) )
171 m_state
= State_Dragging
;
174 else // still didn't move far enough away
182 m_posLast
= event
.GetPosition();
183 MouseDragging(m_item
, m_posLast
);