1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Event classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "event.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/control.h"
32 #include "wx/validate.h"
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler
, wxObject
)
36 IMPLEMENT_ABSTRACT_CLASS(wxEvent
, wxObject
)
37 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent
, wxEvent
)
38 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent
, wxCommandEvent
)
39 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent
, wxEvent
)
40 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent
, wxEvent
)
41 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent
, wxEvent
)
42 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent
, wxEvent
)
43 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent
, wxEvent
)
44 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent
, wxEvent
)
45 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent
, wxEvent
)
46 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent
, wxEvent
)
47 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent
, wxEvent
)
48 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent
, wxEvent
)
49 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent
, wxEvent
)
50 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent
, wxEvent
)
51 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent
, wxEvent
)
52 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent
, wxEvent
)
53 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent
, wxEvent
)
54 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent
, wxEvent
)
55 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent
, wxEvent
)
56 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent
, wxEvent
)
57 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent
, wxEvent
)
58 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent
, wxCommandEvent
)
60 const wxEventTable
*wxEvtHandler::GetEventTable() const { return &wxEvtHandler::sm_eventTable
; }
62 const wxEventTable
wxEvtHandler::sm_eventTable
=
63 { NULL
, &wxEvtHandler::sm_eventTableEntries
[0] };
65 const wxEventTableEntry
wxEvtHandler::sm_eventTableEntries
[] = { { 0, 0, 0, NULL
} };
70 * General wxWindows events, covering
71 * all interesting things that might happen (button clicking, resizing,
72 * setting text in widgets, etc.).
74 * For each completely new event type, derive a new event class.
78 wxEvent::wxEvent(int theId
)
80 m_eventType
= wxEVT_NULL
;
86 m_callbackUserData
= NULL
;
94 wxCommandEvent::wxCommandEvent(wxEventType commandType
, int theId
)
96 m_eventType
= commandType
;
101 m_commandString
= NULL
;
108 wxScrollEvent::wxScrollEvent(wxEventType commandType
, int id
, int pos
, int orient
):
109 wxCommandEvent(commandType
, id
)
111 m_extraLong
= orient
;
121 wxMouseEvent::wxMouseEvent(wxEventType commandType
)
123 m_eventType
= commandType
;
126 m_controlDown
= FALSE
;
130 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
131 // or any button dclick event (but = -1)
132 bool wxMouseEvent::ButtonDClick(int but
) const
136 return (LeftDClick() || MiddleDClick() || RightDClick());
140 return MiddleDClick();
142 return RightDClick();
149 // True if was a button down event (1 = left, 2 = middle, 3 = right)
150 // or any button down event (but = -1)
151 bool wxMouseEvent::ButtonDown(int but
) const
155 return (LeftDown() || MiddleDown() || RightDown());
168 // True if was a button up event (1 = left, 2 = middle, 3 = right)
169 // or any button up event (but = -1)
170 bool wxMouseEvent::ButtonUp(int but
) const
174 return (LeftUp() || MiddleUp() || RightUp());
187 // True if the given button is currently changing state
188 bool wxMouseEvent::Button(int but
) const
192 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
194 return (LeftDown() || LeftUp() || LeftDClick());
196 return (MiddleDown() || MiddleUp() || MiddleDClick());
198 return (RightDown() || RightUp() || RightDClick());
205 bool wxMouseEvent::ButtonIsDown(int but
) const
209 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
213 return MiddleIsDown();
215 return RightIsDown();
222 // Find the logical position of the event given the DC
223 wxPoint
wxMouseEvent::GetLogicalPosition(const wxDC
& dc
) const
225 wxPoint
pt(dc
.DeviceToLogicalX(m_x
), dc
.DeviceToLogicalY(m_y
));
235 wxKeyEvent::wxKeyEvent(wxEventType type
)
239 m_controlDown
= FALSE
;
249 wxEvtHandler::wxEvtHandler(void)
252 m_nextHandler
= NULL
;
253 m_previousHandler
= NULL
;
255 m_dynamicEvents
= NULL
;
258 wxEvtHandler::~wxEvtHandler(void)
260 // Takes itself out of the list of handlers
261 if (m_previousHandler
)
262 m_previousHandler
->m_nextHandler
= m_nextHandler
;
265 m_nextHandler
->m_previousHandler
= m_previousHandler
;
269 wxNode
*node
= m_dynamicEvents
->First();
272 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
273 if (entry
->m_callbackUserData
) delete entry
->m_callbackUserData
;
277 delete m_dynamicEvents
;
285 bool wxEvtHandler::ProcessEvent(wxEvent
& event
)
287 // An event handler can be enabled or disabled
288 if ( GetEvtHandlerEnabled() )
290 // Handle per-instance dynamic event tables first
292 if (SearchDynamicEventTable( event
)) return TRUE
;
294 // Then static per-class event tables
296 const wxEventTable
*table
= GetEventTable();
298 // Try the associated validator first, if this is a window.
299 // Problem: if the event handler of the window has been replaced,
300 // this wxEvtHandler may no longer be a window.
301 // Therefore validators won't be processed if the handler
302 // has been replaced with SetEventHandler.
303 // THIS CAN BE CURED if PushEventHandler is used instead of
304 // SetEventHandler, and then processing will be passed down the
305 // chain of event handlers.
306 if (IsKindOf(CLASSINFO(wxWindow
)))
308 wxWindow
*win
= (wxWindow
*)this;
310 // Can only use the validator of the window which
311 // is receiving the event
312 if ( (win
== event
.GetEventObject()) &&
313 win
->GetValidator() &&
314 win
->GetValidator()->ProcessEvent(event
))
318 // Search upwards through the inheritance hierarchy
321 if (SearchEventTable((wxEventTable
&)*table
, event
))
323 table
= table
->baseTable
;
327 // Try going down the event handler chain
328 if ( GetNextHandler() )
330 if ( GetNextHandler()->ProcessEvent(event
) )
334 // Carry on up the parent-child hierarchy,
335 // but only if event is a command event: it wouldn't
336 // make sense for a parent to receive a child's size event, for example
337 if (IsKindOf(CLASSINFO(wxWindow
)) && event
.IsKindOf(CLASSINFO(wxCommandEvent
)))
339 wxWindow
*win
= (wxWindow
*)this;
340 wxWindow
*parent
= win
->GetParent();
341 if (parent
&& !parent
->IsBeingDeleted())
342 return win
->GetParent()->GetEventHandler()->ProcessEvent(event
);
345 // Last try - application object
346 if (wxTheApp
&& this != wxTheApp
&& wxTheApp
->ProcessEvent(event
))
352 bool wxEvtHandler::SearchEventTable(wxEventTable
& table
, wxEvent
& event
)
355 int commandId
= event
.GetId();
357 while (table
.entries
[i
].m_fn
!= NULL
)
359 wxEventType eventType
= (wxEventType
) table
.entries
[i
].m_eventType
;
361 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
362 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
363 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
364 (table
.entries
[i
].m_lastId
!= -1 &&
365 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
368 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
370 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
372 if ( event
.GetSkipped() )
382 void wxEvtHandler::Connect( int id
, int lastId
,
384 wxObjectEventFunction func
,
387 wxEventTableEntry
*entry
= new wxEventTableEntry
;
389 entry
->m_lastId
= lastId
;
390 entry
->m_eventType
= eventType
;
392 entry
->m_callbackUserData
= userData
;
394 if (!m_dynamicEvents
)
395 m_dynamicEvents
= new wxList
;
397 m_dynamicEvents
->Append( (wxObject
*) entry
);
400 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
402 if (!m_dynamicEvents
) return FALSE
;
404 int commandId
= event
.GetId();
406 wxNode
*node
= m_dynamicEvents
->First();
409 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
410 wxEventType eventType
= (wxEventType
) entry
->m_eventType
;
414 if ((event
.GetEventType() == entry
->m_eventType
) &&
415 (entry
->m_id
== -1 || // Match, if event spec says any id will do (id == -1)
416 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
417 (entry
->m_lastId
!= -1 &&
418 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))))
421 event
.m_callbackUserData
= entry
->m_callbackUserData
;
423 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
425 if (event
.GetSkipped())
436 bool wxEvtHandler::OnClose(void)
438 if (GetNextHandler()) return GetNextHandler()->OnClose();