summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-07-16 21:32:10 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-07-16 21:41:13 +0200
commita17fbd98e735e66a559f71583987b507aea483ba (patch)
treeb716bfcad32045c640eaa5f9e179983a2ed233f0
parent62335c5f0c9b22ee99dee24d62f0ab8391f11098 (diff)
downloadserenity-a17fbd98e735e66a559f71583987b507aea483ba.zip
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.
-rw-r--r--Applications/FileManager/main.cpp2
-rw-r--r--Applications/PaintBrush/main.cpp2
-rw-r--r--Applications/SystemDialog/main.cpp2
-rw-r--r--Applications/TextEditor/TextEditorWidget.cpp8
-rw-r--r--DevTools/VisualBuilder/VBForm.cpp6
-rw-r--r--Libraries/LibGUI/GFilePicker.cpp6
-rw-r--r--Libraries/LibGUI/GMessageBox.cpp51
-rw-r--r--Libraries/LibGUI/GMessageBox.h12
8 files changed, 63 insertions, 26 deletions
diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp
index 321c4f79dd..d1cf93c453 100644
--- a/Applications/FileManager/main.cpp
+++ b/Applications/FileManager/main.cpp
@@ -97,7 +97,7 @@ int main(int argc, char** argv)
input_box.text_value().characters()));
int rc = mkdir(new_dir_path.characters(), 0777);
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 {
directory_view->refresh();
}
diff --git a/Applications/PaintBrush/main.cpp b/Applications/PaintBrush/main.cpp
index 6eedffd752..c5637da19f 100644
--- a/Applications/PaintBrush/main.cpp
+++ b/Applications/PaintBrush/main.cpp
@@ -50,7 +50,7 @@ int main(int argc, char** argv)
auto filename = picker.selected_file().string();
auto bitmap = load_png(filename);
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();
return;
}
diff --git a/Applications/SystemDialog/main.cpp b/Applications/SystemDialog/main.cpp
index f5adbb09c0..db63da83fe 100644
--- a/Applications/SystemDialog/main.cpp
+++ b/Applications/SystemDialog/main.cpp
@@ -29,7 +29,7 @@ int run_shutdown_dialog(int argc, char** 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();
if (result == GMessageBox::ExecOK) {
diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp
index 7eb25ecfad..43feb8a110 100644
--- a/Applications/TextEditor/TextEditorWidget.cpp
+++ b/Applications/TextEditor/TextEditorWidget.cpp
@@ -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&) {
if (!m_path.is_empty()) {
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;
}
@@ -56,7 +56,7 @@ TextEditorWidget::TextEditorWidget()
return;
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;
}
@@ -135,10 +135,10 @@ void TextEditorWidget::open_sesame(const String& path)
CFile file(path);
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()));
m_editor->set_text(String::copy(file.read_all()));
m_path = path;
-} \ No newline at end of file
+}
diff --git a/DevTools/VisualBuilder/VBForm.cpp b/DevTools/VisualBuilder/VBForm.cpp
index bb095d6ca0..6cec82cbbc 100644
--- a/DevTools/VisualBuilder/VBForm.cpp
+++ b/DevTools/VisualBuilder/VBForm.cpp
@@ -294,7 +294,7 @@ void VBForm::load_from_file(const String& path)
{
CFile file(path);
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;
}
@@ -302,7 +302,7 @@ void VBForm::load_from_file(const String& path)
auto form_json = JsonValue::from_string(file_contents);
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;
}
@@ -329,7 +329,7 @@ void VBForm::write_to_file(const String& path)
{
CFile file(path);
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;
}
diff --git a/Libraries/LibGUI/GFilePicker.cpp b/Libraries/LibGUI/GFilePicker.cpp
index 0d197ad81d..9fe7c4db1a 100644
--- a/Libraries/LibGUI/GFilePicker.cpp
+++ b/Libraries/LibGUI/GFilePicker.cpp
@@ -40,7 +40,7 @@ Optional<String> GFilePicker::get_save_filepath()
if (GFilePicker::file_exists(file_path)) {
//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;
}
@@ -110,7 +110,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& path, CObject* parent)
.string();
int rc = mkdir(new_dir_path.characters(), 0777);
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 {
m_model->update();
}
@@ -244,4 +244,4 @@ bool GFilePicker::file_exists(const StringView& path)
return true;
}
return false;
-} \ No newline at end of file
+}
diff --git a/Libraries/LibGUI/GMessageBox.cpp b/Libraries/LibGUI/GMessageBox.cpp
index 33fa96b3a5..92959300d9 100644
--- a/Libraries/LibGUI/GMessageBox.cpp
+++ b/Libraries/LibGUI/GMessageBox.cpp
@@ -4,16 +4,17 @@
#include <LibGUI/GMessageBox.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();
}
-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)
, m_text(text)
, m_type(type)
+ , m_input_type(input_type)
{
set_title(title);
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()
{
auto* widget = new GWidget;
@@ -69,14 +80,32 @@ void GMessageBox::build()
label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
label->set_preferred_size({ text_width, 16 });
- auto* button = new GButton(widget);
- button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
- button->set_preferred_size({ 100, 20 });
- button->set_text("OK");
- button->on_click = [this](auto&) {
- dbgprintf("GMessageBox: OK button clicked\n");
- done(0);
- };
+ auto* button_container = new GWidget(widget);
+ button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
+ button_container->layout()->set_spacing(5);
+ button_container->layout()->set_margins({ 15, 0, 15, 0 });
+
+ if (should_include_ok_button()) {
+ 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_resizable(false);
diff --git a/Libraries/LibGUI/GMessageBox.h b/Libraries/LibGUI/GMessageBox.h
index 3b7a0f9e19..0c7938ef14 100644
--- a/Libraries/LibGUI/GMessageBox.h
+++ b/Libraries/LibGUI/GMessageBox.h
@@ -11,17 +11,25 @@ public:
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;
- 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"; }
private:
+ bool should_include_ok_button() const;
+ bool should_include_cancel_button() const;
void build();
RefPtr<GraphicsBitmap> icon() const;
String m_text;
Type m_type { Type::None };
+ InputType m_input_type { InputType::OK };
};