From c88d5a9a010a07904a828e06c9de61e4801b3438 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Mon, 23 Oct 2023 00:55:53 +0300
Subject: [PATCH 50/50] gtk4x: Reimplement Quit/Leave dialogs as
 GtkAlertDialogs

See osdn #48894

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-gtk-5.0/gui_main.c  | 34 +++++++++++++++++-----------------
 client/gui-gtk-5.0/gui_stuff.c |  3 ++-
 client/gui-gtk-5.0/menu.c      | 26 +++++++++++++++-----------
 3 files changed, 34 insertions(+), 29 deletions(-)

diff --git a/client/gui-gtk-5.0/gui_main.c b/client/gui-gtk-5.0/gui_main.c
index 8885838208..edea92f8b1 100644
--- a/client/gui-gtk-5.0/gui_main.c
+++ b/client/gui-gtk-5.0/gui_main.c
@@ -181,6 +181,7 @@ static bool audio_paused = FALSE;
 static bool client_focus = TRUE;
 
 static GtkApplication *fc_app;
+static GtkAlertDialog *quit_dialog = NULL;
 
 static struct video_mode vmode = { -1, -1 };
 
@@ -2392,16 +2393,21 @@ void remove_net_input(void)
   This is the response callback for the dialog with the message:
   Are you sure you want to quit?
 **************************************************************************/
-static void quit_dialog_response(GtkWidget *dialog, gint response)
+static void quit_dialog_response(GObject *dialog, GAsyncResult *result,
+                                 gpointer data)
 {
-  gtk_window_destroy(GTK_WINDOW(dialog));
-  if (response == GTK_RESPONSE_YES) {
+  int button = gtk_alert_dialog_choose_finish(GTK_ALERT_DIALOG(dialog),
+                                              result, NULL);
+
+  if (button == 0) {
     start_quitting();
     if (client.conn.used) {
       disconnect_from_server(FALSE);
     }
     quit_gtk_main();
   }
+
+  quit_dialog = NULL;
 }
 
 /**********************************************************************//**
@@ -2421,23 +2427,17 @@ void quit_gtk_main(void)
 **************************************************************************/
 void popup_quit_dialog(void)
 {
-  static GtkWidget *dialog;
+  if (quit_dialog == NULL) {
+    const char *buttons[] = { _("Yes"), _("No"), NULL };
 
-  if (!dialog) {
-    dialog = gtk_message_dialog_new(NULL,
-                                    0,
-                                    GTK_MESSAGE_WARNING,
-                                    GTK_BUTTONS_YES_NO,
-                                    _("Are you sure you want to quit?"));
-    setup_dialog(dialog, toplevel);
+    quit_dialog = gtk_alert_dialog_new(_("Are you sure you want to quit?"));
+    gtk_alert_dialog_set_buttons(GTK_ALERT_DIALOG(quit_dialog), buttons);
+    setup_dialog(GTK_WIDGET(quit_dialog), toplevel);
 
-    g_signal_connect(dialog, "response",
-                     G_CALLBACK(quit_dialog_response), NULL);
-    g_signal_connect(dialog, "destroy",
-                     G_CALLBACK(widget_destroyed), &dialog);
+    gtk_alert_dialog_choose(GTK_ALERT_DIALOG(quit_dialog),
+                            GTK_WINDOW(toplevel), NULL,
+                            quit_dialog_response, NULL);
   }
-
-  gtk_window_present(GTK_WINDOW(dialog));
 }
 
 /**********************************************************************//**
diff --git a/client/gui-gtk-5.0/gui_stuff.c b/client/gui-gtk-5.0/gui_stuff.c
index 250d9cf4e3..c7f3c24593 100644
--- a/client/gui-gtk-5.0/gui_stuff.c
+++ b/client/gui-gtk-5.0/gui_stuff.c
@@ -263,7 +263,8 @@ static void close_callback(GtkDialog *dialog, gpointer data)
 **************************************************************************/
 void setup_dialog(GtkWidget *shell, GtkWidget *parent)
 {
-  if (GUI_GTK_OPTION(dialogs_on_top) || GUI_GTK_OPTION(fullscreen)) {
+  if (GTK_IS_WINDOW(shell)
+      && (GUI_GTK_OPTION(dialogs_on_top) || GUI_GTK_OPTION(fullscreen))) {
     gtk_window_set_transient_for(GTK_WINDOW(shell),
                                  GTK_WINDOW(parent));
   }
diff --git a/client/gui-gtk-5.0/menu.c b/client/gui-gtk-5.0/menu.c
index b25cecf4a4..ba71da1ec8 100644
--- a/client/gui-gtk-5.0/menu.c
+++ b/client/gui-gtk-5.0/menu.c
@@ -1292,10 +1292,13 @@ static void save_mapimg_as_callback(GSimpleAction *action, GVariant *parameter,
   This is the response callback for the dialog with the message:
   Leaving a local game will end it!
 ****************************************************************************/
-static void leave_local_game_response(GtkWidget *dialog, gint response)
+static void leave_local_game_response(GObject *dialog, GAsyncResult *result,
+                                      gpointer data)
 {
-  gtk_window_destroy(GTK_WINDOW(dialog));
-  if (response == GTK_RESPONSE_OK) {
+  int button = gtk_alert_dialog_choose_finish(GTK_ALERT_DIALOG(dialog),
+                                              result, NULL);
+
+  if (button == 0) {
     /* It might be killed already */
     if (client.conn.used) {
       /* It will also kill the server */
@@ -1312,14 +1315,15 @@ static void leave_callback(GSimpleAction *action,
                            gpointer data)
 {
   if (is_server_running()) {
-    GtkWidget *dialog
-      = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING,
-                               GTK_BUTTONS_OK_CANCEL,
-                               _("Leaving a local game will end it!"));
-    setup_dialog(dialog, toplevel);
-    g_signal_connect(dialog, "response",
-                     G_CALLBACK(leave_local_game_response), NULL);
-    gtk_window_present(GTK_WINDOW(dialog));
+    const char *buttons[] = { _("Leave"), _("Cancel"), NULL };
+    GtkAlertDialog *dialog
+      = gtk_alert_dialog_new(_("Leaving a local game will end it!"));
+
+    gtk_alert_dialog_set_buttons(GTK_ALERT_DIALOG(dialog), buttons);
+    setup_dialog(GTK_WIDGET(dialog), toplevel);
+    gtk_alert_dialog_choose(GTK_ALERT_DIALOG(dialog),
+                            GTK_WINDOW(toplevel), NULL,
+                            leave_local_game_response, NULL);
   } else {
     disconnect_from_server(TRUE);
   }
-- 
2.42.0