+#endif // wxUSE_GUI
+
+
+#if wxUSE_BASE
+
+// ----------------------------------------------------------------------------
+// wxEventHashTable
+// ----------------------------------------------------------------------------
+
+static const int EVENT_TYPE_TABLE_INIT_SIZE = 31; // Not too big not too small...
+
+wxEventHashTable* wxEventHashTable::sm_first = NULL;
+
+wxEventHashTable::wxEventHashTable(const wxEventTable &table)
+ : m_table(table),
+ m_rebuildHash(true)
+{
+ AllocEventTypeTable(EVENT_TYPE_TABLE_INIT_SIZE);
+
+ m_next = sm_first;
+ if (m_next)
+ m_next->m_previous = this;
+ sm_first = this;
+}
+
+wxEventHashTable::~wxEventHashTable()
+{
+ if (m_next)
+ m_next->m_previous = m_previous;
+ if (m_previous)
+ m_previous->m_next = m_next;
+ if (sm_first == this)
+ sm_first = m_next;
+
+ Clear();
+}
+
+void wxEventHashTable::Clear()
+{
+ for ( size_t i = 0; i < m_size; i++ )
+ {
+ EventTypeTablePointer eTTnode = m_eventTypeTable[i];
+ delete eTTnode;
+ }
+
+ wxDELETEA(m_eventTypeTable);
+
+ m_size = 0;
+}
+
+#if wxUSE_MEMORY_TRACING
+
+// Clear all tables
+void wxEventHashTable::ClearAll()
+{
+ wxEventHashTable* table = sm_first;
+ while (table)
+ {
+ table->Clear();
+ table = table->m_next;
+ }
+}
+
+#endif // wxUSE_MEMORY_TRACING
+
+bool wxEventHashTable::HandleEvent(wxEvent &event, wxEvtHandler *self)
+{
+ if (m_rebuildHash)
+ {
+ InitHashTable();
+ m_rebuildHash = false;
+ }
+
+ if (!m_eventTypeTable)
+ return false;
+
+ // Find all entries for the given event type.
+ wxEventType eventType = event.GetEventType();
+ const EventTypeTablePointer eTTnode = m_eventTypeTable[eventType % m_size];
+ if (eTTnode && eTTnode->eventType == eventType)
+ {
+ // Now start the search for an event handler
+ // that can handle an event with the given ID.
+ const wxEventTableEntryPointerArray&
+ eventEntryTable = eTTnode->eventEntryTable;
+
+ const size_t count = eventEntryTable.GetCount();
+ for (size_t n = 0; n < count; n++)
+ {
+ const wxEventTableEntry& entry = *eventEntryTable[n];
+ if ( wxEvtHandler::ProcessEventIfMatchesId(entry, self, event) )
+ return true;
+ }
+ }
+
+ return false;
+}
+
+void wxEventHashTable::InitHashTable()
+{
+ // Loop over the event tables and all its base tables.
+ const wxEventTable *table = &m_table;
+ while (table)
+ {
+ // Retrieve all valid event handler entries
+ const wxEventTableEntry *entry = table->entries;
+ while (entry->m_fn != 0)
+ {
+ // Add the event entry in the Hash.
+ AddEntry(*entry);
+
+ entry++;
+ }
+
+ table = table->baseTable;
+ }
+
+ // Let's free some memory.
+ size_t i;
+ for(i = 0; i < m_size; i++)
+ {
+ EventTypeTablePointer eTTnode = m_eventTypeTable[i];
+ if (eTTnode)
+ {
+ eTTnode->eventEntryTable.Shrink();
+ }
+ }
+}
+
+void wxEventHashTable::AddEntry(const wxEventTableEntry &entry)
+{
+ // This might happen 'accidentally' as the app is exiting
+ if (!m_eventTypeTable)
+ return;
+
+ EventTypeTablePointer *peTTnode = &m_eventTypeTable[entry.m_eventType % m_size];
+ EventTypeTablePointer eTTnode = *peTTnode;
+
+ if (eTTnode)
+ {
+ if (eTTnode->eventType != entry.m_eventType)
+ {
+ // Resize the table!
+ GrowEventTypeTable();
+ // Try again to add it.
+ AddEntry(entry);
+ return;
+ }
+ }
+ else
+ {
+ eTTnode = new EventTypeTable;
+ eTTnode->eventType = entry.m_eventType;
+ *peTTnode = eTTnode;
+ }
+
+ // Fill all hash entries between entry.m_id and entry.m_lastId...
+ eTTnode->eventEntryTable.Add(&entry);
+}
+
+void wxEventHashTable::AllocEventTypeTable(size_t size)
+{
+ m_eventTypeTable = new EventTypeTablePointer[size];
+ memset((void *)m_eventTypeTable, 0, sizeof(EventTypeTablePointer)*size);
+ m_size = size;
+}
+
+void wxEventHashTable::GrowEventTypeTable()
+{
+ size_t oldSize = m_size;
+ EventTypeTablePointer *oldEventTypeTable = m_eventTypeTable;
+
+ // TODO: Search the most optimal grow sequence
+ AllocEventTypeTable(/* GetNextPrime(oldSize) */oldSize*2+1);
+
+ for ( size_t i = 0; i < oldSize; /* */ )
+ {
+ EventTypeTablePointer eTToldNode = oldEventTypeTable[i];
+ if (eTToldNode)
+ {
+ EventTypeTablePointer *peTTnode = &m_eventTypeTable[eTToldNode->eventType % m_size];
+ EventTypeTablePointer eTTnode = *peTTnode;
+
+ // Check for collision, we don't want any.
+ if (eTTnode)
+ {
+ GrowEventTypeTable();
+ continue; // Don't increment the counter,
+ // as we still need to add this element.
+ }
+ else
+ {
+ // Get the old value and put it in the new table.
+ *peTTnode = oldEventTypeTable[i];
+ }
+ }
+
+ i++;
+ }
+
+ delete[] oldEventTypeTable;
+}
+
+// ----------------------------------------------------------------------------
+// wxEvtHandler
+// ----------------------------------------------------------------------------
+
+wxEvtHandler::wxEvtHandler()
+{
+ m_nextHandler = NULL;
+ m_previousHandler = NULL;
+ m_enabled = true;
+ m_dynamicEvents = NULL;
+ m_pendingEvents = NULL;
+
+ // no client data (yet)
+ m_clientData = NULL;
+ m_clientDataType = wxClientData_None;
+}
+
+wxEvtHandler::~wxEvtHandler()
+{
+ Unlink();
+
+ if (m_dynamicEvents)
+ {
+ for ( wxList::iterator it = m_dynamicEvents->begin(),
+ end = m_dynamicEvents->end();
+ it != end;
+ ++it )
+ {
+ wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it;
+
+ // Remove ourselves from sink destructor notifications
+ // (this has usually been done, in wxTrackable destructor)
+ wxEvtHandler *eventSink = entry->m_fn->GetEvtHandler();
+ if ( eventSink )
+ {
+ wxEventConnectionRef * const
+ evtConnRef = FindRefInTrackerList(eventSink);
+ if ( evtConnRef )
+ {
+ eventSink->RemoveNode(evtConnRef);
+ delete evtConnRef;
+ }
+ }
+
+ delete entry->m_callbackUserData;
+ delete entry;
+ }
+ delete m_dynamicEvents;
+ }
+
+ // Remove us from the list of the pending events if necessary.
+ if (wxTheApp)
+ wxTheApp->RemovePendingEventHandler(this);
+
+ DeletePendingEvents();
+
+ // we only delete object data, not untyped
+ if ( m_clientDataType == wxClientData_Object )
+ delete m_clientObject;
+}
+
+void wxEvtHandler::Unlink()
+{
+ // this event handler must take itself out of the chain of handlers:
+
+ if (m_previousHandler)
+ m_previousHandler->SetNextHandler(m_nextHandler);
+
+ if (m_nextHandler)
+ m_nextHandler->SetPreviousHandler(m_previousHandler);
+
+ m_nextHandler = NULL;
+ m_previousHandler = NULL;
+}
+
+bool wxEvtHandler::IsUnlinked() const
+{
+ return m_previousHandler == NULL &&
+ m_nextHandler == NULL;
+}
+
+#if wxUSE_THREADS
+
+bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event)
+{
+ // check that we are really in a child thread
+ wxASSERT_MSG( !wxThread::IsMain(),
+ wxT("use ProcessEvent() in main thread") );
+
+ AddPendingEvent(event);
+
+ return true;
+}
+
+#endif // wxUSE_THREADS
+
+void wxEvtHandler::QueueEvent(wxEvent *event)
+{
+ wxCHECK_RET( event, "NULL event can't be posted" );
+
+ if (!wxTheApp)
+ {
+ // we need an event loop which manages the list of event handlers with
+ // pending events... cannot proceed without it!
+ wxLogDebug("No application object! Cannot queue this event!");
+
+ // anyway delete the given event to avoid memory leaks
+ delete event;
+
+ return;
+ }
+
+ // 1) Add this event to our list of pending events
+ wxENTER_CRIT_SECT( m_pendingEventsLock );
+
+ if ( !m_pendingEvents )
+ m_pendingEvents = new wxList;
+
+ m_pendingEvents->Append(event);
+
+ // 2) Add this event handler to list of event handlers that
+ // have pending events.
+
+ wxTheApp->AppendPendingEventHandler(this);
+
+ // only release m_pendingEventsLock now because otherwise there is a race
+ // condition as described in the ticket #9093: we could process the event
+ // just added to m_pendingEvents in our ProcessPendingEvents() below before
+ // we had time to append this pointer to wxHandlersWithPendingEvents list; thus
+ // breaking the invariant that a handler should be in the list iff it has
+ // any pending events to process
+ wxLEAVE_CRIT_SECT( m_pendingEventsLock );
+
+ // 3) Inform the system that new pending events are somewhere,
+ // and that these should be processed in idle time.
+ wxWakeUpIdle();
+}
+
+void wxEvtHandler::DeletePendingEvents()
+{
+ if (m_pendingEvents)
+ m_pendingEvents->DeleteContents(true);
+ wxDELETE(m_pendingEvents);
+}
+
+void wxEvtHandler::ProcessPendingEvents()
+{
+ if (!wxTheApp)
+ {
+ // we need an event loop which manages the list of event handlers with
+ // pending events... cannot proceed without it!
+ wxLogDebug("No application object! Cannot process pending events!");
+ return;
+ }
+
+ // we need to process only a single pending event in this call because
+ // each call to ProcessEvent() could result in the destruction of this
+ // same event handler (see the comment at the end of this function)
+
+ wxENTER_CRIT_SECT( m_pendingEventsLock );
+
+ // this method is only called by wxApp if this handler does have
+ // pending events
+ wxCHECK_RET( m_pendingEvents && !m_pendingEvents->IsEmpty(),
+ "should have pending events if called" );
+
+ wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
+ wxEvent* pEvent = static_cast<wxEvent *>(node->GetData());
+
+ // find the first event which can be processed now:
+ wxEventLoopBase* evtLoop = wxEventLoopBase::GetActive();
+ if (evtLoop && evtLoop->IsYielding())
+ {
+ while (node && pEvent && !evtLoop->IsEventAllowedInsideYield(pEvent->GetEventCategory()))
+ {
+ node = node->GetNext();
+ pEvent = node ? static_cast<wxEvent *>(node->GetData()) : NULL;
+ }
+
+ if (!node)
+ {
+ // all our events are NOT processable now... signal this:
+ wxTheApp->DelayPendingEventHandler(this);
+
+ // see the comment at the beginning of evtloop.h header for the
+ // logic behind YieldFor() and behind DelayPendingEventHandler()
+
+ wxLEAVE_CRIT_SECT( m_pendingEventsLock );
+
+ return;
+ }
+ }
+
+ wxEventPtr event(pEvent);
+
+ // it's important we remove event from list before processing it, else a
+ // nested event loop, for example from a modal dialog, might process the
+ // same event again.
+ m_pendingEvents->Erase(node);
+
+ if ( m_pendingEvents->IsEmpty() )
+ {
+ // if there are no more pending events left, we don't need to
+ // stay in this list
+ wxTheApp->RemovePendingEventHandler(this);
+ }
+
+ wxLEAVE_CRIT_SECT( m_pendingEventsLock );
+
+ ProcessEvent(*event);
+
+ // careful: this object could have been deleted by the event handler
+ // executed by the above ProcessEvent() call, so we can't access any fields
+ // of this object any more
+}
+
+/* static */
+bool wxEvtHandler::ProcessEventIfMatchesId(const wxEventTableEntryBase& entry,
+ wxEvtHandler *handler,
+ wxEvent& event)
+{
+ int tableId1 = entry.m_id,
+ tableId2 = entry.m_lastId;
+
+ // match only if the event type is the same and the id is either -1 in
+ // the event table (meaning "any") or the event id matches the id
+ // specified in the event table either exactly or by falling into
+ // range between first and last
+ if ((tableId1 == wxID_ANY) ||
+ (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
+ (tableId2 != wxID_ANY &&
+ (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
+ {
+ event.Skip(false);
+ event.m_callbackUserData = entry.m_callbackUserData;
+
+#if wxUSE_EXCEPTIONS
+ if ( wxTheApp )
+ {
+ // call the handler via wxApp method which allows the user to catch
+ // any exceptions which may be thrown by any handler in the program
+ // in one place
+ wxTheApp->CallEventHandler(handler, *entry.m_fn, event);
+ }
+ else
+#endif // wxUSE_EXCEPTIONS
+ {
+ (*entry.m_fn)(handler, event);
+ }
+
+ if (!event.GetSkipped())
+ return true;
+ }
+
+ return false;
+}
+
+bool wxEvtHandler::DoTryApp(wxEvent& event)
+{
+ if ( wxTheApp && (this != wxTheApp) )
+ {
+ // 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;
+}
+
+bool wxEvtHandler::TryBefore(wxEvent& event)
+{
+#if WXWIN_COMPATIBILITY_2_8
+ // call the old virtual function to keep the code overriding it working
+ return TryValidator(event);
+#else
+ wxUnusedVar(event);
+ return false;
+#endif
+}
+
+bool wxEvtHandler::TryAfter(wxEvent& event)
+{
+ // We only want to pass the window to the application object once even if
+ // there are several chained handlers. Ensure that this is what happens by
+ // only calling DoTryApp() if there is no next handler (which would do it).
+ //
+ // Notice that, unlike simply calling TryAfter() on the last handler in the
+ // chain only from ProcessEvent(), this also works with wxWindow object in
+ // the middle of the chain: its overridden TryAfter() will still be called
+ // and propagate the event upwards the window hierarchy even if it's not
+ // the last one in the chain (which, admittedly, shouldn't happen often).
+ if ( GetNextHandler() )
+ return GetNextHandler()->TryAfter(event);
+
+#if WXWIN_COMPATIBILITY_2_8
+ // as above, call the old virtual function for compatibility
+ return TryParent(event);
+#else
+ return DoTryApp(event);
+#endif
+}