InputBox.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, networkException <networkexception@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <LibGUI/Dialog.h>
  10. namespace GUI {
  11. enum class InputType {
  12. Text,
  13. NonemptyText,
  14. Password,
  15. Numeric
  16. };
  17. class InputBox : public Dialog {
  18. C_OBJECT_ABSTRACT(InputBox)
  19. public:
  20. virtual ~InputBox() override = default;
  21. static ExecResult show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type = InputType::Text, StringView placeholder = {}, RefPtr<Gfx::Bitmap const> icon = nullptr);
  22. static ErrorOr<ExecResult> try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type = InputType::Text, StringView placeholder = {}, RefPtr<Gfx::Bitmap const> icon = nullptr);
  23. static ErrorOr<NonnullRefPtr<InputBox>> create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type, RefPtr<Gfx::Bitmap const> icon = nullptr);
  24. static ErrorOr<ExecResult> show_numeric(Window* parent_window, int& value, int min, int max, StringView title, StringView prompt = {}, RefPtr<Gfx::Bitmap const> icon = nullptr);
  25. static ErrorOr<NonnullRefPtr<InputBox>> create_numeric(Window* parent_window, int value, StringView title, StringView prompt = {}, RefPtr<Gfx::Bitmap const> icon = nullptr);
  26. String const& text_value() const { return m_text_value; }
  27. void set_text_value(String);
  28. int numeric_value() const { return m_numeric_value; }
  29. void set_numeric_value(int);
  30. void set_placeholder(StringView);
  31. void set_range(int min, int max);
  32. private:
  33. InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type, RefPtr<Gfx::Bitmap const> icon);
  34. InputBox(Window* parent_window, int value, String title, String prompt, RefPtr<Gfx::Bitmap const> icon);
  35. virtual void on_done(ExecResult) override;
  36. ErrorOr<void> build();
  37. int m_numeric_value { 0 };
  38. String m_text_value;
  39. String m_prompt;
  40. InputType m_input_type;
  41. RefPtr<Button> m_ok_button;
  42. RefPtr<Button> m_cancel_button;
  43. RefPtr<TextEditor> m_text_editor;
  44. RefPtr<SpinBox> m_spinbox;
  45. RefPtr<Label> m_prompt_label;
  46. RefPtr<Widget> m_label_container;
  47. RefPtr<Gfx::Bitmap const> m_icon;
  48. };
  49. }