ElementFormControlSelect.cpp 5.0 KB

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