aboutsummaryrefslogtreecommitdiff
path: root/core/src/Utils/Dialog/Dialog_linux.cpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-03-27 23:01:07 -0700
committerrtk0c <[email protected]>2021-03-27 23:01:07 -0700
commit442d2d75d71bbc057e667edc301a79fa1cc813be (patch)
treeb5d1e5068a4d481bc6bcd72dca851ac7a85bf7e4 /core/src/Utils/Dialog/Dialog_linux.cpp
Initial setup
Diffstat (limited to 'core/src/Utils/Dialog/Dialog_linux.cpp')
-rw-r--r--core/src/Utils/Dialog/Dialog_linux.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/core/src/Utils/Dialog/Dialog_linux.cpp b/core/src/Utils/Dialog/Dialog_linux.cpp
new file mode 100644
index 0000000..11c3cee
--- /dev/null
+++ b/core/src/Utils/Dialog/Dialog_linux.cpp
@@ -0,0 +1,83 @@
+// Adapted from https://github.com/aaronmjacobs/Boxer/blob/master/src/boxer_linux.cpp
+#include "Dialog.hpp"
+
+#include <gtk/gtk.h>
+
+namespace Dialog {
+namespace {
+
+ GtkMessageType GetMessageType(Style style) {
+ switch (style) {
+ case Style::Info:
+ return GTK_MESSAGE_INFO;
+ case Style::Warning:
+ return GTK_MESSAGE_WARNING;
+ case Style::Error:
+ return GTK_MESSAGE_ERROR;
+ case Style::Question:
+ return GTK_MESSAGE_QUESTION;
+ default:
+ return GTK_MESSAGE_INFO;
+ }
+ }
+
+ GtkButtonsType GetButtonsType(Buttons buttons) {
+ switch (buttons) {
+ case Buttons::OK:
+ return GTK_BUTTONS_OK;
+ case Buttons::OKCancel:
+ return GTK_BUTTONS_OK_CANCEL;
+ case Buttons::YesNo:
+ return GTK_BUTTONS_YES_NO;
+ case Buttons::Quit:
+ return GTK_BUTTONS_CLOSE;
+ default:
+ return GTK_BUTTONS_OK;
+ }
+ }
+
+ Selection getSelection(gint response) {
+ switch (response) {
+ case GTK_RESPONSE_OK:
+ return Selection::OK;
+ case GTK_RESPONSE_CANCEL:
+ return Selection::Cancel;
+ case GTK_RESPONSE_YES:
+ return Selection::Yes;
+ case GTK_RESPONSE_NO:
+ return Selection::No;
+ case GTK_RESPONSE_CLOSE:
+ return Selection::Quit;
+ default:
+ return Selection::None;
+ }
+ }
+
+} // namespace
+
+Selection Show(const char* message, const char* title, Style style, Buttons buttons) {
+ if (!gtk_init_check(0, nullptr)) {
+ return Selection::Error;
+ }
+
+ // Create a parent window to stop gtk_dialog_run from complaining
+ GtkWidget* parent = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+
+ GtkWidget* dialog = gtk_message_dialog_new(GTK_WINDOW(parent),
+ GTK_DIALOG_MODAL,
+ GetMessageType(style),
+ GetButtonsType(buttons),
+ "%s",
+ message);
+ gtk_window_set_title(GTK_WINDOW(dialog), title);
+ Selection selection = getSelection(gtk_dialog_run(GTK_DIALOG(dialog)));
+
+ gtk_widget_destroy(GTK_WIDGET(dialog));
+ gtk_widget_destroy(GTK_WIDGET(parent));
+ while (g_main_context_iteration(nullptr, false)) {
+ // Do nothing
+ }
+
+ return selection;
+}
+} // namespace Dialog