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
)
59 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent
, wxEvent
)
60 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent
, wxEvent
)
62 const wxEventTable
*wxEvtHandler::GetEventTable() const { return &wxEvtHandler::sm_eventTable
; }
64 const wxEventTable
wxEvtHandler::sm_eventTable
=
65 { NULL
, &wxEvtHandler::sm_eventTableEntries
[0] };
67 const wxEventTableEntry
wxEvtHandler::sm_eventTableEntries
[] = { { 0, 0, 0, NULL
} };
72 * General wxWindows events, covering
73 * all interesting things that might happen (button clicking, resizing,
74 * setting text in widgets, etc.).
76 * For each completely new event type, derive a new event class.
80 wxEvent::wxEvent(int theId
)
82 m_eventType
= wxEVT_NULL
;
88 m_callbackUserData
= NULL
;
96 wxCommandEvent::wxCommandEvent(wxEventType commandType
, int theId
)
98 m_eventType
= commandType
;
103 m_commandString
= NULL
;
110 wxScrollEvent::wxScrollEvent(wxEventType commandType
, int id
, int pos
, int orient
):
111 wxCommandEvent(commandType
, id
)
113 m_extraLong
= orient
;
123 wxMouseEvent::wxMouseEvent(wxEventType commandType
)
125 m_eventType
= commandType
;
128 m_controlDown
= FALSE
;
132 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
133 // or any button dclick event (but = -1)
134 bool wxMouseEvent::ButtonDClick(int but
) const
138 return (LeftDClick() || MiddleDClick() || RightDClick());
142 return MiddleDClick();
144 return RightDClick();
151 // True if was a button down event (1 = left, 2 = middle, 3 = right)
152 // or any button down event (but = -1)
153 bool wxMouseEvent::ButtonDown(int but
) const
157 return (LeftDown() || MiddleDown() || RightDown());
170 // True if was a button up event (1 = left, 2 = middle, 3 = right)
171 // or any button up event (but = -1)
172 bool wxMouseEvent::ButtonUp(int but
) const
176 return (LeftUp() || MiddleUp() || RightUp());
189 // True if the given button is currently changing state
190 bool wxMouseEvent::Button(int but
) const
194 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
196 return (LeftDown() || LeftUp() || LeftDClick());
198 return (MiddleDown() || MiddleUp() || MiddleDClick());
200 return (RightDown() || RightUp() || RightDClick());
207 bool wxMouseEvent::ButtonIsDown(int but
) const
211 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
215 return MiddleIsDown();
217 return RightIsDown();
224 // Find the logical position of the event given the DC
225 wxPoint
wxMouseEvent::GetLogicalPosition(const wxDC
& dc
) const
227 wxPoint
pt(dc
.DeviceToLogicalX(m_x
), dc
.DeviceToLogicalY(m_y
));
237 wxKeyEvent::wxKeyEvent(wxEventType type
)
241 m_controlDown
= FALSE
;
251 wxEvtHandler::wxEvtHandler(void)
254 m_nextHandler
= NULL
;
255 m_previousHandler
= NULL
;
257 m_dynamicEvents
= NULL
;
260 wxEvtHandler::~wxEvtHandler(void)
262 // Takes itself out of the list of handlers
263 if (m_previousHandler
)
264 m_previousHandler
->m_nextHandler
= m_nextHandler
;
267 m_nextHandler
->m_previousHandler
= m_previousHandler
;
271 wxNode
*node
= m_dynamicEvents
->First();
274 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
275 if (entry
->m_callbackUserData
) delete entry
->m_callbackUserData
;
279 delete m_dynamicEvents
;
287 bool wxEvtHandler::ProcessEvent(wxEvent
& event
)
289 // An event handler can be enabled or disabled
290 if ( GetEvtHandlerEnabled() )
292 // Handle per-instance dynamic event tables first
294 if (SearchDynamicEventTable( event
)) return TRUE
;
296 // Then static per-class event tables
298 const wxEventTable
*table
= GetEventTable();
300 // Try the associated validator first, if this is a window.
301 // Problem: if the event handler of the window has been replaced,
302 // this wxEvtHandler may no longer be a window.
303 // Therefore validators won't be processed if the handler
304 // has been replaced with SetEventHandler.
305 // THIS CAN BE CURED if PushEventHandler is used instead of
306 // SetEventHandler, and then processing will be passed down the
307 // chain of event handlers.
308 if (IsKindOf(CLASSINFO(wxWindow
)))
310 wxWindow
*win
= (wxWindow
*)this;
312 // Can only use the validator of the window which
313 // is receiving the event
314 if ( (win
== event
.GetEventObject()) &&
315 win
->GetValidator() &&
316 win
->GetValidator()->ProcessEvent(event
))
320 // Search upwards through the inheritance hierarchy
323 if (SearchEventTable((wxEventTable
&)*table
, event
))
325 table
= table
->baseTable
;
329 // Try going down the event handler chain
330 if ( GetNextHandler() )
332 if ( GetNextHandler()->ProcessEvent(event
) )
336 // Carry on up the parent-child hierarchy,
337 // but only if event is a command event: it wouldn't
338 // make sense for a parent to receive a child's size event, for example
339 if (IsKindOf(CLASSINFO(wxWindow
)) && event
.IsKindOf(CLASSINFO(wxCommandEvent
)))
341 wxWindow
*win
= (wxWindow
*)this;
342 wxWindow
*parent
= win
->GetParent();
343 if (parent
&& !parent
->IsBeingDeleted())
344 return win
->GetParent()->GetEventHandler()->ProcessEvent(event
);
347 // Last try - application object
348 if (wxTheApp
&& this != wxTheApp
&& wxTheApp
->ProcessEvent(event
))
354 bool wxEvtHandler::SearchEventTable(wxEventTable
& table
, wxEvent
& event
)
357 int commandId
= event
.GetId();
359 while (table
.entries
[i
].m_fn
!= NULL
)
361 // wxEventType eventType = (wxEventType) table.entries[i].m_eventType;
363 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
364 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
365 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
366 (table
.entries
[i
].m_lastId
!= -1 &&
367 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
370 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
372 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
374 if ( event
.GetSkipped() )
384 void wxEvtHandler::Connect( int id
, int lastId
,
386 wxObjectEventFunction func
,
389 wxEventTableEntry
*entry
= new wxEventTableEntry
;
391 entry
->m_lastId
= lastId
;
392 entry
->m_eventType
= eventType
;
394 entry
->m_callbackUserData
= userData
;
396 if (!m_dynamicEvents
)
397 m_dynamicEvents
= new wxList
;
399 m_dynamicEvents
->Append( (wxObject
*) entry
);
402 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
404 if (!m_dynamicEvents
) return FALSE
;
406 int commandId
= event
.GetId();
408 wxNode
*node
= m_dynamicEvents
->First();
411 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
413 // wxEventType eventType = (wxEventType) entry->m_eventType;
417 if ((event
.GetEventType() == entry
->m_eventType
) &&
418 (entry
->m_id
== -1 || // Match, if event spec says any id will do (id == -1)
419 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
420 (entry
->m_lastId
!= -1 &&
421 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))))
424 event
.m_callbackUserData
= entry
->m_callbackUserData
;
426 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
428 if (event
.GetSkipped())
439 bool wxEvtHandler::OnClose(void)
441 if (GetNextHandler()) return GetNextHandler()->OnClose();