Skip to content

Instantly share code, notes, and snippets.

@adamski
Last active November 4, 2021 14:56
Show Gist options
  • Save adamski/eab3ce6d5991125cd6a9f61efc5028fd to your computer and use it in GitHub Desktop.
Save adamski/eab3ce6d5991125cd6a9f61efc5028fd to your computer and use it in GitHub Desktop.
JUCE Label with centred TextEditor (single line)
/*
==============================================================================
CenteredEditableLabel.h
Created: 4 May 2017 10:37:43am
Author: Adam Wilson
==============================================================================
*/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "CenteredTextEditor.h"
//==============================================================================
/*
*/
class CenteredEditableLabel : public Label
{
public:
CenteredEditableLabel (const String& componentName = String(),
const String& labelText = String()) : Label (componentName, labelText)
{
}
~CenteredEditableLabel()
{
}
protected:
static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
{
if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
ed.setColour (targetColourID, l.findColour (colourID));
}
TextEditor *createEditorComponent () override
{
CenteredTextEditor* const ed = new CenteredTextEditor (getName());
ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
copyAllExplicitColoursTo (*ed);
copyColourIfSpecified (*this, *ed, textWhenEditingColourId, TextEditor::textColourId);
copyColourIfSpecified (*this, *ed, backgroundWhenEditingColourId, TextEditor::backgroundColourId);
copyColourIfSpecified (*this, *ed, outlineWhenEditingColourId, TextEditor::focusedOutlineColourId);
return ed;
}
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CenteredEditableLabel)
};
/*
==============================================================================
CenteredTextEditor.h
Created: 4 May 2017 11:02:08am
Author: Adam Wilson
==============================================================================
*/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
//==============================================================================
/*
*/
class CenteredTextEditor : public TextEditor,
public ComponentListener,
public TextEditor::Listener,
public KeyListener
{
public:
CenteredTextEditor (const String& componentName = String()) : TextEditor (componentName)
{
addComponentListener (this);
addListener (this);
}
~CenteredTextEditor()
{
}
void setSizeAndPosition()
{
int textWidth = TextEditor::getTextWidth();
int textHeight = TextEditor::getTextHeight();
auto parentBounds = getParentComponent()->getBounds();
Rectangle<int> newBounds (parentBounds.getWidth()/2 - (textWidth/2),
0,
textWidth,
textHeight);
setBounds (newBounds);
}
void textEditorTextChanged (TextEditor &editor) override
{
setSizeAndPosition();
}
void componentMovedOrResized (Component &component, bool wasMoved, bool wasResized) override
{
setSizeAndPosition();
}
bool keyPressed (const KeyPress &key, Component *originatingComponent) override
{
setSizeAndPosition();
}
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CenteredTextEditor)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment