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>
|
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
|
|
|
{
|
|
|
|
startTimer(500);
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
GTextBox::~GTextBox()
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
void GTextBox::setText(String&& text)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
2019-01-11 06:10:07 +03:00
|
|
|
m_text = move(text);
|
2018-10-13 23:46:34 +03:00
|
|
|
m_cursorPosition = m_text.length();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
void GTextBox::paintEvent(GPaintEvent&)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
Painter painter(*this);
|
|
|
|
|
|
|
|
// FIXME: Reduce overdraw.
|
2019-01-12 19:02:54 +03:00
|
|
|
painter.fill_rect(rect(), backgroundColor());
|
|
|
|
painter.draw_rect(rect(), foregroundColor());
|
2018-10-13 23:46:34 +03:00
|
|
|
|
|
|
|
if (isFocused())
|
2019-01-12 19:02:54 +03:00
|
|
|
painter.draw_focus_rect(rect());
|
2018-10-13 23:46:34 +03:00
|
|
|
|
|
|
|
Rect innerRect = rect();
|
|
|
|
innerRect.shrink(6, 6);
|
|
|
|
|
2019-01-16 19:54:06 +03:00
|
|
|
size_t maxCharsToPaint = innerRect.width() / font().glyph_width();
|
2018-10-13 23:46:34 +03:00
|
|
|
|
|
|
|
int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0);
|
2019-01-09 07:38:13 +03:00
|
|
|
size_t charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint);
|
2018-10-13 23:46:34 +03:00
|
|
|
|
2019-01-16 19:54:06 +03:00
|
|
|
int y = innerRect.center().y() - font().glyph_height() / 2;
|
2019-01-09 07:38:13 +03:00
|
|
|
for (size_t i = 0; i < charsToPaint; ++i) {
|
2018-10-13 23:46:34 +03:00
|
|
|
char ch = m_text[firstVisibleChar + i];
|
|
|
|
if (ch == ' ')
|
|
|
|
continue;
|
2019-01-16 19:54:06 +03:00
|
|
|
int x = innerRect.x() + (i * font().glyph_width());
|
|
|
|
auto* bitmap = font().glyph_bitmap(ch);
|
2018-10-13 23:46:34 +03:00
|
|
|
if (!bitmap) {
|
2019-01-20 06:49:48 +03:00
|
|
|
dbgprintf("GTextBox: glyph missing: %02x\n", ch);
|
2018-10-13 23:46:34 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-01-12 19:02:54 +03:00
|
|
|
painter.draw_bitmap({x, y}, *bitmap, Color::Black);
|
2018-10-13 23:46:34 +03:00
|
|
|
}
|
|
|
|
|
2018-10-14 00:19:44 +03:00
|
|
|
if (isFocused() && m_cursorBlinkState) {
|
2018-10-13 23:46:34 +03:00
|
|
|
unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar;
|
2019-01-16 19:54:06 +03:00
|
|
|
Rect cursorRect(innerRect.x() + visibleCursorPosition * font().glyph_width(), innerRect.y(), 1, innerRect.height());
|
2019-01-12 19:02:54 +03:00
|
|
|
painter.fill_rect(cursorRect, foregroundColor());
|
2018-10-13 23:46:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
void GTextBox::mouseDownEvent(GMouseEvent&)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
void GTextBox::handleBackspace()
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
if (m_cursorPosition == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_text.length() == 1) {
|
|
|
|
m_text = String::empty();
|
|
|
|
m_cursorPosition = 0;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* buffer;
|
2018-12-21 04:10:45 +03:00
|
|
|
auto newText = StringImpl::create_uninitialized(m_text.length() - 1, buffer);
|
2018-10-13 23:46:34 +03:00
|
|
|
|
|
|
|
memcpy(buffer, m_text.characters(), m_cursorPosition - 1);
|
|
|
|
memcpy(buffer + m_cursorPosition - 1, m_text.characters() + m_cursorPosition, m_text.length() - (m_cursorPosition - 1));
|
|
|
|
|
2019-01-11 06:10:07 +03:00
|
|
|
m_text = move(newText);
|
2018-10-13 23:46:34 +03:00
|
|
|
--m_cursorPosition;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
void GTextBox::keyDownEvent(GKeyEvent& event)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
switch (event.key()) {
|
2019-01-20 06:49:48 +03:00
|
|
|
case GKeyboardKey::LeftArrow:
|
2018-10-13 23:46:34 +03:00
|
|
|
if (m_cursorPosition)
|
|
|
|
--m_cursorPosition;
|
|
|
|
m_cursorBlinkState = true;
|
|
|
|
update();
|
|
|
|
return;
|
2019-01-20 06:49:48 +03:00
|
|
|
case GKeyboardKey::RightArrow:
|
2018-10-13 23:46:34 +03:00
|
|
|
if (m_cursorPosition < m_text.length())
|
|
|
|
++m_cursorPosition;
|
|
|
|
m_cursorBlinkState = true;
|
|
|
|
update();
|
|
|
|
return;
|
2019-01-20 06:49:48 +03:00
|
|
|
case GKeyboardKey::Backspace:
|
2018-10-13 23:46:34 +03:00
|
|
|
return handleBackspace();
|
2019-01-20 06:49:48 +03:00
|
|
|
case GKeyboardKey::Return:
|
2018-10-14 00:19:44 +03:00
|
|
|
if (onReturnPressed)
|
|
|
|
onReturnPressed(*this);
|
|
|
|
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;
|
2018-12-21 04:10:45 +03:00
|
|
|
auto newText = StringImpl::create_uninitialized(m_text.length() + 1, buffer);
|
2018-10-13 23:46:34 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-01-11 06:10:07 +03:00
|
|
|
m_text = move(newText);
|
2018-10-13 23:46:34 +03:00
|
|
|
++m_cursorPosition;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
void GTextBox::timerEvent(GTimerEvent&)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
// FIXME: Disable the timer when not focused.
|
|
|
|
if (!isFocused())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_cursorBlinkState = !m_cursorBlinkState;
|
|
|
|
update();
|
|
|
|
}
|