ElementFormControlTextArea.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include <Rocket/Controls/ElementFormControlTextArea.h>
  28. #include <Rocket/Core/Math.h>
  29. #include <Rocket/Core/ElementUtilities.h>
  30. #include <Rocket/Core/ElementText.h>
  31. #include "WidgetTextInputMultiLine.h"
  32. namespace Rocket {
  33. namespace Controls {
  34. // Constructs a new ElementFormControlTextArea.
  35. ElementFormControlTextArea::ElementFormControlTextArea(const Rocket::Core::String& tag) : ElementFormControl(tag)
  36. {
  37. widget = new WidgetTextInputMultiLine(this);
  38. SetProperty("overflow", "auto");
  39. SetProperty("white-space", "pre-wrap");
  40. }
  41. ElementFormControlTextArea::~ElementFormControlTextArea()
  42. {
  43. delete widget;
  44. }
  45. // Returns a string representation of the current value of the form control.
  46. Rocket::Core::String ElementFormControlTextArea::GetValue() const
  47. {
  48. return GetAttribute< Rocket::Core::String >("value", "");
  49. }
  50. // Sets the current value of the form control.
  51. void ElementFormControlTextArea::SetValue(const Rocket::Core::String& value)
  52. {
  53. SetAttribute("value", value);
  54. }
  55. // Sets the number of characters visible across the text area. Note that this will only be precise when using a
  56. // fixed-width font.
  57. void ElementFormControlTextArea::SetNumColumns(int num_columns)
  58. {
  59. SetAttribute< int >("cols", Rocket::Core::Math::Max(1, num_columns));
  60. }
  61. // Returns the approximate number of characters visible at once.
  62. int ElementFormControlTextArea::GetNumColumns() const
  63. {
  64. return GetAttribute< int >("cols", 20);
  65. }
  66. // Sets the number of visible lines of text in the text area.
  67. void ElementFormControlTextArea::SetNumRows(int num_rows)
  68. {
  69. SetAttribute< int >("rows", Rocket::Core::Math::Max(1, num_rows));
  70. }
  71. // Returns the number of visible lines of text in the text area.
  72. int ElementFormControlTextArea::GetNumRows() const
  73. {
  74. return GetAttribute< int >("rows", 2);
  75. }
  76. // Sets the maximum length (in characters) of this text field.
  77. void ElementFormControlTextArea::SetMaxLength(int max_length)
  78. {
  79. SetAttribute< int >("maxlength", max_length);
  80. }
  81. // Returns the maximum length (in characters) of this text field.
  82. int ElementFormControlTextArea::GetMaxLength() const
  83. {
  84. return GetAttribute< int >("maxlength", -1);
  85. }
  86. // Enables or disables word-wrapping in the text area.
  87. void ElementFormControlTextArea::SetWordWrap(bool word_wrap)
  88. {
  89. if (word_wrap != GetWordWrap())
  90. {
  91. if (word_wrap)
  92. RemoveAttribute("wrap");
  93. else
  94. SetAttribute("wrap", "nowrap");
  95. }
  96. }
  97. // Returns the state of word-wrapping in the text area.
  98. bool ElementFormControlTextArea::GetWordWrap()
  99. {
  100. Rocket::Core::String attribute = GetAttribute< Rocket::Core::String >("wrap", "");
  101. return attribute != "nowrap";
  102. }
  103. // Returns the control's inherent size, based on the length of the input field and the current font size.
  104. bool ElementFormControlTextArea::GetIntrinsicDimensions(Rocket::Core::Vector2f& dimensions)
  105. {
  106. dimensions.x = (float) (GetNumColumns() * Core::ElementUtilities::GetStringWidth(this, "m"));
  107. dimensions.y = (float) (GetNumRows() * Core::ElementUtilities::GetLineHeight(this));
  108. return true;
  109. }
  110. // Updates the control's widget.
  111. void ElementFormControlTextArea::OnUpdate()
  112. {
  113. widget->OnUpdate();
  114. }
  115. // Renders the control's widget.
  116. void ElementFormControlTextArea::OnRender()
  117. {
  118. widget->OnRender();
  119. }
  120. // Formats the element.
  121. void ElementFormControlTextArea::OnLayout()
  122. {
  123. widget->OnLayout();
  124. }
  125. // Called when attributes on the element are changed.
  126. void ElementFormControlTextArea::OnAttributeChange(const Core::AttributeNameList& changed_attributes)
  127. {
  128. ElementFormControl::OnAttributeChange(changed_attributes);
  129. if (changed_attributes.find("wrap") != changed_attributes.end())
  130. {
  131. if (GetWordWrap())
  132. SetProperty("white-space", "pre-wrap");
  133. else
  134. SetProperty("white-space", "pre");
  135. }
  136. if (changed_attributes.find("rows") != changed_attributes.end() ||
  137. changed_attributes.find("cols") != changed_attributes.end())
  138. DirtyLayout();
  139. if (changed_attributes.find("maxlength") != changed_attributes.end())
  140. widget->SetMaxLength(GetMaxLength());
  141. if (changed_attributes.find("value") != changed_attributes.end())
  142. widget->SetValue(GetValue());
  143. }
  144. // Called when properties on the control are changed.
  145. void ElementFormControlTextArea::OnPropertyChange(const Core::PropertyNameList& changed_properties)
  146. {
  147. ElementFormControl::OnPropertyChange(changed_properties);
  148. if (changed_properties.find("color") != changed_properties.end() ||
  149. changed_properties.find("background-color") != changed_properties.end())
  150. widget->UpdateSelectionColours();
  151. }
  152. // Returns the text content of the element.
  153. void ElementFormControlTextArea::GetInnerRML(Rocket::Core::String& content) const
  154. {
  155. content = GetValue();
  156. }
  157. }
  158. }