/* * This source file is part of libRocket, the HTML/CSS Interface Middleware * * For the latest information, see http://www.librocket.com * * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ #include "precompiled.h" #include "EventDispatcher.h" #include "../../Include/Rocket/Core/Element.h" #include "../../Include/Rocket/Core/Event.h" #include "../../Include/Rocket/Core/EventListener.h" #include "../../Include/Rocket/Core/Factory.h" #include "EventSpecification.h" namespace Rocket { namespace Core { EventDispatcher::EventDispatcher(Element* _element) { element = _element; } EventDispatcher::~EventDispatcher() { // Detach from all event dispatchers for (Events::iterator event_itr = events.begin(); event_itr != events.end(); ++event_itr) { for (Listeners::iterator listener_itr = (*event_itr).second.begin(); listener_itr != (*event_itr).second.end(); ++listener_itr) { (*listener_itr).listener->OnDetach(element); } } } void EventDispatcher::AttachEvent(EventId id, EventListener* listener, bool in_capture_phase) { // See if event type exists already Events::iterator event_itr = events.find(id); if (event_itr == events.end()) { // No, add listener to new event type entry event_itr = events.emplace(id, Listeners{ Listener(listener, in_capture_phase) }).first; } else { // Yes, add listener to the existing list of events for the type (*event_itr).second.emplace_back(listener, in_capture_phase); } listener->OnAttach(element); } void EventDispatcher::DetachEvent(EventId id, EventListener* listener, bool in_capture_phase) { // Look up the event Events::iterator event_itr = events.find(id); // Bail if we can't find the event if (event_itr == events.end()) { return; } // Find the relevant listener and erase it Listeners::iterator listener_itr = (*event_itr).second.begin(); while (listener_itr != (*event_itr).second.end()) { if ((*listener_itr).listener == listener && (*listener_itr).in_capture_phase == in_capture_phase) { listener_itr = (*event_itr).second.erase(listener_itr); listener->OnDetach(element); } else ++listener_itr; } } // Detaches all events from this dispatcher and all child dispatchers. void EventDispatcher::DetachAllEvents() { for (Events::iterator event_iterator = events.begin(); event_iterator != events.end(); ++event_iterator) { Listeners& listeners = event_iterator->second; for (size_t i = 0; i < listeners.size(); ++i) listeners[i].listener->OnDetach(element); } events.clear(); for (int i = 0; i < element->GetNumChildren(true); ++i) element->GetChild(i)->GetEventDispatcher()->DetachAllEvents(); } bool EventDispatcher::DispatchEvent(Element* target_element, EventId id, const String& type, const Dictionary& parameters, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase) { Event* event = Factory::InstanceEvent(target_element, id, type, parameters, interruptible); if (!event) return false; //// Click events may be disabled on the element. //if (id == EventId::Click && target_element->IsDisabled()) // return false; // Build the element traversal from the tree typedef std::vector Elements; Elements elements; Element* walk_element = target_element->GetParentNode(); while (walk_element) { elements.push_back(walk_element); walk_element = walk_element->GetParentNode(); } event->SetPhase(EventPhase::Capture); // Capture phase - root, to target (only events that have registered as capture events) // Note: We walk elements in REVERSE as they're placed in the list from the elements parent to the root for (int i = (int)elements.size() - 1; i >= 0 && event->IsPropagating(); i--) { EventDispatcher* dispatcher = elements[i]->GetEventDispatcher(); event->SetCurrentElement(elements[i]); dispatcher->TriggerEvents(event, default_action_phase); } // Target phase - direct at the target if (event->IsPropagating()) { event->SetPhase(EventPhase::Target); event->SetCurrentElement(target_element); TriggerEvents(event, default_action_phase); } if (bubbles && event->IsPropagating()) { event->SetPhase(EventPhase::Bubble); // Bubble phase - target to root (normal event bindings) for (size_t i = 0; i < elements.size() && event->IsPropagating(); i++) { EventDispatcher* dispatcher = elements[i]->GetEventDispatcher(); event->SetCurrentElement(elements[i]); dispatcher->TriggerEvents(event, default_action_phase); } } bool propagating = event->IsPropagating(); event->RemoveReference(); return propagating; } String EventDispatcher::ToString() const { String result; for (auto nvp : events) { const EventSpecification& specification = EventSpecificationInterface::Get(nvp.first); result += CreateString(specification.type.size() + 32, "%s (%d), ", specification.type.c_str(), static_cast(nvp.second.size())); } if (result.size() > 2) { result.resize(result.size() - 2); } return result; } void EventDispatcher::TriggerEvents(Event* event, DefaultActionPhase default_action_phase) { const EventPhase phase = event->GetPhase(); // Look up the event Events::iterator itr = events.find(event->GetId()); if (itr != events.end()) { // Dispatch all actions Listeners& listeners = (*itr).second; for (size_t i = 0; i < listeners.size(); i++) { if (phase == EventPhase::Target || (phase == EventPhase::Capture && listeners[i].in_capture_phase) || (phase == EventPhase::Bubble && !listeners[i].in_capture_phase)) { listeners[i].listener->ProcessEvent(*event); } } } const bool do_default_action = ((int)phase & (int)default_action_phase); if (do_default_action) { element->ProcessDefaultAction(*event); } } } }