Foundation classes for a basic GUI implementing simple widgets and events

Dependents:   TouchScreenGUIDemo

Events/EventDispatcher.cpp

Committer:
duncanFrance
Date:
2016-03-25
Revision:
0:0a590815d51c
Child:
12:63db16fea709

File content as of revision 0:0a590815d51c:

#include "EventDispatcher.h"

EventDispatcher::EventDispatcher() {
}



void EventDispatcher::attachListener(EventListener* l) {
    if(_listeners == NULL) {
        _listeners = new EventListenerWrapper(l);
    } else {
        EventListenerWrapper* w = new EventListenerWrapper(l);
        EventListenerWrapper* p = _listeners;
        while(p->next != NULL) {
            p = p->next;
        }
        p->next = w;
        w->prev = p;
    }   
}

void EventDispatcher::detachListener(EventListener* l) {
    
    EventListenerWrapper *p = _listeners;
    
    if(p == NULL) {
        return;
    }
    
    while(p->listener != l) {
        if(p->next == NULL) {
            return;
        }
        p = p->next;
    }
    
    // Found the listener
    // Is there only one in the list?
    if(p->prev == NULL && p->next == NULL) {
        _listeners = NULL;
    } else if(p->prev == NULL) {
        // First in the list. Move everything down
        _listeners = p->next;
        _listeners->prev = NULL;
    } else if(p->next == NULL) {
        p->prev->next = NULL;
    } else {
        // Middle of the list
        p->prev->next = p->next;
        p->next->prev = p->prev;
    }
    
    delete p;
}

void EventDispatcher::dispatchEvent(Event e) {
    EventListenerWrapper* p = _listeners;
    while(p != NULL) {
        if(p->listener->isEventTarget(e)) {
            p->listener->handleEvent(e);
        }
        
        p = p->next;
    }
}