EventDispatcher.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_EVENTDISPATCHER_H
  29. #define RMLUI_CORE_EVENTDISPATCHER_H
  30. #include "../../Include/RmlUi/Core/Types.h"
  31. #include "../../Include/RmlUi/Core/Event.h"
  32. namespace Rml {
  33. class Element;
  34. class EventListener;
  35. struct CollectedListener;
  36. struct EventListenerEntry {
  37. EventListenerEntry(const EventId id, EventListener* listener, const bool in_capture_phase) : id(id), in_capture_phase(in_capture_phase), listener(listener) {}
  38. EventId id;
  39. bool in_capture_phase;
  40. EventListener* listener;
  41. };
  42. /**
  43. The Event Dispatcher manages a list of event listeners and triggers the events via EventHandlers
  44. whenever requested.
  45. @author Lloyd Weehuizen
  46. */
  47. class EventDispatcher
  48. {
  49. public:
  50. /// Constructor
  51. /// @param element Element this dispatcher acts on
  52. EventDispatcher(Element* element);
  53. /// Destructor
  54. ~EventDispatcher();
  55. /// Attaches a new listener to the specified event name.
  56. /// @param[in] type Type of the event to attach to.
  57. /// @param[in] event_listener The event listener to be notified when the event fires.
  58. /// @param[in] in_capture_phase Should the listener be notified in the capture phase.
  59. void AttachEvent(EventId id, EventListener* event_listener, bool in_capture_phase);
  60. /// Detaches a listener from the specified event name
  61. /// @param[in] type Type of the event to attach to
  62. /// @param[in] event_listener The event listener to be notified when the event fires
  63. /// @param[in] in_capture_phase Should the listener be notified in the capture phase
  64. void DetachEvent(EventId id, EventListener* listener, bool in_capture_phase);
  65. /// Detaches all events from this dispatcher and all child dispatchers.
  66. void DetachAllEvents();
  67. /// Dispatches the specified event.
  68. /// @param[in] target_element The element to target
  69. /// @param[in] id The id of the event
  70. /// @param[in] type The type of the event
  71. /// @param[in] parameters The event parameters
  72. /// @param[in] interruptible Can the event propagation be stopped
  73. /// @param[in] bubbles True if the event should execute the bubble phase
  74. /// @param[in] default_action_phase The phases to execute default actions in
  75. /// @return True if the event was not consumed (ie, was prevented from propagating by an element), false if it was.
  76. static bool DispatchEvent(Element* target_element, EventId id, const String& type, const Dictionary& parameters, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase);
  77. /// Returns event types with number of listeners for debugging.
  78. /// @return Summary of attached listeners.
  79. String ToString() const;
  80. private:
  81. Element* element;
  82. // Listeners are sorted first by (id, phase) and then by the order in which the listener was inserted.
  83. // All listeners added are unique.
  84. typedef Vector< EventListenerEntry > Listeners;
  85. Listeners listeners;
  86. // Collect all the listeners from this dispatcher that are allowed to execute given the input arguments.
  87. void CollectListeners(int dom_distance_from_target, EventId event_id, EventPhase phases_to_execute, Vector<CollectedListener>& collect_listeners);
  88. };
  89. } // namespace Rml
  90. #endif