InputBox.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/BoxLayout.h>
  27. #include <LibGUI/Button.h>
  28. #include <LibGUI/InputBox.h>
  29. #include <LibGUI/Label.h>
  30. #include <LibGUI/TextBox.h>
  31. #include <LibGfx/Font.h>
  32. #include <stdio.h>
  33. namespace GUI {
  34. InputBox::InputBox(Window* parent_window, const StringView& prompt, const StringView& title)
  35. : Dialog(parent_window)
  36. , m_prompt(prompt)
  37. {
  38. set_title(title);
  39. build();
  40. }
  41. InputBox::~InputBox()
  42. {
  43. }
  44. int InputBox::show(String& text_value, Window* parent_window, const StringView& prompt, const StringView& title)
  45. {
  46. auto box = InputBox::construct(parent_window, prompt, title);
  47. box->set_resizable(false);
  48. if (parent_window)
  49. box->set_icon(parent_window->icon());
  50. auto result = box->exec();
  51. text_value = box->text_value();
  52. return result;
  53. }
  54. void InputBox::build()
  55. {
  56. auto& widget = set_main_widget<Widget>();
  57. int text_width = widget.font().width(m_prompt);
  58. int title_width = widget.font().width(title()) + 24 /* icon, plus a little padding -- not perfect */;
  59. int max_width = AK::max(text_width, title_width);
  60. set_rect(x(), y(), max_width + 140, 62);
  61. widget.set_layout<VerticalBoxLayout>();
  62. widget.set_fill_with_background_color(true);
  63. widget.layout()->set_margins({ 6, 6, 6, 6 });
  64. widget.layout()->set_spacing(6);
  65. auto& label_editor_container = widget.add<Widget>();
  66. label_editor_container.set_layout<HorizontalBoxLayout>();
  67. auto& label = label_editor_container.add<Label>(m_prompt);
  68. label.set_fixed_size(text_width, 16);
  69. m_text_editor = label_editor_container.add<TextBox>();
  70. m_text_editor->set_fixed_height(19);
  71. auto& button_container_outer = widget.add<Widget>();
  72. button_container_outer.set_fixed_height(20);
  73. button_container_outer.set_layout<VerticalBoxLayout>();
  74. auto& button_container_inner = button_container_outer.add<Widget>();
  75. button_container_inner.set_layout<HorizontalBoxLayout>();
  76. button_container_inner.layout()->set_spacing(6);
  77. button_container_inner.layout()->set_margins({ 4, 4, 0, 4 });
  78. button_container_inner.layout()->add_spacer();
  79. m_ok_button = button_container_inner.add<Button>();
  80. m_ok_button->set_fixed_height(20);
  81. m_ok_button->set_text("OK");
  82. m_ok_button->on_click = [this](auto) {
  83. dbgln("GUI::InputBox: OK button clicked");
  84. m_text_value = m_text_editor->text();
  85. done(ExecOK);
  86. };
  87. m_cancel_button = button_container_inner.add<Button>();
  88. m_cancel_button->set_fixed_height(20);
  89. m_cancel_button->set_text("Cancel");
  90. m_cancel_button->on_click = [this](auto) {
  91. dbgln("GUI::InputBox: Cancel button clicked");
  92. done(ExecCancel);
  93. };
  94. m_text_editor->on_return_pressed = [this] {
  95. m_ok_button->click();
  96. };
  97. m_text_editor->on_escape_pressed = [this] {
  98. m_cancel_button->click();
  99. };
  100. m_text_editor->set_focus(true);
  101. }
  102. }