ElementFormControlTextArea.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "../../Include/Rocket/Controls/ElementFormControlTextArea.h"
  28. #include "../../Include/Rocket/Core/Math.h"
  29. #include "../../Include/Rocket/Core/ElementUtilities.h"
  30. #include "../../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(Core::PropertyId::OverflowX, Core::Property(Core::Style::Overflow::Auto));
  39. SetProperty(Core::PropertyId::OverflowY, Core::Property(Core::Style::Overflow::Auto));
  40. SetProperty(Core::PropertyId::WhiteSpace, Core::Property(Core::Style::WhiteSpace::Prewrap));
  41. }
  42. ElementFormControlTextArea::~ElementFormControlTextArea()
  43. {
  44. delete widget;
  45. }
  46. // Returns a string representation of the current value of the form control.
  47. Rocket::Core::String ElementFormControlTextArea::GetValue() const
  48. {
  49. return GetAttribute< Rocket::Core::String >("value", "");
  50. }
  51. // Sets the current value of the form control.
  52. void ElementFormControlTextArea::SetValue(const Rocket::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", Rocket::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", Rocket::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. Rocket::Core::String attribute = GetAttribute< Rocket::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(Rocket::Core::Vector2f& dimensions)
  106. {
  107. dimensions.x = (float) (GetNumColumns() * Core::ElementUtilities::GetStringWidth(this, L"m"));
  108. dimensions.y = (float)GetNumRows() * GetLineHeight();
  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::ElementAttributes& changed_attributes)
  128. {
  129. ElementFormControl::OnAttributeChange(changed_attributes);
  130. if (changed_attributes.find("wrap") != changed_attributes.end())
  131. {
  132. if (GetWordWrap())
  133. SetProperty(Core::PropertyId::WhiteSpace, Core::Property(Core::Style::WhiteSpace::Prewrap));
  134. else
  135. SetProperty(Core::PropertyId::WhiteSpace, Core::Property(Core::Style::WhiteSpace::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(Core::PropertyId::Color) != changed_properties.end() ||
  150. changed_properties.find(Core::PropertyId::BackgroundColor) != changed_properties.end())
  151. widget->UpdateSelectionColours();
  152. }
  153. // Returns the text content of the element.
  154. void ElementFormControlTextArea::GetInnerRML(Rocket::Core::String& content) const
  155. {
  156. content = GetValue();
  157. }
  158. }
  159. }