ElementFormControlSelect.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "../../../Include/RmlUi/Core/Elements/ElementFormControlSelect.h"
  29. #include "../../../Include/RmlUi/Core/ElementText.h"
  30. #include "../../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../../Include/RmlUi/Core/Event.h"
  32. #include "WidgetDropDown.h"
  33. namespace Rml {
  34. ElementFormControlSelect::ElementFormControlSelect(const String& tag) : ElementFormControl(tag), widget(nullptr)
  35. {
  36. widget = new WidgetDropDown(this);
  37. }
  38. ElementFormControlSelect::~ElementFormControlSelect()
  39. {
  40. delete widget;
  41. }
  42. String ElementFormControlSelect::GetValue() const
  43. {
  44. return GetAttribute("value", String());
  45. }
  46. void ElementFormControlSelect::SetValue(const String& value)
  47. {
  48. MoveChildren();
  49. SetAttribute("value", value);
  50. }
  51. void ElementFormControlSelect::SetSelection(int selection)
  52. {
  53. MoveChildren();
  54. widget->SetSelection(widget->GetOption(selection));
  55. }
  56. int ElementFormControlSelect::GetSelection() const
  57. {
  58. return widget->GetSelection();
  59. }
  60. Element* ElementFormControlSelect::GetOption(int index)
  61. {
  62. MoveChildren();
  63. return widget->GetOption(index);
  64. }
  65. int ElementFormControlSelect::GetNumOptions()
  66. {
  67. MoveChildren();
  68. return widget->GetNumOptions();
  69. }
  70. int ElementFormControlSelect::Add(const String& rml, const String& value, int before, bool selectable)
  71. {
  72. MoveChildren();
  73. return widget->AddOption(rml, value, before, false, selectable);
  74. }
  75. int ElementFormControlSelect::Add(ElementPtr element, int before)
  76. {
  77. MoveChildren();
  78. return widget->AddOption(std::move(element), before);
  79. }
  80. void ElementFormControlSelect::Remove(int index)
  81. {
  82. MoveChildren();
  83. widget->RemoveOption(index);
  84. }
  85. void ElementFormControlSelect::RemoveAll()
  86. {
  87. MoveChildren();
  88. widget->ClearOptions();
  89. }
  90. void ElementFormControlSelect::ShowSelectBox()
  91. {
  92. widget->ShowSelectBox();
  93. }
  94. void ElementFormControlSelect::HideSelectBox()
  95. {
  96. widget->HideSelectBox();
  97. }
  98. void ElementFormControlSelect::CancelSelectBox()
  99. {
  100. widget->CancelSelectBox();
  101. }
  102. bool ElementFormControlSelect::IsSelectBoxVisible()
  103. {
  104. return widget->IsSelectBoxVisible();
  105. }
  106. void ElementFormControlSelect::OnUpdate()
  107. {
  108. ElementFormControl::OnUpdate();
  109. MoveChildren();
  110. widget->OnUpdate();
  111. }
  112. void ElementFormControlSelect::OnRender()
  113. {
  114. ElementFormControl::OnRender();
  115. widget->OnRender();
  116. }
  117. void ElementFormControlSelect::OnLayout()
  118. {
  119. widget->OnLayout();
  120. }
  121. void ElementFormControlSelect::OnChildAdd(Element* child)
  122. {
  123. if (widget)
  124. widget->OnChildAdd(child);
  125. }
  126. void ElementFormControlSelect::OnChildRemove(Element* child)
  127. {
  128. if (widget)
  129. widget->OnChildRemove(child);
  130. }
  131. void ElementFormControlSelect::MoveChildren()
  132. {
  133. // Move any child elements into the widget (except for the three functional elements).
  134. while (Element* raw_child = GetFirstElementChild())
  135. {
  136. ElementPtr child = As<ElementPtr>(RemoveChild(raw_child));
  137. widget->AddOption(std::move(child), -1);
  138. }
  139. }
  140. bool ElementFormControlSelect::GetIntrinsicDimensions(Vector2f& intrinsic_dimensions, float& /*ratio*/)
  141. {
  142. intrinsic_dimensions.x = 128;
  143. intrinsic_dimensions.y = 16;
  144. return true;
  145. }
  146. void ElementFormControlSelect::OnAttributeChange(const ElementAttributes& changed_attributes)
  147. {
  148. RMLUI_ASSERT(widget);
  149. ElementFormControl::OnAttributeChange(changed_attributes);
  150. auto it = changed_attributes.find("value");
  151. if (it != changed_attributes.end())
  152. widget->OnValueChange(it->second.Get<String>());
  153. }
  154. } // namespace Rml