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.

208 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A text string with a set of colour/font settings that are associated with sub-ranges
  24. of the text.
  25. An attributed string lets you create a string with varied fonts, colours, word-wrapping,
  26. layout, etc., and draw it using AttributedString::draw().
  27. @see TextLayout
  28. */
  29. class JUCE_API AttributedString
  30. {
  31. public:
  32. /** Creates an empty attributed string. */
  33. AttributedString();
  34. /** Creates an attributed string with the given text. */
  35. explicit AttributedString (const String& text);
  36. AttributedString (const AttributedString&);
  37. AttributedString& operator= (const AttributedString&);
  38. AttributedString (AttributedString&&) noexcept;
  39. AttributedString& operator= (AttributedString&&) noexcept;
  40. /** Destructor. */
  41. ~AttributedString() noexcept;
  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. Attribute() noexcept;
  122. ~Attribute() noexcept;
  123. Attribute (const Attribute&) noexcept;
  124. Attribute& operator= (const Attribute&) noexcept;
  125. Attribute (Attribute&&) noexcept;
  126. Attribute& operator= (Attribute&&) noexcept;
  127. /** Creates an attribute that specifies the font and colour for a range of characters. */
  128. Attribute (Range<int> range, const Font& font, Colour colour) noexcept;
  129. /** The range of characters to which this attribute will be applied. */
  130. Range<int> range;
  131. /** The font for this range of characters. */
  132. Font font;
  133. /** The colour for this range of characters. */
  134. Colour colour;
  135. private:
  136. JUCE_LEAK_DETECTOR (Attribute)
  137. };
  138. /** Returns the number of attributes that have been added to this string. */
  139. int getNumAttributes() const noexcept { return attributes.size(); }
  140. /** Returns one of the string's attributes.
  141. The index provided must be less than getNumAttributes(), and >= 0.
  142. */
  143. const Attribute& getAttribute (int index) const noexcept { return attributes.getReference (index); }
  144. //==============================================================================
  145. /** Adds a colour attribute for the specified range. */
  146. void setColour (Range<int> range, Colour colour);
  147. /** Removes all existing colour attributes, and applies this colour to the whole string. */
  148. void setColour (Colour colour);
  149. /** Adds a font attribute for the specified range. */
  150. void setFont (Range<int> range, const Font& font);
  151. /** Removes all existing font attributes, and applies this font to the whole string. */
  152. void setFont (const Font& font);
  153. private:
  154. String text;
  155. float lineSpacing;
  156. Justification justification;
  157. WordWrap wordWrap;
  158. ReadingDirection readingDirection;
  159. Array<Attribute> attributes;
  160. JUCE_LEAK_DETECTOR (AttributedString)
  161. };
  162. } // namespace juce