ElementFormControlInput.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/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. namespace Controls {
  38. // Constructs a new ElementFormControlInput.
  39. ElementFormControlInput::ElementFormControlInput(const Rml::Core::String& tag) : ElementFormControl(tag)
  40. {
  41. type = NULL;
  42. type = new InputTypeText(this);
  43. type_name = "text";
  44. SetClass(type_name, true);
  45. }
  46. ElementFormControlInput::~ElementFormControlInput()
  47. {
  48. delete type;
  49. }
  50. // Returns a string representation of the current value of the form control.
  51. Rml::Core::String ElementFormControlInput::GetValue() const
  52. {
  53. return type->GetValue();
  54. }
  55. // Sets the current value of the form control.
  56. void ElementFormControlInput::SetValue(const Rml::Core::String& value)
  57. {
  58. SetAttribute("value", value);
  59. }
  60. // Returns if this value should be submitted with the form.
  61. bool ElementFormControlInput::IsSubmitted()
  62. {
  63. return type->IsSubmitted();
  64. }
  65. // Updates the element's underlying type.
  66. void ElementFormControlInput::OnUpdate()
  67. {
  68. type->OnUpdate();
  69. }
  70. // Renders the element's underlying type.
  71. void ElementFormControlInput::OnRender()
  72. {
  73. type->OnRender();
  74. }
  75. // Checks for necessary functional changes in the control as a result of changed attributes.
  76. void ElementFormControlInput::OnAttributeChange(const Core::AttributeNameList& changed_attributes)
  77. {
  78. ElementFormControl::OnAttributeChange(changed_attributes);
  79. if (changed_attributes.find("type") != changed_attributes.end())
  80. {
  81. Rml::Core::String new_type_name = GetAttribute< Rml::Core::String >("type", "text");
  82. if (new_type_name != type_name)
  83. {
  84. InputType* new_type = NULL;
  85. if (new_type_name == "password")
  86. new_type = new InputTypeText(this, Rml::Controls::InputTypeText::OBSCURED);
  87. else if (new_type_name == "radio")
  88. new_type = new InputTypeRadio(this);
  89. else if (new_type_name == "checkbox")
  90. new_type = new InputTypeCheckbox(this);
  91. else if (new_type_name == "range")
  92. new_type = new InputTypeRange(this);
  93. else if (new_type_name == "submit")
  94. new_type = new InputTypeSubmit(this);
  95. else if (new_type_name == "button")
  96. new_type = new InputTypeButton(this);
  97. else if (type_name == "text")
  98. new_type = new InputTypeText(this);
  99. if (new_type != NULL)
  100. {
  101. delete type;
  102. type = new_type;
  103. SetClass(type_name, false);
  104. SetClass(new_type_name, true);
  105. type_name = new_type_name;
  106. DirtyLayout();
  107. }
  108. }
  109. }
  110. if (!type->OnAttributeChange(changed_attributes))
  111. DirtyLayout();
  112. }
  113. // Called when properties on the element are changed.
  114. void ElementFormControlInput::OnPropertyChange(const Core::PropertyNameList& changed_properties)
  115. {
  116. ElementFormControl::OnPropertyChange(changed_properties);
  117. if (type != NULL)
  118. type->OnPropertyChange(changed_properties);
  119. }
  120. // If we are the added element, this will pass the call onto our type handler.
  121. void ElementFormControlInput::OnChildAdd(Rml::Core::Element* child)
  122. {
  123. if (child == this)
  124. type->OnChildAdd();
  125. }
  126. // If we are the removed element, this will pass the call onto our type handler.
  127. void ElementFormControlInput::OnChildRemove(Rml::Core::Element* child)
  128. {
  129. if (child == this)
  130. type->OnChildRemove();
  131. }
  132. // Handles the "click" event to toggle the control's checked status.
  133. void ElementFormControlInput::ProcessEvent(Core::Event& event)
  134. {
  135. ElementFormControl::ProcessEvent(event);
  136. type->ProcessEvent(event);
  137. }
  138. bool ElementFormControlInput::GetIntrinsicDimensions(Rml::Core::Vector2f& dimensions)
  139. {
  140. return type->GetIntrinsicDimensions(dimensions);
  141. }
  142. }
  143. }