ladybird/LibGUI/GTextBox.cpp

150 lines
3.9 KiB
C++
Raw Normal View History

#include "GTextBox.h"
#include <AK/StdLibExtras.h>
2019-01-20 01:49:56 +03:00
#include <SharedGraphics/CharacterBitmap.h>
#include <SharedGraphics/Font.h>
#include <SharedGraphics/Painter.h>
#include <Kernel/KeyCode.h>
GTextBox::GTextBox(GWidget* parent)
: GWidget(parent)
{
}
GTextBox::~GTextBox()
{
}
2019-01-21 02:46:08 +03:00
void GTextBox::set_text(String&& text)
{
m_text = move(text);
m_cursor_position = m_text.length();
update();
}
2019-01-21 02:46:08 +03:00
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());
2019-01-21 02:46:08 +03:00
painter.draw_rect(rect(), foreground_color());
2019-01-21 02:46:08 +03:00
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());
}
}
2019-01-21 02:46:08 +03:00
void GTextBox::mousedown_event(GMouseEvent&)
{
}
2019-01-21 02:46:08 +03:00
void GTextBox::handle_backspace()
{
if (m_cursor_position == 0)
return;
if (m_text.length() == 1) {
m_text = String::empty();
m_cursor_position = 0;
if (on_change)
on_change(*this);
update();
return;
}
char* buffer;
auto new_text = 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(new_text);
--m_cursor_position;
if (on_change)
on_change(*this);
update();
}
2019-01-21 02:46:08 +03:00
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:
2019-01-21 02:46:08 +03:00
return handle_backspace();
case KeyCode::Key_Return:
if (on_return_pressed)
on_return_pressed(*this);
2018-10-14 00:19:44 +03:00
return;
}
2018-12-21 04:10:45 +03:00
if (!event.text().is_empty()) {
ASSERT(event.text().length() == 1);
char* buffer;
auto new_text = 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(new_text);
++m_cursor_position;
if (on_change)
on_change(*this);
update();
return;
}
}
void GTextBox::timer_event(GTimerEvent&)
{
// FIXME: Disable the timer when not focused.
2019-01-21 02:46:08 +03:00
if (!is_focused())
return;
m_cursor_blink_state = !m_cursor_blink_state;
update();
}
void GTextBox::focusin_event(GEvent&)
{
start_timer(500);
}
void GTextBox::focusout_event(GEvent&)
{
stop_timer();
}