1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Added some new versions of Graphics::drawText and drawFittedText that take Rectangle parameters.

This commit is contained in:
jules 2012-07-14 11:54:29 +01:00
parent 006e324114
commit dc9e0cb9bb
16 changed files with 87 additions and 56 deletions

View file

@ -96,9 +96,7 @@ void JucerTreeViewBase::paintContent (Graphics& g, const Rectangle<int>& 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()

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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);
}
//==============================================================================

View file

@ -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()

View file

@ -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);
}

View file

@ -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);

View file

@ -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<int>& area,
const Justification& justificationType,
const bool useEllipsesIfTooBig) const
{
if (text.isNotEmpty() && context.clipRegionIntersects (Rectangle<int> (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<int> (x, y, width, height), justificationType, useEllipsesIfTooBig);
}
void Graphics::drawFittedText (const String& text, const Rectangle<int>& area,
const Justification& justification,
const int maximumNumberOfLines,
const float minimumHorizontalScale) const
{
if (text.isNotEmpty()
&& width > 0 && height > 0
&& context.clipRegionIntersects (Rectangle<int> (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<int> (x, y, width, height),
justification, maximumNumberOfLines, minimumHorizontalScale);
}
//==============================================================================
void Graphics::fillRect (int x, int y, int width, int height) const
{

View file

@ -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<int>& 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<int>& area,
const Justification& justificationFlags,
int maximumNumberOfLines,
float minimumHorizontalScale = 0.7f) const;
//==============================================================================
/** Fills the context's entire clip region with the current colour or brush.

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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())));
}

View file

@ -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<int> (w, h), Justification::centred, 1);
}
void getContentSize (int& w, int& h)

View file

@ -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

View file

@ -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)