Skip to content

Commit

Permalink
Make XojMsgBox::showErrorAndQuit() gtk4-compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
bhennion committed Apr 6, 2024
1 parent ff95c3a commit 7f0af1e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
25 changes: 12 additions & 13 deletions src/util/XojMsgBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,22 @@ void XojMsgBox::askQuestionWithMarkup(GtkWindow* win, std::string_view maintext,
}

void XojMsgBox::showErrorAndQuit(std::string& msg, int exitCode) {
/*
* This should be used for fatal errors, typically in early GUI startup (missing UI main file or so).
*
* Todo(gtk4): Figure out how to use a non-blocking dialog that would survive past the `exit` call.
* Putting the `exit()` in a callback is no good, as normal execution would continue in the background, most likely
* leading to a SegFault: that would again kill the dialog.
*/
GtkWidget* dialog = gtk_message_dialog_new_with_markup(defaultWindow, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK, nullptr);

auto formattedMsg = xoj::util::OwnedCString::assumeOwnership(g_markup_escape_text(msg.c_str(), -1));
gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog), formattedMsg.get());
if (defaultWindow != nullptr) {
gtk_window_set_transient_for(GTK_WINDOW(dialog), defaultWindow);
gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog), msg.c_str());

// We use a hack to ensure the app does not exit until the user has read the error message and closed the popup
bool done = false;

xoj::popup::PopupWindowWrapper<XojMsgBox> popup(GTK_DIALOG(dialog), [&done](int) { done = true; });

popup.show(defaultWindow);

while (!done) {
// Let the main loop run so the popup pops up and can be interacted with
g_main_context_iteration(g_main_context_default(), true);
}
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);

exit(exitCode);
}
Expand Down
5 changes: 4 additions & 1 deletion src/util/include/util/XojMsgBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ class XojMsgBox final {
static void showMessageToUser(GtkWindow* win, const std::string& title, const std::string& msg,
GtkMessageType type);
static void showErrorToUser(GtkWindow* win, const std::string& msg);
static void showErrorAndQuit(std::string& msg, int exitCode);

/// @brief This should be used for fatal errors, typically in early GUI startup (missing UI main file or so).
[[noreturn]] static void showErrorAndQuit(std::string& msg, int exitCode);

static void showPluginMessage(const std::string& pluginName, const std::string& msg, bool error = false);
[[deprecated("Will be removed when porting to gtk4")]] static int askPluginQuestion(
const std::string& pluginName, const std::string& msg, const std::vector<Button>& buttons,
Expand Down

0 comments on commit 7f0af1e

Please sign in to comment.