Subclass CTextEdit using Enter key as Return

This commit is contained in:
redtide 2023-05-28 12:36:29 +02:00
parent 1bfcfd4b8c
commit 2752c8c1c8
No known key found for this signature in database
3 changed files with 49 additions and 2 deletions

View file

@ -134,7 +134,7 @@ struct Editor::Impl : EditorController::Receiver,
SValueMenu *scalaRootOctaveSlider_ = nullptr;
CTextLabel* scalaRootKeyLabel_ = nullptr;
SValueMenu* tuningFrequencyDropdown_ = nullptr;
CTextEdit* tuningFrequencyEdit_ = nullptr;
STextEdit* tuningFrequencyEdit_ = nullptr;
CTextLabel* tuningFrequencyLabel_ = nullptr;
CControl *stretchedTuningSlider_ = nullptr;
CTextLabel* stretchedTuningLabel_ = nullptr;
@ -1114,7 +1114,7 @@ void Editor::Impl::createFrameContents()
};
auto createTextEdit = [this, &palette] (const CRect& bounds, int tag, const char* label, CHoriTxtAlign align, int fontsize) {
auto* edit = new CTextEdit(bounds, this, tag, label, nullptr);
auto* edit = new STextEdit(bounds, this, tag, label, nullptr);
auto font = makeOwned<CFontDesc>("Roboto", fontsize);
edit->setFont(font);
edit->setHoriAlign(align);

View file

@ -1348,3 +1348,36 @@ void SPlaceHolder::draw(CDrawContext* dc)
dc->drawLine(bounds.getTopLeft(), bounds.getBottomRight());
dc->drawLine(bounds.getTopRight(), bounds.getBottomLeft());
}
STextEdit::STextEdit(
const CRect& size,
IControlListener* listener,
int32_t tag,
UTF8StringPtr txt,
CBitmap* background,
const int32_t style)
: CTextEdit(size, listener, tag, txt, background, style)
{
}
void STextEdit::onKeyboardEvent(KeyboardEvent &event)
{
if (!platformControl || event.type != EventType::KeyDown)
return;
if (event.virt == VirtualKey::Escape)
{
bWasReturnPressed = false;
platformControl->setText (text);
getFrame ()->setFocusView (nullptr);
looseFocus ();
event.consumed = true;
}
else if (event.virt == VirtualKey::Return || event.virt == VirtualKey::Enter)
{
bWasReturnPressed = true;
getFrame ()->setFocusView (nullptr);
looseFocus ();
event.consumed = true;
}
}

View file

@ -236,6 +236,20 @@ private:
bool inactive_ { false };
};
/// CTextEdit control that can use Enter key as Return
class STextEdit: public CTextEdit {
public:
STextEdit(
const CRect& size,
IControlListener* listener,
int32_t tag,
UTF8StringPtr txt = nullptr,
CBitmap* background = nullptr,
const int32_t style = 0
);
void onKeyboardEvent(KeyboardEvent& event) override;
};
///
class SStyledKnob : public CKnobBase {
public: