GTextBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. set_background_color(Color::White);
  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_glyph({x, y}, 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. if (on_change)
  58. on_change(*this);
  59. update();
  60. return;
  61. }
  62. char* buffer;
  63. auto new_text = StringImpl::create_uninitialized(m_text.length() - 1, buffer);
  64. memcpy(buffer, m_text.characters(), m_cursor_position - 1);
  65. memcpy(buffer + m_cursor_position - 1, m_text.characters() + m_cursor_position, m_text.length() - (m_cursor_position - 1));
  66. m_text = move(new_text);
  67. --m_cursor_position;
  68. if (on_change)
  69. on_change(*this);
  70. update();
  71. }
  72. void GTextBox::keydown_event(GKeyEvent& event)
  73. {
  74. switch (event.key()) {
  75. case KeyCode::Key_Left:
  76. if (m_cursor_position)
  77. --m_cursor_position;
  78. m_cursor_blink_state = true;
  79. update();
  80. return;
  81. case KeyCode::Key_Right:
  82. if (m_cursor_position < m_text.length())
  83. ++m_cursor_position;
  84. m_cursor_blink_state = true;
  85. update();
  86. return;
  87. case KeyCode::Key_Backspace:
  88. return handle_backspace();
  89. case KeyCode::Key_Return:
  90. if (on_return_pressed)
  91. on_return_pressed(*this);
  92. return;
  93. }
  94. if (!event.text().is_empty()) {
  95. ASSERT(event.text().length() == 1);
  96. char* buffer;
  97. auto new_text = StringImpl::create_uninitialized(m_text.length() + 1, buffer);
  98. memcpy(buffer, m_text.characters(), m_cursor_position);
  99. buffer[m_cursor_position] = event.text()[0];
  100. memcpy(buffer + m_cursor_position + 1, m_text.characters() + m_cursor_position, m_text.length() - m_cursor_position);
  101. m_text = move(new_text);
  102. ++m_cursor_position;
  103. if (on_change)
  104. on_change(*this);
  105. update();
  106. return;
  107. }
  108. }
  109. void GTextBox::timer_event(GTimerEvent&)
  110. {
  111. // FIXME: Disable the timer when not focused.
  112. if (!is_focused())
  113. return;
  114. m_cursor_blink_state = !m_cursor_blink_state;
  115. update();
  116. }
  117. void GTextBox::focusin_event(GEvent&)
  118. {
  119. start_timer(500);
  120. }
  121. void GTextBox::focusout_event(GEvent&)
  122. {
  123. stop_timer();
  124. }