ElementFormControlTextArea.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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-2023 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 "../../../Include/RmlUi/Core/Elements/ElementFormControlTextArea.h"
  29. #include "../../../Include/RmlUi/Core/ElementText.h"
  30. #include "../../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../../Include/RmlUi/Core/Math.h"
  32. #include "../../../Include/RmlUi/Core/PropertyIdSet.h"
  33. #include "../../../Include/RmlUi/Core/StyleSheetSpecification.h"
  34. #include "WidgetTextInputMultiLine.h"
  35. namespace Rml {
  36. ElementFormControlTextArea::ElementFormControlTextArea(const String& tag) : ElementFormControl(tag)
  37. {
  38. widget = MakeUnique<WidgetTextInputMultiLine>(this);
  39. SetProperty(PropertyId::OverflowX, Property(Style::Overflow::Auto));
  40. SetProperty(PropertyId::OverflowY, Property(Style::Overflow::Auto));
  41. SetProperty(PropertyId::WhiteSpace, Property(Style::WhiteSpace::Prewrap));
  42. }
  43. ElementFormControlTextArea::~ElementFormControlTextArea() {}
  44. String ElementFormControlTextArea::GetValue() const
  45. {
  46. return GetAttribute<String>("value", "");
  47. }
  48. void ElementFormControlTextArea::SetValue(const String& value)
  49. {
  50. SetAttribute("value", value);
  51. }
  52. void ElementFormControlTextArea::SetNumColumns(int num_columns)
  53. {
  54. SetAttribute<int>("cols", Math::Max(1, num_columns));
  55. }
  56. int ElementFormControlTextArea::GetNumColumns() const
  57. {
  58. return GetAttribute<int>("cols", 20);
  59. }
  60. void ElementFormControlTextArea::SetNumRows(int num_rows)
  61. {
  62. SetAttribute<int>("rows", Math::Max(1, num_rows));
  63. }
  64. int ElementFormControlTextArea::GetNumRows() const
  65. {
  66. return GetAttribute<int>("rows", 2);
  67. }
  68. void ElementFormControlTextArea::SetMaxLength(int max_length)
  69. {
  70. SetAttribute<int>("maxlength", max_length);
  71. }
  72. int ElementFormControlTextArea::GetMaxLength() const
  73. {
  74. return GetAttribute<int>("maxlength", -1);
  75. }
  76. void ElementFormControlTextArea::SetWordWrap(bool word_wrap)
  77. {
  78. if (word_wrap != GetWordWrap())
  79. {
  80. if (word_wrap)
  81. RemoveAttribute("wrap");
  82. else
  83. SetAttribute("wrap", "nowrap");
  84. }
  85. }
  86. bool ElementFormControlTextArea::GetWordWrap()
  87. {
  88. String attribute = GetAttribute<String>("wrap", "");
  89. return attribute != "nowrap";
  90. }
  91. void ElementFormControlTextArea::Select()
  92. {
  93. widget->Select();
  94. }
  95. void ElementFormControlTextArea::SetSelectionRange(int selection_start, int selection_end)
  96. {
  97. widget->SetSelectionRange(selection_start, selection_end);
  98. }
  99. void ElementFormControlTextArea::GetSelection(int* selection_start, int* selection_end, String* selected_text) const
  100. {
  101. widget->GetSelection(selection_start, selection_end, selected_text);
  102. }
  103. bool ElementFormControlTextArea::GetIntrinsicDimensions(Vector2f& dimensions, float& /*ratio*/)
  104. {
  105. dimensions.x = (float)(GetNumColumns() * ElementUtilities::GetStringWidth(this, "m"));
  106. dimensions.y = (float)GetNumRows() * GetLineHeight();
  107. return true;
  108. }
  109. void ElementFormControlTextArea::OnUpdate()
  110. {
  111. widget->OnUpdate();
  112. }
  113. void ElementFormControlTextArea::OnRender()
  114. {
  115. widget->OnRender();
  116. }
  117. void ElementFormControlTextArea::OnResize()
  118. {
  119. widget->OnResize();
  120. }
  121. void ElementFormControlTextArea::OnLayout()
  122. {
  123. widget->OnLayout();
  124. }
  125. void ElementFormControlTextArea::OnAttributeChange(const ElementAttributes& changed_attributes)
  126. {
  127. ElementFormControl::OnAttributeChange(changed_attributes);
  128. if (changed_attributes.find("wrap") != changed_attributes.end())
  129. {
  130. if (GetWordWrap())
  131. SetProperty(PropertyId::WhiteSpace, Property(Style::WhiteSpace::Prewrap));
  132. else
  133. SetProperty(PropertyId::WhiteSpace, Property(Style::WhiteSpace::Pre));
  134. }
  135. if (changed_attributes.find("rows") != changed_attributes.end() || changed_attributes.find("cols") != changed_attributes.end())
  136. DirtyLayout();
  137. auto it = changed_attributes.find("maxlength");
  138. if (it != changed_attributes.end())
  139. widget->SetMaxLength(it->second.Get(-1));
  140. it = changed_attributes.find("value");
  141. if (it != changed_attributes.end())
  142. widget->SetValue(it->second.Get<String>());
  143. }
  144. void ElementFormControlTextArea::OnPropertyChange(const PropertyIdSet& changed_properties)
  145. {
  146. ElementFormControl::OnPropertyChange(changed_properties);
  147. // Some inherited properties require text formatting update, mainly font and line-height properties.
  148. const PropertyIdSet changed_inherited_layout_properties = changed_properties &
  149. (StyleSheetSpecification::GetRegisteredInheritedProperties() & StyleSheetSpecification::GetRegisteredPropertiesForcingLayout());
  150. if (!changed_inherited_layout_properties.Empty())
  151. widget->ForceFormattingOnNextLayout();
  152. if (changed_properties.Contains(PropertyId::Color) || changed_properties.Contains(PropertyId::BackgroundColor))
  153. widget->UpdateSelectionColours();
  154. if (changed_properties.Contains(PropertyId::CaretColor))
  155. widget->GenerateCursor();
  156. }
  157. void ElementFormControlTextArea::GetInnerRML(String& content) const
  158. {
  159. content = GetValue();
  160. }
  161. } // namespace Rml