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