2019-01-20 06:49:48 +03:00
|
|
|
#include "GTextBox.h"
|
2018-12-21 04:18:16 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-01-20 01:49:56 +03:00
|
|
|
#include <SharedGraphics/CharacterBitmap.h>
|
|
|
|
#include <SharedGraphics/Font.h>
|
|
|
|
#include <SharedGraphics/Painter.h>
|
2019-01-26 08:39:13 +03:00
|
|
|
#include <Kernel/KeyCode.h>
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
GTextBox::GTextBox(GWidget* parent)
|
|
|
|
: GWidget(parent)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
GTextBox::~GTextBox()
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GTextBox::set_text(String&& text)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
2019-01-11 06:10:07 +03:00
|
|
|
m_text = move(text);
|
2019-01-27 17:12:33 +03:00
|
|
|
m_cursor_position = m_text.length();
|
2018-10-13 23:46:34 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GTextBox::paint_event(GPaintEvent&)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
Painter painter(*this);
|
|
|
|
|
2019-01-27 17:12:33 +03:00
|
|
|
painter.fill_rect({ rect().x() + 1, rect().y() + 1, rect().width() - 2, rect().height() - 2 }, background_color());
|
2019-01-21 02:46:08 +03:00
|
|
|
painter.draw_rect(rect(), foreground_color());
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
if (is_focused())
|
2019-01-12 19:02:54 +03:00
|
|
|
painter.draw_focus_rect(rect());
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-27 17:12:33 +03:00
|
|
|
Rect inner_rect = rect();
|
|
|
|
inner_rect.shrink(6, 6);
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-27 17:12:33 +03:00
|
|
|
size_t max_chars_to_paint = inner_rect.width() / font().glyph_width();
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-27 17:12:33 +03:00
|
|
|
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);
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-27 17:12:33 +03:00
|
|
|
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];
|
2018-10-13 23:46:34 +03:00
|
|
|
if (ch == ' ')
|
|
|
|
continue;
|
2019-01-27 17:12:33 +03:00
|
|
|
int x = inner_rect.x() + (i * font().glyph_width());
|
2019-01-27 07:14:15 +03:00
|
|
|
painter.draw_bitmap({x, y}, font().glyph_bitmap(ch), Color::Black);
|
2018-10-13 23:46:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-27 17:12:33 +03:00
|
|
|
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());
|
2018-10-13 23:46:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GTextBox::mousedown_event(GMouseEvent&)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GTextBox::handle_backspace()
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
2019-01-27 17:12:33 +03:00
|
|
|
if (m_cursor_position == 0)
|
2018-10-13 23:46:34 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_text.length() == 1) {
|
|
|
|
m_text = String::empty();
|
2019-01-27 17:12:33 +03:00
|
|
|
m_cursor_position = 0;
|
2019-02-03 05:06:58 +03:00
|
|
|
if (on_change)
|
|
|
|
on_change(*this);
|
2018-10-13 23:46:34 +03:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* buffer;
|
2019-01-31 19:31:23 +03:00
|
|
|
auto new_text = StringImpl::create_uninitialized(m_text.length() - 1, buffer);
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-27 17:12:33 +03:00
|
|
|
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));
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
m_text = move(new_text);
|
2019-01-27 17:12:33 +03:00
|
|
|
--m_cursor_position;
|
2019-02-03 05:06:58 +03:00
|
|
|
if (on_change)
|
|
|
|
on_change(*this);
|
2018-10-13 23:46:34 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GTextBox::keydown_event(GKeyEvent& event)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
switch (event.key()) {
|
2019-01-26 08:39:13 +03:00
|
|
|
case KeyCode::Key_Left:
|
2019-01-27 17:12:33 +03:00
|
|
|
if (m_cursor_position)
|
|
|
|
--m_cursor_position;
|
|
|
|
m_cursor_blink_state = true;
|
2018-10-13 23:46:34 +03:00
|
|
|
update();
|
|
|
|
return;
|
2019-01-26 08:39:13 +03:00
|
|
|
case KeyCode::Key_Right:
|
2019-01-27 17:12:33 +03:00
|
|
|
if (m_cursor_position < m_text.length())
|
|
|
|
++m_cursor_position;
|
|
|
|
m_cursor_blink_state = true;
|
2018-10-13 23:46:34 +03:00
|
|
|
update();
|
|
|
|
return;
|
2019-01-26 08:39:13 +03:00
|
|
|
case KeyCode::Key_Backspace:
|
2019-01-21 02:46:08 +03:00
|
|
|
return handle_backspace();
|
2019-01-26 08:39:13 +03:00
|
|
|
case KeyCode::Key_Return:
|
2019-01-26 13:24:16 +03:00
|
|
|
if (on_return_pressed)
|
|
|
|
on_return_pressed(*this);
|
2018-10-14 00:19:44 +03:00
|
|
|
return;
|
2018-10-13 23:46:34 +03:00
|
|
|
}
|
|
|
|
|
2018-12-21 04:10:45 +03:00
|
|
|
if (!event.text().is_empty()) {
|
2018-10-13 23:46:34 +03:00
|
|
|
ASSERT(event.text().length() == 1);
|
|
|
|
|
|
|
|
char* buffer;
|
2019-01-31 19:31:23 +03:00
|
|
|
auto new_text = StringImpl::create_uninitialized(m_text.length() + 1, buffer);
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-27 17:12:33 +03:00
|
|
|
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);
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
m_text = move(new_text);
|
2019-01-27 17:12:33 +03:00
|
|
|
++m_cursor_position;
|
2019-02-03 05:06:58 +03:00
|
|
|
if (on_change)
|
|
|
|
on_change(*this);
|
2018-10-13 23:46:34 +03:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:37:43 +03:00
|
|
|
void GTextBox::timer_event(GTimerEvent&)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
// FIXME: Disable the timer when not focused.
|
2019-01-21 02:46:08 +03:00
|
|
|
if (!is_focused())
|
2018-10-13 23:46:34 +03:00
|
|
|
return;
|
|
|
|
|
2019-01-27 17:12:33 +03:00
|
|
|
m_cursor_blink_state = !m_cursor_blink_state;
|
2018-10-13 23:46:34 +03:00
|
|
|
update();
|
|
|
|
}
|
2019-02-04 12:34:56 +03:00
|
|
|
|
|
|
|
void GTextBox::focusin_event(GEvent&)
|
|
|
|
{
|
|
|
|
start_timer(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextBox::focusout_event(GEvent&)
|
|
|
|
{
|
|
|
|
stop_timer();
|
|
|
|
}
|