LibGUI: Add Yes/No and Yes/No/Cancel MessageBoxes

This commit is contained in:
thatlittlegit 2020-02-16 23:46:02 -05:00 committed by Andreas Kling
parent 48f7c28a5c
commit 4755f355c6
Notes: sideshowbarker 2024-07-19 17:40:16 +09:00
3 changed files with 35 additions and 21 deletions

View file

@ -36,7 +36,9 @@ public:
enum ExecResult {
ExecOK = 0,
ExecCancel = 1,
ExecAborted = 2
ExecAborted = 2,
ExecYes = 3,
ExecNo = 4,
};
virtual ~Dialog() override;

View file

@ -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);

View file

@ -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<Gfx::Bitmap> icon() const;