TextBox.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/TextBox.h>
  7. REGISTER_WIDGET(GUI, TextBox)
  8. REGISTER_WIDGET(GUI, PasswordBox)
  9. namespace GUI {
  10. TextBox::TextBox()
  11. : TextEditor(TextEditor::SingleLine)
  12. {
  13. set_min_width(32);
  14. set_fixed_height(22);
  15. }
  16. TextBox::~TextBox()
  17. {
  18. }
  19. void TextBox::keydown_event(GUI::KeyEvent& event)
  20. {
  21. TextEditor::keydown_event(event);
  22. if (event.key() == Key_Up) {
  23. if (on_up_pressed)
  24. on_up_pressed();
  25. if (has_no_history() || !can_go_backwards_in_history())
  26. return;
  27. if (m_history_index >= static_cast<int>(m_history.size()))
  28. m_saved_input = text();
  29. m_history_index--;
  30. set_text(m_history[m_history_index]);
  31. } else if (event.key() == Key_Down) {
  32. if (on_down_pressed)
  33. on_down_pressed();
  34. if (has_no_history())
  35. return;
  36. if (can_go_forwards_in_history()) {
  37. m_history_index++;
  38. set_text(m_history[m_history_index]);
  39. } else if (m_history_index < static_cast<int>(m_history.size())) {
  40. m_history_index++;
  41. set_text(m_saved_input);
  42. }
  43. }
  44. }
  45. void TextBox::add_current_text_to_history()
  46. {
  47. if (!m_history_enabled)
  48. return;
  49. auto input = text();
  50. if (m_history.is_empty() || m_history.last() != input)
  51. add_input_to_history(input);
  52. m_history_index = static_cast<int>(m_history.size());
  53. m_saved_input = {};
  54. }
  55. void TextBox::add_input_to_history(String input)
  56. {
  57. m_history.append(move(input));
  58. m_history_index++;
  59. }
  60. PasswordBox::PasswordBox()
  61. : TextBox()
  62. {
  63. set_substitution_code_point('*');
  64. set_text_is_secret(true);
  65. }
  66. }