InputTypeText.cpp 3.9 KB

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