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
)
85 m_callbackUserData
= NULL
;
93 wxCommandEvent::wxCommandEvent(WXTYPE commandType
, int theId
)
95 m_eventType
= commandType
;
100 m_commandString
= NULL
;
107 wxScrollEvent::wxScrollEvent(WXTYPE commandType
, int id
, int pos
, int orient
):
108 wxCommandEvent(commandType
, id
)
110 m_extraLong
= orient
;
120 wxMouseEvent::wxMouseEvent(WXTYPE 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(WXTYPE 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 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
359 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
360 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
361 (table
.entries
[i
].m_lastId
!= -1 &&
362 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
365 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
367 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
369 if ( event
.GetSkipped() )
379 void wxEvtHandler::Connect( const int id
, const int lastId
,
381 wxObjectEventFunction func
,
384 wxEventTableEntry
*entry
= new wxEventTableEntry
;
386 entry
->m_lastId
= lastId
;
387 entry
->m_eventType
= eventType
;
389 entry
->m_callbackUserData
= userData
;
391 if (!m_dynamicEvents
)
392 m_dynamicEvents
= new wxList
;
394 m_dynamicEvents
->Append( (wxObject
*) entry
);
397 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
399 if (!m_dynamicEvents
) return FALSE
;
401 int commandId
= event
.GetId();
403 wxNode
*node
= m_dynamicEvents
->First();
406 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
409 if ((event
.GetEventType() == entry
->m_eventType
) &&
410 (entry
->m_id
== -1 || // Match, if event spec says any id will do (id == -1)
411 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
412 (entry
->m_lastId
!= -1 &&
413 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))))
416 event
.m_callbackUserData
= entry
->m_callbackUserData
;
418 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
420 if (event
.GetSkipped())
431 #if WXWIN_COMPATIBILITY
432 void wxEvtHandler::OldOnMenuCommand(int cmd
)
434 if (GetNextHandler()) GetNextHandler()->OldOnMenuCommand(cmd
);
437 void wxEvtHandler::OldOnMenuSelect(int cmd
)
439 if (GetNextHandler()) GetNextHandler()->OldOnMenuSelect(cmd
);
442 void wxEvtHandler::OldOnInitMenuPopup(int pos
)
444 if (GetNextHandler()) GetNextHandler()->OldOnInitMenuPopup(pos
);
447 void wxEvtHandler::OldOnScroll(wxCommandEvent
& event
)
449 if (GetNextHandler()) GetNextHandler()->OldOnScroll(event
);
452 void wxEvtHandler::OldOnPaint(void)
454 if (GetNextHandler()) GetNextHandler()->OldOnPaint();
456 void wxEvtHandler::OldOnSize(int width
, int height
)
458 if (GetNextHandler()) GetNextHandler()->OldOnSize(width
, height
);
461 void wxEvtHandler::OldOnMove(int x
, int y
)
463 if (GetNextHandler()) GetNextHandler()->OldOnMove(x
, y
);
466 void wxEvtHandler::OldOnMouseEvent(wxMouseEvent
& event
)
468 if (GetNextHandler()) GetNextHandler()->OldOnMouseEvent(event
);
471 void wxEvtHandler::OldOnChar(wxKeyEvent
& event
)
473 if (GetNextHandler()) GetNextHandler()->OldOnChar(event
);
476 // Under Windows, we can intercept character input per dialog or frame
477 bool wxEvtHandler::OldOnCharHook(wxKeyEvent
& event
)
479 if (GetNextHandler()) return GetNextHandler()->OldOnCharHook(event
);
483 void wxEvtHandler::OldOnActivate(bool active
)
485 if (GetNextHandler()) GetNextHandler()->OldOnActivate(active
);
488 void wxEvtHandler::OldOnSetFocus(void)
490 if (GetNextHandler()) GetNextHandler()->OldOnSetFocus();
493 void wxEvtHandler::OldOnKillFocus(void)
495 if (GetNextHandler()) GetNextHandler()->OldOnKillFocus();
498 bool wxEvtHandler::OldOnSysColourChange(void)
500 if (GetNextHandler()) return GetNextHandler()->OldOnSysColourChange();
504 void wxEvtHandler::OldOnDropFiles(int n
, char *files
[], int x
, int y
)
506 if (GetNextHandler()) GetNextHandler()->OldOnDropFiles(n
, files
, x
, y
);
510 bool wxEvtHandler::OnClose(void)
512 if (GetNextHandler()) return GetNextHandler()->OnClose();