]>
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
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
26 #include "wx/settings.h"
27 #include "wx/window.h"
30 #include "wx/mousemanager.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 BEGIN_EVENT_TABLE(wxMouseEventsManager
, wxEvtHandler
)
37 EVT_MOUSE_CAPTURE_LOST(wxMouseEventsManager::OnCaptureLost
)
38 EVT_LEFT_DOWN(wxMouseEventsManager::OnLeftDown
)
39 EVT_LEFT_UP(wxMouseEventsManager::OnLeftUp
)
40 EVT_MOTION(wxMouseEventsManager::OnMove
)
43 // ============================================================================
44 // wxMouseEventsManager implementation
45 // ============================================================================
47 void wxMouseEventsManager::Init()
50 m_state
= State_Normal
;
54 bool wxMouseEventsManager::Create(wxWindow
*win
)
56 wxASSERT_MSG( !m_win
, "Create() must not be called twice" );
59 win
->PushEventHandler(this);
64 wxMouseEventsManager::~wxMouseEventsManager()
67 m_win
->RemoveEventHandler(this);
70 void wxMouseEventsManager::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
75 wxFAIL_MSG( "mouse shouldn't be captured in normal state" );
79 MouseClickCancelled(m_item
);
83 MouseDragCancelled(m_item
);
87 m_state
= State_Normal
;
91 void wxMouseEventsManager::OnLeftDown(wxMouseEvent
& event
)
93 wxASSERT_MSG( m_state
== State_Normal
,
94 "state hasn't been reset to normal somehow" );
96 m_posLast
= event
.GetPosition();
97 m_item
= MouseHitTest(m_posLast
);
98 if ( m_item
== wxNOT_FOUND
)
104 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
);