2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2022-01-04 19:08:45 +03:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2022-01-04 19:08:45 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2022-01-06 00:43:24 +03:00
|
|
|
#include <AK/NumberFormat.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/Button.h>
|
2020-07-22 16:29:51 +03:00
|
|
|
#include <LibGUI/ImageWidget.h>
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <LibGUI/Label.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2020-02-15 01:53:11 +03:00
|
|
|
#include <LibGfx/Font.h>
|
2019-03-19 02:01:02 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
int MessageBox::show(Window* parent_window, StringView text, StringView title, Type type, InputType input_type)
|
2019-05-08 21:13:39 +03:00
|
|
|
{
|
2020-07-16 05:45:11 +03:00
|
|
|
auto box = MessageBox::construct(parent_window, text, title, type, input_type);
|
|
|
|
if (parent_window)
|
2020-04-12 10:44:22 +03:00
|
|
|
box->set_icon(parent_window->icon());
|
2019-09-21 21:32:31 +03:00
|
|
|
return box->exec();
|
2019-05-08 21:13:39 +03:00
|
|
|
}
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
int MessageBox::show_error(Window* parent_window, StringView text)
|
2020-05-13 22:11:49 +03:00
|
|
|
{
|
2020-07-16 05:45:11 +03:00
|
|
|
return show(parent_window, text, "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
|
2020-05-13 22:11:49 +03:00
|
|
|
}
|
|
|
|
|
2022-01-04 19:33:26 +03:00
|
|
|
int MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Time> last_unmodified_timestamp)
|
2022-01-04 19:08:45 +03:00
|
|
|
{
|
2022-01-04 19:33:26 +03:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append("Save changes to ");
|
2022-01-04 19:08:45 +03:00
|
|
|
if (path.is_empty())
|
2022-01-04 19:33:26 +03:00
|
|
|
builder.append("untitled document");
|
2022-01-04 19:08:45 +03:00
|
|
|
else
|
2022-01-04 19:33:26 +03:00
|
|
|
builder.appendff("\"{}\"", LexicalPath::basename(path));
|
|
|
|
builder.append(" before closing?");
|
2022-01-04 19:08:45 +03:00
|
|
|
|
2022-01-04 20:07:06 +03:00
|
|
|
if (!path.is_empty() && last_unmodified_timestamp.has_value()) {
|
2022-01-04 19:33:26 +03:00
|
|
|
auto age = (Time::now_monotonic() - *last_unmodified_timestamp).to_seconds();
|
2022-01-06 00:43:24 +03:00
|
|
|
auto readable_time = human_readable_time(age);
|
|
|
|
builder.appendff("\nLast saved {} ago.", readable_time);
|
2022-01-04 19:33:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto box = MessageBox::construct(parent_window, builder.string_view(), "Unsaved changes", Type::Warning, InputType::YesNoCancel);
|
2022-01-04 19:08:45 +03:00
|
|
|
if (parent_window)
|
|
|
|
box->set_icon(parent_window->icon());
|
|
|
|
|
|
|
|
box->m_yes_button->set_text(path.is_empty() ? "Save As..." : "Save");
|
|
|
|
box->m_no_button->set_text("Close");
|
|
|
|
box->m_cancel_button->set_text("Cancel");
|
|
|
|
|
|
|
|
return box->exec();
|
|
|
|
}
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
MessageBox::MessageBox(Window* parent_window, StringView text, StringView title, Type type, InputType input_type)
|
2020-03-04 22:53:51 +03:00
|
|
|
: Dialog(parent_window)
|
2019-03-19 02:01:02 +03:00
|
|
|
, m_text(text)
|
2019-05-08 21:13:39 +03:00
|
|
|
, m_type(type)
|
2019-07-16 22:32:10 +03:00
|
|
|
, m_input_type(input_type)
|
2019-03-19 02:01:02 +03:00
|
|
|
{
|
|
|
|
set_title(title);
|
|
|
|
build();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
MessageBox::~MessageBox()
|
2019-03-19 02:01:02 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
RefPtr<Gfx::Bitmap> MessageBox::icon() const
|
2019-05-08 21:13:39 +03:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Information:
|
2021-11-06 18:25:29 +03:00
|
|
|
return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-information.png").release_value_but_fixme_should_propagate_errors();
|
2019-05-08 21:13:39 +03:00
|
|
|
case Type::Warning:
|
2021-11-06 18:25:29 +03:00
|
|
|
return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-warning.png").release_value_but_fixme_should_propagate_errors();
|
2019-05-08 21:13:39 +03:00
|
|
|
case Type::Error:
|
2021-11-06 18:25:29 +03:00
|
|
|
return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-error.png").release_value_but_fixme_should_propagate_errors();
|
2020-05-20 02:16:46 +03:00
|
|
|
case Type::Question:
|
2021-11-06 18:25:29 +03:00
|
|
|
return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-question.png").release_value_but_fixme_should_propagate_errors();
|
2019-05-08 21:13:39 +03:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
bool MessageBox::should_include_ok_button() const
|
2019-07-16 22:32:10 +03:00
|
|
|
{
|
|
|
|
return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
bool MessageBox::should_include_cancel_button() const
|
2019-07-16 22:32:10 +03:00
|
|
|
{
|
2020-02-17 07:46:02 +03:00
|
|
|
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();
|
2019-07-16 22:32:10 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void MessageBox::build()
|
2019-03-19 02:01:02 +03:00
|
|
|
{
|
2020-03-04 11:46:23 +03:00
|
|
|
auto& widget = set_main_widget<Widget>();
|
2019-03-19 02:01:02 +03:00
|
|
|
|
2020-03-04 11:46:23 +03:00
|
|
|
int text_width = widget.font().width(m_text);
|
2021-07-07 18:36:34 +03:00
|
|
|
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;
|
2019-05-08 21:13:39 +03:00
|
|
|
int icon_width = 0;
|
2019-03-19 02:01:02 +03:00
|
|
|
|
2020-03-04 11:46:23 +03:00
|
|
|
widget.set_layout<VerticalBoxLayout>();
|
|
|
|
widget.set_fill_with_background_color(true);
|
2019-03-19 02:01:02 +03:00
|
|
|
|
Userland+LibGUI: Add shorthand versions of the Margins constructor
This allows for typing [8] instead of [8, 8, 8, 8] to specify the same
margin on all edges, for example. The constructors follow CSS' style of
specifying margins. The added constructors are:
- Margins(int all): Sets the same margin on all edges.
- Margins(int vertical, int horizontal): Sets the first argument to top
and bottom margins, and the second argument to left and right margins.
- Margins(int top, int vertical, int bottom): Sets the first argument to
the top margin, the second argument to the left and right margins,
and the third argument to the bottom margin.
2021-08-17 03:11:38 +03:00
|
|
|
widget.layout()->set_margins(8);
|
2021-01-02 03:36:24 +03:00
|
|
|
widget.layout()->set_spacing(6);
|
2019-03-19 02:01:02 +03:00
|
|
|
|
2020-04-23 12:08:35 +03:00
|
|
|
auto& message_container = widget.add<Widget>();
|
|
|
|
message_container.set_layout<HorizontalBoxLayout>();
|
|
|
|
message_container.layout()->set_spacing(8);
|
2019-05-08 21:13:39 +03:00
|
|
|
|
2020-04-23 12:08:35 +03:00
|
|
|
if (m_type != Type::None) {
|
2020-07-22 16:29:51 +03:00
|
|
|
auto& icon_image = message_container.add<ImageWidget>();
|
2020-06-14 11:06:55 +03:00
|
|
|
icon_image.set_bitmap(icon());
|
2021-07-09 23:19:24 +03:00
|
|
|
if (icon()) {
|
2020-06-18 23:18:19 +03:00
|
|
|
icon_width = icon()->width();
|
2021-07-09 23:19:24 +03:00
|
|
|
if (icon_width > 0)
|
2021-08-17 03:10:51 +03:00
|
|
|
message_container.layout()->set_margins({ 0, 0, 0, 8 });
|
2021-07-09 23:19:24 +03:00
|
|
|
}
|
2019-05-08 21:13:39 +03:00
|
|
|
}
|
|
|
|
|
2020-04-23 12:08:35 +03:00
|
|
|
auto& label = message_container.add<Label>(m_text);
|
2021-07-07 18:36:34 +03:00
|
|
|
label.set_fixed_height(total_text_height);
|
2020-04-23 12:08:35 +03:00
|
|
|
if (m_type != Type::None)
|
|
|
|
label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
2019-03-19 02:01:02 +03:00
|
|
|
|
2020-03-04 21:07:55 +03:00
|
|
|
auto& button_container = widget.add<Widget>();
|
|
|
|
button_container.set_layout<HorizontalBoxLayout>();
|
2020-12-30 03:23:32 +03:00
|
|
|
button_container.set_fixed_height(24);
|
2020-04-23 12:08:35 +03:00
|
|
|
button_container.layout()->set_spacing(8);
|
2019-07-16 22:32:10 +03:00
|
|
|
|
2021-01-02 03:36:24 +03:00
|
|
|
constexpr int button_width = 80;
|
|
|
|
int button_count = 0;
|
|
|
|
|
2022-01-04 19:08:45 +03:00
|
|
|
auto add_button = [&](String label, Dialog::ExecResult result) -> GUI::Button& {
|
2020-03-04 21:07:55 +03:00
|
|
|
auto& button = button_container.add<Button>();
|
2021-01-02 03:36:24 +03:00
|
|
|
button.set_fixed_width(button_width);
|
2020-03-04 21:07:55 +03:00
|
|
|
button.set_text(label);
|
2020-05-12 21:30:33 +03:00
|
|
|
button.on_click = [this, label, result](auto) {
|
2020-02-17 07:46:02 +03:00
|
|
|
done(result);
|
2019-07-16 22:32:10 +03:00
|
|
|
};
|
2021-01-02 03:36:24 +03:00
|
|
|
++button_count;
|
2022-01-04 19:08:45 +03:00
|
|
|
return button;
|
2020-02-17 07:46:02 +03:00
|
|
|
};
|
|
|
|
|
2020-04-23 12:08:35 +03:00
|
|
|
button_container.layout()->add_spacer();
|
2020-02-17 07:46:02 +03:00
|
|
|
if (should_include_ok_button())
|
2022-01-04 19:08:45 +03:00
|
|
|
m_ok_button = add_button("OK", Dialog::ExecOK);
|
2020-02-17 07:46:02 +03:00
|
|
|
if (should_include_yes_button())
|
2022-01-04 19:08:45 +03:00
|
|
|
m_yes_button = add_button("Yes", Dialog::ExecYes);
|
2020-02-17 07:46:02 +03:00
|
|
|
if (should_include_no_button())
|
2022-01-04 19:08:45 +03:00
|
|
|
m_no_button = add_button("No", Dialog::ExecNo);
|
2020-02-17 07:46:02 +03:00
|
|
|
if (should_include_cancel_button())
|
2022-01-04 19:08:45 +03:00
|
|
|
m_cancel_button = add_button("Cancel", Dialog::ExecCancel);
|
2020-04-23 12:08:35 +03:00
|
|
|
button_container.layout()->add_spacer();
|
|
|
|
|
2021-01-02 03:36:24 +03:00
|
|
|
int width = (button_count * button_width) + ((button_count - 1) * button_container.layout()->spacing()) + 32;
|
|
|
|
width = max(width, text_width + icon_width + 56);
|
2019-05-08 21:13:39 +03:00
|
|
|
|
2021-07-07 18:36:34 +03:00
|
|
|
set_rect(x(), y(), width, 80 + label.max_height());
|
2019-05-08 21:13:39 +03:00
|
|
|
set_resizable(false);
|
2019-03-19 02:01:02 +03:00
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|