ElementLabel.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "ElementLabel.h"
  29. namespace Rml {
  30. ElementLabel::ElementLabel(const String& tag) : Element(tag)
  31. {
  32. AddEventListener(EventId::Click, this, true);
  33. }
  34. ElementLabel::~ElementLabel()
  35. {
  36. RemoveEventListener(EventId::Click, this, true);
  37. }
  38. void ElementLabel::OnPseudoClassChange(const String& pseudo_class, bool activate)
  39. {
  40. if (pseudo_class == "active" || pseudo_class == "hover")
  41. {
  42. if (Element* target = GetTarget())
  43. {
  44. OverridePseudoClass(target, pseudo_class, activate);
  45. }
  46. }
  47. }
  48. void ElementLabel::ProcessEvent(Event& event)
  49. {
  50. // Forward clicks to the target.
  51. if (event == EventId::Click && !disable_click)
  52. {
  53. if (event.GetPhase() == EventPhase::Capture || event.GetPhase() == EventPhase::Target)
  54. {
  55. if (Element* label_target = GetTarget())
  56. {
  57. Element* event_target = event.GetTargetElement();
  58. // If the event is already on the way to the label target, there is no reason to intervene with the
  59. // click. Just let the event move down the chain without interrupting it. Otherwise, intervene to
  60. // manually redirect the click to the label target.
  61. if (!label_target->Contains(event_target))
  62. {
  63. // Temporarily disable click captures to avoid infinite recursion in case this element is on the path to the target element.
  64. disable_click = true;
  65. event.StopPropagation();
  66. label_target->Focus();
  67. label_target->Click();
  68. disable_click = false;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. // Get the first descending element whose tag name matches one of tags.
  75. static Element* TagMatchRecursive(const StringList& tags, Element* element)
  76. {
  77. const int num_children = element->GetNumChildren();
  78. for (int i = 0; i < num_children; i++)
  79. {
  80. Element* child = element->GetChild(i);
  81. for (const String& tag : tags)
  82. {
  83. if (child->GetTagName() == tag)
  84. return child;
  85. }
  86. Element* matching_element = TagMatchRecursive(tags, child);
  87. if (matching_element)
  88. return matching_element;
  89. }
  90. return nullptr;
  91. }
  92. Element* ElementLabel::GetTarget()
  93. {
  94. const String target_id = GetAttribute<String>("for", "");
  95. if (target_id.empty())
  96. {
  97. const StringList matching_tags = {"button", "input", "textarea", "progress", "progressbar", "select"};
  98. return TagMatchRecursive(matching_tags, this);
  99. }
  100. else
  101. {
  102. Element* target = GetElementById(target_id);
  103. if (target != this)
  104. return target;
  105. }
  106. return nullptr;
  107. }
  108. } // namespace Rml