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.

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