i am using the LengthAndCharacterRestriction class indirectly via setInputRestrictions() to filter my TextEditor.
PropertyTextEditor.cpp
/*
==============================================================================
PropertyTextEditor.cpp
Created: 19 Aug 2024 5:45:26pm
Author: Dirk Schiller
==============================================================================
*/
#include "PropertyTextEditor.h"
// JUCE Modules / juce_gui_basics / widgets / juce_TextEditor.cpp
// void TextEditor::drawContent (Graphics& g)
// ...
// if (! selection.isEmpty())
// ...
//
// // >> DSchiller - Rounded Selection + Little X-Padding
//
// int xPadding = 0;
// juce::Path path;
// path.addRoundedRectangle(boundingBox.toPath().getBounds().getX() - xPadding, boundingBox.toPath().getBounds().getY(), boundingBox.toPath().getBounds().getWidth() + 2 * xPadding, boundingBox.toPath().getBounds().getHeight(), 2);
//
// g.fillPath (path, transform);
//
// // << DSchiller - Rounded Selection
//
// // DSchiller - Original
// // g.fillPath (boundingBox.toPath(), transform);
// bool TextEditor::selectAll()
// ...
// // >> DSchiller - Change caret position
// moveCaretTo (0, false);
// moveCaretTo (getTotalNumChars(), true);
// // << DSchiller - Change caret position
//
// // DSchiller - Original
// // moveCaretTo (getTotalNumChars(), false);
// // moveCaretTo (0, true);
PropertyTextEditor::PropertyTextEditor(const juce::String& name) {
setBounds(0, 0, 104, 31);
auto typeface = juce::Typeface::createSystemTypefaceFor(Assets::RobotoMonoRegular_ttf, Assets::RobotoMonoRegular_ttfSize);
juce::FontOptions options(typeface);
juce::Font font(options);
font.setHeight(21.f);
setFont(font);
setColour(textColourId, juce::Colour(0xfffff95e));
applyFontToAllText(font);
setColour(highlightColourId, juce::Colour(0xfffff95e));
setColour(highlightedTextColourId, juce::Colour(0xff111111));
setJustification(juce::Justification::topRight);
setCaretVisible(true);
int caretColourId = 0x1000204;
setColour(caretColourId, juce::Colour(0xff0084ff));
setInputRestrictions(8, "0123456789");
}
void PropertyTextEditor::mouseWheelMove(const juce::MouseEvent& event, const juce::MouseWheelDetails& wheel)
{
giveAwayKeyboardFocus();
auto currentText = getText();
int currentValue = currentText.getIntValue();
float scrollAmount = wheel.deltaY;
int increment = 1;
if (event.mods.isCtrlDown())
{
increment = 100;
}
else if (event.mods.isShiftDown())
{
increment = 10;
}
if (scrollAmount > 0)
{
currentValue -= increment * fmax(abs(scrollAmount) * 10, 1);
}
else if (scrollAmount < 0)
{
currentValue += increment * fmax(abs(scrollAmount) * 10, 1);
}
if (currentValue < 0) {
currentValue = 0;
}
setText(juce::String(currentValue), juce::dontSendNotification);
}
void PropertyTextEditor::paint(juce::Graphics& g) {
g.setColour(juce::Colour(0xff444444));
g.fillRoundedRectangle(0, 0, getWidth(), getHeight(), 5);
}
void PropertyTextEditor::paintOverChildren(juce::Graphics& g) { }
PropertyTextEditor.h
/*
==============================================================================
PropertyTextEditor.h
Created: 19 Aug 2024 5:45:26pm
Author: Dirk Schiller
==============================================================================
*/
#pragma once
#include <JuceHeader.h>
class PropertyTextEditor : public juce::TextEditor
{
public:
PropertyTextEditor(const juce::String& name);
~PropertyTextEditor() override = default;
void mouseWheelMove(const juce::MouseEvent& event, const juce::MouseWheelDetails& wheel) override;
void paint(juce::Graphics& g) override;
void paintOverChildren(juce::Graphics& g) override;
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PropertyTextEditor)
};
When i enter text and select it being the caret at the left position of the text and i press any of the filtered keys eg. Spacebar then a) the text get’s removed and b) the caret doesn’t go back to the correct position.
i expect that when any of the filtered, not allowed keys is pressed, the selected text should not be removed.
Also when i start using setInputRestrictions i get a leak detection error when i quit the application:
*** Leaked objects detected: 1 instance(s) of class LengthAndCharacterRestriction
JUCE Assertion failure in juce_LeakedObjectDetector.h:104
Also when i reset the filter i still get leaks on quit:
PropertyTextEditor::~PropertyTextEditor() {
// Clear input restrictions to enforce cleanup
setInputRestrictions(0, {});
}


