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.

juce_AttributedString.h 8.1KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_ATTRIBUTEDSTRING_H_INCLUDED
  18. #define JUCE_ATTRIBUTEDSTRING_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A text string with a set of colour/font settings that are associated with sub-ranges
  22. of the text.
  23. An attributed string lets you create a string with varied fonts, colours, word-wrapping,
  24. layout, etc., and draw it using AttributedString::draw().
  25. @see TextLayout
  26. */
  27. class JUCE_API AttributedString
  28. {
  29. public:
  30. /** Creates an empty attributed string. */
  31. AttributedString();
  32. /** Creates an attributed string with the given text. */
  33. explicit AttributedString (const String& text);
  34. AttributedString (const AttributedString&);
  35. AttributedString& operator= (const AttributedString&);
  36. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  37. AttributedString (AttributedString&&) noexcept;
  38. AttributedString& operator= (AttributedString&&) noexcept;
  39. #endif
  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. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  126. Attribute (Attribute&&) noexcept;
  127. Attribute& operator= (Attribute&&) noexcept;
  128. #endif
  129. /** Creates an attribute that specifies the font and colour for a range of characters. */
  130. Attribute (Range<int> range, const Font& font, Colour colour) noexcept;
  131. /** The range of characters to which this attribute will be applied. */
  132. Range<int> range;
  133. /** The font for this range of characters. */
  134. Font font;
  135. /** The colour for this range of characters. */
  136. Colour colour;
  137. private:
  138. JUCE_LEAK_DETECTOR (Attribute)
  139. };
  140. /** Returns the number of attributes that have been added to this string. */
  141. int getNumAttributes() const noexcept { return attributes.size(); }
  142. /** Returns one of the string's attributes.
  143. The index provided must be less than getNumAttributes(), and >= 0.
  144. */
  145. const Attribute& getAttribute (int index) const noexcept { return attributes.getReference (index); }
  146. //==============================================================================
  147. /** Adds a colour attribute for the specified range. */
  148. void setColour (Range<int> range, Colour colour);
  149. /** Removes all existing colour attributes, and applies this colour to the whole string. */
  150. void setColour (Colour colour);
  151. /** Adds a font attribute for the specified range. */
  152. void setFont (Range<int> range, const Font& font);
  153. /** Removes all existing font attributes, and applies this font to the whole string. */
  154. void setFont (const Font& font);
  155. private:
  156. String text;
  157. float lineSpacing;
  158. Justification justification;
  159. WordWrap wordWrap;
  160. ReadingDirection readingDirection;
  161. Array<Attribute> attributes;
  162. JUCE_LEAK_DETECTOR (AttributedString)
  163. };
  164. #endif // JUCE_ATTRIBUTEDSTRING_H_INCLUDED