GTextBox.cpp 3.7 KB

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