Skip to content

Commit

Permalink
LibWeb: Require parent window argument for MessageBox
Browse files Browse the repository at this point in the history
Since the vast majority of message boxes should be modal, require
the parent window to be passed in, which can be nullptr for the
rare case that they don't. By it being the first argument, the
default arguments also don't need to be explicitly stated in most
cases, and it encourages passing in a parent window handle.

Fix up several message boxes that should have been modal.
  • Loading branch information
tomuta authored and awesomekling committed Jul 16, 2020
1 parent 6568765 commit 27bd2ea
Show file tree
Hide file tree
Showing 30 changed files with 109 additions and 135 deletions.
4 changes: 2 additions & 2 deletions Applications/Browser/DownloadWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ void DownloadWidget::did_finish(bool success, const ByteBuffer& payload, RefPtr<
m_cancel_button->update();

if (!success) {
GUI::MessageBox::show(String::format("Download failed for some reason"), "Download failed", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
GUI::MessageBox::show(window(), String::format("Download failed for some reason"), "Download failed", GUI::MessageBox::Type::Error);
window()->close();
return;
}

auto file_or_error = Core::File::open(m_destination_path, Core::IODevice::WriteOnly);
if (file_or_error.is_error()) {
GUI::MessageBox::show(String::format("Cannot open %s for writing", m_destination_path.characters()), "Download failed", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
GUI::MessageBox::show(window(), String::format("Cannot open %s for writing", m_destination_path.characters()), "Download failed", GUI::MessageBox::Type::Error);
window()->close();
return;
}
Expand Down
4 changes: 2 additions & 2 deletions Applications/DisplaySettings/DisplaySettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ void DisplaySettingsWidget::send_settings_to_window_server()
{
auto result = GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetResolution>(m_monitor_widget->desktop_resolution());
if (!result->success()) {
GUI::MessageBox::show(String::format("Reverting to resolution %dx%d", result->resolution().width(), result->resolution().height()),
"Unable to set resolution", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
GUI::MessageBox::show(root_widget()->window(), String::format("Reverting to resolution %dx%d", result->resolution().width(), result->resolution().height()),
"Unable to set resolution", GUI::MessageBox::Type::Error);
}

if (!m_monitor_widget->wallpaper().is_empty()) {
Expand Down
2 changes: 1 addition & 1 deletion Applications/FileManager/DirectoryView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void DirectoryView::handle_activation(const GUI::ModelIndex& index)
on_launch(url, *default_launcher);
} else {
auto error_message = String::format("Could not open %s", path.characters());
GUI::MessageBox::show(error_message, "File Manager", GUI::MessageBox::Type::Error);
GUI::MessageBox::show(window(), error_message, "File Manager", GUI::MessageBox::Type::Error);
}
}

Expand Down
6 changes: 3 additions & 3 deletions Applications/FileManager/PropertiesDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ bool PropertiesDialog::apply_changes()
String new_file = make_full_path(new_name).characters();

if (GUI::FilePicker::file_exists(new_file)) {
GUI::MessageBox::show(String::format("A file \"%s\" already exists!", new_name.characters()), "Error", GUI::MessageBox::Type::Error);
GUI::MessageBox::show(this, String::format("A file \"%s\" already exists!", new_name.characters()), "Error", GUI::MessageBox::Type::Error);
return false;
}

if (rename(make_full_path(m_name).characters(), new_file.characters())) {
GUI::MessageBox::show(String::format("Could not rename file: %s!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
GUI::MessageBox::show(this, String::format("Could not rename file: %s!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
return false;
}

Expand All @@ -214,7 +214,7 @@ bool PropertiesDialog::apply_changes()

if (m_permissions_dirty) {
if (chmod(make_full_path(m_name).characters(), m_mode)) {
GUI::MessageBox::show(String::format("Could not update permissions: %s!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
GUI::MessageBox::show(this, String::format("Could not update permissions: %s!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
return false;
}

Expand Down
45 changes: 19 additions & 26 deletions Applications/FileManager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ int run_in_desktop_mode(RefPtr<Core::ConfigFile> config, String initial_location
input_box->text_value().characters()));
int rc = mkdir(new_dir_path.characters(), 0777);
if (rc < 0) {
GUI::MessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
GUI::MessageBox::show(window, String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
}
}
});
Expand All @@ -187,16 +187,16 @@ int run_in_desktop_mode(RefPtr<Core::ConfigFile> config, String initial_location
struct stat st;
int rc = stat(new_file_path.characters(), &st);
if ((rc < 0 && errno != ENOENT)) {
GUI::MessageBox::show(String::format("stat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
GUI::MessageBox::show(window, String::format("stat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
return;
}
if (rc == 0) {
GUI::MessageBox::show(String::format("%s: Already exists", new_file_path.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
GUI::MessageBox::show(window, String::format("%s: Already exists", new_file_path.characters()), "Error", GUI::MessageBox::Type::Error);
return;
}
int fd = creat(new_file_path.characters(), 0666);
if (fd < 0) {
GUI::MessageBox::show(String::format("creat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
GUI::MessageBox::show(window, String::format("creat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
return;
}
rc = close(fd);
Expand Down Expand Up @@ -330,7 +330,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
input_box->text_value().characters()));
int rc = mkdir(new_dir_path.characters(), 0777);
if (rc < 0) {
GUI::MessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
GUI::MessageBox::show(window, String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
} else {
refresh_tree_view();
}
Expand All @@ -347,16 +347,16 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
struct stat st;
int rc = stat(new_file_path.characters(), &st);
if ((rc < 0 && errno != ENOENT)) {
GUI::MessageBox::show(String::format("stat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
GUI::MessageBox::show(window, String::format("stat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
return;
}
if (rc == 0) {
GUI::MessageBox::show(String::format("%s: Already exists", new_file_path.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
GUI::MessageBox::show(window, String::format("%s: Already exists", new_file_path.characters()), "Error", GUI::MessageBox::Type::Error);
return;
}
int fd = creat(new_file_path.characters(), 0666);
if (fd < 0) {
GUI::MessageBox::show(String::format("creat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
GUI::MessageBox::show(window, String::format("creat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
return;
}
rc = close(fd);
Expand Down Expand Up @@ -518,7 +518,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
auto new_path = String::format("%s/%s", target_directory.characters(), url.basename().characters());
if (!FileUtils::copy_file_or_directory(url.path(), new_path)) {
auto error_message = String::format("Could not paste %s.", url.path().characters());
GUI::MessageBox::show(error_message, "File Manager", GUI::MessageBox::Type::Error);
GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error);
} else {
refresh_tree_view();
}
Expand All @@ -542,25 +542,22 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
}

if (confirm == ConfirmBeforeDelete::Yes) {
auto result = GUI::MessageBox::show(
auto result = GUI::MessageBox::show(window,
message,
"Confirm deletion",
GUI::MessageBox::Type::Warning,
GUI::MessageBox::InputType::OKCancel,
window);
GUI::MessageBox::InputType::OKCancel);
if (result == GUI::MessageBox::ExecCancel)
return;
}

for (auto& path : paths) {
struct stat st;
if (lstat(path.characters(), &st)) {
GUI::MessageBox::show(
GUI::MessageBox::show(window,
String::format("lstat(%s) failed: %s", path.characters(), strerror(errno)),
"Delete failed",
GUI::MessageBox::Type::Error,
GUI::MessageBox::InputType::OK,
window);
GUI::MessageBox::Type::Error);
break;
} else {
refresh_tree_view();
Expand All @@ -571,24 +568,20 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
int error = FileUtils::delete_directory(path, error_path);

if (error) {
GUI::MessageBox::show(
GUI::MessageBox::show(window,
String::format("Failed to delete directory \"%s\": %s", error_path.characters(), strerror(error)),
"Delete failed",
GUI::MessageBox::Type::Error,
GUI::MessageBox::InputType::OK,
window);
GUI::MessageBox::Type::Error);
break;
} else {
refresh_tree_view();
}
} else if (unlink(path.characters()) < 0) {
int saved_errno = errno;
GUI::MessageBox::show(
GUI::MessageBox::show(window,
String::format("unlink(%s) failed: %s", path.characters(), strerror(saved_errno)),
"Delete failed",
GUI::MessageBox::Type::Error,
GUI::MessageBox::InputType::OK,
window);
GUI::MessageBox::Type::Error);
break;
}
}
Expand Down Expand Up @@ -726,7 +719,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio

directory_view.on_error = [&](int, const char* error_string, bool quit) {
auto error_message = String::format("Could not read directory: %s", error_string);
GUI::MessageBox::show(error_message, "File Manager", GUI::MessageBox::Type::Error);
GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error);

if (quit)
exit(1);
Expand Down Expand Up @@ -880,7 +873,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
auto error_message = String::format("Could not copy %s into %s.",
url_to_copy.to_string().characters(),
new_path.characters());
GUI::MessageBox::show(error_message, "File Manager", GUI::MessageBox::Type::Error);
GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error);
} else {
refresh_tree_view();
}
Expand Down
2 changes: 1 addition & 1 deletion Applications/FontEditor/FontEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ bool FontEditorWidget::save_as(const String& path)
{
auto ret_val = m_edited_font->write_to_file(path);
if (!ret_val) {
GUI::MessageBox::show("The font file could not be saved.", "Save failed", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
GUI::MessageBox::show(window(), "The font file could not be saved.", "Save failed", GUI::MessageBox::Type::Error);
return false;
}
m_path = path;
Expand Down
4 changes: 2 additions & 2 deletions Applications/FontEditor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int main(int argc, char** argv)
edited_font = Gfx::Font::load_from_file(path)->clone();
if (!edited_font) {
String message = String::format("Couldn't load font: %s\n", path);
GUI::MessageBox::show(message, "Font Editor", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
GUI::MessageBox::show(nullptr, message, "Font Editor", GUI::MessageBox::Type::Error);
return 1;
}
}
Expand Down Expand Up @@ -99,7 +99,7 @@ int main(int argc, char** argv)
RefPtr<Gfx::Font> new_font = Gfx::Font::load_from_file(open_path.value())->clone();
if (!new_font) {
String message = String::format("Couldn't load font: %s\n", open_path.value().characters());
GUI::MessageBox::show(message, "Font Editor", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
GUI::MessageBox::show(window, message, "Font Editor", GUI::MessageBox::Type::Error);
return;
}

Expand Down
8 changes: 3 additions & 5 deletions Applications/Help/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int main(int argc, char* argv[])

if (!file->open(Core::IODevice::OpenMode::ReadOnly)) {
int saved_errno = errno;
GUI::MessageBox::show(strerror(saved_errno), "Failed to open man page", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
GUI::MessageBox::show(window, strerror(saved_errno), "Failed to open man page", GUI::MessageBox::Type::Error);
return;
}
auto buffer = file->read_all();
Expand Down Expand Up @@ -167,12 +167,10 @@ int main(int argc, char* argv[])

auto open_external = [&](auto& url) {
if (!Desktop::Launcher::open(url)) {
GUI::MessageBox::show(
GUI::MessageBox::show(window,
String::format("The link to '%s' could not be opened.", url.to_string().characters()),
"Failed to open link",
GUI::MessageBox::Type::Error,
GUI::MessageBox::InputType::OK,
window);
GUI::MessageBox::Type::Error);
}
};

Expand Down
15 changes: 6 additions & 9 deletions Applications/HexEditor/HexEditorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ HexEditorWidget::HexEditorWidget()

m_new_action = GUI::Action::create("New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GUI::Action&) {
if (m_document_dirty) {
auto save_document_first_box = GUI::MessageBox::construct("Save Document First?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel, window());
auto save_document_first_result = save_document_first_box->exec();

if (save_document_first_result != GUI::Dialog::ExecResult::ExecOK)
if (GUI::MessageBox::show(window(), "Save Document First?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel) != GUI::Dialog::ExecResult::ExecOK)
return;
m_save_action->activate();
}
Expand All @@ -89,7 +86,7 @@ HexEditorWidget::HexEditorWidget()
set_path(LexicalPath());
update_title();
} else {
GUI::MessageBox::show("Invalid file size entered.", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
GUI::MessageBox::show(window(), "Invalid file size entered.", "Error", GUI::MessageBox::Type::Error);
}
}
});
Expand All @@ -106,7 +103,7 @@ HexEditorWidget::HexEditorWidget()
m_save_action = GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](const GUI::Action&) {
if (!m_path.is_empty()) {
if (!m_editor->write_to_file(m_path)) {
GUI::MessageBox::show("Unable to save file.\n", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
} else {
m_document_dirty = false;
update_title();
Expand All @@ -123,7 +120,7 @@ HexEditorWidget::HexEditorWidget()
return;

if (!m_editor->write_to_file(save_path.value())) {
GUI::MessageBox::show("Unable to save file.\n", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
return;
}

Expand Down Expand Up @@ -230,7 +227,7 @@ void HexEditorWidget::open_file(const String& path)
{
auto file = Core::File::construct(path);
if (!file->open(Core::IODevice::ReadOnly)) {
GUI::MessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
GUI::MessageBox::show(window(), String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
return;
}

Expand All @@ -243,6 +240,6 @@ bool HexEditorWidget::request_close()
{
if (!m_document_dirty)
return true;
auto result = GUI::MessageBox::show("The file has been modified. Quit without saving?", "Quit without saving?", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel, window());
auto result = GUI::MessageBox::show(window(), "The file has been modified. Quit without saving?", "Quit without saving?", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
return result == GUI::MessageBox::ExecOK;
}
4 changes: 2 additions & 2 deletions Applications/KeyboardMapper/KeyboardMapperWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void KeyboardMapperWidget::save_to_file(const StringView& file_name)
sb.append(" for write. Error: ");
sb.append(file->error_string());

GUI::MessageBox::show(sb.to_string(), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
return;
}

Expand All @@ -216,7 +216,7 @@ void KeyboardMapperWidget::save_to_file(const StringView& file_name)
sb.append("Unable to save file. Error: ");
sb.append(strerror(error_number));

GUI::MessageBox::show(sb.to_string(), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
return;
}

Expand Down
Loading

0 comments on commit 27bd2ea

Please sign in to comment.