ElementLabel.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "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* target = GetTarget())
  56. {
  57. // Temporarily disable click captures to avoid infinite recursion in case this element is on the path to the target element.
  58. disable_click = true;
  59. event.StopPropagation();
  60. target->Focus();
  61. target->Click();
  62. disable_click = false;
  63. }
  64. }
  65. }
  66. }
  67. // Get the first descending element whose tag name matches one of tags.
  68. static Element* TagMatchRecursive(const StringList& tags, Element* element)
  69. {
  70. const int num_children = element->GetNumChildren();
  71. for (int i = 0; i < num_children; i++)
  72. {
  73. Element* child = element->GetChild(i);
  74. for (const String& tag : tags)
  75. {
  76. if (child->GetTagName() == tag)
  77. return child;
  78. }
  79. Element* matching_element = TagMatchRecursive(tags, child);
  80. if (matching_element)
  81. return matching_element;
  82. }
  83. return nullptr;
  84. }
  85. Element* ElementLabel::GetTarget()
  86. {
  87. const String target_id = GetAttribute<String>("for", "");
  88. if (target_id.empty())
  89. {
  90. const StringList matching_tags = { "button", "input", "textarea", "progressbar", "select" };
  91. return TagMatchRecursive(matching_tags, this);
  92. }
  93. else
  94. {
  95. return GetElementById(target_id);
  96. }
  97. return nullptr;
  98. }
  99. } // namespace Rml