ladybird/LibGUI/GTextBox.cpp
Andreas Kling a258d6507a mmap all the font files!
Font now uses the same in-memory format as the font files we have on disk.
This allows us to simply mmap() the font files and not use any additional
memory for them. Very cool! :^)

Hacking on this exposed a bug in file-backed VMObjects where the first client
to instantiate a VMObject for a specific inode also got to decide its size.
Since file-backed VMObjects always have the same size as the underlying file,
this made no sense, so I removed the ability to even set a size in that case.
2019-02-05 06:43:33 +01:00

150 lines
3.9 KiB
C++

#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)
{
set_background_color(Color::White);
}
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_glyph({x, y}, 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;
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();
}
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 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.
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();
}