mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-29 19:10:26 +00:00
LibGUI: Add input types to GMessageBox.
Currently the two available input types are: - GMessageBox::InputType::OK (default) - GMessageBox::InputType::OKCancel Based on your choice, GMessageBox::exec() will return ExecOK or ExecCancel.
This commit is contained in:
parent
62335c5f0c
commit
a17fbd98e7
Notes:
sideshowbarker
2024-07-19 13:13:55 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/a17fbd98e73
8 changed files with 63 additions and 26 deletions
|
@ -97,7 +97,7 @@ int main(int argc, char** argv)
|
||||||
input_box.text_value().characters()));
|
input_box.text_value().characters()));
|
||||||
int rc = mkdir(new_dir_path.characters(), 0777);
|
int rc = mkdir(new_dir_path.characters(), 0777);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, window);
|
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
|
||||||
} else {
|
} else {
|
||||||
directory_view->refresh();
|
directory_view->refresh();
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ int main(int argc, char** argv)
|
||||||
auto filename = picker.selected_file().string();
|
auto filename = picker.selected_file().string();
|
||||||
auto bitmap = load_png(filename);
|
auto bitmap = load_png(filename);
|
||||||
if (!bitmap) {
|
if (!bitmap) {
|
||||||
GMessageBox msgbox(String::format("Failed to load '%s'", filename.characters()), "Open failed", GMessageBox::Type::Error, window);
|
GMessageBox msgbox(String::format("Failed to load '%s'", filename.characters()), "Open failed", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
|
||||||
msgbox.exec();
|
msgbox.exec();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ int run_shutdown_dialog(int argc, char** argv)
|
||||||
GApplication app(argc, argv);
|
GApplication app(argc, argv);
|
||||||
|
|
||||||
{
|
{
|
||||||
GMessageBox box("Shut down Serenity?", "Confirm Shutdown", GMessageBox::Type::Warning, GMessageBox::InputType::OkCancel);
|
GMessageBox box("Shut down Serenity?", "Confirm Shutdown", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel);
|
||||||
auto result = box.exec();
|
auto result = box.exec();
|
||||||
|
|
||||||
if (result == GMessageBox::ExecOK) {
|
if (result == GMessageBox::ExecOK) {
|
||||||
|
|
|
@ -46,7 +46,7 @@ TextEditorWidget::TextEditorWidget()
|
||||||
auto save_action = GAction::create("Save document", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
|
auto save_action = GAction::create("Save document", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
|
||||||
if (!m_path.is_empty()) {
|
if (!m_path.is_empty()) {
|
||||||
if (!m_editor->write_to_file(m_path))
|
if (!m_editor->write_to_file(m_path))
|
||||||
GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, window());
|
GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ TextEditorWidget::TextEditorWidget()
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!m_editor->write_to_file(save_name.value())) {
|
if (!m_editor->write_to_file(save_name.value())) {
|
||||||
GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, window());
|
GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +135,7 @@ void TextEditorWidget::open_sesame(const String& path)
|
||||||
CFile file(path);
|
CFile file(path);
|
||||||
|
|
||||||
if (!file.open(CIODevice::ReadOnly)) {
|
if (!file.open(CIODevice::ReadOnly)) {
|
||||||
GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, window());
|
GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||||
}
|
}
|
||||||
|
|
||||||
window()->set_title(String::format("Text Editor: %s", path.characters()));
|
window()->set_title(String::format("Text Editor: %s", path.characters()));
|
||||||
|
|
|
@ -294,7 +294,7 @@ void VBForm::load_from_file(const String& path)
|
||||||
{
|
{
|
||||||
CFile file(path);
|
CFile file(path);
|
||||||
if (!file.open(CIODevice::ReadOnly)) {
|
if (!file.open(CIODevice::ReadOnly)) {
|
||||||
GMessageBox::show(String::format("Could not open '%s' for reading", path.characters()), "Error", GMessageBox::Type::Error, window());
|
GMessageBox::show(String::format("Could not open '%s' for reading", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,7 +302,7 @@ void VBForm::load_from_file(const String& path)
|
||||||
auto form_json = JsonValue::from_string(file_contents);
|
auto form_json = JsonValue::from_string(file_contents);
|
||||||
|
|
||||||
if (!form_json.is_object()) {
|
if (!form_json.is_object()) {
|
||||||
GMessageBox::show(String::format("Could not parse '%s'", path.characters()), "Error", GMessageBox::Type::Error, window());
|
GMessageBox::show(String::format("Could not parse '%s'", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -329,7 +329,7 @@ void VBForm::write_to_file(const String& path)
|
||||||
{
|
{
|
||||||
CFile file(path);
|
CFile file(path);
|
||||||
if (!file.open(CIODevice::WriteOnly)) {
|
if (!file.open(CIODevice::WriteOnly)) {
|
||||||
GMessageBox::show(String::format("Could not open '%s' for writing", path.characters()), "Error", GMessageBox::Type::Error, window());
|
GMessageBox::show(String::format("Could not open '%s' for writing", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ Optional<String> GFilePicker::get_save_filepath()
|
||||||
|
|
||||||
if (GFilePicker::file_exists(file_path)) {
|
if (GFilePicker::file_exists(file_path)) {
|
||||||
//TODO: Add Yes, No Messagebox to give the user a proper option
|
//TODO: Add Yes, No Messagebox to give the user a proper option
|
||||||
GMessageBox::show("File already exists: Overwrite?\n", "Warning", GMessageBox::Type::Warning, &picker);
|
GMessageBox::show("File already exists: Overwrite?\n", "Warning", GMessageBox::Type::Warning, GMessageBox::InputType::OK, &picker);
|
||||||
return file_path;
|
return file_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +110,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& path, CObject* parent)
|
||||||
.string();
|
.string();
|
||||||
int rc = mkdir(new_dir_path.characters(), 0777);
|
int rc = mkdir(new_dir_path.characters(), 0777);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, this);
|
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, this);
|
||||||
} else {
|
} else {
|
||||||
m_model->update();
|
m_model->update();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,16 +4,17 @@
|
||||||
#include <LibGUI/GMessageBox.h>
|
#include <LibGUI/GMessageBox.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void GMessageBox::show(const StringView& text, const StringView& title, Type type, CObject* parent)
|
void GMessageBox::show(const StringView& text, const StringView& title, Type type, InputType input_type, CObject* parent)
|
||||||
{
|
{
|
||||||
GMessageBox box(text, title, type, parent);
|
GMessageBox box(text, title, type, input_type, parent);
|
||||||
box.exec();
|
box.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
GMessageBox::GMessageBox(const StringView& text, const StringView& title, Type type, CObject* parent)
|
GMessageBox::GMessageBox(const StringView& text, const StringView& title, Type type, InputType input_type, CObject* parent)
|
||||||
: GDialog(parent)
|
: GDialog(parent)
|
||||||
, m_text(text)
|
, m_text(text)
|
||||||
, m_type(type)
|
, m_type(type)
|
||||||
|
, m_input_type(input_type)
|
||||||
{
|
{
|
||||||
set_title(title);
|
set_title(title);
|
||||||
build();
|
build();
|
||||||
|
@ -37,6 +38,16 @@ RefPtr<GraphicsBitmap> GMessageBox::icon() const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GMessageBox::should_include_ok_button() const
|
||||||
|
{
|
||||||
|
return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GMessageBox::should_include_cancel_button() const
|
||||||
|
{
|
||||||
|
return m_input_type == InputType::OKCancel;
|
||||||
|
}
|
||||||
|
|
||||||
void GMessageBox::build()
|
void GMessageBox::build()
|
||||||
{
|
{
|
||||||
auto* widget = new GWidget;
|
auto* widget = new GWidget;
|
||||||
|
@ -69,14 +80,32 @@ void GMessageBox::build()
|
||||||
label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
||||||
label->set_preferred_size({ text_width, 16 });
|
label->set_preferred_size({ text_width, 16 });
|
||||||
|
|
||||||
auto* button = new GButton(widget);
|
auto* button_container = new GWidget(widget);
|
||||||
button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
|
button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
|
||||||
button->set_preferred_size({ 100, 20 });
|
button_container->layout()->set_spacing(5);
|
||||||
button->set_text("OK");
|
button_container->layout()->set_margins({ 15, 0, 15, 0 });
|
||||||
button->on_click = [this](auto&) {
|
|
||||||
dbgprintf("GMessageBox: OK button clicked\n");
|
if (should_include_ok_button()) {
|
||||||
done(0);
|
auto* ok_button = new GButton(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(GDialog::ExecOK);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (should_include_cancel_button()) {
|
||||||
|
auto* cancel_button = new GButton(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(GDialog::ExecCancel);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
set_rect(x(), y(), text_width + icon_width + 80, 100);
|
set_rect(x(), y(), text_width + icon_width + 80, 100);
|
||||||
set_resizable(false);
|
set_resizable(false);
|
||||||
|
|
|
@ -11,17 +11,25 @@ public:
|
||||||
Error,
|
Error,
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit GMessageBox(const StringView& text, const StringView& title, Type type = Type::None, CObject* parent = nullptr);
|
enum class InputType {
|
||||||
|
OK,
|
||||||
|
OKCancel,
|
||||||
|
};
|
||||||
|
|
||||||
|
explicit GMessageBox(const StringView& text, const StringView& title, Type type = Type::None, InputType = InputType::OK, CObject* parent = nullptr);
|
||||||
virtual ~GMessageBox() override;
|
virtual ~GMessageBox() override;
|
||||||
|
|
||||||
static void show(const StringView& text, const StringView& title, Type type = Type::None, CObject* parent = nullptr);
|
static void show(const StringView& text, const StringView& title, Type type = Type::None, InputType = InputType::OK, CObject* parent = nullptr);
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "GMessageBox"; }
|
virtual const char* class_name() const override { return "GMessageBox"; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
bool should_include_ok_button() const;
|
||||||
|
bool should_include_cancel_button() const;
|
||||||
void build();
|
void build();
|
||||||
RefPtr<GraphicsBitmap> icon() const;
|
RefPtr<GraphicsBitmap> icon() const;
|
||||||
|
|
||||||
String m_text;
|
String m_text;
|
||||||
Type m_type { Type::None };
|
Type m_type { Type::None };
|
||||||
|
InputType m_input_type { InputType::OK };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue