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.

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