2
0

EventSpecification.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "EventSpecification.h"
  29. #include "../../Include/RmlUi/Core/ID.h"
  30. namespace Rml {
  31. // An EventId is an index into the specifications vector.
  32. static Vector<EventSpecification> specifications = { { EventId::Invalid, "invalid", false, false, DefaultActionPhase::None } };
  33. // Reverse lookup map from event type to id.
  34. static UnorderedMap<String, EventId> type_lookup;
  35. namespace EventSpecificationInterface {
  36. void Initialize()
  37. {
  38. // Must be specified in the same order as in EventId
  39. specifications = {
  40. // id type interruptible bubbles default_action
  41. {EventId::Invalid , "invalid" , false , false , DefaultActionPhase::None},
  42. {EventId::Mousedown , "mousedown" , true , true , DefaultActionPhase::TargetAndBubble},
  43. {EventId::Mousescroll , "mousescroll" , true , true , DefaultActionPhase::TargetAndBubble},
  44. {EventId::Mouseover , "mouseover" , true , true , DefaultActionPhase::Target},
  45. {EventId::Mouseout , "mouseout" , true , true , DefaultActionPhase::Target},
  46. {EventId::Focus , "focus" , false , false , DefaultActionPhase::Target},
  47. {EventId::Blur , "blur" , false , false , DefaultActionPhase::Target},
  48. {EventId::Keydown , "keydown" , true , true , DefaultActionPhase::TargetAndBubble},
  49. {EventId::Keyup , "keyup" , true , true , DefaultActionPhase::TargetAndBubble},
  50. {EventId::Textinput , "textinput" , true , true , DefaultActionPhase::TargetAndBubble},
  51. {EventId::Mouseup , "mouseup" , true , true , DefaultActionPhase::TargetAndBubble},
  52. {EventId::Click , "click" , true , true , DefaultActionPhase::TargetAndBubble},
  53. {EventId::Dblclick , "dblclick" , true , true , DefaultActionPhase::TargetAndBubble},
  54. {EventId::Load , "load" , false , false , DefaultActionPhase::None},
  55. {EventId::Unload , "unload" , false , false , DefaultActionPhase::None},
  56. {EventId::Show , "show" , false , false , DefaultActionPhase::None},
  57. {EventId::Hide , "hide" , false , false , DefaultActionPhase::None},
  58. {EventId::Mousemove , "mousemove" , true , true , DefaultActionPhase::None},
  59. {EventId::Dragmove , "dragmove" , true , true , DefaultActionPhase::None},
  60. {EventId::Drag , "drag" , false , true , DefaultActionPhase::Target},
  61. {EventId::Dragstart , "dragstart" , false , true , DefaultActionPhase::Target},
  62. {EventId::Dragover , "dragover" , true , true , DefaultActionPhase::None},
  63. {EventId::Dragdrop , "dragdrop" , true , true , DefaultActionPhase::None},
  64. {EventId::Dragout , "dragout" , true , true , DefaultActionPhase::None},
  65. {EventId::Dragend , "dragend" , true , true , DefaultActionPhase::None},
  66. {EventId::Handledrag , "handledrag" , false , true , DefaultActionPhase::None},
  67. {EventId::Resize , "resize" , false , false , DefaultActionPhase::None},
  68. {EventId::Scroll , "scroll" , false , true , DefaultActionPhase::None},
  69. {EventId::Animationend , "animationend" , false , true , DefaultActionPhase::None},
  70. {EventId::Transitionend , "transitionend" , false , true , DefaultActionPhase::None},
  71. {EventId::Change , "change" , false , true , DefaultActionPhase::None},
  72. {EventId::Submit , "submit" , true , true , DefaultActionPhase::None},
  73. {EventId::Tabchange , "tabchange" , false , true , DefaultActionPhase::None},
  74. {EventId::Columnadd , "columnadd" , false , true , DefaultActionPhase::None},
  75. {EventId::Rowadd , "rowadd" , false , true , DefaultActionPhase::None},
  76. {EventId::Rowchange , "rowchange" , false , true , DefaultActionPhase::None},
  77. {EventId::Rowremove , "rowremove" , false , true , DefaultActionPhase::None},
  78. {EventId::Rowupdate , "rowupdate" , false , true , DefaultActionPhase::None},
  79. };
  80. type_lookup.clear();
  81. type_lookup.reserve(specifications.size());
  82. for (auto& specification : specifications)
  83. type_lookup.emplace(specification.type, specification.id);
  84. #ifdef RMLUI_DEBUG
  85. // Verify that all event ids are specified
  86. RMLUI_ASSERT((int)specifications.size() == (int)EventId::NumDefinedIds);
  87. for (int i = 0; i < (int)specifications.size(); i++)
  88. {
  89. // Verify correct order
  90. RMLUI_ASSERT(i == (int)specifications[i].id);
  91. }
  92. #endif
  93. }
  94. static EventSpecification& GetMutable(EventId id)
  95. {
  96. size_t i = static_cast<size_t>(id);
  97. if (i < specifications.size())
  98. return specifications[i];
  99. return specifications[0];
  100. }
  101. // Get event specification for the given type.
  102. // If not found: Inserts a new entry with given values.
  103. static EventSpecification& GetOrInsert(const String& event_type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  104. {
  105. auto it = type_lookup.find(event_type);
  106. if (it != type_lookup.end())
  107. return GetMutable(it->second);
  108. const size_t new_id_num = specifications.size();
  109. if (new_id_num >= size_t(EventId::MaxNumIds))
  110. {
  111. Log::Message(Log::LT_ERROR, "Error while registering event type '%s': Maximum number of allowed events exceeded.", event_type.c_str());
  112. RMLUI_ERROR;
  113. return specifications.front();
  114. }
  115. // No specification found for this name, insert a new entry with default values
  116. EventId new_id = static_cast<EventId>(new_id_num);
  117. specifications.push_back(EventSpecification{ new_id, event_type, interruptible, bubbles, default_action_phase });
  118. type_lookup.emplace(event_type, new_id);
  119. return specifications.back();
  120. }
  121. const EventSpecification& Get(EventId id)
  122. {
  123. return GetMutable(id);
  124. }
  125. const EventSpecification& GetOrInsert(const String& event_type)
  126. {
  127. // Default values for new event types defined as follows:
  128. constexpr bool interruptible = true;
  129. constexpr bool bubbles = true;
  130. constexpr DefaultActionPhase default_action_phase = DefaultActionPhase::None;
  131. return GetOrInsert(event_type, interruptible, bubbles, default_action_phase);
  132. }
  133. EventId GetIdOrInsert(const String& event_type)
  134. {
  135. auto it = type_lookup.find(event_type);
  136. if (it != type_lookup.end())
  137. return it->second;
  138. return GetOrInsert(event_type).id;
  139. }
  140. EventId InsertOrReplaceCustom(const String& event_type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  141. {
  142. const size_t size_before = specifications.size();
  143. EventSpecification& specification = GetOrInsert(event_type, interruptible, bubbles, default_action_phase);
  144. bool got_existing_entry = (size_before == specifications.size());
  145. // If we found an existing entry of same type, replace it, but only if it is a custom event id.
  146. if (got_existing_entry && (int)specification.id >= (int)EventId::FirstCustomId)
  147. {
  148. specification.interruptible = interruptible;
  149. specification.bubbles = bubbles;
  150. specification.default_action_phase = default_action_phase;
  151. }
  152. return specification.id;
  153. }
  154. }
  155. } // namespace Rml