InputBox.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/BoxLayout.h>
  8. #include <LibGUI/Button.h>
  9. #include <LibGUI/InputBox.h>
  10. #include <LibGUI/Label.h>
  11. #include <LibGUI/TextBox.h>
  12. #include <LibGfx/Font.h>
  13. namespace GUI {
  14. InputBox::InputBox(Window* parent_window, String& text_value, StringView const& prompt, StringView const& title, StringView const& placeholder, InputType input_type)
  15. : Dialog(parent_window)
  16. , m_text_value(text_value)
  17. , m_prompt(prompt)
  18. , m_placeholder(placeholder)
  19. {
  20. set_title(title);
  21. build(input_type);
  22. }
  23. InputBox::~InputBox()
  24. {
  25. }
  26. int InputBox::show(Window* parent_window, String& text_value, StringView const& prompt, StringView const& title, StringView const& placeholder, InputType input_type)
  27. {
  28. auto box = InputBox::construct(parent_window, text_value, prompt, title, placeholder, input_type);
  29. box->set_resizable(false);
  30. if (parent_window)
  31. box->set_icon(parent_window->icon());
  32. auto result = box->exec();
  33. text_value = box->text_value();
  34. return result;
  35. }
  36. void InputBox::build(InputType input_type)
  37. {
  38. auto& widget = set_main_widget<Widget>();
  39. int text_width = widget.font().width(m_prompt);
  40. int title_width = widget.font().width(title()) + 24 /* icon, plus a little padding -- not perfect */;
  41. int max_width = max(text_width, title_width);
  42. set_rect(x(), y(), max_width + 140, 62);
  43. widget.set_layout<VerticalBoxLayout>();
  44. widget.set_fill_with_background_color(true);
  45. widget.layout()->set_margins(6);
  46. widget.layout()->set_spacing(6);
  47. auto& label_editor_container = widget.add<Widget>();
  48. label_editor_container.set_layout<HorizontalBoxLayout>();
  49. auto& label = label_editor_container.add<Label>(m_prompt);
  50. label.set_fixed_size(text_width, 16);
  51. switch (input_type) {
  52. case InputType::Text:
  53. m_text_editor = label_editor_container.add<TextBox>();
  54. break;
  55. case InputType::Password:
  56. m_text_editor = label_editor_container.add<PasswordBox>();
  57. break;
  58. }
  59. m_text_editor->set_fixed_height(19);
  60. m_text_editor->set_text(m_text_value);
  61. if (!m_placeholder.is_null())
  62. m_text_editor->set_placeholder(m_placeholder);
  63. auto& button_container_outer = widget.add<Widget>();
  64. button_container_outer.set_fixed_height(20);
  65. button_container_outer.set_layout<VerticalBoxLayout>();
  66. auto& button_container_inner = button_container_outer.add<Widget>();
  67. button_container_inner.set_layout<HorizontalBoxLayout>();
  68. button_container_inner.layout()->set_spacing(6);
  69. button_container_inner.layout()->set_margins({ 4, 0, 4, 4 });
  70. button_container_inner.layout()->add_spacer();
  71. m_ok_button = button_container_inner.add<Button>();
  72. m_ok_button->set_fixed_height(20);
  73. m_ok_button->set_text("OK");
  74. m_ok_button->on_click = [this](auto) {
  75. dbgln("GUI::InputBox: OK button clicked");
  76. m_text_value = m_text_editor->text();
  77. done(ExecOK);
  78. };
  79. m_cancel_button = button_container_inner.add<Button>();
  80. m_cancel_button->set_fixed_height(20);
  81. m_cancel_button->set_text("Cancel");
  82. m_cancel_button->on_click = [this](auto) {
  83. dbgln("GUI::InputBox: Cancel button clicked");
  84. done(ExecCancel);
  85. };
  86. m_text_editor->on_return_pressed = [this] {
  87. m_ok_button->click();
  88. };
  89. m_text_editor->on_escape_pressed = [this] {
  90. m_cancel_button->click();
  91. };
  92. m_text_editor->set_focus(true);
  93. }
  94. }