ElementFormControlSelect.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include <Rocket/Controls/ElementFormControlSelect.h>
  28. #include <Rocket/Core/ElementText.h>
  29. #include <Rocket/Core/Event.h>
  30. #include <Rocket/Core/ElementUtilities.h>
  31. #include "WidgetDropDown.h"
  32. namespace Rocket {
  33. namespace Controls {
  34. // Constructs a new ElementFormControlSelect.
  35. ElementFormControlSelect::ElementFormControlSelect(const Rocket::Core::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. Rocket::Core::String ElementFormControlSelect::GetValue() const
  45. {
  46. ROCKET_ASSERT(widget != NULL);
  47. return widget->GetValue();
  48. }
  49. // Sets the current value of the form control.
  50. void ElementFormControlSelect::SetValue(const Rocket::Core::String& value)
  51. {
  52. OnUpdate();
  53. ROCKET_ASSERT(widget != NULL);
  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. ROCKET_ASSERT(widget != NULL);
  61. widget->SetSelection(selection);
  62. }
  63. // Returns the index of the currently selected item.
  64. int ElementFormControlSelect::GetSelection() const
  65. {
  66. ROCKET_ASSERT(widget != NULL);
  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. ROCKET_ASSERT(widget != NULL);
  74. return widget->GetOption(index);
  75. }
  76. // Returns the number of options in the select control.
  77. int ElementFormControlSelect::GetNumOptions()
  78. {
  79. OnUpdate();
  80. ROCKET_ASSERT(widget != NULL);
  81. return widget->GetNumOptions();
  82. }
  83. // Adds a new option to the select control.
  84. int ElementFormControlSelect::Add(const Rocket::Core::String& rml, const Rocket::Core::String& value, int before, bool selectable)
  85. {
  86. OnUpdate();
  87. ROCKET_ASSERT(widget != NULL);
  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. ROCKET_ASSERT(widget != NULL);
  95. widget->RemoveOption(index);
  96. }
  97. // Removes all options from the select control.
  98. void ElementFormControlSelect::RemoveAll()
  99. {
  100. OnUpdate();
  101. ROCKET_ASSERT(widget != NULL);
  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 (HasChildNodes())
  110. {
  111. Core::Element* child = GetFirstChild();
  112. // Check for a value attribute.
  113. Rocket::Core::String attribute_value = child->GetAttribute<Rocket::Core::String>("value", "");
  114. // Pull the inner RML and add the option.
  115. Rocket::Core::String rml;
  116. child->GetInnerRML(rml);
  117. widget->AddOption(rml, attribute_value, -1, child->GetAttribute("selected") != NULL, child->GetAttribute("unselectable") == NULL);
  118. RemoveChild(child);
  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(Rocket::Core::Vector2f& intrinsic_dimensions)
  134. {
  135. intrinsic_dimensions.x = 128;
  136. intrinsic_dimensions.y = 16;
  137. return true;
  138. }
  139. }
  140. }