Audio plugin host https://kx.studio/carla
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.

219 lines
8.5KB

  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. #ifndef JUCE_ATTRIBUTEDSTRING_H_INCLUDED
  18. #define JUCE_ATTRIBUTEDSTRING_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A text string with a set of colour/font settings that are associated with sub-ranges
  22. of the text.
  23. An attributed string lets you create a string with varied fonts, colours, word-wrapping,
  24. layout, etc., and draw it using AttributedString::draw().
  25. @see TextLayout
  26. */
  27. class JUCE_API AttributedString
  28. {
  29. public:
  30. /** Creates an empty attributed string. */
  31. AttributedString();
  32. /** Creates an attributed string with the given text. */
  33. explicit AttributedString (const String& text);
  34. AttributedString (const AttributedString&);
  35. AttributedString& operator= (const AttributedString&);
  36. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  37. AttributedString (AttributedString&&) noexcept;
  38. AttributedString& operator= (AttributedString&&) noexcept;
  39. #endif
  40. /** Destructor. */
  41. ~AttributedString();
  42. //==============================================================================
  43. /** Returns the complete text of this attributed string. */
  44. const String& getText() const noexcept { return text; }
  45. /** Replaces all the text.
  46. This will change the text, but won't affect any of the colour or font attributes
  47. that have been added.
  48. */
  49. void setText (const String& newText);
  50. /** Appends some text (with a default font and colour). */
  51. void append (const String& textToAppend);
  52. /** Appends some text, with a specified font, and the default colour (black). */
  53. void append (const String& textToAppend, const Font& font);
  54. /** Appends some text, with a specified colour, and the default font. */
  55. void append (const String& textToAppend, Colour colour);
  56. /** Appends some text, with a specified font and colour. */
  57. void append (const String& textToAppend, const Font& font, Colour colour);
  58. /** Appends another AttributedString to this one.
  59. Note that this will only append the text, fonts, and colours - it won't copy any
  60. other properties such as justification, line-spacing, etc from the other object.
  61. */
  62. void append (const AttributedString& other);
  63. /** Resets the string, clearing all text and attributes.
  64. Note that this won't affect global settings like the justification type,
  65. word-wrap mode, etc.
  66. */
  67. void clear();
  68. //==============================================================================
  69. /** Draws this string within the given area.
  70. The layout of the string within the rectangle is controlled by the justification
  71. value passed to setJustification().
  72. */
  73. void draw (Graphics& g, const Rectangle<float>& area) const;
  74. //==============================================================================
  75. /** Returns the justification that should be used for laying-out the text.
  76. This may include both vertical and horizontal flags.
  77. */
  78. Justification getJustification() const noexcept { return justification; }
  79. /** Sets the justification that should be used for laying-out the text.
  80. This may include both vertical and horizontal flags.
  81. */
  82. void setJustification (Justification newJustification) noexcept;
  83. //==============================================================================
  84. /** Types of word-wrap behaviour.
  85. @see getWordWrap, setWordWrap
  86. */
  87. enum WordWrap
  88. {
  89. none, /**< No word-wrapping: lines extend indefinitely. */
  90. byWord, /**< Lines are wrapped on a word boundary. */
  91. byChar, /**< Lines are wrapped on a character boundary. */
  92. };
  93. /** Returns the word-wrapping behaviour. */
  94. WordWrap getWordWrap() const noexcept { return wordWrap; }
  95. /** Sets the word-wrapping behaviour. */
  96. void setWordWrap (WordWrap newWordWrap) noexcept;
  97. //==============================================================================
  98. /** Types of reading direction that can be used.
  99. @see getReadingDirection, setReadingDirection
  100. */
  101. enum ReadingDirection
  102. {
  103. natural,
  104. leftToRight,
  105. rightToLeft,
  106. };
  107. /** Returns the reading direction for the text. */
  108. ReadingDirection getReadingDirection() const noexcept { return readingDirection; }
  109. /** Sets the reading direction that should be used for the text. */
  110. void setReadingDirection (ReadingDirection newReadingDirection) noexcept;
  111. //==============================================================================
  112. /** Returns the extra line-spacing distance. */
  113. float getLineSpacing() const noexcept { return lineSpacing; }
  114. /** Sets an extra line-spacing distance. */
  115. void setLineSpacing (float newLineSpacing) noexcept;
  116. //==============================================================================
  117. /** An attribute that has been applied to a range of characters in an AttributedString. */
  118. class JUCE_API Attribute
  119. {
  120. public:
  121. /** Creates an attribute that changes the colour for a range of characters.
  122. @see AttributedString::setColour()
  123. */
  124. Attribute (Range<int> range, Colour colour);
  125. /** Creates an attribute that changes the font for a range of characters.
  126. @see AttributedString::setFont()
  127. */
  128. Attribute (Range<int> range, const Font& font);
  129. Attribute (const Attribute&);
  130. ~Attribute();
  131. /** If this attribute specifies a font, this returns it; otherwise it returns nullptr. */
  132. const Font* getFont() const noexcept { return font; }
  133. /** If this attribute specifies a colour, this returns it; otherwise it returns nullptr. */
  134. const Colour* getColour() const noexcept { return colour; }
  135. /** The range of characters to which this attribute will be applied. */
  136. const Range<int> range;
  137. private:
  138. ScopedPointer<Font> font;
  139. ScopedPointer<Colour> colour;
  140. friend class AttributedString;
  141. Attribute (const Attribute&, int);
  142. Attribute& operator= (const Attribute&);
  143. JUCE_LEAK_DETECTOR (Attribute)
  144. };
  145. /** Returns the number of attributes that have been added to this string. */
  146. int getNumAttributes() const noexcept { return attributes.size(); }
  147. /** Returns one of the string's attributes.
  148. The index provided must be less than getNumAttributes(), and >= 0.
  149. */
  150. const Attribute* getAttribute (int index) const noexcept { return attributes.getUnchecked (index); }
  151. //==============================================================================
  152. /** Adds a colour attribute for the specified range. */
  153. void setColour (Range<int> range, Colour colour);
  154. /** Removes all existing colour attributes, and applies this colour to the whole string. */
  155. void setColour (Colour colour);
  156. /** Adds a font attribute for the specified range. */
  157. void setFont (Range<int> range, const Font& font);
  158. /** Removes all existing font attributes, and applies this font to the whole string. */
  159. void setFont (const Font& font);
  160. private:
  161. String text;
  162. float lineSpacing;
  163. Justification justification;
  164. WordWrap wordWrap;
  165. ReadingDirection readingDirection;
  166. OwnedArray<Attribute> attributes;
  167. JUCE_LEAK_DETECTOR (AttributedString)
  168. };
  169. #endif // JUCE_ATTRIBUTEDSTRING_H_INCLUDED