ElementFormControlTextArea.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/Controls/ElementFormControlTextArea.h"
  29. #include "../../Include/RmlUi/Core/Math.h"
  30. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../Include/RmlUi/Core/ElementText.h"
  32. #include "WidgetTextInputMultiLine.h"
  33. namespace Rml {
  34. namespace Controls {
  35. // Constructs a new ElementFormControlTextArea.
  36. ElementFormControlTextArea::ElementFormControlTextArea(const Rml::Core::String& tag) : ElementFormControl(tag)
  37. {
  38. widget = new WidgetTextInputMultiLine(this);
  39. SetProperty("overflow", "auto");
  40. SetProperty("white-space", "pre-wrap");
  41. }
  42. ElementFormControlTextArea::~ElementFormControlTextArea()
  43. {
  44. delete widget;
  45. }
  46. // Returns a string representation of the current value of the form control.
  47. Rml::Core::String ElementFormControlTextArea::GetValue() const
  48. {
  49. return GetAttribute< Rml::Core::String >("value", "");
  50. }
  51. // Sets the current value of the form control.
  52. void ElementFormControlTextArea::SetValue(const Rml::Core::String& value)
  53. {
  54. SetAttribute("value", value);
  55. }
  56. // Sets the number of characters visible across the text area. Note that this will only be precise when using a
  57. // fixed-width font.
  58. void ElementFormControlTextArea::SetNumColumns(int num_columns)
  59. {
  60. SetAttribute< int >("cols", Rml::Core::Math::Max(1, num_columns));
  61. }
  62. // Returns the approximate number of characters visible at once.
  63. int ElementFormControlTextArea::GetNumColumns() const
  64. {
  65. return GetAttribute< int >("cols", 20);
  66. }
  67. // Sets the number of visible lines of text in the text area.
  68. void ElementFormControlTextArea::SetNumRows(int num_rows)
  69. {
  70. SetAttribute< int >("rows", Rml::Core::Math::Max(1, num_rows));
  71. }
  72. // Returns the number of visible lines of text in the text area.
  73. int ElementFormControlTextArea::GetNumRows() const
  74. {
  75. return GetAttribute< int >("rows", 2);
  76. }
  77. // Sets the maximum length (in characters) of this text field.
  78. void ElementFormControlTextArea::SetMaxLength(int max_length)
  79. {
  80. SetAttribute< int >("maxlength", max_length);
  81. }
  82. // Returns the maximum length (in characters) of this text field.
  83. int ElementFormControlTextArea::GetMaxLength() const
  84. {
  85. return GetAttribute< int >("maxlength", -1);
  86. }
  87. // Enables or disables word-wrapping in the text area.
  88. void ElementFormControlTextArea::SetWordWrap(bool word_wrap)
  89. {
  90. if (word_wrap != GetWordWrap())
  91. {
  92. if (word_wrap)
  93. RemoveAttribute("wrap");
  94. else
  95. SetAttribute("wrap", "nowrap");
  96. }
  97. }
  98. // Returns the state of word-wrapping in the text area.
  99. bool ElementFormControlTextArea::GetWordWrap()
  100. {
  101. Rml::Core::String attribute = GetAttribute< Rml::Core::String >("wrap", "");
  102. return attribute != "nowrap";
  103. }
  104. // Returns the control's inherent size, based on the length of the input field and the current font size.
  105. bool ElementFormControlTextArea::GetIntrinsicDimensions(Rml::Core::Vector2f& dimensions)
  106. {
  107. dimensions.x = (float) (GetNumColumns() * Core::ElementUtilities::GetStringWidth(this, "m"));
  108. dimensions.y = (float) (GetNumRows() * Core::ElementUtilities::GetLineHeight(this));
  109. return true;
  110. }
  111. // Updates the control's widget.
  112. void ElementFormControlTextArea::OnUpdate()
  113. {
  114. widget->OnUpdate();
  115. }
  116. // Renders the control's widget.
  117. void ElementFormControlTextArea::OnRender()
  118. {
  119. widget->OnRender();
  120. }
  121. // Formats the element.
  122. void ElementFormControlTextArea::OnLayout()
  123. {
  124. widget->OnLayout();
  125. }
  126. // Called when attributes on the element are changed.
  127. void ElementFormControlTextArea::OnAttributeChange(const Core::AttributeNameList& changed_attributes)
  128. {
  129. ElementFormControl::OnAttributeChange(changed_attributes);
  130. if (changed_attributes.find("wrap") != changed_attributes.end())
  131. {
  132. if (GetWordWrap())
  133. SetProperty("white-space", "pre-wrap");
  134. else
  135. SetProperty("white-space", "pre");
  136. }
  137. if (changed_attributes.find("rows") != changed_attributes.end() ||
  138. changed_attributes.find("cols") != changed_attributes.end())
  139. DirtyLayout();
  140. if (changed_attributes.find("maxlength") != changed_attributes.end())
  141. widget->SetMaxLength(GetMaxLength());
  142. if (changed_attributes.find("value") != changed_attributes.end())
  143. widget->SetValue(GetValue());
  144. }
  145. // Called when properties on the control are changed.
  146. void ElementFormControlTextArea::OnPropertyChange(const Core::PropertyNameList& changed_properties)
  147. {
  148. ElementFormControl::OnPropertyChange(changed_properties);
  149. if (changed_properties.find("color") != changed_properties.end() ||
  150. changed_properties.find("background-color") != changed_properties.end())
  151. widget->UpdateSelectionColours();
  152. }
  153. // Returns the text content of the element.
  154. void ElementFormControlTextArea::GetInnerRML(Rml::Core::String& content) const
  155. {
  156. content = GetValue();
  157. }
  158. }
  159. }