From 4755f355c64dfefdc77233cb6f394ea7933ee590 Mon Sep 17 00:00:00 2001 From: thatlittlegit Date: Sun, 16 Feb 2020 23:46:02 -0500 Subject: [PATCH] LibGUI: Add Yes/No and Yes/No/Cancel MessageBoxes --- Libraries/LibGUI/Dialog.h | 4 ++- Libraries/LibGUI/MessageBox.cpp | 48 +++++++++++++++++++-------------- Libraries/LibGUI/MessageBox.h | 4 +++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/Libraries/LibGUI/Dialog.h b/Libraries/LibGUI/Dialog.h index c909267932..10de2981a5 100644 --- a/Libraries/LibGUI/Dialog.h +++ b/Libraries/LibGUI/Dialog.h @@ -36,7 +36,9 @@ public: enum ExecResult { ExecOK = 0, ExecCancel = 1, - ExecAborted = 2 + ExecAborted = 2, + ExecYes = 3, + ExecNo = 4, }; virtual ~Dialog() override; diff --git a/Libraries/LibGUI/MessageBox.cpp b/Libraries/LibGUI/MessageBox.cpp index c910b3a386..3bec6bbd82 100644 --- a/Libraries/LibGUI/MessageBox.cpp +++ b/Libraries/LibGUI/MessageBox.cpp @@ -74,7 +74,17 @@ bool MessageBox::should_include_ok_button() const bool MessageBox::should_include_cancel_button() const { - return m_input_type == InputType::OKCancel; + 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() @@ -114,27 +124,25 @@ void MessageBox::build() button_container->layout()->set_spacing(5); button_container->layout()->set_margins({ 15, 0, 15, 0 }); - if (should_include_ok_button()) { - auto ok_button = Button::construct(button_container); - ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); - ok_button->set_preferred_size(0, 20); - ok_button->set_text("OK"); - ok_button->on_click = [this](auto&) { - dbgprintf("GMessageBox: OK button clicked\n"); - done(Dialog::ExecOK); + auto add_button = [&](String label, Dialog::ExecResult result) { + auto button = Button::construct(button_container); + button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); + button->set_preferred_size(0, 20); + button->set_text(label); + button->on_click = [=](auto&) { + dbg() << "GUI::MessageBox: '" << label << "' button clicked"; + done(result); }; - } + }; - if (should_include_cancel_button()) { - auto cancel_button = Button::construct(button_container); - cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); - cancel_button->set_preferred_size(0, 20); - cancel_button->set_text("Cancel"); - cancel_button->on_click = [this](auto&) { - dbgprintf("GMessageBox: Cancel button clicked\n"); - done(Dialog::ExecCancel); - }; - } + if (should_include_ok_button()) + add_button("OK", Dialog::ExecOK); + if (should_include_yes_button()) + add_button("Yes", Dialog::ExecYes); + if (should_include_no_button()) + add_button("No", Dialog::ExecNo); + if (should_include_cancel_button()) + add_button("Cancel", Dialog::ExecCancel); set_rect(x(), y(), text_width + icon_width + 80, 100); set_resizable(false); diff --git a/Libraries/LibGUI/MessageBox.h b/Libraries/LibGUI/MessageBox.h index d8f04a02bd..bcf7232774 100644 --- a/Libraries/LibGUI/MessageBox.h +++ b/Libraries/LibGUI/MessageBox.h @@ -43,6 +43,8 @@ public: enum class InputType { OK, OKCancel, + YesNo, + YesNoCancel, }; virtual ~MessageBox() override; @@ -54,6 +56,8 @@ private: bool should_include_ok_button() const; bool should_include_cancel_button() const; + bool should_include_yes_button() const; + bool should_include_no_button() const; void build(); RefPtr icon() const;