2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-09-12 16:06:24 +02:00
|
|
|
* Copyright (c) 2021, networkException <networkexception@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
#include <LibGUI/Button.h>
|
2023-04-16 16:02:29 -04:00
|
|
|
#include <LibGUI/ImageWidget.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/InputBox.h>
|
|
|
|
|
#include <LibGUI/Label.h>
|
2023-04-16 16:04:18 -04:00
|
|
|
#include <LibGUI/SpinBox.h>
|
2020-02-23 10:31:26 +01:00
|
|
|
#include <LibGUI/TextBox.h>
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2023-04-16 16:02:29 -04:00
|
|
|
ErrorOr<NonnullRefPtr<InputBox>> InputBox::create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
|
2023-04-16 16:02:07 -04:00
|
|
|
{
|
2023-04-16 16:04:18 -04:00
|
|
|
VERIFY(input_type != InputType::Numeric);
|
2023-04-16 16:02:29 -04:00
|
|
|
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))));
|
2023-04-16 16:02:07 -04:00
|
|
|
TRY(box->build());
|
|
|
|
|
return box;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
ErrorOr<NonnullRefPtr<InputBox>> InputBox::create_numeric(Window* parent_window, int value, StringView title, StringView prompt, RefPtr<Gfx::Bitmap const> icon)
|
|
|
|
|
{
|
|
|
|
|
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))));
|
|
|
|
|
TRY(box->build());
|
|
|
|
|
return box;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:02:29 -04:00
|
|
|
InputBox::InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
|
2020-03-04 20:53:51 +01:00
|
|
|
: Dialog(parent_window)
|
2022-11-16 07:35:02 -05:00
|
|
|
, m_text_value(move(text_value))
|
2023-04-16 16:02:07 -04:00
|
|
|
, m_prompt(move(prompt))
|
2022-12-31 17:52:09 +01:00
|
|
|
, m_input_type(input_type)
|
2023-04-16 16:02:29 -04:00
|
|
|
, m_icon(move(icon))
|
2019-03-19 01:41:00 +01:00
|
|
|
{
|
2023-04-16 16:02:07 -04:00
|
|
|
set_title(move(title).to_deprecated_string());
|
|
|
|
|
set_resizable(false);
|
|
|
|
|
set_auto_shrink(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
InputBox::InputBox(Window* parent_window, int value, String title, String prompt, RefPtr<Gfx::Bitmap const> icon)
|
|
|
|
|
: Dialog(parent_window)
|
|
|
|
|
, m_numeric_value(value)
|
|
|
|
|
, m_prompt(move(prompt))
|
|
|
|
|
, m_input_type(InputType::Numeric)
|
|
|
|
|
, m_icon(move(icon))
|
|
|
|
|
{
|
|
|
|
|
set_title(move(title).to_deprecated_string());
|
|
|
|
|
set_resizable(false);
|
|
|
|
|
set_auto_shrink(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:02:29 -04:00
|
|
|
Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr<Gfx::Bitmap const> icon)
|
2023-04-16 16:02:07 -04:00
|
|
|
{
|
2023-04-16 16:02:29 -04:00
|
|
|
return MUST(try_show(parent_window, text_value, prompt, title, input_type, placeholder, move(icon)));
|
2019-03-19 01:41:00 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:02:29 -04:00
|
|
|
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)
|
2020-07-16 07:54:42 -06:00
|
|
|
{
|
2023-04-16 16:04:18 -04:00
|
|
|
VERIFY(input_type != InputType::Numeric);
|
2023-04-16 16:02:29 -04:00
|
|
|
auto box = TRY(InputBox::create(parent_window, text_value, prompt, title, input_type, move(icon)));
|
2020-07-17 10:21:44 -04:00
|
|
|
if (parent_window)
|
|
|
|
|
box->set_icon(parent_window->icon());
|
2023-04-16 16:02:07 -04:00
|
|
|
box->set_placeholder(placeholder);
|
2020-07-16 07:54:42 -06:00
|
|
|
auto result = box->exec();
|
|
|
|
|
text_value = box->text_value();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
ErrorOr<Dialog::ExecResult> InputBox::show_numeric(Window* parent_window, int& value, int min, int max, StringView title, StringView prompt, RefPtr<Gfx::Bitmap const> icon)
|
|
|
|
|
{
|
|
|
|
|
auto box = TRY(InputBox::create_numeric(parent_window, value, title, prompt, move(icon)));
|
|
|
|
|
if (parent_window)
|
|
|
|
|
box->set_icon(parent_window->icon());
|
|
|
|
|
box->set_range(min, max);
|
|
|
|
|
auto result = box->exec();
|
|
|
|
|
value = box->numeric_value();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:02:07 -04:00
|
|
|
void InputBox::set_placeholder(StringView view)
|
2022-11-16 07:35:02 -05:00
|
|
|
{
|
2023-04-16 16:02:07 -04:00
|
|
|
m_text_editor->set_placeholder(view);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
void InputBox::set_range(int min, int max)
|
|
|
|
|
{
|
|
|
|
|
m_spinbox->set_range(min, max);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:02:07 -04:00
|
|
|
void InputBox::set_text_value(String value)
|
|
|
|
|
{
|
|
|
|
|
if (m_text_value == value)
|
|
|
|
|
return;
|
|
|
|
|
m_text_value = move(value);
|
|
|
|
|
m_text_editor->set_text(m_text_value);
|
2022-11-16 07:35:02 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
void InputBox::set_numeric_value(int value)
|
|
|
|
|
{
|
|
|
|
|
if (m_numeric_value == value)
|
|
|
|
|
return;
|
|
|
|
|
m_numeric_value = value;
|
|
|
|
|
m_spinbox->set_value(value);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-16 07:35:02 -05:00
|
|
|
void InputBox::on_done(ExecResult result)
|
|
|
|
|
{
|
2023-04-16 16:02:07 -04:00
|
|
|
if (result != ExecResult::OK)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
if (m_text_editor) {
|
|
|
|
|
auto value = String::from_deprecated_string(m_text_editor->text());
|
|
|
|
|
if (!value.is_error())
|
|
|
|
|
m_text_value = value.release_value();
|
|
|
|
|
} else if (m_spinbox)
|
|
|
|
|
m_numeric_value = m_spinbox->value();
|
2023-04-16 16:02:07 -04:00
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
if (m_input_type == InputType::NonemptyText)
|
2023-04-16 16:02:07 -04:00
|
|
|
VERIFY(!m_text_value.is_empty());
|
2022-11-16 07:35:02 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:02:07 -04:00
|
|
|
ErrorOr<void> InputBox::build()
|
2019-03-19 01:41:00 +01:00
|
|
|
{
|
2023-09-19 01:13:48 +01:00
|
|
|
auto main_widget = set_main_widget<Widget>();
|
2023-08-13 14:52:36 +02:00
|
|
|
main_widget->set_layout<VerticalBoxLayout>(6, 6);
|
2023-04-16 16:02:07 -04:00
|
|
|
main_widget->set_fill_with_background_color(true);
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2023-04-16 16:02:29 -04:00
|
|
|
if (!m_prompt.is_empty()) {
|
2023-09-22 22:28:59 +01:00
|
|
|
auto& prompt_container = main_widget->add<Widget>();
|
|
|
|
|
prompt_container.set_layout<HorizontalBoxLayout>(0, 8);
|
2023-05-22 13:07:04 -04:00
|
|
|
if (m_icon) {
|
2023-09-22 22:28:59 +01:00
|
|
|
auto& image_widget = prompt_container.add<ImageWidget>();
|
|
|
|
|
image_widget.set_bitmap(m_icon);
|
2023-05-22 13:07:04 -04:00
|
|
|
}
|
2023-09-22 22:28:59 +01:00
|
|
|
m_prompt_label = prompt_container.add<Label>();
|
2023-04-16 16:02:29 -04:00
|
|
|
m_prompt_label->set_autosize(true);
|
|
|
|
|
m_prompt_label->set_text_wrapping(Gfx::TextWrapping::DontWrap);
|
2023-05-22 13:07:04 -04:00
|
|
|
m_prompt_label->set_text(m_prompt);
|
2023-04-16 16:02:29 -04:00
|
|
|
}
|
|
|
|
|
|
2022-12-31 17:52:09 +01:00
|
|
|
switch (m_input_type) {
|
2021-07-24 22:59:39 -06:00
|
|
|
case InputType::Text:
|
2023-01-14 15:14:47 -07:00
|
|
|
case InputType::NonemptyText:
|
2023-09-22 22:28:59 +01:00
|
|
|
m_text_editor = main_widget->add<TextBox>();
|
2021-07-24 22:59:39 -06:00
|
|
|
break;
|
|
|
|
|
case InputType::Password:
|
2023-09-22 22:28:59 +01:00
|
|
|
m_text_editor = main_widget->add<PasswordBox>();
|
2021-07-24 22:59:39 -06:00
|
|
|
break;
|
2023-04-16 16:04:18 -04:00
|
|
|
case InputType::Numeric:
|
2023-09-22 22:28:59 +01:00
|
|
|
m_spinbox = main_widget->add<SpinBox>();
|
2023-04-16 16:04:18 -04:00
|
|
|
break;
|
2021-07-24 22:59:39 -06:00
|
|
|
}
|
|
|
|
|
|
2023-09-22 22:28:59 +01:00
|
|
|
auto& button_container = main_widget->add<Widget>();
|
|
|
|
|
button_container.set_layout<HorizontalBoxLayout>(0, 6);
|
|
|
|
|
button_container.add_spacer();
|
2021-07-06 16:47:44 +02:00
|
|
|
|
2023-09-22 22:28:59 +01:00
|
|
|
m_ok_button = button_container.add<DialogButton>("OK"_string);
|
2023-04-16 16:04:18 -04:00
|
|
|
m_ok_button->on_click = [this](auto) {
|
|
|
|
|
if (m_spinbox)
|
|
|
|
|
m_spinbox->set_value_from_current_text();
|
|
|
|
|
done(ExecResult::OK);
|
|
|
|
|
};
|
2022-01-24 14:29:21 -05:00
|
|
|
m_ok_button->set_default(true);
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2023-09-22 22:28:59 +01:00
|
|
|
m_cancel_button = button_container.add<DialogButton>("Cancel"_string);
|
2023-04-16 16:02:07 -04:00
|
|
|
m_cancel_button->on_click = [this](auto) { done(ExecResult::Cancel); };
|
2020-04-29 19:31:15 +02:00
|
|
|
|
2023-09-22 22:28:59 +01:00
|
|
|
auto guarantee_width = [this, &button_container] {
|
2023-05-22 13:07:04 -04:00
|
|
|
if (m_prompt.is_empty())
|
|
|
|
|
return;
|
2023-09-22 22:28:59 +01:00
|
|
|
auto width = button_container.calculated_min_size().value().width().as_int();
|
2023-05-22 13:07:04 -04:00
|
|
|
auto constexpr golden_ratio = 1.618;
|
2023-09-22 22:28:59 +01:00
|
|
|
button_container.set_min_width(width * golden_ratio);
|
2019-03-19 01:41:00 +01:00
|
|
|
};
|
2023-05-22 13:07:04 -04:00
|
|
|
guarantee_width();
|
|
|
|
|
on_font_change = [guarantee_width] { guarantee_width(); };
|
2023-04-16 16:02:07 -04:00
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
if (m_text_editor) {
|
|
|
|
|
m_text_editor->set_text(m_text_value);
|
2022-06-28 17:12:52 +02:00
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
if (m_input_type == InputType::NonemptyText) {
|
|
|
|
|
m_text_editor->on_change = [this] {
|
|
|
|
|
m_ok_button->set_enabled(!m_text_editor->text().is_empty());
|
|
|
|
|
};
|
|
|
|
|
m_text_editor->on_change();
|
|
|
|
|
}
|
2023-01-14 15:14:47 -07:00
|
|
|
}
|
|
|
|
|
|
2023-04-16 16:04:18 -04:00
|
|
|
if (m_spinbox)
|
|
|
|
|
m_spinbox->set_value(m_numeric_value);
|
|
|
|
|
|
2023-04-16 16:02:07 -04:00
|
|
|
auto size = main_widget->effective_min_size();
|
|
|
|
|
resize(TRY(size.width().shrink_value()), TRY(size.height().shrink_value()));
|
|
|
|
|
|
|
|
|
|
return {};
|
2019-03-19 01:41:00 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|