GTextBox.cpp 4.0 KB

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