Various small changes
[dasher.git] / Src / DasherCore / EventHandler.cpp
bloba473da5e53c3ffe4a5f845b8ab9e4fcf1f01c2b2
2 #include "../Common/Common.h"
4 #include "EventHandler.h"
5 #include "DasherComponent.h"
6 #include "DasherInterfaceBase.h"
8 #include <algorithm>
9 #include <iostream>
11 using namespace Dasher;
13 // Track memory leaks on Windows to the line that new'd the memory
14 #ifdef _WIN32
15 #ifdef _DEBUG
16 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
17 #define new DEBUG_NEW
18 #undef THIS_FILE
19 static char THIS_FILE[] = __FILE__;
20 #endif
21 #endif
23 void CEventHandler::InsertEvent(CEvent *pEvent) {
25 // We may end up here recursively, so keep track of how far down we
26 // are, and only permit new handlers to be registered after all
27 // messages are processed.
29 // An alternative approach would be a message queue - this might actually be a bit more sensible
30 ++m_iInHandler;
32 // Loop through components and notify them of the event
33 for(std::vector < CDasherComponent * >::iterator iCurrent(m_vListeners.begin()); iCurrent != m_vListeners.end(); ++iCurrent) {
34 (*iCurrent)->HandleEvent(pEvent);
37 // Call external handler last, to make sure that internal components are fully up to date before external events happen
38 m_pInterface->InterfaceEventHandler(pEvent);
39 m_pInterface->ExternalEventHandler(pEvent);
41 --m_iInHandler;
43 if(m_iInHandler == 0) {
44 for(std::vector < CDasherComponent * >::iterator iCurrent(m_vListenerQueue.begin()); iCurrent != m_vListenerQueue.end(); ++iCurrent)
45 m_vListeners.push_back(*iCurrent);
46 m_vListenerQueue.clear();
50 void CEventHandler::RegisterListener(CDasherComponent *pListener) {
51 if((std::find(m_vListeners.begin(), m_vListeners.end(), pListener) == m_vListeners.end()) &&
52 (std::find(m_vListenerQueue.begin(), m_vListenerQueue.end(), pListener) == m_vListenerQueue.end())) {
53 if(!m_iInHandler > 0)
54 m_vListeners.push_back(pListener);
55 else
56 m_vListenerQueue.push_back(pListener);
58 else {
59 // Can't add the same listener twice
63 void CEventHandler::UnregisterListener(CDasherComponent *pListener) {
65 std::vector < CDasherComponent * >::iterator iFound;
67 iFound = std::find(m_vListeners.begin(), m_vListeners.end(), pListener);
69 if(iFound != m_vListeners.end())
70 m_vListeners.erase(iFound);