ElementFormControlTextArea.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "../../../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. // Constructs a new ElementFormControlTextArea.
  37. ElementFormControlTextArea::ElementFormControlTextArea(const String& tag) : ElementFormControl(tag)
  38. {
  39. widget = new WidgetTextInputMultiLine(this);
  40. SetProperty(PropertyId::OverflowX, Property(Style::Overflow::Auto));
  41. SetProperty(PropertyId::OverflowY, Property(Style::Overflow::Auto));
  42. SetProperty(PropertyId::WhiteSpace, Property(Style::WhiteSpace::Prewrap));
  43. }
  44. ElementFormControlTextArea::~ElementFormControlTextArea()
  45. {
  46. delete widget;
  47. }
  48. // Returns a string representation of the current value of the form control.
  49. String ElementFormControlTextArea::GetValue() const
  50. {
  51. return GetAttribute< String >("value", "");
  52. }
  53. // Sets the current value of the form control.
  54. void ElementFormControlTextArea::SetValue(const String& value)
  55. {
  56. SetAttribute("value", value);
  57. }
  58. // Sets the number of characters visible across the text area. Note that this will only be precise when using a
  59. // fixed-width font.
  60. void ElementFormControlTextArea::SetNumColumns(int num_columns)
  61. {
  62. SetAttribute< int >("cols", Math::Max(1, num_columns));
  63. }
  64. // Returns the approximate number of characters visible at once.
  65. int ElementFormControlTextArea::GetNumColumns() const
  66. {
  67. return GetAttribute< int >("cols", 20);
  68. }
  69. // Sets the number of visible lines of text in the text area.
  70. void ElementFormControlTextArea::SetNumRows(int num_rows)
  71. {
  72. SetAttribute< int >("rows", Math::Max(1, num_rows));
  73. }
  74. // Returns the number of visible lines of text in the text area.
  75. int ElementFormControlTextArea::GetNumRows() const
  76. {
  77. return GetAttribute< int >("rows", 2);
  78. }
  79. // Sets the maximum length (in characters) of this text field.
  80. void ElementFormControlTextArea::SetMaxLength(int max_length)
  81. {
  82. SetAttribute< int >("maxlength", max_length);
  83. }
  84. // Returns the maximum length (in characters) of this text field.
  85. int ElementFormControlTextArea::GetMaxLength() const
  86. {
  87. return GetAttribute< int >("maxlength", -1);
  88. }
  89. // Enables or disables word-wrapping in the text area.
  90. void ElementFormControlTextArea::SetWordWrap(bool word_wrap)
  91. {
  92. if (word_wrap != GetWordWrap())
  93. {
  94. if (word_wrap)
  95. RemoveAttribute("wrap");
  96. else
  97. SetAttribute("wrap", "nowrap");
  98. }
  99. }
  100. // Returns the state of word-wrapping in the text area.
  101. bool ElementFormControlTextArea::GetWordWrap()
  102. {
  103. String attribute = GetAttribute< String >("wrap", "");
  104. return attribute != "nowrap";
  105. }
  106. // Returns the control's inherent size, based on the length of the input field and the current font size.
  107. bool ElementFormControlTextArea::GetIntrinsicDimensions(Vector2f& dimensions, float& /*ratio*/)
  108. {
  109. dimensions.x = (float) (GetNumColumns() * ElementUtilities::GetStringWidth(this, "m"));
  110. dimensions.y = (float)GetNumRows() * GetLineHeight();
  111. return true;
  112. }
  113. // Updates the control's widget.
  114. void ElementFormControlTextArea::OnUpdate()
  115. {
  116. widget->OnUpdate();
  117. }
  118. // Renders the control's widget.
  119. void ElementFormControlTextArea::OnRender()
  120. {
  121. widget->OnRender();
  122. }
  123. // Formats the element.
  124. void ElementFormControlTextArea::OnResize()
  125. {
  126. widget->OnResize();
  127. }
  128. // Formats the element.
  129. void ElementFormControlTextArea::OnLayout()
  130. {
  131. widget->OnLayout();
  132. }
  133. // Called when attributes on the element are changed.
  134. void ElementFormControlTextArea::OnAttributeChange(const ElementAttributes& changed_attributes)
  135. {
  136. ElementFormControl::OnAttributeChange(changed_attributes);
  137. if (changed_attributes.find("wrap") != changed_attributes.end())
  138. {
  139. if (GetWordWrap())
  140. SetProperty(PropertyId::WhiteSpace, Property(Style::WhiteSpace::Prewrap));
  141. else
  142. SetProperty(PropertyId::WhiteSpace, Property(Style::WhiteSpace::Pre));
  143. }
  144. if (changed_attributes.find("rows") != changed_attributes.end() || changed_attributes.find("cols") != changed_attributes.end())
  145. DirtyLayout();
  146. auto it = changed_attributes.find("maxlength");
  147. if (it != changed_attributes.end())
  148. widget->SetMaxLength(it->second.Get(-1));
  149. it = changed_attributes.find("value");
  150. if (it != changed_attributes.end())
  151. widget->SetValue(it->second.Get<String>());
  152. }
  153. // Called when properties on the control are changed.
  154. void ElementFormControlTextArea::OnPropertyChange(const PropertyIdSet& changed_properties)
  155. {
  156. ElementFormControl::OnPropertyChange(changed_properties);
  157. // Some inherited properties require text formatting update, mainly font and line-height properties.
  158. const PropertyIdSet changed_inherited_layout_properties = changed_properties &
  159. (StyleSheetSpecification::GetRegisteredInheritedProperties() & StyleSheetSpecification::GetRegisteredPropertiesForcingLayout());
  160. if (!changed_inherited_layout_properties.Empty())
  161. widget->ForceFormattingOnNextLayout();
  162. if (changed_properties.Contains(PropertyId::Color) || changed_properties.Contains(PropertyId::BackgroundColor))
  163. widget->UpdateSelectionColours();
  164. if (changed_properties.Contains(PropertyId::CaretColor))
  165. widget->GenerateCursor();
  166. }
  167. // Returns the text content of the element.
  168. void ElementFormControlTextArea::GetInnerRML(String& content) const
  169. {
  170. content = GetValue();
  171. }
  172. } // namespace Rml