2018-10-13 23:46:34 +03:00
|
|
|
#include "TextBox.h"
|
|
|
|
#include "Painter.h"
|
|
|
|
#include "Font.h"
|
|
|
|
#include "CBitmap.h"
|
|
|
|
#include <AK/StdLib.h>
|
|
|
|
|
|
|
|
TextBox::TextBox(Widget* parent)
|
|
|
|
: Widget(parent)
|
|
|
|
{
|
|
|
|
startTimer(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
TextBox::~TextBox()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBox::setText(String&& text)
|
|
|
|
{
|
|
|
|
m_text = std::move(text);
|
|
|
|
m_cursorPosition = m_text.length();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-10-13 23:51:50 +03:00
|
|
|
void TextBox::paintEvent(PaintEvent&)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
Painter painter(*this);
|
|
|
|
|
|
|
|
// FIXME: Reduce overdraw.
|
|
|
|
painter.fillRect(rect(), backgroundColor());
|
|
|
|
painter.drawRect(rect(), foregroundColor());
|
|
|
|
|
|
|
|
if (isFocused())
|
|
|
|
painter.drawFocusRect(rect());
|
|
|
|
|
|
|
|
Rect innerRect = rect();
|
|
|
|
innerRect.shrink(6, 6);
|
|
|
|
|
2018-10-14 14:06:05 +03:00
|
|
|
unsigned maxCharsToPaint = innerRect.width() / font().glyphWidth();
|
2018-10-13 23:46:34 +03:00
|
|
|
|
|
|
|
int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0);
|
|
|
|
unsigned charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint);
|
|
|
|
|
2018-10-14 14:06:05 +03:00
|
|
|
int y = innerRect.center().y() - font().glyphHeight() / 2;
|
2018-10-13 23:46:34 +03:00
|
|
|
for (unsigned i = 0; i < charsToPaint; ++i) {
|
|
|
|
char ch = m_text[firstVisibleChar + i];
|
|
|
|
if (ch == ' ')
|
|
|
|
continue;
|
2018-10-14 14:06:05 +03:00
|
|
|
int x = innerRect.x() + (i * font().glyphWidth());
|
|
|
|
auto* bitmap = font().glyphBitmap(ch);
|
2018-10-13 23:46:34 +03:00
|
|
|
if (!bitmap) {
|
|
|
|
printf("TextBox: glyph missing: %02x\n", ch);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
painter.drawBitmap({x, y}, *bitmap, Color::Black);
|
|
|
|
}
|
|
|
|
|
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;
|
2018-10-14 14:06:05 +03:00
|
|
|
Rect cursorRect(innerRect.x() + visibleCursorPosition * font().glyphWidth(), innerRect.y(), 1, innerRect.height());
|
2018-10-13 23:46:34 +03:00
|
|
|
painter.fillRect(cursorRect, foregroundColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 23:51:50 +03:00
|
|
|
void TextBox::mouseDownEvent(MouseEvent&)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBox::handleBackspace()
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
|
|
|
|
m_text = std::move(newText);
|
|
|
|
--m_cursorPosition;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-10-13 23:51:50 +03:00
|
|
|
void TextBox::keyDownEvent(KeyEvent& event)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
switch (event.key()) {
|
|
|
|
case KeyboardKey::LeftArrow:
|
|
|
|
if (m_cursorPosition)
|
|
|
|
--m_cursorPosition;
|
|
|
|
m_cursorBlinkState = true;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
case KeyboardKey::RightArrow:
|
|
|
|
if (m_cursorPosition < m_text.length())
|
|
|
|
++m_cursorPosition;
|
|
|
|
m_cursorBlinkState = true;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
case KeyboardKey::Backspace:
|
|
|
|
return handleBackspace();
|
2018-10-14 00:19:44 +03:00
|
|
|
case KeyboardKey::Return:
|
|
|
|
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);
|
|
|
|
|
|
|
|
m_text = std::move(newText);
|
|
|
|
++m_cursorPosition;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 23:51:50 +03:00
|
|
|
void TextBox::timerEvent(TimerEvent&)
|
2018-10-13 23:46:34 +03:00
|
|
|
{
|
|
|
|
// FIXME: Disable the timer when not focused.
|
|
|
|
if (!isFocused())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_cursorBlinkState = !m_cursorBlinkState;
|
|
|
|
update();
|
|
|
|
}
|