diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index 869556476d..94dad67e18 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -90,8 +90,7 @@ int main(int argc, char** argv) ); int rc = mkdir(new_dir_path.characters(), 0777); if (rc < 0) { - GMessageBox message_box(String::format("mkdir() failed: %s", strerror(errno)), "Error", window); - message_box.exec(); + GMessageBox::show(String::format("mkdir() failed: %s", strerror(errno)), "Error", GMessageBox::Type::Error, window); } else { directory_view->refresh(); } diff --git a/Base/res/icons/32x32/msgbox-error.png b/Base/res/icons/32x32/msgbox-error.png new file mode 100644 index 0000000000..9c23ef00d0 Binary files /dev/null and b/Base/res/icons/32x32/msgbox-error.png differ diff --git a/Base/res/icons/32x32/msgbox-information.png b/Base/res/icons/32x32/msgbox-information.png new file mode 100644 index 0000000000..92ebbaa49e Binary files /dev/null and b/Base/res/icons/32x32/msgbox-information.png differ diff --git a/Base/res/icons/32x32/msgbox-warning.png b/Base/res/icons/32x32/msgbox-warning.png new file mode 100644 index 0000000000..99c4b07297 Binary files /dev/null and b/Base/res/icons/32x32/msgbox-warning.png differ diff --git a/DevTools/VisualBuilder/VBForm.cpp b/DevTools/VisualBuilder/VBForm.cpp index 63c4886f30..aac6e8f122 100644 --- a/DevTools/VisualBuilder/VBForm.cpp +++ b/DevTools/VisualBuilder/VBForm.cpp @@ -307,8 +307,7 @@ void VBForm::write_to_file(const String& path) { CFile file(path); if (!file.open(CIODevice::WriteOnly)) { - GMessageBox box(String::format("Could not open '%s' for writing", path.characters()), "Error", window()); - box.exec(); + GMessageBox::show(String::format("Could not open '%s' for writing", path.characters()), "Error", GMessageBox::Type::Error, window()); return; } file.printf("[Form]\n"); diff --git a/LibGUI/GMessageBox.cpp b/LibGUI/GMessageBox.cpp index dddc8414c6..aa1f83c330 100644 --- a/LibGUI/GMessageBox.cpp +++ b/LibGUI/GMessageBox.cpp @@ -2,10 +2,18 @@ #include #include #include +#include -GMessageBox::GMessageBox(const String& text, const String& title, CObject* parent) +void GMessageBox::show(const String& text, const String& title, Type type, CObject* parent) +{ + GMessageBox box(text, title, type, parent); + box.exec(); +} + +GMessageBox::GMessageBox(const String& text, const String& title, Type type, CObject* parent) : GDialog(parent) , m_text(text) + , m_type(type) { set_title(title); build(); @@ -15,14 +23,27 @@ GMessageBox::~GMessageBox() { } +RetainPtr GMessageBox::icon() const +{ + 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; + } +} + void GMessageBox::build() { auto* widget = new GWidget; set_main_widget(widget); int text_width = widget->font().width(m_text); - - set_rect(x(), y(), text_width + 80, 80); + int icon_width = 0; widget->set_layout(make(Orientation::Vertical)); widget->set_fill_with_background_color(true); @@ -30,8 +51,22 @@ void GMessageBox::build() widget->layout()->set_margins({ 0, 15, 0, 15 }); widget->layout()->set_spacing(15); - auto* label = new GLabel(m_text, widget); - label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); + GWidget* message_container = widget; + if (m_type != Type::None) { + message_container = new GWidget(widget); + message_container->set_layout(make(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); label->set_preferred_size({ text_width, 16 }); auto* button = new GButton(widget); @@ -42,4 +77,7 @@ void GMessageBox::build() dbgprintf("GMessageBox: OK button clicked\n"); done(0); }; + + set_rect(x(), y(), text_width + icon_width + 80, 100); + set_resizable(false); } diff --git a/LibGUI/GMessageBox.h b/LibGUI/GMessageBox.h index 3e5acff024..352079c7a6 100644 --- a/LibGUI/GMessageBox.h +++ b/LibGUI/GMessageBox.h @@ -4,11 +4,22 @@ class GMessageBox : public GDialog { public: - explicit GMessageBox(const String& text, const String& title, CObject* parent = nullptr); + enum class Type { + None, + Information, + Warning, + Error, + }; + + explicit GMessageBox(const String& text, const String& title, Type type = Type::None, CObject* parent = nullptr); virtual ~GMessageBox() override; + static void show(const String& text, const String& title, Type type = Type::None, CObject* parent = nullptr); + private: void build(); + RetainPtr icon() const; String m_text; + Type m_type { Type::None }; };