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.

220 lines
8.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_ATTRIBUTEDSTRING_JUCEHEADER__
  19. #define __JUCE_ATTRIBUTEDSTRING_JUCEHEADER__
  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. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  38. AttributedString (AttributedString&&) noexcept;
  39. AttributedString& operator= (AttributedString&&) noexcept;
  40. #endif
  41. /** Destructor. */
  42. ~AttributedString();
  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 (const 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. /** Creates an attribute that changes the colour for a range of characters.
  123. @see AttributedString::setColour()
  124. */
  125. Attribute (Range<int> range, Colour colour);
  126. /** Creates an attribute that changes the font for a range of characters.
  127. @see AttributedString::setFont()
  128. */
  129. Attribute (Range<int> range, const Font& font);
  130. Attribute (const Attribute&);
  131. ~Attribute();
  132. /** If this attribute specifies a font, this returns it; otherwise it returns nullptr. */
  133. const Font* getFont() const noexcept { return font; }
  134. /** If this attribute specifies a colour, this returns it; otherwise it returns nullptr. */
  135. const Colour* getColour() const noexcept { return colour; }
  136. /** The range of characters to which this attribute will be applied. */
  137. const Range<int> range;
  138. private:
  139. ScopedPointer<Font> font;
  140. ScopedPointer<Colour> colour;
  141. friend class AttributedString;
  142. Attribute (const Attribute&, int);
  143. Attribute& operator= (const Attribute&);
  144. JUCE_LEAK_DETECTOR (Attribute)
  145. };
  146. /** Returns the number of attributes that have been added to this string. */
  147. int getNumAttributes() const noexcept { return attributes.size(); }
  148. /** Returns one of the string's attributes.
  149. The index provided must be less than getNumAttributes(), and >= 0.
  150. */
  151. const Attribute* getAttribute (int index) const noexcept { return attributes.getUnchecked (index); }
  152. //==============================================================================
  153. /** Adds a colour attribute for the specified range. */
  154. void setColour (Range<int> range, Colour colour);
  155. /** Removes all existing colour attributes, and applies this colour to the whole string. */
  156. void setColour (Colour colour);
  157. /** Adds a font attribute for the specified range. */
  158. void setFont (Range<int> range, const Font& font);
  159. /** Removes all existing font attributes, and applies this font to the whole string. */
  160. void setFont (const Font& font);
  161. private:
  162. String text;
  163. float lineSpacing;
  164. Justification justification;
  165. WordWrap wordWrap;
  166. ReadingDirection readingDirection;
  167. OwnedArray<Attribute> attributes;
  168. JUCE_LEAK_DETECTOR (AttributedString)
  169. };
  170. #endif // __JUCE_ATTRIBUTEDSTRING_JUCEHEADER__