1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#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::paintEvent(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);
unsigned maxCharsToPaint = innerRect.width() / font().glyphWidth();
int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0);
unsigned charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint);
int y = innerRect.center().y() - font().glyphHeight() / 2;
for (unsigned i = 0; i < charsToPaint; ++i) {
char ch = m_text[firstVisibleChar + i];
if (ch == ' ')
continue;
int x = innerRect.x() + (i * font().glyphWidth());
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 (isFocused() && 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::mouseDownEvent(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::keyDownEvent(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();
case KeyboardKey::Return:
if (onReturnPressed)
onReturnPressed(*this);
return;
}
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::timerEvent(TimerEvent&)
{
// FIXME: Disable the timer when not focused.
if (!isFocused())
return;
m_cursorBlinkState = !m_cursorBlinkState;
update();
}
|