tagging release
[dasher.git] / trunk / Src / DasherCore / EventHandler.cpp
blob2b9cafbd8c22646b124aa5c85fa674995b6be7e0
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
39 m_pInterface->InterfaceEventHandler(pEvent);
41 m_pInterface->ExternalEventHandler(pEvent);
43 --m_iInHandler;
45 if(m_iInHandler == 0) {
46 for(std::vector < CDasherComponent * >::iterator iCurrent(m_vListenerQueue.begin()); iCurrent != m_vListenerQueue.end(); ++iCurrent)
47 m_vListeners.push_back(*iCurrent);
48 m_vListenerQueue.clear();
52 void CEventHandler::RegisterListener(CDasherComponent *pListener) {
54 if((std::find(m_vListeners.begin(), m_vListeners.end(), pListener) == m_vListeners.end()) &&
55 (std::find(m_vListenerQueue.begin(), m_vListenerQueue.end(), pListener) == m_vListenerQueue.end())) {
56 if(!m_iInHandler > 0)
57 m_vListeners.push_back(pListener);
58 else
59 m_vListenerQueue.push_back(pListener);
61 else {
62 // Can't add the same listener twice
66 void CEventHandler::UnregisterListener(CDasherComponent *pListener) {
68 std::vector < CDasherComponent * >::iterator iFound;
70 iFound = std::find(m_vListeners.begin(), m_vListeners.end(), pListener);
72 if(iFound != m_vListeners.end())
73 m_vListeners.erase(iFound);
75 iFound = std::find(m_vListenerQueue.begin(), m_vListenerQueue.end(), pListener);
77 if(iFound != m_vListenerQueue.end())
78 m_vListenerQueue.erase(iFound);