InputTypeText.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. void InputTypeText::OnResize()
  60. {
  61. widget->OnResize();
  62. }
  63. // Checks for necessary functional changes in the control as a result of changed attributes.
  64. bool InputTypeText::OnAttributeChange(const Core::ElementAttributes& changed_attributes)
  65. {
  66. bool dirty_layout = false;
  67. // Check if maxlength has been defined.
  68. auto it = changed_attributes.find("maxlength");
  69. if (it != changed_attributes.end())
  70. widget->SetMaxLength(it->second.Get(-1));
  71. // Check if size has been defined.
  72. it = changed_attributes.find("size");
  73. if (it != changed_attributes.end())
  74. {
  75. size = it->second.Get(20);
  76. dirty_layout = true;
  77. }
  78. // Check if the value has been changed.
  79. it = changed_attributes.find("value");
  80. if (it != changed_attributes.end())
  81. widget->SetValue(it->second.Get<Core::String>());
  82. return !dirty_layout;
  83. }
  84. // Called when properties on the control are changed.
  85. void InputTypeText::OnPropertyChange(const Core::PropertyNameList& changed_properties)
  86. {
  87. if (changed_properties.find(Core::PropertyId::Color) != changed_properties.end() ||
  88. changed_properties.find(Core::PropertyId::BackgroundColor) != changed_properties.end())
  89. widget->UpdateSelectionColours();
  90. }
  91. // Checks for necessary functional changes in the control as a result of the event.
  92. void InputTypeText::ProcessDefaultAction(Core::Event& RMLUI_UNUSED_PARAMETER(event))
  93. {
  94. RMLUI_UNUSED(event);
  95. }
  96. // Sizes the dimensions to the element's inherent size.
  97. bool InputTypeText::GetIntrinsicDimensions(Rml::Core::Vector2f& dimensions)
  98. {
  99. dimensions.x = (float) (size * Core::ElementUtilities::GetStringWidth(element, L"m"));
  100. dimensions.y = element->GetLineHeight() + 2.0f;
  101. return true;
  102. }
  103. }
  104. }