EventSpecification.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. static UnorderedMap<String, EventId> type_lookup;
  35. namespace EventSpecificationInterface {
  36. void Initialize()
  37. {
  38. // Must be specified in the same order as their event_id
  39. specifications = {
  40. {EventId::Invalid , "invalid" , false , false , DefaultActionPhase::None},
  41. {EventId::Mousedown , "mousedown" , true , true , DefaultActionPhase::TargetAndBubble},
  42. {EventId::Mousescroll , "mousescroll" , true , true , DefaultActionPhase::TargetAndBubble},
  43. {EventId::Mouseover , "mouseover" , true , true , DefaultActionPhase::Target},
  44. {EventId::Mouseout , "mouseout" , true , true , DefaultActionPhase::Target},
  45. {EventId::Focus , "focus" , false , false , DefaultActionPhase::Target},
  46. {EventId::Blur , "blur" , false , false , DefaultActionPhase::Target},
  47. {EventId::Keydown , "keydown" , true , true , DefaultActionPhase::TargetAndBubble},
  48. {EventId::Keyup , "keyup" , true , true , DefaultActionPhase::TargetAndBubble},
  49. {EventId::Textinput , "textinput" , true , true , DefaultActionPhase::TargetAndBubble},
  50. {EventId::Mouseup , "mouseup" , true , true , DefaultActionPhase::TargetAndBubble},
  51. {EventId::Click , "click" , true , true , DefaultActionPhase::TargetAndBubble},
  52. {EventId::Dblclick , "dblclick" , true , true , DefaultActionPhase::TargetAndBubble},
  53. {EventId::Load , "load" , false , false , DefaultActionPhase::None},
  54. {EventId::Unload , "unload" , false , false , DefaultActionPhase::None},
  55. {EventId::Show , "show" , false , false , DefaultActionPhase::None},
  56. {EventId::Hide , "hide" , false , false , DefaultActionPhase::None},
  57. {EventId::Mousemove , "mousemove" , true , true , DefaultActionPhase::None},
  58. {EventId::Dragmove , "dragmove" , true , true , DefaultActionPhase::Target},
  59. {EventId::Drag , "drag" , false , true , DefaultActionPhase::Target},
  60. {EventId::Dragstart , "dragstart" , false , true , DefaultActionPhase::Target},
  61. {EventId::Dragover , "dragstart" , true , false , DefaultActionPhase::Target},
  62. {EventId::Dragdrop , "dragdrop" , true , false , DefaultActionPhase::Target},
  63. {EventId::Dragout , "dragout" , true , false , DefaultActionPhase::Target},
  64. {EventId::Dragend , "dragend" , true , true , DefaultActionPhase::None},
  65. {EventId::Handledrag , "handledrag" , false , true , DefaultActionPhase::None},
  66. {EventId::Resize , "resize" , false , false , DefaultActionPhase::None},
  67. {EventId::Scroll , "scroll" , false , true , DefaultActionPhase::None},
  68. {EventId::Scrollchange , "scrollchange" , false , true , DefaultActionPhase::None},
  69. {EventId::Animationend , "animationend" , true , true , DefaultActionPhase::None},
  70. {EventId::Transitionend , "transitionend" , true , 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 , "tabchange" , 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 ROCKET_DEBUG
  85. // Verify all event ids specified
  86. ROCKET_ASSERT((int)specifications.size() == (int)EventId::NumDefinedIds);
  87. for (int i = 0; i < (int)specifications.size(); i++)
  88. {
  89. // Verify correct order
  90. ROCKET_ASSERT(i == (int)specifications[i].id);
  91. }
  92. #endif
  93. }
  94. const EventSpecification& Get(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. EventId GetIdOrDefineDefault(const String& event_type)
  102. {
  103. auto it = type_lookup.find(event_type);
  104. if (it != type_lookup.end())
  105. return it->second;
  106. // No specification found for this name, insert a new entry with default values
  107. EventId new_id = static_cast<EventId>(specifications.size());
  108. specifications.push_back(EventSpecification{ new_id, event_type, true, true, DefaultActionPhase::None });
  109. type_lookup.emplace(event_type, new_id);
  110. return new_id;
  111. }
  112. }
  113. }
  114. }