ElementFormControlInput.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "../../Include/Rocket/Controls/ElementFormControlInput.h"
  28. #include "../../Include/Rocket/Core/Event.h"
  29. #include "InputTypeButton.h"
  30. #include "InputTypeCheckbox.h"
  31. #include "InputTypeRadio.h"
  32. #include "InputTypeRange.h"
  33. #include "InputTypeSubmit.h"
  34. #include "InputTypeText.h"
  35. namespace Rocket {
  36. namespace Controls {
  37. // Constructs a new ElementFormControlInput.
  38. ElementFormControlInput::ElementFormControlInput(const Rocket::Core::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. type = NULL;
  44. }
  45. ElementFormControlInput::~ElementFormControlInput()
  46. {
  47. delete type;
  48. }
  49. // Returns a string representation of the current value of the form control.
  50. Rocket::Core::String ElementFormControlInput::GetValue() const
  51. {
  52. ROCKET_ASSERT(type);
  53. return type->GetValue();
  54. }
  55. // Sets the current value of the form control.
  56. void ElementFormControlInput::SetValue(const Rocket::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. ROCKET_ASSERT(type);
  64. return type->IsSubmitted();
  65. }
  66. // Updates the element's underlying type.
  67. void ElementFormControlInput::OnUpdate()
  68. {
  69. ROCKET_ASSERT(type);
  70. type->OnUpdate();
  71. }
  72. // Renders the element's underlying type.
  73. void ElementFormControlInput::OnRender()
  74. {
  75. ROCKET_ASSERT(type);
  76. type->OnRender();
  77. }
  78. void ElementFormControlInput::OnResize()
  79. {
  80. ROCKET_ASSERT(type);
  81. type->OnResize();
  82. }
  83. // Checks for necessary functional changes in the control as a result of changed attributes.
  84. void ElementFormControlInput::OnAttributeChange(const Core::ElementAttributes& changed_attributes)
  85. {
  86. ElementFormControl::OnAttributeChange(changed_attributes);
  87. Rocket::Core::String new_type_name;
  88. auto it_type = changed_attributes.find("type");
  89. if (it_type != changed_attributes.end())
  90. {
  91. new_type_name = it_type->second.Get<Core::String>("text");
  92. }
  93. else if (!type)
  94. {
  95. // Ref. comment in constructor.
  96. new_type_name = "text";
  97. }
  98. if (!new_type_name.empty() && new_type_name != type_name)
  99. {
  100. InputType* new_type = nullptr;
  101. if (new_type_name == "password")
  102. new_type = new InputTypeText(this, Rocket::Controls::InputTypeText::OBSCURED);
  103. else if (new_type_name == "radio")
  104. new_type = new InputTypeRadio(this);
  105. else if (new_type_name == "checkbox")
  106. new_type = new InputTypeCheckbox(this);
  107. else if (new_type_name == "range")
  108. new_type = new InputTypeRange(this);
  109. else if (new_type_name == "submit")
  110. new_type = new InputTypeSubmit(this);
  111. else if (new_type_name == "button")
  112. new_type = new InputTypeButton(this);
  113. else if (new_type_name == "text")
  114. new_type = new InputTypeText(this);
  115. if (new_type)
  116. {
  117. delete type;
  118. type = new_type;
  119. if(!type_name.empty())
  120. SetClass(type_name, false);
  121. SetClass(new_type_name, true);
  122. type_name = new_type_name;
  123. DirtyLayout();
  124. }
  125. }
  126. if (!type->OnAttributeChange(changed_attributes))
  127. DirtyLayout();
  128. }
  129. // Called when properties on the element are changed.
  130. void ElementFormControlInput::OnPropertyChange(const Core::PropertyNameList& changed_properties)
  131. {
  132. ElementFormControl::OnPropertyChange(changed_properties);
  133. if (type != NULL)
  134. type->OnPropertyChange(changed_properties);
  135. }
  136. // If we are the added element, this will pass the call onto our type handler.
  137. void ElementFormControlInput::OnChildAdd(Rocket::Core::Element* child)
  138. {
  139. if (child == this && type != NULL)
  140. type->OnChildAdd();
  141. }
  142. // If we are the removed element, this will pass the call onto our type handler.
  143. void ElementFormControlInput::OnChildRemove(Rocket::Core::Element* child)
  144. {
  145. if (child == this && type != NULL)
  146. type->OnChildRemove();
  147. }
  148. // Handles the "click" event to toggle the control's checked status.
  149. void ElementFormControlInput::ProcessDefaultAction(Core::Event& event)
  150. {
  151. ElementFormControl::ProcessDefaultAction(event);
  152. if(type != NULL)
  153. type->ProcessDefaultAction(event);
  154. }
  155. bool ElementFormControlInput::GetIntrinsicDimensions(Rocket::Core::Vector2f& dimensions)
  156. {
  157. if (!type)
  158. return false;
  159. return type->GetIntrinsicDimensions(dimensions);
  160. }
  161. }
  162. }