EventDispatcher.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 RMLUICOREEVENTDISPATCHER_H
  29. #define RMLUICOREEVENTDISPATCHER_H
  30. #include "../../Include/RmlUi/Core/String.h"
  31. #include "../../Include/RmlUi/Core/Event.h"
  32. #include <map>
  33. namespace Rml {
  34. namespace Core {
  35. class Element;
  36. class EventListener;
  37. /**
  38. The Event Dispatcher manages a list of event listeners (based on URL) and triggers the events via EventHandlers
  39. whenever requested.
  40. @author Lloyd Weehuizen
  41. */
  42. class EventDispatcher
  43. {
  44. public:
  45. /// Constructor
  46. /// @param element Element this dispatcher acts on
  47. EventDispatcher(Element* element);
  48. // Destructor
  49. ~EventDispatcher();
  50. /// Attaches a new listener to the specified event name
  51. /// @param[in] type Type of the event to attach to
  52. /// @param[in] event_listener The event listener to be notified when the event fires
  53. /// @param[in] in_capture_phase Should the listener be notified in the capture phase
  54. void AttachEvent(const String& type, EventListener* event_listener, bool in_capture_phase);
  55. /// Detaches a listener from the specified event name
  56. /// @param[in] type Type of the event to attach to
  57. /// @para[in]m 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 DetachEvent(const String& type, EventListener* listener, bool in_capture_phase);
  60. /// Detaches all events from this dispatcher and all child dispatchers.
  61. void DetachAllEvents();
  62. /// Dispatches the specified event with element as the target
  63. /// @param[in] target_element The target element of the event
  64. /// @param[in] name The name of the event
  65. /// @param[in] parameters The event parameters
  66. /// @param[in] interruptible Can the event propagation be stopped
  67. /// @return True if the event was not consumed (ie, was prevented from propagating by an element), false if it was.
  68. bool DispatchEvent(Element* element, const String& name, const Dictionary& parameters, bool interruptible);
  69. /// Returns event types with number of listeners for debugging.
  70. /// @return Summary of attached listeners.
  71. String ToString() const;
  72. private:
  73. Element* element;
  74. struct Listener
  75. {
  76. Listener(EventListener* _listener, bool _in_capture_phase) : listener(_listener), in_capture_phase(_in_capture_phase) {}
  77. EventListener* listener;
  78. bool in_capture_phase;
  79. };
  80. typedef std::vector< Listener > Listeners;
  81. typedef std::unordered_map< String, Listeners > Events;
  82. Events events;
  83. void TriggerEvents(Event* event);
  84. };
  85. }
  86. }
  87. #endif