ElementFormControlSelect.cpp 4.8 KB

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