/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include namespace GUI { int MessageBox::show(Window* parent_window, const StringView& text, const StringView& title, Type type, InputType input_type) { auto box = MessageBox::construct(parent_window, text, title, type, input_type); if (parent_window) box->set_icon(parent_window->icon()); return box->exec(); } int MessageBox::show_error(Window* parent_window, const StringView& text) { return show(parent_window, text, "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK); } MessageBox::MessageBox(Window* parent_window, const StringView& text, const StringView& title, Type type, InputType input_type) : Dialog(parent_window) , m_text(text) , m_type(type) , m_input_type(input_type) { set_title(title); build(); } MessageBox::~MessageBox() { } RefPtr MessageBox::icon() const { switch (m_type) { case Type::Information: return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-information.png"); case Type::Warning: return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-warning.png"); case Type::Error: return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-error.png"); case Type::Question: return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-question.png"); 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(); } void MessageBox::build() { auto& widget = set_main_widget(); int text_width = widget.font().width(m_text); auto number_of_lines = m_text.split('\n').size(); int padded_text_height = widget.font().glyph_height() * 1.6; int total_text_height = number_of_lines * padded_text_height; int icon_width = 0; widget.set_layout(); widget.set_fill_with_background_color(true); widget.layout()->set_margins(8); widget.layout()->set_spacing(6); auto& message_container = widget.add(); message_container.set_layout(); message_container.layout()->set_spacing(8); if (m_type != Type::None) { auto& icon_image = message_container.add(); icon_image.set_bitmap(icon()); if (icon()) { icon_width = icon()->width(); if (icon_width > 0) message_container.layout()->set_margins({ 0, 0, 0, 8 }); } } auto& label = message_container.add