InputTypeText.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "InputTypeText.h"
  29. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  30. #include "WidgetTextInputSingleLine.h"
  31. #include "WidgetTextInputSingleLinePassword.h"
  32. #include "../../Include/RmlUi/Controls/ElementFormControlInput.h"
  33. namespace Rml {
  34. namespace Controls {
  35. InputTypeText::InputTypeText(ElementFormControlInput* element, Visibility visibility) : InputType(element)
  36. {
  37. if (visibility == VISIBLE)
  38. widget = new WidgetTextInputSingleLine(element);
  39. else
  40. widget = new WidgetTextInputSingleLinePassword(element);
  41. widget->SetMaxLength(element->GetAttribute< int >("maxlength", -1));
  42. widget->SetValue(element->GetAttribute< Rml::Core::String >("value", ""));
  43. size = element->GetAttribute< int >("size", 20);
  44. }
  45. InputTypeText::~InputTypeText()
  46. {
  47. delete widget;
  48. }
  49. // Called every update from the host element.
  50. void InputTypeText::OnUpdate()
  51. {
  52. widget->OnUpdate();
  53. }
  54. // Called every render from the host element.
  55. void InputTypeText::OnRender()
  56. {
  57. widget->OnRender();
  58. }
  59. // Checks for necessary functional changes in the control as a result of changed attributes.
  60. bool InputTypeText::OnAttributeChange(const Core::AttributeNameList& changed_attributes)
  61. {
  62. bool dirty_layout = false;
  63. // Check if maxlength has been defined.
  64. if (changed_attributes.find("maxlength") != changed_attributes.end())
  65. widget->SetMaxLength(element->GetAttribute< int >("maxlength", -1));
  66. // Check if size has been defined.
  67. if (changed_attributes.find("size") != changed_attributes.end())
  68. {
  69. size = element->GetAttribute< int >("size", 20);
  70. dirty_layout = true;
  71. }
  72. // Check if the value has been changed.
  73. if (changed_attributes.find("value") != changed_attributes.end())
  74. widget->SetValue(element->GetAttribute< Rml::Core::String >("value", ""));
  75. return !dirty_layout;
  76. }
  77. // Called when properties on the control are changed.
  78. void InputTypeText::OnPropertyChange(const Core::PropertyNameList& changed_properties)
  79. {
  80. if (changed_properties.find("color") != changed_properties.end() ||
  81. changed_properties.find("background-color") != changed_properties.end())
  82. widget->UpdateSelectionColours();
  83. }
  84. // Checks for necessary functional changes in the control as a result of the event.
  85. void InputTypeText::ProcessEvent(Core::Event& RMLUI_UNUSED_PARAMETER(event))
  86. {
  87. RMLUI_UNUSED(event);
  88. }
  89. // Sizes the dimensions to the element's inherent size.
  90. bool InputTypeText::GetIntrinsicDimensions(Rml::Core::Vector2f& dimensions)
  91. {
  92. dimensions.x = (float) (size * Core::ElementUtilities::GetStringWidth(element, "m"));
  93. dimensions.y = (float) Core::ElementUtilities::GetLineHeight(element) + 2;
  94. return true;
  95. }
  96. }
  97. }