void TextEditor::paintOverChildren (Graphics& g)
{
if (textToShowWhenEmpty.isNotEmpty()
&& (! hasKeyboardFocus (false))
&& getTotalNumChars() == 0)
{
g.setColour (colourForTextWhenEmpty);
g.setFont (getFont());
if (isMultiLine())
g.drawText (textToShowWhenEmpty, getLocalBounds(),
Justification::centred, true);
else
g.drawText (textToShowWhenEmpty,
leftIndent, 0, viewport->getWidth() - leftIndent, getHeight(),
Justification::centredLeft, true);
}
getLookAndFeel().drawTextEditorOutline (g, getWidth(), getHeight(), *this);
}
I find it a bit odd that the textToShowWhenEmpty
ignores the justification set in setJustification
, what is the rationale behind that?
--- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp
+++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp
@@ -929,13 +929,16 @@ bool TextEditor::redo() { return undoOrRedo (false); }
//==============================================================================
void TextEditor::setMultiLine (const bool shouldBeMultiLine,
- const bool shouldWordWrap)
+ const bool shouldWordWrap,
+ const bool shouldUpdateEmptyTextJustification)
{
if (multiline != shouldBeMultiLine
|| wordWrap != (shouldWordWrap && shouldBeMultiLine))
{
multiline = shouldBeMultiLine;
wordWrap = shouldWordWrap && shouldBeMultiLine;
+ if (shouldUpdateEmptyTextJustification)
+ emptyTextJustification = Justification::centred;
viewport->setScrollBarsShown (scrollbarVisible && multiline,
scrollbarVisible && multiline);
@@ -1008,6 +1011,15 @@ void TextEditor::setJustification (Justification j)
}
}
+void TextEditor::setEmptyTextJustification (Justification j)
+{
+ if (emptyTextJustification != j)
+ {
+ emptyTextJustification = j;
+ resized();
+ }
+}
+
//==============================================================================
void TextEditor::setFont (const Font& newFont)
{
@@ -1650,11 +1662,11 @@ void TextEditor::paintOverChildren (Graphics& g)
if (isMultiLine())
g.drawText (textToShowWhenEmpty, getLocalBounds(),
- Justification::centred, true);
+ emptyTextJustification, true);
else
g.drawText (textToShowWhenEmpty,
leftIndent, 0, viewport->getWidth() - leftIndent, getHeight(),
- Justification::centredLeft, true);
+ emptyTextJustification, true);
}
getLookAndFeel().drawTextEditorOutline (g, getWidth(), getHeight(), *this);
--- a/modules/juce_gui_basics/widgets/juce_TextEditor.h
+++ b/modules/juce_gui_basics/widgets/juce_TextEditor.h
@@ -72,7 +72,8 @@ public:
@see isMultiLine, setReturnKeyStartsNewLine
*/
void setMultiLine (bool shouldBeMultiLine,
- bool shouldWordWrap = true);
+ bool shouldWordWrap = true,
+ bool shouldUpdateEmptyTextJustification = true);
/** Returns true if the editor is in multi-line mode. */
bool isMultiLine() const;
@@ -499,6 +500,9 @@ public:
/** Modifies the horizontal justification of the text within the editor window. */
void setJustification (Justification newJustification);
+ /** Modifies the horizontal justification of the empty text shown within the editor window. */
+ void setEmptyTextJustification (Justification newJustification);
+
/** Sets the line spacing of the TextEditor.
The default (and minimum) value is 1.0 and values > 1.0 will increase the line spacing as a
multiple of the line height e.g. for double-spacing call this method with an argument of 2.0.
@@ -714,6 +718,7 @@ private:
TextHolderComponent* textHolder;
BorderSize<int> borderSize { 1, 1, 1, 3 };
Justification justification { Justification::left };
+ Justification emptyTextJustification { Justification:: left };
bool readOnly = false;
bool caretVisible = true;
in case anyone else needs it, this patch adds the option to setEmptyTextJustification
and should not change any existing behaviour for legacy code.