InputBox.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, String& text_value, const StringView& prompt, const StringView& title)
  35. : Dialog(parent_window)
  36. , m_text_value(text_value)
  37. , m_prompt(prompt)
  38. {
  39. set_title(title);
  40. build();
  41. }
  42. InputBox::~InputBox()
  43. {
  44. }
  45. int InputBox::show(Window* parent_window, String& text_value, const StringView& prompt, const StringView& title)
  46. {
  47. auto box = InputBox::construct(parent_window, text_value, prompt, title);
  48. box->set_resizable(false);
  49. if (parent_window)
  50. box->set_icon(parent_window->icon());
  51. auto result = box->exec();
  52. text_value = box->text_value();
  53. return result;
  54. }
  55. void InputBox::build()
  56. {
  57. auto& widget = set_main_widget<Widget>();
  58. int text_width = widget.font().width(m_prompt);
  59. int title_width = widget.font().width(title()) + 24 /* icon, plus a little padding -- not perfect */;
  60. int max_width = max(text_width, title_width);
  61. set_rect(x(), y(), max_width + 140, 62);
  62. widget.set_layout<VerticalBoxLayout>();
  63. widget.set_fill_with_background_color(true);
  64. widget.layout()->set_margins({ 6, 6, 6, 6 });
  65. widget.layout()->set_spacing(6);
  66. auto& label_editor_container = widget.add<Widget>();
  67. label_editor_container.set_layout<HorizontalBoxLayout>();
  68. auto& label = label_editor_container.add<Label>(m_prompt);
  69. label.set_fixed_size(text_width, 16);
  70. m_text_editor = label_editor_container.add<TextBox>();
  71. m_text_editor->set_fixed_height(19);
  72. m_text_editor->set_text(m_text_value);
  73. auto& button_container_outer = widget.add<Widget>();
  74. button_container_outer.set_fixed_height(20);
  75. button_container_outer.set_layout<VerticalBoxLayout>();
  76. auto& button_container_inner = button_container_outer.add<Widget>();
  77. button_container_inner.set_layout<HorizontalBoxLayout>();
  78. button_container_inner.layout()->set_spacing(6);
  79. button_container_inner.layout()->set_margins({ 4, 4, 0, 4 });
  80. button_container_inner.layout()->add_spacer();
  81. m_ok_button = button_container_inner.add<Button>();
  82. m_ok_button->set_fixed_height(20);
  83. m_ok_button->set_text("OK");
  84. m_ok_button->on_click = [this](auto) {
  85. dbgln("GUI::InputBox: OK button clicked");
  86. m_text_value = m_text_editor->text();
  87. done(ExecOK);
  88. };
  89. m_cancel_button = button_container_inner.add<Button>();
  90. m_cancel_button->set_fixed_height(20);
  91. m_cancel_button->set_text("Cancel");
  92. m_cancel_button->on_click = [this](auto) {
  93. dbgln("GUI::InputBox: Cancel button clicked");
  94. done(ExecCancel);
  95. };
  96. m_text_editor->on_return_pressed = [this] {
  97. m_ok_button->click();
  98. };
  99. m_text_editor->on_escape_pressed = [this] {
  100. m_cancel_button->click();
  101. };
  102. m_text_editor->set_focus(true);
  103. }
  104. }