[tepl] utils: add util function to associate a secondary window



commit 2f99fa5186d3dbe206a694de0e2ee8584716a5ce
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Nov 25 16:00:58 2017 +0100

    utils: add util function to associate a secondary window

 tepl/tepl-close-confirm-dialog-single.c |   24 ++------------
 tepl/tepl-utils.c                       |   52 +++++++++++++++++++++++++++++++
 tepl/tepl-utils.h                       |    4 ++
 3 files changed, 60 insertions(+), 20 deletions(-)
---
diff --git a/tepl/tepl-close-confirm-dialog-single.c b/tepl/tepl-close-confirm-dialog-single.c
index 8423a0f..6f5a532 100644
--- a/tepl/tepl-close-confirm-dialog-single.c
+++ b/tepl/tepl-close-confirm-dialog-single.c
@@ -20,10 +20,10 @@
 #include "config.h"
 #include "tepl-close-confirm-dialog-single.h"
 #include <glib/gi18n-lib.h>
-#include "tepl-application-window.h"
 #include "tepl-buffer.h"
 #include "tepl-file.h"
 #include "tepl-tab.h"
+#include "tepl-utils.h"
 
 #define CAN_CLOSE (TRUE)
 #define CANNOT_CLOSE (FALSE)
@@ -84,8 +84,6 @@ create_dialog (GTask *task)
        TeplBuffer *buffer;
        TeplFile *file;
        const gchar *file_short_name;
-       GtkWidget *toplevel;
-       GtkWindow *window = NULL;
        GtkWidget *dialog;
 
        tab = g_task_get_source_object (task);
@@ -94,13 +92,7 @@ create_dialog (GTask *task)
 
        file_short_name = tepl_file_get_short_name (file);
 
-       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tab));
-       if (gtk_widget_is_toplevel (toplevel))
-       {
-               window = GTK_WINDOW (toplevel);
-       }
-
-       dialog = gtk_message_dialog_new (window,
+       dialog = gtk_message_dialog_new (NULL,
                                         GTK_DIALOG_DESTROY_WITH_PARENT |
                                         GTK_DIALOG_MODAL,
                                         GTK_MESSAGE_WARNING,
@@ -114,16 +106,8 @@ create_dialog (GTask *task)
                                _("_Save"), GTK_RESPONSE_ACCEPT,
                                NULL);
 
-       if (GTK_IS_APPLICATION_WINDOW (window) &&
-           tepl_application_window_is_main_window (GTK_APPLICATION_WINDOW (window)))
-       {
-               TeplApplicationWindow *tepl_window;
-               GtkWindowGroup *window_group;
-
-               tepl_window = tepl_application_window_get_from_gtk_application_window (GTK_APPLICATION_WINDOW 
(window));
-               window_group = tepl_application_window_get_window_group (tepl_window);
-               gtk_window_group_add_window (window_group, GTK_WINDOW (dialog));
-       }
+       _tepl_utils_associate_secondary_window (GTK_WINDOW (dialog),
+                                               GTK_WIDGET (tab));
 
        g_signal_connect (dialog,
                          "response",
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index ce0cdc9..c707a76 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -24,6 +24,7 @@
 
 #include "tepl-utils.h"
 #include <string.h>
+#include "tepl-application-window.h"
 
 /*
  * SECTION:utils
@@ -385,3 +386,54 @@ _tepl_utils_create_close_button (void)
 
        return close_button;
 }
+
+/* For a secondary window (e.g. a GtkDialog):
+ * - Set transient parent.
+ * - Add it to the GtkWindowGroup.
+ * Just by giving a widget inside the main window.
+ */
+void
+_tepl_utils_associate_secondary_window (GtkWindow *secondary_window,
+                                       GtkWidget *main_window_widget)
+{
+       GtkWidget *toplevel;
+       GtkWindow *main_window = NULL;
+
+       g_return_if_fail (GTK_IS_WINDOW (secondary_window));
+       g_return_if_fail (GTK_IS_WIDGET (main_window_widget));
+
+       /* gtk_widget_get_toplevel() is a bit evil, normally it's a bad practice
+        * when an object is aware of who contains it, i.e. it's fine that a
+        * container knows what it contains (of course) but the reverse is not
+        * true.
+        *
+        * But here it's just to setup correctly e.g. a GtkDialog, it's
+        * something a bit specific to GTK+. As long as this bad practice is
+        * applied only in this case (setting the transient parent and adding
+        * the secondary window to a GtkWindowGroup), it should be fine. It
+        * would be more problematic to call other TeplApplicationWindow
+        * functions.
+        */
+       toplevel = gtk_widget_get_toplevel (main_window_widget);
+       if (gtk_widget_is_toplevel (toplevel))
+       {
+               main_window = GTK_WINDOW (toplevel);
+       }
+
+       if (main_window != NULL)
+       {
+               gtk_window_set_transient_for (secondary_window, main_window);
+       }
+
+       if (GTK_IS_APPLICATION_WINDOW (main_window) &&
+           tepl_application_window_is_main_window (GTK_APPLICATION_WINDOW (main_window)))
+       {
+               TeplApplicationWindow *tepl_window;
+               GtkWindowGroup *window_group;
+
+               tepl_window = tepl_application_window_get_from_gtk_application_window (GTK_APPLICATION_WINDOW 
(main_window));
+
+               window_group = tepl_application_window_get_window_group (tepl_window);
+               gtk_window_group_add_window (window_group, secondary_window);
+       }
+}
diff --git a/tepl/tepl-utils.h b/tepl/tepl-utils.h
index 98c4085..e806387 100644
--- a/tepl/tepl-utils.h
+++ b/tepl/tepl-utils.h
@@ -53,6 +53,10 @@ gchar *              _tepl_utils_get_fallback_basename_for_display   (GFile *location);
 
 GtkWidget *    _tepl_utils_create_close_button                 (void);
 
+G_GNUC_INTERNAL
+void           _tepl_utils_associate_secondary_window          (GtkWindow *secondary_window,
+                                                                GtkWidget *main_window_widget);
+
 G_END_DECLS
 
 #endif /* TEPL_UTILS_H */


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