InputBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(const StringView& prompt, const StringView& title, Core::Object* parent)
  35. : Dialog(parent)
  36. , m_prompt(prompt)
  37. {
  38. set_title(title);
  39. build();
  40. }
  41. InputBox::~InputBox()
  42. {
  43. }
  44. void InputBox::build()
  45. {
  46. auto& widget = set_main_widget<Widget>();
  47. int text_width = widget.font().width(m_prompt);
  48. int title_width = widget.font().width(title()) + 24 /* icon, plus a little padding -- not perfect */;
  49. int max_width = AK::max(text_width, title_width);
  50. set_rect(x(), y(), max_width + 80, 80);
  51. widget.set_layout<VerticalBoxLayout>();
  52. widget.set_fill_with_background_color(true);
  53. widget.layout()->set_margins({ 8, 8, 8, 8 });
  54. widget.layout()->set_spacing(8);
  55. auto label = widget.add<Label>(m_prompt);
  56. label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  57. label->set_preferred_size(text_width, 16);
  58. m_text_editor = widget.add<TextBox>();
  59. m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  60. m_text_editor->set_preferred_size(0, 19);
  61. auto button_container_outer = widget.add<Widget>();
  62. button_container_outer->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  63. button_container_outer->set_preferred_size(0, 20);
  64. button_container_outer->set_layout<VerticalBoxLayout>();
  65. auto button_container_inner = button_container_outer->add<Widget>();
  66. button_container_inner->set_layout<HorizontalBoxLayout>();
  67. button_container_inner->layout()->set_spacing(8);
  68. m_cancel_button = button_container_inner->add<Button>();
  69. m_cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  70. m_cancel_button->set_preferred_size(0, 20);
  71. m_cancel_button->set_text("Cancel");
  72. m_cancel_button->on_click = [this] {
  73. dbgprintf("GInputBox: Cancel button clicked\n");
  74. done(ExecCancel);
  75. };
  76. m_ok_button = button_container_inner->add<Button>();
  77. m_ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  78. m_ok_button->set_preferred_size(0, 20);
  79. m_ok_button->set_text("OK");
  80. m_ok_button->on_click = [this] {
  81. dbgprintf("GInputBox: OK button clicked\n");
  82. m_text_value = m_text_editor->text();
  83. done(ExecOK);
  84. };
  85. m_text_editor->on_return_pressed = [this] {
  86. m_ok_button->click();
  87. };
  88. m_text_editor->on_escape_pressed = [this] {
  89. m_cancel_button->click();
  90. };
  91. m_text_editor->set_focus(true);
  92. }
  93. }