GTextBox.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "GTextBox.h"
  2. #include <AK/StdLibExtras.h>
  3. #include <SharedGraphics/CharacterBitmap.h>
  4. #include <SharedGraphics/Font.h>
  5. #include <SharedGraphics/Painter.h>
  6. #include <Kernel/KeyCode.h>
  7. GTextBox::GTextBox(GWidget* parent)
  8. : GWidget(parent)
  9. {
  10. startTimer(500);
  11. }
  12. GTextBox::~GTextBox()
  13. {
  14. }
  15. void GTextBox::set_text(String&& text)
  16. {
  17. m_text = move(text);
  18. m_cursorPosition = m_text.length();
  19. update();
  20. }
  21. void GTextBox::paint_event(GPaintEvent&)
  22. {
  23. Painter painter(*this);
  24. // FIXME: Reduce overdraw.
  25. painter.fill_rect(rect(), background_color());
  26. painter.draw_rect(rect(), foreground_color());
  27. if (is_focused())
  28. painter.draw_focus_rect(rect());
  29. Rect innerRect = rect();
  30. innerRect.shrink(6, 6);
  31. size_t maxCharsToPaint = innerRect.width() / font().glyph_width();
  32. int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0);
  33. size_t charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint);
  34. int y = innerRect.center().y() - font().glyph_height() / 2;
  35. for (size_t i = 0; i < charsToPaint; ++i) {
  36. char ch = m_text[firstVisibleChar + i];
  37. if (ch == ' ')
  38. continue;
  39. int x = innerRect.x() + (i * font().glyph_width());
  40. painter.draw_bitmap({x, y}, font().glyph_bitmap(ch), Color::Black);
  41. }
  42. if (is_focused() && m_cursorBlinkState) {
  43. unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar;
  44. Rect cursorRect(innerRect.x() + visibleCursorPosition * font().glyph_width(), innerRect.y(), 1, innerRect.height());
  45. painter.fill_rect(cursorRect, foreground_color());
  46. }
  47. }
  48. void GTextBox::mousedown_event(GMouseEvent&)
  49. {
  50. }
  51. void GTextBox::handle_backspace()
  52. {
  53. if (m_cursorPosition == 0)
  54. return;
  55. if (m_text.length() == 1) {
  56. m_text = String::empty();
  57. m_cursorPosition = 0;
  58. update();
  59. return;
  60. }
  61. char* buffer;
  62. auto newText = StringImpl::create_uninitialized(m_text.length() - 1, buffer);
  63. memcpy(buffer, m_text.characters(), m_cursorPosition - 1);
  64. memcpy(buffer + m_cursorPosition - 1, m_text.characters() + m_cursorPosition, m_text.length() - (m_cursorPosition - 1));
  65. m_text = move(newText);
  66. --m_cursorPosition;
  67. update();
  68. }
  69. void GTextBox::keydown_event(GKeyEvent& event)
  70. {
  71. switch (event.key()) {
  72. case KeyCode::Key_Left:
  73. if (m_cursorPosition)
  74. --m_cursorPosition;
  75. m_cursorBlinkState = true;
  76. update();
  77. return;
  78. case KeyCode::Key_Right:
  79. if (m_cursorPosition < m_text.length())
  80. ++m_cursorPosition;
  81. m_cursorBlinkState = true;
  82. update();
  83. return;
  84. case KeyCode::Key_Backspace:
  85. return handle_backspace();
  86. case KeyCode::Key_Return:
  87. if (on_return_pressed)
  88. on_return_pressed(*this);
  89. return;
  90. }
  91. if (!event.text().is_empty()) {
  92. ASSERT(event.text().length() == 1);
  93. char* buffer;
  94. auto newText = StringImpl::create_uninitialized(m_text.length() + 1, buffer);
  95. memcpy(buffer, m_text.characters(), m_cursorPosition);
  96. buffer[m_cursorPosition] = event.text()[0];
  97. memcpy(buffer + m_cursorPosition + 1, m_text.characters() + m_cursorPosition, m_text.length() - m_cursorPosition);
  98. m_text = move(newText);
  99. ++m_cursorPosition;
  100. update();
  101. return;
  102. }
  103. }
  104. void GTextBox::timerEvent(GTimerEvent&)
  105. {
  106. // FIXME: Disable the timer when not focused.
  107. if (!is_focused())
  108. return;
  109. m_cursorBlinkState = !m_cursorBlinkState;
  110. update();
  111. }