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(wxNotifyEvent
, wxCommandEvent
)
39 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent
, wxCommandEvent
)
40 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent
, wxEvent
)
41 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent
, wxEvent
)
42 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent
, wxEvent
)
43 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent
, wxEvent
)
44 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent
, wxEvent
)
45 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent
, wxEvent
)
46 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent
, wxEvent
)
47 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent
, wxEvent
)
48 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent
, wxEvent
)
49 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent
, wxEvent
)
50 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent
, wxEvent
)
51 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent
, wxEvent
)
52 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent
, wxEvent
)
53 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent
, wxEvent
)
54 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent
, wxEvent
)
55 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent
, wxEvent
)
56 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent
, wxEvent
)
57 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent
, wxEvent
)
58 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent
, wxCommandEvent
)
59 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent
, wxCommandEvent
)
60 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent
, wxEvent
)
61 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent
, wxEvent
)
63 const wxEventTable
*wxEvtHandler::GetEventTable() const
64 { return &wxEvtHandler::sm_eventTable
; }
66 const wxEventTable
wxEvtHandler::sm_eventTable
=
67 { (const wxEventTable
*)NULL
, &wxEvtHandler::sm_eventTableEntries
[0] };
69 const wxEventTableEntry
wxEvtHandler::sm_eventTableEntries
[] =
70 { { 0, 0, 0, (wxObjectEventFunction
) NULL
, (wxObject
*) NULL
} };
72 #endif // !USE_SHARED_LIBRARY
75 /* To put pending event handlers */
76 extern wxList
*wxPendingEvents
;
77 extern wxCriticalSection
*wxPendingEventsLocker
;
81 * General wxWindows events, covering
82 * all interesting things that might happen (button clicking, resizing,
83 * setting text in widgets, etc.).
85 * For each completely new event type, derive a new event class.
89 wxEvent::wxEvent(int theId
)
91 m_eventType
= wxEVT_NULL
;
92 m_eventObject
= (wxObject
*) NULL
;
93 m_eventHandle
= (char *) NULL
;
97 m_callbackUserData
= (wxObject
*) NULL
;
98 m_isCommandEvent
= FALSE
;
101 wxObject
*wxEvent::Clone() const
103 wxEvent
*event
= (wxEvent
*)wxObject::Clone();
105 event
->m_eventType
= m_eventType
;
106 event
->m_eventObject
= m_eventObject
;
107 event
->m_eventHandle
= m_eventHandle
;
108 event
->m_timeStamp
= m_timeStamp
;
110 event
->m_skipped
= m_skipped
;
111 event
->m_callbackUserData
= m_callbackUserData
;
112 event
->m_isCommandEvent
= m_isCommandEvent
;
122 wxCommandEvent::wxCommandEvent(wxEventType commandType
, int theId
)
124 m_eventType
= commandType
;
125 m_clientData
= (char *) NULL
;
126 m_clientObject
= (wxClientData
*) NULL
;
130 m_commandString
= (wxChar
*) NULL
;
131 m_isCommandEvent
= TRUE
;
138 wxScrollEvent::wxScrollEvent(wxEventType commandType
,
142 : wxCommandEvent(commandType
, id
)
144 m_extraLong
= orient
;
154 wxMouseEvent::wxMouseEvent(wxEventType commandType
)
156 m_eventType
= commandType
;
159 m_controlDown
= FALSE
;
163 m_middleDown
= FALSE
;
168 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
169 // or any button dclick event (but = -1)
170 bool wxMouseEvent::ButtonDClick(int but
) const
175 return (LeftDClick() || MiddleDClick() || RightDClick());
179 return MiddleDClick();
181 return RightDClick();
183 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDClick"));
189 // True if was a button down event (1 = left, 2 = middle, 3 = right)
190 // or any button down event (but = -1)
191 bool wxMouseEvent::ButtonDown(int but
) const
196 return (LeftDown() || MiddleDown() || RightDown());
204 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDown"));
210 // True if was a button up event (1 = left, 2 = middle, 3 = right)
211 // or any button up event (but = -1)
212 bool wxMouseEvent::ButtonUp(int but
) const
216 return (LeftUp() || MiddleUp() || RightUp());
224 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonUp"));
230 // True if the given button is currently changing state
231 bool wxMouseEvent::Button(int but
) const
235 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
237 return (LeftDown() || LeftUp() || LeftDClick());
239 return (MiddleDown() || MiddleUp() || MiddleDClick());
241 return (RightDown() || RightUp() || RightDClick());
243 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::Button"));
249 bool wxMouseEvent::ButtonIsDown(int but
) const
253 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
257 return MiddleIsDown();
259 return RightIsDown();
261 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonIsDown"));
267 // Find the logical position of the event given the DC
268 wxPoint
wxMouseEvent::GetLogicalPosition(const wxDC
& dc
) const
270 wxPoint
pt(dc
.DeviceToLogicalX(m_x
), dc
.DeviceToLogicalY(m_y
));
280 wxKeyEvent::wxKeyEvent(wxEventType type
)
284 m_controlDown
= FALSE
;
294 wxEvtHandler::wxEvtHandler()
296 m_nextHandler
= (wxEvtHandler
*) NULL
;
297 m_previousHandler
= (wxEvtHandler
*) NULL
;
299 m_dynamicEvents
= (wxList
*) NULL
;
302 m_eventsLocker
= new wxCriticalSection();
304 m_pendingEvents
= (wxList
*) NULL
;
307 wxEvtHandler::~wxEvtHandler()
309 // Takes itself out of the list of handlers
310 if (m_previousHandler
)
311 m_previousHandler
->m_nextHandler
= m_nextHandler
;
314 m_nextHandler
->m_previousHandler
= m_previousHandler
;
318 wxNode
*node
= m_dynamicEvents
->First();
321 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
322 if (entry
->m_callbackUserData
) delete entry
->m_callbackUserData
;
326 delete m_dynamicEvents
;
331 delete m_pendingEvents
;
333 delete m_eventsLocker
;
338 bool wxEvtHandler::ProcessThreadEvent(wxEvent
& event
)
341 wxCriticalSectionLocker
locker(*m_eventsLocker
);
343 // check that we are really in a child thread
344 wxASSERT( !wxThread::IsMain() );
346 if (m_pendingEvents
== NULL
)
347 m_pendingEvents
= new wxList();
349 event_main
= (wxEvent
*)event
.Clone();
351 m_pendingEvents
->Append(event_main
);
353 wxPendingEventsLocker
->Enter();
354 wxPendingEvents
->Append(this);
355 wxPendingEventsLocker
->Leave();
360 void wxEvtHandler::ProcessPendingEvents()
362 wxCriticalSectionLocker
locker(*m_eventsLocker
);
363 wxNode
*node
= m_pendingEvents
->First();
366 while (node
!= NULL
) {
367 event
= (wxEvent
*)node
->Data();
368 ProcessEvent(*event
);
370 node
= m_pendingEvents
->First();
379 bool wxEvtHandler::ProcessEvent(wxEvent
& event
)
381 // check that our flag corresponds to reality
382 wxASSERT( m_isWindow
== IsKindOf(CLASSINFO(wxWindow
)) );
384 // An event handler can be enabled or disabled
385 if ( GetEvtHandlerEnabled() )
388 // Check whether we are in a child thread.
389 if (!wxThread::IsMain())
390 return ProcessThreadEvent(event
);
392 // Handle per-instance dynamic event tables first
394 if ( m_dynamicEvents
&& SearchDynamicEventTable(event
) )
397 // Then static per-class event tables
399 const wxEventTable
*table
= GetEventTable();
401 // Try the associated validator first, if this is a window.
402 // Problem: if the event handler of the window has been replaced,
403 // this wxEvtHandler may no longer be a window.
404 // Therefore validators won't be processed if the handler
405 // has been replaced with SetEventHandler.
406 // THIS CAN BE CURED if PushEventHandler is used instead of
407 // SetEventHandler, and then processing will be passed down the
408 // chain of event handlers.
411 wxWindow
*win
= (wxWindow
*)this;
413 // Can only use the validator of the window which
414 // is receiving the event
415 if ( win
== event
.GetEventObject() )
417 wxValidator
*validator
= win
->GetValidator();
418 if ( validator
&& validator
->ProcessEvent(event
) )
425 // Search upwards through the inheritance hierarchy
428 if ( SearchEventTable((wxEventTable
&)*table
, event
) )
430 table
= table
->baseTable
;
434 // Try going down the event handler chain
435 if ( GetNextHandler() )
437 if ( GetNextHandler()->ProcessEvent(event
) )
441 // Carry on up the parent-child hierarchy,
442 // but only if event is a command event: it wouldn't
443 // make sense for a parent to receive a child's size event, for example
444 if ( m_isWindow
&& event
.IsCommandEvent() )
446 wxWindow
*win
= (wxWindow
*)this;
447 wxWindow
*parent
= win
->GetParent();
448 if (parent
&& !parent
->IsBeingDeleted())
449 return parent
->GetEventHandler()->ProcessEvent(event
);
452 // Last try - application object.
453 if ( wxTheApp
&& (this != wxTheApp
) )
455 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
456 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
457 // processed appropriately via SearchEventTable.
458 if ( event
.GetEventType() != wxEVT_IDLE
)
460 if ( wxTheApp
->ProcessEvent(event
) )
468 bool wxEvtHandler::SearchEventTable(wxEventTable
& table
, wxEvent
& event
)
471 int commandId
= event
.GetId();
473 // BC++ doesn't like while (table.entries[i].m_fn)
476 while (table
.entries
[i
].m_fn
!= 0)
478 while (table
.entries
[i
].m_fn
!= 0L)
481 if ((event
.GetEventType() == table
.entries
[i
].m_eventType
) &&
482 (table
.entries
[i
].m_id
== -1 || // Match, if event spec says any id will do (id == -1)
483 (table
.entries
[i
].m_lastId
== -1 && commandId
== table
.entries
[i
].m_id
) ||
484 (table
.entries
[i
].m_lastId
!= -1 &&
485 (commandId
>= table
.entries
[i
].m_id
&& commandId
<= table
.entries
[i
].m_lastId
))))
488 event
.m_callbackUserData
= table
.entries
[i
].m_callbackUserData
;
490 (this->*((wxEventFunction
) (table
.entries
[i
].m_fn
)))(event
);
492 if ( event
.GetSkipped() )
501 void wxEvtHandler::Connect( int id
, int lastId
,
502 wxEventType eventType
,
503 wxObjectEventFunction func
,
506 wxEventTableEntry
*entry
= new wxEventTableEntry
;
508 entry
->m_lastId
= lastId
;
509 entry
->m_eventType
= eventType
;
511 entry
->m_callbackUserData
= userData
;
513 if (!m_dynamicEvents
)
514 m_dynamicEvents
= new wxList
;
516 m_dynamicEvents
->Append( (wxObject
*) entry
);
519 bool wxEvtHandler::SearchDynamicEventTable( wxEvent
& event
)
521 wxCHECK_MSG( m_dynamicEvents
, FALSE
,
522 _T("caller should check that we have dynamic events") );
524 int commandId
= event
.GetId();
526 wxNode
*node
= m_dynamicEvents
->First();
529 wxEventTableEntry
*entry
= (wxEventTableEntry
*)node
->Data();
533 // Match, if event spec says any id will do (id == -1)
534 if ( (event
.GetEventType() == entry
->m_eventType
) &&
535 (entry
->m_id
== -1 ||
536 (entry
->m_lastId
== -1 && commandId
== entry
->m_id
) ||
537 (entry
->m_lastId
!= -1 &&
538 (commandId
>= entry
->m_id
&& commandId
<= entry
->m_lastId
))) )
541 event
.m_callbackUserData
= entry
->m_callbackUserData
;
543 (this->*((wxEventFunction
) (entry
->m_fn
)))(event
);
545 if (event
.GetSkipped())
556 #if WXWIN_COMPATIBILITY
557 bool wxEvtHandler::OnClose()
559 if (GetNextHandler())
560 return GetNextHandler()->OnClose();
564 #endif // WXWIN_COMPATIBILITY
566 // Find a window with the focus, that is also a descendant of the given window.
567 // This is used to determine the window to initially send commands to.
568 wxWindow
* wxFindFocusDescendant(wxWindow
* ancestor
)
570 // Process events starting with the window with the focus, if any.
571 wxWindow
* focusWin
= wxWindow::FindFocus();
572 wxWindow
* win
= focusWin
;
574 // Check if this is a descendant of this frame.
575 // If not, win will be set to NULL.
581 win
= win
->GetParent();
583 if (win
== (wxWindow
*) NULL
)
584 focusWin
= (wxWindow
*) NULL
;