InputBox.cpp 3.0 KB

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