+static const int EVENT_TYPE_TABLE_INIT_SIZE = 31; // Not to big not to small...
+
+wxEventHashTable::wxEventHashTable(const wxEventTable &table)
+ : m_table(table),
+ m_rebuildHash(TRUE)
+{
+ AllocEventTypeTable(EVENT_TYPE_TABLE_INIT_SIZE);
+}
+
+wxEventHashTable::~wxEventHashTable()
+{
+ size_t i;
+ for(i = 0; i < m_size; i++)
+ {
+ EventTypeTablePointer eTTnode = m_eventTypeTable[i];
+ if (eTTnode)
+ {
+ delete eTTnode;
+ }
+ }
+
+ delete[] m_eventTypeTable;
+}
+
+bool wxEventHashTable::HandleEvent(wxEvent &event, wxEvtHandler *self)
+{
+ if (m_rebuildHash)
+ {
+ InitHashTable();
+ m_rebuildHash = 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.
+ int eventId = event.GetId();
+ const wxEventTableEntryPointerArray &eventEntryTable = eTTnode->eventEntryTable;
+
+ size_t n;
+ size_t count = eventEntryTable.GetCount();
+ for (n = 0; n < count; n++)
+ {
+ const wxEventTableEntry* entry = eventEntryTable[n];
+ int tableId1 = entry->m_id,
+ tableId2 = entry->m_lastId;
+
+ if ((tableId1 == -1) ||
+ (tableId2 == -1 && tableId1 == eventId) ||
+ (tableId2 != -1 &&
+ (eventId >= tableId1 && eventId <= tableId2)))
+ {
+ event.Skip(FALSE);
+ event.m_callbackUserData = entry->m_callbackUserData;
+
+ (self->*((wxEventFunction) (entry->m_fn)))(event);
+
+ if (!event.GetSkipped())
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+ }
+
+ 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;
+ }
+
+ // Lets 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)
+{
+ 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
+// ----------------------------------------------------------------------------