+ // 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;
+}
+
+wxEventFilter* wxEvtHandler::ms_filterList = NULL;
+
+/* static */ void wxEvtHandler::AddFilter(wxEventFilter* filter)
+{
+ wxCHECK_RET( filter, "NULL filter" );
+
+ filter->m_next = ms_filterList;
+ ms_filterList = filter;
+}
+
+/* static */ void wxEvtHandler::RemoveFilter(wxEventFilter* filter)
+{
+ wxEventFilter* prev = NULL;
+ for ( wxEventFilter* f = ms_filterList; f; f = f->m_next )
+ {
+ if ( f == filter )
+ {
+ // Set the previous list element or the list head to the next
+ // element.
+ if ( prev )
+ prev->m_next = f->m_next;
+ else
+ ms_filterList = f->m_next;
+
+ // Also reset the next pointer in the filter itself just to avoid
+ // having possibly dangling pointers, even though it's not strictly
+ // necessary.
+ f->m_next = NULL;
+
+ // Skip the assert below.
+ return;
+ }
+ }
+
+ wxFAIL_MSG( "Filter not found" );