TextBox.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/TextEditor.h>
  10. namespace GUI {
  11. class TextBox : public TextEditor {
  12. C_OBJECT(TextBox)
  13. public:
  14. virtual ~TextBox() override = default;
  15. Function<void()> on_up_pressed;
  16. Function<void()> on_down_pressed;
  17. void set_history_enabled(bool enabled) { m_history_enabled = enabled; }
  18. void add_current_text_to_history();
  19. protected:
  20. TextBox();
  21. private:
  22. virtual void keydown_event(GUI::KeyEvent&) override;
  23. bool has_no_history() const { return !m_history_enabled || m_history.is_empty(); }
  24. bool can_go_backwards_in_history() const { return m_history_index > 0; }
  25. bool can_go_forwards_in_history() const { return m_history_index < static_cast<int>(m_history.size()) - 1; }
  26. void add_input_to_history(String);
  27. bool m_history_enabled { false };
  28. Vector<String> m_history;
  29. int m_history_index { -1 };
  30. String m_saved_input;
  31. };
  32. class PasswordBox : public TextBox {
  33. C_OBJECT(PasswordBox)
  34. private:
  35. PasswordBox();
  36. };
  37. class UrlBox : public TextBox {
  38. C_OBJECT(UrlBox)
  39. public:
  40. virtual ~UrlBox() override = default;
  41. void set_focus_transition(bool focus_transition) { m_focus_transition = focus_transition; }
  42. bool is_focus_transition() const { return m_focus_transition; }
  43. private:
  44. UrlBox();
  45. virtual void mousedown_event(GUI::MouseEvent&) override;
  46. virtual void focusout_event(GUI::FocusEvent&) override;
  47. bool m_focus_transition { true };
  48. };
  49. }