ElementFormControlSelect.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/Controls/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. namespace Controls {
  35. // Constructs a new ElementFormControlSelect.
  36. ElementFormControlSelect::ElementFormControlSelect(const Rml::Core::String& tag) : ElementFormControl(tag)
  37. {
  38. widget = new WidgetDropDown(this);
  39. }
  40. ElementFormControlSelect::~ElementFormControlSelect()
  41. {
  42. delete widget;
  43. }
  44. // Returns a string representation of the current value of the form control.
  45. Rml::Core::String ElementFormControlSelect::GetValue() const
  46. {
  47. RMLUI_ASSERT(widget != nullptr);
  48. return widget->GetValue();
  49. }
  50. // Sets the current value of the form control.
  51. void ElementFormControlSelect::SetValue(const Rml::Core::String& value)
  52. {
  53. OnUpdate();
  54. RMLUI_ASSERT(widget != nullptr);
  55. widget->SetValue(value);
  56. }
  57. // Sets the index of the selection. If the new index lies outside of the bounds, it will be clamped.
  58. void ElementFormControlSelect::SetSelection(int selection)
  59. {
  60. OnUpdate();
  61. RMLUI_ASSERT(widget != nullptr);
  62. widget->SetSelection(selection);
  63. }
  64. // Returns the index of the currently selected item.
  65. int ElementFormControlSelect::GetSelection() const
  66. {
  67. RMLUI_ASSERT(widget != nullptr);
  68. return widget->GetSelection();
  69. }
  70. // Returns one of the select control's option elements.
  71. SelectOption* ElementFormControlSelect::GetOption(int index)
  72. {
  73. OnUpdate();
  74. RMLUI_ASSERT(widget != nullptr);
  75. return widget->GetOption(index);
  76. }
  77. // Returns the number of options in the select control.
  78. int ElementFormControlSelect::GetNumOptions()
  79. {
  80. OnUpdate();
  81. RMLUI_ASSERT(widget != nullptr);
  82. return widget->GetNumOptions();
  83. }
  84. // Adds a new option to the select control.
  85. int ElementFormControlSelect::Add(const Rml::Core::String& rml, const Rml::Core::String& value, int before, bool selectable)
  86. {
  87. OnUpdate();
  88. RMLUI_ASSERT(widget != nullptr);
  89. return widget->AddOption(rml, value, before, false, selectable);
  90. }
  91. // Removes an option from the select control.
  92. void ElementFormControlSelect::Remove(int index)
  93. {
  94. OnUpdate();
  95. RMLUI_ASSERT(widget != nullptr);
  96. widget->RemoveOption(index);
  97. }
  98. // Removes all options from the select control.
  99. void ElementFormControlSelect::RemoveAll()
  100. {
  101. OnUpdate();
  102. RMLUI_ASSERT(widget != nullptr);
  103. widget->ClearOptions();
  104. }
  105. // Moves all children to be under control of the widget.
  106. void ElementFormControlSelect::OnUpdate()
  107. {
  108. ElementFormControl::OnUpdate();
  109. // Move any child elements into the widget (except for the three functional elements).
  110. while (HasChildNodes())
  111. {
  112. Core::Element* child = GetFirstChild();
  113. // Check for a value attribute.
  114. Rml::Core::String attribute_value = child->GetAttribute<Rml::Core::String>("value", "");
  115. // Pull the inner RML and add the option.
  116. Rml::Core::String rml;
  117. child->GetInnerRML(rml);
  118. widget->AddOption(rml, attribute_value, -1, child->GetAttribute("selected"), !child->GetAttribute("unselectable"));
  119. RemoveChild(child);
  120. }
  121. }
  122. // Updates the layout of the widget's elements.
  123. void ElementFormControlSelect::OnRender()
  124. {
  125. ElementFormControl::OnRender();
  126. widget->OnRender();
  127. }
  128. // Forces an internal layout.
  129. void ElementFormControlSelect::OnLayout()
  130. {
  131. widget->OnLayout();
  132. }
  133. // Returns true to mark this element as replaced.
  134. bool ElementFormControlSelect::GetIntrinsicDimensions(Rml::Core::Vector2f& intrinsic_dimensions)
  135. {
  136. intrinsic_dimensions.x = 128;
  137. intrinsic_dimensions.y = 16;
  138. return true;
  139. }
  140. }
  141. }