Add TextBox::onReturnPressed.

This commit is contained in:
Andreas Kling 2018-10-13 23:19:44 +02:00
parent 7a0a7abc52
commit d305c316e1
Notes: sideshowbarker 2024-07-19 18:48:43 +09:00
5 changed files with 14 additions and 1 deletions

View file

@ -105,6 +105,7 @@ enum KeyboardKey {
UpArrow,
DownArrow,
Backspace,
Return,
};
class KeyEvent final : public Event {

View file

@ -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;

View file

@ -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()) {

View file

@ -1,6 +1,7 @@
#pragma once
#include "Widget.h"
#include <functional>
class TextBox final : public Widget {
public:
@ -10,6 +11,8 @@ public:
String text() const { return m_text; }
void setText(String&&);
std::function<void(TextBox&)> onReturnPressed;
private:
virtual void paintEvent(PaintEvent&) override;
virtual void mouseDownEvent(MouseEvent&) override;

View file

@ -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);
}