EventDispatcher.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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-2023 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/Event.h"
  31. #include "../../Include/RmlUi/Core/Types.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) :
  38. id(id), in_capture_phase(in_capture_phase), listener(listener)
  39. {}
  40. EventId id;
  41. bool in_capture_phase;
  42. EventListener* listener;
  43. };
  44. /**
  45. The Event Dispatcher manages a list of event listeners and triggers the events via EventHandlers
  46. whenever requested.
  47. @author Lloyd Weehuizen
  48. */
  49. class EventDispatcher {
  50. public:
  51. /// Constructor
  52. /// @param element Element this dispatcher acts on
  53. EventDispatcher(Element* element);
  54. /// Destructor
  55. ~EventDispatcher();
  56. /// Attaches a new listener to the specified event name.
  57. /// @param[in] type Type of the event to attach to.
  58. /// @param[in] event_listener The event listener to be notified when the event fires.
  59. /// @param[in] in_capture_phase Should the listener be notified in the capture phase.
  60. void AttachEvent(EventId id, EventListener* event_listener, bool in_capture_phase);
  61. /// Detaches a listener from the specified event name
  62. /// @param[in] type Type of the event to attach to
  63. /// @param[in] event_listener The event listener to be notified when the event fires
  64. /// @param[in] in_capture_phase Should the listener be notified in the capture phase
  65. void DetachEvent(EventId id, EventListener* listener, bool in_capture_phase);
  66. /// Detaches all events from this dispatcher and all child dispatchers.
  67. void DetachAllEvents();
  68. /// Dispatches the specified event.
  69. /// @param[in] target_element The element to target
  70. /// @param[in] id The id of the event
  71. /// @param[in] type The type of the event
  72. /// @param[in] parameters The event parameters
  73. /// @param[in] interruptible Can the event propagation be stopped
  74. /// @param[in] bubbles True if the event should execute the bubble phase
  75. /// @param[in] default_action_phase The phases to execute default actions in
  76. /// @return True if the event was not consumed (ie, was prevented from propagating by an element), false if it was.
  77. static bool DispatchEvent(Element* target_element, EventId id, const String& type, const Dictionary& parameters, bool interruptible, bool bubbles,
  78. DefaultActionPhase default_action_phase);
  79. /// Returns event types with number of listeners for debugging.
  80. /// @return Summary of attached listeners.
  81. String ToString() const;
  82. private:
  83. Element* element;
  84. // Listeners are sorted first by (id, phase) and then by the order in which the listener was inserted.
  85. // All listeners added are unique.
  86. typedef Vector<EventListenerEntry> Listeners;
  87. Listeners listeners;
  88. // Collect all the listeners from this dispatcher that are allowed to execute given the input arguments.
  89. void CollectListeners(int dom_distance_from_target, EventId event_id, EventPhase phases_to_execute, Vector<CollectedListener>& collect_listeners);
  90. };
  91. } // namespace Rml
  92. #endif