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
)
59 const wxEventTable
*wxEvtHandler::GetEventTable() const { return &wxEvtHandler::sm_eventTable
; }
61 const wxEventTable
wxEvtHandler::sm_eventTable
=
62 { NULL
, &wxEvtHandler::sm_eventTableEntries
[0] };
64 const wxEventTableEntry
wxEvtHandler::sm_eventTableEntries
[] = { { 0, 0, 0, NULL
} };
69 * General wxWindows events, covering
70 * all interesting things that might happen (button clicking, resizing,
71 * setting text in widgets, etc.).
73 * For each completely new event type, derive a new event class.
77 wxEvent::wxEvent(int theId
)
79 m_eventType
= wxEVT_NULL
;
85 m_callbackUserData
= NULL
;
93 wxCommandEvent::wxCommandEvent(wxEventType commandType
, int theId
)
95 m_eventType
= commandType
;
100 m_commandString
= NULL
;
107 wxScrollEvent::wxScrollEvent(wxEventType commandType
, int id
, int pos
, int orient
):
108 wxCommandEvent(commandType
, id
)
110 m_extraLong
= orient
;
120 wxMouseEvent::wxMouseEvent(wxEventType commandType
)
122 m_eventType
= commandType
;
125 m_controlDown
= FALSE
;
129 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
130 // or any button dclick event (but = -1)
131 bool wxMouseEvent::ButtonDClick(int but
) const
135 return (LeftDClick() || MiddleDClick() || RightDClick());
139 return MiddleDClick();
141 return RightDClick();
148 // True if was a button down event (1 = left, 2 = middle, 3 = right)
149 // or any button down event (but = -1)
150 bool wxMouseEvent::ButtonDown(int but
) const
154 return (LeftDown() || MiddleDown() || RightDown());
167 // True if was a button up event (1 = left, 2 = middle, 3 = right)
168 // or any button up event (but = -1)
169 bool wxMouseEvent::ButtonUp(int but
) const
173 return (LeftUp() || MiddleUp() || RightUp());
186 // True if the given button is currently changing state
187 bool wxMouseEvent::Button(int but
) const
191 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
193 return (LeftDown() || LeftUp() || LeftDClick());
195 return (MiddleDown() || MiddleUp() || MiddleDClick());
197 return (RightDown() || RightUp() || RightDClick());
204 bool wxMouseEvent::ButtonIsDown(int but
) const
208 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
212 return MiddleIsDown();
214 return RightIsDown();
221 // Find the logical position of the event given the DC
222 wxPoint
wxMouseEvent::GetLogicalPosition(const wxDC
& dc
) const
224 wxPoint
pt(dc
.DeviceToLogicalX(m_x
), dc
.DeviceToLogicalY(m_y
));
234 wxKeyEvent::wxKeyEvent(wxEventType type
)
238 m_controlDown
= FALSE
;
248 wxEvtHandler::wxEvtHandler(void)
251 m_nextHandler
= NULL
;
252 m_previousHandler
= NULL
;
254 m_dynamicEvents
= NULL
;
257 wxEvtHandler::~wxEvtHandler(void)
259 // Takes itself out of the list of handlers
260 if (m_previousHandler
)
261 m_previousHandler
->m_nextHandler
= m_nextHandler
;
264 m_nextHandler
->m_previousHandler
= m_previousHandler
;
268 wxNode
*node
= m_dynamicEvents
->First();
271 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
272 if (entry
->m_callbackUserData
) delete entry
->m_callbackUserData
;
276 delete m_dynamicEvents
;
284 bool wxEvtHandler::ProcessEvent(wxEvent
& event
)
286 // An event handler can be enabled or disabled
287 if ( GetEvtHandlerEnabled() )
289 // Handle per-instance dynamic event tables first
291 if (SearchDynamicEventTable( event
)) return TRUE
;
293 // Then static per-class event tables
295 const wxEventTable
*table
= GetEventTable();
297 // Try the associated validator first, if this is a window.
298 // Problem: if the event handler of the window has been replaced,
299 // this wxEvtHandler may no longer be a window.
300 // Therefore validators won't be processed if the handler
301 // has been replaced with SetEventHandler.
302 // THIS CAN BE CURED if PushEventHandler is used instead of
303 // SetEventHandler, and then processing will be passed down the
304 // chain of event handlers.
305 if (IsKindOf(CLASSINFO(wxWindow
)))
307 wxWindow
*win
= (wxWindow
*)this;
309 // Can only use the validator of the window which
310 // is receiving the event
311 if ( (win
== event
.GetEventObject()) &&
312 win
->GetValidator() &&
313 win
->GetValidator()->ProcessEvent(event
))
317 // Search upwards through the inheritance hierarchy
320 if (SearchEventTable((wxEventTable
&)*table
, event
))
322 table
= table
->baseTable
;
326 // Try going down the event handler chain
327 if ( GetNextHandler() )
329 if ( GetNextHandler()->ProcessEvent(event
) )
333 // Carry on up the parent-child hierarchy,
334 // but only if event is a command event: it wouldn't
335 // make sense for a parent to receive a child's size event, for example
336 if (IsKindOf(CLASSINFO(wxWindow
)) && event
.IsKindOf(CLASSINFO(wxCommandEvent
)))
338 wxWindow
*win
= (wxWindow
*)this;
339 wxWindow
*parent
= win
->GetParent();
340 if (parent
&& !parent
->IsBeingDeleted())
341 return win
->GetParent()->GetEventHandler()->ProcessEvent(event
);
344 // Last try - application object
345 if (wxTheApp
&& this != wxTheApp
&& wxTheApp
->ProcessEvent(event
))
351 bool wxEvtHandler::SearchEventTable(wxEventTable
& table
, wxEvent
& event
)
354 int commandId
= event
.GetId();
356 while (table
.entries
[i
].m_fn
!= NULL
)
358 wxEventType eventType
= (wxEventType
) table
.entries
[i
].m_eventType
;
360 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
361 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
362 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
363 (table
.entries
[i
].m_lastId
!= -1 &&
364 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
367 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
369 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
371 if ( event
.GetSkipped() )
381 void wxEvtHandler::Connect( int id
, int lastId
,
383 wxObjectEventFunction func
,
386 wxEventTableEntry
*entry
= new wxEventTableEntry
;
388 entry
->m_lastId
= lastId
;
389 entry
->m_eventType
= eventType
;
391 entry
->m_callbackUserData
= userData
;
393 if (!m_dynamicEvents
)
394 m_dynamicEvents
= new wxList
;
396 m_dynamicEvents
->Append( (wxObject
*) entry
);
399 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
401 if (!m_dynamicEvents
) return FALSE
;
403 int commandId
= event
.GetId();
405 wxNode
*node
= m_dynamicEvents
->First();
408 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
409 wxEventType eventType
= (wxEventType
) entry
->m_eventType
;
413 if ((event
.GetEventType() == entry
->m_eventType
) &&
414 (entry
->m_id
== -1 || // Match, if event spec says any id will do (id == -1)
415 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
416 (entry
->m_lastId
!= -1 &&
417 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))))
420 event
.m_callbackUserData
= entry
->m_callbackUserData
;
422 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
424 if (event
.GetSkipped())
436 #if WXWIN_COMPATIBILITY
437 void wxEvtHandler::OldOnMenuCommand(int cmd)
439 if (GetNextHandler()) GetNextHandler()->OldOnMenuCommand(cmd);
442 void wxEvtHandler::OldOnMenuSelect(int cmd)
444 if (GetNextHandler()) GetNextHandler()->OldOnMenuSelect(cmd);
447 void wxEvtHandler::OldOnInitMenuPopup(int pos)
449 if (GetNextHandler()) GetNextHandler()->OldOnInitMenuPopup(pos);
452 void wxEvtHandler::OldOnScroll(wxCommandEvent& event)
454 if (GetNextHandler()) GetNextHandler()->OldOnScroll(event);
457 void wxEvtHandler::OldOnPaint(void)
459 if (GetNextHandler()) GetNextHandler()->OldOnPaint();
461 void wxEvtHandler::OldOnSize(int width, int height)
463 if (GetNextHandler()) GetNextHandler()->OldOnSize(width, height);
466 void wxEvtHandler::OldOnMove(int x, int y)
468 if (GetNextHandler()) GetNextHandler()->OldOnMove(x, y);
471 void wxEvtHandler::OldOnMouseEvent(wxMouseEvent& event)
473 if (GetNextHandler()) GetNextHandler()->OldOnMouseEvent(event);
476 void wxEvtHandler::OldOnChar(wxKeyEvent& event)
478 if (GetNextHandler()) GetNextHandler()->OldOnChar(event);
481 // Under Windows, we can intercept character input per dialog or frame
482 bool wxEvtHandler::OldOnCharHook(wxKeyEvent& event)
484 if (GetNextHandler()) return GetNextHandler()->OldOnCharHook(event);
488 void wxEvtHandler::OldOnActivate(bool active)
490 if (GetNextHandler()) GetNextHandler()->OldOnActivate(active);
493 void wxEvtHandler::OldOnSetFocus(void)
495 if (GetNextHandler()) GetNextHandler()->OldOnSetFocus();
498 void wxEvtHandler::OldOnKillFocus(void)
500 if (GetNextHandler()) GetNextHandler()->OldOnKillFocus();
503 bool wxEvtHandler::OldOnSysColourChange(void)
505 if (GetNextHandler()) return GetNextHandler()->OldOnSysColourChange();
509 void wxEvtHandler::OldOnDropFiles(int n, char *files[], int x, int y)
511 if (GetNextHandler()) GetNextHandler()->OldOnDropFiles(n, files, x, y);
516 bool wxEvtHandler::OnClose(void)
518 if (GetNextHandler()) return GetNextHandler()->OnClose();