ElementFormControlInput.cpp 4.9 KB

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