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.

154 lines
5.8KB

  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_TEXTLAYOUT_JUCEHEADER__
  19. #define __JUCE_TEXTLAYOUT_JUCEHEADER__
  20. #include "juce_Font.h"
  21. #include "../placement/juce_Justification.h"
  22. class Graphics;
  23. //==============================================================================
  24. /**
  25. A laid-out arrangement of text.
  26. You can add text in different fonts to a TextLayout object, then call its
  27. layout() method to word-wrap it into lines. The layout can then be drawn
  28. using a graphics context.
  29. It's handy if you've got a message to display, because you can format it,
  30. measure the extent of the layout, and then create a suitably-sized window
  31. to show it in.
  32. @see Font, Graphics::drawFittedText, GlyphArrangement
  33. */
  34. class JUCE_API TextLayout
  35. {
  36. public:
  37. //==============================================================================
  38. /** Creates an empty text layout.
  39. Text can then be appended using the appendText() method.
  40. */
  41. TextLayout();
  42. /** Creates a copy of another layout object. */
  43. TextLayout (const TextLayout& other);
  44. /** Creates a text layout from an initial string and font. */
  45. TextLayout (const String& text, const Font& font);
  46. /** Destructor. */
  47. ~TextLayout();
  48. /** Copies another layout onto this one. */
  49. TextLayout& operator= (const TextLayout& layoutToCopy);
  50. //==============================================================================
  51. /** Clears the layout, removing all its text. */
  52. void clear();
  53. /** Adds a string to the end of the arrangement.
  54. The string will be broken onto new lines wherever it contains
  55. carriage-returns or linefeeds. After adding it, you can call layout()
  56. to wrap long lines into a paragraph and justify it.
  57. */
  58. void appendText (const String& textToAppend,
  59. const Font& fontToUse);
  60. /** Replaces all the text with a new string.
  61. This is equivalent to calling clear() followed by appendText().
  62. */
  63. void setText (const String& newText,
  64. const Font& fontToUse);
  65. /** Returns true if the layout has not had any text added yet. */
  66. bool isEmpty() const;
  67. //==============================================================================
  68. /** Breaks the text up to form a paragraph with the given width.
  69. @param maximumWidth any text wider than this will be split
  70. across multiple lines
  71. @param justification how the lines are to be laid-out horizontally
  72. @param attemptToBalanceLineLengths if true, it will try to split the lines at a
  73. width that keeps all the lines of text at a
  74. similar length - this is good when you're displaying
  75. a short message and don't want it to get split
  76. onto two lines with only a couple of words on
  77. the second line, which looks untidy.
  78. */
  79. void layout (int maximumWidth,
  80. const Justification& justification,
  81. bool attemptToBalanceLineLengths);
  82. //==============================================================================
  83. /** Returns the overall width of the entire text layout. */
  84. int getWidth() const;
  85. /** Returns the overall height of the entire text layout. */
  86. int getHeight() const;
  87. /** Returns the total number of lines of text. */
  88. int getNumLines() const { return totalLines; }
  89. /** Returns the width of a particular line of text.
  90. @param lineNumber the line, from 0 to (getNumLines() - 1)
  91. */
  92. int getLineWidth (int lineNumber) const;
  93. //==============================================================================
  94. /** Renders the text at a specified position using a graphics context.
  95. */
  96. void draw (Graphics& g, int topLeftX, int topLeftY) const;
  97. /** Renders the text within a specified rectangle using a graphics context.
  98. The justification flags dictate how the block of text should be positioned
  99. within the rectangle.
  100. */
  101. void drawWithin (Graphics& g,
  102. int x, int y, int w, int h,
  103. const Justification& layoutFlags) const;
  104. private:
  105. //==============================================================================
  106. class Token;
  107. friend class OwnedArray <Token>;
  108. OwnedArray <Token> tokens;
  109. int totalLines;
  110. JUCE_LEAK_DETECTOR (TextLayout);
  111. };
  112. #endif // __JUCE_TEXTLAYOUT_JUCEHEADER__