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
|
#include "GTextBox.h"
#include <AK/StdLibExtras.h>
#include <SharedGraphics/CharacterBitmap.h>
#include <SharedGraphics/Font.h>
#include <SharedGraphics/Painter.h>
#include <Kernel/KeyCode.h>
GTextBox::GTextBox(GWidget* parent)
: GWidget(parent)
{
startTimer(500);
}
GTextBox::~GTextBox()
{
}
void GTextBox::set_text(String&& text)
{
m_text = move(text);
m_cursor_position = m_text.length();
update();
}
void GTextBox::paint_event(GPaintEvent&)
{
Painter painter(*this);
painter.fill_rect({ rect().x() + 1, rect().y() + 1, rect().width() - 2, rect().height() - 2 }, background_color());
painter.draw_rect(rect(), foreground_color());
if (is_focused())
painter.draw_focus_rect(rect());
Rect inner_rect = rect();
inner_rect.shrink(6, 6);
size_t max_chars_to_paint = inner_rect.width() / font().glyph_width();
int first_visible_char = max((int)m_cursor_position - (int)max_chars_to_paint, 0);
size_t chars_to_paint = min(m_text.length() - first_visible_char, max_chars_to_paint);
int y = inner_rect.center().y() - font().glyph_height() / 2;
for (size_t i = 0; i < chars_to_paint; ++i) {
char ch = m_text[first_visible_char + i];
if (ch == ' ')
continue;
int x = inner_rect.x() + (i * font().glyph_width());
painter.draw_bitmap({x, y}, font().glyph_bitmap(ch), Color::Black);
}
if (is_focused() && m_cursor_blink_state) {
unsigned visible_cursor_position = m_cursor_position - first_visible_char;
Rect cursor_rect(inner_rect.x() + visible_cursor_position * font().glyph_width(), inner_rect.y(), 1, inner_rect.height());
painter.fill_rect(cursor_rect, foreground_color());
}
}
void GTextBox::mousedown_event(GMouseEvent&)
{
}
void GTextBox::handle_backspace()
{
if (m_cursor_position == 0)
return;
if (m_text.length() == 1) {
m_text = String::empty();
m_cursor_position = 0;
update();
return;
}
char* buffer;
auto newText = StringImpl::create_uninitialized(m_text.length() - 1, buffer);
memcpy(buffer, m_text.characters(), m_cursor_position - 1);
memcpy(buffer + m_cursor_position - 1, m_text.characters() + m_cursor_position, m_text.length() - (m_cursor_position - 1));
m_text = move(newText);
--m_cursor_position;
update();
}
void GTextBox::keydown_event(GKeyEvent& event)
{
switch (event.key()) {
case KeyCode::Key_Left:
if (m_cursor_position)
--m_cursor_position;
m_cursor_blink_state = true;
update();
return;
case KeyCode::Key_Right:
if (m_cursor_position < m_text.length())
++m_cursor_position;
m_cursor_blink_state = true;
update();
return;
case KeyCode::Key_Backspace:
return handle_backspace();
case KeyCode::Key_Return:
if (on_return_pressed)
on_return_pressed(*this);
return;
}
if (!event.text().is_empty()) {
ASSERT(event.text().length() == 1);
char* buffer;
auto newText = StringImpl::create_uninitialized(m_text.length() + 1, buffer);
memcpy(buffer, m_text.characters(), m_cursor_position);
buffer[m_cursor_position] = event.text()[0];
memcpy(buffer + m_cursor_position + 1, m_text.characters() + m_cursor_position, m_text.length() - m_cursor_position);
m_text = move(newText);
++m_cursor_position;
update();
return;
}
}
void GTextBox::timer_event(GTimerEvent&)
{
// FIXME: Disable the timer when not focused.
if (!is_focused())
return;
m_cursor_blink_state = !m_cursor_blink_state;
update();
}
|