X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e306597309a120f2ae91385c731a5cb2722c52aa..95dee6514a24c2958b9571eb1c1a44ad3aad7a9e:/src/common/event.cpp diff --git a/src/common/event.cpp b/src/common/event.cpp index fb469f58e1..904178bc7b 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -78,6 +78,12 @@ #endif // !USE_SHARED_LIBRARY +#if wxUSE_THREADS +/* To put pending event handlers */ +extern wxList *wxPendingEvents; +extern wxCriticalSection *wxPendingEventsLocker; +#endif + /* * General wxWindows events, covering * all interesting things that might happen (button clicking, resizing, @@ -96,6 +102,7 @@ wxEvent::wxEvent(int theId) m_id = theId; m_skipped = FALSE; m_callbackUserData = (wxObject *) NULL; + m_isCommandEvent = FALSE; } /* @@ -112,6 +119,7 @@ wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId) m_commandInt = 0; m_id = theId; m_commandString = (char *) NULL; + m_isCommandEvent = TRUE; } /* @@ -163,7 +171,7 @@ bool wxMouseEvent::ButtonDClick(int but) const case 3: return RightDClick(); default: - wxFAIL_MSG("invalid parameter in wxMouseEvent::ButtonDClick"); + wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDClick")); } return FALSE; @@ -184,7 +192,7 @@ bool wxMouseEvent::ButtonDown(int but) const case 3: return RightDown(); default: - wxFAIL_MSG("invalid parameter in wxMouseEvent::ButtonDown"); + wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDown")); } return FALSE; @@ -204,7 +212,7 @@ bool wxMouseEvent::ButtonUp(int but) const case 3: return RightUp(); default: - wxFAIL_MSG("invalid parameter in wxMouseEvent::ButtonUp"); + wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonUp")); } return FALSE; @@ -223,7 +231,7 @@ bool wxMouseEvent::Button(int but) const case 3: return (RightDown() || RightUp() || RightDClick()); default: - wxFAIL_MSG("invalid parameter in wxMouseEvent::Button"); + wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::Button")); } return FALSE; @@ -241,7 +249,7 @@ bool wxMouseEvent::ButtonIsDown(int but) const case 3: return RightIsDown(); default: - wxFAIL_MSG("invalid parameter in wxMouseEvent::ButtonIsDown"); + wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonIsDown")); } return FALSE; @@ -280,6 +288,11 @@ wxEvtHandler::wxEvtHandler() m_previousHandler = (wxEvtHandler *) NULL; m_enabled = TRUE; m_dynamicEvents = (wxList *) NULL; + m_isWindow = FALSE; +#if wxUSE_THREADS + m_eventsLocker = new wxCriticalSection(); +#endif + m_pendingEvents = (wxList *) NULL; } wxEvtHandler::~wxEvtHandler() @@ -303,7 +316,53 @@ wxEvtHandler::~wxEvtHandler() } delete m_dynamicEvents; }; + + if (m_pendingEvents) + delete m_pendingEvents; + +#if wxUSE_THREADS + delete m_eventsLocker; +#endif +} + +#if wxUSE_THREADS +bool wxEvtHandler::ProcessThreadEvent(wxEvent& event) +{ + wxEvent *event_main; + wxCriticalSectionLocker locker(*m_eventsLocker); + + // check that we are really in a child thread + wxASSERT( !wxThread::IsMain() ); + + if (m_pendingEvents == NULL) + m_pendingEvents = new wxList(); + + event_main = (wxEvent *)event.GetClassInfo()->CreateObject(); + *event_main = event; + + m_pendingEvents->Append(event_main); + + wxPendingEventsLocker->Enter(); + wxPendingEvents->Append(this); + wxPendingEventsLocker->Leave(); + + return TRUE; +} + +void wxEvtHandler::ProcessPendingEvents() +{ + wxCriticalSectionLocker locker(*m_eventsLocker); + wxNode *node = m_pendingEvents->First(); + wxEvent *event; + + while (node != NULL) { + event = (wxEvent *)node->Data(); + ProcessEvent(*event); + delete node; + node = m_pendingEvents->First(); + } } +#endif /* * Event table stuff @@ -311,14 +370,20 @@ wxEvtHandler::~wxEvtHandler() bool wxEvtHandler::ProcessEvent(wxEvent& event) { - bool isWindow = IsKindOf(CLASSINFO(wxWindow)); + // check that our flag corresponds to reality + wxASSERT( m_isWindow == IsKindOf(CLASSINFO(wxWindow)) ); // An event handler can be enabled or disabled if ( GetEvtHandlerEnabled() ) { +#if wxUSE_THREADS + // Check whether we are in a child thread. + if (!wxThread::IsMain()) + return ProcessThreadEvent(event); +#endif // Handle per-instance dynamic event tables first - if (SearchDynamicEventTable( event )) + if ( m_dynamicEvents && SearchDynamicEventTable(event) ) return TRUE; // Then static per-class event tables @@ -333,24 +398,26 @@ bool wxEvtHandler::ProcessEvent(wxEvent& event) // THIS CAN BE CURED if PushEventHandler is used instead of // SetEventHandler, and then processing will be passed down the // chain of event handlers. - if ( isWindow ) + if ( m_isWindow ) { wxWindow *win = (wxWindow *)this; // Can only use the validator of the window which // is receiving the event - if ( (win == event.GetEventObject()) && - win->GetValidator() && - win->GetValidator()->ProcessEvent(event)) + if ( win == event.GetEventObject() ) { - return TRUE; + wxValidator *validator = win->GetValidator(); + if ( validator && validator->ProcessEvent(event) ) + { + return TRUE; + } } } // Search upwards through the inheritance hierarchy - while (table) + while ( table ) { - if (SearchEventTable((wxEventTable&)*table, event)) + if ( SearchEventTable((wxEventTable&)*table, event) ) return TRUE; table = table->baseTable; } @@ -366,23 +433,25 @@ bool wxEvtHandler::ProcessEvent(wxEvent& event) // Carry on up the parent-child hierarchy, // but only if event is a command event: it wouldn't // make sense for a parent to receive a child's size event, for example - if ( isWindow && event.IsKindOf(CLASSINFO(wxCommandEvent)) ) + if ( m_isWindow && event.IsCommandEvent() ) { wxWindow *win = (wxWindow *)this; wxWindow *parent = win->GetParent(); if (parent && !parent->IsBeingDeleted()) - return win->GetParent()->GetEventHandler()->ProcessEvent(event); + return parent->GetEventHandler()->ProcessEvent(event); } // Last try - application object. - // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always swallow - // it. wxEVT_IDLE is sent explicitly to wxApp so it will be processed - // appropriately via SearchEventTable. - if ( wxTheApp && (this != wxTheApp) && (event.GetEventType() != wxEVT_IDLE) - ) + if ( wxTheApp && (this != wxTheApp) ) { - if ( wxTheApp->ProcessEvent(event) ) - return TRUE; + // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always + // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be + // processed appropriately via SearchEventTable. + if ( event.GetEventType() != wxEVT_IDLE ) + { + if ( wxTheApp->ProcessEvent(event) ) + return TRUE; + } } return FALSE; @@ -421,7 +490,6 @@ bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event) } return FALSE; } - void wxEvtHandler::Connect( int id, int lastId, wxEventType eventType, wxObjectEventFunction func, @@ -442,7 +510,8 @@ void wxEvtHandler::Connect( int id, int lastId, bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event ) { - if (!m_dynamicEvents) return FALSE; + wxCHECK_MSG( m_dynamicEvents, FALSE, + _T("caller should check that we have dynamic events") ); int commandId = event.GetId(); @@ -484,5 +553,28 @@ bool wxEvtHandler::OnClose() else return FALSE; } -#endif +#endif // WXWIN_COMPATIBILITY + +// Find a window with the focus, that is also a descendant of the given window. +// This is used to determine the window to initially send commands to. +wxWindow* wxFindFocusDescendant(wxWindow* ancestor) +{ + // Process events starting with the window with the focus, if any. + wxWindow* focusWin = wxWindow::FindFocus(); + wxWindow* win = focusWin; + + // Check if this is a descendant of this frame. + // If not, win will be set to NULL. + while (win) + { + if (win == ancestor) + break; + else + win = win->GetParent(); + } + if (win == (wxWindow*) NULL) + focusWin = (wxWindow*) NULL; + + return focusWin; +}