GTextBox.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_cursor_position = m_text.length();
  19. update();
  20. }
  21. void GTextBox::paint_event(GPaintEvent&)
  22. {
  23. Painter painter(*this);
  24. painter.fill_rect({ rect().x() + 1, rect().y() + 1, rect().width() - 2, rect().height() - 2 }, background_color());
  25. painter.draw_rect(rect(), foreground_color());
  26. if (is_focused())
  27. painter.draw_focus_rect(rect());
  28. Rect inner_rect = rect();
  29. inner_rect.shrink(6, 6);
  30. size_t max_chars_to_paint = inner_rect.width() / font().glyph_width();
  31. int first_visible_char = max((int)m_cursor_position - (int)max_chars_to_paint, 0);
  32. size_t chars_to_paint = min(m_text.length() - first_visible_char, max_chars_to_paint);
  33. int y = inner_rect.center().y() - font().glyph_height() / 2;
  34. for (size_t i = 0; i < chars_to_paint; ++i) {
  35. char ch = m_text[first_visible_char + i];
  36. if (ch == ' ')
  37. continue;
  38. int x = inner_rect.x() + (i * font().glyph_width());
  39. painter.draw_bitmap({x, y}, font().glyph_bitmap(ch), Color::Black);
  40. }
  41. if (is_focused() && m_cursor_blink_state) {
  42. unsigned visible_cursor_position = m_cursor_position - first_visible_char;
  43. Rect cursor_rect(inner_rect.x() + visible_cursor_position * font().glyph_width(), inner_rect.y(), 1, inner_rect.height());
  44. painter.fill_rect(cursor_rect, foreground_color());
  45. }
  46. }
  47. void GTextBox::mousedown_event(GMouseEvent&)
  48. {
  49. }
  50. void GTextBox::handle_backspace()
  51. {
  52. if (m_cursor_position == 0)
  53. return;
  54. if (m_text.length() == 1) {
  55. m_text = String::empty();
  56. m_cursor_position = 0;
  57. update();
  58. return;
  59. }
  60. char* buffer;
  61. auto newText = StringImpl::create_uninitialized(m_text.length() - 1, buffer);
  62. memcpy(buffer, m_text.characters(), m_cursor_position - 1);
  63. memcpy(buffer + m_cursor_position - 1, m_text.characters() + m_cursor_position, m_text.length() - (m_cursor_position - 1));
  64. m_text = move(newText);
  65. --m_cursor_position;
  66. update();
  67. }
  68. void GTextBox::keydown_event(GKeyEvent& event)
  69. {
  70. switch (event.key()) {
  71. case KeyCode::Key_Left:
  72. if (m_cursor_position)
  73. --m_cursor_position;
  74. m_cursor_blink_state = true;
  75. update();
  76. return;
  77. case KeyCode::Key_Right:
  78. if (m_cursor_position < m_text.length())
  79. ++m_cursor_position;
  80. m_cursor_blink_state = true;
  81. update();
  82. return;
  83. case KeyCode::Key_Backspace:
  84. return handle_backspace();
  85. case KeyCode::Key_Return:
  86. if (on_return_pressed)
  87. on_return_pressed(*this);
  88. return;
  89. }
  90. if (!event.text().is_empty()) {
  91. ASSERT(event.text().length() == 1);
  92. char* buffer;
  93. auto newText = StringImpl::create_uninitialized(m_text.length() + 1, buffer);
  94. memcpy(buffer, m_text.characters(), m_cursor_position);
  95. buffer[m_cursor_position] = event.text()[0];
  96. memcpy(buffer + m_cursor_position + 1, m_text.characters() + m_cursor_position, m_text.length() - m_cursor_position);
  97. m_text = move(newText);
  98. ++m_cursor_position;
  99. update();
  100. return;
  101. }
  102. }
  103. void GTextBox::timer_event(GTimerEvent&)
  104. {
  105. // FIXME: Disable the timer when not focused.
  106. if (!is_focused())
  107. return;
  108. m_cursor_blink_state = !m_cursor_blink_state;
  109. update();
  110. }