2019-03-18 23:01:02 +00:00
|
|
|
#include <LibGUI/GBoxLayout.h>
|
|
|
|
#include <LibGUI/GButton.h>
|
2019-06-07 09:46:02 +00:00
|
|
|
#include <LibGUI/GLabel.h>
|
|
|
|
#include <LibGUI/GMessageBox.h>
|
2019-05-08 18:13:39 +00:00
|
|
|
#include <stdio.h>
|
2019-03-18 23:01:02 +00:00
|
|
|
|
2019-06-02 12:58:02 +00:00
|
|
|
void GMessageBox::show(const StringView& text, const StringView& title, Type type, CObject* parent)
|
2019-05-08 18:13:39 +00:00
|
|
|
{
|
|
|
|
GMessageBox box(text, title, type, parent);
|
|
|
|
box.exec();
|
|
|
|
}
|
|
|
|
|
2019-06-02 12:58:02 +00:00
|
|
|
GMessageBox::GMessageBox(const StringView& text, const StringView& title, Type type, CObject* parent)
|
2019-03-18 23:01:02 +00:00
|
|
|
: GDialog(parent)
|
|
|
|
, m_text(text)
|
2019-05-08 18:13:39 +00:00
|
|
|
, m_type(type)
|
2019-03-18 23:01:02 +00:00
|
|
|
{
|
|
|
|
set_title(title);
|
|
|
|
build();
|
|
|
|
}
|
|
|
|
|
|
|
|
GMessageBox::~GMessageBox()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<GraphicsBitmap> GMessageBox::icon() const
|
2019-05-08 18:13:39 +00:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Information:
|
|
|
|
return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-information.png");
|
|
|
|
case Type::Warning:
|
|
|
|
return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-warning.png");
|
|
|
|
case Type::Error:
|
|
|
|
return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-error.png");
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 23:01:02 +00:00
|
|
|
void GMessageBox::build()
|
|
|
|
{
|
|
|
|
auto* widget = new GWidget;
|
|
|
|
set_main_widget(widget);
|
|
|
|
|
|
|
|
int text_width = widget->font().width(m_text);
|
2019-05-08 18:13:39 +00:00
|
|
|
int icon_width = 0;
|
2019-03-18 23:01:02 +00:00
|
|
|
|
|
|
|
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
|
|
|
|
widget->set_fill_with_background_color(true);
|
|
|
|
|
|
|
|
widget->layout()->set_margins({ 0, 15, 0, 15 });
|
|
|
|
widget->layout()->set_spacing(15);
|
|
|
|
|
2019-05-08 18:13:39 +00:00
|
|
|
GWidget* message_container = widget;
|
|
|
|
if (m_type != Type::None) {
|
|
|
|
message_container = new GWidget(widget);
|
|
|
|
message_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
|
|
|
|
message_container->layout()->set_margins({ 8, 0, 8, 0 });
|
|
|
|
message_container->layout()->set_spacing(8);
|
|
|
|
|
|
|
|
auto* icon_label = new GLabel(message_container);
|
|
|
|
icon_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
|
|
|
|
icon_label->set_preferred_size({ 32, 32 });
|
|
|
|
icon_label->set_icon(icon());
|
|
|
|
icon_width = icon_label->icon()->width();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* label = new GLabel(m_text, message_container);
|
|
|
|
label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
2019-03-18 23:01:02 +00:00
|
|
|
label->set_preferred_size({ text_width, 16 });
|
|
|
|
|
|
|
|
auto* button = new GButton(widget);
|
|
|
|
button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
|
2019-03-27 19:47:48 +00:00
|
|
|
button->set_preferred_size({ 100, 20 });
|
2019-05-24 14:32:20 +00:00
|
|
|
button->set_text("OK");
|
2019-06-07 09:46:02 +00:00
|
|
|
button->on_click = [this](auto&) {
|
2019-03-20 21:31:21 +00:00
|
|
|
dbgprintf("GMessageBox: OK button clicked\n");
|
2019-03-18 23:01:02 +00:00
|
|
|
done(0);
|
|
|
|
};
|
2019-05-08 18:13:39 +00:00
|
|
|
|
|
|
|
set_rect(x(), y(), text_width + icon_width + 80, 100);
|
|
|
|
set_resizable(false);
|
2019-03-18 23:01:02 +00:00
|
|
|
}
|