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.

207 lines
8.0KB

  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. @tags{Graphics}
  29. */
  30. class JUCE_API AttributedString
  31. {
  32. public:
  33. /** Creates an empty attributed string. */
  34. AttributedString() = default;
  35. /** Creates an attributed string with the given text. */
  36. explicit AttributedString (const String& newString) { setText (newString); }
  37. AttributedString (const AttributedString&) = default;
  38. AttributedString& operator= (const AttributedString&) = default;
  39. AttributedString (AttributedString&&) noexcept = default;
  40. AttributedString& operator= (AttributedString&&) noexcept = default;
  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() = default;
  121. Attribute (const Attribute&) = default;
  122. Attribute& operator= (const Attribute&) = default;
  123. Attribute (Attribute&&) noexcept = default;
  124. Attribute& operator= (Attribute&&) noexcept = default;
  125. /** Creates an attribute that specifies the font and colour for a range of characters. */
  126. Attribute (Range<int> range, const Font& font, Colour colour) noexcept;
  127. /** The range of characters to which this attribute will be applied. */
  128. Range<int> range;
  129. /** The font for this range of characters. */
  130. Font font;
  131. /** The colour for this range of characters. */
  132. Colour colour { 0xff000000 };
  133. private:
  134. JUCE_LEAK_DETECTOR (Attribute)
  135. };
  136. /** Returns the number of attributes that have been added to this string. */
  137. int getNumAttributes() const noexcept { return attributes.size(); }
  138. /** Returns one of the string's attributes.
  139. The index provided must be less than getNumAttributes(), and >= 0.
  140. */
  141. const Attribute& getAttribute (int index) const noexcept { return attributes.getReference (index); }
  142. //==============================================================================
  143. /** Adds a colour attribute for the specified range. */
  144. void setColour (Range<int> range, Colour colour);
  145. /** Removes all existing colour attributes, and applies this colour to the whole string. */
  146. void setColour (Colour colour);
  147. /** Adds a font attribute for the specified range. */
  148. void setFont (Range<int> range, const Font& font);
  149. /** Removes all existing font attributes, and applies this font to the whole string. */
  150. void setFont (const Font& font);
  151. private:
  152. String text;
  153. float lineSpacing = 0.0f;
  154. Justification justification = Justification::left;
  155. WordWrap wordWrap = AttributedString::byWord;
  156. ReadingDirection readingDirection = AttributedString::natural;
  157. Array<Attribute> attributes;
  158. JUCE_LEAK_DETECTOR (AttributedString)
  159. };
  160. } // namespace juce