From dc9e0cb9bb2e6021307ce16d48fa471e13acb5ff Mon Sep 17 00:00:00 2001 From: jules Date: Sat, 14 Jul 2012 11:54:29 +0100 Subject: [PATCH] Added some new versions of Graphics::drawText and drawFittedText that take Rectangle parameters. --- .../Utility/jucer_JucerTreeViewBase.cpp | 4 +- .../Source/Utility/jucer_MiscUtilities.h | 3 +- .../Source/demos/AudioDemoPlaybackPage.cpp | 3 +- .../JuceDemo/Source/demos/DragAndDropDemo.cpp | 2 +- .../JuceDemo/Source/demos/ThreadingDemo.cpp | 2 +- extras/JuceDemo/Source/demos/WidgetsDemo.cpp | 5 +- .../Source/GraphEditorPanel.cpp | 4 +- .../contexts/juce_GraphicsContext.cpp | 50 +++++++++++-------- .../contexts/juce_GraphicsContext.h | 39 +++++++++++++++ .../buttons/juce_HyperlinkButton.cpp | 3 +- .../lookandfeel/juce_LookAndFeel.cpp | 8 +-- .../properties/juce_PropertyPanel.cpp | 2 +- .../juce_gui_basics/widgets/juce_ComboBox.cpp | 4 +- .../juce_gui_basics/widgets/juce_Slider.cpp | 2 +- .../widgets/juce_TextEditor.cpp | 3 +- .../misc/juce_ColourSelector.cpp | 9 ++-- 16 files changed, 87 insertions(+), 56 deletions(-) diff --git a/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.cpp b/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.cpp index 718122d0e2..0399f2ffe7 100644 --- a/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.cpp +++ b/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.cpp @@ -96,9 +96,7 @@ void JucerTreeViewBase::paintContent (Graphics& g, const Rectangle& area) g.setColour (isMissing() ? getContrastingColour (Colours::red, 0.8f) : getContrastingColour (0.8f)); - g.drawFittedText (getDisplayName(), - area.getX(), area.getY(), area.getWidth(), area.getHeight(), - Justification::centredLeft, 1, 0.8f); + g.drawFittedText (getDisplayName(), area, Justification::centredLeft, 1, 0.8f); } Component* JucerTreeViewBase::createItemComponent() diff --git a/extras/Introjucer/Source/Utility/jucer_MiscUtilities.h b/extras/Introjucer/Source/Utility/jucer_MiscUtilities.h index a770d248e0..57105775ce 100644 --- a/extras/Introjucer/Source/Utility/jucer_MiscUtilities.h +++ b/extras/Introjucer/Source/Utility/jucer_MiscUtilities.h @@ -287,8 +287,7 @@ public: g.setColour (Colours::white.overlaidWith (colour).contrasting()); g.setFont (Font (getHeight() * 0.6f, Font::bold)); - g.drawFittedText (colour.toDisplayString (true), - 2, 1, getWidth() - 4, getHeight() - 1, + g.drawFittedText (colour.toDisplayString (true), getLocalBounds().reduced (2, 1), Justification::centred, 1); } diff --git a/extras/JuceDemo/Source/demos/AudioDemoPlaybackPage.cpp b/extras/JuceDemo/Source/demos/AudioDemoPlaybackPage.cpp index 8455355b2d..f59f29380e 100644 --- a/extras/JuceDemo/Source/demos/AudioDemoPlaybackPage.cpp +++ b/extras/JuceDemo/Source/demos/AudioDemoPlaybackPage.cpp @@ -100,8 +100,7 @@ public: else { g.setFont (14.0f); - g.drawFittedText ("(No audio file selected)", 0, 0, getWidth(), getHeight(), - Justification::centred, 2); + g.drawFittedText ("(No audio file selected)", getLocalBounds(), Justification::centred, 2); } } diff --git a/extras/JuceDemo/Source/demos/DragAndDropDemo.cpp b/extras/JuceDemo/Source/demos/DragAndDropDemo.cpp index b4777855fc..94eaef20a7 100644 --- a/extras/JuceDemo/Source/demos/DragAndDropDemo.cpp +++ b/extras/JuceDemo/Source/demos/DragAndDropDemo.cpp @@ -127,7 +127,7 @@ public: g.setColour (Colours::black); g.setFont (14.0f); - g.drawFittedText (message, 10, 0, getWidth() - 20, getHeight(), Justification::centred, 4); + g.drawFittedText (message, getLocalBounds().reduced (10, 0), Justification::centred, 4); } //============================================================================== diff --git a/extras/JuceDemo/Source/demos/ThreadingDemo.cpp b/extras/JuceDemo/Source/demos/ThreadingDemo.cpp index 270006dd2a..1fadbca301 100644 --- a/extras/JuceDemo/Source/demos/ThreadingDemo.cpp +++ b/extras/JuceDemo/Source/demos/ThreadingDemo.cpp @@ -66,7 +66,7 @@ public: g.setColour (Colours::black); g.setFont (10.0f); - g.drawText (String::toHexString ((int64) threadId), 0, 0, getWidth(), getHeight(), Justification::centred, false); + g.drawText (String::toHexString ((int64) threadId), getLocalBounds(), Justification::centred, false); } void parentSizeChanged() diff --git a/extras/JuceDemo/Source/demos/WidgetsDemo.cpp b/extras/JuceDemo/Source/demos/WidgetsDemo.cpp index d81dbaa905..9508096d92 100644 --- a/extras/JuceDemo/Source/demos/WidgetsDemo.cpp +++ b/extras/JuceDemo/Source/demos/WidgetsDemo.cpp @@ -136,7 +136,8 @@ public: g.setFont (15.0f); g.setColour (Colours::black); g.drawFittedText ("drag this box onto the desktop to show how the same component can move from being lightweight to being a separate window", - 4, 0, getWidth() - 8, getHeight(), Justification::horizontallyJustified, 5); + getLocalBounds().reduced (4, 0), + getHeight(), Justification::horizontallyJustified, 5); g.drawRect (getLocalBounds()); } @@ -179,7 +180,7 @@ public: g.setColour (Colours::black); g.drawFittedText ("this is a customised menu item (also demonstrating the Timer class)...", - 4, 0, getWidth() - 8, getHeight(), + getLocalBounds().reduced (4, 0), Justification::centred, 3); } diff --git a/extras/audio plugin host/Source/GraphEditorPanel.cpp b/extras/audio plugin host/Source/GraphEditorPanel.cpp index 659d4b44b6..7b2f1852c5 100644 --- a/extras/audio plugin host/Source/GraphEditorPanel.cpp +++ b/extras/audio plugin host/Source/GraphEditorPanel.cpp @@ -334,9 +334,7 @@ public: g.setColour (Colours::black); g.setFont (font); - g.drawFittedText (getName(), - x + 4, y + 2, w - 8, h - 4, - Justification::centred, 2); + g.drawFittedText (getName(), getLocalBounds().reduced (4, 2), Justification::centred, 2); g.setColour (Colours::grey); g.drawRect (x, y, w, h); diff --git a/modules/juce_graphics/contexts/juce_GraphicsContext.cpp b/modules/juce_graphics/contexts/juce_GraphicsContext.cpp index 2605ad6825..f696cceccf 100644 --- a/modules/juce_graphics/contexts/juce_GraphicsContext.cpp +++ b/modules/juce_graphics/contexts/juce_GraphicsContext.cpp @@ -38,13 +38,8 @@ namespace } //============================================================================== -LowLevelGraphicsContext::LowLevelGraphicsContext() -{ -} - -LowLevelGraphicsContext::~LowLevelGraphicsContext() -{ -} +LowLevelGraphicsContext::LowLevelGraphicsContext() {} +LowLevelGraphicsContext::~LowLevelGraphicsContext() {} //============================================================================== Graphics::Graphics (const Image& imageToDrawOnto) @@ -266,7 +261,8 @@ void Graphics::drawTextAsPath (const String& text, const AffineTransform& transf } } -void Graphics::drawMultiLineText (const String& text, const int startX, const int baselineY, const int maximumLineWidth) const +void Graphics::drawMultiLineText (const String& text, const int startX, + const int baselineY, const int maximumLineWidth) const { if (text.isNotEmpty() && startX < context.getClipBounds().getRight()) @@ -279,40 +275,43 @@ void Graphics::drawMultiLineText (const String& text, const int startX, const in } } -void Graphics::drawText (const String& text, - const int x, const int y, const int width, const int height, +void Graphics::drawText (const String& text, const Rectangle& area, const Justification& justificationType, const bool useEllipsesIfTooBig) const { - if (text.isNotEmpty() && context.clipRegionIntersects (Rectangle (x, y, width, height))) + if (text.isNotEmpty() && context.clipRegionIntersects (area)) { GlyphArrangement arr; - arr.addCurtailedLineOfText (context.getFont(), text, - 0.0f, 0.0f, (float) width, + 0.0f, 0.0f, (float) area.getWidth(), useEllipsesIfTooBig); arr.justifyGlyphs (0, arr.getNumGlyphs(), - (float) x, (float) y, (float) width, (float) height, + (float) area.getX(), (float) area.getY(), + (float) area.getWidth(), (float) area.getHeight(), justificationType); arr.draw (*this); } } -void Graphics::drawFittedText (const String& text, - const int x, const int y, const int width, const int height, +void Graphics::drawText (const String& text, const int x, const int y, const int width, const int height, + const Justification& justificationType, + const bool useEllipsesIfTooBig) const +{ + drawText (text, Rectangle (x, y, width, height), justificationType, useEllipsesIfTooBig); +} + +void Graphics::drawFittedText (const String& text, const Rectangle& area, const Justification& justification, const int maximumNumberOfLines, const float minimumHorizontalScale) const { - if (text.isNotEmpty() - && width > 0 && height > 0 - && context.clipRegionIntersects (Rectangle (x, y, width, height))) + if (text.isNotEmpty() && (! area.isEmpty()) && context.clipRegionIntersects (area)) { GlyphArrangement arr; - arr.addFittedText (context.getFont(), text, - (float) x, (float) y, (float) width, (float) height, + (float) area.getX(), (float) area.getY(), + (float) area.getWidth(), (float) area.getHeight(), justification, maximumNumberOfLines, minimumHorizontalScale); @@ -321,6 +320,15 @@ void Graphics::drawFittedText (const String& text, } } +void Graphics::drawFittedText (const String& text, const int x, const int y, const int width, const int height, + const Justification& justification, + const int maximumNumberOfLines, + const float minimumHorizontalScale) const +{ + drawFittedText (text,Rectangle (x, y, width, height), + justification, maximumNumberOfLines, minimumHorizontalScale); +} + //============================================================================== void Graphics::fillRect (int x, int y, int width, int height) const { diff --git a/modules/juce_graphics/contexts/juce_GraphicsContext.h b/modules/juce_graphics/contexts/juce_GraphicsContext.h index 72ec791d1e..b2166295ae 100644 --- a/modules/juce_graphics/contexts/juce_GraphicsContext.h +++ b/modules/juce_graphics/contexts/juce_GraphicsContext.h @@ -184,6 +184,20 @@ public: const Justification& justificationType, bool useEllipsesIfTooBig) const; + /** Draws a line of text within a specified rectangle. + + The text will be positioned within the rectangle based on the justification + flags passed-in. If the string is too long to fit inside the rectangle, it will + either be truncated or will have ellipsis added to its end (if the useEllipsesIfTooBig + flag is true). + + @see drawSingleLineText, drawFittedText, drawMultiLineText, GlyphArrangement::addJustifiedText + */ + void drawText (const String& text, + const Rectangle& area, + const Justification& justificationType, + bool useEllipsesIfTooBig) const; + /** Tries to draw a text string inside a given space. This does its best to make the given text readable within the specified rectangle, @@ -209,6 +223,31 @@ public: int maximumNumberOfLines, float minimumHorizontalScale = 0.7f) const; + /** Tries to draw a text string inside a given space. + + This does its best to make the given text readable within the specified rectangle, + so it useful for labelling things. + + If the text is too big, it'll be squashed horizontally or broken over multiple lines + if the maximumLinesToUse value allows this. If the text just won't fit into the space, + it'll cram as much as possible in there, and put some ellipsis at the end to show that + it's been truncated. + + A Justification parameter lets you specify how the text is laid out within the rectangle, + both horizontally and vertically. + + The minimumHorizontalScale parameter specifies how much the text can be squashed horizontally + to try to squeeze it into the space. If you don't want any horizontal scaling to occur, you + can set this value to 1.0f. + + @see GlyphArrangement::addFittedText + */ + void drawFittedText (const String& text, + const Rectangle& area, + const Justification& justificationFlags, + int maximumNumberOfLines, + float minimumHorizontalScale = 0.7f) const; + //============================================================================== /** Fills the context's entire clip region with the current colour or brush. diff --git a/modules/juce_gui_basics/buttons/juce_HyperlinkButton.cpp b/modules/juce_gui_basics/buttons/juce_HyperlinkButton.cpp index 0f5c3e07ae..7672e4ef42 100644 --- a/modules/juce_gui_basics/buttons/juce_HyperlinkButton.cpp +++ b/modules/juce_gui_basics/buttons/juce_HyperlinkButton.cpp @@ -104,8 +104,7 @@ void HyperlinkButton::paintButton (Graphics& g, g.setFont (getFontToUse()); - g.drawText (getButtonText(), - 2, 0, getWidth() - 2, getHeight(), + g.drawText (getButtonText(), getLocalBounds().reduced (1, 0), justification.getOnlyHorizontalFlags() | Justification::verticallyCentred, true); } diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp index 2a35cd4871..2cdf6c444d 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp @@ -1117,8 +1117,7 @@ void LookAndFeel::drawPopupMenuItem (Graphics& g, } g.drawFittedText (text, - leftBorder, 0, - width - (leftBorder + rightBorder), height, + leftBorder, 0, width - (leftBorder + rightBorder), height, Justification::centredLeft, 1); if (shortcutKeyText.isNotEmpty()) @@ -1129,10 +1128,7 @@ void LookAndFeel::drawPopupMenuItem (Graphics& g, g.setFont (f2); g.drawText (shortcutKeyText, - leftBorder, - 0, - width - (leftBorder + rightBorder + 4), - height, + leftBorder, 0, width - (leftBorder + rightBorder + 4), height, Justification::centredRight, true); } diff --git a/modules/juce_gui_basics/properties/juce_PropertyPanel.cpp b/modules/juce_gui_basics/properties/juce_PropertyPanel.cpp index 4632036139..c92a082a12 100644 --- a/modules/juce_gui_basics/properties/juce_PropertyPanel.cpp +++ b/modules/juce_gui_basics/properties/juce_PropertyPanel.cpp @@ -203,7 +203,7 @@ void PropertyPanel::paint (Graphics& g) { g.setColour (Colours::black.withAlpha (0.5f)); g.setFont (14.0f); - g.drawText (messageWhenEmpty, 0, 0, getWidth(), 30, + g.drawText (messageWhenEmpty, getLocalBounds().withHeight (30), Justification::centred, true); } } diff --git a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp index 94bd55bc79..a9cdbd7926 100644 --- a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp @@ -388,9 +388,7 @@ void ComboBox::paint (Graphics& g) { g.setColour (findColour (textColourId).withMultipliedAlpha (0.5f)); g.setFont (label->getFont()); - g.drawFittedText (textWhenNothingSelected, - label->getX() + 2, label->getY() + 1, - label->getWidth() - 4, label->getHeight() - 2, + g.drawFittedText (textWhenNothingSelected, label->getBounds().reduced (2, 1), label->getJustificationType(), jmax (1, (int) (label->getHeight() / label->getFont().getHeight()))); } diff --git a/modules/juce_gui_basics/widgets/juce_Slider.cpp b/modules/juce_gui_basics/widgets/juce_Slider.cpp index 6cdd8def95..31acb7db24 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.cpp +++ b/modules/juce_gui_basics/widgets/juce_Slider.cpp @@ -1229,7 +1229,7 @@ public: { g.setFont (font); g.setColour (findColour (TooltipWindow::textColourId, true)); - g.drawFittedText (text, 0, 0, w, h, Justification::centred, 1); + g.drawFittedText (text, Rectangle (w, h), Justification::centred, 1); } void getContentSize (int& w, int& h) diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp index c4b7aea2c1..3a95b2795b 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp @@ -1734,8 +1734,7 @@ void TextEditor::paintOverChildren (Graphics& g) if (isMultiLine()) { - g.drawText (textToShowWhenEmpty, - 0, 0, getWidth(), getHeight(), + g.drawText (textToShowWhenEmpty, getLocalBounds(), Justification::centred, true); } else diff --git a/modules/juce_gui_extra/misc/juce_ColourSelector.cpp b/modules/juce_gui_extra/misc/juce_ColourSelector.cpp index c297d78182..925c4176b8 100644 --- a/modules/juce_gui_extra/misc/juce_ColourSelector.cpp +++ b/modules/juce_gui_extra/misc/juce_ColourSelector.cpp @@ -215,8 +215,7 @@ public: void resized() { - marker.setBounds (0, roundToInt ((getHeight() - edge * 2) * h), - getWidth(), edge * 2); + marker.setBounds (0, roundToInt ((getHeight() - edge * 2) * h), getWidth(), edge * 2); } void mouseDown (const MouseEvent& e) @@ -350,8 +349,7 @@ ColourSelector::~ColourSelector() //============================================================================== Colour ColourSelector::getCurrentColour() const { - return ((flags & showAlphaChannel) != 0) ? colour - : colour.withAlpha ((uint8) 0xff); + return ((flags & showAlphaChannel) != 0) ? colour : colour.withAlpha ((uint8) 0xff); } void ColourSelector::setCurrentColour (const Colour& c) @@ -435,8 +433,7 @@ void ColourSelector::paint (Graphics& g) g.setColour (Colours::white.overlaidWith (currentColour).contrasting()); g.setFont (Font (14.0f, Font::bold)); g.drawText (currentColour.toDisplayString ((flags & showAlphaChannel) != 0), - previewArea.getX(), previewArea.getY(), previewArea.getWidth(), previewArea.getHeight(), - Justification::centred, false); + previewArea, Justification::centred, false); } if ((flags & showSliders) != 0)