From d305c316e11bece3b1eb623018d52bf9ba95daf9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 13 Oct 2018 23:19:44 +0200 Subject: [PATCH] Add TextBox::onReturnPressed. --- Widgets/Event.h | 1 + Widgets/EventLoopSDL.cpp | 1 + Widgets/TextBox.cpp | 6 +++++- Widgets/TextBox.h | 3 +++ Widgets/test.cpp | 4 ++++ 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Widgets/Event.h b/Widgets/Event.h index f1db082e8a3..ab63b0d0a91 100644 --- a/Widgets/Event.h +++ b/Widgets/Event.h @@ -105,6 +105,7 @@ enum KeyboardKey { UpArrow, DownArrow, Backspace, + Return, }; class KeyEvent final : public Event { diff --git a/Widgets/EventLoopSDL.cpp b/Widgets/EventLoopSDL.cpp index 4b5278e8685..5049d79e68b 100644 --- a/Widgets/EventLoopSDL.cpp +++ b/Widgets/EventLoopSDL.cpp @@ -40,6 +40,7 @@ void EventLoopSDL::handleKeyEvent(Event::Type type, const SDL_KeyboardEvent& sdl case SDLK_UP: key = KeyboardKey::UpArrow; break; case SDLK_DOWN: key = KeyboardKey::DownArrow; break; case SDLK_BACKSPACE: key = KeyboardKey::Backspace; break; + case SDLK_RETURN: key = KeyboardKey::Return; break; } keyEvent->m_key = key; diff --git a/Widgets/TextBox.cpp b/Widgets/TextBox.cpp index d0883ea2353..45089540444 100644 --- a/Widgets/TextBox.cpp +++ b/Widgets/TextBox.cpp @@ -56,7 +56,7 @@ void TextBox::paintEvent(PaintEvent&) painter.drawBitmap({x, y}, *bitmap, Color::Black); } - if (m_cursorBlinkState) { + if (isFocused() && m_cursorBlinkState) { unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar; Rect cursorRect(innerRect.x() + visibleCursorPosition * font.glyphWidth(), innerRect.y(), 1, innerRect.height()); painter.fillRect(cursorRect, foregroundColor()); @@ -107,6 +107,10 @@ void TextBox::keyDownEvent(KeyEvent& event) return; case KeyboardKey::Backspace: return handleBackspace(); + case KeyboardKey::Return: + if (onReturnPressed) + onReturnPressed(*this); + return; } if (!event.text().isEmpty()) { diff --git a/Widgets/TextBox.h b/Widgets/TextBox.h index 16769808827..626ea6d8df6 100644 --- a/Widgets/TextBox.h +++ b/Widgets/TextBox.h @@ -1,6 +1,7 @@ #pragma once #include "Widget.h" +#include class TextBox final : public Widget { public: @@ -10,6 +11,8 @@ public: String text() const { return m_text; } void setText(String&&); + std::function onReturnPressed; + private: virtual void paintEvent(PaintEvent&) override; virtual void mouseDownEvent(MouseEvent&) override; diff --git a/Widgets/test.cpp b/Widgets/test.cpp index 5e8f4037799..1c17229137c 100644 --- a/Widgets/test.cpp +++ b/Widgets/test.cpp @@ -83,6 +83,10 @@ int main(int argc, char** argv) tb->setText("Hello!"); tb->setFocus(true); + tb->onReturnPressed = [] (TextBox& textBox) { + printf("TextBox %p return pressed: '%s'\n", &textBox, textBox.text().characters()); + }; + WindowManager::the().setActiveWindow(widgetTestWindow); }