GTextBox.cpp 4.1 KB

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