]> git.saurik.com Git - wxWidgets.git/blob - src/common/mousemanager.cpp
added default ctor to wxMouseEventsManager, this is convenient when deriving window...
[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 void wxMouseEventsManager::Init()
49 {
50 m_win = NULL;
51 m_state = State_Normal;
52 m_item = wxNOT_FOUND;
53 }
54
55 bool wxMouseEventsManager::Create(wxWindow *win)
56 {
57 wxASSERT_MSG( !m_win, "Create() must not be called twice" );
58
59 m_win = win;
60 win->PushEventHandler(this);
61
62 return true;
63 }
64
65 wxMouseEventsManager::~wxMouseEventsManager()
66 {
67 if ( m_win )
68 m_win->RemoveEventHandler(this);
69 }
70
71 void wxMouseEventsManager::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
72 {
73 switch ( m_state )
74 {
75 case State_Normal:
76 wxFAIL_MSG( "mouse shouldn't be captured in normal state" );
77 break;
78
79 case State_Pressed:
80 MouseClickCancelled(m_item);
81 break;
82
83 case State_Dragging:
84 MouseDragCancelled(m_item);
85 break;
86 }
87
88 m_state = State_Normal;
89 m_item = wxNOT_FOUND;
90 }
91
92 void wxMouseEventsManager::OnLeftDown(wxMouseEvent& event)
93 {
94 wxASSERT_MSG( m_state == State_Normal,
95 "state hasn't been reset to normal somehow" );
96
97 m_posLast = event.GetPosition();
98 m_item = MouseHitTest(m_posLast);
99 if ( m_item == wxNOT_FOUND )
100 {
101 event.Skip();
102 return;
103 }
104
105 m_state = State_Pressed;
106 m_win->CaptureMouse();
107 MouseClickBegin(m_item);
108 }
109
110 void wxMouseEventsManager::OnLeftUp(wxMouseEvent& event)
111 {
112 switch ( m_state )
113 {
114 case State_Normal:
115 // ignore it, the mouse hasn't been pressed over any item initially
116 // so releasing it shouldn't do anything
117 event.Skip();
118
119 // skip releasing the capture below
120 return;
121
122 case State_Pressed:
123 if ( MouseHitTest(event.GetPosition()) == m_item )
124 {
125 // mouse released over the same item, so it was a click
126 MouseClicked(m_item);
127 }
128 break;
129
130 case State_Dragging:
131 MouseDragEnd(m_item, event.GetPosition());
132 break;
133 }
134
135 m_state = State_Normal;
136 m_item = wxNOT_FOUND;
137 m_win->ReleaseMouse();
138 }
139
140 void wxMouseEventsManager::OnMove(wxMouseEvent& event)
141 {
142 switch ( m_state )
143 {
144 case State_Normal:
145 event.Skip();
146 break;
147
148 case State_Pressed:
149 wxASSERT_MSG( event.LeftIsDown(),
150 "should have detected mouse being released" );
151
152 {
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
157 static const int
158 dragMinX = wxSystemSettings::GetMetric(wxSYS_DRAG_X);
159 static const int
160 dragMinY = wxSystemSettings::GetMetric(wxSYS_DRAG_Y);
161
162 const wxPoint& pos = event.GetPosition();
163 const wxPoint ofs = pos - m_posLast;
164 if ( abs(ofs.x) > dragMinX || abs(ofs.y) > dragMinY )
165 {
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) )
170 {
171 m_state = State_Dragging;
172 }
173 }
174 else // still didn't move far enough away
175 {
176 event.Skip();
177 }
178 }
179 break;
180
181 case State_Dragging:
182 m_posLast = event.GetPosition();
183 MouseDragging(m_item, m_posLast);
184 break;
185 }
186 }
187