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. namespace GUI {
  9. TextBox::TextBox()
  10. : TextEditor(TextEditor::SingleLine)
  11. {
  12. set_min_width(32);
  13. set_fixed_height(22);
  14. }
  15. TextBox::~TextBox()
  16. {
  17. }
  18. void TextBox::keydown_event(GUI::KeyEvent& event)
  19. {
  20. TextEditor::keydown_event(event);
  21. if (event.key() == Key_Up) {
  22. if (on_up_pressed)
  23. on_up_pressed();
  24. if (has_no_history() || !can_go_backwards_in_history())
  25. return;
  26. if (m_history_index >= static_cast<int>(m_history.size()))
  27. m_saved_input = text();
  28. m_history_index--;
  29. set_text(m_history[m_history_index]);
  30. } else if (event.key() == Key_Down) {
  31. if (on_down_pressed)
  32. on_down_pressed();
  33. if (has_no_history())
  34. return;
  35. if (can_go_forwards_in_history()) {
  36. m_history_index++;
  37. set_text(m_history[m_history_index]);
  38. } else if (m_history_index < static_cast<int>(m_history.size())) {
  39. m_history_index++;
  40. set_text(m_saved_input);
  41. }
  42. }
  43. }
  44. void TextBox::add_current_text_to_history()
  45. {
  46. if (!m_history_enabled)
  47. return;
  48. auto input = text();
  49. if (m_history.is_empty() || m_history.last() != input)
  50. add_input_to_history(input);
  51. m_history_index = static_cast<int>(m_history.size());
  52. m_saved_input = {};
  53. }
  54. void TextBox::add_input_to_history(String input)
  55. {
  56. m_history.append(move(input));
  57. m_history_index++;
  58. }
  59. PasswordBox::PasswordBox()
  60. : TextBox()
  61. {
  62. set_substitution_code_point('*');
  63. undo_action().set_enabled(false);
  64. redo_action().set_enabled(false);
  65. }
  66. }