remove unneeded wxFindSuitableParent()
[wxWidgets.git] / src / common / mousemanager.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/mousemanager.cpp
3 // Purpose: implementation of wxMouseEventsManager class
4 // Author: Vadim Zeitlin
5 // Created: 2009-04-21
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/settings.h"
28 #include "wx/window.h"
29 #endif // WX_PRECOMP
30
31 #include "wx/mousemanager.h"
32
33 // ----------------------------------------------------------------------------
34 // event tables
35 // ----------------------------------------------------------------------------
36
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)
42 END_EVENT_TABLE()
43
44 // ============================================================================
45 // wxMouseEventsManager implementation
46 // ============================================================================
47
48 wxMouseEventsManager::wxMouseEventsManager(wxWindow *win)
49 : m_win(win)
50 {
51 m_state = State_Normal;
52 m_item = wxNOT_FOUND;
53
54 win->PushEventHandler(this);
55 }
56
57 wxMouseEventsManager::~wxMouseEventsManager()
58 {
59 m_win->RemoveEventHandler(this);
60 }
61
62 void wxMouseEventsManager::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
63 {
64 switch ( m_state )
65 {
66 case State_Normal:
67 wxFAIL_MSG( "mouse shouldn't be captured in normal state" );
68 break;
69
70 case State_Pressed:
71 MouseClickCancelled(m_item);
72 break;
73
74 case State_Dragging:
75 MouseDragCancelled(m_item);
76 break;
77 }
78
79 m_state = State_Normal;
80 m_item = wxNOT_FOUND;
81 }
82
83 void wxMouseEventsManager::OnLeftDown(wxMouseEvent& event)
84 {
85 wxASSERT_MSG( m_state == State_Normal,
86 "state hasn't been reset to normal somehow" );
87
88 m_posLast = event.GetPosition();
89 m_item = MouseHitTest(m_posLast);
90 if ( m_item == wxNOT_FOUND )
91 {
92 event.Skip();
93 return;
94 }
95
96 m_state = State_Pressed;
97 m_win->CaptureMouse();
98 MouseClickBegin(m_item);
99 }
100
101 void wxMouseEventsManager::OnLeftUp(wxMouseEvent& event)
102 {
103 switch ( m_state )
104 {
105 case State_Normal:
106 // ignore it, the mouse hasn't been pressed over any item initially
107 // so releasing it shouldn't do anything
108 event.Skip();
109
110 // skip releasing the capture below
111 return;
112
113 case State_Pressed:
114 if ( MouseHitTest(event.GetPosition()) == m_item )
115 {
116 // mouse released over the same item, so it was a click
117 MouseClicked(m_item);
118 }
119 break;
120
121 case State_Dragging:
122 MouseDragEnd(m_item, event.GetPosition());
123 break;
124 }
125
126 m_state = State_Normal;
127 m_item = wxNOT_FOUND;
128 m_win->ReleaseMouse();
129 }
130
131 void wxMouseEventsManager::OnMove(wxMouseEvent& event)
132 {
133 switch ( m_state )
134 {
135 case State_Normal:
136 event.Skip();
137 break;
138
139 case State_Pressed:
140 wxASSERT_MSG( event.LeftIsDown(),
141 "should have detected mouse being released" );
142
143 {
144 // it's probably a bad idea to query the system for these
145 // values every time the mouse is moved so cache them on the
146 // assumption that they don't change -- which is wrong, of
147 // course, the user can change them but it doesn't happen often
148 static const int
149 dragMinX = wxSystemSettings::GetMetric(wxSYS_DRAG_X);
150 static const int
151 dragMinY = wxSystemSettings::GetMetric(wxSYS_DRAG_Y);
152
153 const wxPoint& pos = event.GetPosition();
154 const wxPoint ofs = pos - m_posLast;
155 if ( abs(ofs.x) > dragMinX || abs(ofs.y) > dragMinY )
156 {
157 // the mouse left the rectangle inside which its movements
158 // are considered to be too small to constitute a start of
159 // drag operation, do [attempt to] start it now
160 if ( MouseDragBegin(m_item, pos) )
161 {
162 m_state = State_Dragging;
163 }
164 }
165 else // still didn't move far enough away
166 {
167 event.Skip();
168 }
169 }
170 break;
171
172 case State_Dragging:
173 m_posLast = event.GetPosition();
174 MouseDragging(m_item, m_posLast);
175 break;
176 }
177 }
178