InputBox.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, networkException <networkexception@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/Button.h>
  10. #include <LibGUI/ImageWidget.h>
  11. #include <LibGUI/InputBox.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/SpinBox.h>
  14. #include <LibGUI/TextBox.h>
  15. namespace GUI {
  16. ErrorOr<NonnullRefPtr<InputBox>> InputBox::create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
  17. {
  18. VERIFY(input_type != InputType::Numeric);
  19. auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) InputBox(parent_window, text_value, TRY(String::from_utf8(title)), TRY(String::from_utf8(prompt)), input_type, move(icon))));
  20. TRY(box->build());
  21. return box;
  22. }
  23. ErrorOr<NonnullRefPtr<InputBox>> InputBox::create_numeric(Window* parent_window, int value, StringView title, StringView prompt, RefPtr<Gfx::Bitmap const> icon)
  24. {
  25. auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) InputBox(parent_window, value, TRY(String::from_utf8(title)), TRY(String::from_utf8(prompt)), move(icon))));
  26. TRY(box->build());
  27. return box;
  28. }
  29. InputBox::InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
  30. : Dialog(parent_window)
  31. , m_text_value(move(text_value))
  32. , m_prompt(move(prompt))
  33. , m_input_type(input_type)
  34. , m_icon(move(icon))
  35. {
  36. set_title(move(title).to_deprecated_string());
  37. set_resizable(false);
  38. set_auto_shrink(true);
  39. }
  40. InputBox::InputBox(Window* parent_window, int value, String title, String prompt, RefPtr<Gfx::Bitmap const> icon)
  41. : Dialog(parent_window)
  42. , m_numeric_value(value)
  43. , m_prompt(move(prompt))
  44. , m_input_type(InputType::Numeric)
  45. , m_icon(move(icon))
  46. {
  47. set_title(move(title).to_deprecated_string());
  48. set_resizable(false);
  49. set_auto_shrink(true);
  50. }
  51. Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr<Gfx::Bitmap const> icon)
  52. {
  53. return MUST(try_show(parent_window, text_value, prompt, title, input_type, placeholder, move(icon)));
  54. }
  55. ErrorOr<Dialog::ExecResult> InputBox::try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr<Gfx::Bitmap const> icon)
  56. {
  57. VERIFY(input_type != InputType::Numeric);
  58. auto box = TRY(InputBox::create(parent_window, text_value, prompt, title, input_type, move(icon)));
  59. if (parent_window)
  60. box->set_icon(parent_window->icon());
  61. box->set_placeholder(placeholder);
  62. auto result = box->exec();
  63. text_value = box->text_value();
  64. return result;
  65. }
  66. ErrorOr<Dialog::ExecResult> InputBox::show_numeric(Window* parent_window, int& value, int min, int max, StringView title, StringView prompt, RefPtr<Gfx::Bitmap const> icon)
  67. {
  68. auto box = TRY(InputBox::create_numeric(parent_window, value, title, prompt, move(icon)));
  69. if (parent_window)
  70. box->set_icon(parent_window->icon());
  71. box->set_range(min, max);
  72. auto result = box->exec();
  73. value = box->numeric_value();
  74. return result;
  75. }
  76. void InputBox::set_placeholder(StringView view)
  77. {
  78. m_text_editor->set_placeholder(view);
  79. }
  80. void InputBox::set_range(int min, int max)
  81. {
  82. m_spinbox->set_range(min, max);
  83. }
  84. void InputBox::set_text_value(String value)
  85. {
  86. if (m_text_value == value)
  87. return;
  88. m_text_value = move(value);
  89. m_text_editor->set_text(m_text_value);
  90. }
  91. void InputBox::set_numeric_value(int value)
  92. {
  93. if (m_numeric_value == value)
  94. return;
  95. m_numeric_value = value;
  96. m_spinbox->set_value(value);
  97. }
  98. void InputBox::on_done(ExecResult result)
  99. {
  100. if (result != ExecResult::OK)
  101. return;
  102. if (m_text_editor) {
  103. auto value = String::from_deprecated_string(m_text_editor->text());
  104. if (!value.is_error())
  105. m_text_value = value.release_value();
  106. } else if (m_spinbox)
  107. m_numeric_value = m_spinbox->value();
  108. if (m_input_type == InputType::NonemptyText)
  109. VERIFY(!m_text_value.is_empty());
  110. }
  111. ErrorOr<void> InputBox::build()
  112. {
  113. auto main_widget = TRY(set_main_widget<Widget>());
  114. TRY(main_widget->try_set_layout<VerticalBoxLayout>(4, 6));
  115. main_widget->set_fill_with_background_color(true);
  116. auto top_container = TRY(main_widget->try_add<Widget>());
  117. TRY(top_container->try_set_layout<HorizontalBoxLayout>(0, 8));
  118. if (m_icon) {
  119. auto image_widget = TRY(top_container->try_add<ImageWidget>());
  120. image_widget->set_bitmap(m_icon);
  121. }
  122. auto input_container = TRY(top_container->try_add<Widget>());
  123. auto orientation = m_icon ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal;
  124. TRY(input_container->try_set_layout<BoxLayout>(orientation));
  125. TRY(input_container->add_spacer());
  126. if (!m_prompt.is_empty()) {
  127. m_label_container = TRY(input_container->try_add<Widget>());
  128. TRY(m_label_container->try_set_layout<HorizontalBoxLayout>());
  129. m_prompt_label = TRY(m_label_container->try_add<Label>());
  130. m_prompt_label->set_autosize(true);
  131. m_prompt_label->set_text_wrapping(Gfx::TextWrapping::DontWrap);
  132. m_prompt_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  133. m_prompt_label->set_text(move(m_prompt));
  134. }
  135. switch (m_input_type) {
  136. case InputType::Text:
  137. case InputType::NonemptyText:
  138. m_text_editor = TRY(input_container->try_add<TextBox>());
  139. break;
  140. case InputType::Password:
  141. m_text_editor = TRY(input_container->try_add<PasswordBox>());
  142. break;
  143. case InputType::Numeric:
  144. m_spinbox = TRY(input_container->try_add<SpinBox>());
  145. break;
  146. }
  147. TRY(input_container->add_spacer());
  148. auto button_container = TRY(main_widget->try_add<Widget>());
  149. TRY(button_container->try_set_layout<HorizontalBoxLayout>(0, 6));
  150. TRY(button_container->add_spacer());
  151. m_ok_button = TRY(button_container->try_add<DialogButton>("OK"_short_string));
  152. m_ok_button->on_click = [this](auto) {
  153. if (m_spinbox)
  154. m_spinbox->set_value_from_current_text();
  155. done(ExecResult::OK);
  156. };
  157. m_ok_button->set_default(true);
  158. m_cancel_button = TRY(button_container->try_add<DialogButton>("Cancel"_short_string));
  159. m_cancel_button->on_click = [this](auto) { done(ExecResult::Cancel); };
  160. auto resize_editor = [this, button_container] {
  161. auto width = button_container->effective_min_size().width().as_int();
  162. if (m_text_editor)
  163. m_text_editor->set_min_width(width);
  164. if (m_spinbox)
  165. m_spinbox->set_min_width(width);
  166. if (!m_icon && m_label_container)
  167. m_label_container->set_fixed_width(m_prompt_label->max_width());
  168. };
  169. resize_editor();
  170. on_font_change = [resize_editor] { resize_editor(); };
  171. if (m_text_editor) {
  172. m_text_editor->set_text(m_text_value);
  173. if (m_input_type == InputType::NonemptyText) {
  174. m_text_editor->on_change = [this] {
  175. m_ok_button->set_enabled(!m_text_editor->text().is_empty());
  176. };
  177. m_text_editor->on_change();
  178. }
  179. }
  180. if (m_spinbox)
  181. m_spinbox->set_value(m_numeric_value);
  182. auto size = main_widget->effective_min_size();
  183. resize(TRY(size.width().shrink_value()), TRY(size.height().shrink_value()));
  184. return {};
  185. }
  186. }