ElementFormControlInput.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/ElementFormControlInput.h"
  29. #include "../../../Include/RmlUi/Core/Event.h"
  30. #include "InputTypeButton.h"
  31. #include "InputTypeCheckbox.h"
  32. #include "InputTypeRadio.h"
  33. #include "InputTypeRange.h"
  34. #include "InputTypeSubmit.h"
  35. #include "InputTypeText.h"
  36. namespace Rml {
  37. // Constructs a new ElementFormControlInput.
  38. ElementFormControlInput::ElementFormControlInput(const String& tag) : ElementFormControl(tag)
  39. {
  40. // OnAttributeChange will be called right after this, possible with a non-default type. Thus,
  41. // creating the default InputTypeText here may result in it being destroyed in just a few moments.
  42. // Instead, we create the InputTypeText in OnAttributeChange in the case where the type attribute has not been set.
  43. }
  44. ElementFormControlInput::~ElementFormControlInput()
  45. {}
  46. // Returns a string representation of the current value of the form control.
  47. String ElementFormControlInput::GetValue() const
  48. {
  49. RMLUI_ASSERT(type);
  50. return type->GetValue();
  51. }
  52. // Sets the current value of the form control.
  53. void ElementFormControlInput::SetValue(const String& value)
  54. {
  55. SetAttribute("value", value);
  56. }
  57. // Returns if this value should be submitted with the form.
  58. bool ElementFormControlInput::IsSubmitted()
  59. {
  60. RMLUI_ASSERT(type);
  61. return type->IsSubmitted();
  62. }
  63. void ElementFormControlInput::Select()
  64. {
  65. RMLUI_ASSERT(type);
  66. type->Select();
  67. }
  68. void ElementFormControlInput::SetSelectionRange(int selection_start, int selection_end)
  69. {
  70. RMLUI_ASSERT(type);
  71. type->SetSelectionRange(selection_start, selection_end);
  72. }
  73. void ElementFormControlInput::GetSelection(int* selection_start, int* selection_end, String* selected_text) const
  74. {
  75. RMLUI_ASSERT(type);
  76. type->GetSelection(selection_start, selection_end, selected_text);
  77. }
  78. // Updates the element's underlying type.
  79. void ElementFormControlInput::OnUpdate()
  80. {
  81. RMLUI_ASSERT(type);
  82. type->OnUpdate();
  83. }
  84. // Renders the element's underlying type.
  85. void ElementFormControlInput::OnRender()
  86. {
  87. RMLUI_ASSERT(type);
  88. type->OnRender();
  89. }
  90. void ElementFormControlInput::OnResize()
  91. {
  92. RMLUI_ASSERT(type);
  93. type->OnResize();
  94. }
  95. void ElementFormControlInput::OnLayout()
  96. {
  97. RMLUI_ASSERT(type);
  98. type->OnLayout();
  99. }
  100. // Checks for necessary functional changes in the control as a result of changed attributes.
  101. void ElementFormControlInput::OnAttributeChange(const ElementAttributes& changed_attributes)
  102. {
  103. ElementFormControl::OnAttributeChange(changed_attributes);
  104. String new_type_name;
  105. auto it_type = changed_attributes.find("type");
  106. if (it_type != changed_attributes.end())
  107. {
  108. new_type_name = it_type->second.Get<String>("text");
  109. }
  110. if (!type || (!new_type_name.empty() && new_type_name != type_name))
  111. {
  112. // Reset the existing type before constructing a new one. This ensures the old type removes properties and event
  113. // listeners attached to this element, so it does not interfere with new ones being attached by the new type.
  114. type.reset();
  115. if (new_type_name == "password")
  116. type = MakeUnique<InputTypeText>(this, InputTypeText::OBSCURED);
  117. else if (new_type_name == "radio")
  118. type = MakeUnique<InputTypeRadio>(this);
  119. else if (new_type_name == "checkbox")
  120. type = MakeUnique<InputTypeCheckbox>(this);
  121. else if (new_type_name == "range")
  122. type = MakeUnique<InputTypeRange>(this);
  123. else if (new_type_name == "submit")
  124. type = MakeUnique<InputTypeSubmit>(this);
  125. else if (new_type_name == "button")
  126. type = MakeUnique<InputTypeButton>(this);
  127. else
  128. {
  129. new_type_name = "text";
  130. type = MakeUnique<InputTypeText>(this);
  131. }
  132. if (!type_name.empty())
  133. SetClass(type_name, false);
  134. SetClass(new_type_name, true);
  135. type_name = new_type_name;
  136. DirtyLayout();
  137. }
  138. RMLUI_ASSERT(type);
  139. if (!type->OnAttributeChange(changed_attributes))
  140. DirtyLayout();
  141. }
  142. // Called when properties on the element are changed.
  143. void ElementFormControlInput::OnPropertyChange(const PropertyIdSet& changed_properties)
  144. {
  145. ElementFormControl::OnPropertyChange(changed_properties);
  146. if (type)
  147. type->OnPropertyChange(changed_properties);
  148. }
  149. // If we are the added element, this will pass the call onto our type handler.
  150. void ElementFormControlInput::OnChildAdd(Element* child)
  151. {
  152. if (child == this && type)
  153. type->OnChildAdd();
  154. }
  155. // If we are the removed element, this will pass the call onto our type handler.
  156. void ElementFormControlInput::OnChildRemove(Element* child)
  157. {
  158. if (child == this && type)
  159. type->OnChildRemove();
  160. }
  161. // Handles the "click" event to toggle the control's checked status.
  162. void ElementFormControlInput::ProcessDefaultAction(Event& event)
  163. {
  164. ElementFormControl::ProcessDefaultAction(event);
  165. if (type)
  166. type->ProcessDefaultAction(event);
  167. }
  168. bool ElementFormControlInput::GetIntrinsicDimensions(Vector2f& dimensions, float& ratio)
  169. {
  170. if (type)
  171. return type->GetIntrinsicDimensions(dimensions, ratio);
  172. return false;
  173. }
  174. } // namespace Rml