]> git.saurik.com Git - wxWidgets.git/blob - src/common/mousemanager.cpp
Fix background drawing in EVT_ERASE_BACKGROUND handler in erase sample.
[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->SetFocus();
107 m_win->CaptureMouse();
108 MouseClickBegin(m_item);
109 }
110
111 void wxMouseEventsManager::OnLeftUp(wxMouseEvent& event)
112 {
113 switch ( m_state )
114 {
115 case State_Normal:
116 // ignore it, the mouse hasn't been pressed over any item initially
117 // so releasing it shouldn't do anything
118 event.Skip();
119
120 // skip releasing the capture below
121 return;
122
123 case State_Pressed:
124 if ( MouseHitTest(event.GetPosition()) == m_item )
125 {
126 // mouse released over the same item, so it was a click
127 MouseClicked(m_item);
128 }
129 break;
130
131 case State_Dragging:
132 MouseDragEnd(m_item, event.GetPosition());
133 break;
134 }
135
136 m_state = State_Normal;
137 m_item = wxNOT_FOUND;
138 m_win->ReleaseMouse();
139 }
140
141 void wxMouseEventsManager::OnMove(wxMouseEvent& event)
142 {
143 switch ( m_state )
144 {
145 case State_Normal:
146 event.Skip();
147 break;
148
149 case State_Pressed:
150 wxASSERT_MSG( event.LeftIsDown(),
151 "should have detected mouse being released" );
152
153 {
154 // it's probably a bad idea to query the system for these
155 // values every time the mouse is moved so cache them on the
156 // assumption that they don't change -- which is wrong, of
157 // course, the user can change them but it doesn't happen often
158 static const int
159 dragMinX = wxSystemSettings::GetMetric(wxSYS_DRAG_X);
160 static const int
161 dragMinY = wxSystemSettings::GetMetric(wxSYS_DRAG_Y);
162
163 const wxPoint& pos = event.GetPosition();
164 const wxPoint ofs = pos - m_posLast;
165 if ( abs(ofs.x) > dragMinX || abs(ofs.y) > dragMinY )
166 {
167 // the mouse left the rectangle inside which its movements
168 // are considered to be too small to constitute a start of
169 // drag operation, do [attempt to] start it now
170 if ( MouseDragBegin(m_item, pos) )
171 {
172 m_state = State_Dragging;
173 }
174 }
175 else // still didn't move far enough away
176 {
177 event.Skip();
178 }
179 }
180 break;
181
182 case State_Dragging:
183 m_posLast = event.GetPosition();
184 MouseDragging(m_item, m_posLast);
185 break;
186 }
187 }
188