/* * Copyright (c) 2018-2022, Andreas Kling * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include namespace GUI { ErrorOr> MessageBox::create(Window* parent_window, StringView text, StringView title, Type type, InputType input_type) { auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MessageBox(parent_window, type, input_type))); TRY(box->build()); box->set_title(TRY(String::from_utf8(title)).to_deprecated_string()); box->set_text(TRY(String::from_utf8(text))); auto size = box->main_widget()->effective_min_size(); box->resize(TRY(size.width().shrink_value()), TRY(size.height().shrink_value())); return box; } Dialog::ExecResult MessageBox::show(Window* parent_window, StringView text, StringView title, Type type, InputType input_type) { return MUST(try_show(parent_window, text, title, type, input_type)); } ErrorOr MessageBox::try_show(Window* parent_window, StringView text, StringView title, Type type, InputType input_type) { auto box = TRY(MessageBox::create(parent_window, text, title, type, input_type)); if (parent_window) box->set_icon(parent_window->icon()); return box->exec(); } ErrorOr MessageBox::try_show(Badge, i32 window_server_client_id, i32 parent_window_id, StringView text, StringView title) { auto box = TRY(MessageBox::create(nullptr, text, title, MessageBox::Type::Warning, MessageBox::InputType::YesNo)); auto parent_rect = ConnectionToWindowServer::the().get_window_rect_from_client(window_server_client_id, parent_window_id); box->center_within(parent_rect); box->constrain_to_desktop(); box->set_screen_position(ScreenPosition::DoNotPosition); box->Dialog::show(); ConnectionToWindowServer::the().set_window_parent_from_client(window_server_client_id, parent_window_id, box->window_id()); return box->exec(); } Dialog::ExecResult MessageBox::show_error(Window* parent_window, StringView text) { return MUST(try_show_error(parent_window, text)); } ErrorOr MessageBox::try_show_error(Window* parent_window, StringView text) { return TRY(try_show(parent_window, text, "Error"sv, GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK)); } Dialog::ExecResult MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp) { return MUST(try_ask_about_unsaved_changes(parent_window, path, move(last_unmodified_timestamp))); } ErrorOr MessageBox::try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp) { StringBuilder builder; TRY(builder.try_append("Save changes to "sv)); if (path.is_empty()) TRY(builder.try_append("untitled document"sv)); else TRY(builder.try_appendff("\"{}\"", LexicalPath::basename(path))); TRY(builder.try_append(" before closing?"sv)); if (!path.is_empty() && last_unmodified_timestamp.has_value()) { auto age = (MonotonicTime::now() - *last_unmodified_timestamp).to_seconds(); auto readable_time = human_readable_time(age); TRY(builder.try_appendff("\nLast saved {} ago.", readable_time)); } auto box = TRY(MessageBox::create(parent_window, builder.string_view(), "Unsaved Changes"sv, Type::Warning, InputType::YesNoCancel)); if (parent_window) box->set_icon(parent_window->icon()); if (path.is_empty()) box->m_yes_button->set_text("Save As..."_string); else box->m_yes_button->set_text("Save"_string); box->m_no_button->set_text("Discard"_string); box->m_cancel_button->set_text("Cancel"_string); return box->exec(); } void MessageBox::set_text(String text) { m_text_label->set_text(move(text)); } MessageBox::MessageBox(Window* parent_window, Type type, InputType input_type) : Dialog(parent_window) , m_type(type) , m_input_type(input_type) { set_resizable(false); set_auto_shrink(true); } ErrorOr> MessageBox::icon() const { switch (m_type) { case Type::Information: return TRY(Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-information.png"sv)); case Type::Warning: return TRY(Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-warning.png"sv)); case Type::Error: return TRY(Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-error.png"sv)); case Type::Question: return TRY(Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-question.png"sv)); default: return nullptr; } } bool MessageBox::should_include_ok_button() const { return m_input_type == InputType::OK || m_input_type == InputType::OKCancel; } bool MessageBox::should_include_cancel_button() const { return m_input_type == InputType::OKCancel || m_input_type == InputType::YesNoCancel; } bool MessageBox::should_include_yes_button() const { return m_input_type == InputType::YesNo || m_input_type == InputType::YesNoCancel; } bool MessageBox::should_include_no_button() const { return should_include_yes_button(); } ErrorOr MessageBox::build() { auto main_widget = TRY(set_main_widget()); main_widget->set_fill_with_background_color(true); main_widget->set_layout(8, 6); auto message_container = TRY(main_widget->try_add()); auto message_margins = Margins { 8, m_type != Type::None ? 8 : 0 }; message_container->set_layout(message_margins, 8); if (auto icon = TRY(this->icon()); icon && m_type != Type::None) { auto image_widget = TRY(message_container->try_add()); image_widget->set_bitmap(icon); } m_text_label = TRY(message_container->try_add