2 #include "../Common/Common.h"
4 #include "EventHandler.h"
5 #include "DasherComponent.h"
6 #include "DasherInterfaceBase.h"
11 using namespace Dasher
;
13 // Track memory leaks on Windows to the line that new'd the memory
16 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
19 static char THIS_FILE
[] = __FILE__
;
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
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
);
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())) {
54 m_vListeners
.push_back(pListener
);
56 m_vListenerQueue
.push_back(pListener
);
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
);