From de98b2770bd32651e8472809dd67882ee6680808 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 8 May 2019 20:13:39 +0200 Subject: [PATCH] GMessageBox: Add icons to message boxes with 3 standard ones to choose from. --- Applications/FileManager/main.cpp | 3 +- Base/res/icons/32x32/msgbox-error.png | Bin 0 -> 666 bytes Base/res/icons/32x32/msgbox-information.png | Bin 0 -> 636 bytes Base/res/icons/32x32/msgbox-warning.png | Bin 0 -> 583 bytes DevTools/VisualBuilder/VBForm.cpp | 3 +- LibGUI/GMessageBox.cpp | 48 ++++++++++++++++++-- LibGUI/GMessageBox.h | 13 +++++- 7 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 Base/res/icons/32x32/msgbox-error.png create mode 100644 Base/res/icons/32x32/msgbox-information.png create mode 100644 Base/res/icons/32x32/msgbox-warning.png 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 0000000000000000000000000000000000000000..9c23ef00d052ebc85a88a0aedd9f6708f99f52c1 GIT binary patch literal 666 zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANM!iJmTwArXh)PIt@}c9dwF zpRzeqRNqNMC4l4JD#0adp7)4Xf4CGc@J0N|qWyt`RPdJPKBu!<;kqj>zFeFhXPPxX1RRiWRrF?mu=I`Nxf#de*^A1`I9z`9haX`1 z!SyDSYmR2ilyZ<28T*MKFWFWe>-$ZcSi(JtM4Ln}auw{DF{bkQ{( z&D-k&84YLV2e15mwXygC_lHFeoWd2(UYnLoeUbJ?_Z2OX)Gxsv7<{a>} zS)vqS?sQ>Z`jt#M&gc&!o!c~7)Pp&mS$;Hab(VL15GCPf{lR3dgR#~IpUFac4^%pD z=ol@E5h^_#b(d++iH>u&r+s&~3A~B2S~flAzBxyat@~ulpe|+h`4>2Pd|BM~Ek2pU ze7i4ARYF;~(K+E^bd&j$Q){DsxNy}dM(h_7NSFH2$CsWRCe_$@Z`0D%3*MjHUsPxz zEp%1o-`_K@Bl1?>Ytz|b5hK@GR3!ZElCNi&dx=r=1K|%gJZpWAJ(=-->LbTB2e$r> z+@5lO;kTJP<`x>x6z$|*Icc}0Ze(Jxl;eho&6;bE6qe_E<-11LWyksa`?mV#|Bu4D Yi+@=FVdQ&MBb@07KIwxBvhE literal 0 HcmV?d00001 diff --git a/Base/res/icons/32x32/msgbox-information.png b/Base/res/icons/32x32/msgbox-information.png new file mode 100644 index 0000000000000000000000000000000000000000..92ebbaa49eafd31f2284537558a77aba9b956af1 GIT binary patch literal 636 zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANM!&YmugArXh)PPfgz9U#&+ zf3>!{rb(;QGKYiJQ70bQzD;{EDL&!V!p#e}WVfv|W&PhI@t;YbHNUiszj52{g%?f+ zWN?)#$F4j)Q7Tk^UyzRD!o8Cx=smAJ|6TjX_5M{QyRCO}&tXzOV0fW#pH8Fq1Ev{9 zuWQ#&cPRfPAi;2+VfumQ3l0fSb}x3!EQn&TGhlN&QzgG${leTmjB#8G*pJ<=iQnGR zaf!h>ZEyH?Wreu;%(7?v4X$6Ew7T5kJ(EsDU7*Powh!N1o-x$(CQhi%tt(?W@c8ZQc(jq1@M?|&ZnTM?m}{Xj&6UygY?YkNi84#BWx z9R3`8yw=*;TWh^*IJBbDWwDL(gBce>n6__A%nO%TWIIXmf?m$j+*=0!<8GUEJ+Ay`7*~Ao%ukoAL!EAw5Z^-~;aF9!~$obS~>K z&h&Zgwpd}?anF;}-Zcq&oqn|9?(ex4{}(?h)rrpPxF>2D82!-lqngYzS5GlllRHjR zdR8&UbZRxZ&a=}g+T4Fh`;Im1!~pZv7T1_7U-?;f@a|>$^|dRgm^VB=?~B8(ylp~D tQ&TJF$*u66_TSWAjraeozxMkX-k-n6a{6AMAOiyfgQu&X%Q~loCIHJB8g2jp literal 0 HcmV?d00001 diff --git a/Base/res/icons/32x32/msgbox-warning.png b/Base/res/icons/32x32/msgbox-warning.png new file mode 100644 index 0000000000000000000000000000000000000000..99c4b07297c566cbd0ed491a6396b6bf73b0a62f GIT binary patch literal 583 zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANM!e4Z|jArXh)PCuA;*+HOf z|4iB4y#ZD3A-w`0xMV6ii*$p|$y79b;B(DYUGk+;OVPAH;K)RA2d6E)Zy!!LvU2i{ zW3MMLCoMT~?$&PW|J(ns$^9WO`{>o8=i%bE?~U3@U+$S6l`bfC`(xFe?oo6hSH%AH38dr&$D-McwX4=ov^TO@=GrDuBL6F>$Og_WizeV zwtPYE)zx}u!#y2Xu6~QVpz+m|BS|Bp=hrUPj)e(puXy+V;uG02wQ5ObT}zorPWCHh z%@^CQ91pr6@%*=#W=79vR*AiTqyGK>@nBD`YlP3;Xn{D{WlxXj^9Kt}F6FxCb9`7 zee~oD$9H$FMF+WWJ?q?V^;79y?JEC8FFjXYE#JGZU14hJeUo=4(fP4I7CvfZKfP3` zMZqWR-U4mTmYu&^8)c5ye$Fqc3H~N6d;iKkbLnKenOupjPM*I`KUd#3Un(te=htg3 z)_3k%-{0rYSn*LO(LVQGu!!HZ=XdU #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 }; };