mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 02:38:11 +00:00
Start working on a simple TextBox widget.
This commit is contained in:
parent
6f1b384cde
commit
dfb70ed234
7 changed files with 201 additions and 2 deletions
|
@ -98,6 +98,15 @@ enum class MouseButton : byte {
|
||||||
Right,
|
Right,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum KeyboardKey {
|
||||||
|
Invalid,
|
||||||
|
LeftArrow,
|
||||||
|
RightArrow,
|
||||||
|
UpArrow,
|
||||||
|
DownArrow,
|
||||||
|
Backspace,
|
||||||
|
};
|
||||||
|
|
||||||
class KeyEvent final : public Event {
|
class KeyEvent final : public Event {
|
||||||
public:
|
public:
|
||||||
KeyEvent(Type type, int key)
|
KeyEvent(Type type, int key)
|
||||||
|
|
|
@ -32,6 +32,16 @@ static inline MouseButton toMouseButton(byte sdlButton)
|
||||||
void EventLoopSDL::handleKeyEvent(Event::Type type, const SDL_KeyboardEvent& sdlKey)
|
void EventLoopSDL::handleKeyEvent(Event::Type type, const SDL_KeyboardEvent& sdlKey)
|
||||||
{
|
{
|
||||||
auto keyEvent = make<KeyEvent>(type, 0);
|
auto keyEvent = make<KeyEvent>(type, 0);
|
||||||
|
int key = 0;
|
||||||
|
|
||||||
|
switch (sdlKey.keysym.sym) {
|
||||||
|
case SDLK_LEFT: key = KeyboardKey::LeftArrow; break;
|
||||||
|
case SDLK_RIGHT: key = KeyboardKey::RightArrow; break;
|
||||||
|
case SDLK_UP: key = KeyboardKey::UpArrow; break;
|
||||||
|
case SDLK_DOWN: key = KeyboardKey::DownArrow; break;
|
||||||
|
case SDLK_BACKSPACE: key = KeyboardKey::Backspace; break;
|
||||||
|
}
|
||||||
|
keyEvent->m_key = key;
|
||||||
|
|
||||||
if (sdlKey.keysym.sym > SDLK_UNKNOWN && sdlKey.keysym.sym <= 'z') {
|
if (sdlKey.keysym.sym > SDLK_UNKNOWN && sdlKey.keysym.sym <= 'z') {
|
||||||
char buf[] = { 0, 0 };
|
char buf[] = { 0, 0 };
|
||||||
|
@ -62,6 +72,7 @@ void EventLoopSDL::handleKeyEvent(Event::Type type, const SDL_KeyboardEvent& sdl
|
||||||
case ']': ch = '}'; break;
|
case ']': ch = '}'; break;
|
||||||
case '\\': ch = '|'; break;
|
case '\\': ch = '|'; break;
|
||||||
case '\'': ch = '"'; break;
|
case '\'': ch = '"'; break;
|
||||||
|
case ';': ch = ':'; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ VFS_OBJS = \
|
||||||
CBitmap.o \
|
CBitmap.o \
|
||||||
CheckBox.o \
|
CheckBox.o \
|
||||||
ListBox.o \
|
ListBox.o \
|
||||||
|
TextBox.o \
|
||||||
test.o
|
test.o
|
||||||
|
|
||||||
OBJS = $(AK_OBJS) $(VFS_OBJS)
|
OBJS = $(AK_OBJS) $(VFS_OBJS)
|
||||||
|
|
|
@ -40,6 +40,14 @@ public:
|
||||||
setHeight(height() + h);
|
setHeight(height() + h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void shrink(int w, int h)
|
||||||
|
{
|
||||||
|
setX(x() + w / 2);
|
||||||
|
setWidth(width() - w);
|
||||||
|
setY(y() + h / 2);
|
||||||
|
setHeight(height() - h);
|
||||||
|
}
|
||||||
|
|
||||||
bool contains(int x, int y) const
|
bool contains(int x, int y) const
|
||||||
{
|
{
|
||||||
return x >= m_location.x() && x < right() && y >= m_location.y() && y < bottom();
|
return x >= m_location.x() && x < right() && y >= m_location.y() && y < bottom();
|
||||||
|
|
137
Widgets/TextBox.cpp
Normal file
137
Widgets/TextBox.cpp
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
#include "TextBox.h"
|
||||||
|
#include "Painter.h"
|
||||||
|
#include "Font.h"
|
||||||
|
#include "CBitmap.h"
|
||||||
|
#include <AK/StdLib.h>
|
||||||
|
|
||||||
|
TextBox::TextBox(Widget* parent)
|
||||||
|
: Widget(parent)
|
||||||
|
{
|
||||||
|
startTimer(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
TextBox::~TextBox()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::setText(String&& text)
|
||||||
|
{
|
||||||
|
m_text = std::move(text);
|
||||||
|
m_cursorPosition = m_text.length();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::onPaint(PaintEvent&)
|
||||||
|
{
|
||||||
|
Painter painter(*this);
|
||||||
|
|
||||||
|
// FIXME: Reduce overdraw.
|
||||||
|
painter.fillRect(rect(), backgroundColor());
|
||||||
|
painter.drawRect(rect(), foregroundColor());
|
||||||
|
|
||||||
|
if (isFocused())
|
||||||
|
painter.drawFocusRect(rect());
|
||||||
|
|
||||||
|
Rect innerRect = rect();
|
||||||
|
innerRect.shrink(6, 6);
|
||||||
|
|
||||||
|
auto& font = Font::defaultFont();
|
||||||
|
|
||||||
|
unsigned maxCharsToPaint = innerRect.width() / font.glyphWidth();
|
||||||
|
|
||||||
|
int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0);
|
||||||
|
unsigned charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint);
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < charsToPaint; ++i) {
|
||||||
|
char ch = m_text[firstVisibleChar + i];
|
||||||
|
if (ch == ' ')
|
||||||
|
continue;
|
||||||
|
int x = innerRect.x() + (i * font.glyphWidth());
|
||||||
|
int y = innerRect.y();
|
||||||
|
auto* bitmap = font.glyphBitmap(ch);
|
||||||
|
if (!bitmap) {
|
||||||
|
printf("TextBox: glyph missing: %02x\n", ch);
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
painter.drawBitmap({x, y}, *bitmap, Color::Black);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_cursorBlinkState) {
|
||||||
|
unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar;
|
||||||
|
Rect cursorRect(innerRect.x() + visibleCursorPosition * font.glyphWidth(), innerRect.y(), 1, innerRect.height());
|
||||||
|
painter.fillRect(cursorRect, foregroundColor());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::onMouseDown(MouseEvent&)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::handleBackspace()
|
||||||
|
{
|
||||||
|
if (m_cursorPosition == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (m_text.length() == 1) {
|
||||||
|
m_text = String::empty();
|
||||||
|
m_cursorPosition = 0;
|
||||||
|
update();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* buffer;
|
||||||
|
auto newText = StringImpl::createUninitialized(m_text.length() - 1, buffer);
|
||||||
|
|
||||||
|
memcpy(buffer, m_text.characters(), m_cursorPosition - 1);
|
||||||
|
memcpy(buffer + m_cursorPosition - 1, m_text.characters() + m_cursorPosition, m_text.length() - (m_cursorPosition - 1));
|
||||||
|
|
||||||
|
m_text = std::move(newText);
|
||||||
|
--m_cursorPosition;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::onKeyDown(KeyEvent& event)
|
||||||
|
{
|
||||||
|
switch (event.key()) {
|
||||||
|
case KeyboardKey::LeftArrow:
|
||||||
|
if (m_cursorPosition)
|
||||||
|
--m_cursorPosition;
|
||||||
|
m_cursorBlinkState = true;
|
||||||
|
update();
|
||||||
|
return;
|
||||||
|
case KeyboardKey::RightArrow:
|
||||||
|
if (m_cursorPosition < m_text.length())
|
||||||
|
++m_cursorPosition;
|
||||||
|
m_cursorBlinkState = true;
|
||||||
|
update();
|
||||||
|
return;
|
||||||
|
case KeyboardKey::Backspace:
|
||||||
|
return handleBackspace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!event.text().isEmpty()) {
|
||||||
|
ASSERT(event.text().length() == 1);
|
||||||
|
|
||||||
|
char* buffer;
|
||||||
|
auto newText = StringImpl::createUninitialized(m_text.length() + 1, buffer);
|
||||||
|
|
||||||
|
memcpy(buffer, m_text.characters(), m_cursorPosition);
|
||||||
|
buffer[m_cursorPosition] = event.text()[0];
|
||||||
|
memcpy(buffer + m_cursorPosition + 1, m_text.characters() + m_cursorPosition, m_text.length() - m_cursorPosition);
|
||||||
|
|
||||||
|
m_text = std::move(newText);
|
||||||
|
++m_cursorPosition;
|
||||||
|
update();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::onTimer(TimerEvent&)
|
||||||
|
{
|
||||||
|
// FIXME: Disable the timer when not focused.
|
||||||
|
if (!isFocused())
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_cursorBlinkState = !m_cursorBlinkState;
|
||||||
|
update();
|
||||||
|
}
|
25
Widgets/TextBox.h
Normal file
25
Widgets/TextBox.h
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Widget.h"
|
||||||
|
|
||||||
|
class TextBox final : public Widget {
|
||||||
|
public:
|
||||||
|
explicit TextBox(Widget* parent = nullptr);
|
||||||
|
virtual ~TextBox() override;
|
||||||
|
|
||||||
|
String text() const { return m_text; }
|
||||||
|
void setText(String&&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual void onPaint(PaintEvent&) override;
|
||||||
|
virtual void onMouseDown(MouseEvent&) override;
|
||||||
|
virtual void onKeyDown(KeyEvent&) override;
|
||||||
|
virtual void onTimer(TimerEvent&) override;
|
||||||
|
|
||||||
|
void handleBackspace();
|
||||||
|
|
||||||
|
String m_text;
|
||||||
|
unsigned m_cursorPosition { 0 };
|
||||||
|
bool m_cursorBlinkState { false };
|
||||||
|
};
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include "ClockWidget.h"
|
#include "ClockWidget.h"
|
||||||
#include "CheckBox.h"
|
#include "CheckBox.h"
|
||||||
#include "ListBox.h"
|
#include "ListBox.h"
|
||||||
|
#include "TextBox.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
|
@ -48,7 +49,7 @@ int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
auto* widgetTestWindow = new Window;
|
auto* widgetTestWindow = new Window;
|
||||||
widgetTestWindow->setTitle("Widget test");
|
widgetTestWindow->setTitle("Widget test");
|
||||||
widgetTestWindow->setRect({ 20, 40, 100, 160 });
|
widgetTestWindow->setRect({ 20, 40, 100, 180 });
|
||||||
|
|
||||||
auto* widgetTestWindowWidget = new Widget;
|
auto* widgetTestWindowWidget = new Widget;
|
||||||
widgetTestWindowWidget->setWindowRelativeRect({ 0, 0, 100, 100 });
|
widgetTestWindowWidget->setWindowRelativeRect({ 0, 0, 100, 100 });
|
||||||
|
@ -67,11 +68,18 @@ int main(int argc, char** argv)
|
||||||
c->setCaption("CheckBox");
|
c->setCaption("CheckBox");
|
||||||
|
|
||||||
auto *lb = new ListBox(widgetTestWindowWidget);
|
auto *lb = new ListBox(widgetTestWindowWidget);
|
||||||
lb->setWindowRelativeRect({0, 60, 100, 100 });
|
lb->setWindowRelativeRect({ 0, 60, 100, 100 });
|
||||||
lb->addItem("This");
|
lb->addItem("This");
|
||||||
lb->addItem("is");
|
lb->addItem("is");
|
||||||
lb->addItem("a");
|
lb->addItem("a");
|
||||||
lb->addItem("ListBox");
|
lb->addItem("ListBox");
|
||||||
|
|
||||||
|
auto *tb = new TextBox(widgetTestWindowWidget);
|
||||||
|
tb->setWindowRelativeRect({ 0, 160, 100, 20 });
|
||||||
|
tb->setText("Hello!");
|
||||||
|
tb->setFocus(true);
|
||||||
|
|
||||||
|
WindowManager::the().setActiveWindow(widgetTestWindow);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto* win = new Window;
|
auto* win = new Window;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue