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
|
|
|
{
|
|
|
|
startTimer(500);
|
|
|
|
}
|
|
|
|
|
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);
|
2018-10-13 23:46:34 +03:00
|
|
|
m_cursorPosition = m_text.length();
|
|
|
|
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);
|
|
|
|
|
|
|
|
// FIXME: Reduce overdraw.
|
2019-01-21 02:46:08 +03:00
|
|
|
painter.fill_rect(rect(), background_color());
|
|
|
|
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
|
|
|
|
|
|
|
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());
|
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-21 02:46:08 +03:00
|
|
|
if (is_focused() && 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-21 02:46:08 +03:00
|
|
|
painter.fill_rect(cursorRect, 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
|
|
|
{
|
|
|
|
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-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:
|
2018-10-13 23:46:34 +03:00
|
|
|
if (m_cursorPosition)
|
|
|
|
--m_cursorPosition;
|
|
|
|
m_cursorBlinkState = true;
|
|
|
|
update();
|
|
|
|
return;
|
2019-01-26 08:39:13 +03:00
|
|
|
case KeyCode::Key_Right:
|
2018-10-13 23:46:34 +03:00
|
|
|
if (m_cursorPosition < m_text.length())
|
|
|
|
++m_cursorPosition;
|
|
|
|
m_cursorBlinkState = true;
|
|
|
|
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;
|
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.
|
2019-01-21 02:46:08 +03:00
|
|
|
if (!is_focused())
|
2018-10-13 23:46:34 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_cursorBlinkState = !m_cursorBlinkState;
|
|
|
|
update();
|
|
|
|
}
|