ElementFormControlInput.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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-2023 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. ElementFormControlInput::ElementFormControlInput(const String& tag) : ElementFormControl(tag)
  38. {
  39. // OnAttributeChange will be called right after this, possible with a non-default type. Thus,
  40. // creating the default InputTypeText here may result in it being destroyed in just a few moments.
  41. // Instead, we create the InputTypeText in OnAttributeChange in the case where the type attribute has not been set.
  42. }
  43. ElementFormControlInput::~ElementFormControlInput() {}
  44. String ElementFormControlInput::GetValue() const
  45. {
  46. RMLUI_ASSERT(type);
  47. return type->GetValue();
  48. }
  49. void ElementFormControlInput::SetValue(const String& value)
  50. {
  51. SetAttribute("value", value);
  52. }
  53. bool ElementFormControlInput::IsSubmitted()
  54. {
  55. RMLUI_ASSERT(type);
  56. return type->IsSubmitted();
  57. }
  58. void ElementFormControlInput::Select()
  59. {
  60. RMLUI_ASSERT(type);
  61. type->Select();
  62. }
  63. void ElementFormControlInput::SetSelectionRange(int selection_start, int selection_end)
  64. {
  65. RMLUI_ASSERT(type);
  66. type->SetSelectionRange(selection_start, selection_end);
  67. }
  68. void ElementFormControlInput::GetSelection(int* selection_start, int* selection_end, String* selected_text) const
  69. {
  70. RMLUI_ASSERT(type);
  71. type->GetSelection(selection_start, selection_end, selected_text);
  72. }
  73. void ElementFormControlInput::OnUpdate()
  74. {
  75. RMLUI_ASSERT(type);
  76. type->OnUpdate();
  77. }
  78. void ElementFormControlInput::OnRender()
  79. {
  80. RMLUI_ASSERT(type);
  81. type->OnRender();
  82. }
  83. void ElementFormControlInput::OnResize()
  84. {
  85. RMLUI_ASSERT(type);
  86. type->OnResize();
  87. }
  88. void ElementFormControlInput::OnLayout()
  89. {
  90. RMLUI_ASSERT(type);
  91. type->OnLayout();
  92. }
  93. void ElementFormControlInput::OnAttributeChange(const ElementAttributes& changed_attributes)
  94. {
  95. ElementFormControl::OnAttributeChange(changed_attributes);
  96. String new_type_name;
  97. auto it_type = changed_attributes.find("type");
  98. if (it_type != changed_attributes.end())
  99. {
  100. new_type_name = it_type->second.Get<String>("text");
  101. }
  102. if (!type || (!new_type_name.empty() && new_type_name != type_name))
  103. {
  104. // Reset the existing type before constructing a new one. This ensures the old type removes properties and event
  105. // listeners attached to this element, so it does not interfere with new ones being attached by the new type.
  106. type.reset();
  107. if (new_type_name == "password")
  108. type = MakeUnique<InputTypeText>(this, InputTypeText::OBSCURED);
  109. else if (new_type_name == "radio")
  110. type = MakeUnique<InputTypeRadio>(this);
  111. else if (new_type_name == "checkbox")
  112. type = MakeUnique<InputTypeCheckbox>(this);
  113. else if (new_type_name == "range")
  114. type = MakeUnique<InputTypeRange>(this);
  115. else if (new_type_name == "submit")
  116. type = MakeUnique<InputTypeSubmit>(this);
  117. else if (new_type_name == "button")
  118. type = MakeUnique<InputTypeButton>(this);
  119. else
  120. {
  121. new_type_name = "text";
  122. type = MakeUnique<InputTypeText>(this);
  123. }
  124. if (!type_name.empty())
  125. SetClass(type_name, false);
  126. SetClass(new_type_name, true);
  127. type_name = new_type_name;
  128. DirtyLayout();
  129. }
  130. RMLUI_ASSERT(type);
  131. if (!type->OnAttributeChange(changed_attributes))
  132. DirtyLayout();
  133. }
  134. void ElementFormControlInput::OnPropertyChange(const PropertyIdSet& changed_properties)
  135. {
  136. ElementFormControl::OnPropertyChange(changed_properties);
  137. if (type)
  138. type->OnPropertyChange(changed_properties);
  139. }
  140. void ElementFormControlInput::OnChildAdd(Element* child)
  141. {
  142. if (child == this && type)
  143. type->OnChildAdd();
  144. }
  145. void ElementFormControlInput::OnChildRemove(Element* child)
  146. {
  147. if (child == this && type)
  148. type->OnChildRemove();
  149. }
  150. void ElementFormControlInput::ProcessDefaultAction(Event& event)
  151. {
  152. ElementFormControl::ProcessDefaultAction(event);
  153. if (type)
  154. type->ProcessDefaultAction(event);
  155. }
  156. bool ElementFormControlInput::GetIntrinsicDimensions(Vector2f& dimensions, float& ratio)
  157. {
  158. if (type)
  159. return type->GetIntrinsicDimensions(dimensions, ratio);
  160. return false;
  161. }
  162. } // namespace Rml