The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. A text string with a set of colour/font settings that are associated with sub-ranges
  21. of the text.
  22. An attributed string lets you create a string with varied fonts, colours, word-wrapping,
  23. layout, etc., and draw it using AttributedString::draw().
  24. @see TextLayout
  25. */
  26. class JUCE_API AttributedString
  27. {
  28. public:
  29. /** Creates an empty attributed string. */
  30. AttributedString();
  31. /** Creates an attributed string with the given text. */
  32. explicit AttributedString (const String& text);
  33. AttributedString (const AttributedString&);
  34. AttributedString& operator= (const AttributedString&);
  35. AttributedString (AttributedString&&) noexcept;
  36. AttributedString& operator= (AttributedString&&) noexcept;
  37. /** Destructor. */
  38. ~AttributedString() noexcept;
  39. //==============================================================================
  40. /** Returns the complete text of this attributed string. */
  41. const String& getText() const noexcept { return text; }
  42. /** Replaces all the text.
  43. This will change the text, but won't affect any of the colour or font attributes
  44. that have been added.
  45. */
  46. void setText (const String& newText);
  47. /** Appends some text (with a default font and colour). */
  48. void append (const String& textToAppend);
  49. /** Appends some text, with a specified font, and the default colour (black). */
  50. void append (const String& textToAppend, const Font& font);
  51. /** Appends some text, with a specified colour, and the default font. */
  52. void append (const String& textToAppend, Colour colour);
  53. /** Appends some text, with a specified font and colour. */
  54. void append (const String& textToAppend, const Font& font, Colour colour);
  55. /** Appends another AttributedString to this one.
  56. Note that this will only append the text, fonts, and colours - it won't copy any
  57. other properties such as justification, line-spacing, etc from the other object.
  58. */
  59. void append (const AttributedString& other);
  60. /** Resets the string, clearing all text and attributes.
  61. Note that this won't affect global settings like the justification type,
  62. word-wrap mode, etc.
  63. */
  64. void clear();
  65. //==============================================================================
  66. /** Draws this string within the given area.
  67. The layout of the string within the rectangle is controlled by the justification
  68. value passed to setJustification().
  69. */
  70. void draw (Graphics& g, const Rectangle<float>& area) const;
  71. //==============================================================================
  72. /** Returns the justification that should be used for laying-out the text.
  73. This may include both vertical and horizontal flags.
  74. */
  75. Justification getJustification() const noexcept { return justification; }
  76. /** Sets the justification that should be used for laying-out the text.
  77. This may include both vertical and horizontal flags.
  78. */
  79. void setJustification (Justification newJustification) noexcept;
  80. //==============================================================================
  81. /** Types of word-wrap behaviour.
  82. @see getWordWrap, setWordWrap
  83. */
  84. enum WordWrap
  85. {
  86. none, /**< No word-wrapping: lines extend indefinitely. */
  87. byWord, /**< Lines are wrapped on a word boundary. */
  88. byChar, /**< Lines are wrapped on a character boundary. */
  89. };
  90. /** Returns the word-wrapping behaviour. */
  91. WordWrap getWordWrap() const noexcept { return wordWrap; }
  92. /** Sets the word-wrapping behaviour. */
  93. void setWordWrap (WordWrap newWordWrap) noexcept;
  94. //==============================================================================
  95. /** Types of reading direction that can be used.
  96. @see getReadingDirection, setReadingDirection
  97. */
  98. enum ReadingDirection
  99. {
  100. natural,
  101. leftToRight,
  102. rightToLeft,
  103. };
  104. /** Returns the reading direction for the text. */
  105. ReadingDirection getReadingDirection() const noexcept { return readingDirection; }
  106. /** Sets the reading direction that should be used for the text. */
  107. void setReadingDirection (ReadingDirection newReadingDirection) noexcept;
  108. //==============================================================================
  109. /** Returns the extra line-spacing distance. */
  110. float getLineSpacing() const noexcept { return lineSpacing; }
  111. /** Sets an extra line-spacing distance. */
  112. void setLineSpacing (float newLineSpacing) noexcept;
  113. //==============================================================================
  114. /** An attribute that has been applied to a range of characters in an AttributedString. */
  115. class JUCE_API Attribute
  116. {
  117. public:
  118. Attribute() noexcept;
  119. ~Attribute() noexcept;
  120. Attribute (const Attribute&) noexcept;
  121. Attribute& operator= (const Attribute&) noexcept;
  122. Attribute (Attribute&&) noexcept;
  123. Attribute& operator= (Attribute&&) noexcept;
  124. /** Creates an attribute that specifies the font and colour for a range of characters. */
  125. Attribute (Range<int> range, const Font& font, Colour colour) noexcept;
  126. /** The range of characters to which this attribute will be applied. */
  127. Range<int> range;
  128. /** The font for this range of characters. */
  129. Font font;
  130. /** The colour for this range of characters. */
  131. Colour colour;
  132. private:
  133. JUCE_LEAK_DETECTOR (Attribute)
  134. };
  135. /** Returns the number of attributes that have been added to this string. */
  136. int getNumAttributes() const noexcept { return attributes.size(); }
  137. /** Returns one of the string's attributes.
  138. The index provided must be less than getNumAttributes(), and >= 0.
  139. */
  140. const Attribute& getAttribute (int index) const noexcept { return attributes.getReference (index); }
  141. //==============================================================================
  142. /** Adds a colour attribute for the specified range. */
  143. void setColour (Range<int> range, Colour colour);
  144. /** Removes all existing colour attributes, and applies this colour to the whole string. */
  145. void setColour (Colour colour);
  146. /** Adds a font attribute for the specified range. */
  147. void setFont (Range<int> range, const Font& font);
  148. /** Removes all existing font attributes, and applies this font to the whole string. */
  149. void setFont (const Font& font);
  150. private:
  151. String text;
  152. float lineSpacing;
  153. Justification justification;
  154. WordWrap wordWrap;
  155. ReadingDirection readingDirection;
  156. Array<Attribute> attributes;
  157. JUCE_LEAK_DETECTOR (AttributedString)
  158. };