/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, networkException * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include namespace GUI { ErrorOr> InputBox::create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type, RefPtr icon) { VERIFY(input_type != InputType::Numeric); 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)))); TRY(box->build()); return box; } ErrorOr> InputBox::create_numeric(Window* parent_window, int value, StringView title, StringView prompt, RefPtr 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; } InputBox::InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type, RefPtr icon) : Dialog(parent_window) , m_text_value(move(text_value)) , m_prompt(move(prompt)) , m_input_type(input_type) , m_icon(move(icon)) { set_title(move(title).to_deprecated_string()); set_resizable(false); set_auto_shrink(true); } InputBox::InputBox(Window* parent_window, int value, String title, String prompt, RefPtr 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); } Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr icon) { return MUST(try_show(parent_window, text_value, prompt, title, input_type, placeholder, move(icon))); } ErrorOr InputBox::try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr icon) { VERIFY(input_type != InputType::Numeric); auto box = TRY(InputBox::create(parent_window, text_value, prompt, title, input_type, move(icon))); if (parent_window) box->set_icon(parent_window->icon()); box->set_placeholder(placeholder); auto result = box->exec(); text_value = box->text_value(); return result; } ErrorOr InputBox::show_numeric(Window* parent_window, int& value, int min, int max, StringView title, StringView prompt, RefPtr 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; } void InputBox::set_placeholder(StringView view) { m_text_editor->set_placeholder(view); } void InputBox::set_range(int min, int max) { m_spinbox->set_range(min, max); } 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); } void InputBox::set_numeric_value(int value) { if (m_numeric_value == value) return; m_numeric_value = value; m_spinbox->set_value(value); } void InputBox::on_done(ExecResult result) { if (result != ExecResult::OK) return; 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(); if (m_input_type == InputType::NonemptyText) VERIFY(!m_text_value.is_empty()); } ErrorOr InputBox::build() { auto main_widget = TRY(set_main_widget()); TRY(main_widget->try_set_layout(4, 6)); main_widget->set_fill_with_background_color(true); auto top_container = TRY(main_widget->try_add()); TRY(top_container->try_set_layout(0, 8)); if (m_icon) { auto image_widget = TRY(top_container->try_add()); image_widget->set_bitmap(m_icon); } auto input_container = TRY(top_container->try_add()); auto orientation = m_icon ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal; TRY(input_container->try_set_layout(orientation)); TRY(input_container->add_spacer()); if (!m_prompt.is_empty()) { m_label_container = TRY(input_container->try_add()); TRY(m_label_container->try_set_layout()); m_prompt_label = TRY(m_label_container->try_add