EventSpecification.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "EventSpecification.h"
  29. #include "../../Include/Rocket/Core/ID.h"
  30. namespace Rocket {
  31. namespace Core {
  32. // An EventId is an index into the specifications vector.
  33. static std::vector<EventSpecification> specifications = { { EventId::Invalid, "invalid", false, false, DefaultActionPhase::None } };
  34. // Reverse lookup map from event type to id.
  35. static UnorderedMap<String, EventId> type_lookup;
  36. namespace EventSpecificationInterface {
  37. void Initialize()
  38. {
  39. // Must be specified in the same order as in EventId
  40. specifications = {
  41. // id type interruptible bubbles default_action
  42. {EventId::Invalid , "invalid" , false , false , DefaultActionPhase::None},
  43. {EventId::Mousedown , "mousedown" , true , true , DefaultActionPhase::TargetAndBubble},
  44. {EventId::Mousescroll , "mousescroll" , true , true , DefaultActionPhase::TargetAndBubble},
  45. {EventId::Mouseover , "mouseover" , true , true , DefaultActionPhase::Target},
  46. {EventId::Mouseout , "mouseout" , true , true , DefaultActionPhase::Target},
  47. {EventId::Focus , "focus" , false , false , DefaultActionPhase::Target},
  48. {EventId::Blur , "blur" , false , false , DefaultActionPhase::Target},
  49. {EventId::Keydown , "keydown" , true , true , DefaultActionPhase::TargetAndBubble},
  50. {EventId::Keyup , "keyup" , true , true , DefaultActionPhase::TargetAndBubble},
  51. {EventId::Textinput , "textinput" , true , true , DefaultActionPhase::TargetAndBubble},
  52. {EventId::Mouseup , "mouseup" , true , true , DefaultActionPhase::TargetAndBubble},
  53. {EventId::Click , "click" , true , true , DefaultActionPhase::TargetAndBubble},
  54. {EventId::Dblclick , "dblclick" , true , true , DefaultActionPhase::TargetAndBubble},
  55. {EventId::Load , "load" , false , false , DefaultActionPhase::None},
  56. {EventId::Unload , "unload" , false , false , DefaultActionPhase::None},
  57. {EventId::Show , "show" , false , false , DefaultActionPhase::None},
  58. {EventId::Hide , "hide" , false , false , DefaultActionPhase::None},
  59. {EventId::Mousemove , "mousemove" , true , true , DefaultActionPhase::None},
  60. {EventId::Dragmove , "dragmove" , true , true , DefaultActionPhase::None},
  61. {EventId::Drag , "drag" , false , true , DefaultActionPhase::Target},
  62. {EventId::Dragstart , "dragstart" , false , true , DefaultActionPhase::Target},
  63. {EventId::Dragover , "dragover" , true , true , DefaultActionPhase::None},
  64. {EventId::Dragdrop , "dragdrop" , true , true , DefaultActionPhase::None},
  65. {EventId::Dragout , "dragout" , true , true , DefaultActionPhase::None},
  66. {EventId::Dragend , "dragend" , true , true , DefaultActionPhase::None},
  67. {EventId::Handledrag , "handledrag" , false , true , DefaultActionPhase::None},
  68. {EventId::Resize , "resize" , false , false , DefaultActionPhase::None},
  69. {EventId::Scroll , "scroll" , false , true , DefaultActionPhase::None},
  70. {EventId::Animationend , "animationend" , false , true , DefaultActionPhase::None},
  71. {EventId::Transitionend , "transitionend" , false , true , DefaultActionPhase::None},
  72. {EventId::Change , "change" , false , true , DefaultActionPhase::None},
  73. {EventId::Submit , "submit" , true , true , DefaultActionPhase::None},
  74. {EventId::Tabchange , "tabchange" , false , true , DefaultActionPhase::None},
  75. {EventId::Columnadd , "columnadd" , false , true , DefaultActionPhase::None},
  76. {EventId::Rowadd , "rowadd" , false , true , DefaultActionPhase::None},
  77. {EventId::Rowchange , "rowchange" , false , true , DefaultActionPhase::None},
  78. {EventId::Rowremove , "rowremove" , false , true , DefaultActionPhase::None},
  79. {EventId::Rowupdate , "rowupdate" , false , true , DefaultActionPhase::None},
  80. };
  81. type_lookup.clear();
  82. type_lookup.reserve(specifications.size());
  83. for (auto& specification : specifications)
  84. type_lookup.emplace(specification.type, specification.id);
  85. #ifdef ROCKET_DEBUG
  86. // Verify that all event ids are specified
  87. ROCKET_ASSERT((int)specifications.size() == (int)EventId::NumDefinedIds);
  88. for (int i = 0; i < (int)specifications.size(); i++)
  89. {
  90. // Verify correct order
  91. ROCKET_ASSERT(i == (int)specifications[i].id);
  92. }
  93. #endif
  94. }
  95. static EventSpecification& GetMutable(EventId id)
  96. {
  97. size_t i = static_cast<size_t>(id);
  98. if (i < specifications.size())
  99. return specifications[i];
  100. return specifications[0];
  101. }
  102. // Get event specification for the given type.
  103. // If not found: Inserts a new entry with given values.
  104. static EventSpecification& GetOrInsert(const String& event_type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  105. {
  106. auto it = type_lookup.find(event_type);
  107. if (it != type_lookup.end())
  108. return GetMutable(it->second);
  109. // No specification found for this name, insert a new entry with default values
  110. EventId new_id = static_cast<EventId>(specifications.size());
  111. specifications.push_back(EventSpecification{ new_id, event_type, interruptible, bubbles, default_action_phase });
  112. type_lookup.emplace(event_type, new_id);
  113. return specifications.back();
  114. }
  115. const EventSpecification& Get(EventId id)
  116. {
  117. return GetMutable(id);
  118. }
  119. const EventSpecification& GetOrInsert(const String& event_type)
  120. {
  121. // Default values for new event types defined as follows:
  122. constexpr bool interruptible = true;
  123. constexpr bool bubbles = true;
  124. constexpr DefaultActionPhase default_action_phase = DefaultActionPhase::None;
  125. return GetOrInsert(event_type, interruptible, bubbles, default_action_phase);
  126. }
  127. EventId GetIdOrInsert(const String& event_type)
  128. {
  129. auto it = type_lookup.find(event_type);
  130. if (it != type_lookup.end())
  131. return it->second;
  132. return GetOrInsert(event_type).id;
  133. }
  134. EventId InsertOrReplaceCustom(const String& event_type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  135. {
  136. const size_t size_before = specifications.size();
  137. EventSpecification& specification = GetOrInsert(event_type, interruptible, bubbles, default_action_phase);
  138. bool got_existing_entry = (size_before == specifications.size());
  139. // If we found an existing entry of same type, replace it, but only if it is a custom event id.
  140. if (got_existing_entry && (int)specification.id >= (int)EventId::FirstCustomId)
  141. {
  142. specification.interruptible = interruptible;
  143. specification.bubbles = bubbles;
  144. specification.default_action_phase = default_action_phase;
  145. }
  146. return specification.id;
  147. }
  148. }
  149. }
  150. }