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();
275 delete m_dynamicEvents
;
283 bool wxEvtHandler::ProcessEvent(wxEvent
& event
)
285 // An event handler can be enabled or disabled
286 if ( GetEvtHandlerEnabled() )
288 // Handle per-instance dynamic event tables first
290 if (SearchDynamicEventTable( event
)) return TRUE
;
292 // Then static per-class event tables
294 const wxEventTable
*table
= GetEventTable();
296 // Try the associated validator first, if this is a window.
297 // Problem: if the event handler of the window has been replaced,
298 // this wxEvtHandler may no longer be a window.
299 // Therefore validators won't be processed if the handler
300 // has been replaced with SetEventHandler.
301 // THIS CAN BE CURED if PushEventHandler is used instead of
302 // SetEventHandler, and then processing will be passed down the
303 // chain of event handlers.
304 if (IsKindOf(CLASSINFO(wxWindow
)))
306 wxWindow
*win
= (wxWindow
*)this;
308 // Can only use the validator of the window which
309 // is receiving the event
310 if ( (win
== event
.GetEventObject()) &&
311 win
->GetValidator() &&
312 win
->GetValidator()->ProcessEvent(event
))
316 // Search upwards through the inheritance hierarchy
319 if (SearchEventTable((wxEventTable
&)*table
, event
))
321 table
= table
->baseTable
;
325 // Try going down the event handler chain
326 if ( GetNextHandler() )
328 if ( GetNextHandler()->ProcessEvent(event
) )
332 // Carry on up the parent-child hierarchy,
333 // but only if event is a command event: it wouldn't
334 // make sense for a parent to receive a child's size event, for example
335 if (IsKindOf(CLASSINFO(wxWindow
)) && event
.IsKindOf(CLASSINFO(wxCommandEvent
)))
337 wxWindow
*win
= (wxWindow
*)this;
338 wxWindow
*parent
= win
->GetParent();
339 if (parent
&& !parent
->IsBeingDeleted())
340 return win
->GetParent()->GetEventHandler()->ProcessEvent(event
);
343 // Last try - application object
344 if (wxTheApp
&& this != wxTheApp
&& wxTheApp
->ProcessEvent(event
))
350 bool wxEvtHandler::SearchEventTable(wxEventTable
& table
, wxEvent
& event
)
353 int commandId
= event
.GetId();
355 while (table
.entries
[i
].m_fn
!= NULL
)
357 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
358 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
359 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
360 (table
.entries
[i
].m_lastId
!= -1 &&
361 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
364 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
366 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
368 if ( event
.GetSkipped() )
378 void wxEvtHandler::Connect( const int id
, const int lastId
,
380 wxObjectEventFunction func
,
383 wxEventTableEntry
*entry
= new wxEventTableEntry
;
385 entry
->m_lastId
= lastId
;
386 entry
->m_eventType
= eventType
;
388 entry
->m_callbackUserData
= userData
;
390 if (!m_dynamicEvents
)
391 m_dynamicEvents
= new wxList
;
393 m_dynamicEvents
->Append( (wxObject
*) entry
);
396 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
398 if (!m_dynamicEvents
) return FALSE
;
400 int commandId
= event
.GetId();
402 wxNode
*node
= m_dynamicEvents
->First();
405 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
408 if ((event
.GetEventType() == entry
->m_eventType
) &&
409 (entry
->m_id
== -1 || // Match, if event spec says any id will do (id == -1)
410 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
411 (entry
->m_lastId
!= -1 &&
412 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))))
415 event
.m_callbackUserData
= entry
->m_callbackUserData
;
417 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
419 if (event
.GetSkipped())
430 #if WXWIN_COMPATIBILITY
431 void wxEvtHandler::OldOnMenuCommand(int cmd
)
433 if (GetNextHandler()) GetNextHandler()->OldOnMenuCommand(cmd
);
436 void wxEvtHandler::OldOnMenuSelect(int cmd
)
438 if (GetNextHandler()) GetNextHandler()->OldOnMenuSelect(cmd
);
441 void wxEvtHandler::OldOnInitMenuPopup(int pos
)
443 if (GetNextHandler()) GetNextHandler()->OldOnInitMenuPopup(pos
);
446 void wxEvtHandler::OldOnScroll(wxCommandEvent
& event
)
448 if (GetNextHandler()) GetNextHandler()->OldOnScroll(event
);
451 void wxEvtHandler::OldOnPaint(void)
453 if (GetNextHandler()) GetNextHandler()->OldOnPaint();
455 void wxEvtHandler::OldOnSize(int width
, int height
)
457 if (GetNextHandler()) GetNextHandler()->OldOnSize(width
, height
);
460 void wxEvtHandler::OldOnMove(int x
, int y
)
462 if (GetNextHandler()) GetNextHandler()->OldOnMove(x
, y
);
465 void wxEvtHandler::OldOnMouseEvent(wxMouseEvent
& event
)
467 if (GetNextHandler()) GetNextHandler()->OldOnMouseEvent(event
);
470 void wxEvtHandler::OldOnChar(wxKeyEvent
& event
)
472 if (GetNextHandler()) GetNextHandler()->OldOnChar(event
);
475 // Under Windows, we can intercept character input per dialog or frame
476 bool wxEvtHandler::OldOnCharHook(wxKeyEvent
& event
)
478 if (GetNextHandler()) return GetNextHandler()->OldOnCharHook(event
);
482 void wxEvtHandler::OldOnActivate(bool active
)
484 if (GetNextHandler()) GetNextHandler()->OldOnActivate(active
);
487 void wxEvtHandler::OldOnSetFocus(void)
489 if (GetNextHandler()) GetNextHandler()->OldOnSetFocus();
492 void wxEvtHandler::OldOnKillFocus(void)
494 if (GetNextHandler()) GetNextHandler()->OldOnKillFocus();
497 bool wxEvtHandler::OldOnSysColourChange(void)
499 if (GetNextHandler()) return GetNextHandler()->OldOnSysColourChange();
503 void wxEvtHandler::OldOnDropFiles(int n
, char *files
[], int x
, int y
)
505 if (GetNextHandler()) GetNextHandler()->OldOnDropFiles(n
, files
, x
, y
);
509 bool wxEvtHandler::OnClose(void)
511 if (GetNextHandler()) return GetNextHandler()->OnClose();