[gtranslator/gtk4: 72/79] Fix save current document dialog when modified




commit 4d6003d63fdfcca01b86bf5399e010f3d38a0419
Author: Daniel GarcĂ­a Moreno <dani danigm net>
Date:   Wed Sep 28 18:27:05 2022 +0200

    Fix save current document dialog when modified

 src/gtr-actions-file.c | 68 +++++++++++++++++++++++++-------------------------
 src/gtr-actions.h      |  2 +-
 src/gtr-application.c  | 18 ++++++-------
 3 files changed, 43 insertions(+), 45 deletions(-)
---
diff --git a/src/gtr-actions-file.c b/src/gtr-actions-file.c
index 957c009e..fc33f807 100644
--- a/src/gtr-actions-file.c
+++ b/src/gtr-actions-file.c
@@ -171,21 +171,27 @@ gtr_file_chooser_cb (GtkNativeDialog * dialog, guint reply, gpointer user_data)
 }
 
 static void
-handle_dialog_response (GtkNativeDialog *dialog, gint response_id, GtrWindow *window ){
-  if (response_id == GTK_RESPONSE_YES)
-  {
-    gtr_save_current_file_dialog (NULL, window);
-  }
-  else if (response_id == GTK_RESPONSE_CANCEL)
-  {
-    //to be implemented
-  }
+handle_save_current_dialog_response (GtkDialog *dialog,
+                                     gint response_id,
+                                     void (*callback)(GtrWindow *))
+{
+  GtrWindow *window = gtr_application_get_active_window (GTR_APP);
+  switch (response_id)
+    {
+      case GTK_RESPONSE_YES:
+        gtr_save_current_file_dialog (NULL, window);
+      case GTK_RESPONSE_NO:
+        callback (window);
+        break;
+      default:
+        break;
+    }
+  gtk_window_destroy (GTK_WINDOW (dialog));
 }
 
-gboolean
-gtr_want_to_save_current_dialog (GtrWindow * window)
+void
+gtr_want_to_save_current_dialog (GtrWindow * window, void (*callback)(GtrWindow *))
 {
-  gint res = 0;
   GtrTab *tab;
   GtrPo *po;
 
@@ -221,35 +227,18 @@ gtr_want_to_save_current_dialog (GtrWindow * window)
                           _("Continue without saving"), GTK_RESPONSE_NO,
                           NULL);
 
-  //res = gtk_dialog_run (GTK_DIALOG (dialog));
-  gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
-  //gtk_widget_destroy (dialog);
-
-  // Below code can be removed as response signal is connected
-  /*if (res == GTK_RESPONSE_CANCEL)
-    return FALSE;
-
-  if (res == GTK_RESPONSE_YES)
-    gtr_save_current_file_dialog (NULL, window);*/
-  g_signal_connect(dialog, "response", G_CALLBACK(handle_dialog_response), window);
-
-  return TRUE;
+  gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
+  g_signal_connect (dialog, "response", G_CALLBACK (handle_save_current_dialog_response), callback);
+  gtk_window_present (GTK_WINDOW (dialog));
 }
 
 /*
  * The "Open file" dialog.
  */
-void
-gtr_open_file_dialog (GtrWindow * window)
+static void
+gtr_open_file_dialog_nocheck (GtrWindow *window)
 {
   GtkNativeDialog *dialog;
-  g_autoptr (GList) list = NULL;
-  list = get_modified_documents (window);
-  if (list != NULL)
-    {
-      if (!gtr_want_to_save_current_dialog (window))
-        return;
-    }
 
   dialog = gtr_file_chooser_new (GTK_WINDOW (window),
                                  FILESEL_OPEN,
@@ -260,6 +249,17 @@ gtr_open_file_dialog (GtrWindow * window)
   gtk_native_dialog_show (dialog);
 }
 
+void
+gtr_open_file_dialog (GtrWindow *window)
+{
+  g_autoptr (GList) list = NULL;
+  list = get_modified_documents (window);
+  if (list != NULL)
+    gtr_want_to_save_current_dialog (window, gtr_open_file_dialog_nocheck);
+  else
+    gtr_open_file_dialog_nocheck (window);
+}
+
 static void
 save_dialog_response_cb (GtkNativeDialog * dialog,
                          gint response_id, GtrWindow * window)
diff --git a/src/gtr-actions.h b/src/gtr-actions.h
index b6283d3e..695f14f2 100644
--- a/src/gtr-actions.h
+++ b/src/gtr-actions.h
@@ -44,7 +44,7 @@ struct _LastSearchData
   gint wrap_around:1;
 };
 
-gboolean gtr_want_to_save_current_dialog (GtrWindow * window);
+void gtr_want_to_save_current_dialog (GtrWindow * window, void (*callback)(GtrWindow *));
 
 void gtr_open_file_dialog (GtrWindow * window);
 
diff --git a/src/gtr-application.c b/src/gtr-application.c
index 4f08c265..ef1861cc 100644
--- a/src/gtr-application.c
+++ b/src/gtr-application.c
@@ -122,13 +122,13 @@ save_accels (void)
     }
 }
 
-/*static gboolean
+static gboolean
 on_window_delete_event_cb (GtrWindow * window,
                            GdkEvent * event, GtrApplication * app)
 {
   gtr_file_quit (window);
   return TRUE;
-}*/
+}
 
 static void
 set_active_window (GtrApplication *app,
@@ -463,12 +463,9 @@ dl_activated (GSimpleAction *action,
   GtrPoState state = gtr_po_get_state (gtr_tab_get_po (active_tab));
 
   if (state == GTR_PO_STATE_MODIFIED)
-    {
-      if (!gtr_want_to_save_current_dialog (priv->active_window))
-        return;
-    }
-
-  gtr_window_show_dlteams (priv->active_window);
+    gtr_want_to_save_current_dialog (priv->active_window, gtr_window_show_dlteams);
+  else
+    gtr_window_show_dlteams (priv->active_window);
 }
 
 static void
@@ -864,9 +861,10 @@ gtr_application_create_window (GtrApplication *app)
   /* both of these signals are not valid in gtk4
   g_signal_connect (window, "focus_in_event",
                     G_CALLBACK (window_focus_in_event), app);
+  */
 
-  g_signal_connect (window, "delete-event",
-                    G_CALLBACK (on_window_delete_event_cb), app);*/
+  g_signal_connect (window, "close-request",
+                    G_CALLBACK (on_window_delete_event_cb), app);
 
   g_signal_connect (window, "destroy",
                     G_CALLBACK (on_window_destroy_cb), app);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]